reconfigure.scm 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. '(string-contains (call-with-input-file "/etc/passwd"
  96. (lambda (port)
  97. (get-string-all port)))
  98. "jakob")
  99. marionette)))
  100. (test-end))))
  101. (gexp->derivation "switch-to-system" (test (switch-system-program os))))
  102. (define* (run-upgrade-services-test)
  103. "Run a test of an OS running UPGRADE-SERVICES-PROGRAM, which upgrades the
  104. Shepherd (PID 1) by unloading obsolete services and loading new services."
  105. (define os
  106. (marionette-operating-system
  107. (simple-operating-system)
  108. #:imported-modules '((gnu services herd)
  109. (guix combinators))))
  110. (define vm (virtual-machine os))
  111. (define dummy-service
  112. ;; Shepherd service that does nothing, for the sole purpose of ensuring
  113. ;; that it is properly installed and started by the script.
  114. (shepherd-service (provision '(dummy))
  115. (start #~(const #t))
  116. (stop #~(const #t))
  117. (respawn? #f)))
  118. (define (test enable-dummy disable-dummy)
  119. (with-imported-modules '((gnu build marionette))
  120. #~(begin
  121. (use-modules (gnu build marionette)
  122. (srfi srfi-64))
  123. (define marionette
  124. (make-marionette (list #$vm)))
  125. ;; Return the names of the running services on MARIONETTE.
  126. (define (running-services marionette)
  127. (marionette-eval
  128. '(begin
  129. (use-modules (gnu services herd))
  130. (map live-service-canonical-name (current-services)))
  131. marionette))
  132. (test-runner-current (system-test-runner #$output))
  133. (test-begin "upgrade-services")
  134. (let ((services-prior (running-services marionette)))
  135. (test-assert "script successfully evaluated"
  136. (marionette-eval
  137. '(primitive-load #$enable-dummy)
  138. marionette))
  139. (test-assert "script started new service"
  140. (and (not (memq 'dummy services-prior))
  141. (memq 'dummy (running-services marionette))))
  142. (test-assert "script successfully evaluated"
  143. (marionette-eval
  144. '(primitive-load #$disable-dummy)
  145. marionette))
  146. (test-assert "script stopped obsolete service"
  147. (not (memq 'dummy (running-services marionette)))))
  148. (test-end))))
  149. (gexp->derivation
  150. "upgrade-services"
  151. (let* ((file (shepherd-service-file dummy-service))
  152. (enable (upgrade-services-program (list file) '(dummy) '() '()))
  153. (disable (upgrade-services-program '() '() '(dummy) '())))
  154. (test enable disable))))
  155. (define* (run-install-bootloader-test)
  156. "Run a test of an OS running INSTALL-BOOTLOADER-PROGRAM, which installs a
  157. bootloader's configuration file."
  158. (define os
  159. (marionette-operating-system
  160. (simple-operating-system)
  161. #:imported-modules '((gnu services herd)
  162. (guix combinators))))
  163. (define vm (virtual-machine
  164. (operating-system os)
  165. (volatile? #f)))
  166. (define (test script)
  167. (with-imported-modules '((gnu build marionette))
  168. #~(begin
  169. (use-modules (gnu build marionette)
  170. (ice-9 regex)
  171. (srfi srfi-1)
  172. (srfi srfi-64))
  173. (define marionette
  174. (make-marionette (list #$vm)))
  175. ;; Return the system generation paths that have GRUB menu entries.
  176. (define (generations-in-grub-cfg marionette)
  177. (let ((grub-cfg (marionette-eval
  178. '(begin
  179. (call-with-input-file "/boot/grub/grub.cfg"
  180. (lambda (port)
  181. (get-string-all port))))
  182. marionette)))
  183. (map (lambda (parameter)
  184. (second (string-split (match:substring parameter) #\=)))
  185. (list-matches "system=[^ ]*" grub-cfg))))
  186. (test-runner-current (system-test-runner #$output))
  187. (test-begin "install-bootloader")
  188. (test-assert "no prior menu entry for system generation"
  189. (not (member #$os (generations-in-grub-cfg marionette))))
  190. (test-assert "script successfully evaluated"
  191. (marionette-eval
  192. '(primitive-load #$script)
  193. marionette))
  194. (test-assert "menu entry created for system generation"
  195. (member #$os (generations-in-grub-cfg marionette)))
  196. (test-end))))
  197. (let* ((bootloader ((compose bootloader-configuration-bootloader
  198. operating-system-bootloader)
  199. os))
  200. ;; The typical use-case for 'install-bootloader-program' is to read
  201. ;; the boot parameters for the existing menu entries on the system,
  202. ;; parse them with 'boot-parameters->menu-entry', and pass the
  203. ;; results to 'operating-system-bootcfg'. However, to obtain boot
  204. ;; parameters, we would need to start the marionette, which we should
  205. ;; ideally avoid doing outside of the 'test' G-Expression. Thus, we
  206. ;; generate a bootloader configuration for the script as if there
  207. ;; were no existing menu entries. In the grand scheme of things, this
  208. ;; matters little -- these tests should not make assertions about the
  209. ;; behavior of 'operating-system-bootcfg'.
  210. (bootcfg (operating-system-bootcfg os '()))
  211. (bootcfg-file (bootloader-configuration-file bootloader)))
  212. (gexp->derivation
  213. "install-bootloader"
  214. ;; Due to the read-only nature of the virtual machines used in the system
  215. ;; test suite, the bootloader installer script is omitted. 'grub-install'
  216. ;; would attempt to write directly to the virtual disk if the
  217. ;; installation script were run.
  218. (test
  219. (install-bootloader-program #f #f #f bootcfg bootcfg-file '(#f) "/")))))
  220. (define %test-switch-to-system
  221. (system-test
  222. (name "switch-to-system")
  223. (description "Create a new generation of the system profile.")
  224. (value (run-switch-to-system-test))))
  225. (define %test-upgrade-services
  226. (system-test
  227. (name "upgrade-services")
  228. (description "Upgrade the Shepherd by unloading obsolete services and
  229. loading new services.")
  230. (value (run-upgrade-services-test))))
  231. (define %test-install-bootloader
  232. (system-test
  233. (name "install-bootloader")
  234. (description "Install a bootloader and its configuration file.")
  235. (value (run-install-bootloader-test))))