reconfigure.scm 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2019 Jakob L. Kreuze <zerodaysfordays@sdf.org>
  3. ;;;
  4. ;;; This file is part of GNU Guix.
  5. ;;;
  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.
  10. ;;;
  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.
  15. ;;;
  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/>.
  18. (define-module (gnu tests reconfigure)
  19. #:use-module (gnu bootloader)
  20. #:use-module (gnu services shepherd)
  21. #:use-module (gnu system)
  22. #:use-module (gnu system accounts)
  23. #:use-module (gnu system shadow)
  24. #:use-module (gnu system vm)
  25. #:use-module (gnu tests)
  26. #:use-module (guix derivations)
  27. #:use-module (guix gexp)
  28. #:use-module (guix monads)
  29. #:use-module (guix scripts system reconfigure)
  30. #:use-module (guix store)
  31. #:export (%test-switch-to-system
  32. %test-upgrade-services
  33. %test-install-bootloader))
  34. ;;; Commentary:
  35. ;;;
  36. ;;; Test in-place system reconfiguration: advancing the system generation on a
  37. ;;; running instance of the Guix System.
  38. ;;;
  39. ;;; Code:
  40. (define* (run-switch-to-system-test)
  41. "Run a test of an OS running SWITCH-SYSTEM-PROGRAM, which creates a new
  42. generation of the system profile."
  43. (define os
  44. (marionette-operating-system
  45. (operating-system
  46. (inherit (simple-operating-system))
  47. (users (cons (user-account
  48. (name "jakob")
  49. (group "users")
  50. (home-directory "/home/jakob"))
  51. %base-user-accounts)))
  52. #:imported-modules '((gnu services herd)
  53. (guix combinators))))
  54. (define vm (virtual-machine os))
  55. (define (test script)
  56. (with-imported-modules '((gnu build marionette))
  57. #~(begin
  58. (use-modules (gnu build marionette)
  59. (srfi srfi-64))
  60. (define marionette
  61. (make-marionette (list #$vm)))
  62. ;; Return the names of the generation symlinks on MARIONETTE.
  63. (define (system-generations marionette)
  64. (marionette-eval
  65. '(begin
  66. (use-modules (ice-9 ftw)
  67. (srfi srfi-1))
  68. (let* ((profile-dir "/var/guix/profiles/")
  69. (entries (map first (cddr (file-system-tree profile-dir)))))
  70. (remove (lambda (entry)
  71. (member entry '("per-user" "system")))
  72. entries)))
  73. marionette))
  74. (test-runner-current (system-test-runner #$output))
  75. (test-begin "switch-to-system")
  76. (let ((generations-prior (system-generations marionette)))
  77. (test-assert "script successfully evaluated"
  78. (marionette-eval
  79. '(primitive-load #$script)
  80. marionette))
  81. (test-equal "script created new generation"
  82. (length (system-generations marionette))
  83. (1+ (length generations-prior)))
  84. (test-assert "script activated the new generation"
  85. (and (eqv? 'symlink
  86. (marionette-eval
  87. '(stat:type (lstat "/run/current-system"))
  88. marionette))
  89. (string= #$os
  90. (marionette-eval
  91. '(readlink "/run/current-system")
  92. marionette))))
  93. (test-assert "script activated user accounts"
  94. (marionette-eval
  95. '(begin
  96. (use-modules (rnrs io ports))
  97. (string-contains (call-with-input-file "/etc/passwd"
  98. get-string-all)
  99. "jakob"))
  100. marionette)))
  101. (test-end))))
  102. (gexp->derivation "switch-to-system" (test (switch-system-program os))))
  103. (define* (run-upgrade-services-test)
  104. "Run a test of an OS running UPGRADE-SERVICES-PROGRAM, which upgrades the
  105. Shepherd (PID 1) by unloading obsolete services and loading new services."
  106. (define os
  107. (marionette-operating-system
  108. (simple-operating-system)
  109. #:imported-modules '((gnu services herd)
  110. (guix combinators))))
  111. (define vm (virtual-machine os))
  112. (define dummy-service
  113. ;; Shepherd service that does nothing, for the sole purpose of ensuring
  114. ;; that it is properly installed and started by the script.
  115. (shepherd-service (provision '(dummy))
  116. (start #~(const #t))
  117. (stop #~(const #t))
  118. (respawn? #f)))
  119. (define (test enable-dummy disable-dummy)
  120. (with-imported-modules '((gnu build marionette))
  121. #~(begin
  122. (use-modules (gnu build marionette)
  123. (srfi srfi-64))
  124. (define marionette
  125. (make-marionette (list #$vm)))
  126. ;; Return the names of the running services on MARIONETTE.
  127. (define (running-services marionette)
  128. (marionette-eval
  129. '(begin
  130. (use-modules (gnu services herd))
  131. (map live-service-canonical-name (current-services)))
  132. marionette))
  133. (test-runner-current (system-test-runner #$output))
  134. (test-begin "upgrade-services")
  135. (let ((services-prior (running-services marionette)))
  136. (test-assert "script successfully evaluated"
  137. (marionette-eval
  138. '(primitive-load #$enable-dummy)
  139. marionette))
  140. (test-assert "script started new service"
  141. (and (not (memq 'dummy services-prior))
  142. (memq 'dummy (running-services marionette))))
  143. (test-assert "script successfully evaluated"
  144. (marionette-eval
  145. '(primitive-load #$disable-dummy)
  146. marionette))
  147. (test-assert "script stopped obsolete service"
  148. (not (memq 'dummy (running-services marionette)))))
  149. (test-end))))
  150. (gexp->derivation
  151. "upgrade-services"
  152. (let* ((file (shepherd-service-file dummy-service))
  153. (enable (upgrade-services-program (list file) '(dummy) '() '()))
  154. (disable (upgrade-services-program '() '() '(dummy) '())))
  155. (test enable disable))))
  156. (define* (run-install-bootloader-test)
  157. "Run a test of an OS running INSTALL-BOOTLOADER-PROGRAM, which installs a
  158. bootloader's configuration file."
  159. (define os
  160. (marionette-operating-system
  161. (simple-operating-system)
  162. #:imported-modules '((gnu services herd)
  163. (guix combinators))))
  164. (define vm (virtual-machine
  165. (operating-system os)
  166. (volatile? #f)))
  167. (define (test script)
  168. (with-imported-modules '((gnu build marionette))
  169. #~(begin
  170. (use-modules (gnu build marionette)
  171. (ice-9 regex)
  172. (srfi srfi-1)
  173. (srfi srfi-64))
  174. (define marionette
  175. (make-marionette (list #$vm)))
  176. ;; Return the system generation paths that have GRUB menu entries.
  177. (define (generations-in-grub-cfg marionette)
  178. (let ((grub-cfg (marionette-eval
  179. '(begin
  180. (use-modules (rnrs io ports))
  181. (call-with-input-file "/boot/grub/grub.cfg"
  182. get-string-all))
  183. marionette)))
  184. (map (lambda (parameter)
  185. (second (string-split (match:substring parameter) #\=)))
  186. (list-matches "system=[^ ]*" grub-cfg))))
  187. (test-runner-current (system-test-runner #$output))
  188. (test-begin "install-bootloader")
  189. (test-assert "no prior menu entry for system generation"
  190. (not (member #$os (generations-in-grub-cfg marionette))))
  191. (test-assert "script successfully evaluated"
  192. (marionette-eval
  193. '(primitive-load #$script)
  194. marionette))
  195. (test-assert "menu entry created for system generation"
  196. (member #$os (generations-in-grub-cfg marionette)))
  197. (test-end))))
  198. (let* ((bootloader ((compose bootloader-configuration-bootloader
  199. operating-system-bootloader)
  200. os))
  201. ;; The typical use-case for 'install-bootloader-program' is to read
  202. ;; the boot parameters for the existing menu entries on the system,
  203. ;; parse them with 'boot-parameters->menu-entry', and pass the
  204. ;; results to 'operating-system-bootcfg'. However, to obtain boot
  205. ;; parameters, we would need to start the marionette, which we should
  206. ;; ideally avoid doing outside of the 'test' G-Expression. Thus, we
  207. ;; generate a bootloader configuration for the script as if there
  208. ;; were no existing menu entries. In the grand scheme of things, this
  209. ;; matters little -- these tests should not make assertions about the
  210. ;; behavior of 'operating-system-bootcfg'.
  211. (bootcfg (operating-system-bootcfg os '()))
  212. (bootcfg-file (bootloader-configuration-file bootloader)))
  213. (gexp->derivation
  214. "install-bootloader"
  215. ;; Due to the read-only nature of the virtual machines used in the system
  216. ;; test suite, the bootloader installer script is omitted. 'grub-install'
  217. ;; would attempt to write directly to the virtual disk if the
  218. ;; installation script were run.
  219. (test
  220. (install-bootloader-program #f #f #f bootcfg bootcfg-file '(#f) "/")))))
  221. (define %test-switch-to-system
  222. (system-test
  223. (name "switch-to-system")
  224. (description "Create a new generation of the system profile.")
  225. (value (run-switch-to-system-test))))
  226. (define %test-upgrade-services
  227. (system-test
  228. (name "upgrade-services")
  229. (description "Upgrade the Shepherd by unloading obsolete services and
  230. loading new services.")
  231. (value (run-upgrade-services-test))))
  232. (define %test-install-bootloader
  233. (system-test
  234. (name "install-bootloader")
  235. (description "Install a bootloader and its configuration file.")
  236. (value (run-install-bootloader-test))))