tests.scm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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>
  4. ;;; Copyright © 2017 Tobias Geerinckx-Rice <me@tobias.gr>
  5. ;;;
  6. ;;; This file is part of GNU Guix.
  7. ;;;
  8. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  9. ;;; under the terms of the GNU General Public License as published by
  10. ;;; the Free Software Foundation; either version 3 of the License, or (at
  11. ;;; your option) any later version.
  12. ;;;
  13. ;;; GNU Guix is distributed in the hope that it will be useful, but
  14. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;;; GNU General Public License for more details.
  17. ;;;
  18. ;;; You should have received a copy of the GNU General Public License
  19. ;;; 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)
  25. #:use-module (gnu bootloader grub)
  26. #:use-module (gnu system)
  27. #:use-module (gnu system file-systems)
  28. #:use-module (gnu system shadow)
  29. #:use-module (gnu services)
  30. #:use-module (gnu services base)
  31. #:use-module (gnu services shepherd)
  32. #:use-module (guix discovery)
  33. #:use-module (srfi srfi-1)
  34. #:use-module (srfi srfi-9 gnu)
  35. #:use-module (ice-9 match)
  36. #:export (marionette-configuration
  37. marionette-configuration?
  38. marionette-configuration-device
  39. marionette-configuration-imported-modules
  40. marionette-configuration-requirements
  41. marionette-service-type
  42. marionette-operating-system
  43. define-os-with-source
  44. simple-operating-system
  45. system-test
  46. system-test?
  47. system-test-name
  48. system-test-value
  49. system-test-description
  50. system-test-location
  51. fold-system-tests
  52. all-system-tests))
  53. ;;; Commentary:
  54. ;;;
  55. ;;; This module provides the infrastructure to run operating system tests.
  56. ;;; The most important part of that is tools to instrument the OS under test,
  57. ;;; essentially allowing to run in a virtual machine controlled by the host
  58. ;;; system--hence the name "marionette".
  59. ;;;
  60. ;;; Code:
  61. (define-record-type* <marionette-configuration>
  62. marionette-configuration make-marionette-configuration
  63. marionette-configuration?
  64. (device marionette-configuration-device ;string
  65. (default "/dev/hvc0"))
  66. (imported-modules marionette-configuration-imported-modules
  67. (default '()))
  68. (requirements marionette-configuration-requirements ;list of symbols
  69. (default '())))
  70. (define (marionette-shepherd-service config)
  71. "Return the Shepherd service for the marionette REPL"
  72. (match config
  73. (($ <marionette-configuration> device imported-modules requirement)
  74. (list (shepherd-service
  75. (provision '(marionette))
  76. ;; Always depend on UDEV so that DEVICE is available.
  77. (requirement `(udev ,@requirement))
  78. (modules '((ice-9 match)
  79. (srfi srfi-9 gnu)
  80. (guix build syscalls)
  81. (rnrs bytevectors)))
  82. (start
  83. (with-imported-modules `((guix build syscalls)
  84. ,@imported-modules)
  85. #~(lambda ()
  86. (define (clear-echo termios)
  87. (set-field termios (termios-local-flags)
  88. (logand (lognot (local-flags ECHO))
  89. (termios-local-flags termios))))
  90. (define (self-quoting? x)
  91. (letrec-syntax ((one-of (syntax-rules ()
  92. ((_) #f)
  93. ((_ pred rest ...)
  94. (or (pred x)
  95. (one-of rest ...))))))
  96. (one-of symbol? string? pair? null? vector?
  97. bytevector? number? boolean?)))
  98. (match (primitive-fork)
  99. (0
  100. (dynamic-wind
  101. (const #t)
  102. (lambda ()
  103. (let* ((repl (open-file #$device "r+0"))
  104. (termios (tcgetattr (fileno repl)))
  105. (console (open-file "/dev/console" "r+0")))
  106. ;; Don't echo input back.
  107. (tcsetattr (fileno repl) (tcsetattr-action TCSANOW)
  108. (clear-echo termios))
  109. ;; Redirect output to the console.
  110. (close-fdes 1)
  111. (close-fdes 2)
  112. (dup2 (fileno console) 1)
  113. (dup2 (fileno console) 2)
  114. (close-port console)
  115. (display 'ready repl)
  116. (let loop ()
  117. (newline repl)
  118. (match (read repl)
  119. ((? eof-object?)
  120. (primitive-exit 0))
  121. (expr
  122. (catch #t
  123. (lambda ()
  124. (let ((result (primitive-eval expr)))
  125. (write (if (self-quoting? result)
  126. result
  127. (object->string result))
  128. repl)))
  129. (lambda (key . args)
  130. (print-exception (current-error-port)
  131. (stack-ref (make-stack #t) 1)
  132. key args)
  133. (write #f repl)))))
  134. (loop))))
  135. (lambda ()
  136. (primitive-exit 1))))
  137. (pid
  138. pid)))))
  139. (stop #~(make-kill-destructor)))))))
  140. (define marionette-service-type
  141. ;; This is the type of the "marionette" service, allowing a guest system to
  142. ;; be manipulated from the host. This marionette REPL is essentially a
  143. ;; universal backdoor.
  144. (service-type (name 'marionette-repl)
  145. (extensions
  146. (list (service-extension shepherd-root-service-type
  147. marionette-shepherd-service)))))
  148. (define* (marionette-operating-system os
  149. #:key
  150. (imported-modules '())
  151. (requirements '()))
  152. "Return a marionetteed variant of OS such that OS can be used as a
  153. marionette in a virtual machine--i.e., controlled from the host system. The
  154. marionette service in the guest is started after the Shepherd services listed
  155. in REQUIREMENTS."
  156. (operating-system
  157. (inherit os)
  158. (services (cons (service marionette-service-type
  159. (marionette-configuration
  160. (requirements requirements)
  161. (imported-modules imported-modules)))
  162. (operating-system-user-services os)))))
  163. (define-syntax define-os-with-source
  164. (syntax-rules (use-modules operating-system)
  165. "Define two variables: OS containing the given operating system, and
  166. SOURCE containing the source to define OS as an sexp.
  167. This is convenient when we need both the <operating-system> object so we can
  168. instantiate it, and the source to create it so we can store in in a file in
  169. the system under test."
  170. ((_ (os source)
  171. (use-modules modules ...)
  172. (operating-system fields ...))
  173. (begin
  174. (define os
  175. (operating-system fields ...))
  176. (define source
  177. '(begin
  178. (use-modules modules ...)
  179. (operating-system fields ...)))))))
  180. ;;;
  181. ;;; Simple operating systems.
  182. ;;;
  183. (define %simple-os
  184. (operating-system
  185. (host-name "komputilo")
  186. (timezone "Europe/Berlin")
  187. (locale "en_US.UTF-8")
  188. (bootloader (bootloader-configuration
  189. (bootloader grub-bootloader)
  190. (target "/dev/sdX")))
  191. (file-systems (cons (file-system
  192. (device "my-root")
  193. (title 'label)
  194. (mount-point "/")
  195. (type "ext4"))
  196. %base-file-systems))
  197. (firmware '())
  198. (users (cons (user-account
  199. (name "alice")
  200. (comment "Bob's sister")
  201. (group "users")
  202. (supplementary-groups '("wheel" "audio" "video"))
  203. (home-directory "/home/alice"))
  204. %base-user-accounts))))
  205. (define-syntax-rule (simple-operating-system user-services ...)
  206. "Return an operating system that includes USER-SERVICES in addition to
  207. %BASE-SERVICES."
  208. (operating-system (inherit %simple-os)
  209. (services (cons* user-services ... %base-services))))
  210. ;;;
  211. ;;; Tests.
  212. ;;;
  213. (define-record-type* <system-test> system-test make-system-test
  214. system-test?
  215. (name system-test-name) ;string
  216. (value system-test-value) ;%STORE-MONAD value
  217. (description system-test-description) ;string
  218. (location system-test-location (innate) ;<location>
  219. (default (and=> (current-source-location)
  220. source-properties->location))))
  221. (define (write-system-test test port)
  222. (match test
  223. (($ <system-test> name _ _ ($ <location> file line))
  224. (format port "#<system-test ~a ~a:~a ~a>"
  225. name file line
  226. (number->string (object-address test) 16)))
  227. (($ <system-test> name)
  228. (format port "#<system-test ~a ~a>" name
  229. (number->string (object-address test) 16)))))
  230. (set-record-type-printer! <system-test> write-system-test)
  231. (define (test-modules)
  232. "Return the list of modules that define system tests."
  233. (scheme-modules (dirname (search-path %load-path "guix.scm"))
  234. "gnu/tests"))
  235. (define (fold-system-tests proc seed)
  236. "Invoke PROC on each system test, passing it the test and the previous
  237. result."
  238. (fold-module-public-variables (lambda (obj result)
  239. (if (system-test? obj)
  240. (cons obj result)
  241. result))
  242. '()
  243. (test-modules)))
  244. (define (all-system-tests)
  245. "Return the list of system tests."
  246. (reverse (fold-system-tests cons '())))
  247. ;;; tests.scm ends here