1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2015, 2016, 2017 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 "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))
139 (s3 (shepherd-service (provision '(s3))
140 (requirement '(s1 s2))
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))
147 (test-equal "shepherd-service-upgrade: nothing to do"
151 (shepherd-service-upgrade '() '()))
154 (test-equal "shepherd-service-upgrade: one unchanged, one upgraded, one new"
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))
168 (shepherd-service (provision '(bar))
170 (shepherd-service (provision '(baz))
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"
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))
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
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))
206 (lambda (unload load)
207 (list (map live-service-provision unload)
208 (map shepherd-service-provision load)))))
210 (test-eq "lookup-service-types"
212 (and (null? (lookup-service-types 'does-not-exist-at-all))
213 (match (lookup-service-types 'system)