1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2015, 2016 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-assert "service-back-edges"
35 (let* ((t1 (service-type (name 't1) (extensions '())
36 (compose +) (extend *)))
37 (t2 (service-type (name 't2)
39 (list (service-extension t1 (const '()))))
40 (compose +) (extend *)))
41 (t3 (service-type (name 't3)
43 (list (service-extension t2 identity)
44 (service-extension t1 list)))))
48 (e (service-back-edges (list s1 s2 s3))))
49 (and (lset= eq? (e s1) (list s2 s3))
50 (lset= eq? (e s2) (list s3))
53 (test-equal "fold-services"
54 ;; Make sure 'fold-services' returns the right result. The numbers come
55 ;; from services of type T3; 'xyz 60' comes from the service of type T2,
56 ;; where 60 = 15 × 4 = (1 + 2 + 3 + 4 + 5) × 4.
57 '(initial-value 5 4 3 2 1 xyz 60)
58 (let* ((t1 (service-type (name 't1) (extensions '())
61 (t2 (service-type (name 't2)
63 (list (service-extension t1
65 (compose (cut reduce + 0 <>))
67 (t3 (service-type (name 't3)
69 (list (service-extension t2 identity)
70 (service-extension t1 list)))))
71 (r (fold-services (cons* (service t1 'initial-value)
77 (and (eq? (service-kind r) t1)
78 (service-parameters r))))
80 (test-assert "fold-services, ambiguity"
81 (let* ((t1 (service-type (name 't1) (extensions '())
84 (t2 (service-type (name 't2)
86 (list (service-extension t1 list)))))
88 (guard (c ((ambiguous-target-service-error? c)
89 (and (eq? (ambiguous-target-service-error-target-type c)
91 (eq? (ambiguous-target-service-error-service c)
93 (fold-services (list (service t1 'first)
99 (test-assert "fold-services, missing target"
100 (let* ((t1 (service-type (name 't1) (extensions '())))
101 (t2 (service-type (name 't2)
103 (list (service-extension t1 list)))))
105 (guard (c ((missing-target-service-error? c)
106 (and (eq? (missing-target-service-error-target-type c)
108 (eq? (missing-target-service-error-service c)
110 (fold-services (list s) #:target-type t1)
113 (test-assert "shepherd-service-lookup-procedure"
114 (let* ((s1 (shepherd-service (provision '(s1 s1b)) (start #f)))
115 (s2 (shepherd-service (provision '(s2 s2b)) (start #f)))
116 (s3 (shepherd-service (provision '(s3 s3b s3c)) (start #f)))
117 (lookup (shepherd-service-lookup-procedure (list s1 s2 s3))))
118 (and (eq? (lookup 's1) (lookup 's1b) s1)
119 (eq? (lookup 's2) (lookup 's2b) s2)
120 (eq? (lookup 's3) (lookup 's3b) s3))))
122 (test-assert "shepherd-service-back-edges"
123 (let* ((s1 (shepherd-service (provision '(s1)) (start #f)))
124 (s2 (shepherd-service (provision '(s2))
127 (s3 (shepherd-service (provision '(s3))
128 (requirement '(s1 s2))
130 (e (shepherd-service-back-edges (list s1 s2 s3))))
131 (and (lset= eq? (e s1) (list s2 s3))
132 (lset= eq? (e s2) (list s3))
135 (test-equal "shepherd-service-upgrade: nothing to do"
139 (shepherd-service-upgrade '() '()))
142 (test-equal "shepherd-service-upgrade: one unchanged, one upgraded, one new"
147 ;; Here 'foo' is not upgraded because it is still running, whereas
148 ;; 'bar' is upgraded because it is not currently running. 'baz' is
149 ;; loaded because it's a new service.
150 (shepherd-service-upgrade
151 (list (live-service '(foo) '() #t)
152 (live-service '(bar) '() #f)
153 (live-service '(root) '() #t)) ;essential!
154 (list (shepherd-service (provision '(foo))
156 (shepherd-service (provision '(bar))
158 (shepherd-service (provision '(baz))
160 (lambda (unload load)
161 (list (map live-service-provision unload)
162 (map shepherd-service-provision load)))))
164 (test-equal "shepherd-service-upgrade: service depended on is not unloaded"
169 ;; Service 'bar' is not among the target services; yet, it must not be
170 ;; unloaded because 'foo' depends on it.
171 (shepherd-service-upgrade
172 (list (live-service '(foo) '(bar) #t)
173 (live-service '(bar) '() #t) ;still used!
174 (live-service '(baz) '() #t))
175 (list (shepherd-service (provision '(foo))
177 (lambda (unload load)
178 (list (map live-service-provision unload)
179 (map shepherd-service-provision load)))))
181 (test-equal "shepherd-service-upgrade: obsolete services that depend on each other"
182 '(((foo) (bar) (baz)) ;unload
186 ;; 'foo', 'bar', and 'baz' depend on each other, but all of them are
187 ;; obsolete, and thus should be unloaded.
188 (shepherd-service-upgrade
189 (list (live-service '(foo) '(bar) #t) ;obsolete
190 (live-service '(bar) '(baz) #t) ;obsolete
191 (live-service '(baz) '() #t)) ;obsolete
192 (list (shepherd-service (provision '(qux))
194 (lambda (unload load)
195 (list (map live-service-provision unload)
196 (map shepherd-service-provision load)))))