tests: nfs-server: Use marionette helper procedures.
[jackhill/guix/guix.git] / gnu / tests / nfs.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2016, 2017, 2020 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
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 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
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 "/var/run/rpcbind.sock"))))
142
143 \f
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 dhcp-client-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 (operating-system-user-services os)
174 (syslog-service-type config
175 =>
176 (syslog-configuration
177 (inherit config)
178 (config-file
179 (plain-file
180 "syslog.conf"
181 "*.* /dev/console\n")))))))))
182
183 (define (run-nfs-server-test)
184 "Run a test of an OS running a service of NFS-SERVICE-TYPE."
185 (define os
186 (marionette-operating-system
187 %nfs-os
188 #:requirements '(nscd)
189 #:imported-modules '((gnu services herd)
190 (guix combinators))))
191 (define test
192 (with-imported-modules '((gnu build marionette))
193 #~(begin
194 (use-modules (gnu build marionette)
195 (srfi srfi-64))
196
197 (define marionette
198 (make-marionette (list #$(virtual-machine os))))
199
200 (mkdir #$output)
201 (chdir #$output)
202
203 (test-begin "nfs-daemon")
204 (marionette-eval
205 '(begin
206 (current-output-port
207 (open-file "/dev/console" "w0"))
208 (chmod "/export" #o777)
209 (with-output-to-file "/export/hello"
210 (lambda () (display "hello world")))
211 (chmod "/export/hello" #o777))
212 marionette)
213
214 (test-assert "nscd PID file is created"
215 (marionette-eval
216 '(begin
217 (use-modules (gnu services herd))
218 (start-service 'nscd))
219 marionette))
220
221 (test-assert "nscd is listening on its socket"
222 (wait-for-unix-socket "/var/run/nscd/socket"
223 marionette))
224
225 (test-assert "network is up"
226 (marionette-eval
227 '(begin
228 (use-modules (gnu services herd))
229 (start-service 'networking))
230 marionette))
231
232 ;; Wait for the NFS services to be up and running.
233 (test-assert "nfs services are running"
234 (and (marionette-eval
235 '(begin
236 (use-modules (gnu services herd))
237 (start-service 'nfs))
238 marionette)
239 (wait-for-file "/var/run/rpc.statd.pid")))
240
241 (test-assert "nfs share is advertised"
242 (marionette-eval
243 '(zero? (system* (string-append #$nfs-utils "/sbin/showmount")
244 "-e" "nfs-server"))
245 marionette))
246
247 (test-assert "nfs share mounted"
248 (marionette-eval
249 '(begin
250 (and (zero? (system* (string-append #$nfs-utils "/sbin/mount.nfs4")
251 "nfs-server:/" "/remote" "-v"))
252 (file-exists? "/remote/hello")))
253 marionette))
254 (test-end)
255 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
256
257 (gexp->derivation "nfs-server-test" test))
258
259 (define %test-nfs-server
260 (system-test
261 (name "nfs-server")
262 (description "Test that an NFS server can be started and exported
263 directories can be mounted.")
264 (value (run-nfs-server-test))))