services: Service types can now specify a default value for instances.
[jackhill/guix/guix.git] / tests / services.scm
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
19 (define-module (test-services)
20 #:use-module (gnu services)
21 #:use-module (gnu services herd)
22 #:use-module (gnu services shepherd)
23 #:use-module (srfi srfi-1)
24 #:use-module (srfi srfi-26)
25 #:use-module (srfi srfi-34)
26 #:use-module (srfi srfi-64))
27
28 (define live-service
29 (@@ (gnu services herd) live-service))
30
31 \f
32 (test-begin "services")
33
34 (test-equal "services, default value"
35 '(42 123 234 error)
36 (let* ((t1 (service-type (name 't1) (extensions '())))
37 (t2 (service-type (name 't2) (extensions '())
38 (default-value 42))))
39 (list (service-value (service t2))
40 (service-value (service t2 123))
41 (service-value (service t1 234))
42 (guard (c ((missing-value-service-error? c) 'error))
43 (service t1)))))
44
45 (test-assert "service-back-edges"
46 (let* ((t1 (service-type (name 't1) (extensions '())
47 (compose +) (extend *)))
48 (t2 (service-type (name 't2)
49 (extensions
50 (list (service-extension t1 (const '()))))
51 (compose +) (extend *)))
52 (t3 (service-type (name 't3)
53 (extensions
54 (list (service-extension t2 identity)
55 (service-extension t1 list)))))
56 (s1 (service t1 #t))
57 (s2 (service t2 #t))
58 (s3 (service t3 #t))
59 (e (service-back-edges (list s1 s2 s3))))
60 (and (lset= eq? (e s1) (list s2 s3))
61 (lset= eq? (e s2) (list s3))
62 (null? (e s3)))))
63
64 (test-equal "fold-services"
65 ;; Make sure 'fold-services' returns the right result. The numbers come
66 ;; from services of type T3; 'xyz 60' comes from the service of type T2,
67 ;; where 60 = 15 × 4 = (1 + 2 + 3 + 4 + 5) × 4.
68 '(initial-value 5 4 3 2 1 xyz 60)
69 (let* ((t1 (service-type (name 't1) (extensions '())
70 (compose concatenate)
71 (extend cons)))
72 (t2 (service-type (name 't2)
73 (extensions
74 (list (service-extension t1
75 (cut list 'xyz <>))))
76 (compose (cut reduce + 0 <>))
77 (extend *)))
78 (t3 (service-type (name 't3)
79 (extensions
80 (list (service-extension t2 identity)
81 (service-extension t1 list)))))
82 (r (fold-services (cons* (service t1 'initial-value)
83 (service t2 4)
84 (map (lambda (x)
85 (service t3 x))
86 (iota 5 1)))
87 #:target-type t1)))
88 (and (eq? (service-kind r) t1)
89 (service-value r))))
90
91 (test-assert "fold-services, ambiguity"
92 (let* ((t1 (service-type (name 't1) (extensions '())
93 (compose concatenate)
94 (extend cons)))
95 (t2 (service-type (name 't2)
96 (extensions
97 (list (service-extension t1 list)))))
98 (s (service t2 42)))
99 (guard (c ((ambiguous-target-service-error? c)
100 (and (eq? (ambiguous-target-service-error-target-type c)
101 t1)
102 (eq? (ambiguous-target-service-error-service c)
103 s))))
104 (fold-services (list (service t1 'first)
105 (service t1 'second)
106 s)
107 #:target-type t1)
108 #f)))
109
110 (test-assert "fold-services, missing target"
111 (let* ((t1 (service-type (name 't1) (extensions '())))
112 (t2 (service-type (name 't2)
113 (extensions
114 (list (service-extension t1 list)))))
115 (s (service t2 42)))
116 (guard (c ((missing-target-service-error? c)
117 (and (eq? (missing-target-service-error-target-type c)
118 t1)
119 (eq? (missing-target-service-error-service c)
120 s))))
121 (fold-services (list s) #:target-type t1)
122 #f)))
123
124 (test-assert "shepherd-service-lookup-procedure"
125 (let* ((s1 (shepherd-service (provision '(s1 s1b)) (start #f)))
126 (s2 (shepherd-service (provision '(s2 s2b)) (start #f)))
127 (s3 (shepherd-service (provision '(s3 s3b s3c)) (start #f)))
128 (lookup (shepherd-service-lookup-procedure (list s1 s2 s3))))
129 (and (eq? (lookup 's1) (lookup 's1b) s1)
130 (eq? (lookup 's2) (lookup 's2b) s2)
131 (eq? (lookup 's3) (lookup 's3b) s3))))
132
133 (test-assert "shepherd-service-back-edges"
134 (let* ((s1 (shepherd-service (provision '(s1)) (start #f)))
135 (s2 (shepherd-service (provision '(s2))
136 (requirement '(s1))
137 (start #f)))
138 (s3 (shepherd-service (provision '(s3))
139 (requirement '(s1 s2))
140 (start #f)))
141 (e (shepherd-service-back-edges (list s1 s2 s3))))
142 (and (lset= eq? (e s1) (list s2 s3))
143 (lset= eq? (e s2) (list s3))
144 (null? (e s3)))))
145
146 (test-equal "shepherd-service-upgrade: nothing to do"
147 '(() ())
148 (call-with-values
149 (lambda ()
150 (shepherd-service-upgrade '() '()))
151 list))
152
153 (test-equal "shepherd-service-upgrade: one unchanged, one upgraded, one new"
154 '(((bar)) ;unload
155 ((bar) (baz))) ;load
156 (call-with-values
157 (lambda ()
158 ;; Here 'foo' is not upgraded because it is still running, whereas
159 ;; 'bar' is upgraded because it is not currently running. 'baz' is
160 ;; loaded because it's a new service.
161 (shepherd-service-upgrade
162 (list (live-service '(foo) '() #t)
163 (live-service '(bar) '() #f)
164 (live-service '(root) '() #t)) ;essential!
165 (list (shepherd-service (provision '(foo))
166 (start #t))
167 (shepherd-service (provision '(bar))
168 (start #t))
169 (shepherd-service (provision '(baz))
170 (start #t)))))
171 (lambda (unload load)
172 (list (map live-service-provision unload)
173 (map shepherd-service-provision load)))))
174
175 (test-equal "shepherd-service-upgrade: service depended on is not unloaded"
176 '(((baz)) ;unload
177 ()) ;load
178 (call-with-values
179 (lambda ()
180 ;; Service 'bar' is not among the target services; yet, it must not be
181 ;; unloaded because 'foo' depends on it.
182 (shepherd-service-upgrade
183 (list (live-service '(foo) '(bar) #t)
184 (live-service '(bar) '() #t) ;still used!
185 (live-service '(baz) '() #t))
186 (list (shepherd-service (provision '(foo))
187 (start #t)))))
188 (lambda (unload load)
189 (list (map live-service-provision unload)
190 (map shepherd-service-provision load)))))
191
192 (test-equal "shepherd-service-upgrade: obsolete services that depend on each other"
193 '(((foo) (bar) (baz)) ;unload
194 ((qux))) ;load
195 (call-with-values
196 (lambda ()
197 ;; 'foo', 'bar', and 'baz' depend on each other, but all of them are
198 ;; obsolete, and thus should be unloaded.
199 (shepherd-service-upgrade
200 (list (live-service '(foo) '(bar) #t) ;obsolete
201 (live-service '(bar) '(baz) #t) ;obsolete
202 (live-service '(baz) '() #t)) ;obsolete
203 (list (shepherd-service (provision '(qux))
204 (start #t)))))
205 (lambda (unload load)
206 (list (map live-service-provision unload)
207 (map shepherd-service-provision load)))))
208
209 (test-end)