telephony.scm 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2021 Maxim Cournoyer <maxim.cournoyer@gnu.org>.
  3. ;;;
  4. ;;; This file is part of GNU Guix.
  5. ;;;
  6. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; GNU Guix is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (gnu tests telephony)
  19. #:use-module (gnu)
  20. #:use-module (gnu packages)
  21. #:use-module (gnu packages guile)
  22. #:use-module (gnu tests)
  23. #:use-module (gnu system vm)
  24. #:use-module (gnu services)
  25. #:use-module (gnu services dbus)
  26. #:use-module (gnu services networking)
  27. #:use-module (gnu services ssh)
  28. #:use-module (gnu services telephony)
  29. #:use-module (guix gexp)
  30. #:use-module (guix modules)
  31. #:export (%test-jami
  32. %test-jami-provisioning))
  33. ;;;
  34. ;;; Jami daemon.
  35. ;;;
  36. (include "data/jami-dummy-account.dat") ;defines %jami-account-content-sexp
  37. (define %dummy-jami-account-archive
  38. ;; A Jami account archive is a gzipped JSON file.
  39. (computed-file
  40. "dummy-jami-account.gz"
  41. (with-extensions (list guile-json-4 guile-zlib)
  42. #~(begin
  43. (use-modules (json) (zlib))
  44. (let ((port (open-output-file #$output)))
  45. (call-with-gzip-output-port port
  46. (lambda (port)
  47. (scm->json '#$%jami-account-content-sexp port))))))))
  48. (define %allowed-contacts '("1dbcb0f5f37324228235564b79f2b9737e9a008f"
  49. "2dbcb0f5f37324228235564b79f2b9737e9a008f"))
  50. (define %moderators '("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  51. "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"))
  52. (define %dummy-jami-account (jami-account
  53. (archive %dummy-jami-account-archive)
  54. (allowed-contacts %allowed-contacts)
  55. (moderators %moderators)
  56. (rendezvous-point? #t)
  57. (peer-discovery? #f)
  58. (bootstrap-hostnames '("bootstrap.me"
  59. "fallback.another.host"))
  60. (name-server-uri "https://my.name.server")))
  61. (define* (make-jami-os #:key provisioning?)
  62. (operating-system
  63. (host-name "jami")
  64. (timezone "America/Montreal")
  65. (locale "en_US.UTF-8")
  66. (bootloader (bootloader-configuration
  67. (bootloader grub-bootloader)
  68. (targets '("/dev/sdX"))))
  69. (file-systems (cons (file-system
  70. (device (file-system-label "my-root"))
  71. (mount-point "/")
  72. (type "ext4"))
  73. %base-file-systems))
  74. (firmware '())
  75. (services (cons* (service jami-service-type
  76. (if provisioning?
  77. (jami-configuration
  78. (debug? #t)
  79. (accounts (list %dummy-jami-account)))
  80. (jami-configuration
  81. (debug? #t))))
  82. (service dbus-root-service-type)
  83. ;; The following services/packages are added for
  84. ;; debugging purposes.
  85. (service dhcp-client-service-type)
  86. (service openssh-service-type
  87. (openssh-configuration
  88. (permit-root-login #t)
  89. (allow-empty-passwords? #t)))
  90. %base-services))
  91. (packages (cons* (specification->package "recutils")
  92. (specification->package "strace")
  93. %base-packages))))
  94. (define %jami-os
  95. (make-jami-os))
  96. (define %jami-os-provisioning
  97. (make-jami-os #:provisioning? #t))
  98. (define* (run-jami-test #:key provisioning?)
  99. "Run tests in %JAMI-OS. When PROVISIONING? is true, test the
  100. accounts provisioning feature of the service."
  101. (define os (marionette-operating-system
  102. (if provisioning?
  103. %jami-os-provisioning
  104. %jami-os)
  105. #:imported-modules '((gnu services herd)
  106. (guix combinators))))
  107. (define vm (virtual-machine
  108. (operating-system os)
  109. (memory-size 512)))
  110. (define username (assoc-ref %jami-account-content-sexp
  111. "Account.username"))
  112. (define test
  113. (with-imported-modules (source-module-closure
  114. '((gnu build marionette)
  115. (gnu build jami-service)))
  116. #~(begin
  117. (use-modules (rnrs base)
  118. (srfi srfi-11)
  119. (srfi srfi-64)
  120. (gnu build marionette)
  121. (gnu build jami-service))
  122. (define marionette
  123. (make-marionette (list #$vm)))
  124. (mkdir #$output)
  125. (chdir #$output)
  126. (test-begin "jami")
  127. (test-assert "service is running"
  128. (marionette-eval
  129. '(begin
  130. (use-modules (gnu services herd))
  131. (match (start-service 'jami)
  132. (#f #f)
  133. (('service response-parts ...)
  134. (match (assq-ref response-parts 'running)
  135. ((pid) (number? pid))))))
  136. marionette))
  137. (test-assert "service can be stopped"
  138. (marionette-eval
  139. '(begin
  140. (use-modules (gnu services herd)
  141. (rnrs base))
  142. (setenv "PATH" "/run/current-system/profile/bin")
  143. (let ((pid (match (start-service 'jami)
  144. (#f #f)
  145. (('service response-parts ...)
  146. (match (assq-ref response-parts 'running)
  147. ((pid) pid))))))
  148. (assert (number? pid))
  149. (match (stop-service 'jami)
  150. (services ;a list of service symbols
  151. (member 'jami services)))
  152. ;; Sometimes, the process still appear in pgrep, even
  153. ;; though we are using waitpid after sending it SIGTERM
  154. ;; in the service; use retries.
  155. (with-retries 20 1
  156. (not (zero? (status:exit-val
  157. (system* "pgrep" "dring")))))))
  158. marionette))
  159. (test-assert "service can be restarted"
  160. (marionette-eval
  161. '(begin
  162. (use-modules (gnu services herd)
  163. (rnrs base))
  164. ;; Start and retrieve the current PID.
  165. (define pid (match (start-service 'jami)
  166. (#f #f)
  167. (('service response-parts ...)
  168. (match (assq-ref response-parts 'running)
  169. ((pid) pid)))))
  170. (assert (number? pid))
  171. ;; Restart the service.
  172. (restart-service 'jami)
  173. (define new-pid (match (start-service 'jami)
  174. (#f #f)
  175. (('service response-parts ...)
  176. (match (assq-ref response-parts 'running)
  177. ((pid) pid)))))
  178. (assert (number? new-pid))
  179. (not (eq? pid new-pid)))
  180. marionette))
  181. (unless #$provisioning? (test-skip 1))
  182. (test-assert "jami accounts provisioning, account present"
  183. (marionette-eval
  184. '(begin
  185. (use-modules (gnu services herd)
  186. (rnrs base))
  187. ;; Accounts take some time to appear after being added.
  188. (with-retries 20 1
  189. (with-shepherd-action 'jami ('list-accounts) results
  190. (let ((account (assoc-ref (car results) #$username)))
  191. (assert (string=? #$username
  192. (assoc-ref account
  193. "Account.username")))))))
  194. marionette))
  195. (unless #$provisioning? (test-skip 1))
  196. (test-assert "jami accounts provisioning, allowed-contacts"
  197. (marionette-eval
  198. '(begin
  199. (use-modules (gnu services herd)
  200. (rnrs base)
  201. (srfi srfi-1))
  202. ;; Public mode is disabled.
  203. (with-shepherd-action 'jami ('list-account-details)
  204. results
  205. (let ((account (assoc-ref (car results) #$username)))
  206. (assert (string=? "false"
  207. (assoc-ref account
  208. "DHT.PublicInCalls")))))
  209. ;; Allowed contacts match those declared in the configuration.
  210. (with-shepherd-action 'jami ('list-contacts) results
  211. (let ((contacts (assoc-ref (car results) #$username)))
  212. (assert (lset= string-ci=? contacts '#$%allowed-contacts)))))
  213. marionette))
  214. (unless #$provisioning? (test-skip 1))
  215. (test-assert "jami accounts provisioning, moderators"
  216. (marionette-eval
  217. '(begin
  218. (use-modules (gnu services herd)
  219. (rnrs base)
  220. (srfi srfi-1))
  221. ;; Moderators match those declared in the configuration.
  222. (with-shepherd-action 'jami ('list-moderators) results
  223. (let ((moderators (assoc-ref (car results) #$username)))
  224. (assert (lset= string-ci=? moderators '#$%moderators))))
  225. ;; Moderators can be added via the Shepherd action.
  226. (with-shepherd-action 'jami
  227. ('add-moderator "cccccccccccccccccccccccccccccccccccccccc"
  228. #$username) results
  229. (let ((moderators (car results)))
  230. (assert (lset= string-ci=? moderators
  231. (cons "cccccccccccccccccccccccccccccccccccccccc"
  232. '#$%moderators))))))
  233. marionette))
  234. (unless #$provisioning? (test-skip 1))
  235. (test-assert "jami service actions, ban/unban contacts"
  236. (marionette-eval
  237. '(begin
  238. (use-modules (gnu services herd)
  239. (rnrs base)
  240. (srfi srfi-1))
  241. ;; Globally ban a contact.
  242. (with-shepherd-action 'jami
  243. ('ban-contact "1dbcb0f5f37324228235564b79f2b9737e9a008f") _
  244. (with-shepherd-action 'jami ('list-banned-contacts) results
  245. (every (match-lambda
  246. ((username . banned-contacts)
  247. (member "1dbcb0f5f37324228235564b79f2b9737e9a008f"
  248. banned-contacts)))
  249. (car results))))
  250. ;; Ban a contact for a single account.
  251. (with-shepherd-action 'jami
  252. ('ban-contact "dddddddddddddddddddddddddddddddddddddddd"
  253. #$username) _
  254. (with-shepherd-action 'jami ('list-banned-contacts) results
  255. (every (match-lambda
  256. ((username . banned-contacts)
  257. (let ((found? (member "dddddddddddddddddddddddddddddddddddddddd"
  258. banned-contacts)))
  259. (if (string=? #$username username)
  260. found?
  261. (not found?)))))
  262. (car results)))))
  263. marionette))
  264. (unless #$provisioning? (test-skip 1))
  265. (test-assert "jami service actions, enable/disable accounts"
  266. (marionette-eval
  267. '(begin
  268. (use-modules (gnu services herd)
  269. (rnrs base))
  270. (with-shepherd-action 'jami
  271. ('disable-account #$username) _
  272. (with-shepherd-action 'jami ('list-accounts) results
  273. (let ((account (assoc-ref (car results) #$username)))
  274. (assert (string= "false"
  275. (assoc-ref account "Account.enable"))))))
  276. (with-shepherd-action 'jami
  277. ('enable-account #$username) _
  278. (with-shepherd-action 'jami ('list-accounts) results
  279. (let ((account (assoc-ref (car results) #$username)))
  280. (assert (string= "true"
  281. (assoc-ref account "Account.enable")))))))
  282. marionette))
  283. (unless #$provisioning? (test-skip 1))
  284. (test-assert "jami account parameters"
  285. (marionette-eval
  286. '(begin
  287. (use-modules (gnu services herd)
  288. (rnrs base)
  289. (srfi srfi-1))
  290. (with-shepherd-action 'jami ('list-account-details) results
  291. (let ((account-details (assoc-ref (car results)
  292. #$username)))
  293. (assert (lset<=
  294. equal?
  295. '(("Account.hostname" .
  296. "bootstrap.me;fallback.another.host")
  297. ("Account.peerDiscovery" . "false")
  298. ("Account.rendezVous" . "true")
  299. ("RingNS.uri" . "https://my.name.server"))
  300. account-details)))))
  301. marionette))
  302. (test-end)
  303. (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
  304. (gexp->derivation (if provisioning?
  305. "jami-provisioning-test"
  306. "jami-test")
  307. test))
  308. (define %test-jami
  309. (system-test
  310. (name "jami")
  311. (description "Basic tests for the jami service.")
  312. (value (run-jami-test))))
  313. (define %test-jami-provisioning
  314. (system-test
  315. (name "jami-provisioning")
  316. (description "Provisioning test for the jami service.")
  317. (value (run-jami-test #:provisioning? #t))))
  318. ;; Local Variables:
  319. ;; eval: (put 'with-retries 'scheme-indent-function 2)
  320. ;; End: