gnu: linux-libre: Update to 5.0.10.
[guix.git] / tests / services.scm
blob44ad0022c6d9d63f96a934b3315e0c3c3624aa8d
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/>.
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 (define live-service
30   (@@ (gnu services herd) live-service))
33 (test-begin "services")
35 (test-equal "services, default value"
36   '(42 123 234 error)
37   (let* ((t1 (service-type (name 't1) (extensions '())))
38          (t2 (service-type (name 't2) (extensions '())
39                            (default-value 42))))
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))
44             (service t1)))))
46 (test-assert "service-back-edges"
47   (let* ((t1 (service-type (name 't1) (extensions '())
48                            (compose +) (extend *)))
49          (t2 (service-type (name 't2)
50                            (extensions
51                             (list (service-extension t1 (const '()))))
52                            (compose +) (extend *)))
53          (t3 (service-type (name 't3)
54                            (extensions
55                             (list (service-extension t2 identity)
56                                   (service-extension t1 list)))))
57          (s1 (service t1 #t))
58          (s2 (service t2 #t))
59          (s3 (service t3 #t))
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))
63          (null? (e 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 '())
71                            (compose concatenate)
72                            (extend cons)))
73          (t2 (service-type (name 't2)
74                            (extensions
75                             (list (service-extension t1
76                                                      (cut list 'xyz <>))))
77                            (compose (cut reduce + 0 <>))
78                            (extend *)))
79          (t3 (service-type (name 't3)
80                            (extensions
81                             (list (service-extension t2 identity)
82                                   (service-extension t1 list)))))
83          (r  (fold-services (cons* (service t1 'initial-value)
84                                    (service t2 4)
85                                    (map (lambda (x)
86                                           (service t3 x))
87                                         (iota 5 1)))
88                             #:target-type t1)))
89     (and (eq? (service-kind r) t1)
90          (service-value r))))
92 (test-assert "fold-services, ambiguity"
93   (let* ((t1 (service-type (name 't1) (extensions '())
94                            (compose concatenate)
95                            (extend cons)))
96          (t2 (service-type (name 't2)
97                            (extensions
98                             (list (service-extension t1 list)))))
99          (s  (service t2 42)))
100     (guard (c ((ambiguous-target-service-error? c)
101                (and (eq? (ambiguous-target-service-error-target-type c)
102                          t1)
103                     (eq? (ambiguous-target-service-error-service c)
104                          s))))
105       (fold-services (list (service t1 'first)
106                            (service t1 'second)
107                            s)
108                      #:target-type t1)
109       #f)))
111 (test-assert "fold-services, missing target"
112   (let* ((t1 (service-type (name 't1) (extensions '())))
113          (t2 (service-type (name 't2)
114                            (extensions
115                             (list (service-extension t1 list)))))
116          (s  (service t2 42)))
117     (guard (c ((missing-target-service-error? c)
118                (and (eq? (missing-target-service-error-target-type c)
119                          t1)
120                     (eq? (missing-target-service-error-service c)
121                          s))))
122       (fold-services (list s) #:target-type t1)
123       #f)))
125 (test-assert "instantiate-missing-services"
126   (let* ((t1 (service-type (name 't1) (extensions '())
127                            (default-value 'dflt)
128                            (compose concatenate)
129                            (extend cons)))
130          (t2 (service-type (name 't2)
131                            (extensions
132                             (list (service-extension t1 list)))))
133          (s1 (service t1 'hey!))
134          (s2 (service t2 42)))
135     (and (lset= equal?
136                 (list (service t1) s2)
137                 (instantiate-missing-services (list s2)))
138          (equal? (list s1 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)
145                            (extend cons)))
146          (t2 (service-type (name 't2)
147                            (default-value 'dflt2)
148                            (compose concatenate)
149                            (extend cons)
150                            (extensions
151                             (list (service-extension t1 list)))))
152          (t3 (service-type (name 't3)
153                            (extensions
154                             (list (service-extension t2 list)))))
155          (s1 (service t1))
156          (s2 (service t2))
157          (s3 (service t3 42))
158          (== (cut lset= equal? <...>)))
159     (and (== (list s1 s2 s3)
160              (instantiate-missing-services (list s3)))
161          (== (list s1 s2 s3)
162              (instantiate-missing-services (list s1 s3)))
163          (== (list s1 s2 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)
169                            (extensions
170                             (list (service-extension t1 list)))))
171          (s  (service t2 42)))
172     (guard (c ((missing-target-service-error? c)
173                (and (eq? (missing-target-service-error-target-type c)
174                          t1)
175                     (eq? (missing-target-service-error-service c)
176                          s))))
177       (instantiate-missing-services (list s))
178       #f)))
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))
192                                (requirement '(s1))
193                                (start #f)))
194          (s3 (shepherd-service (provision '(s3))
195                                (requirement '(s1 s2))
196                                (start #f)))
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))
200          (null? (e s3)))))
202 (test-equal "shepherd-service-upgrade: nothing to do"
203   '(() ())
204   (call-with-values
205       (lambda ()
206         (shepherd-service-upgrade '() '()))
207     list))
209 (test-equal "shepherd-service-upgrade: one unchanged, one upgraded, one new"
210   '(()                                            ;unload
211     ((foo)))                                      ;restart
212   (call-with-values
213       (lambda ()
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
217         ;; a new service.
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))
223                                  (start #t))
224                (shepherd-service (provision '(bar))
225                                  (start #t))
226                (shepherd-service (provision '(baz))
227                                  (start #t)))))
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"
233   '(((baz))                                       ;unload
234     ((foo)))                                      ;restart
235   (call-with-values
236       (lambda ()
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))
245                                  (start #t)))))
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
252     ())                                           ;restart
253   (call-with-values
254       (lambda ()
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))
262                                  (start #t)))))
263     (lambda (unload restart)
264       (list (map live-service-provision unload)
265             (map shepherd-service-provision restart)))))
267 (test-eq "lookup-service-types"
268   system-service-type
269   (and (null? (lookup-service-types 'does-not-exist-at-all))
270        (match (lookup-service-types 'system)
271          ((one) one)
272          (x x))))
274 (test-end)