nfs.scm 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2016, 2017 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. (define-module (gnu tests nfs)
  24. #:use-module (gnu tests)
  25. #:use-module (gnu bootloader)
  26. #:use-module (gnu bootloader grub)
  27. #:use-module (gnu system)
  28. #:use-module (gnu system file-systems)
  29. #:use-module (gnu system shadow)
  30. #:use-module (gnu system vm)
  31. #:use-module (gnu services)
  32. #:use-module (gnu services base)
  33. #:use-module (gnu services nfs)
  34. #:use-module (gnu services networking)
  35. #:use-module (gnu packages onc-rpc)
  36. #:use-module (gnu packages nfs)
  37. #:use-module (guix gexp)
  38. #:use-module (guix store)
  39. #:use-module (guix monads)
  40. #:export (%test-nfs
  41. %test-nfs-server))
  42. (define %base-os
  43. (operating-system
  44. (host-name "olitupmok")
  45. (timezone "Europe/Berlin")
  46. (locale "en_US.UTF-8")
  47. (bootloader (bootloader-configuration
  48. (bootloader grub-bootloader)
  49. (target "/dev/sdX")))
  50. (file-systems %base-file-systems)
  51. (users %base-user-accounts)
  52. (packages (cons*
  53. rpcbind
  54. %base-packages))
  55. (services (cons*
  56. (service rpcbind-service-type)
  57. (service dhcp-client-service-type)
  58. %base-services))))
  59. (define (run-nfs-test name socket)
  60. "Run a test of an OS running RPC-SERVICE, which should create SOCKET."
  61. (define os
  62. (marionette-operating-system
  63. %base-os
  64. #:imported-modules '((gnu services herd)
  65. (guix combinators))))
  66. (define test
  67. (with-imported-modules '((gnu build marionette))
  68. #~(begin
  69. (use-modules (gnu build marionette)
  70. (srfi srfi-64))
  71. (define marionette
  72. (make-marionette (list #$(virtual-machine os))))
  73. (define (wait-for-socket file)
  74. ;; Wait until SOCKET exists in the guest
  75. (marionette-eval
  76. `(let loop ((i 10))
  77. (cond ((and (file-exists? ,file)
  78. (eq? 'socket (stat:type (stat ,file))))
  79. #t)
  80. ((> i 0)
  81. (sleep 1)
  82. (loop (- i 1)))
  83. (else
  84. (error "Socket didn't show up: " ,file))))
  85. marionette))
  86. (mkdir #$output)
  87. (chdir #$output)
  88. (test-begin "rpc-daemon")
  89. ;; Wait for the rpcbind daemon to be up and running.
  90. (test-assert "RPC service running"
  91. (marionette-eval
  92. '(begin
  93. (use-modules (gnu services herd))
  94. (start-service 'rpcbind-daemon))
  95. marionette))
  96. ;; Check the socket file and that the service is still running.
  97. (test-assert "RPC socket exists"
  98. (and
  99. (wait-for-socket #$socket)
  100. (marionette-eval
  101. '(begin
  102. (use-modules (gnu services herd)
  103. (srfi srfi-1))
  104. (live-service-running
  105. (find (lambda (live)
  106. (memq 'rpcbind-daemon
  107. (live-service-provision live)))
  108. (current-services))))
  109. marionette)))
  110. (test-assert "Probe RPC daemon"
  111. (marionette-eval
  112. '(zero? (system* "rpcinfo" "-p"))
  113. marionette))
  114. (test-end)
  115. (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
  116. (gexp->derivation name test))
  117. (define %test-nfs
  118. (system-test
  119. (name "nfs")
  120. (description "Test some things related to NFS.")
  121. (value (run-nfs-test name "/var/run/rpcbind.sock"))))
  122. (define %nfs-os
  123. (let ((os (simple-operating-system
  124. (simple-service 'create-target-directory activation-service-type
  125. #~(begin
  126. (mkdir "/remote")
  127. (chmod "/remote" #o777)
  128. #t))
  129. (service dhcp-client-service-type)
  130. (service nfs-service-type
  131. (nfs-configuration
  132. (debug '(nfs nfsd mountd))
  133. (exports '(("/export"
  134. ;; crossmnt = This is the pseudo root.
  135. ;; fsid=0 = root file system of the export
  136. "*(ro,insecure,no_subtree_check,crossmnt,fsid=0)"))))))))
  137. (operating-system
  138. (inherit os)
  139. (host-name "nfs-server")
  140. ;; We need to use a tmpfs here, because the test system's root file
  141. ;; system cannot be re-exported via NFS.
  142. (file-systems (cons
  143. (file-system
  144. (device "none")
  145. (mount-point "/export")
  146. (type "tmpfs")
  147. (create-mount-point? #t))
  148. %base-file-systems))
  149. (services
  150. ;; Enable debugging output.
  151. (modify-services (operating-system-user-services os)
  152. (syslog-service-type config
  153. =>
  154. (syslog-configuration
  155. (inherit config)
  156. (config-file
  157. (plain-file
  158. "syslog.conf"
  159. "*.* /dev/console\n")))))))))
  160. (define (run-nfs-server-test)
  161. "Run a test of an OS running a service of NFS-SERVICE-TYPE."
  162. (define os
  163. (marionette-operating-system
  164. %nfs-os
  165. #:requirements '(nscd)
  166. #:imported-modules '((gnu services herd)
  167. (guix combinators))))
  168. (define test
  169. (with-imported-modules '((gnu build marionette))
  170. #~(begin
  171. (use-modules (gnu build marionette)
  172. (srfi srfi-64))
  173. (define marionette
  174. (make-marionette (list #$(virtual-machine os))))
  175. (define (wait-for-file file)
  176. ;; Wait until FILE exists in the guest
  177. (marionette-eval
  178. `(let loop ((i 10))
  179. (cond ((file-exists? ,file)
  180. #t)
  181. ((> i 0)
  182. (sleep 1)
  183. (loop (- i 1)))
  184. (else
  185. (error "File didn't show up: " ,file))))
  186. marionette))
  187. (mkdir #$output)
  188. (chdir #$output)
  189. (test-begin "nfs-daemon")
  190. (marionette-eval
  191. '(begin
  192. (current-output-port
  193. (open-file "/dev/console" "w0"))
  194. (chmod "/export" #o777)
  195. (with-output-to-file "/export/hello"
  196. (lambda () (display "hello world")))
  197. (chmod "/export/hello" #o777))
  198. marionette)
  199. (test-assert "nscd PID file is created"
  200. (marionette-eval
  201. '(begin
  202. (use-modules (gnu services herd))
  203. (start-service 'nscd))
  204. marionette))
  205. (test-assert "nscd is listening on its socket"
  206. (marionette-eval
  207. ;; XXX: Work around a race condition in nscd: nscd creates its
  208. ;; PID file before it is listening on its socket.
  209. '(let ((sock (socket PF_UNIX SOCK_STREAM 0)))
  210. (let try ()
  211. (catch 'system-error
  212. (lambda ()
  213. (connect sock AF_UNIX "/var/run/nscd/socket")
  214. (close-port sock)
  215. (format #t "nscd is ready~%")
  216. #t)
  217. (lambda args
  218. (format #t "waiting for nscd...~%")
  219. (usleep 500000)
  220. (try)))))
  221. marionette))
  222. (test-assert "network is up"
  223. (marionette-eval
  224. '(begin
  225. (use-modules (gnu services herd))
  226. (start-service 'networking))
  227. marionette))
  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")))
  236. (test-assert "nfs share is advertised"
  237. (marionette-eval
  238. '(zero? (system* (string-append #$nfs-utils "/sbin/showmount")
  239. "-e" "nfs-server"))
  240. marionette))
  241. (test-assert "nfs share mounted"
  242. (marionette-eval
  243. '(begin
  244. (and (zero? (system* (string-append #$nfs-utils "/sbin/mount.nfs4")
  245. "nfs-server:/" "/remote" "-v"))
  246. (file-exists? "/remote/hello")))
  247. marionette))
  248. (test-end)
  249. (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
  250. (gexp->derivation "nfs-server-test" test))
  251. (define %test-nfs-server
  252. (system-test
  253. (name "nfs-server")
  254. (description "Test that an NFS server can be started and exported
  255. directories can be mounted.")
  256. (value (run-nfs-server-test))))