base.scm 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2016, 2017, 2018, 2019 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2018 Clément Lassieur <clement@lassieur.org>
  4. ;;;
  5. ;;; This file is part of GNU Guix.
  6. ;;;
  7. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  8. ;;; under the terms of the GNU General Public License as published by
  9. ;;; the Free Software Foundation; either version 3 of the License, or (at
  10. ;;; your option) any later version.
  11. ;;;
  12. ;;; GNU Guix is distributed in the hope that it will be useful, but
  13. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;;; GNU General Public License for more details.
  16. ;;;
  17. ;;; You should have received a copy of the GNU General Public License
  18. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  19. (define-module (gnu tests base)
  20. #:use-module (gnu tests)
  21. #:use-module (gnu system)
  22. #:use-module (gnu system shadow)
  23. #:use-module (gnu system nss)
  24. #:use-module (gnu system vm)
  25. #:use-module (gnu services)
  26. #:use-module (gnu services base)
  27. #:use-module (gnu services dbus)
  28. #:use-module (gnu services avahi)
  29. #:use-module (gnu services mcron)
  30. #:use-module (gnu services shepherd)
  31. #:use-module (gnu services networking)
  32. #:use-module (gnu packages base)
  33. #:use-module (gnu packages bash)
  34. #:use-module (gnu packages imagemagick)
  35. #:use-module (gnu packages ocr)
  36. #:use-module (gnu packages package-management)
  37. #:use-module (gnu packages linux)
  38. #:use-module (gnu packages tmux)
  39. #:use-module (guix gexp)
  40. #:use-module (guix store)
  41. #:use-module (guix monads)
  42. #:use-module (guix packages)
  43. #:use-module (srfi srfi-1)
  44. #:use-module (ice-9 match)
  45. #:export (run-basic-test
  46. %test-basic-os
  47. %test-halt
  48. %test-cleanup
  49. %test-mcron
  50. %test-nss-mdns))
  51. (define %simple-os
  52. (simple-operating-system))
  53. (define* (run-basic-test os command #:optional (name "basic")
  54. #:key initialization)
  55. "Return a derivation called NAME that tests basic features of the OS started
  56. using COMMAND, a gexp that evaluates to a list of strings. Compare some
  57. properties of running system to what's declared in OS, an <operating-system>.
  58. When INITIALIZATION is true, it must be a one-argument procedure that is
  59. passed a gexp denoting the marionette, and it must return gexp that is
  60. inserted before the first test. This is used to introduce an extra
  61. initialization step, such as entering a LUKS passphrase."
  62. (define special-files
  63. (service-value
  64. (fold-services (operating-system-services os)
  65. #:target-type special-files-service-type)))
  66. (define guix&co
  67. (match (package-transitive-propagated-inputs guix)
  68. (((labels packages) ...)
  69. (cons guix packages))))
  70. (define test
  71. (with-imported-modules '((gnu build marionette)
  72. (guix build syscalls))
  73. #~(begin
  74. (use-modules (gnu build marionette)
  75. (guix build syscalls)
  76. (srfi srfi-1)
  77. (srfi srfi-26)
  78. (srfi srfi-64)
  79. (ice-9 match))
  80. (define marionette
  81. (make-marionette #$command))
  82. (mkdir #$output)
  83. (chdir #$output)
  84. (test-begin "basic")
  85. #$(and initialization
  86. (initialization #~marionette))
  87. (test-assert "uname"
  88. (match (marionette-eval '(uname) marionette)
  89. (#("Linux" host-name version _ architecture)
  90. (and (string=? host-name
  91. #$(operating-system-host-name os))
  92. (string-prefix? #$(package-version
  93. (operating-system-kernel os))
  94. version)
  95. (string-prefix? architecture %host-type)))))
  96. ;; Shepherd reads the config file *before* binding its control
  97. ;; socket, so /var/run/shepherd/socket might not exist yet when the
  98. ;; 'marionette' service is started.
  99. (test-assert "shepherd socket ready"
  100. (marionette-eval
  101. `(begin
  102. (use-modules (gnu services herd))
  103. (let loop ((i 10))
  104. (cond ((file-exists? (%shepherd-socket-file))
  105. #t)
  106. ((> i 0)
  107. (sleep 1)
  108. (loop (- i 1)))
  109. (else
  110. #f))))
  111. marionette))
  112. (test-eq "stdin is /dev/null"
  113. 'eof
  114. ;; Make sure services can no longer read from stdin once the
  115. ;; system has booted.
  116. (marionette-eval
  117. `(begin
  118. (use-modules (gnu services herd))
  119. (start 'user-processes)
  120. ((@@ (gnu services herd) eval-there)
  121. '(let ((result (read (current-input-port))))
  122. (if (eof-object? result)
  123. 'eof
  124. result))))
  125. marionette))
  126. (test-assert "shell and user commands"
  127. ;; Is everything in $PATH?
  128. (zero? (marionette-eval '(system "
  129. . /etc/profile
  130. set -e -x
  131. guix --version
  132. ls --version
  133. grep --version
  134. info --version")
  135. marionette)))
  136. (test-equal "special files"
  137. '#$special-files
  138. (marionette-eval
  139. '(begin
  140. (use-modules (ice-9 match))
  141. (map (match-lambda
  142. ((file target)
  143. (list file (readlink file))))
  144. '#$special-files))
  145. marionette))
  146. (test-assert "accounts"
  147. (let ((users (marionette-eval '(begin
  148. (use-modules (ice-9 match))
  149. (let loop ((result '()))
  150. (match (getpw)
  151. (#f (reverse result))
  152. (x (loop (cons x result))))))
  153. marionette)))
  154. (lset= equal?
  155. (map (lambda (user)
  156. (list (passwd:name user)
  157. (passwd:dir user)))
  158. users)
  159. (list
  160. #$@(map (lambda (account)
  161. `(list ,(user-account-name account)
  162. ,(user-account-home-directory account)))
  163. (operating-system-user-accounts os))))))
  164. (test-assert "shepherd services"
  165. (let ((services (marionette-eval
  166. '(begin
  167. (use-modules (gnu services herd))
  168. (map (compose car live-service-provision)
  169. (current-services)))
  170. marionette)))
  171. (lset= eq?
  172. (pk 'services services)
  173. '(root #$@(operating-system-shepherd-service-names os)))))
  174. (test-assert "homes"
  175. (let ((homes
  176. '#$(map user-account-home-directory
  177. (filter user-account-create-home-directory?
  178. (operating-system-user-accounts os)))))
  179. (marionette-eval
  180. `(begin
  181. (use-modules (gnu services herd) (srfi srfi-1))
  182. ;; Home directories are supposed to exist once 'user-homes'
  183. ;; has been started.
  184. (start-service 'user-homes)
  185. (every (lambda (home)
  186. (and (file-exists? home)
  187. (file-is-directory? home)))
  188. ',homes))
  189. marionette)))
  190. (test-assert "skeletons in home directories"
  191. (let ((users+homes
  192. '#$(filter-map (lambda (account)
  193. (and (user-account-create-home-directory?
  194. account)
  195. (not (user-account-system? account))
  196. (list (user-account-name account)
  197. (user-account-home-directory
  198. account))))
  199. (operating-system-user-accounts os))))
  200. (marionette-eval
  201. `(begin
  202. (use-modules (srfi srfi-1) (ice-9 ftw)
  203. (ice-9 match))
  204. (every (match-lambda
  205. ((user home)
  206. ;; Make sure HOME has all the skeletons...
  207. (and (null? (lset-difference string=?
  208. (scandir "/etc/skel/")
  209. (scandir home)))
  210. ;; ... and that everything is user-owned.
  211. (let* ((pw (getpwnam user))
  212. (uid (passwd:uid pw))
  213. (gid (passwd:gid pw))
  214. (st (lstat home)))
  215. (define (user-owned? file)
  216. (= uid (stat:uid (lstat file))))
  217. (and (= uid (stat:uid st))
  218. (eq? 'directory (stat:type st))
  219. (every user-owned?
  220. (find-files home
  221. #:directories? #t)))))))
  222. ',users+homes))
  223. marionette)))
  224. (test-equal "permissions on /root"
  225. #o700
  226. (let ((root-home #$(any (lambda (account)
  227. (and (zero? (user-account-uid account))
  228. (user-account-home-directory
  229. account)))
  230. (operating-system-user-accounts os))))
  231. (stat:perms (marionette-eval `(stat ,root-home) marionette))))
  232. (test-equal "no extra home directories"
  233. '()
  234. ;; Make sure the home directories that are not supposed to be
  235. ;; created are indeed not created.
  236. (let ((nonexistent
  237. '#$(filter-map (lambda (user)
  238. (and (not
  239. (user-account-create-home-directory?
  240. user))
  241. (user-account-home-directory user)))
  242. (operating-system-user-accounts os))))
  243. (marionette-eval
  244. `(begin
  245. (use-modules (srfi srfi-1))
  246. ;; Note: Do not flag "/var/empty".
  247. (filter file-exists?
  248. ',(remove (cut string-prefix? "/var/" <>)
  249. nonexistent)))
  250. marionette)))
  251. (test-equal "login on tty1"
  252. "root\n"
  253. (begin
  254. (marionette-control "sendkey ctrl-alt-f1" marionette)
  255. ;; Wait for the 'term-tty1' service to be running (using
  256. ;; 'start-service' is the simplest and most reliable way to do
  257. ;; that.)
  258. (marionette-eval
  259. '(begin
  260. (use-modules (gnu services herd))
  261. (start-service 'term-tty1))
  262. marionette)
  263. ;; Now we can type.
  264. (marionette-type "root\n\nid -un > logged-in\n" marionette)
  265. ;; It can take a while before the shell commands are executed.
  266. (marionette-eval '(use-modules (rnrs io ports)) marionette)
  267. (wait-for-file "/root/logged-in" marionette
  268. #:read 'get-string-all)))
  269. ;; There should be one utmpx entry for the user logged in on tty1.
  270. (test-equal "utmpx entry"
  271. '(("root" "tty1" #f))
  272. (marionette-eval
  273. '(begin
  274. (use-modules (guix build syscalls)
  275. (srfi srfi-1))
  276. (filter-map (lambda (entry)
  277. (and (equal? (login-type USER_PROCESS)
  278. (utmpx-login-type entry))
  279. (list (utmpx-user entry) (utmpx-line entry)
  280. (utmpx-host entry))))
  281. (utmpx-entries)))
  282. marionette))
  283. ;; Likewise for /var/log/wtmp (used by 'last').
  284. (test-assert "wtmp entry"
  285. (match (marionette-eval
  286. '(begin
  287. (use-modules (guix build syscalls)
  288. (srfi srfi-1))
  289. (define (entry->list entry)
  290. (list (utmpx-user entry) (utmpx-line entry)
  291. (utmpx-host entry) (utmpx-login-type entry)))
  292. (call-with-input-file "/var/log/wtmp"
  293. (lambda (port)
  294. (let loop ((result '()))
  295. (if (eof-object? (peek-char port))
  296. (map entry->list (reverse result))
  297. (loop (cons (read-utmpx port) result)))))))
  298. marionette)
  299. (((users lines hosts types) ..1)
  300. (every (lambda (type)
  301. (eqv? type (login-type LOGIN_PROCESS)))
  302. types))))
  303. (test-assert "host name resolution"
  304. (match (marionette-eval
  305. '(begin
  306. ;; Wait for nscd or our requests go through it.
  307. (use-modules (gnu services herd))
  308. (start-service 'nscd)
  309. (list (getaddrinfo "localhost")
  310. (getaddrinfo #$(operating-system-host-name os))))
  311. marionette)
  312. ((((? vector?) ..1) ((? vector?) ..1))
  313. #t)
  314. (x
  315. (pk 'failure x #f))))
  316. (test-equal "nscd invalidate action"
  317. '(#t) ;one value, #t
  318. (marionette-eval '(with-shepherd-action 'nscd ('invalidate "hosts")
  319. result
  320. result)
  321. marionette))
  322. (test-equal "nscd invalidate action, wrong table"
  323. '(#f) ;one value, #f
  324. (marionette-eval '(with-shepherd-action 'nscd ('invalidate "xyz")
  325. result
  326. result)
  327. marionette))
  328. (test-equal "host not found"
  329. #f
  330. (marionette-eval
  331. '(false-if-exception (getaddrinfo "does-not-exist"))
  332. marionette))
  333. (test-equal "locale"
  334. "en_US.utf8"
  335. (marionette-eval '(let ((before (setlocale LC_ALL "en_US.utf8")))
  336. (setlocale LC_ALL before))
  337. marionette))
  338. (test-eq "/run/current-system is a GC root"
  339. 'success!
  340. (marionette-eval '(begin
  341. ;; Make sure the (guix …) modules are found.
  342. (eval-when (expand load eval)
  343. (set! %load-path
  344. (append (map (lambda (package)
  345. (string-append package
  346. "/share/guile/site/"
  347. (effective-version)))
  348. '#$guix&co)
  349. %load-path)))
  350. (use-modules (srfi srfi-34) (guix store))
  351. (let ((system (readlink "/run/current-system")))
  352. (guard (c ((nix-protocol-error? c)
  353. (and (file-exists? system)
  354. 'success!)))
  355. (with-store store
  356. (delete-paths store (list system))
  357. #f))))
  358. marionette))
  359. ;; This symlink is currently unused, but better have it point to the
  360. ;; right place. See
  361. ;; <https://lists.gnu.org/archive/html/guix-devel/2016-08/msg01641.html>.
  362. (test-equal "/var/guix/gcroots/profiles is a valid symlink"
  363. "/var/guix/profiles"
  364. (marionette-eval '(readlink "/var/guix/gcroots/profiles")
  365. marionette))
  366. (test-assert "screendump"
  367. (begin
  368. (marionette-control (string-append "screendump " #$output
  369. "/tty1.ppm")
  370. marionette)
  371. (file-exists? "tty1.ppm")))
  372. (test-assert "screen text"
  373. (let ((text (marionette-screen-text marionette
  374. #:ocrad
  375. #$(file-append ocrad
  376. "/bin/ocrad"))))
  377. ;; Check whether the welcome message and shell prompt are
  378. ;; displayed. Note: OCR confuses "y" and "V" for instance, so
  379. ;; we cannot reliably match the whole text.
  380. (and (string-contains text "This is the GNU")
  381. (string-contains text
  382. (string-append
  383. "root@"
  384. #$(operating-system-host-name os))))))
  385. (test-end)
  386. (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
  387. (gexp->derivation name test))
  388. (define %test-basic-os
  389. (system-test
  390. (name "basic")
  391. (description
  392. "Instrument %SIMPLE-OS, run it in a VM, and run a series of basic
  393. functionality tests.")
  394. (value
  395. (let* ((os (marionette-operating-system
  396. %simple-os
  397. #:imported-modules '((gnu services herd)
  398. (guix combinators))))
  399. (vm (virtual-machine os)))
  400. ;; XXX: Add call to 'virtualized-operating-system' to get the exact same
  401. ;; set of services as the OS produced by
  402. ;; 'system-qemu-image/shared-store-script'.
  403. (run-basic-test (virtualized-operating-system os '())
  404. #~(list #$vm))))))
  405. ;;;
  406. ;;; Halt.
  407. ;;;
  408. (define (run-halt-test vm)
  409. ;; As reported in <http://bugs.gnu.org/26931>, running tmux would previously
  410. ;; lead the 'stop' method of 'user-processes' to an infinite loop, with the
  411. ;; tmux server process as a zombie that remains in the list of processes.
  412. ;; This test reproduces this scenario.
  413. (define test
  414. (with-imported-modules '((gnu build marionette))
  415. #~(begin
  416. (use-modules (gnu build marionette))
  417. (define marionette
  418. (make-marionette '(#$vm)))
  419. (define ocrad
  420. #$(file-append ocrad "/bin/ocrad"))
  421. ;; Wait for tty1 and log in.
  422. (marionette-eval '(begin
  423. (use-modules (gnu services herd))
  424. (start-service 'term-tty1))
  425. marionette)
  426. (marionette-type "root\n" marionette)
  427. (wait-for-screen-text marionette
  428. (lambda (text)
  429. (string-contains text "root@komputilo"))
  430. #:ocrad ocrad)
  431. ;; Start tmux and wait for it to be ready.
  432. (marionette-type "tmux new-session 'echo 1 > /ready; bash'\n"
  433. marionette)
  434. (wait-for-file "/ready" marionette)
  435. ;; Make sure to stop the test after a while.
  436. (sigaction SIGALRM (lambda _
  437. (format (current-error-port)
  438. "FAIL: Time is up, but VM still running.\n")
  439. (primitive-exit 1)))
  440. (alarm 10)
  441. ;; Get debugging info.
  442. (marionette-eval '(current-output-port
  443. (open-file "/dev/console" "w0"))
  444. marionette)
  445. (marionette-eval '(system* #$(file-append procps "/bin/ps")
  446. "-eo" "pid,ppid,stat,comm")
  447. marionette)
  448. ;; See if 'halt' actually works.
  449. (marionette-eval '(system* "/run/current-system/profile/sbin/halt")
  450. marionette)
  451. ;; If we reach this line, that means the VM was properly stopped in
  452. ;; a timely fashion.
  453. (alarm 0)
  454. (call-with-output-file #$output
  455. (lambda (port)
  456. (display "success!" port))))))
  457. (gexp->derivation "halt" test))
  458. (define %test-halt
  459. (system-test
  460. (name "halt")
  461. (description
  462. "Use the 'halt' command and make sure it succeeds and does not get stuck
  463. in a loop. See <http://bugs.gnu.org/26931>.")
  464. (value
  465. (let ((os (marionette-operating-system
  466. (operating-system
  467. (inherit %simple-os)
  468. (packages (cons tmux %base-packages)))
  469. #:imported-modules '((gnu services herd)
  470. (guix combinators)))))
  471. (run-halt-test (virtual-machine os))))))
  472. ;;;
  473. ;;; Cleanup of /tmp, /var/run, etc.
  474. ;;;
  475. (define %cleanup-os
  476. (simple-operating-system
  477. (simple-service 'dirty-things
  478. boot-service-type
  479. (let ((script (plain-file
  480. "create-utf8-file.sh"
  481. (string-append
  482. "echo $0: dirtying /tmp...\n"
  483. "set -e; set -x\n"
  484. "touch /witness\n"
  485. "exec touch /tmp/λαμβδα"))))
  486. (with-imported-modules '((guix build utils))
  487. #~(begin
  488. (setenv "PATH"
  489. #$(file-append coreutils "/bin"))
  490. (invoke #$(file-append bash "/bin/sh")
  491. #$script)))))))
  492. (define (run-cleanup-test name)
  493. (define os
  494. (marionette-operating-system %cleanup-os
  495. #:imported-modules '((gnu services herd)
  496. (guix combinators))))
  497. (define test
  498. (with-imported-modules '((gnu build marionette))
  499. #~(begin
  500. (use-modules (gnu build marionette)
  501. (srfi srfi-64)
  502. (ice-9 match))
  503. (define marionette
  504. (make-marionette (list #$(virtual-machine os))))
  505. (mkdir #$output)
  506. (chdir #$output)
  507. (test-begin "cleanup")
  508. (test-assert "dirty service worked"
  509. (marionette-eval '(file-exists? "/witness") marionette))
  510. (test-equal "/tmp cleaned up"
  511. '("." "..")
  512. (marionette-eval '(begin
  513. (use-modules (ice-9 ftw))
  514. (scandir "/tmp"))
  515. marionette))
  516. (test-end)
  517. (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
  518. (gexp->derivation "cleanup" test))
  519. (define %test-cleanup
  520. ;; See <https://bugs.gnu.org/26353>.
  521. (system-test
  522. (name "cleanup")
  523. (description "Make sure the 'cleanup' service can remove files with
  524. non-ASCII names from /tmp.")
  525. (value (run-cleanup-test name))))
  526. ;;;
  527. ;;; Mcron.
  528. ;;;
  529. (define %mcron-os
  530. ;; System with an mcron service, with one mcron job for "root" and one mcron
  531. ;; job for an unprivileged user.
  532. (let ((job1 #~(job '(next-second '(0 5 10 15 20 25 30 35 40 45 50 55))
  533. (lambda ()
  534. (unless (file-exists? "witness")
  535. (call-with-output-file "witness"
  536. (lambda (port)
  537. (display (list (getuid) (getgid)) port)))))))
  538. (job2 #~(job next-second-from
  539. (lambda ()
  540. (call-with-output-file "witness"
  541. (lambda (port)
  542. (display (list (getuid) (getgid)) port))))
  543. #:user "alice"))
  544. (job3 #~(job next-second-from ;to test $PATH
  545. "touch witness-touch")))
  546. (simple-operating-system
  547. (service mcron-service-type
  548. (mcron-configuration (jobs (list job1 job2 job3)))))))
  549. (define (run-mcron-test name)
  550. (define os
  551. (marionette-operating-system
  552. %mcron-os
  553. #:imported-modules '((gnu services herd)
  554. (guix combinators))))
  555. (define test
  556. (with-imported-modules '((gnu build marionette))
  557. #~(begin
  558. (use-modules (gnu build marionette)
  559. (srfi srfi-64)
  560. (ice-9 match))
  561. (define marionette
  562. (make-marionette (list #$(virtual-machine os))))
  563. (mkdir #$output)
  564. (chdir #$output)
  565. (test-begin "mcron")
  566. (test-assert "service running"
  567. (marionette-eval
  568. '(begin
  569. (use-modules (gnu services herd))
  570. (start-service 'mcron))
  571. marionette))
  572. ;; Make sure root's mcron job runs, has its cwd set to "/root", and
  573. ;; runs with the right UID/GID.
  574. (test-equal "root's job"
  575. '(0 0)
  576. (wait-for-file "/root/witness" marionette))
  577. ;; Likewise for Alice's job. We cannot know what its GID is since
  578. ;; it's chosen by 'groupadd', but it's strictly positive.
  579. (test-assert "alice's job"
  580. (match (wait-for-file "/home/alice/witness" marionette)
  581. ((1000 gid)
  582. (>= gid 100))))
  583. ;; Last, the job that uses a command; allows us to test whether
  584. ;; $PATH is sane.
  585. (test-equal "root's job with command"
  586. ""
  587. (wait-for-file "/root/witness-touch" marionette
  588. #:read '(@ (ice-9 rdelim) read-string)))
  589. ;; Make sure the 'schedule' action is accepted.
  590. (test-equal "schedule action"
  591. '(#t) ;one value, #t
  592. (marionette-eval '(with-shepherd-action 'mcron ('schedule) result
  593. result)
  594. marionette))
  595. (test-end)
  596. (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
  597. (gexp->derivation name test))
  598. (define %test-mcron
  599. (system-test
  600. (name "mcron")
  601. (description "Make sure the mcron service works as advertised.")
  602. (value (run-mcron-test name))))
  603. ;;;
  604. ;;; Avahi and NSS-mDNS.
  605. ;;;
  606. (define %avahi-os
  607. (operating-system
  608. (inherit %simple-os)
  609. (name-service-switch %mdns-host-lookup-nss)
  610. (services (cons* (service avahi-service-type
  611. (avahi-configuration (debug? #t)))
  612. (dbus-service)
  613. (service dhcp-client-service-type) ;needed for multicast
  614. ;; Enable heavyweight debugging output.
  615. (modify-services (operating-system-user-services
  616. %simple-os)
  617. (nscd-service-type config
  618. => (nscd-configuration
  619. (inherit config)
  620. (debug-level 3)
  621. (log-file "/dev/console")))
  622. (syslog-service-type config
  623. =>
  624. (syslog-configuration
  625. (inherit config)
  626. (config-file
  627. (plain-file
  628. "syslog.conf"
  629. "*.* /dev/console\n")))))))))
  630. (define (run-nss-mdns-test)
  631. ;; Test resolution of '.local' names via libc. Start the marionette service
  632. ;; *after* nscd. Failing to do that, libc will try to connect to nscd,
  633. ;; fail, then never try again (see '__nss_not_use_nscd_hosts' in libc),
  634. ;; leading to '.local' resolution failures.
  635. (define os
  636. (marionette-operating-system
  637. %avahi-os
  638. #:requirements '(nscd)
  639. #:imported-modules '((gnu services herd)
  640. (guix combinators))))
  641. (define mdns-host-name
  642. (string-append (operating-system-host-name os)
  643. ".local"))
  644. (define test
  645. (with-imported-modules '((gnu build marionette))
  646. #~(begin
  647. (use-modules (gnu build marionette)
  648. (srfi srfi-1)
  649. (srfi srfi-64)
  650. (ice-9 match))
  651. (define marionette
  652. (make-marionette (list #$(virtual-machine os))))
  653. (mkdir #$output)
  654. (chdir #$output)
  655. (test-begin "avahi")
  656. (test-assert "nscd PID file is created"
  657. (marionette-eval
  658. '(begin
  659. (use-modules (gnu services herd))
  660. (start-service 'nscd))
  661. marionette))
  662. (test-assert "nscd is listening on its socket"
  663. (marionette-eval
  664. ;; XXX: Work around a race condition in nscd: nscd creates its
  665. ;; PID file before it is listening on its socket.
  666. '(let ((sock (socket PF_UNIX SOCK_STREAM 0)))
  667. (let try ()
  668. (catch 'system-error
  669. (lambda ()
  670. (connect sock AF_UNIX "/var/run/nscd/socket")
  671. (close-port sock)
  672. (format #t "nscd is ready~%")
  673. #t)
  674. (lambda args
  675. (format #t "waiting for nscd...~%")
  676. (usleep 500000)
  677. (try)))))
  678. marionette))
  679. (test-assert "avahi is running"
  680. (marionette-eval
  681. '(begin
  682. (use-modules (gnu services herd))
  683. (start-service 'avahi-daemon))
  684. marionette))
  685. (test-assert "network is up"
  686. (marionette-eval
  687. '(begin
  688. (use-modules (gnu services herd))
  689. (start-service 'networking))
  690. marionette))
  691. (test-equal "avahi-resolve-host-name"
  692. 0
  693. (marionette-eval
  694. '(system*
  695. "/run/current-system/profile/bin/avahi-resolve-host-name"
  696. "-v" #$mdns-host-name)
  697. marionette))
  698. (test-equal "avahi-browse"
  699. 0
  700. (marionette-eval
  701. '(system* "avahi-browse" "-avt")
  702. marionette))
  703. (test-assert "getaddrinfo .local"
  704. ;; Wait for the 'avahi-daemon' service and perform a resolution.
  705. (match (marionette-eval
  706. '(getaddrinfo #$mdns-host-name)
  707. marionette)
  708. (((? vector? addrinfos) ..1)
  709. (pk 'getaddrinfo addrinfos)
  710. (and (any (lambda (ai)
  711. (= AF_INET (addrinfo:fam ai)))
  712. addrinfos)
  713. (any (lambda (ai)
  714. (= AF_INET6 (addrinfo:fam ai)))
  715. addrinfos)))))
  716. (test-assert "gethostbyname .local"
  717. (match (pk 'gethostbyname
  718. (marionette-eval '(gethostbyname #$mdns-host-name)
  719. marionette))
  720. ((? vector? result)
  721. (and (string=? (hostent:name result) #$mdns-host-name)
  722. (= (hostent:addrtype result) AF_INET)))))
  723. (test-end)
  724. (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
  725. (gexp->derivation "nss-mdns" test))
  726. (define %test-nss-mdns
  727. (system-test
  728. (name "nss-mdns")
  729. (description
  730. "Test Avahi's multicast-DNS implementation, and in particular, test its
  731. glibc name service switch (NSS) module.")
  732. (value (run-nss-mdns-test))))