base.scm 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921
  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. (wait-for-screen-text marionette
  491. (lambda (text)
  492. (string-contains text "root@komputilo"))
  493. #:ocrad ocrad)
  494. ;; Start tmux and wait for it to be ready.
  495. (marionette-type "tmux new-session 'echo 1 > /ready; bash'\n"
  496. marionette)
  497. (wait-for-file "/ready" marionette)
  498. ;; Make sure to stop the test after a while.
  499. (sigaction SIGALRM (lambda _
  500. (format (current-error-port)
  501. "FAIL: Time is up, but VM still running.\n")
  502. (primitive-exit 1)))
  503. (alarm 10)
  504. ;; Get debugging info.
  505. (marionette-eval '(current-output-port
  506. (open-file "/dev/console" "w0"))
  507. marionette)
  508. (marionette-eval '(system* #$(file-append procps "/bin/ps")
  509. "-eo" "pid,ppid,stat,comm")
  510. marionette)
  511. ;; See if 'halt' actually works.
  512. (marionette-eval '(system* "/run/current-system/profile/sbin/halt")
  513. marionette)
  514. ;; If we reach this line, that means the VM was properly stopped in
  515. ;; a timely fashion.
  516. (alarm 0)
  517. (call-with-output-file #$output
  518. (lambda (port)
  519. (display "success!" port))))))
  520. (gexp->derivation "halt" test))
  521. (define %test-halt
  522. (system-test
  523. (name "halt")
  524. (description
  525. "Use the 'halt' command and make sure it succeeds and does not get stuck
  526. in a loop. See <http://bugs.gnu.org/26931>.")
  527. (value
  528. (let ((os (marionette-operating-system
  529. (operating-system
  530. (inherit %simple-os)
  531. (packages (cons tmux %base-packages)))
  532. #:imported-modules '((gnu services herd)
  533. (guix combinators)))))
  534. (run-halt-test (virtual-machine os))))))
  535. ;;;
  536. ;;; Cleanup of /tmp, /var/run, etc.
  537. ;;;
  538. (define %cleanup-os
  539. (simple-operating-system
  540. (simple-service 'dirty-things
  541. boot-service-type
  542. (let ((script (plain-file
  543. "create-utf8-file.sh"
  544. (string-append
  545. "echo $0: dirtying /tmp...\n"
  546. "set -e; set -x\n"
  547. "touch /witness\n"
  548. "exec touch /tmp/λαμβδα"))))
  549. (with-imported-modules '((guix build utils))
  550. #~(begin
  551. (setenv "PATH"
  552. #$(file-append coreutils "/bin"))
  553. (invoke #$(file-append bash "/bin/sh")
  554. #$script)))))))
  555. (define (run-cleanup-test name)
  556. (define os
  557. (marionette-operating-system %cleanup-os
  558. #:imported-modules '((gnu services herd)
  559. (guix combinators))))
  560. (define test
  561. (with-imported-modules '((gnu build marionette))
  562. #~(begin
  563. (use-modules (gnu build marionette)
  564. (srfi srfi-64)
  565. (ice-9 match))
  566. (define marionette
  567. (make-marionette (list #$(virtual-machine os))))
  568. (mkdir #$output)
  569. (chdir #$output)
  570. (test-begin "cleanup")
  571. (test-assert "dirty service worked"
  572. (marionette-eval '(file-exists? "/witness") marionette))
  573. (test-equal "/tmp cleaned up"
  574. '("." "..")
  575. (marionette-eval '(begin
  576. (use-modules (ice-9 ftw))
  577. (scandir "/tmp"))
  578. marionette))
  579. (test-end)
  580. (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
  581. (gexp->derivation "cleanup" test))
  582. (define %test-cleanup
  583. ;; See <https://bugs.gnu.org/26353>.
  584. (system-test
  585. (name "cleanup")
  586. (description "Make sure the 'cleanup' service can remove files with
  587. non-ASCII names from /tmp.")
  588. (value (run-cleanup-test name))))
  589. ;;;
  590. ;;; Mcron.
  591. ;;;
  592. (define %mcron-os
  593. ;; System with an mcron service, with one mcron job for "root" and one mcron
  594. ;; job for an unprivileged user.
  595. (let ((job1 #~(job '(next-second '(0 5 10 15 20 25 30 35 40 45 50 55))
  596. (lambda ()
  597. (unless (file-exists? "witness")
  598. (call-with-output-file "witness"
  599. (lambda (port)
  600. (display (list (getuid) (getgid)) port)))))))
  601. (job2 #~(job next-second-from
  602. (lambda ()
  603. (call-with-output-file "witness"
  604. (lambda (port)
  605. (display (list (getuid) (getgid)) port))))
  606. #:user "alice"))
  607. (job3 #~(job next-second-from ;to test $PATH
  608. "touch witness-touch")))
  609. (simple-operating-system
  610. (service mcron-service-type
  611. (mcron-configuration (jobs (list job1 job2 job3)))))))
  612. (define (run-mcron-test name)
  613. (define os
  614. (marionette-operating-system
  615. %mcron-os
  616. #:imported-modules '((gnu services herd)
  617. (guix combinators))))
  618. (define test
  619. (with-imported-modules '((gnu build marionette))
  620. #~(begin
  621. (use-modules (gnu build marionette)
  622. (srfi srfi-64)
  623. (ice-9 match))
  624. (define marionette
  625. (make-marionette (list #$(virtual-machine os))))
  626. (mkdir #$output)
  627. (chdir #$output)
  628. (test-begin "mcron")
  629. (test-assert "service running"
  630. (marionette-eval
  631. '(begin
  632. (use-modules (gnu services herd))
  633. (start-service 'mcron))
  634. marionette))
  635. ;; Make sure root's mcron job runs, has its cwd set to "/root", and
  636. ;; runs with the right UID/GID.
  637. (test-equal "root's job"
  638. '(0 0)
  639. (wait-for-file "/root/witness" marionette))
  640. ;; Likewise for Alice's job. We cannot know what its GID is since
  641. ;; it's chosen by 'groupadd', but it's strictly positive.
  642. (test-assert "alice's job"
  643. (match (wait-for-file "/home/alice/witness" marionette)
  644. ((1000 gid)
  645. (>= gid 100))))
  646. ;; Last, the job that uses a command; allows us to test whether
  647. ;; $PATH is sane.
  648. (test-equal "root's job with command"
  649. ""
  650. (wait-for-file "/root/witness-touch" marionette
  651. #:read '(@ (ice-9 rdelim) read-string)))
  652. ;; Make sure the 'schedule' action is accepted.
  653. (test-equal "schedule action"
  654. '(#t) ;one value, #t
  655. (marionette-eval '(with-shepherd-action 'mcron ('schedule) result
  656. result)
  657. marionette))
  658. (test-end)
  659. (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
  660. (gexp->derivation name test))
  661. (define %test-mcron
  662. (system-test
  663. (name "mcron")
  664. (description "Make sure the mcron service works as advertised.")
  665. (value (run-mcron-test name))))
  666. ;;;
  667. ;;; Avahi and NSS-mDNS.
  668. ;;;
  669. (define %avahi-os
  670. (operating-system
  671. (inherit %simple-os)
  672. (name-service-switch %mdns-host-lookup-nss)
  673. (services (cons* (service avahi-service-type
  674. (avahi-configuration (debug? #t)))
  675. (dbus-service)
  676. (service dhcp-client-service-type) ;needed for multicast
  677. ;; Enable heavyweight debugging output.
  678. (modify-services (operating-system-user-services
  679. %simple-os)
  680. (nscd-service-type config
  681. => (nscd-configuration
  682. (inherit config)
  683. (debug-level 3)
  684. (log-file "/dev/console")))
  685. (syslog-service-type config
  686. =>
  687. (syslog-configuration
  688. (inherit config)
  689. (config-file
  690. (plain-file
  691. "syslog.conf"
  692. "*.* /dev/console\n")))))))))
  693. (define (run-nss-mdns-test)
  694. ;; Test resolution of '.local' names via libc. Start the marionette service
  695. ;; *after* nscd. Failing to do that, libc will try to connect to nscd,
  696. ;; fail, then never try again (see '__nss_not_use_nscd_hosts' in libc),
  697. ;; leading to '.local' resolution failures.
  698. (define os
  699. (marionette-operating-system
  700. %avahi-os
  701. #:requirements '(nscd)
  702. #:imported-modules '((gnu services herd)
  703. (guix combinators))))
  704. (define mdns-host-name
  705. (string-append (operating-system-host-name os)
  706. ".local"))
  707. (define test
  708. (with-imported-modules '((gnu build marionette))
  709. #~(begin
  710. (use-modules (gnu build marionette)
  711. (srfi srfi-1)
  712. (srfi srfi-64)
  713. (ice-9 match))
  714. (define marionette
  715. (make-marionette (list #$(virtual-machine os))))
  716. (mkdir #$output)
  717. (chdir #$output)
  718. (test-begin "avahi")
  719. (test-assert "nscd PID file is created"
  720. (marionette-eval
  721. '(begin
  722. (use-modules (gnu services herd))
  723. (start-service 'nscd))
  724. marionette))
  725. (test-assert "nscd is listening on its socket"
  726. (marionette-eval
  727. ;; XXX: Work around a race condition in nscd: nscd creates its
  728. ;; PID file before it is listening on its socket.
  729. '(let ((sock (socket PF_UNIX SOCK_STREAM 0)))
  730. (let try ()
  731. (catch 'system-error
  732. (lambda ()
  733. (connect sock AF_UNIX "/var/run/nscd/socket")
  734. (close-port sock)
  735. (format #t "nscd is ready~%")
  736. #t)
  737. (lambda args
  738. (format #t "waiting for nscd...~%")
  739. (usleep 500000)
  740. (try)))))
  741. marionette))
  742. (test-assert "avahi is running"
  743. (marionette-eval
  744. '(begin
  745. (use-modules (gnu services herd))
  746. (start-service 'avahi-daemon))
  747. marionette))
  748. (test-assert "network is up"
  749. (marionette-eval
  750. '(begin
  751. (use-modules (gnu services herd))
  752. (start-service 'networking))
  753. marionette))
  754. (test-equal "avahi-resolve-host-name"
  755. 0
  756. (marionette-eval
  757. '(system*
  758. "/run/current-system/profile/bin/avahi-resolve-host-name"
  759. "-v" #$mdns-host-name)
  760. marionette))
  761. (test-equal "avahi-browse"
  762. 0
  763. (marionette-eval
  764. '(system* "avahi-browse" "-avt")
  765. marionette))
  766. (test-assert "getaddrinfo .local"
  767. ;; Wait for the 'avahi-daemon' service and perform a resolution.
  768. (match (marionette-eval
  769. '(getaddrinfo #$mdns-host-name)
  770. marionette)
  771. (((? vector? addrinfos) ..1)
  772. (pk 'getaddrinfo addrinfos)
  773. (and (any (lambda (ai)
  774. (= AF_INET (addrinfo:fam ai)))
  775. addrinfos)
  776. (any (lambda (ai)
  777. (= AF_INET6 (addrinfo:fam ai)))
  778. addrinfos)))))
  779. (test-assert "gethostbyname .local"
  780. (match (pk 'gethostbyname
  781. (marionette-eval '(gethostbyname #$mdns-host-name)
  782. marionette))
  783. ((? vector? result)
  784. (and (string=? (hostent:name result) #$mdns-host-name)
  785. (= (hostent:addrtype result) AF_INET)))))
  786. (test-end)
  787. (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
  788. (gexp->derivation "nss-mdns" test))
  789. (define %test-nss-mdns
  790. (system-test
  791. (name "nss-mdns")
  792. (description
  793. "Test Avahi's multicast-DNS implementation, and in particular, test its
  794. glibc name service switch (NSS) module.")
  795. (value (run-nss-mdns-test))))