tests.scm 11 KB

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