gnu: tint2: Add source file-name.
[jackhill/guix/guix.git] / gnu / tests / nfs.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2016, 2017, 2020, 2021 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 admin)
37 #:use-module (gnu packages onc-rpc)
38 #:use-module (gnu packages nfs)
39 #:use-module (guix gexp)
40 #:use-module (guix store)
41 #:use-module (guix monads)
42 #:export (%test-nfs
43 %test-nfs-server
44 %test-nfs-full))
45
46 (define %base-os
47 (operating-system
48 (host-name "olitupmok")
49 (timezone "Europe/Berlin")
50 (locale "en_US.UTF-8")
51
52 (bootloader (bootloader-configuration
53 (bootloader grub-bootloader)
54 (targets '("/dev/sdX"))))
55 (file-systems %base-file-systems)
56 (users %base-user-accounts)
57 (packages (cons*
58 rpcbind
59 %base-packages))
60 (services (cons*
61 (service rpcbind-service-type)
62 (service dhcp-client-service-type)
63 %base-services))))
64
65 (define (run-nfs-test name socket)
66 "Run a test of an OS running RPC-SERVICE, which should create SOCKET."
67 (define os
68 (marionette-operating-system
69 %base-os
70 #:imported-modules '((gnu services herd)
71 (guix combinators))))
72
73 (define test
74 (with-imported-modules '((gnu build marionette))
75 #~(begin
76 (use-modules (gnu build marionette)
77 (srfi srfi-64))
78
79 (define marionette
80 (make-marionette (list #$(virtual-machine os))))
81
82 (define (wait-for-socket file)
83 ;; Wait until SOCKET exists in the guest
84 (marionette-eval
85 `(let loop ((i 10))
86 (cond ((and (file-exists? ,file)
87 (eq? 'socket (stat:type (stat ,file))))
88 #t)
89 ((> i 0)
90 (sleep 1)
91 (loop (- i 1)))
92 (else
93 (error "Socket didn't show up: " ,file))))
94 marionette))
95
96 (test-runner-current (system-test-runner #$output))
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
105 ;; Ensure 'rpcinfo' can be found below.
106 (setenv "PATH" "/run/current-system/profile/bin")
107
108 (start-service 'rpcbind-daemon))
109 marionette))
110
111 ;; Check the socket file and that the service is still running.
112 (test-assert "RPC socket exists"
113 (and
114 (wait-for-socket #$socket)
115 (marionette-eval
116 '(begin
117 (use-modules (gnu services herd)
118 (srfi srfi-1))
119
120 (live-service-running
121 (find (lambda (live)
122 (memq 'rpcbind-daemon
123 (live-service-provision live)))
124 (current-services))))
125 marionette)))
126
127 (test-assert "Probe RPC daemon"
128 (marionette-eval
129 '(zero? (system* "rpcinfo" "-p"))
130 marionette))
131
132 (test-end))))
133
134 (gexp->derivation name test))
135
136 (define %test-nfs
137 (system-test
138 (name "nfs")
139 (description "Test some things related to NFS.")
140 (value (run-nfs-test name "/var/run/rpcbind.sock"))))
141
142 \f
143 (define %nfs-os
144 (let ((os (simple-operating-system
145 (simple-service 'create-target-directory activation-service-type
146 #~(begin
147 (mkdir "/remote")
148 (chmod "/remote" #o777)
149 #t))
150 (service dhcp-client-service-type)
151 (service nfs-service-type
152 (nfs-configuration
153 (debug '(nfs nfsd mountd))
154 (exports '(("/export"
155 ;; crossmnt = This is the pseudo root.
156 ;; fsid=0 = root file system of the export
157 "*(ro,insecure,no_subtree_check,crossmnt,fsid=0)"))))))))
158 (operating-system
159 (inherit os)
160 (host-name "nfs-server")
161 ;; We need to use a tmpfs here, because the test system's root file
162 ;; system cannot be re-exported via NFS.
163 (file-systems (cons
164 (file-system
165 (device "none")
166 (mount-point "/export")
167 (type "tmpfs")
168 (create-mount-point? #t))
169 %base-file-systems))
170 (services
171 ;; Enable debugging output.
172 (modify-services (operating-system-user-services os)
173 (syslog-service-type config
174 =>
175 (syslog-configuration
176 (inherit config)
177 (config-file
178 (plain-file
179 "syslog.conf"
180 "*.* /dev/console\n")))))))))
181
182 (define (run-nfs-server-test)
183 "Run a test of an OS running a service of NFS-SERVICE-TYPE."
184 (define os
185 (marionette-operating-system
186 %nfs-os
187 #:requirements '(nscd)
188 #:imported-modules '((gnu services herd)
189 (guix combinators))))
190 (define test
191 (with-imported-modules '((gnu build marionette))
192 #~(begin
193 (use-modules (gnu build marionette)
194 (srfi srfi-64))
195
196 (define marionette
197 (make-marionette (list #$(virtual-machine os))))
198
199 (test-runner-current (system-test-runner #$output))
200 (test-begin "nfs-daemon")
201 (marionette-eval
202 '(begin
203 (current-output-port
204 (open-file "/dev/console" "w0"))
205 (chmod "/export" #o777)
206 (with-output-to-file "/export/hello"
207 (lambda () (display "hello world")))
208 (chmod "/export/hello" #o777))
209 marionette)
210
211 (test-assert "nscd PID file is created"
212 (marionette-eval
213 '(begin
214 (use-modules (gnu services herd))
215 (start-service 'nscd))
216 marionette))
217
218 (test-assert "nscd is listening on its socket"
219 (wait-for-unix-socket "/var/run/nscd/socket"
220 marionette))
221
222 (test-assert "network is up"
223 (marionette-eval
224 '(begin
225 (use-modules (gnu services herd))
226 (start-service 'networking))
227 marionette))
228
229 ;; Wait for the NFS services to be up and running.
230 (test-assert "nfs services are running"
231 (and (marionette-eval
232 '(begin
233 (use-modules (gnu services herd))
234 (start-service 'nfs))
235 marionette)
236 (wait-for-file "/var/run/rpc.statd.pid" marionette)))
237
238 (test-assert "nfs share is advertised"
239 (marionette-eval
240 '(zero? (system* (string-append #$nfs-utils "/sbin/showmount")
241 "-e" "nfs-server"))
242 marionette))
243
244 (test-assert "nfs share mounted"
245 (marionette-eval
246 '(begin
247 (and (zero? (system* (string-append #$nfs-utils "/sbin/mount.nfs4")
248 "nfs-server:/" "/remote" "-v"))
249 (file-exists? "/remote/hello")))
250 marionette))
251 (test-end))))
252
253 (gexp->derivation "nfs-server-test" test))
254
255 (define %test-nfs-server
256 (system-test
257 (name "nfs-server")
258 (description "Test that an NFS server can be started and exported
259 directories can be mounted.")
260 (value (run-nfs-server-test))))
261
262 \f
263 (define (run-nfs-full-test)
264 "Run a test of an OS mounting its root file system via NFS."
265 (define nfs-root-server-os
266 (let ((os (simple-operating-system)))
267 (marionette-operating-system
268 (operating-system
269 (inherit os)
270 (services
271 (cons*
272 (service static-networking-service-type
273 (list
274 (static-networking
275 (addresses (list (network-address
276 (device "ens5")
277 (value "10.0.2.15/24")))))))
278 (simple-service 'export activation-service-type
279 #~(begin
280 (mkdir-p "/export")
281 (chmod "/export" #o777)))
282 (service nfs-service-type
283 (nfs-configuration
284 (nfsd-port 2049)
285 (nfs-versions '("4.2"))
286 (exports '(("/export"
287 "*(rw,insecure,no_subtree_check,\
288 crossmnt,fsid=root,no_root_squash,insecure,async)")))))
289 (modify-services (operating-system-user-services os)
290 (syslog-service-type config
291 =>
292 (syslog-configuration
293 (inherit config)
294 (config-file
295 (plain-file
296 "syslog.conf"
297 "*.* /dev/console\n"))))))))
298 #:requirements '(nscd)
299 #:imported-modules '((gnu services herd)
300 (guix combinators)))))
301
302 (define nfs-root-client-os
303 (marionette-operating-system
304 (simple-operating-system
305 (service static-networking-service-type
306 (list
307 (static-networking
308 (addresses
309 (list (network-address
310 (device "ens5")
311 (value "10.0.2.16/24")))))))
312 (service nfs-service-type
313 (nfs-configuration
314 (nfsd-port 2049)
315 (nfs-versions '("4.2"))))
316 (simple-service 'export activation-service-type
317 #~(begin
318 (mkdir-p "/export")
319 (chmod "/export" #o777))))
320 #:requirements '(nscd)
321 #:imported-modules '((gnu services herd)
322 (guix combinators))))
323
324 (define test
325 (with-imported-modules '((gnu build marionette))
326 #~(begin
327 (use-modules (gnu build marionette)
328 (srfi srfi-64))
329
330 (test-runner-current (system-test-runner #$output))
331 (test-begin "start-nfs-boot-test")
332
333 ;;; Start up NFS server host.
334 (mkdir "/tmp/server")
335 (define server-marionette
336 (make-marionette
337 (cons* #$(virtual-machine
338 (operating-system nfs-root-server-os)
339 (volatile? #f))
340 '("-device" "e1000,netdev=n1,mac=52:54:00:12:34:56"
341 "-netdev" "socket,id=n1,listen=:1234"))
342 #:socket-directory "/tmp/server"))
343
344 ;;; Wait for the NFS services to be up and running.
345 (test-assert "nfs services are running"
346 (wait-for-file "/var/run/rpc.statd.pid" server-marionette))
347
348 (test-assert "NFS port is ready"
349 (wait-for-tcp-port 2049 server-marionette))
350
351 ;;; Start up NFS client host.
352 (mkdir "/tmp/client")
353 (define client-marionette
354 (make-marionette
355 (cons* #$(virtual-machine
356 (operating-system nfs-root-client-os)
357 (volatile? #f))
358 '("-device" "e1000,netdev=n2,mac=52:54:00:12:34:57"
359 "-netdev" "socket,id=n2,connect=127.0.0.1:1234"))
360 #:socket-directory "/tmp/client"))
361
362 (test-assert "NFS port is ready"
363 (wait-for-tcp-port 2049 client-marionette))
364
365 (marionette-eval
366 '(begin
367 (use-modules (rnrs io ports))
368 (current-output-port
369 (open-file "/dev/console" "w0"))
370 (and
371 (system* (string-append #$nfs-utils "/sbin/mount.nfs")
372 "10.0.2.15:/export" "/export" "-v")
373 (let ((content (call-with-input-file "/proc/mounts"
374 get-string-all)))
375 (call-with-output-file "/export/mounts"
376 (lambda (port)
377 (display content port))))))
378 client-marionette)
379
380 ;;; Check whether NFS client host communicated with NFS server host.
381 (test-assert "nfs client deposited file"
382 (wait-for-file "/export/mounts" server-marionette))
383
384 (marionette-eval
385 '(begin
386 (current-output-port
387 (open-file "/dev/console" "w0"))
388 (call-with-input-file "/export/mounts" display))
389 server-marionette)
390
391 (test-end))))
392
393 (gexp->derivation "nfs-full-test" test))
394
395 (define %test-nfs-full
396 (system-test
397 (name "nfs-full")
398 (description "Test that an NFS server can be started and the exported
399 directory can be used by another machine.")
400 (value (run-nfs-full-test))))