services.scm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2015, 2016, 2017, 2018, 2019 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. (t2 (service-type (name 't2) (extensions '())
  32. (default-value 42))))
  33. (list (service-value (service t2))
  34. (service-value (service t2 123))
  35. (service-value (service t1 234))
  36. (guard (c ((missing-value-service-error? c) 'error))
  37. (service t1)))))
  38. (test-assert "service-back-edges"
  39. (let* ((t1 (service-type (name 't1) (extensions '())
  40. (compose +) (extend *)))
  41. (t2 (service-type (name 't2)
  42. (extensions
  43. (list (service-extension t1 (const '()))))
  44. (compose +) (extend *)))
  45. (t3 (service-type (name 't3)
  46. (extensions
  47. (list (service-extension t2 identity)
  48. (service-extension t1 list)))))
  49. (s1 (service t1 #t))
  50. (s2 (service t2 #t))
  51. (s3 (service t3 #t))
  52. (e (service-back-edges (list s1 s2 s3))))
  53. (and (lset= eq? (e s1) (list s2 s3))
  54. (lset= eq? (e s2) (list s3))
  55. (null? (e s3)))))
  56. (test-equal "fold-services"
  57. ;; Make sure 'fold-services' returns the right result. The numbers come
  58. ;; from services of type T3; 'xyz 60' comes from the service of type T2,
  59. ;; where 60 = 15 × 4 = (1 + 2 + 3 + 4 + 5) × 4.
  60. '(initial-value 5 4 3 2 1 xyz 60)
  61. (let* ((t1 (service-type (name 't1) (extensions '())
  62. (compose concatenate)
  63. (extend cons)))
  64. (t2 (service-type (name 't2)
  65. (extensions
  66. (list (service-extension t1
  67. (cut list 'xyz <>))))
  68. (compose (cut reduce + 0 <>))
  69. (extend *)))
  70. (t3 (service-type (name 't3)
  71. (extensions
  72. (list (service-extension t2 identity)
  73. (service-extension t1 list)))))
  74. (r (fold-services (cons* (service t1 'initial-value)
  75. (service t2 4)
  76. (map (lambda (x)
  77. (service t3 x))
  78. (iota 5 1)))
  79. #:target-type t1)))
  80. (and (eq? (service-kind r) t1)
  81. (service-value r))))
  82. (test-assert "fold-services, ambiguity"
  83. (let* ((t1 (service-type (name 't1) (extensions '())
  84. (compose concatenate)
  85. (extend cons)))
  86. (t2 (service-type (name 't2)
  87. (extensions
  88. (list (service-extension t1 list)))))
  89. (s (service t2 42)))
  90. (guard (c ((ambiguous-target-service-error? c)
  91. (and (eq? (ambiguous-target-service-error-target-type c)
  92. t1)
  93. (eq? (ambiguous-target-service-error-service c)
  94. s))))
  95. (fold-services (list (service t1 'first)
  96. (service t1 'second)
  97. s)
  98. #:target-type t1)
  99. #f)))
  100. (test-assert "fold-services, missing target"
  101. (let* ((t1 (service-type (name 't1) (extensions '())))
  102. (t2 (service-type (name 't2)
  103. (extensions
  104. (list (service-extension t1 list)))))
  105. (s (service t2 42)))
  106. (guard (c ((missing-target-service-error? c)
  107. (and (eq? (missing-target-service-error-target-type c)
  108. t1)
  109. (eq? (missing-target-service-error-service c)
  110. s))))
  111. (fold-services (list s) #:target-type t1)
  112. #f)))
  113. (test-assert "instantiate-missing-services"
  114. (let* ((t1 (service-type (name 't1) (extensions '())
  115. (default-value 'dflt)
  116. (compose concatenate)
  117. (extend cons)))
  118. (t2 (service-type (name 't2)
  119. (extensions
  120. (list (service-extension t1 list)))))
  121. (s1 (service t1 'hey!))
  122. (s2 (service t2 42)))
  123. (and (lset= equal?
  124. (list (service t1) s2)
  125. (instantiate-missing-services (list s2)))
  126. (equal? (list s1 s2)
  127. (instantiate-missing-services (list s1 s2))))))
  128. (test-assert "instantiate-missing-services, indirect"
  129. (let* ((t1 (service-type (name 't1) (extensions '())
  130. (default-value 'dflt)
  131. (compose concatenate)
  132. (extend cons)))
  133. (t2 (service-type (name 't2)
  134. (default-value 'dflt2)
  135. (compose concatenate)
  136. (extend cons)
  137. (extensions
  138. (list (service-extension t1 list)))))
  139. (t3 (service-type (name 't3)
  140. (extensions
  141. (list (service-extension t2 list)))))
  142. (s1 (service t1))
  143. (s2 (service t2))
  144. (s3 (service t3 42))
  145. (== (cut lset= equal? <...>)))
  146. (and (== (list s1 s2 s3)
  147. (instantiate-missing-services (list s3)))
  148. (== (list s1 s2 s3)
  149. (instantiate-missing-services (list s1 s3)))
  150. (== (list s1 s2 s3)
  151. (instantiate-missing-services (list s2 s3))))))
  152. (test-assert "instantiate-missing-services, no default value"
  153. (let* ((t1 (service-type (name 't1) (extensions '())))
  154. (t2 (service-type (name 't2)
  155. (extensions
  156. (list (service-extension t1 list)))))
  157. (s (service t2 42)))
  158. (guard (c ((missing-target-service-error? c)
  159. (and (eq? (missing-target-service-error-target-type c)
  160. t1)
  161. (eq? (missing-target-service-error-service c)
  162. s))))
  163. (instantiate-missing-services (list s))
  164. #f)))
  165. (test-assert "shepherd-service-lookup-procedure"
  166. (let* ((s1 (shepherd-service (provision '(s1 s1b)) (start #f)))
  167. (s2 (shepherd-service (provision '(s2 s2b)) (start #f)))
  168. (s3 (shepherd-service (provision '(s3 s3b s3c)) (start #f)))
  169. (lookup (shepherd-service-lookup-procedure (list s1 s2 s3))))
  170. (and (eq? (lookup 's1) (lookup 's1b) s1)
  171. (eq? (lookup 's2) (lookup 's2b) s2)
  172. (eq? (lookup 's3) (lookup 's3b) s3))))
  173. (test-assert "shepherd-service-back-edges"
  174. (let* ((s1 (shepherd-service (provision '(s1)) (start #f)))
  175. (s2 (shepherd-service (provision '(s2))
  176. (requirement '(s1))
  177. (start #f)))
  178. (s3 (shepherd-service (provision '(s3))
  179. (requirement '(s1 s2))
  180. (start #f)))
  181. (e (shepherd-service-back-edges (list s1 s2 s3))))
  182. (and (lset= eq? (e s1) (list s2 s3))
  183. (lset= eq? (e s2) (list s3))
  184. (null? (e s3)))))
  185. (test-equal "shepherd-service-upgrade: nothing to do"
  186. '(() ())
  187. (call-with-values
  188. (lambda ()
  189. (shepherd-service-upgrade '() '()))
  190. list))
  191. (test-equal "shepherd-service-upgrade: one unchanged, one upgraded, one new"
  192. '(() ;unload
  193. ((foo))) ;restart
  194. (call-with-values
  195. (lambda ()
  196. ;; Here 'foo' is replaced and must be explicitly restarted later
  197. ;; because it is still running, whereas 'bar' is upgraded right away
  198. ;; because it is not currently running. 'baz' is loaded because it's
  199. ;; a new service.
  200. (shepherd-service-upgrade
  201. (list (live-service '(foo) '() #t)
  202. (live-service '(bar) '() #f)
  203. (live-service '(root) '() #t)) ;essential!
  204. (list (shepherd-service (provision '(foo))
  205. (start #t))
  206. (shepherd-service (provision '(bar))
  207. (start #t))
  208. (shepherd-service (provision '(baz))
  209. (start #t)))))
  210. (lambda (unload restart)
  211. (list (map live-service-provision unload)
  212. (map shepherd-service-provision restart)))))
  213. (test-equal "shepherd-service-upgrade: service depended on is not unloaded"
  214. '(((baz)) ;unload
  215. ((foo))) ;restart
  216. (call-with-values
  217. (lambda ()
  218. ;; Service 'bar' is not among the target services; yet, it must not be
  219. ;; unloaded because 'foo' depends on it. 'foo' gets replaced but it
  220. ;; must be restarted manually.
  221. (shepherd-service-upgrade
  222. (list (live-service '(foo) '(bar) #t)
  223. (live-service '(bar) '() #t) ;still used!
  224. (live-service '(baz) '() #t))
  225. (list (shepherd-service (provision '(foo))
  226. (start #t)))))
  227. (lambda (unload restart)
  228. (list (map live-service-provision unload)
  229. (map shepherd-service-provision restart)))))
  230. (test-equal "shepherd-service-upgrade: obsolete services that depend on each other"
  231. '(((foo) (bar) (baz)) ;unload
  232. ()) ;restart
  233. (call-with-values
  234. (lambda ()
  235. ;; 'foo', 'bar', and 'baz' depend on each other, but all of them are
  236. ;; obsolete, and thus should be unloaded.
  237. (shepherd-service-upgrade
  238. (list (live-service '(foo) '(bar) #t) ;obsolete
  239. (live-service '(bar) '(baz) #t) ;obsolete
  240. (live-service '(baz) '() #t)) ;obsolete
  241. (list (shepherd-service (provision '(qux))
  242. (start #t)))))
  243. (lambda (unload restart)
  244. (list (map live-service-provision unload)
  245. (map shepherd-service-provision restart)))))
  246. (test-eq "lookup-service-types"
  247. system-service-type
  248. (and (null? (lookup-service-types 'does-not-exist-at-all))
  249. (match (lookup-service-types 'system)
  250. ((one) one)
  251. (x x))))
  252. (test-end)