base.scm 36 KB

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