ui: Present 'use-modules' hints with a question mark.
[guix.git] / tests / services.scm
blobca32b565c49ad57c56baad3e0c3682ea75e58fb9
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/>.
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 "shepherd-service-lookup-procedure"
126   (let* ((s1 (shepherd-service (provision '(s1 s1b)) (start #f)))
127          (s2 (shepherd-service (provision '(s2 s2b)) (start #f)))
128          (s3 (shepherd-service (provision '(s3 s3b s3c)) (start #f)))
129          (lookup (shepherd-service-lookup-procedure (list s1 s2 s3))))
130     (and (eq? (lookup 's1) (lookup 's1b) s1)
131          (eq? (lookup 's2) (lookup 's2b) s2)
132          (eq? (lookup 's3) (lookup 's3b) s3))))
134 (test-assert "shepherd-service-back-edges"
135   (let* ((s1 (shepherd-service (provision '(s1)) (start #f)))
136          (s2 (shepherd-service (provision '(s2))
137                                (requirement '(s1))
138                                (start #f)))
139          (s3 (shepherd-service (provision '(s3))
140                                (requirement '(s1 s2))
141                                (start #f)))
142          (e  (shepherd-service-back-edges (list s1 s2 s3))))
143     (and (lset= eq? (e s1) (list s2 s3))
144          (lset= eq? (e s2) (list s3))
145          (null? (e s3)))))
147 (test-equal "shepherd-service-upgrade: nothing to do"
148   '(() ())
149   (call-with-values
150       (lambda ()
151         (shepherd-service-upgrade '() '()))
152     list))
154 (test-equal "shepherd-service-upgrade: one unchanged, one upgraded, one new"
155   '(((bar))                                       ;unload
156     ((bar) (baz)))                                ;load
157   (call-with-values
158       (lambda ()
159         ;; Here 'foo' is not upgraded because it is still running, whereas
160         ;; 'bar' is upgraded because it is not currently running.  'baz' is
161         ;; loaded because it's a new service.
162         (shepherd-service-upgrade
163          (list (live-service '(foo) '() #t)
164                (live-service '(bar) '() #f)
165                (live-service '(root) '() #t))     ;essential!
166          (list (shepherd-service (provision '(foo))
167                                  (start #t))
168                (shepherd-service (provision '(bar))
169                                  (start #t))
170                (shepherd-service (provision '(baz))
171                                  (start #t)))))
172     (lambda (unload load)
173       (list (map live-service-provision unload)
174             (map shepherd-service-provision load)))))
176 (test-equal "shepherd-service-upgrade: service depended on is not unloaded"
177   '(((baz))                                       ;unload
178     ())                                           ;load
179   (call-with-values
180       (lambda ()
181         ;; Service 'bar' is not among the target services; yet, it must not be
182         ;; unloaded because 'foo' depends on it.
183         (shepherd-service-upgrade
184          (list (live-service '(foo) '(bar) #t)
185                (live-service '(bar) '() #t)       ;still used!
186                (live-service '(baz) '() #t))
187          (list (shepherd-service (provision '(foo))
188                                  (start #t)))))
189     (lambda (unload load)
190       (list (map live-service-provision unload)
191             (map shepherd-service-provision load)))))
193 (test-equal "shepherd-service-upgrade: obsolete services that depend on each other"
194   '(((foo) (bar) (baz))                           ;unload
195     ((qux)))                                      ;load
196   (call-with-values
197       (lambda ()
198         ;; 'foo', 'bar', and 'baz' depend on each other, but all of them are
199         ;; obsolete, and thus should be unloaded.
200         (shepherd-service-upgrade
201          (list (live-service '(foo) '(bar) #t)    ;obsolete
202                (live-service '(bar) '(baz) #t)    ;obsolete
203                (live-service '(baz) '() #t))      ;obsolete
204          (list (shepherd-service (provision '(qux))
205                                  (start #t)))))
206     (lambda (unload load)
207       (list (map live-service-provision unload)
208             (map shepherd-service-provision load)))))
210 (test-eq "lookup-service-types"
211   system-service-type
212   (and (null? (lookup-service-types 'does-not-exist-at-all))
213        (match (lookup-service-types 'system)
214          ((one) one)
215          (x x))))
217 (test-end)