1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2016 Ludovic Courtès <ludo@gnu.org>
4 ;;; This file is part of GNU Guix.
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.
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.
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/>.
19 (define-module (gnu tests)
20 #:use-module (guix gexp)
21 #:use-module (gnu system)
22 #:use-module (gnu services)
23 #:use-module (gnu services shepherd)
24 #:export (marionette-service-type
25 marionette-operating-system
26 define-os-with-source))
30 ;;; This module provides the infrastructure to run operating system tests.
31 ;;; The most important part of that is tools to instrument the OS under test,
32 ;;; essentially allowing to run in a virtual machine controlled by the host
33 ;;; system--hence the name "marionette".
37 (define (marionette-shepherd-service imported-modules)
38 "Return the Shepherd service for the marionette REPL"
42 (list (shepherd-service
43 (provision '(marionette))
44 (requirement '(udev)) ;so that DEVICE is available
45 (modules '((ice-9 match)
49 (imported-modules `((guix build syscalls)
53 (define (clear-echo termios)
54 (set-field termios (termios-local-flags)
55 (logand (lognot (local-flags ECHO))
56 (termios-local-flags termios))))
58 (define (self-quoting? x)
59 (letrec-syntax ((one-of (syntax-rules ()
63 (one-of rest ...))))))
64 (one-of symbol? string? pair? null? vector?
65 bytevector? number? boolean?)))
67 (match (primitive-fork)
72 (let* ((repl (open-file #$device "r+0"))
73 (termios (tcgetattr (fileno repl)))
74 (console (open-file "/dev/console" "r+0")))
75 ;; Don't echo input back.
76 (tcsetattr (fileno repl) (tcsetattr-action TCSANOW)
79 ;; Redirect output to the console.
82 (dup2 (fileno console) 1)
83 (dup2 (fileno console) 2)
96 (let ((result (primitive-eval expr)))
97 (write (if (self-quoting? result)
99 (object->string result))
102 (print-exception (current-error-port)
103 (stack-ref (make-stack #t) 1)
108 (primitive-exit 1))))
111 (stop #~(make-kill-destructor)))))
113 (define marionette-service-type
114 ;; This is the type of the "marionette" service, allowing a guest system to
115 ;; be manipulated from the host. This marionette REPL is essentially a
116 ;; universal backdoor.
117 (service-type (name 'marionette-repl)
119 (list (service-extension shepherd-root-service-type
120 marionette-shepherd-service)))))
122 (define* (marionette-operating-system os
123 #:key (imported-modules '()))
124 "Return a marionetteed variant of OS such that OS can be used as a marionette
125 in a virtual machine--i.e., controlled from the host system."
128 (services (cons (service marionette-service-type imported-modules)
129 (operating-system-user-services os)))))
131 (define-syntax define-os-with-source
132 (syntax-rules (use-modules operating-system)
133 "Define two variables: OS containing the given operating system, and
134 SOURCE containing the source to define OS as an sexp.
136 This is convenient when we need both the <operating-system> object so we can
137 instantiate it, and the source to create it so we can store in in a file in
138 the system under test."
140 (use-modules modules ...)
141 (operating-system fields ...))
144 (operating-system fields ...))
147 (use-modules modules ...)
148 (operating-system fields ...)))))))
150 ;;; tests.scm ends here