services: databases: Don't specify a default postgresql version.
[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 266
c4fe53bd 267\f
a1a39ed5
S
268(define (run-nfs-root-fs-test)
269 "Run a test of an OS mounting its root file system via NFS."
270 (define nfs-root-server-os
271 (marionette-operating-system
272 (operating-system
273 (inherit %nfs-os)
a1a39ed5
S
274 (services
275 (modify-services (operating-system-user-services %nfs-os)
c41a8572 276 (nfs-service-type config =>
a1a39ed5
S
277 (nfs-configuration
278 (debug '(nfs nfsd mountd))
c41a8572
DM
279 ;;; Note: Adding the following line causes Guix to hang.
280 ;(rpcmountd-port 20001)
281 ;;; Note: Adding the following line causes Guix to hang.
282 ;(rpcstatd-port 20002) ; FIXME: Set broadcast port AND listening port.
283 (nfsd-port 2049)
284 (nfs-versions '("4.2"))
a1a39ed5
S
285 (exports '(("/export"
286 "*(rw,insecure,no_subtree_check,crossmnt,fsid=root,no_root_squash,insecure,async)"))))))))
287 #:requirements '(nscd)
288 #:imported-modules '((gnu services herd)
289 (guix combinators))))
290
291 (define nfs-root-client-os
292 (marionette-operating-system
293 (operating-system
c41a8572 294 (inherit (simple-operating-system (service dhcp-client-service-type)))
a1a39ed5
S
295 (kernel-arguments '("ip=dhcp"))
296 (file-systems (cons
297 (file-system
298 (type "nfs")
299 (mount-point "/")
300 (device ":/export")
c41a8572 301 (options "addr=127.0.0.1,vers=4.2"))
a1a39ed5
S
302 %base-file-systems)))
303 #:requirements '(nscd)
304 #:imported-modules '((gnu services herd)
305 (guix combinators))))
306
307 (define test
308 (with-imported-modules '((gnu build marionette))
309 #~(begin
310 (use-modules (gnu build marionette)
311 (srfi srfi-64))
312
a1a39ed5
S
313 (mkdir #$output)
314 (chdir #$output)
315
c4fe53bd 316 (test-begin "start-nfs-boot-test")
c41a8572
DM
317
318 ;;; Start up NFS server host.
319
320 (mkdir "/tmp/server")
321 (define server-marionette
322 (make-marionette (list #$(virtual-machine
323 nfs-root-server-os
324 ;(operating-system nfs-root-server-os)
325 ;(port-forwardings '( ; (111 . 111)
326 ; (2049 . 2049)
327 ; (20001 . 20001)
328 ; (20002 . 20002)))
329))
330 #:socket-directory "/tmp/server"))
331
a1a39ed5
S
332 (marionette-eval
333 '(begin
334 (use-modules (gnu services herd))
a1a39ed5
S
335 (current-output-port
336 (open-file "/dev/console" "w0"))
c4fe53bd 337 ;; FIXME: Instead statfs "/" and "/export" and wait until they
c41a8572
DM
338 ;; are different file systems. But Guile doesn't seem to have
339 ;; statfs.
340 (sleep 5)
a1a39ed5
S
341 (chmod "/export" #o777)
342 (symlink "/gnu" "/export/gnu")
343 (start-service 'nscd)
344 (start-service 'networking)
345 (start-service 'nfs))
346 server-marionette)
347
c41a8572
DM
348 ;;; Wait for the NFS services to be up and running.
349
a1a39ed5
S
350 (test-assert "nfs services are running"
351 (wait-for-file "/var/run/rpc.statd.pid" server-marionette))
352
c41a8572
DM
353 (test-assert "NFS port is ready"
354 (wait-for-tcp-port 2049 server-marionette))
355
356 (test-assert "NFS statd port is ready"
357 (wait-for-tcp-port 20002 server-marionette))
358
359 (test-assert "NFS mountd port is ready"
360 (wait-for-tcp-port 20001 server-marionette))
361
362 ;;; FIXME: (test-assert "NFS portmapper port is ready"
363 ;;; FIXME: (wait-for-tcp-port 111 server-marionette))
364
365 ;;; Start up NFS client host.
366
367 (define client-marionette
368 (make-marionette (list #$(virtual-machine
369 nfs-root-client-os
370 ;(port-forwardings '((111 . 111)
371 ; (2049 . 2049)
372 ; (20001 . 20001)
373 ; (20002 . 20002)))
374 ))))
375
a1a39ed5
S
376 (marionette-eval
377 '(begin
378 (use-modules (gnu services herd))
c4fe53bd 379 (use-modules (rnrs io ports))
a1a39ed5
S
380
381 (current-output-port
382 (open-file "/dev/console" "w0"))
c4fe53bd
DM
383 (let ((content (call-with-input-file "/proc/mounts" get-string-all)))
384 (call-with-output-file "/mounts.new"
385 (lambda (port)
386 (display content port))))
387 (chmod "/mounts.new" #o777)
388 (rename-file "/mounts.new" "/mounts"))
a1a39ed5
S
389 client-marionette)
390
391 (test-assert "nfs-root-client booted")
c4fe53bd 392
c41a8572
DM
393 ;;; Check whether NFS client host communicated with NFS server host.
394
c4fe53bd
DM
395 (test-assert "nfs client deposited file"
396 (wait-for-file "/export/mounts" server-marionette))
a1a39ed5
S
397 (marionette-eval
398 '(begin
c4fe53bd
DM
399 (current-output-port
400 (open-file "/dev/console" "w0"))
401 (call-with-input-file "/export/mounts" display))
a1a39ed5
S
402 server-marionette)
403
404 (test-end)
405 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
406
407 (gexp->derivation "nfs-server-test" test))
408
409(define %test-nfs-root-fs
410 (system-test
411 (name "nfs-root-fs")
c41a8572
DM
412 (description "Test that an NFS server can be started and the exported
413directory can be used as root filesystem.")
a1a39ed5 414 (value (run-nfs-root-fs-test))))