gnu: rust-rand-0.4: Don't skip build.
[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
RW
41 #:export (%test-nfs
42 %test-nfs-server))
7d73b2c6
JD
43
44(define %base-os
45 (operating-system
46 (host-name "olitupmok")
47 (timezone "Europe/Berlin")
48 (locale "en_US.UTF-8")
49
fdfdecdb
TGR
50 (bootloader (bootloader-configuration
51 (bootloader grub-bootloader)
52 (target "/dev/sdX")))
7d73b2c6
JD
53 (file-systems %base-file-systems)
54 (users %base-user-accounts)
55 (packages (cons*
56 rpcbind
57 %base-packages))
58 (services (cons*
907eeac2 59 (service rpcbind-service-type)
39d7fdce 60 (service dhcp-client-service-type)
7d73b2c6
JD
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."
8b113790
LC
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.
c24b1547 100 (test-assert "RPC service running"
8b113790
LC
101 (marionette-eval
102 '(begin
103 (use-modules (gnu services herd))
e8cec7cc
LC
104
105 ;; Ensure 'rpcinfo' can be found below.
106 (setenv "PATH" "/run/current-system/profile/bin")
107
c24b1547 108 (start-service 'rpcbind-daemon))
8b113790
LC
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))
7d73b2c6
JD
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"))))
907eeac2
RW
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 (define (wait-for-file file)
200 ;; Wait until FILE exists in the guest
201 (marionette-eval
202 `(let loop ((i 10))
203 (cond ((file-exists? ,file)
204 #t)
205 ((> i 0)
206 (sleep 1)
207 (loop (- i 1)))
208 (else
209 (error "File didn't show up: " ,file))))
210 marionette))
211
212 (mkdir #$output)
213 (chdir #$output)
214
215 (test-begin "nfs-daemon")
216 (marionette-eval
217 '(begin
218 (current-output-port
219 (open-file "/dev/console" "w0"))
220 (chmod "/export" #o777)
221 (with-output-to-file "/export/hello"
222 (lambda () (display "hello world")))
223 (chmod "/export/hello" #o777))
224 marionette)
225
226 (test-assert "nscd PID file is created"
227 (marionette-eval
228 '(begin
229 (use-modules (gnu services herd))
230 (start-service 'nscd))
231 marionette))
232
233 (test-assert "nscd is listening on its socket"
234 (marionette-eval
235 ;; XXX: Work around a race condition in nscd: nscd creates its
236 ;; PID file before it is listening on its socket.
237 '(let ((sock (socket PF_UNIX SOCK_STREAM 0)))
238 (let try ()
239 (catch 'system-error
240 (lambda ()
241 (connect sock AF_UNIX "/var/run/nscd/socket")
242 (close-port sock)
243 (format #t "nscd is ready~%")
244 #t)
245 (lambda args
246 (format #t "waiting for nscd...~%")
247 (usleep 500000)
248 (try)))))
249 marionette))
250
251 (test-assert "network is up"
252 (marionette-eval
253 '(begin
254 (use-modules (gnu services herd))
255 (start-service 'networking))
256 marionette))
257
258 ;; Wait for the NFS services to be up and running.
259 (test-assert "nfs services are running"
260 (and (marionette-eval
261 '(begin
262 (use-modules (gnu services herd))
263 (start-service 'nfs))
264 marionette)
265 (wait-for-file "/var/run/rpc.statd.pid")))
266
267 (test-assert "nfs share is advertised"
268 (marionette-eval
269 '(zero? (system* (string-append #$nfs-utils "/sbin/showmount")
270 "-e" "nfs-server"))
271 marionette))
272
273 (test-assert "nfs share mounted"
274 (marionette-eval
275 '(begin
276 (and (zero? (system* (string-append #$nfs-utils "/sbin/mount.nfs4")
277 "nfs-server:/" "/remote" "-v"))
278 (file-exists? "/remote/hello")))
279 marionette))
280 (test-end)
281 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
282
283 (gexp->derivation "nfs-server-test" test))
284
285(define %test-nfs-server
286 (system-test
287 (name "nfs-server")
288 (description "Test that an NFS server can be started and exported
289directories can be mounted.")
290 (value (run-nfs-server-test))))