services.scm 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2015, 2016, 2017 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. (define live-service
  28. (@@ (gnu services herd) live-service))
  29. (test-begin "services")
  30. (test-equal "services, default value"
  31. '(42 123 234 error)
  32. (let* ((t1 (service-type (name 't1) (extensions '())))
  33. (t2 (service-type (name 't2) (extensions '())
  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 '())
  42. (compose +) (extend *)))
  43. (t2 (service-type (name 't2)
  44. (extensions
  45. (list (service-extension t1 (const '()))))
  46. (compose +) (extend *)))
  47. (t3 (service-type (name 't3)
  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 '())
  64. (compose concatenate)
  65. (extend cons)))
  66. (t2 (service-type (name 't2)
  67. (extensions
  68. (list (service-extension t1
  69. (cut list 'xyz <>))))
  70. (compose (cut reduce + 0 <>))
  71. (extend *)))
  72. (t3 (service-type (name 't3)
  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 '())
  86. (compose concatenate)
  87. (extend cons)))
  88. (t2 (service-type (name 't2)
  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 '())))
  104. (t2 (service-type (name 't2)
  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 "shepherd-service-lookup-procedure"
  116. (let* ((s1 (shepherd-service (provision '(s1 s1b)) (start #f)))
  117. (s2 (shepherd-service (provision '(s2 s2b)) (start #f)))
  118. (s3 (shepherd-service (provision '(s3 s3b s3c)) (start #f)))
  119. (lookup (shepherd-service-lookup-procedure (list s1 s2 s3))))
  120. (and (eq? (lookup 's1) (lookup 's1b) s1)
  121. (eq? (lookup 's2) (lookup 's2b) s2)
  122. (eq? (lookup 's3) (lookup 's3b) s3))))
  123. (test-assert "shepherd-service-back-edges"
  124. (let* ((s1 (shepherd-service (provision '(s1)) (start #f)))
  125. (s2 (shepherd-service (provision '(s2))
  126. (requirement '(s1))
  127. (start #f)))
  128. (s3 (shepherd-service (provision '(s3))
  129. (requirement '(s1 s2))
  130. (start #f)))
  131. (e (shepherd-service-back-edges (list s1 s2 s3))))
  132. (and (lset= eq? (e s1) (list s2 s3))
  133. (lset= eq? (e s2) (list s3))
  134. (null? (e s3)))))
  135. (test-equal "shepherd-service-upgrade: nothing to do"
  136. '(() ())
  137. (call-with-values
  138. (lambda ()
  139. (shepherd-service-upgrade '() '()))
  140. list))
  141. (test-equal "shepherd-service-upgrade: one unchanged, one upgraded, one new"
  142. '(((bar)) ;unload
  143. ((bar) (baz))) ;load
  144. (call-with-values
  145. (lambda ()
  146. ;; Here 'foo' is not upgraded because it is still running, whereas
  147. ;; 'bar' is upgraded because it is not currently running. 'baz' is
  148. ;; loaded because it's a new service.
  149. (shepherd-service-upgrade
  150. (list (live-service '(foo) '() #t)
  151. (live-service '(bar) '() #f)
  152. (live-service '(root) '() #t)) ;essential!
  153. (list (shepherd-service (provision '(foo))
  154. (start #t))
  155. (shepherd-service (provision '(bar))
  156. (start #t))
  157. (shepherd-service (provision '(baz))
  158. (start #t)))))
  159. (lambda (unload load)
  160. (list (map live-service-provision unload)
  161. (map shepherd-service-provision load)))))
  162. (test-equal "shepherd-service-upgrade: service depended on is not unloaded"
  163. '(((baz)) ;unload
  164. ()) ;load
  165. (call-with-values
  166. (lambda ()
  167. ;; Service 'bar' is not among the target services; yet, it must not be
  168. ;; unloaded because 'foo' depends on it.
  169. (shepherd-service-upgrade
  170. (list (live-service '(foo) '(bar) #t)
  171. (live-service '(bar) '() #t) ;still used!
  172. (live-service '(baz) '() #t))
  173. (list (shepherd-service (provision '(foo))
  174. (start #t)))))
  175. (lambda (unload load)
  176. (list (map live-service-provision unload)
  177. (map shepherd-service-provision load)))))
  178. (test-equal "shepherd-service-upgrade: obsolete services that depend on each other"
  179. '(((foo) (bar) (baz)) ;unload
  180. ((qux))) ;load
  181. (call-with-values
  182. (lambda ()
  183. ;; 'foo', 'bar', and 'baz' depend on each other, but all of them are
  184. ;; obsolete, and thus should be unloaded.
  185. (shepherd-service-upgrade
  186. (list (live-service '(foo) '(bar) #t) ;obsolete
  187. (live-service '(bar) '(baz) #t) ;obsolete
  188. (live-service '(baz) '() #t)) ;obsolete
  189. (list (shepherd-service (provision '(qux))
  190. (start #t)))))
  191. (lambda (unload load)
  192. (list (map live-service-provision unload)
  193. (map shepherd-service-provision load)))))
  194. (test-eq "lookup-service-types"
  195. system-service-type
  196. (and (null? (lookup-service-types 'does-not-exist-at-all))
  197. (match (lookup-service-types 'system)
  198. ((one) one)
  199. (x x))))
  200. (test-end)