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))
29 (test-begin "services")
31 (test-equal "services, default value"
33 (let* ((t1 (service-type (name 't1) (extensions '())))
34 (t2 (service-type (name 't2) (extensions '())
36 (list (service-value (service t2))
37 (service-value (service t2 123))
38 (service-value (service t1 234))
39 (guard (c ((missing-value-service-error? c) 'error))
42 (test-assert "service-back-edges"
43 (let* ((t1 (service-type (name 't1) (extensions '())
44 (compose +) (extend *)))
45 (t2 (service-type (name 't2)
47 (list (service-extension t1 (const '()))))
48 (compose +) (extend *)))
49 (t3 (service-type (name 't3)
51 (list (service-extension t2 identity)
52 (service-extension t1 list)))))
56 (e (service-back-edges (list s1 s2 s3))))
57 (and (lset= eq? (e s1) (list s2 s3))
58 (lset= eq? (e s2) (list s3))
61 (test-equal "fold-services"
62 ;; Make sure 'fold-services' returns the right result. The numbers come
63 ;; from services of type T3; 'xyz 60' comes from the service of type T2,
64 ;; where 60 = 15 × 4 = (1 + 2 + 3 + 4 + 5) × 4.
65 '(initial-value 5 4 3 2 1 xyz 60)
66 (let* ((t1 (service-type (name 't1) (extensions '())
69 (t2 (service-type (name 't2)
71 (list (service-extension t1
73 (compose (cut reduce + 0 <>))
75 (t3 (service-type (name 't3)
77 (list (service-extension t2 identity)
78 (service-extension t1 list)))))
79 (r (fold-services (cons* (service t1 'initial-value)
85 (and (eq? (service-kind r) t1)
88 (test-assert "fold-services, ambiguity"
89 (let* ((t1 (service-type (name 't1) (extensions '())
92 (t2 (service-type (name 't2)
94 (list (service-extension t1 list)))))
96 (guard (c ((ambiguous-target-service-error? c)
97 (and (eq? (ambiguous-target-service-error-target-type c)
99 (eq? (ambiguous-target-service-error-service c)
101 (fold-services (list (service t1 'first)
107 (test-assert "fold-services, missing target"
108 (let* ((t1 (service-type (name 't1) (extensions '())))
109 (t2 (service-type (name 't2)
111 (list (service-extension t1 list)))))
113 (guard (c ((missing-target-service-error? c)
114 (and (eq? (missing-target-service-error-target-type c)
116 (eq? (missing-target-service-error-service c)
118 (fold-services (list s) #:target-type t1)
121 (test-assert "instantiate-missing-services"
122 (let* ((t1 (service-type (name 't1) (extensions '())
123 (default-value 'dflt)
124 (compose concatenate)
126 (t2 (service-type (name 't2)
128 (list (service-extension t1 list)))))
129 (s1 (service t1 'hey!))
130 (s2 (service t2 42)))
132 (list (service t1) s2)
133 (instantiate-missing-services (list s2)))
135 (instantiate-missing-services (list s1 s2))))))
137 (test-assert "instantiate-missing-services, indirect"
138 (let* ((t1 (service-type (name 't1) (extensions '())
139 (default-value 'dflt)
140 (compose concatenate)
142 (t2 (service-type (name 't2)
143 (default-value 'dflt2)
144 (compose concatenate)
147 (list (service-extension t1 list)))))
148 (t3 (service-type (name 't3)
150 (list (service-extension t2 list)))))
154 (== (cut lset= equal? <...>)))
155 (and (== (list s1 s2 s3)
156 (instantiate-missing-services (list s3)))
158 (instantiate-missing-services (list s1 s3)))
160 (instantiate-missing-services (list s2 s3))))))
162 (test-assert "instantiate-missing-services, no default value"
163 (let* ((t1 (service-type (name 't1) (extensions '())))
164 (t2 (service-type (name 't2)
166 (list (service-extension t1 list)))))
168 (guard (c ((missing-target-service-error? c)
169 (and (eq? (missing-target-service-error-target-type c)
171 (eq? (missing-target-service-error-service c)
173 (instantiate-missing-services (list s))
176 (test-assert "shepherd-service-lookup-procedure"
177 (let* ((s1 (shepherd-service (provision '(s1 s1b)) (start #f)))
178 (s2 (shepherd-service (provision '(s2 s2b)) (start #f)))
179 (s3 (shepherd-service (provision '(s3 s3b s3c)) (start #f)))
180 (lookup (shepherd-service-lookup-procedure (list s1 s2 s3))))
181 (and (eq? (lookup 's1) (lookup 's1b) s1)
182 (eq? (lookup 's2) (lookup 's2b) s2)
183 (eq? (lookup 's3) (lookup 's3b) s3))))
185 (test-assert "shepherd-service-back-edges"
186 (let* ((s1 (shepherd-service (provision '(s1)) (start #f)))
187 (s2 (shepherd-service (provision '(s2))
190 (s3 (shepherd-service (provision '(s3))
191 (requirement '(s1 s2))
193 (e (shepherd-service-back-edges (list s1 s2 s3))))
194 (and (lset= eq? (e s1) (list s2 s3))
195 (lset= eq? (e s2) (list s3))
198 (test-equal "shepherd-service-upgrade: nothing to do"
202 (shepherd-service-upgrade '() '()))
205 (test-equal "shepherd-service-upgrade: one unchanged, one upgraded, one new"
210 ;; Here 'foo' is replaced and must be explicitly restarted later
211 ;; because it is still running, whereas 'bar' is upgraded right away
212 ;; because it is not currently running. 'baz' is loaded because it's
214 (shepherd-service-upgrade
215 (list (live-service '(foo) '() #t)
216 (live-service '(bar) '() #f)
217 (live-service '(root) '() #t)) ;essential!
218 (list (shepherd-service (provision '(foo))
220 (shepherd-service (provision '(bar))
222 (shepherd-service (provision '(baz))
224 (lambda (unload restart)
225 (list (map live-service-provision unload)
226 (map shepherd-service-provision restart)))))
228 (test-equal "shepherd-service-upgrade: service depended on is not unloaded"
233 ;; Service 'bar' is not among the target services; yet, it must not be
234 ;; unloaded because 'foo' depends on it. 'foo' gets replaced but it
235 ;; must be restarted manually.
236 (shepherd-service-upgrade
237 (list (live-service '(foo) '(bar) #t)
238 (live-service '(bar) '() #t) ;still used!
239 (live-service '(baz) '() #t))
240 (list (shepherd-service (provision '(foo))
242 (lambda (unload restart)
243 (list (map live-service-provision unload)
244 (map shepherd-service-provision restart)))))
246 (test-equal "shepherd-service-upgrade: obsolete services that depend on each other"
247 '(((foo) (bar) (baz)) ;unload
251 ;; 'foo', 'bar', and 'baz' depend on each other, but all of them are
252 ;; obsolete, and thus should be unloaded.
253 (shepherd-service-upgrade
254 (list (live-service '(foo) '(bar) #t) ;obsolete
255 (live-service '(bar) '(baz) #t) ;obsolete
256 (live-service '(baz) '() #t)) ;obsolete
257 (list (shepherd-service (provision '(qux))
259 (lambda (unload restart)
260 (list (map live-service-provision unload)
261 (map shepherd-service-provision restart)))))
263 (test-eq "lookup-service-types"
265 (and (null? (lookup-service-types 'does-not-exist-at-all))
266 (match (lookup-service-types 'system)