2016-05-04 23:31:08 +02:00
|
|
|
|
;;; GNU Guix --- Functional package management for GNU
|
2022-05-13 23:39:17 +02:00
|
|
|
|
;;; Copyright © 2016-2020, 2022 Ludovic Courtès <ludo@gnu.org>
|
2017-05-15 22:24:18 +02:00
|
|
|
|
;;; Copyright © 2017 Mathieu Othacehe <m.othacehe@gmail.com>
|
2017-09-03 01:19:38 +02:00
|
|
|
|
;;; Copyright © 2017 Tobias Geerinckx-Rice <me@tobias.gr>
|
2021-03-30 12:40:14 +02:00
|
|
|
|
;;; Copyright © 2021 Maxime Devos <maximedevos@telenet.be>
|
2016-05-04 23:31:08 +02: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 22:58:08 +02:00
|
|
|
|
#:use-module (guix diagnostics)
|
2016-06-20 22:34:13 +02:00
|
|
|
|
#:use-module (guix records)
|
2018-08-29 22:32:05 +02:00
|
|
|
|
#:use-module ((guix ui) #:select (warn-about-load-error))
|
2017-09-03 01:19:38 +02:00
|
|
|
|
#:use-module (gnu bootloader)
|
2017-05-15 22:24:18 +02:00
|
|
|
|
#:use-module (gnu bootloader grub)
|
2016-05-04 23:31:08 +02:00
|
|
|
|
#:use-module (gnu system)
|
2017-03-31 22:13:50 +02:00
|
|
|
|
#:use-module (gnu system file-systems)
|
|
|
|
|
#:use-module (gnu system shadow)
|
2016-05-04 23:31:08 +02:00
|
|
|
|
#:use-module (gnu services)
|
2017-03-31 22:13:50 +02:00
|
|
|
|
#:use-module (gnu services base)
|
2016-05-04 23:31:08 +02:00
|
|
|
|
#:use-module (gnu services shepherd)
|
2017-05-04 11:43:01 +02:00
|
|
|
|
#:use-module (guix discovery)
|
2016-06-20 22:34:13 +02:00
|
|
|
|
#:use-module (srfi srfi-1)
|
|
|
|
|
#:use-module (srfi srfi-9 gnu)
|
|
|
|
|
#:use-module (ice-9 match)
|
2016-06-27 21:09:08 +02:00
|
|
|
|
#:export (marionette-configuration
|
|
|
|
|
marionette-configuration?
|
|
|
|
|
marionette-configuration-device
|
|
|
|
|
marionette-configuration-imported-modules
|
|
|
|
|
marionette-configuration-requirements
|
|
|
|
|
|
|
|
|
|
marionette-service-type
|
2016-06-20 21:51:59 +02:00
|
|
|
|
marionette-operating-system
|
2016-06-20 22:34:13 +02:00
|
|
|
|
define-os-with-source
|
|
|
|
|
|
2021-02-19 19:09:00 +01:00
|
|
|
|
%simple-os
|
2017-03-31 22:13:50 +02:00
|
|
|
|
simple-operating-system
|
|
|
|
|
|
2016-06-20 22:34:13 +02: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 23:31:08 +02: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 22:17:49 +01:00
|
|
|
|
;;; essentially allowing it to run in a virtual machine controlled by the host
|
2016-05-04 23:31:08 +02:00
|
|
|
|
;;; system--hence the name "marionette".
|
|
|
|
|
;;;
|
|
|
|
|
;;; Code:
|
|
|
|
|
|
2016-06-27 21:09:08 +02:00
|
|
|
|
(define-record-type* <marionette-configuration>
|
|
|
|
|
marionette-configuration make-marionette-configuration
|
|
|
|
|
marionette-configuration?
|
|
|
|
|
(device marionette-configuration-device ;string
|
2018-02-19 21:58:18 +01:00
|
|
|
|
(default "/dev/virtio-ports/org.gnu.guix.port.0"))
|
2016-06-27 21:09:08 +02:00
|
|
|
|
(imported-modules marionette-configuration-imported-modules
|
|
|
|
|
(default '()))
|
2021-03-30 12:40:14 +02:00
|
|
|
|
(extensions marionette-configuration-extensions
|
|
|
|
|
(default '())) ; list of packages
|
2016-06-27 21:09:08 +02:00
|
|
|
|
(requirements marionette-configuration-requirements ;list of symbols
|
|
|
|
|
(default '())))
|
|
|
|
|
|
2021-03-30 12:40:14 +02: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)))
|
|
|
|
|
|
2016-06-27 21:09:08 +02:00
|
|
|
|
(define (marionette-shepherd-service config)
|
2016-05-04 23:31:08 +02:00
|
|
|
|
"Return the Shepherd service for the marionette REPL"
|
2016-06-27 21:09:08 +02:00
|
|
|
|
(match config
|
2021-03-30 12:40:14 +02:00
|
|
|
|
(($ <marionette-configuration> device imported-modules extensions
|
|
|
|
|
requirement)
|
2016-06-27 21:09:08 +02: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 22:07:53 +02:00
|
|
|
|
(srfi srfi-9 gnu)))
|
2016-06-27 21:09:08 +02:00
|
|
|
|
(start
|
2021-03-30 12:40:14 +02:00
|
|
|
|
(with-imported-modules-and-extensions imported-modules extensions
|
2016-07-12 00:38:50 +02:00
|
|
|
|
#~(lambda ()
|
|
|
|
|
(define (self-quoting? x)
|
|
|
|
|
(letrec-syntax ((one-of (syntax-rules ()
|
|
|
|
|
((_) #f)
|
|
|
|
|
((_ pred rest ...)
|
|
|
|
|
(or (pred x)
|
|
|
|
|
(one-of rest ...))))))
|
2019-09-23 22:07:53 +02:00
|
|
|
|
(one-of symbol? string? keyword? pair? null? array?
|
2019-12-15 21:27:31 +01:00
|
|
|
|
number? boolean? char?)))
|
2016-07-12 00:38:50 +02:00
|
|
|
|
|
|
|
|
|
(match (primitive-fork)
|
|
|
|
|
(0
|
|
|
|
|
(dynamic-wind
|
|
|
|
|
(const #t)
|
|
|
|
|
(lambda ()
|
2018-02-19 21:58:18 +01:00
|
|
|
|
(let ((repl (open-file #$device "r+0"))
|
|
|
|
|
(console (open-file "/dev/console" "r+0")))
|
2016-07-12 00:38:50 +02:00
|
|
|
|
;; 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))))
|
|
|
|
|
(lambda ()
|
|
|
|
|
(primitive-exit 1))))
|
|
|
|
|
(pid
|
|
|
|
|
pid)))))
|
2016-06-27 21:09:08 +02:00
|
|
|
|
(stop #~(make-kill-destructor)))))))
|
2016-05-04 23:31:08 +02: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-20 00:21:29 +02:00
|
|
|
|
;; universal backdoor.
|
2016-05-04 23:31:08 +02:00
|
|
|
|
(service-type (name 'marionette-repl)
|
|
|
|
|
(extensions
|
|
|
|
|
(list (service-extension shepherd-root-service-type
|
2022-05-13 23:39:17 +02: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 23:31:08 +02:00
|
|
|
|
|
|
|
|
|
(define* (marionette-operating-system os
|
2016-06-27 21:09:08 +02:00
|
|
|
|
#:key
|
|
|
|
|
(imported-modules '())
|
2021-03-30 12:40:14 +02:00
|
|
|
|
(extensions '())
|
2016-06-27 21:09:08 +02: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 12:40:14 +02:00
|
|
|
|
in REQUIREMENTS. The packages in the list EXTENSIONS are made available from
|
|
|
|
|
the backdoor REPL."
|
2016-05-04 23:31:08 +02:00
|
|
|
|
(operating-system
|
|
|
|
|
(inherit os)
|
2018-01-09 09:26:27 +01: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 21:09:08 +02:00
|
|
|
|
(services (cons (service marionette-service-type
|
|
|
|
|
(marionette-configuration
|
|
|
|
|
(requirements requirements)
|
2021-03-30 12:40:14 +02:00
|
|
|
|
(extensions extensions)
|
2016-06-27 21:09:08 +02:00
|
|
|
|
(imported-modules imported-modules)))
|
2016-05-04 23:31:08 +02:00
|
|
|
|
(operating-system-user-services os)))))
|
|
|
|
|
|
2016-06-20 21:51:59 +02: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 22:13:50 +02:00
|
|
|
|
|
|
|
|
|
;;;
|
|
|
|
|
;;; Simple operating systems.
|
|
|
|
|
;;;
|
|
|
|
|
|
|
|
|
|
(define %simple-os
|
|
|
|
|
(operating-system
|
|
|
|
|
(host-name "komputilo")
|
|
|
|
|
(timezone "Europe/Berlin")
|
|
|
|
|
(locale "en_US.UTF-8")
|
|
|
|
|
|
2017-09-03 01:19:38 +02:00
|
|
|
|
(bootloader (bootloader-configuration
|
|
|
|
|
(bootloader grub-bootloader)
|
2021-08-07 21:07:47 +02:00
|
|
|
|
(targets '("/dev/sdX"))))
|
2017-03-31 22:13:50 +02:00
|
|
|
|
(file-systems (cons (file-system
|
2018-05-18 15:04:36 +02:00
|
|
|
|
(device (file-system-label "my-root"))
|
2017-03-31 22:13:50 +02:00
|
|
|
|
(mount-point "/")
|
|
|
|
|
(type "ext4"))
|
|
|
|
|
%base-file-systems))
|
|
|
|
|
(firmware '())
|
|
|
|
|
|
|
|
|
|
(users (cons (user-account
|
|
|
|
|
(name "alice")
|
|
|
|
|
(comment "Bob's sister")
|
|
|
|
|
(group "users")
|
2019-03-22 14:06:54 +01:00
|
|
|
|
(supplementary-groups '("wheel" "audio" "video")))
|
2017-03-31 22:13:50 +02: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 22:34:13 +02: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 16:10:40 +01: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 22:34:13 +02: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 22:32:05 +02:00
|
|
|
|
"gnu/tests"
|
|
|
|
|
#:warn warn-about-load-error))
|
2016-06-20 22:34:13 +02:00
|
|
|
|
|
|
|
|
|
(define (fold-system-tests proc seed)
|
|
|
|
|
"Invoke PROC on each system test, passing it the test and the previous
|
|
|
|
|
result."
|
2017-05-04 11:43:01 +02:00
|
|
|
|
(fold-module-public-variables (lambda (obj result)
|
|
|
|
|
(if (system-test? obj)
|
|
|
|
|
(cons obj result)
|
|
|
|
|
result))
|
|
|
|
|
'()
|
|
|
|
|
(test-modules)))
|
2016-06-20 22:34:13 +02:00
|
|
|
|
|
|
|
|
|
(define (all-system-tests)
|
|
|
|
|
"Return the list of system tests."
|
|
|
|
|
(reverse (fold-system-tests cons '())))
|
|
|
|
|
|
2021-03-30 12:40:14 +02:00
|
|
|
|
|
|
|
|
|
;; Local Variables:
|
|
|
|
|
;; eval: (put 'with-imported-modules-and-extensions 'scheme-indent-function 2)
|
|
|
|
|
;; End:
|
|
|
|
|
|
2016-05-04 23:31:08 +02:00
|
|
|
|
;;; tests.scm ends here
|