tests: Strengthen regexp in 'packages.scm'.
[jackhill/guix/guix.git] / gnu / tests / base.scm
CommitLineData
e9f693d0
LC
1;;; GNU Guix --- Functional package management for GNU
2;;; Copyright © 2016 Ludovic Courtès <ludo@gnu.org>
3;;;
4;;; This file is part of GNU Guix.
5;;;
6;;; GNU Guix is free software; you can redistribute it and/or modify it
7;;; under the terms of the GNU General Public License as published by
8;;; the Free Software Foundation; either version 3 of the License, or (at
9;;; your option) any later version.
10;;;
11;;; GNU Guix is distributed in the hope that it will be useful, but
12;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14;;; GNU General Public License for more details.
15;;;
16;;; You should have received a copy of the GNU General Public License
17;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
18
19(define-module (gnu tests base)
20 #:use-module (gnu tests)
21 #:use-module (gnu system)
22 #:use-module (gnu system grub)
23 #:use-module (gnu system file-systems)
24 #:use-module (gnu system shadow)
25 #:use-module (gnu system vm)
26 #:use-module (gnu services)
27 #:use-module (gnu services shepherd)
28 #:use-module (guix gexp)
29 #:use-module (guix store)
30 #:use-module (guix monads)
31 #:use-module (guix packages)
32 #:use-module (srfi srfi-1)
33 #:export (%test-basic-os))
34
35(define %simple-os
36 (operating-system
37 (host-name "komputilo")
38 (timezone "Europe/Berlin")
39 (locale "en_US.UTF-8")
40
41 (bootloader (grub-configuration (device "/dev/sdX")))
42 (file-systems (cons (file-system
43 (device "my-root")
44 (title 'label)
45 (mount-point "/")
46 (type "ext4"))
47 %base-file-systems))
48 (firmware '())
49
50 (users (cons (user-account
51 (name "alice")
52 (comment "Bob's sister")
53 (group "users")
54 (supplementary-groups '("wheel" "audio" "video"))
55 (home-directory "/home/alice"))
56 %base-user-accounts))))
57
58\f
59(define %test-basic-os
60 ;; Monadic derivation that instruments %SIMPLE-OS, runs it in a VM, and runs
61 ;; a series of basic functionality tests.
62 (mlet* %store-monad ((os -> (marionette-operating-system
63 %simple-os
64 #:imported-modules '((gnu services herd)
65 (guix combinators))))
66 (run (system-qemu-image/shared-store-script
67 os #:graphic? #f)))
68 (define test
69 #~(begin
70 (use-modules (gnu build marionette)
71 (srfi srfi-1)
72 (srfi srfi-26)
73 (srfi srfi-64)
74 (ice-9 match))
75
76 (define marionette
77 (make-marionette (list #$run)))
78
79 (mkdir #$output)
80 (chdir #$output)
81
82 (test-begin "basic")
83
84 (test-assert "uname"
85 (match (marionette-eval '(uname) marionette)
86 (#("Linux" "komputilo" version _ "x86_64")
87 (string-prefix? #$(package-version
88 (operating-system-kernel os))
89 version))))
90
91 (test-assert "shell and user commands"
92 ;; Is everything in $PATH?
93 (zero? (marionette-eval '(system "
94. /etc/profile
95set -e -x
96guix --version
97ls --version
98grep --version
99info --version")
100 marionette)))
101
102 (test-assert "accounts"
103 (let ((users (marionette-eval '(begin
104 (use-modules (ice-9 match))
105 (let loop ((result '()))
106 (match (getpw)
107 (#f (reverse result))
108 (x (loop (cons x result))))))
109 marionette)))
110 (lset= string=?
111 (map passwd:name users)
112 (list
113 #$@(map user-account-name
114 (operating-system-user-accounts os))))))
115
116 (test-assert "shepherd services"
117 (let ((services (marionette-eval '(begin
118 (use-modules (gnu services herd))
119 (call-with-values current-services
120 append))
121 marionette)))
122 (lset= eq?
123 (pk 'services services)
124 '(root #$@(operating-system-shepherd-service-names
125 (virtualized-operating-system os '()))))))
126
127 (test-equal "login on tty1"
128 "root\n"
129 (begin
130 (marionette-control "sendkey ctrl-alt-f1" marionette)
5a555642
LC
131 ;; Wait for the 'term-tty1' service to be running (using
132 ;; 'start-service' is the simplest and most reliable way to do
133 ;; that.)
e9f693d0
LC
134 (marionette-eval
135 '(begin
136 (use-modules (gnu services herd))
5a555642 137 (start-service 'term-tty1))
e9f693d0
LC
138 marionette)
139
140 ;; Now we can type.
141 (marionette-type "root\n\nid -un > logged-in\n" marionette)
142
143 ;; It can take a while before the shell commands are executed.
144 (let loop ((i 0))
145 (unless (or (file-exists? "/root/logged-in") (> i 15))
146 (sleep 1)
147 (loop (+ i 1))))
148 (marionette-eval '(use-modules (rnrs io ports)) marionette)
149 (marionette-eval '(call-with-input-file "/root/logged-in"
150 get-string-all)
151 marionette)))
152
153 (test-assert "screendump"
154 (begin
155 (marionette-control (string-append "screendump " #$output
156 "/tty1.ppm")
157 marionette)
158 (file-exists? "tty1.ppm")))
159
160 (test-end)
161 (exit (= (test-runner-fail-count (test-runner-current)) 0))))
162
163 (gexp->derivation "basic" test
164 #:modules '((gnu build marionette)))))