services.scm 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2015-2019, 2022 Ludovic Courtès <ludo@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 (test-services)
  19. #:use-module (gnu services)
  20. #:use-module (gnu services herd)
  21. #:use-module (gnu services shepherd)
  22. #:use-module (srfi srfi-1)
  23. #:use-module (srfi srfi-26)
  24. #:use-module (srfi srfi-34)
  25. #:use-module (srfi srfi-64)
  26. #:use-module (ice-9 match))
  27. (test-begin "services")
  28. (test-equal "services, default value"
  29. '(42 123 234 error)
  30. (let* ((t1 (service-type (name 't1) (extensions '())
  31. (description "")))
  32. (t2 (service-type (name 't2) (extensions '())
  33. (description "")
  34. (default-value 42))))
  35. (list (service-value (service t2))
  36. (service-value (service t2 123))
  37. (service-value (service t1 234))
  38. (guard (c ((missing-value-service-error? c) 'error))
  39. (service t1)))))
  40. (test-assert "service-back-edges"
  41. (let* ((t1 (service-type (name 't1) (extensions '()) (description "")
  42. (compose +) (extend *)))
  43. (t2 (service-type (name 't2) (description "")
  44. (extensions
  45. (list (service-extension t1 (const '()))))
  46. (compose +) (extend *)))
  47. (t3 (service-type (name 't3) (description "")
  48. (extensions
  49. (list (service-extension t2 identity)
  50. (service-extension t1 list)))))
  51. (s1 (service t1 #t))
  52. (s2 (service t2 #t))
  53. (s3 (service t3 #t))
  54. (e (service-back-edges (list s1 s2 s3))))
  55. (and (lset= eq? (e s1) (list s2 s3))
  56. (lset= eq? (e s2) (list s3))
  57. (null? (e s3)))))
  58. (test-equal "fold-services"
  59. ;; Make sure 'fold-services' returns the right result. The numbers come
  60. ;; from services of type T3; 'xyz 60' comes from the service of type T2,
  61. ;; where 60 = 15 × 4 = (1 + 2 + 3 + 4 + 5) × 4.
  62. '(initial-value 5 4 3 2 1 xyz 60)
  63. (let* ((t1 (service-type (name 't1) (extensions '()) (description "")
  64. (compose concatenate)
  65. (extend cons)))
  66. (t2 (service-type (name 't2) (description "")
  67. (extensions
  68. (list (service-extension t1
  69. (cut list 'xyz <>))))
  70. (compose (cut reduce + 0 <>))
  71. (extend *)))
  72. (t3 (service-type (name 't3) (description "")
  73. (extensions
  74. (list (service-extension t2 identity)
  75. (service-extension t1 list)))))
  76. (r (fold-services (cons* (service t1 'initial-value)
  77. (service t2 4)
  78. (map (lambda (x)
  79. (service t3 x))
  80. (iota 5 1)))
  81. #:target-type t1)))
  82. (and (eq? (service-kind r) t1)
  83. (service-value r))))
  84. (test-assert "fold-services, ambiguity"
  85. (let* ((t1 (service-type (name 't1) (extensions '()) (description "")
  86. (compose concatenate)
  87. (extend cons)))
  88. (t2 (service-type (name 't2) (description "")
  89. (extensions
  90. (list (service-extension t1 list)))))
  91. (s (service t2 42)))
  92. (guard (c ((ambiguous-target-service-error? c)
  93. (and (eq? (ambiguous-target-service-error-target-type c)
  94. t1)
  95. (eq? (ambiguous-target-service-error-service c)
  96. s))))
  97. (fold-services (list (service t1 'first)
  98. (service t1 'second)
  99. s)
  100. #:target-type t1)
  101. #f)))
  102. (test-assert "fold-services, missing target"
  103. (let* ((t1 (service-type (name 't1) (extensions '()) (description "")))
  104. (t2 (service-type (name 't2) (description "")
  105. (extensions
  106. (list (service-extension t1 list)))))
  107. (s (service t2 42)))
  108. (guard (c ((missing-target-service-error? c)
  109. (and (eq? (missing-target-service-error-target-type c)
  110. t1)
  111. (eq? (missing-target-service-error-service c)
  112. s))))
  113. (fold-services (list s) #:target-type t1)
  114. #f)))
  115. (test-assert "instantiate-missing-services"
  116. (let* ((t1 (service-type (name 't1) (extensions '()) (description "")
  117. (default-value 'dflt)
  118. (compose concatenate)
  119. (extend cons)))
  120. (t2 (service-type (name 't2) (description "")
  121. (extensions
  122. (list (service-extension t1 list)))))
  123. (s1 (service t1 'hey!))
  124. (s2 (service t2 42)))
  125. (and (lset= equal?
  126. (list (service t1) s2)
  127. (instantiate-missing-services (list s2)))
  128. (equal? (list s1 s2)
  129. (instantiate-missing-services (list s1 s2))))))
  130. (test-assert "instantiate-missing-services, indirect"
  131. (let* ((t1 (service-type (name 't1) (extensions '()) (description "")
  132. (default-value 'dflt)
  133. (compose concatenate)
  134. (extend cons)))
  135. (t2 (service-type (name 't2) (description "")
  136. (default-value 'dflt2)
  137. (compose concatenate)
  138. (extend cons)
  139. (extensions
  140. (list (service-extension t1 list)))))
  141. (t3 (service-type (name 't3) (description "")
  142. (extensions
  143. (list (service-extension t2 list)))))
  144. (s1 (service t1))
  145. (s2 (service t2))
  146. (s3 (service t3 42))
  147. (== (cut lset= equal? <...>)))
  148. (and (== (list s1 s2 s3)
  149. (instantiate-missing-services (list s3)))
  150. (== (list s1 s2 s3)
  151. (instantiate-missing-services (list s1 s3)))
  152. (== (list s1 s2 s3)
  153. (instantiate-missing-services (list s2 s3))))))
  154. (test-assert "instantiate-missing-services, no default value"
  155. (let* ((t1 (service-type (name 't1) (extensions '()) (description "")))
  156. (t2 (service-type (name 't2) (description "")
  157. (extensions
  158. (list (service-extension t1 list)))))
  159. (s (service t2 42)))
  160. (guard (c ((missing-target-service-error? c)
  161. (and (eq? (missing-target-service-error-target-type c)
  162. t1)
  163. (eq? (missing-target-service-error-service c)
  164. s))))
  165. (instantiate-missing-services (list s))
  166. #f)))
  167. (test-assert "shepherd-service-lookup-procedure"
  168. (let* ((s1 (shepherd-service (provision '(s1 s1b)) (start #f)))
  169. (s2 (shepherd-service (provision '(s2 s2b)) (start #f)))
  170. (s3 (shepherd-service (provision '(s3 s3b s3c)) (start #f)))
  171. (lookup (shepherd-service-lookup-procedure (list s1 s2 s3))))
  172. (and (eq? (lookup 's1) (lookup 's1b) s1)
  173. (eq? (lookup 's2) (lookup 's2b) s2)
  174. (eq? (lookup 's3) (lookup 's3b) s3))))
  175. (test-assert "shepherd-service-back-edges"
  176. (let* ((s1 (shepherd-service (provision '(s1)) (start #f)))
  177. (s2 (shepherd-service (provision '(s2))
  178. (requirement '(s1))
  179. (start #f)))
  180. (s3 (shepherd-service (provision '(s3))
  181. (requirement '(s1 s2))
  182. (start #f)))
  183. (e (shepherd-service-back-edges (list s1 s2 s3))))
  184. (and (lset= eq? (e s1) (list s2 s3))
  185. (lset= eq? (e s2) (list s3))
  186. (null? (e s3)))))
  187. (test-equal "shepherd-service-upgrade: nothing to do"
  188. '(() ())
  189. (call-with-values
  190. (lambda ()
  191. (shepherd-service-upgrade '() '()))
  192. list))
  193. (test-equal "shepherd-service-upgrade: one unchanged, one upgraded, one new"
  194. '(() ;unload
  195. ((foo))) ;restart
  196. (call-with-values
  197. (lambda ()
  198. ;; Here 'foo' is replaced and must be explicitly restarted later
  199. ;; because it is still running, whereas 'bar' is upgraded right away
  200. ;; because it is not currently running. 'baz' is loaded because it's
  201. ;; a new service.
  202. (shepherd-service-upgrade
  203. (list (live-service '(foo) '() #f #t)
  204. (live-service '(bar) '() #f #f)
  205. (live-service '(root) '() #f #t)) ;essential!
  206. (list (shepherd-service (provision '(foo))
  207. (start #t))
  208. (shepherd-service (provision '(bar))
  209. (start #t))
  210. (shepherd-service (provision '(baz))
  211. (start #t)))))
  212. (lambda (unload restart)
  213. (list (map live-service-provision unload)
  214. (map shepherd-service-provision restart)))))
  215. (test-equal "shepherd-service-upgrade: service depended on is not unloaded"
  216. '(((baz)) ;unload
  217. ((foo))) ;restart
  218. (call-with-values
  219. (lambda ()
  220. ;; Service 'bar' is not among the target services; yet, it must not be
  221. ;; unloaded because 'foo' depends on it. 'foo' gets replaced but it
  222. ;; must be restarted manually.
  223. (shepherd-service-upgrade
  224. (list (live-service '(foo) '(bar) #f #t)
  225. (live-service '(bar) '() #f #t) ;still used!
  226. (live-service '(baz) '() #f #t))
  227. (list (shepherd-service (provision '(foo))
  228. (start #t)))))
  229. (lambda (unload restart)
  230. (list (map live-service-provision unload)
  231. (map shepherd-service-provision restart)))))
  232. (test-equal "shepherd-service-upgrade: obsolete services that depend on each other"
  233. '(((foo) (bar) (baz)) ;unload
  234. ()) ;restart
  235. (call-with-values
  236. (lambda ()
  237. ;; 'foo', 'bar', and 'baz' depend on each other, but all of them are
  238. ;; obsolete, and thus should be unloaded.
  239. (shepherd-service-upgrade
  240. (list (live-service '(foo) '(bar) #f #t) ;obsolete
  241. (live-service '(bar) '(baz) #f #t) ;obsolete
  242. (live-service '(baz) '() #f #t)) ;obsolete
  243. (list (shepherd-service (provision '(qux))
  244. (start #t)))))
  245. (lambda (unload restart)
  246. (list (map live-service-provision unload)
  247. (map shepherd-service-provision restart)))))
  248. (test-equal "shepherd-service-upgrade: transient service"
  249. ;; Transient service must not be unloaded:
  250. ;; <https://issues.guix.gnu.org/54812>.
  251. '(((foo)) ;unload
  252. ((qux))) ;restart
  253. (call-with-values
  254. (lambda ()
  255. (shepherd-service-upgrade
  256. (list (live-service '(sshd-42) '() #t 42) ;transient
  257. (live-service '(foo) '() #f #t) ;obsolete
  258. (live-service '(qux) '() #f #t)) ;running
  259. (list (shepherd-service (provision '(qux))
  260. (start #t)))))
  261. (lambda (unload restart)
  262. (list (map live-service-provision unload)
  263. (map shepherd-service-provision restart)))))
  264. (test-eq "lookup-service-types"
  265. system-service-type
  266. (and (null? (lookup-service-types 'does-not-exist-at-all))
  267. (match (lookup-service-types 'system)
  268. ((one) one)
  269. (x x))))
  270. (test-equal "modify-services: do nothing"
  271. '(1 2 3)
  272. (let* ((t1 (service-type (name 't1)
  273. (extensions '())
  274. (description "")))
  275. (t2 (service-type (name 't2)
  276. (extensions '())
  277. (description "")))
  278. (t3 (service-type (name 't3)
  279. (extensions '())
  280. (description "")))
  281. (services (list (service t1 1) (service t2 2) (service t3 3))))
  282. (sort (map service-value
  283. (modify-services services))
  284. <)))
  285. (test-equal "modify-services: delete service"
  286. '(1)
  287. (let* ((t1 (service-type (name 't1)
  288. (extensions '())
  289. (description "")))
  290. (t2 (service-type (name 't2)
  291. (extensions '())
  292. (description "")))
  293. (t3 (service-type (name 't3)
  294. (extensions '())
  295. (description "")))
  296. (services (list (service t1 1) (service t2 2) (service t3 3))))
  297. (sort (map service-value
  298. (modify-services services
  299. (delete t3)
  300. (delete t2)))
  301. <)))
  302. (test-error "modify-services: delete non-existing service"
  303. #t
  304. (let* ((t1 (service-type (name 't1)
  305. (extensions '())
  306. (description "")))
  307. (t2 (service-type (name 't2)
  308. (extensions '())
  309. (description "")))
  310. (t3 (service-type (name 't2)
  311. (extensions '())
  312. (description "")))
  313. (services (list (service t1 1) (service t2 2))))
  314. (modify-services services
  315. (delete t3))))
  316. (test-equal "modify-services: change value"
  317. '(2 11 33)
  318. (let* ((t1 (service-type (name 't1)
  319. (extensions '())
  320. (description "")))
  321. (t2 (service-type (name 't2)
  322. (extensions '())
  323. (description "")))
  324. (t3 (service-type (name 't3)
  325. (extensions '())
  326. (description "")))
  327. (services (list (service t1 1) (service t2 2) (service t3 3))))
  328. (sort (map service-value
  329. (modify-services services
  330. (t1 value => 11)
  331. (t3 value => 33)))
  332. <)))
  333. (test-error "modify-services: change value for non-existing service"
  334. #t
  335. (let* ((t1 (service-type (name 't1)
  336. (extensions '())
  337. (description "")))
  338. (t2 (service-type (name 't2)
  339. (extensions '())
  340. (description "")))
  341. (t3 (service-type (name 't3)
  342. (extensions '())
  343. (description "")))
  344. (services (list (service t1 1) (service t3 3))))
  345. (map service-value
  346. (modify-services services
  347. (t2 value => 22)))))
  348. (test-end)