1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2016, 2017 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2017 Mathieu Othacehe <m.othacehe@gmail.com>
5 ;;; This file is part of GNU Guix.
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.
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.
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/>.
20 (define-module (gnu tests)
21 #:use-module (guix gexp)
22 #:use-module (guix utils)
23 #:use-module (guix records)
24 #:use-module (gnu bootloader grub)
25 #:use-module (gnu system)
26 #:use-module (gnu system file-systems)
27 #:use-module (gnu system shadow)
28 #:use-module (gnu services)
29 #:use-module (gnu services base)
30 #:use-module (gnu services shepherd)
31 #:use-module (guix discovery)
32 #:use-module (srfi srfi-1)
33 #:use-module (srfi srfi-9 gnu)
34 #:use-module (ice-9 match)
35 #:export (marionette-configuration
36 marionette-configuration?
37 marionette-configuration-device
38 marionette-configuration-imported-modules
39 marionette-configuration-requirements
41 marionette-service-type
42 marionette-operating-system
45 simple-operating-system
51 system-test-description
59 ;;; This module provides the infrastructure to run operating system tests.
60 ;;; The most important part of that is tools to instrument the OS under test,
61 ;;; essentially allowing to run in a virtual machine controlled by the host
62 ;;; system--hence the name "marionette".
66 (define-record-type* <marionette-configuration>
67 marionette-configuration make-marionette-configuration
68 marionette-configuration?
69 (device marionette-configuration-device ;string
70 (default "/dev/hvc0"))
71 (imported-modules marionette-configuration-imported-modules
73 (requirements marionette-configuration-requirements ;list of symbols
76 (define (marionette-shepherd-service config)
77 "Return the Shepherd service for the marionette REPL"
79 (($ <marionette-configuration> device imported-modules requirement)
80 (list (shepherd-service
81 (provision '(marionette))
83 ;; Always depend on UDEV so that DEVICE is available.
84 (requirement `(udev ,@requirement))
86 (modules '((ice-9 match)
91 (with-imported-modules `((guix build syscalls)
94 (define (clear-echo termios)
95 (set-field termios (termios-local-flags)
96 (logand (lognot (local-flags ECHO))
97 (termios-local-flags termios))))
99 (define (self-quoting? x)
100 (letrec-syntax ((one-of (syntax-rules ()
104 (one-of rest ...))))))
105 (one-of symbol? string? pair? null? vector?
106 bytevector? number? boolean?)))
108 (match (primitive-fork)
113 (let* ((repl (open-file #$device "r+0"))
114 (termios (tcgetattr (fileno repl)))
115 (console (open-file "/dev/console" "r+0")))
116 ;; Don't echo input back.
117 (tcsetattr (fileno repl) (tcsetattr-action TCSANOW)
118 (clear-echo termios))
120 ;; Redirect output to the console.
123 (dup2 (fileno console) 1)
124 (dup2 (fileno console) 2)
127 (display 'ready repl)
137 (let ((result (primitive-eval expr)))
138 (write (if (self-quoting? result)
140 (object->string result))
143 (print-exception (current-error-port)
144 (stack-ref (make-stack #t) 1)
149 (primitive-exit 1))))
152 (stop #~(make-kill-destructor)))))))
154 (define marionette-service-type
155 ;; This is the type of the "marionette" service, allowing a guest system to
156 ;; be manipulated from the host. This marionette REPL is essentially a
157 ;; universal backdoor.
158 (service-type (name 'marionette-repl)
160 (list (service-extension shepherd-root-service-type
161 marionette-shepherd-service)))))
163 (define* (marionette-operating-system os
165 (imported-modules '())
167 "Return a marionetteed variant of OS such that OS can be used as a
168 marionette in a virtual machine--i.e., controlled from the host system. The
169 marionette service in the guest is started after the Shepherd services listed
173 (services (cons (service marionette-service-type
174 (marionette-configuration
175 (requirements requirements)
176 (imported-modules imported-modules)))
177 (operating-system-user-services os)))))
179 (define-syntax define-os-with-source
180 (syntax-rules (use-modules operating-system)
181 "Define two variables: OS containing the given operating system, and
182 SOURCE containing the source to define OS as an sexp.
184 This is convenient when we need both the <operating-system> object so we can
185 instantiate it, and the source to create it so we can store in in a file in
186 the system under test."
188 (use-modules modules ...)
189 (operating-system fields ...))
192 (operating-system fields ...))
195 (use-modules modules ...)
196 (operating-system fields ...)))))))
200 ;;; Simple operating systems.
205 (host-name "komputilo")
206 (timezone "Europe/Berlin")
207 (locale "en_US.UTF-8")
209 (bootloader (grub-configuration (target "/dev/sdX")))
210 (file-systems (cons (file-system
218 (users (cons (user-account
220 (comment "Bob's sister")
222 (supplementary-groups '("wheel" "audio" "video"))
223 (home-directory "/home/alice"))
224 %base-user-accounts))))
226 (define-syntax-rule (simple-operating-system user-services ...)
227 "Return an operating system that includes USER-SERVICES in addition to
229 (operating-system (inherit %simple-os)
230 (services (cons* user-services ... %base-services))))
238 (define-record-type* <system-test> system-test make-system-test
240 (name system-test-name) ;string
241 (value system-test-value) ;%STORE-MONAD value
242 (description system-test-description) ;string
243 (location system-test-location (innate) ;<location>
244 (default (and=> (current-source-location)
245 source-properties->location))))
247 (define (write-system-test test port)
249 (($ <system-test> name _ _ ($ <location> file line))
250 (format port "#<system-test ~a ~a:~a ~a>"
252 (number->string (object-address test) 16)))
253 (($ <system-test> name)
254 (format port "#<system-test ~a ~a>" name
255 (number->string (object-address test) 16)))))
257 (set-record-type-printer! <system-test> write-system-test)
259 (define (test-modules)
260 "Return the list of modules that define system tests."
261 (scheme-modules (dirname (search-path %load-path "guix.scm"))
264 (define (fold-system-tests proc seed)
265 "Invoke PROC on each system test, passing it the test and the previous
267 (fold-module-public-variables (lambda (obj result)
268 (if (system-test? obj)
274 (define (all-system-tests)
275 "Return the list of system tests."
276 (reverse (fold-system-tests cons '())))
278 ;;; tests.scm ends here