gnu: Add cl-simple-date-postgres-glue.
[jackhill/guix/guix.git] / gnu / tests / nfs.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2016, 2017 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2016 John Darrington <jmd@gnu.org>
4 ;;; Copyright © 2017 Mathieu Othacehe <m.othacehe@gmail.com>
5 ;;; Copyright © 2017 Tobias Geerinckx-Rice <me@tobias.gr>
6 ;;; Copyright © 2018 Clément Lassieur <clement@lassieur.org>
7 ;;; Copyright © 2019, 2020 Ricardo Wurmus <rekado@elephly.net>
8 ;;;
9 ;;; This file is part of GNU Guix.
10 ;;;
11 ;;; GNU Guix is free software; you can redistribute it and/or modify it
12 ;;; under the terms of the GNU General Public License as published by
13 ;;; the Free Software Foundation; either version 3 of the License, or (at
14 ;;; your option) any later version.
15 ;;;
16 ;;; GNU Guix is distributed in the hope that it will be useful, but
17 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;;; GNU General Public License for more details.
20 ;;;
21 ;;; You should have received a copy of the GNU General Public License
22 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
23
24 (define-module (gnu tests nfs)
25 #:use-module (gnu tests)
26 #:use-module (gnu bootloader)
27 #:use-module (gnu bootloader grub)
28 #:use-module (gnu system)
29 #:use-module (gnu system file-systems)
30 #:use-module (gnu system shadow)
31 #:use-module (gnu system vm)
32 #:use-module (gnu services)
33 #:use-module (gnu services base)
34 #:use-module (gnu services nfs)
35 #:use-module (gnu services networking)
36 #:use-module (gnu packages onc-rpc)
37 #:use-module (gnu packages nfs)
38 #:use-module (guix gexp)
39 #:use-module (guix store)
40 #:use-module (guix monads)
41 #:export (%test-nfs
42 %test-nfs-server))
43
44 (define %base-os
45 (operating-system
46 (host-name "olitupmok")
47 (timezone "Europe/Berlin")
48 (locale "en_US.UTF-8")
49
50 (bootloader (bootloader-configuration
51 (bootloader grub-bootloader)
52 (target "/dev/sdX")))
53 (file-systems %base-file-systems)
54 (users %base-user-accounts)
55 (packages (cons*
56 rpcbind
57 %base-packages))
58 (services (cons*
59 (service rpcbind-service-type)
60 (service dhcp-client-service-type)
61 %base-services))))
62
63 (define (run-nfs-test name socket)
64 "Run a test of an OS running RPC-SERVICE, which should create SOCKET."
65 (define os
66 (marionette-operating-system
67 %base-os
68 #:imported-modules '((gnu services herd)
69 (guix combinators))))
70
71 (define test
72 (with-imported-modules '((gnu build marionette))
73 #~(begin
74 (use-modules (gnu build marionette)
75 (srfi srfi-64))
76
77 (define marionette
78 (make-marionette (list #$(virtual-machine os))))
79
80 (define (wait-for-socket file)
81 ;; Wait until SOCKET exists in the guest
82 (marionette-eval
83 `(let loop ((i 10))
84 (cond ((and (file-exists? ,file)
85 (eq? 'socket (stat:type (stat ,file))))
86 #t)
87 ((> i 0)
88 (sleep 1)
89 (loop (- i 1)))
90 (else
91 (error "Socket didn't show up: " ,file))))
92 marionette))
93
94 (mkdir #$output)
95 (chdir #$output)
96
97 (test-begin "rpc-daemon")
98
99 ;; Wait for the rpcbind daemon to be up and running.
100 (test-assert "RPC service running"
101 (marionette-eval
102 '(begin
103 (use-modules (gnu services herd))
104 (start-service 'rpcbind-daemon))
105 marionette))
106
107 ;; Check the socket file and that the service is still running.
108 (test-assert "RPC socket exists"
109 (and
110 (wait-for-socket #$socket)
111 (marionette-eval
112 '(begin
113 (use-modules (gnu services herd)
114 (srfi srfi-1))
115
116 (live-service-running
117 (find (lambda (live)
118 (memq 'rpcbind-daemon
119 (live-service-provision live)))
120 (current-services))))
121 marionette)))
122
123 (test-assert "Probe RPC daemon"
124 (marionette-eval
125 '(zero? (system* "rpcinfo" "-p"))
126 marionette))
127
128 (test-end)
129 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
130
131 (gexp->derivation name test))
132
133 (define %test-nfs
134 (system-test
135 (name "nfs")
136 (description "Test some things related to NFS.")
137 (value (run-nfs-test name "/var/run/rpcbind.sock"))))
138
139 \f
140 (define %nfs-os
141 (let ((os (simple-operating-system
142 (simple-service 'create-target-directory activation-service-type
143 #~(begin
144 (mkdir "/remote")
145 (chmod "/remote" #o777)
146 #t))
147 (service dhcp-client-service-type)
148 (service nfs-service-type
149 (nfs-configuration
150 (debug '(nfs nfsd mountd))
151 (exports '(("/export"
152 ;; crossmnt = This is the pseudo root.
153 ;; fsid=0 = root file system of the export
154 "*(ro,insecure,no_subtree_check,crossmnt,fsid=0)"))))))))
155 (operating-system
156 (inherit os)
157 (host-name "nfs-server")
158 ;; We need to use a tmpfs here, because the test system's root file
159 ;; system cannot be re-exported via NFS.
160 (file-systems (cons
161 (file-system
162 (device "none")
163 (mount-point "/export")
164 (type "tmpfs")
165 (create-mount-point? #t))
166 %base-file-systems))
167 (services
168 ;; Enable debugging output.
169 (modify-services (operating-system-user-services os)
170 (syslog-service-type config
171 =>
172 (syslog-configuration
173 (inherit config)
174 (config-file
175 (plain-file
176 "syslog.conf"
177 "*.* /dev/console\n")))))))))
178
179 (define (run-nfs-server-test)
180 "Run a test of an OS running a service of NFS-SERVICE-TYPE."
181 (define os
182 (marionette-operating-system
183 %nfs-os
184 #:requirements '(nscd)
185 #:imported-modules '((gnu services herd)
186 (guix combinators))))
187 (define test
188 (with-imported-modules '((gnu build marionette))
189 #~(begin
190 (use-modules (gnu build marionette)
191 (srfi srfi-64))
192
193 (define marionette
194 (make-marionette (list #$(virtual-machine os))))
195 (define (wait-for-file file)
196 ;; Wait until FILE exists in the guest
197 (marionette-eval
198 `(let loop ((i 10))
199 (cond ((file-exists? ,file)
200 #t)
201 ((> i 0)
202 (sleep 1)
203 (loop (- i 1)))
204 (else
205 (error "File didn't show up: " ,file))))
206 marionette))
207
208 (mkdir #$output)
209 (chdir #$output)
210
211 (test-begin "nfs-daemon")
212 (marionette-eval
213 '(begin
214 (current-output-port
215 (open-file "/dev/console" "w0"))
216 (chmod "/export" #o777)
217 (with-output-to-file "/export/hello"
218 (lambda () (display "hello world")))
219 (chmod "/export/hello" #o777))
220 marionette)
221
222 (test-assert "nscd PID file is created"
223 (marionette-eval
224 '(begin
225 (use-modules (gnu services herd))
226 (start-service 'nscd))
227 marionette))
228
229 (test-assert "nscd is listening on its socket"
230 (marionette-eval
231 ;; XXX: Work around a race condition in nscd: nscd creates its
232 ;; PID file before it is listening on its socket.
233 '(let ((sock (socket PF_UNIX SOCK_STREAM 0)))
234 (let try ()
235 (catch 'system-error
236 (lambda ()
237 (connect sock AF_UNIX "/var/run/nscd/socket")
238 (close-port sock)
239 (format #t "nscd is ready~%")
240 #t)
241 (lambda args
242 (format #t "waiting for nscd...~%")
243 (usleep 500000)
244 (try)))))
245 marionette))
246
247 (test-assert "network is up"
248 (marionette-eval
249 '(begin
250 (use-modules (gnu services herd))
251 (start-service 'networking))
252 marionette))
253
254 ;; Wait for the NFS services to be up and running.
255 (test-assert "nfs services are running"
256 (and (marionette-eval
257 '(begin
258 (use-modules (gnu services herd))
259 (start-service 'nfs))
260 marionette)
261 (wait-for-file "/var/run/rpc.statd.pid")))
262
263 (test-assert "nfs share is advertised"
264 (marionette-eval
265 '(zero? (system* (string-append #$nfs-utils "/sbin/showmount")
266 "-e" "nfs-server"))
267 marionette))
268
269 (test-assert "nfs share mounted"
270 (marionette-eval
271 '(begin
272 (and (zero? (system* (string-append #$nfs-utils "/sbin/mount.nfs4")
273 "nfs-server:/" "/remote" "-v"))
274 (file-exists? "/remote/hello")))
275 marionette))
276 (test-end)
277 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
278
279 (gexp->derivation "nfs-server-test" test))
280
281 (define %test-nfs-server
282 (system-test
283 (name "nfs-server")
284 (description "Test that an NFS server can be started and exported
285 directories can be mounted.")
286 (value (run-nfs-server-test))))