base.scm 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  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 (guix build utils) (srfi srfi-1)
  203. (ice-9 ftw) (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 "ownership and permissions of /var/empty"
  233. '(0 0 #o555)
  234. (let ((st (marionette-eval `(stat "/var/empty") marionette)))
  235. (list (stat:uid st) (stat:gid st)
  236. (stat:perms st))))
  237. (test-equal "no extra home directories"
  238. '()
  239. ;; Make sure the home directories that are not supposed to be
  240. ;; created are indeed not created.
  241. (let ((nonexistent
  242. '#$(filter-map (lambda (user)
  243. (and (not
  244. (user-account-create-home-directory?
  245. user))
  246. (user-account-home-directory user)))
  247. (operating-system-user-accounts os))))
  248. (marionette-eval
  249. `(begin
  250. (use-modules (srfi srfi-1))
  251. ;; Note: Do not flag "/var/empty".
  252. (filter file-exists?
  253. ',(remove (cut string-prefix? "/var/" <>)
  254. nonexistent)))
  255. marionette)))
  256. (test-equal "login on tty1"
  257. "root\n"
  258. (begin
  259. (marionette-control "sendkey ctrl-alt-f1" marionette)
  260. ;; Wait for the 'term-tty1' service to be running (using
  261. ;; 'start-service' is the simplest and most reliable way to do
  262. ;; that.)
  263. (marionette-eval
  264. '(begin
  265. (use-modules (gnu services herd))
  266. (start-service 'term-tty1))
  267. marionette)
  268. ;; Now we can type.
  269. (marionette-type "root\n\nid -un > logged-in\n" marionette)
  270. ;; It can take a while before the shell commands are executed.
  271. (marionette-eval '(use-modules (rnrs io ports)) marionette)
  272. (wait-for-file "/root/logged-in" marionette
  273. #:read 'get-string-all)))
  274. (test-equal "getlogin on tty1"
  275. "\"root\""
  276. (begin
  277. ;; Assume we logged in in the previous test and type.
  278. (marionette-type "guile -c '(write (getlogin))' > /root/login-id.tmp\n"
  279. marionette)
  280. (marionette-type "mv /root/login-id{.tmp,}\n"
  281. marionette)
  282. ;; It can take a while before the shell commands are executed.
  283. (marionette-eval '(use-modules (rnrs io ports)) marionette)
  284. (wait-for-file "/root/login-id" marionette
  285. #:read 'get-string-all)))
  286. ;; There should be one utmpx entry for the user logged in on tty1.
  287. (test-equal "utmpx entry"
  288. '(("root" "tty1" #f))
  289. (marionette-eval
  290. '(begin
  291. (use-modules (guix build syscalls)
  292. (srfi srfi-1))
  293. (filter-map (lambda (entry)
  294. (and (equal? (login-type USER_PROCESS)
  295. (utmpx-login-type entry))
  296. (list (utmpx-user entry) (utmpx-line entry)
  297. (utmpx-host entry))))
  298. (utmpx-entries)))
  299. marionette))
  300. ;; Likewise for /var/log/wtmp (used by 'last').
  301. (test-assert "wtmp entry"
  302. (match (marionette-eval
  303. '(begin
  304. (use-modules (guix build syscalls)
  305. (srfi srfi-1))
  306. (define (entry->list entry)
  307. (list (utmpx-user entry) (utmpx-line entry)
  308. (utmpx-host entry) (utmpx-login-type entry)))
  309. (call-with-input-file "/var/log/wtmp"
  310. (lambda (port)
  311. (let loop ((result '()))
  312. (if (eof-object? (peek-char port))
  313. (map entry->list (reverse result))
  314. (loop (cons (read-utmpx port) result)))))))
  315. marionette)
  316. (((users lines hosts types) ..1)
  317. (every (lambda (type)
  318. (eqv? type (login-type LOGIN_PROCESS)))
  319. types))))
  320. (test-assert "host name resolution"
  321. (match (marionette-eval
  322. '(begin
  323. ;; Wait for nscd or our requests go through it.
  324. (use-modules (gnu services herd))
  325. (start-service 'nscd)
  326. (list (getaddrinfo "localhost")
  327. (getaddrinfo #$(operating-system-host-name os))))
  328. marionette)
  329. ((((? vector?) ..1) ((? vector?) ..1))
  330. #t)
  331. (x
  332. (pk 'failure x #f))))
  333. (test-equal "nscd invalidate action"
  334. '(#t) ;one value, #t
  335. (marionette-eval '(with-shepherd-action 'nscd ('invalidate "hosts")
  336. result
  337. result)
  338. marionette))
  339. ;; FIXME: The 'invalidate' action can't reliably obtain the exit
  340. ;; code of 'nscd' so skip this test.
  341. (test-skip 1)
  342. (test-equal "nscd invalidate action, wrong table"
  343. '(#f) ;one value, #f
  344. (marionette-eval '(with-shepherd-action 'nscd ('invalidate "xyz")
  345. result
  346. result)
  347. marionette))
  348. (test-equal "host not found"
  349. #f
  350. (marionette-eval
  351. '(false-if-exception (getaddrinfo "does-not-exist"))
  352. marionette))
  353. (test-equal "locale"
  354. "en_US.utf8"
  355. (marionette-eval '(let ((before (setlocale LC_ALL "en_US.utf8")))
  356. (setlocale LC_ALL before))
  357. marionette))
  358. (test-eq "/run/current-system is a GC root"
  359. 'success!
  360. (marionette-eval '(begin
  361. ;; Make sure the (guix …) modules are found.
  362. (eval-when (expand load eval)
  363. (set! %load-path
  364. (append (map (lambda (package)
  365. (string-append package
  366. "/share/guile/site/"
  367. (effective-version)))
  368. '#$guix&co)
  369. %load-path)))
  370. (use-modules (srfi srfi-34) (guix store))
  371. (let ((system (readlink "/run/current-system")))
  372. (guard (c ((store-protocol-error? c)
  373. (and (file-exists? system)
  374. 'success!)))
  375. (with-store store
  376. (delete-paths store (list system))
  377. #f))))
  378. marionette))
  379. ;; This symlink is currently unused, but better have it point to the
  380. ;; right place. See
  381. ;; <https://lists.gnu.org/archive/html/guix-devel/2016-08/msg01641.html>.
  382. (test-equal "/var/guix/gcroots/profiles is a valid symlink"
  383. "/var/guix/profiles"
  384. (marionette-eval '(readlink "/var/guix/gcroots/profiles")
  385. marionette))
  386. (test-assert "screendump"
  387. (begin
  388. (marionette-control (string-append "screendump " #$output
  389. "/tty1.ppm")
  390. marionette)
  391. (file-exists? "tty1.ppm")))
  392. (test-assert "screen text"
  393. (let ((text (marionette-screen-text marionette
  394. #:ocrad
  395. #$(file-append ocrad
  396. "/bin/ocrad"))))
  397. ;; Check whether the welcome message and shell prompt are
  398. ;; displayed. Note: OCR confuses "y" and "V" for instance, so
  399. ;; we cannot reliably match the whole text.
  400. (and (string-contains text "This is the GNU")
  401. (string-contains text
  402. (string-append
  403. "root@"
  404. #$(operating-system-host-name os))))))
  405. (test-end)
  406. (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
  407. (gexp->derivation name test))
  408. (define %test-basic-os
  409. (system-test
  410. (name "basic")
  411. (description
  412. "Instrument %SIMPLE-OS, run it in a VM, and run a series of basic
  413. functionality tests.")
  414. (value
  415. (let* ((os (marionette-operating-system
  416. %simple-os
  417. #:imported-modules '((gnu services herd)
  418. (guix combinators))))
  419. (vm (virtual-machine os)))
  420. ;; XXX: Add call to 'virtualized-operating-system' to get the exact same
  421. ;; set of services as the OS produced by
  422. ;; 'system-qemu-image/shared-store-script'.
  423. (run-basic-test (virtualized-operating-system os '())
  424. #~(list #$vm))))))
  425. ;;;
  426. ;;; Halt.
  427. ;;;
  428. (define (run-halt-test vm)
  429. ;; As reported in <http://bugs.gnu.org/26931>, running tmux would previously
  430. ;; lead the 'stop' method of 'user-processes' to an infinite loop, with the
  431. ;; tmux server process as a zombie that remains in the list of processes.
  432. ;; This test reproduces this scenario.
  433. (define test
  434. (with-imported-modules '((gnu build marionette))
  435. #~(begin
  436. (use-modules (gnu build marionette))
  437. (define marionette
  438. (make-marionette '(#$vm)))
  439. (define ocrad
  440. #$(file-append ocrad "/bin/ocrad"))
  441. ;; Wait for tty1 and log in.
  442. (marionette-eval '(begin
  443. (use-modules (gnu services herd))
  444. (start-service 'term-tty1))
  445. marionette)
  446. (marionette-type "root\n" marionette)
  447. (wait-for-screen-text marionette
  448. (lambda (text)
  449. (string-contains text "root@komputilo"))
  450. #:ocrad ocrad)
  451. ;; Start tmux and wait for it to be ready.
  452. (marionette-type "tmux new-session 'echo 1 > /ready; bash'\n"
  453. marionette)
  454. (wait-for-file "/ready" marionette)
  455. ;; Make sure to stop the test after a while.
  456. (sigaction SIGALRM (lambda _
  457. (format (current-error-port)
  458. "FAIL: Time is up, but VM still running.\n")
  459. (primitive-exit 1)))
  460. (alarm 10)
  461. ;; Get debugging info.
  462. (marionette-eval '(current-output-port
  463. (open-file "/dev/console" "w0"))
  464. marionette)
  465. (marionette-eval '(system* #$(file-append procps "/bin/ps")
  466. "-eo" "pid,ppid,stat,comm")
  467. marionette)
  468. ;; See if 'halt' actually works.
  469. (marionette-eval '(system* "/run/current-system/profile/sbin/halt")
  470. marionette)
  471. ;; If we reach this line, that means the VM was properly stopped in
  472. ;; a timely fashion.
  473. (alarm 0)
  474. (call-with-output-file #$output
  475. (lambda (port)
  476. (display "success!" port))))))
  477. (gexp->derivation "halt" test))
  478. (define %test-halt
  479. (system-test
  480. (name "halt")
  481. (description
  482. "Use the 'halt' command and make sure it succeeds and does not get stuck
  483. in a loop. See <http://bugs.gnu.org/26931>.")
  484. (value
  485. (let ((os (marionette-operating-system
  486. (operating-system
  487. (inherit %simple-os)
  488. (packages (cons tmux %base-packages)))
  489. #:imported-modules '((gnu services herd)
  490. (guix combinators)))))
  491. (run-halt-test (virtual-machine os))))))
  492. ;;;
  493. ;;; Cleanup of /tmp, /var/run, etc.
  494. ;;;
  495. (define %cleanup-os
  496. (simple-operating-system
  497. (simple-service 'dirty-things
  498. boot-service-type
  499. (let ((script (plain-file
  500. "create-utf8-file.sh"
  501. (string-append
  502. "echo $0: dirtying /tmp...\n"
  503. "set -e; set -x\n"
  504. "touch /witness\n"
  505. "exec touch /tmp/λαμβδα"))))
  506. (with-imported-modules '((guix build utils))
  507. #~(begin
  508. (setenv "PATH"
  509. #$(file-append coreutils "/bin"))
  510. (invoke #$(file-append bash "/bin/sh")
  511. #$script)))))))
  512. (define (run-cleanup-test name)
  513. (define os
  514. (marionette-operating-system %cleanup-os
  515. #:imported-modules '((gnu services herd)
  516. (guix combinators))))
  517. (define test
  518. (with-imported-modules '((gnu build marionette))
  519. #~(begin
  520. (use-modules (gnu build marionette)
  521. (srfi srfi-64)
  522. (ice-9 match))
  523. (define marionette
  524. (make-marionette (list #$(virtual-machine os))))
  525. (mkdir #$output)
  526. (chdir #$output)
  527. (test-begin "cleanup")
  528. (test-assert "dirty service worked"
  529. (marionette-eval '(file-exists? "/witness") marionette))
  530. (test-equal "/tmp cleaned up"
  531. '("." "..")
  532. (marionette-eval '(begin
  533. (use-modules (ice-9 ftw))
  534. (scandir "/tmp"))
  535. marionette))
  536. (test-end)
  537. (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
  538. (gexp->derivation "cleanup" test))
  539. (define %test-cleanup
  540. ;; See <https://bugs.gnu.org/26353>.
  541. (system-test
  542. (name "cleanup")
  543. (description "Make sure the 'cleanup' service can remove files with
  544. non-ASCII names from /tmp.")
  545. (value (run-cleanup-test name))))
  546. ;;;
  547. ;;; Mcron.
  548. ;;;
  549. (define %mcron-os
  550. ;; System with an mcron service, with one mcron job for "root" and one mcron
  551. ;; job for an unprivileged user.
  552. (let ((job1 #~(job '(next-second '(0 5 10 15 20 25 30 35 40 45 50 55))
  553. (lambda ()
  554. (unless (file-exists? "witness")
  555. (call-with-output-file "witness"
  556. (lambda (port)
  557. (display (list (getuid) (getgid)) port)))))))
  558. (job2 #~(job next-second-from
  559. (lambda ()
  560. (call-with-output-file "witness"
  561. (lambda (port)
  562. (display (list (getuid) (getgid)) port))))
  563. #:user "alice"))
  564. (job3 #~(job next-second-from ;to test $PATH
  565. "touch witness-touch")))
  566. (simple-operating-system
  567. (service mcron-service-type
  568. (mcron-configuration (jobs (list job1 job2 job3)))))))
  569. (define (run-mcron-test name)
  570. (define os
  571. (marionette-operating-system
  572. %mcron-os
  573. #:imported-modules '((gnu services herd)
  574. (guix combinators))))
  575. (define test
  576. (with-imported-modules '((gnu build marionette))
  577. #~(begin
  578. (use-modules (gnu build marionette)
  579. (srfi srfi-64)
  580. (ice-9 match))
  581. (define marionette
  582. (make-marionette (list #$(virtual-machine os))))
  583. (mkdir #$output)
  584. (chdir #$output)
  585. (test-begin "mcron")
  586. (test-assert "service running"
  587. (marionette-eval
  588. '(begin
  589. (use-modules (gnu services herd))
  590. (start-service 'mcron))
  591. marionette))
  592. ;; Make sure root's mcron job runs, has its cwd set to "/root", and
  593. ;; runs with the right UID/GID.
  594. (test-equal "root's job"
  595. '(0 0)
  596. (wait-for-file "/root/witness" marionette))
  597. ;; Likewise for Alice's job. We cannot know what its GID is since
  598. ;; it's chosen by 'groupadd', but it's strictly positive.
  599. (test-assert "alice's job"
  600. (match (wait-for-file "/home/alice/witness" marionette)
  601. ((1000 gid)
  602. (>= gid 100))))
  603. ;; Last, the job that uses a command; allows us to test whether
  604. ;; $PATH is sane.
  605. (test-equal "root's job with command"
  606. ""
  607. (wait-for-file "/root/witness-touch" marionette
  608. #:read '(@ (ice-9 rdelim) read-string)))
  609. ;; Make sure the 'schedule' action is accepted.
  610. (test-equal "schedule action"
  611. '(#t) ;one value, #t
  612. (marionette-eval '(with-shepherd-action 'mcron ('schedule) result
  613. result)
  614. marionette))
  615. (test-end)
  616. (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
  617. (gexp->derivation name test))
  618. (define %test-mcron
  619. (system-test
  620. (name "mcron")
  621. (description "Make sure the mcron service works as advertised.")
  622. (value (run-mcron-test name))))
  623. ;;;
  624. ;;; Avahi and NSS-mDNS.
  625. ;;;
  626. (define %avahi-os
  627. (operating-system
  628. (inherit %simple-os)
  629. (name-service-switch %mdns-host-lookup-nss)
  630. (services (cons* (service avahi-service-type
  631. (avahi-configuration (debug? #t)))
  632. (dbus-service)
  633. (service dhcp-client-service-type) ;needed for multicast
  634. ;; Enable heavyweight debugging output.
  635. (modify-services (operating-system-user-services
  636. %simple-os)
  637. (nscd-service-type config
  638. => (nscd-configuration
  639. (inherit config)
  640. (debug-level 3)
  641. (log-file "/dev/console")))
  642. (syslog-service-type config
  643. =>
  644. (syslog-configuration
  645. (inherit config)
  646. (config-file
  647. (plain-file
  648. "syslog.conf"
  649. "*.* /dev/console\n")))))))))
  650. (define (run-nss-mdns-test)
  651. ;; Test resolution of '.local' names via libc. Start the marionette service
  652. ;; *after* nscd. Failing to do that, libc will try to connect to nscd,
  653. ;; fail, then never try again (see '__nss_not_use_nscd_hosts' in libc),
  654. ;; leading to '.local' resolution failures.
  655. (define os
  656. (marionette-operating-system
  657. %avahi-os
  658. #:requirements '(nscd)
  659. #:imported-modules '((gnu services herd)
  660. (guix combinators))))
  661. (define mdns-host-name
  662. (string-append (operating-system-host-name os)
  663. ".local"))
  664. (define test
  665. (with-imported-modules '((gnu build marionette))
  666. #~(begin
  667. (use-modules (gnu build marionette)
  668. (srfi srfi-1)
  669. (srfi srfi-64)
  670. (ice-9 match))
  671. (define marionette
  672. (make-marionette (list #$(virtual-machine os))))
  673. (mkdir #$output)
  674. (chdir #$output)
  675. (test-begin "avahi")
  676. (test-assert "nscd PID file is created"
  677. (marionette-eval
  678. '(begin
  679. (use-modules (gnu services herd))
  680. (start-service 'nscd))
  681. marionette))
  682. (test-assert "nscd is listening on its socket"
  683. (marionette-eval
  684. ;; XXX: Work around a race condition in nscd: nscd creates its
  685. ;; PID file before it is listening on its socket.
  686. '(let ((sock (socket PF_UNIX SOCK_STREAM 0)))
  687. (let try ()
  688. (catch 'system-error
  689. (lambda ()
  690. (connect sock AF_UNIX "/var/run/nscd/socket")
  691. (close-port sock)
  692. (format #t "nscd is ready~%")
  693. #t)
  694. (lambda args
  695. (format #t "waiting for nscd...~%")
  696. (usleep 500000)
  697. (try)))))
  698. marionette))
  699. (test-assert "avahi is running"
  700. (marionette-eval
  701. '(begin
  702. (use-modules (gnu services herd))
  703. (start-service 'avahi-daemon))
  704. marionette))
  705. (test-assert "network is up"
  706. (marionette-eval
  707. '(begin
  708. (use-modules (gnu services herd))
  709. (start-service 'networking))
  710. marionette))
  711. (test-equal "avahi-resolve-host-name"
  712. 0
  713. (marionette-eval
  714. '(system*
  715. "/run/current-system/profile/bin/avahi-resolve-host-name"
  716. "-v" #$mdns-host-name)
  717. marionette))
  718. (test-equal "avahi-browse"
  719. 0
  720. (marionette-eval
  721. '(system* "avahi-browse" "-avt")
  722. marionette))
  723. (test-assert "getaddrinfo .local"
  724. ;; Wait for the 'avahi-daemon' service and perform a resolution.
  725. (match (marionette-eval
  726. '(getaddrinfo #$mdns-host-name)
  727. marionette)
  728. (((? vector? addrinfos) ..1)
  729. (pk 'getaddrinfo addrinfos)
  730. (and (any (lambda (ai)
  731. (= AF_INET (addrinfo:fam ai)))
  732. addrinfos)
  733. (any (lambda (ai)
  734. (= AF_INET6 (addrinfo:fam ai)))
  735. addrinfos)))))
  736. (test-assert "gethostbyname .local"
  737. (match (pk 'gethostbyname
  738. (marionette-eval '(gethostbyname #$mdns-host-name)
  739. marionette))
  740. ((? vector? result)
  741. (and (string=? (hostent:name result) #$mdns-host-name)
  742. (= (hostent:addrtype result) AF_INET)))))
  743. (test-end)
  744. (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
  745. (gexp->derivation "nss-mdns" test))
  746. (define %test-nss-mdns
  747. (system-test
  748. (name "nss-mdns")
  749. (description
  750. "Test Avahi's multicast-DNS implementation, and in particular, test its
  751. glibc name service switch (NSS) module.")
  752. (value (run-nss-mdns-test))))