projects/guix·blob master · refs

397 lines · 14.1 KiB

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