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))
29 (@@ (gnu services herd) live-service))
32 (test-begin "services")
34 (test-equal "services, default value"
36 (let* ((t1 (service-type (name 't1) (extensions '())))
37 (t2 (service-type (name 't2) (extensions '())
39 (list (service-value (service t2))
40 (service-value (service t2 123))
41 (service-value (service t1 234))
42 (guard (c ((missing-value-service-error? c) 'error))
45 (test-assert "service-back-edges"
46 (let* ((t1 (service-type (name 't1) (extensions '())
47 (compose +) (extend *)))
48 (t2 (service-type (name 't2)
50 (list (service-extension t1 (const '()))))
51 (compose +) (extend *)))
52 (t3 (service-type (name 't3)
54 (list (service-extension t2 identity)
55 (service-extension t1 list)))))
59 (e (service-back-edges (list s1 s2 s3))))
60 (and (lset= eq? (e s1) (list s2 s3))
61 (lset= eq? (e s2) (list s3))
64 (test-equal "fold-services"
65 ;; Make sure 'fold-services' returns the right result. The numbers come
66 ;; from services of type T3; 'xyz 60' comes from the service of type T2,
67 ;; where 60 = 15 × 4 = (1 + 2 + 3 + 4 + 5) × 4.
68 '(initial-value 5 4 3 2 1 xyz 60)
69 (let* ((t1 (service-type (name 't1) (extensions '())
72 (t2 (service-type (name 't2)
74 (list (service-extension t1
76 (compose (cut reduce + 0 <>))
78 (t3 (service-type (name 't3)
80 (list (service-extension t2 identity)
81 (service-extension t1 list)))))
82 (r (fold-services (cons* (service t1 'initial-value)
88 (and (eq? (service-kind r) t1)
91 (test-assert "fold-services, ambiguity"
92 (let* ((t1 (service-type (name 't1) (extensions '())
95 (t2 (service-type (name 't2)
97 (list (service-extension t1 list)))))
99 (guard (c ((ambiguous-target-service-error? c)
100 (and (eq? (ambiguous-target-service-error-target-type c)
102 (eq? (ambiguous-target-service-error-service c)
104 (fold-services (list (service t1 'first)
110 (test-assert "fold-services, missing target"
111 (let* ((t1 (service-type (name 't1) (extensions '())))
112 (t2 (service-type (name 't2)
114 (list (service-extension t1 list)))))
116 (guard (c ((missing-target-service-error? c)
117 (and (eq? (missing-target-service-error-target-type c)
119 (eq? (missing-target-service-error-service c)
121 (fold-services (list s) #:target-type t1)
124 (test-assert "shepherd-service-lookup-procedure"
125 (let* ((s1 (shepherd-service (provision '(s1 s1b)) (start #f)))
126 (s2 (shepherd-service (provision '(s2 s2b)) (start #f)))
127 (s3 (shepherd-service (provision '(s3 s3b s3c)) (start #f)))
128 (lookup (shepherd-service-lookup-procedure (list s1 s2 s3))))
129 (and (eq? (lookup 's1) (lookup 's1b) s1)
130 (eq? (lookup 's2) (lookup 's2b) s2)
131 (eq? (lookup 's3) (lookup 's3b) s3))))
133 (test-assert "shepherd-service-back-edges"
134 (let* ((s1 (shepherd-service (provision '(s1)) (start #f)))
135 (s2 (shepherd-service (provision '(s2))
138 (s3 (shepherd-service (provision '(s3))
139 (requirement '(s1 s2))
141 (e (shepherd-service-back-edges (list s1 s2 s3))))
142 (and (lset= eq? (e s1) (list s2 s3))
143 (lset= eq? (e s2) (list s3))
146 (test-equal "shepherd-service-upgrade: nothing to do"
150 (shepherd-service-upgrade '() '()))
153 (test-equal "shepherd-service-upgrade: one unchanged, one upgraded, one new"
158 ;; Here 'foo' is not upgraded because it is still running, whereas
159 ;; 'bar' is upgraded because it is not currently running. 'baz' is
160 ;; loaded because it's a new service.
161 (shepherd-service-upgrade
162 (list (live-service '(foo) '() #t)
163 (live-service '(bar) '() #f)
164 (live-service '(root) '() #t)) ;essential!
165 (list (shepherd-service (provision '(foo))
167 (shepherd-service (provision '(bar))
169 (shepherd-service (provision '(baz))
171 (lambda (unload load)
172 (list (map live-service-provision unload)
173 (map shepherd-service-provision load)))))
175 (test-equal "shepherd-service-upgrade: service depended on is not unloaded"
180 ;; Service 'bar' is not among the target services; yet, it must not be
181 ;; unloaded because 'foo' depends on it.
182 (shepherd-service-upgrade
183 (list (live-service '(foo) '(bar) #t)
184 (live-service '(bar) '() #t) ;still used!
185 (live-service '(baz) '() #t))
186 (list (shepherd-service (provision '(foo))
188 (lambda (unload load)
189 (list (map live-service-provision unload)
190 (map shepherd-service-provision load)))))
192 (test-equal "shepherd-service-upgrade: obsolete services that depend on each other"
193 '(((foo) (bar) (baz)) ;unload
197 ;; 'foo', 'bar', and 'baz' depend on each other, but all of them are
198 ;; obsolete, and thus should be unloaded.
199 (shepherd-service-upgrade
200 (list (live-service '(foo) '(bar) #t) ;obsolete
201 (live-service '(bar) '(baz) #t) ;obsolete
202 (live-service '(baz) '() #t)) ;obsolete
203 (list (shepherd-service (provision '(qux))
205 (lambda (unload load)
206 (list (map live-service-provision unload)
207 (map shepherd-service-provision load)))))