telephony.scm 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2021, 2022 Maxim Cournoyer <maxim.cournoyer@gmail.com>.
  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. (test-runner-current (system-test-runner #$output))
  125. (test-begin "jami")
  126. (test-assert "service is running"
  127. (marionette-eval
  128. '(begin
  129. (use-modules (gnu services herd))
  130. (match (start-service 'jami)
  131. (#f #f)
  132. (('service response-parts ...)
  133. (match (assq-ref response-parts 'running)
  134. ((pid) (number? pid))))))
  135. marionette))
  136. (test-assert "service can be stopped"
  137. (marionette-eval
  138. '(begin
  139. (use-modules (gnu services herd)
  140. (rnrs base))
  141. (setenv "PATH" "/run/current-system/profile/bin")
  142. (let ((pid (match (start-service 'jami)
  143. (#f #f)
  144. (('service response-parts ...)
  145. (match (assq-ref response-parts 'running)
  146. ((pid) pid))))))
  147. (assert (number? pid))
  148. (match (stop-service 'jami)
  149. (services ;a list of service symbols
  150. (member 'jami services)))
  151. ;; Sometimes, the process still appear in pgrep, even
  152. ;; though we are using waitpid after sending it SIGTERM
  153. ;; in the service; use retries.
  154. (with-retries 20 1
  155. (not (zero? (status:exit-val
  156. (system* "pgrep" "jamid")))))))
  157. marionette))
  158. (test-assert "service can be restarted"
  159. (marionette-eval
  160. '(begin
  161. (use-modules (gnu services herd)
  162. (rnrs base))
  163. ;; Start and retrieve the current PID.
  164. (define pid (match (start-service 'jami)
  165. (#f #f)
  166. (('service response-parts ...)
  167. (match (assq-ref response-parts 'running)
  168. ((pid) pid)))))
  169. (assert (number? pid))
  170. ;; Restart the service.
  171. (restart-service 'jami)
  172. (define new-pid (match (start-service 'jami)
  173. (#f #f)
  174. (('service response-parts ...)
  175. (match (assq-ref response-parts 'running)
  176. ((pid) pid)))))
  177. (assert (number? new-pid))
  178. (not (eq? pid new-pid)))
  179. marionette))
  180. (unless #$provisioning? (test-skip 1))
  181. (test-assert "jami accounts provisioning, account present"
  182. (marionette-eval
  183. '(begin
  184. (use-modules (gnu services herd)
  185. (rnrs base))
  186. ;; Accounts take some time to appear after being added.
  187. (with-retries 20 1
  188. (with-shepherd-action 'jami ('list-accounts) results
  189. (let ((account (assoc-ref (car results) #$username)))
  190. (assert (string=? #$username
  191. (assoc-ref account
  192. "Account.username")))))))
  193. marionette))
  194. (unless #$provisioning? (test-skip 1))
  195. (test-assert "jami accounts provisioning, allowed-contacts"
  196. (marionette-eval
  197. '(begin
  198. (use-modules (gnu services herd)
  199. (rnrs base)
  200. (srfi srfi-1))
  201. ;; Public mode is disabled.
  202. (with-shepherd-action 'jami ('list-account-details)
  203. results
  204. (let ((account (assoc-ref (car results) #$username)))
  205. (assert (string=? "false"
  206. (assoc-ref account
  207. "DHT.PublicInCalls")))))
  208. ;; Allowed contacts match those declared in the configuration.
  209. (with-shepherd-action 'jami ('list-contacts) results
  210. (let ((contacts (assoc-ref (car results) #$username)))
  211. (assert (lset= string-ci=? contacts '#$%allowed-contacts)))))
  212. marionette))
  213. (unless #$provisioning? (test-skip 1))
  214. (test-assert "jami accounts provisioning, moderators"
  215. (marionette-eval
  216. '(begin
  217. (use-modules (gnu services herd)
  218. (rnrs base)
  219. (srfi srfi-1))
  220. ;; Moderators match those declared in the configuration.
  221. (with-shepherd-action 'jami ('list-moderators) results
  222. (let ((moderators (assoc-ref (car results) #$username)))
  223. (assert (lset= string-ci=? moderators '#$%moderators))))
  224. ;; Moderators can be added via the Shepherd action.
  225. (with-shepherd-action 'jami
  226. ('add-moderator "cccccccccccccccccccccccccccccccccccccccc"
  227. #$username) results
  228. (let ((moderators (car results)))
  229. (assert (lset= string-ci=? moderators
  230. (cons "cccccccccccccccccccccccccccccccccccccccc"
  231. '#$%moderators))))))
  232. marionette))
  233. (unless #$provisioning? (test-skip 1))
  234. (test-assert "jami service actions, ban/unban contacts"
  235. (marionette-eval
  236. '(begin
  237. (use-modules (gnu services herd)
  238. (rnrs base)
  239. (srfi srfi-1))
  240. ;; Globally ban a contact.
  241. (with-shepherd-action 'jami
  242. ('ban-contact "1dbcb0f5f37324228235564b79f2b9737e9a008f") _
  243. (with-shepherd-action 'jami ('list-banned-contacts) results
  244. (every (match-lambda
  245. ((username . banned-contacts)
  246. (member "1dbcb0f5f37324228235564b79f2b9737e9a008f"
  247. banned-contacts)))
  248. (car results))))
  249. ;; Ban a contact for a single account.
  250. (with-shepherd-action 'jami
  251. ('ban-contact "dddddddddddddddddddddddddddddddddddddddd"
  252. #$username) _
  253. (with-shepherd-action 'jami ('list-banned-contacts) results
  254. (every (match-lambda
  255. ((username . banned-contacts)
  256. (let ((found? (member "dddddddddddddddddddddddddddddddddddddddd"
  257. banned-contacts)))
  258. (if (string=? #$username username)
  259. found?
  260. (not found?)))))
  261. (car results)))))
  262. marionette))
  263. (unless #$provisioning? (test-skip 1))
  264. (test-assert "jami service actions, enable/disable accounts"
  265. (marionette-eval
  266. '(begin
  267. (use-modules (gnu services herd)
  268. (rnrs base))
  269. (with-shepherd-action 'jami
  270. ('disable-account #$username) _
  271. (with-shepherd-action 'jami ('list-accounts) results
  272. (let ((account (assoc-ref (car results) #$username)))
  273. (assert (string= "false"
  274. (assoc-ref account "Account.enable"))))))
  275. (with-shepherd-action 'jami
  276. ('enable-account #$username) _
  277. (with-shepherd-action 'jami ('list-accounts) results
  278. (let ((account (assoc-ref (car results) #$username)))
  279. (assert (string= "true"
  280. (assoc-ref account "Account.enable")))))))
  281. marionette))
  282. (unless #$provisioning? (test-skip 1))
  283. (test-assert "jami account parameters"
  284. (marionette-eval
  285. '(begin
  286. (use-modules (gnu services herd)
  287. (rnrs base)
  288. (srfi srfi-1))
  289. (with-shepherd-action 'jami ('list-account-details) results
  290. (let ((account-details (assoc-ref (car results)
  291. #$username)))
  292. (assert (lset<=
  293. equal?
  294. '(("Account.hostname" .
  295. "bootstrap.me;fallback.another.host")
  296. ("Account.peerDiscovery" . "false")
  297. ("Account.rendezVous" . "true")
  298. ("RingNS.uri" . "https://my.name.server"))
  299. account-details)))))
  300. marionette))
  301. (test-end))))
  302. (gexp->derivation (if provisioning?
  303. "jami-provisioning-test"
  304. "jami-test")
  305. test))
  306. (define %test-jami
  307. (system-test
  308. (name "jami")
  309. (description "Basic tests for the jami service.")
  310. (value (run-jami-test))))
  311. (define %test-jami-provisioning
  312. (system-test
  313. (name "jami-provisioning")
  314. (description "Provisioning test for the jami service.")
  315. (value (run-jami-test #:provisioning? #t))))
  316. ;; Local Variables:
  317. ;; eval: (put 'with-retries 'scheme-indent-function 2)
  318. ;; End: