tests: Add "nfs-root-fs" system test.
[jackhill/guix/guix.git] / gnu / tests / nfs.scm
CommitLineData
7d73b2c6 1;;; GNU Guix --- Functional package management for GNU
e8cec7cc 2;;; Copyright © 2016, 2017, 2020 Ludovic Courtès <ludo@gnu.org>
7d73b2c6 3;;; Copyright © 2016 John Darrington <jmd@gnu.org>
b09a8da4 4;;; Copyright © 2017 Mathieu Othacehe <m.othacehe@gmail.com>
fdfdecdb 5;;; Copyright © 2017 Tobias Geerinckx-Rice <me@tobias.gr>
c24b1547 6;;; Copyright © 2018 Clément Lassieur <clement@lassieur.org>
907eeac2 7;;; Copyright © 2019, 2020 Ricardo Wurmus <rekado@elephly.net>
7d73b2c6
JD
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)
fdfdecdb 26 #:use-module (gnu bootloader)
b09a8da4 27 #:use-module (gnu bootloader grub)
7d73b2c6 28 #:use-module (gnu system)
7d73b2c6
JD
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)
907eeac2 37 #:use-module (gnu packages nfs)
7d73b2c6
JD
38 #:use-module (guix gexp)
39 #:use-module (guix store)
40 #:use-module (guix monads)
907eeac2 41 #:export (%test-nfs
a1a39ed5
S
42 %test-nfs-server
43 %test-nfs-root-fs))
7d73b2c6
JD
44
45(define %base-os
46 (operating-system
47 (host-name "olitupmok")
48 (timezone "Europe/Berlin")
49 (locale "en_US.UTF-8")
50
fdfdecdb
TGR
51 (bootloader (bootloader-configuration
52 (bootloader grub-bootloader)
53 (target "/dev/sdX")))
7d73b2c6
JD
54 (file-systems %base-file-systems)
55 (users %base-user-accounts)
56 (packages (cons*
57 rpcbind
58 %base-packages))
59 (services (cons*
907eeac2 60 (service rpcbind-service-type)
39d7fdce 61 (service dhcp-client-service-type)
7d73b2c6
JD
62 %base-services))))
63
64(define (run-nfs-test name socket)
65 "Run a test of an OS running RPC-SERVICE, which should create SOCKET."
8b113790
LC
66 (define os
67 (marionette-operating-system
68 %base-os
69 #:imported-modules '((gnu services herd)
70 (guix combinators))))
71
72 (define test
73 (with-imported-modules '((gnu build marionette))
74 #~(begin
75 (use-modules (gnu build marionette)
76 (srfi srfi-64))
77
78 (define marionette
79 (make-marionette (list #$(virtual-machine os))))
80
81 (define (wait-for-socket file)
82 ;; Wait until SOCKET exists in the guest
83 (marionette-eval
84 `(let loop ((i 10))
85 (cond ((and (file-exists? ,file)
86 (eq? 'socket (stat:type (stat ,file))))
87 #t)
88 ((> i 0)
89 (sleep 1)
90 (loop (- i 1)))
91 (else
92 (error "Socket didn't show up: " ,file))))
93 marionette))
94
95 (mkdir #$output)
96 (chdir #$output)
97
98 (test-begin "rpc-daemon")
99
100 ;; Wait for the rpcbind daemon to be up and running.
c24b1547 101 (test-assert "RPC service running"
8b113790
LC
102 (marionette-eval
103 '(begin
104 (use-modules (gnu services herd))
e8cec7cc
LC
105
106 ;; Ensure 'rpcinfo' can be found below.
107 (setenv "PATH" "/run/current-system/profile/bin")
108
c24b1547 109 (start-service 'rpcbind-daemon))
8b113790
LC
110 marionette))
111
112 ;; Check the socket file and that the service is still running.
113 (test-assert "RPC socket exists"
114 (and
115 (wait-for-socket #$socket)
116 (marionette-eval
117 '(begin
118 (use-modules (gnu services herd)
119 (srfi srfi-1))
120
121 (live-service-running
122 (find (lambda (live)
123 (memq 'rpcbind-daemon
124 (live-service-provision live)))
125 (current-services))))
126 marionette)))
127
128 (test-assert "Probe RPC daemon"
129 (marionette-eval
130 '(zero? (system* "rpcinfo" "-p"))
131 marionette))
132
133 (test-end)
134 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
135
136 (gexp->derivation name test))
7d73b2c6
JD
137
138(define %test-nfs
139 (system-test
140 (name "nfs")
141 (description "Test some things related to NFS.")
142 (value (run-nfs-test name "/var/run/rpcbind.sock"))))
907eeac2
RW
143
144\f
145(define %nfs-os
146 (let ((os (simple-operating-system
147 (simple-service 'create-target-directory activation-service-type
148 #~(begin
149 (mkdir "/remote")
150 (chmod "/remote" #o777)
151 #t))
152 (service dhcp-client-service-type)
153 (service nfs-service-type
154 (nfs-configuration
155 (debug '(nfs nfsd mountd))
156 (exports '(("/export"
157 ;; crossmnt = This is the pseudo root.
158 ;; fsid=0 = root file system of the export
159 "*(ro,insecure,no_subtree_check,crossmnt,fsid=0)"))))))))
160 (operating-system
161 (inherit os)
162 (host-name "nfs-server")
163 ;; We need to use a tmpfs here, because the test system's root file
164 ;; system cannot be re-exported via NFS.
165 (file-systems (cons
166 (file-system
167 (device "none")
168 (mount-point "/export")
169 (type "tmpfs")
170 (create-mount-point? #t))
171 %base-file-systems))
172 (services
173 ;; Enable debugging output.
174 (modify-services (operating-system-user-services os)
175 (syslog-service-type config
176 =>
177 (syslog-configuration
178 (inherit config)
179 (config-file
180 (plain-file
181 "syslog.conf"
182 "*.* /dev/console\n")))))))))
183
184(define (run-nfs-server-test)
185 "Run a test of an OS running a service of NFS-SERVICE-TYPE."
186 (define os
187 (marionette-operating-system
188 %nfs-os
189 #:requirements '(nscd)
190 #:imported-modules '((gnu services herd)
191 (guix combinators))))
192 (define test
193 (with-imported-modules '((gnu build marionette))
194 #~(begin
195 (use-modules (gnu build marionette)
196 (srfi srfi-64))
197
198 (define marionette
199 (make-marionette (list #$(virtual-machine os))))
907eeac2
RW
200
201 (mkdir #$output)
202 (chdir #$output)
203
204 (test-begin "nfs-daemon")
205 (marionette-eval
206 '(begin
207 (current-output-port
208 (open-file "/dev/console" "w0"))
209 (chmod "/export" #o777)
210 (with-output-to-file "/export/hello"
211 (lambda () (display "hello world")))
212 (chmod "/export/hello" #o777))
213 marionette)
214
215 (test-assert "nscd PID file is created"
216 (marionette-eval
217 '(begin
218 (use-modules (gnu services herd))
219 (start-service 'nscd))
220 marionette))
221
222 (test-assert "nscd is listening on its socket"
a5547295
LC
223 (wait-for-unix-socket "/var/run/nscd/socket"
224 marionette))
907eeac2
RW
225
226 (test-assert "network is up"
227 (marionette-eval
228 '(begin
229 (use-modules (gnu services herd))
230 (start-service 'networking))
231 marionette))
232
233 ;; Wait for the NFS services to be up and running.
234 (test-assert "nfs services are running"
235 (and (marionette-eval
236 '(begin
237 (use-modules (gnu services herd))
238 (start-service 'nfs))
239 marionette)
eb019af8 240 (wait-for-file "/var/run/rpc.statd.pid" marionette)))
907eeac2
RW
241
242 (test-assert "nfs share is advertised"
243 (marionette-eval
244 '(zero? (system* (string-append #$nfs-utils "/sbin/showmount")
245 "-e" "nfs-server"))
246 marionette))
247
248 (test-assert "nfs share mounted"
249 (marionette-eval
250 '(begin
251 (and (zero? (system* (string-append #$nfs-utils "/sbin/mount.nfs4")
252 "nfs-server:/" "/remote" "-v"))
253 (file-exists? "/remote/hello")))
254 marionette))
255 (test-end)
256 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
257
258 (gexp->derivation "nfs-server-test" test))
259
260(define %test-nfs-server
261 (system-test
262 (name "nfs-server")
263 (description "Test that an NFS server can be started and exported
264directories can be mounted.")
265 (value (run-nfs-server-test))))
a1a39ed5
S
266
267(define (run-nfs-root-fs-test)
268 "Run a test of an OS mounting its root file system via NFS."
269 (define nfs-root-server-os
270 (marionette-operating-system
271 (operating-system
272 (inherit %nfs-os)
273 (file-systems %base-file-systems)
274 (services
275 (modify-services (operating-system-user-services %nfs-os)
276 (nfs-service-type
277 config
278 =>
279 (nfs-configuration
280 (debug '(nfs nfsd mountd))
281 (exports '(("/export"
282 "*(rw,insecure,no_subtree_check,crossmnt,fsid=root,no_root_squash,insecure,async)"))))))))
283 #:requirements '(nscd)
284 #:imported-modules '((gnu services herd)
285 (guix combinators))))
286
287 (define nfs-root-client-os
288 (marionette-operating-system
289 (operating-system
290 (inherit %nfs-os)
291 (kernel-arguments '("ip=dhcp"))
292 (file-systems (cons
293 (file-system
294 (type "nfs")
295 (mount-point "/")
296 (device ":/export")
297 (options "addr=0.0.0.0,vers=4.2"))
298 %base-file-systems)))
299 #:requirements '(nscd)
300 #:imported-modules '((gnu services herd)
301 (guix combinators))))
302
303 (define test
304 (with-imported-modules '((gnu build marionette))
305 #~(begin
306 (use-modules (gnu build marionette)
307 (srfi srfi-64))
308
309 (mkdir "/tmp/server")
310 (define server-marionette
311 (make-marionette (list #$(virtual-machine nfs-root-server-os)) #:socket-directory "/tmp/server"))
312 (define client-marionette
313 (make-marionette (list #$(virtual-machine nfs-root-client-os))))
314
315 (mkdir #$output)
316 (chdir #$output)
317
318 (test-begin "start-nfs-root-server")
319 (marionette-eval
320 '(begin
321 (use-modules (gnu services herd))
322
323 (current-output-port
324 (open-file "/dev/console" "w0"))
325 (chmod "/export" #o777)
326 (symlink "/gnu" "/export/gnu")
327 (start-service 'nscd)
328 (start-service 'networking)
329 (start-service 'nfs))
330 server-marionette)
331
332 ;; Wait for the NFS services to be up and running.
333 (test-assert "nfs services are running"
334 (wait-for-file "/var/run/rpc.statd.pid" server-marionette))
335
336 (test-begin "boot-nfs-root-client")
337 (marionette-eval
338 '(begin
339 (use-modules (gnu services herd))
340
341 (current-output-port
342 (open-file "/dev/console" "w0"))
343 (with-output-to-file "/var/run/mounts"
344 (lambda () (system* "mount")))
345 (chmod "/var/run/mounts" #o777))
346 client-marionette)
347
348 (test-assert "nfs-root-client booted")
349 (marionette-eval
350 '(begin
351 (and (file-exists? "/export/var/run/mounts")
352 (system* "cat" "/export/var/run/mounts")))
353 server-marionette)
354
355 (test-end)
356 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
357
358 (gexp->derivation "nfs-server-test" test))
359
360(define %test-nfs-root-fs
361 (system-test
362 (name "nfs-root-fs")
363 (description "Test that an NFS server can be started and exported
364directories can be mounted.")
365 (value (run-nfs-root-fs-test))))