1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2015, 2016, 2017, 2018, 2019 Ludovic Courtès <ludo@gnu.org>
4 ;;; This file is part of GNU Guix.
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.
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.
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/>.
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 #:use-module (ice-9 match))
30 (@@ (gnu services herd) live-service))
33 (test-begin "services")
35 (test-equal "services, default value"
37 (let* ((t1 (service-type (name 't1) (extensions '())))
38 (t2 (service-type (name 't2) (extensions '())
40 (list (service-value (service t2))
41 (service-value (service t2 123))
42 (service-value (service t1 234))
43 (guard (c ((missing-value-service-error? c) 'error))
46 (test-assert "service-back-edges"
47 (let* ((t1 (service-type (name 't1) (extensions '())
48 (compose +) (extend *)))
49 (t2 (service-type (name 't2)
51 (list (service-extension t1 (const '()))))
52 (compose +) (extend *)))
53 (t3 (service-type (name 't3)
55 (list (service-extension t2 identity)
56 (service-extension t1 list)))))
60 (e (service-back-edges (list s1 s2 s3))))
61 (and (lset= eq? (e s1) (list s2 s3))
62 (lset= eq? (e s2) (list s3))
65 (test-equal "fold-services"
66 ;; Make sure 'fold-services' returns the right result. The numbers come
67 ;; from services of type T3; 'xyz 60' comes from the service of type T2,
68 ;; where 60 = 15 × 4 = (1 + 2 + 3 + 4 + 5) × 4.
69 '(initial-value 5 4 3 2 1 xyz 60)
70 (let* ((t1 (service-type (name 't1) (extensions '())
73 (t2 (service-type (name 't2)
75 (list (service-extension t1
77 (compose (cut reduce + 0 <>))
79 (t3 (service-type (name 't3)
81 (list (service-extension t2 identity)
82 (service-extension t1 list)))))
83 (r (fold-services (cons* (service t1 'initial-value)
89 (and (eq? (service-kind r) t1)
92 (test-assert "fold-services, ambiguity"
93 (let* ((t1 (service-type (name 't1) (extensions '())
96 (t2 (service-type (name 't2)
98 (list (service-extension t1 list)))))
100 (guard (c ((ambiguous-target-service-error? c)
101 (and (eq? (ambiguous-target-service-error-target-type c)
103 (eq? (ambiguous-target-service-error-service c)
105 (fold-services (list (service t1 'first)
111 (test-assert "fold-services, missing target"
112 (let* ((t1 (service-type (name 't1) (extensions '())))
113 (t2 (service-type (name 't2)
115 (list (service-extension t1 list)))))
117 (guard (c ((missing-target-service-error? c)
118 (and (eq? (missing-target-service-error-target-type c)
120 (eq? (missing-target-service-error-service c)
122 (fold-services (list s) #:target-type t1)
125 (test-assert "instantiate-missing-services"
126 (let* ((t1 (service-type (name 't1) (extensions '())
127 (default-value 'dflt)
128 (compose concatenate)
130 (t2 (service-type (name 't2)
132 (list (service-extension t1 list)))))
133 (s1 (service t1 'hey!))
134 (s2 (service t2 42)))
136 (list (service t1) s2)
137 (instantiate-missing-services (list s2)))
139 (instantiate-missing-services (list s1 s2))))))
141 (test-assert "instantiate-missing-services, indirect"
142 (let* ((t1 (service-type (name 't1) (extensions '())
143 (default-value 'dflt)
144 (compose concatenate)
146 (t2 (service-type (name 't2)
147 (default-value 'dflt2)
148 (compose concatenate)
151 (list (service-extension t1 list)))))
152 (t3 (service-type (name 't3)
154 (list (service-extension t2 list)))))
158 (== (cut lset= equal? <...>)))
159 (and (== (list s1 s2 s3)
160 (instantiate-missing-services (list s3)))
162 (instantiate-missing-services (list s1 s3)))
164 (instantiate-missing-services (list s2 s3))))))
166 (test-assert "instantiate-missing-services, no default value"
167 (let* ((t1 (service-type (name 't1) (extensions '())))
168 (t2 (service-type (name 't2)
170 (list (service-extension t1 list)))))
172 (guard (c ((missing-target-service-error? c)
173 (and (eq? (missing-target-service-error-target-type c)
175 (eq? (missing-target-service-error-service c)
177 (instantiate-missing-services (list s))
180 (test-assert "shepherd-service-lookup-procedure"
181 (let* ((s1 (shepherd-service (provision '(s1 s1b)) (start #f)))
182 (s2 (shepherd-service (provision '(s2 s2b)) (start #f)))
183 (s3 (shepherd-service (provision '(s3 s3b s3c)) (start #f)))
184 (lookup (shepherd-service-lookup-procedure (list s1 s2 s3))))
185 (and (eq? (lookup 's1) (lookup 's1b) s1)
186 (eq? (lookup 's2) (lookup 's2b) s2)
187 (eq? (lookup 's3) (lookup 's3b) s3))))
189 (test-assert "shepherd-service-back-edges"
190 (let* ((s1 (shepherd-service (provision '(s1)) (start #f)))
191 (s2 (shepherd-service (provision '(s2))
194 (s3 (shepherd-service (provision '(s3))
195 (requirement '(s1 s2))
197 (e (shepherd-service-back-edges (list s1 s2 s3))))
198 (and (lset= eq? (e s1) (list s2 s3))
199 (lset= eq? (e s2) (list s3))
202 (test-equal "shepherd-service-upgrade: nothing to do"
206 (shepherd-service-upgrade '() '()))
209 (test-equal "shepherd-service-upgrade: one unchanged, one upgraded, one new"
214 ;; Here 'foo' is replaced and must be explicitly restarted later
215 ;; because it is still running, whereas 'bar' is upgraded right away
216 ;; because it is not currently running. 'baz' is loaded because it's
218 (shepherd-service-upgrade
219 (list (live-service '(foo) '() #t)
220 (live-service '(bar) '() #f)
221 (live-service '(root) '() #t)) ;essential!
222 (list (shepherd-service (provision '(foo))
224 (shepherd-service (provision '(bar))
226 (shepherd-service (provision '(baz))
228 (lambda (unload restart)
229 (list (map live-service-provision unload)
230 (map shepherd-service-provision restart)))))
232 (test-equal "shepherd-service-upgrade: service depended on is not unloaded"
237 ;; Service 'bar' is not among the target services; yet, it must not be
238 ;; unloaded because 'foo' depends on it. 'foo' gets replaced but it
239 ;; must be restarted manually.
240 (shepherd-service-upgrade
241 (list (live-service '(foo) '(bar) #t)
242 (live-service '(bar) '() #t) ;still used!
243 (live-service '(baz) '() #t))
244 (list (shepherd-service (provision '(foo))
246 (lambda (unload restart)
247 (list (map live-service-provision unload)
248 (map shepherd-service-provision restart)))))
250 (test-equal "shepherd-service-upgrade: obsolete services that depend on each other"
251 '(((foo) (bar) (baz)) ;unload
255 ;; 'foo', 'bar', and 'baz' depend on each other, but all of them are
256 ;; obsolete, and thus should be unloaded.
257 (shepherd-service-upgrade
258 (list (live-service '(foo) '(bar) #t) ;obsolete
259 (live-service '(bar) '(baz) #t) ;obsolete
260 (live-service '(baz) '() #t)) ;obsolete
261 (list (shepherd-service (provision '(qux))
263 (lambda (unload restart)
264 (list (map live-service-provision unload)
265 (map shepherd-service-provision restart)))))
267 (test-eq "lookup-service-types"
269 (and (null? (lookup-service-types 'does-not-exist-at-all))
270 (match (lookup-service-types 'system)