base.scm 36 KB

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