2016-05-04 21:31:08 +00:00
|
|
|
|
;;; GNU Guix --- Functional package management for GNU
|
2023-04-21 13:38:06 +00:00
|
|
|
|
;;; Copyright © 2016-2020, 2022-2023 Ludovic Courtès <ludo@gnu.org>
|
2017-05-15 20:24:18 +00:00
|
|
|
|
;;; Copyright © 2017 Mathieu Othacehe <m.othacehe@gmail.com>
|
2017-09-02 23:19:38 +00:00
|
|
|
|
;;; Copyright © 2017 Tobias Geerinckx-Rice <me@tobias.gr>
|
2021-03-30 10:40:14 +00:00
|
|
|
|
;;; Copyright © 2021 Maxime Devos <maximedevos@telenet.be>
|
2016-05-04 21:31:08 +00:00
|
|
|
|
;;;
|
|
|
|
|
;;; This file is part of GNU Guix.
|
|
|
|
|
;;;
|
|
|
|
|
;;; GNU Guix is free software; you can redistribute it and/or modify it
|
|
|
|
|
;;; under the terms of the GNU General Public License as published by
|
|
|
|
|
;;; the Free Software Foundation; either version 3 of the License, or (at
|
|
|
|
|
;;; your option) any later version.
|
|
|
|
|
;;;
|
|
|
|
|
;;; GNU Guix is distributed in the hope that it will be useful, but
|
|
|
|
|
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
;;; GNU General Public License for more details.
|
|
|
|
|
;;;
|
|
|
|
|
;;; You should have received a copy of the GNU General Public License
|
|
|
|
|
;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
(define-module (gnu tests)
|
|
|
|
|
#:use-module (guix gexp)
|
2020-07-24 20:58:08 +00:00
|
|
|
|
#:use-module (guix diagnostics)
|
2016-06-20 20:34:13 +00:00
|
|
|
|
#:use-module (guix records)
|
2018-08-29 20:32:05 +00:00
|
|
|
|
#:use-module ((guix ui) #:select (warn-about-load-error))
|
2017-09-02 23:19:38 +00:00
|
|
|
|
#:use-module (gnu bootloader)
|
2017-05-15 20:24:18 +00:00
|
|
|
|
#:use-module (gnu bootloader grub)
|
2016-05-04 21:31:08 +00:00
|
|
|
|
#:use-module (gnu system)
|
2017-03-31 20:13:50 +00:00
|
|
|
|
#:use-module (gnu system file-systems)
|
|
|
|
|
#:use-module (gnu system shadow)
|
2016-05-04 21:31:08 +00:00
|
|
|
|
#:use-module (gnu services)
|
2017-03-31 20:13:50 +00:00
|
|
|
|
#:use-module (gnu services base)
|
2016-05-04 21:31:08 +00:00
|
|
|
|
#:use-module (gnu services shepherd)
|
2017-05-04 09:43:01 +00:00
|
|
|
|
#:use-module (guix discovery)
|
2016-06-20 20:34:13 +00:00
|
|
|
|
#:use-module (srfi srfi-1)
|
|
|
|
|
#:use-module (srfi srfi-9 gnu)
|
|
|
|
|
#:use-module (ice-9 match)
|
2016-06-27 19:09:08 +00:00
|
|
|
|
#:export (marionette-configuration
|
|
|
|
|
marionette-configuration?
|
|
|
|
|
marionette-configuration-device
|
|
|
|
|
marionette-configuration-imported-modules
|
|
|
|
|
marionette-configuration-requirements
|
|
|
|
|
|
|
|
|
|
marionette-service-type
|
2016-06-20 19:51:59 +00:00
|
|
|
|
marionette-operating-system
|
2016-06-20 20:34:13 +00:00
|
|
|
|
define-os-with-source
|
|
|
|
|
|
2021-02-19 18:09:00 +00:00
|
|
|
|
%simple-os
|
2017-03-31 20:13:50 +00:00
|
|
|
|
simple-operating-system
|
|
|
|
|
|
2016-06-20 20:34:13 +00:00
|
|
|
|
system-test
|
|
|
|
|
system-test?
|
|
|
|
|
system-test-name
|
|
|
|
|
system-test-value
|
|
|
|
|
system-test-description
|
|
|
|
|
system-test-location
|
|
|
|
|
|
|
|
|
|
fold-system-tests
|
|
|
|
|
all-system-tests))
|
2016-05-04 21:31:08 +00:00
|
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
;;;
|
|
|
|
|
;;; This module provides the infrastructure to run operating system tests.
|
|
|
|
|
;;; The most important part of that is tools to instrument the OS under test,
|
2020-01-22 21:17:49 +00:00
|
|
|
|
;;; essentially allowing it to run in a virtual machine controlled by the host
|
2016-05-04 21:31:08 +00:00
|
|
|
|
;;; system--hence the name "marionette".
|
|
|
|
|
;;;
|
|
|
|
|
;;; Code:
|
|
|
|
|
|
2016-06-27 19:09:08 +00:00
|
|
|
|
(define-record-type* <marionette-configuration>
|
|
|
|
|
marionette-configuration make-marionette-configuration
|
|
|
|
|
marionette-configuration?
|
|
|
|
|
(device marionette-configuration-device ;string
|
2018-02-19 20:58:18 +00:00
|
|
|
|
(default "/dev/virtio-ports/org.gnu.guix.port.0"))
|
2016-06-27 19:09:08 +00:00
|
|
|
|
(imported-modules marionette-configuration-imported-modules
|
|
|
|
|
(default '()))
|
2021-03-30 10:40:14 +00:00
|
|
|
|
(extensions marionette-configuration-extensions
|
|
|
|
|
(default '())) ; list of packages
|
2016-06-27 19:09:08 +00:00
|
|
|
|
(requirements marionette-configuration-requirements ;list of symbols
|
|
|
|
|
(default '())))
|
|
|
|
|
|
2021-03-30 10:40:14 +00:00
|
|
|
|
;; Hack: avoid indenting code beyond column 80 in marionette-shepherd-service.
|
|
|
|
|
(define-syntax-rule (with-imported-modules-and-extensions imported-modules
|
|
|
|
|
extensions
|
|
|
|
|
gexp)
|
|
|
|
|
(with-imported-modules imported-modules
|
|
|
|
|
(with-extensions extensions
|
|
|
|
|
gexp)))
|
|
|
|
|
|
2023-04-21 13:38:06 +00:00
|
|
|
|
(define (marionette-program device imported-modules extensions)
|
|
|
|
|
"Return the program that runs the marionette REPL on DEVICE. Ensure
|
|
|
|
|
IMPORTED-MODULES and EXTENSIONS are accessible from the REPL."
|
|
|
|
|
(define code
|
|
|
|
|
(with-imported-modules-and-extensions
|
|
|
|
|
`((guix build utils)
|
|
|
|
|
(guix build syscalls)
|
|
|
|
|
,@imported-modules)
|
|
|
|
|
extensions
|
|
|
|
|
#~(begin
|
|
|
|
|
(use-modules (ice-9 match)
|
|
|
|
|
(ice-9 binary-ports))
|
|
|
|
|
|
|
|
|
|
(define (self-quoting? x)
|
|
|
|
|
(letrec-syntax ((one-of (syntax-rules ()
|
|
|
|
|
((_) #f)
|
|
|
|
|
((_ pred rest ...)
|
|
|
|
|
(or (pred x)
|
|
|
|
|
(one-of rest ...))))))
|
|
|
|
|
(one-of symbol? string? keyword? pair? null? array?
|
|
|
|
|
number? boolean? char?)))
|
|
|
|
|
|
|
|
|
|
(let ((repl (open-file #$device "r+0"))
|
|
|
|
|
(console (open-file "/dev/console" "r+0")))
|
|
|
|
|
;; Redirect output to the console.
|
|
|
|
|
(close-fdes 1)
|
|
|
|
|
(close-fdes 2)
|
|
|
|
|
(dup2 (fileno console) 1)
|
|
|
|
|
(dup2 (fileno console) 2)
|
|
|
|
|
(close-port console)
|
|
|
|
|
|
|
|
|
|
(display 'ready repl)
|
|
|
|
|
(let loop ()
|
|
|
|
|
(newline repl)
|
|
|
|
|
|
|
|
|
|
(match (read repl)
|
|
|
|
|
((? eof-object?)
|
|
|
|
|
(primitive-exit 0))
|
|
|
|
|
(expr
|
|
|
|
|
(catch #t
|
|
|
|
|
(lambda ()
|
|
|
|
|
(let ((result (primitive-eval expr)))
|
|
|
|
|
(write (if (self-quoting? result)
|
|
|
|
|
result
|
|
|
|
|
(object->string result))
|
|
|
|
|
repl)))
|
|
|
|
|
(lambda (key . args)
|
|
|
|
|
(print-exception (current-error-port)
|
|
|
|
|
(stack-ref (make-stack #t) 1)
|
|
|
|
|
key args)
|
|
|
|
|
(write #f repl)))))
|
|
|
|
|
(loop))))))
|
|
|
|
|
|
|
|
|
|
(program-file "marionette-repl.scm" code))
|
|
|
|
|
|
2016-06-27 19:09:08 +00:00
|
|
|
|
(define (marionette-shepherd-service config)
|
2016-05-04 21:31:08 +00:00
|
|
|
|
"Return the Shepherd service for the marionette REPL"
|
2016-06-27 19:09:08 +00:00
|
|
|
|
(match config
|
2021-03-30 10:40:14 +00:00
|
|
|
|
(($ <marionette-configuration> device imported-modules extensions
|
|
|
|
|
requirement)
|
2016-06-27 19:09:08 +00:00
|
|
|
|
(list (shepherd-service
|
|
|
|
|
(provision '(marionette))
|
|
|
|
|
|
|
|
|
|
;; Always depend on UDEV so that DEVICE is available.
|
|
|
|
|
(requirement `(udev ,@requirement))
|
|
|
|
|
|
|
|
|
|
(modules '((ice-9 match)
|
2019-09-23 20:07:53 +00:00
|
|
|
|
(srfi srfi-9 gnu)))
|
2023-04-21 13:38:06 +00:00
|
|
|
|
(start #~(make-forkexec-constructor
|
|
|
|
|
(list #$(marionette-program device
|
|
|
|
|
imported-modules
|
|
|
|
|
extensions))))
|
2016-06-27 19:09:08 +00:00
|
|
|
|
(stop #~(make-kill-destructor)))))))
|
2016-05-04 21:31:08 +00:00
|
|
|
|
|
|
|
|
|
(define marionette-service-type
|
|
|
|
|
;; This is the type of the "marionette" service, allowing a guest system to
|
|
|
|
|
;; be manipulated from the host. This marionette REPL is essentially a
|
2016-06-19 22:21:29 +00:00
|
|
|
|
;; universal backdoor.
|
2016-05-04 21:31:08 +00:00
|
|
|
|
(service-type (name 'marionette-repl)
|
|
|
|
|
(extensions
|
|
|
|
|
(list (service-extension shepherd-root-service-type
|
2022-05-13 21:39:17 +00:00
|
|
|
|
marionette-shepherd-service)))
|
|
|
|
|
(description "The @dfn{marionette} service allows a guest
|
|
|
|
|
system (virtual machine) to be manipulated by the host. It is used for system
|
|
|
|
|
tests.")))
|
2016-05-04 21:31:08 +00:00
|
|
|
|
|
|
|
|
|
(define* (marionette-operating-system os
|
2016-06-27 19:09:08 +00:00
|
|
|
|
#:key
|
|
|
|
|
(imported-modules '())
|
2021-03-30 10:40:14 +00:00
|
|
|
|
(extensions '())
|
2016-06-27 19:09:08 +00:00
|
|
|
|
(requirements '()))
|
|
|
|
|
"Return a marionetteed variant of OS such that OS can be used as a
|
|
|
|
|
marionette in a virtual machine--i.e., controlled from the host system. The
|
|
|
|
|
marionette service in the guest is started after the Shepherd services listed
|
2021-03-30 10:40:14 +00:00
|
|
|
|
in REQUIREMENTS. The packages in the list EXTENSIONS are made available from
|
|
|
|
|
the backdoor REPL."
|
2016-05-04 21:31:08 +00:00
|
|
|
|
(operating-system
|
|
|
|
|
(inherit os)
|
2018-01-09 08:26:27 +00:00
|
|
|
|
;; Make sure the guest dies on error.
|
|
|
|
|
(kernel-arguments (cons "panic=1"
|
|
|
|
|
(operating-system-user-kernel-arguments os)))
|
|
|
|
|
;; Make sure the guest doesn't hang in the REPL on error.
|
|
|
|
|
(initrd (lambda (fs . rest)
|
|
|
|
|
(apply (operating-system-initrd os) fs
|
|
|
|
|
#:on-error 'backtrace
|
|
|
|
|
rest)))
|
2016-06-27 19:09:08 +00:00
|
|
|
|
(services (cons (service marionette-service-type
|
|
|
|
|
(marionette-configuration
|
|
|
|
|
(requirements requirements)
|
2021-03-30 10:40:14 +00:00
|
|
|
|
(extensions extensions)
|
2016-06-27 19:09:08 +00:00
|
|
|
|
(imported-modules imported-modules)))
|
2016-05-04 21:31:08 +00:00
|
|
|
|
(operating-system-user-services os)))))
|
|
|
|
|
|
2016-06-20 19:51:59 +00:00
|
|
|
|
(define-syntax define-os-with-source
|
|
|
|
|
(syntax-rules (use-modules operating-system)
|
|
|
|
|
"Define two variables: OS containing the given operating system, and
|
|
|
|
|
SOURCE containing the source to define OS as an sexp.
|
|
|
|
|
|
|
|
|
|
This is convenient when we need both the <operating-system> object so we can
|
|
|
|
|
instantiate it, and the source to create it so we can store in in a file in
|
|
|
|
|
the system under test."
|
|
|
|
|
((_ (os source)
|
|
|
|
|
(use-modules modules ...)
|
|
|
|
|
(operating-system fields ...))
|
|
|
|
|
(begin
|
|
|
|
|
(define os
|
|
|
|
|
(operating-system fields ...))
|
|
|
|
|
(define source
|
|
|
|
|
'(begin
|
|
|
|
|
(use-modules modules ...)
|
|
|
|
|
(operating-system fields ...)))))))
|
|
|
|
|
|
2017-03-31 20:13:50 +00:00
|
|
|
|
|
|
|
|
|
;;;
|
|
|
|
|
;;; Simple operating systems.
|
|
|
|
|
;;;
|
|
|
|
|
|
|
|
|
|
(define %simple-os
|
|
|
|
|
(operating-system
|
|
|
|
|
(host-name "komputilo")
|
|
|
|
|
(timezone "Europe/Berlin")
|
|
|
|
|
(locale "en_US.UTF-8")
|
|
|
|
|
|
2017-09-02 23:19:38 +00:00
|
|
|
|
(bootloader (bootloader-configuration
|
|
|
|
|
(bootloader grub-bootloader)
|
2021-08-07 19:07:47 +00:00
|
|
|
|
(targets '("/dev/sdX"))))
|
2017-03-31 20:13:50 +00:00
|
|
|
|
(file-systems (cons (file-system
|
2018-05-18 13:04:36 +00:00
|
|
|
|
(device (file-system-label "my-root"))
|
2017-03-31 20:13:50 +00:00
|
|
|
|
(mount-point "/")
|
|
|
|
|
(type "ext4"))
|
|
|
|
|
%base-file-systems))
|
|
|
|
|
(firmware '())
|
|
|
|
|
|
|
|
|
|
(users (cons (user-account
|
|
|
|
|
(name "alice")
|
|
|
|
|
(comment "Bob's sister")
|
|
|
|
|
(group "users")
|
2019-03-22 13:06:54 +00:00
|
|
|
|
(supplementary-groups '("wheel" "audio" "video")))
|
2017-03-31 20:13:50 +00:00
|
|
|
|
%base-user-accounts))))
|
|
|
|
|
|
|
|
|
|
(define-syntax-rule (simple-operating-system user-services ...)
|
|
|
|
|
"Return an operating system that includes USER-SERVICES in addition to
|
|
|
|
|
%BASE-SERVICES."
|
|
|
|
|
(operating-system (inherit %simple-os)
|
|
|
|
|
(services (cons* user-services ... %base-services))))
|
|
|
|
|
|
|
|
|
|
|
2016-06-20 20:34:13 +00:00
|
|
|
|
|
|
|
|
|
;;;
|
|
|
|
|
;;; Tests.
|
|
|
|
|
;;;
|
|
|
|
|
|
|
|
|
|
(define-record-type* <system-test> system-test make-system-test
|
|
|
|
|
system-test?
|
|
|
|
|
(name system-test-name) ;string
|
|
|
|
|
(value system-test-value) ;%STORE-MONAD value
|
|
|
|
|
(description system-test-description) ;string
|
|
|
|
|
(location system-test-location (innate) ;<location>
|
|
|
|
|
(default (and=> (current-source-location)
|
|
|
|
|
source-properties->location))))
|
|
|
|
|
|
|
|
|
|
(define (write-system-test test port)
|
|
|
|
|
(match test
|
|
|
|
|
(($ <system-test> name _ _ ($ <location> file line))
|
|
|
|
|
(format port "#<system-test ~a ~a:~a ~a>"
|
|
|
|
|
name file line
|
|
|
|
|
(number->string (object-address test) 16)))
|
|
|
|
|
(($ <system-test> name)
|
|
|
|
|
(format port "#<system-test ~a ~a>" name
|
|
|
|
|
(number->string (object-address test) 16)))))
|
|
|
|
|
|
|
|
|
|
(set-record-type-printer! <system-test> write-system-test)
|
|
|
|
|
|
2020-03-05 15:10:40 +00:00
|
|
|
|
(define-gexp-compiler (compile-system-test (test <system-test>)
|
|
|
|
|
system target)
|
|
|
|
|
"Compile TEST to a derivation."
|
|
|
|
|
;; XXX: SYSTEM and TARGET are ignored.
|
|
|
|
|
(system-test-value test))
|
|
|
|
|
|
2016-06-20 20:34:13 +00:00
|
|
|
|
(define (test-modules)
|
|
|
|
|
"Return the list of modules that define system tests."
|
|
|
|
|
(scheme-modules (dirname (search-path %load-path "guix.scm"))
|
2018-08-29 20:32:05 +00:00
|
|
|
|
"gnu/tests"
|
|
|
|
|
#:warn warn-about-load-error))
|
2016-06-20 20:34:13 +00:00
|
|
|
|
|
|
|
|
|
(define (fold-system-tests proc seed)
|
|
|
|
|
"Invoke PROC on each system test, passing it the test and the previous
|
|
|
|
|
result."
|
2017-05-04 09:43:01 +00:00
|
|
|
|
(fold-module-public-variables (lambda (obj result)
|
|
|
|
|
(if (system-test? obj)
|
|
|
|
|
(cons obj result)
|
|
|
|
|
result))
|
|
|
|
|
'()
|
|
|
|
|
(test-modules)))
|
2016-06-20 20:34:13 +00:00
|
|
|
|
|
|
|
|
|
(define (all-system-tests)
|
|
|
|
|
"Return the list of system tests."
|
|
|
|
|
(reverse (fold-system-tests cons '())))
|
|
|
|
|
|
2021-03-30 10:40:14 +00:00
|
|
|
|
|
|
|
|
|
;; Local Variables:
|
|
|
|
|
;; eval: (put 'with-imported-modules-and-extensions 'scheme-indent-function 2)
|
|
|
|
|
;; End:
|
|
|
|
|
|
2016-05-04 21:31:08 +00:00
|
|
|
|
;;; tests.scm ends here
|