aboutsummaryrefslogtreecommitdiff
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2015, 2017 Ludovic Courtès <ludo@gnu.org>
;;; Copyright © 2020 Maxim Cournoyer <maxim.cournoyer@gmail.com>
;;;
;;; 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 (test-file-systems)
  #:use-module (guix store)
  #:use-module (guix modules)
  #:use-module (gnu system file-systems)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-64)
  #:use-module (ice-9 match))

;; Test the (gnu system file-systems) module.

(test-begin "file-systems")

(test-assert "file-system-needed-for-boot?"
  (let-syntax ((dummy-fs (syntax-rules ()
                           ((_ directory)
                            (file-system
                              (device "foo")
                              (mount-point directory)
                              (type "ext4"))))))
    (parameterize ((%store-prefix "/gnu/guix/store"))
      (and (file-system-needed-for-boot? (dummy-fs "/"))
           (file-system-needed-for-boot? (dummy-fs "/gnu"))
           (file-system-needed-for-boot? (dummy-fs "/gnu/guix"))
           (file-system-needed-for-boot? (dummy-fs "/gnu/guix/store"))
           (not (file-system-needed-for-boot?
                 (dummy-fs "/gnu/guix/store/foo")))
           (not (file-system-needed-for-boot? (dummy-fs "/gn")))
           (not (file-system-needed-for-boot?
                 (file-system
                   (inherit (dummy-fs (%store-prefix)))
                   (device "/foo")
                   (flags '(bind-mount read-only)))))))))

(test-assert "does not pull (guix config)"
  ;; This module is meant both for the host side and "build side", so make
  ;; sure it doesn't pull in (guix config), which depends on the user's
  ;; config.
  (not (member '(guix config)
               (source-module-closure '((gnu system file-systems))))))

(test-equal "does not pull (gnu packages …)"
  ;; Same story: (gnu packages …) should not be pulled.
  #f
  (find (match-lambda
          (('gnu 'packages _ ..1) #t)
          (_ #f))
        (source-module-closure '((gnu system file-systems)))))

(test-equal "file-system-options->alist"
  '("autodefrag" ("subvol" . "home") ("compress" . "lzo"))
  (file-system-options->alist "autodefrag,subvol=home,compress=lzo"))

(test-equal "file-system-options->alist (#f)"
  '()
  (file-system-options->alist #f))

(test-equal "alist->file-system-options"
  "autodefrag,subvol=root,compress=lzo"
  (alist->file-system-options '("autodefrag"
                                ("subvol" . "root")
                                ("compress" . "lzo"))))

(test-equal "alist->file-system-options (null)"
  #f
  (alist->file-system-options '()))


;;;
;;; Btrfs related.
;;;

(define %btrfs-root-subvolume
  (file-system
    (device (file-system-label "btrfs-pool"))
    (mount-point "/")
    (type "btrfs")
    (options "subvol=rootfs,compress=zstd")))

(define %btrfs-store-subvolid
  (file-system
    (device (file-system-label "btrfs-pool"))
    (mount-point "/gnu/store")
    (type "btrfs")
    (options "subvolid=10,compress=zstd")
    (dependencies (list %btrfs-root-subvolume))))

(define %btrfs-store-subvolume
  (file-system
    (device (file-system-label "btrfs-pool"))
    (mount-point "/gnu/store")
    (type "btrfs")
    (options "subvol=/some/nested/file/name")
    (dependencies (list %btrfs-root-subvolume))))

(test-assert "btrfs-subvolume? (subvol)"
  (btrfs-subvolume? %btrfs-root-subvolume))

(test-assert "btrfs-subvolume? (subvolid)"
  (btrfs-subvolume? %btrfs-store-subvolid))

(test-equal "btrfs-store-subvolume-file-name"
  "/some/nested/file/name"
  (parameterize ((%store-prefix "/gnu/store"))
    (btrfs-store-subvolume-file-name (list %btrfs-root-subvolume
                                           %btrfs-store-subvolume))))

(test-error "btrfs-store-subvolume-file-name (subvolid)"
            (parameterize ((%store-prefix "/gnu/store"))
              (btrfs-store-subvolume-file-name (list %btrfs-root-subvolume
                                                     %btrfs-store-subvolid))))

(test-end)
uot;) (location "Hong Kong") (timezone '("Europe" "Zagreb")) (keyboard '("English (US)" "English (intl., with AltGr dead keys)"))) "Converse over PORT with the guided installer to choose the specified LANGUAGE, LOCATION, TIMEZONE, and KEYBOARD." (converse port ((list-selection (title "Locale language") (multiple-choices? #f) (items _)) language) ((list-selection (title "Locale location") (multiple-choices? #f) (items _)) location) ((menu (title "GNU Guix install") (text _) (items (,guided _ ...))) ;"Guided graphical installation" guided) ((list-selection (title "Timezone") (multiple-choices? #f) (items _)) (first timezone)) ((list-selection (title "Timezone") (multiple-choices? #f) (items _)) (second timezone)) ((list-selection (title "Layout") (multiple-choices? #f) (items _)) (first keyboard)) ((list-selection (title "Variant") (multiple-choices? #f) (items _)) (second keyboard)))) (define* (enter-host-name+passwords port #:key (host-name "guix") (root-password "foo") (users '(("alice" "pass1") ("bob" "pass2") ("charlie" "pass3")))) "Converse over PORT with the guided installer to choose HOST-NAME, ROOT-PASSWORD, and USERS." (converse port ((input (title "Hostname") (text _) (default _)) host-name) ((input (title "System administrator password") (text _) (default _)) root-password) ((input (title "Password confirmation required") (text _) (default _)) root-password) ((add-users) (match users (((names passwords) ...) (map (lambda (name password) `(user (name ,name) (real-name ,(string-titlecase name)) (home-directory ,(string-append "/home/" name)) (password ,password))) names passwords)))))) (define* (choose-kernel port #:key (kernel "Linux Libre")) "Converse over PORT with the guided installer to choose the specified KERNEL." (converse port ((list-selection (title "Kernel") (multiple-choices? #f) (items _)) kernel))) (define* (choose-services port #:key (choose-desktop-environment? (const #f)) (choose-network-service? (lambda (service) (or (string-contains service "SSH") (string-contains service "NSS")))) (choose-network-management-tool? (lambda (service) (string-contains service "DHCP"))) (choose-misc-service? (lambda (service) (string-contains service "NTP"))) (choose-other-service? (const #f))) "Converse over PORT to choose services." (define desktop-environments '()) (converse port ((checkbox-list (title "Desktop environment") (text _) (items ,services)) (let ((desktops (filter choose-desktop-environment? services))) (set! desktop-environments desktops) desktops)) ((checkbox-list (title "Network service") (text _) (items ,services)) (filter choose-network-service? services)) ;; The "Network management" dialog shows up only when no desktop ;; environments have been selected, hence the guard. ((list-selection (title "Network management") (multiple-choices? #f) (items ,services)) (null? desktop-environments) (find choose-network-management-tool? services)) ((checkbox-list (title "Console services") (text _) (items ,services)) (null? desktop-environments) (filter choose-misc-service? services)) ((checkbox-list (title "Printing and document services") (text _) (items ,services)) (filter choose-other-service? services)))) (define (edit-configuration-file file) "Edit FILE, an operating system configuration file generated by the installer, by adding a marionette service such that the installed OS is instrumented for further testing." (define (read-expressions port) (let loop ((result '())) (match (read port) ((? eof-object?) (reverse result)) (exp (loop (cons exp result)))))) (define (edit exp) (match exp (('operating-system _ ...) `(marionette-operating-system ,exp #:imported-modules '((gnu services herd) (guix build utils) (guix combinators)))) (_ exp))) (let ((content (call-with-input-file file read-expressions))) ;; XXX: Remove the file before re-writing it, to be sure there are no ;; leftovers. We shouldn't have to do that as CALL-WITH-OUTPUT-FILE uses ;; the O_TRUNC flag by default. (delete-file file) (call-with-output-file file (lambda (port) (format port "\ ;; Operating system configuration edited for automated testing.~%~%") (pretty-print '(use-modules (gnu tests)) port) (for-each (lambda (exp) (pretty-print (edit exp) port) (newline port)) content))) #t)) (define* (choose-partitioning port #:key (encrypted? #t) (uefi-support? #f) (passphrase "thepassphrase") (edit-configuration-file edit-configuration-file)) "Converse over PORT to choose the partitioning method. When ENCRYPTED? is true, choose full-disk encryption with PASSPHRASE as the LUKS passphrase. When UEFI-SUPPORT? is true, assume that we are running the installation tests on an UEFI capable machine. This conversation stops when the user partitions have been formatted, right before the installer generates the configuration file and shows it in a dialog box. " (converse port ((list-selection (title "Partitioning method") (multiple-choices? #f) (items (,not-encrypted ,encrypted _ ...))) (if encrypted? encrypted not-encrypted)) ((list-selection (title "Disk") (multiple-choices? #f) (items (,disks ...))) ;; When running the installation from an ISO image, the CD/DVD drive ;; shows up in the list. Avoid it. (find (lambda (disk) (not (or (string-contains disk "DVD") (string-contains disk "CD-ROM")))) disks)) ;; The "Partition table" dialog pops up only if there's not already a ;; partition table and if the system does not support UEFI. ((list-selection (title "Partition table") (multiple-choices? #f) (items _)) ;; When UEFI is supported, the partition is forced to GPT by the ;; installer. (not uefi-support?) "gpt") ((list-selection (title "Partition scheme") (multiple-choices? #f) (items (,one-partition _ ...))) one-partition) ((list-selection (title "Guided partitioning") (multiple-choices? #f) (items (,disk _ ...))) disk) ((input (title "Password required") (text _) (default #f)) encrypted? ;only when ENCRYPTED? passphrase) ((input (title "Password confirmation required") (text _) (default #f)) encrypted? passphrase) ((confirmation (title "Format disk?") (text _)) #t) ((info (title "Preparing partitions") _ ...) (values)) ;nothing to return ((starting-final-step) ;; Do not return anything. The reply will be sent by ;; 'conclude-installation' and in the meantime the installer just waits ;; for us, giving us a chance to do things such as changing partition ;; UUIDs before it generates the configuration file. (values)))) (define (start-installation port) "Start the installation by checking over PORT that we get the generated configuration file, accepting it and starting the installation, and then receiving the pause message once the 'guix system init' process has completed." ;; Assume the previous message received was 'starting-final-step'; here we ;; send the reply to that message, which lets the installer continue. (write #t port) (newline port) (force-output port) (converse port ((file-dialog (title "Configuration file") (text _) (file ,configuration-file)) (edit-configuration-file configuration-file)) ((pause) ;"Press Enter to continue." (values)))) (define (complete-installation port) "Complete the installation by replying to the installer pause message and waiting for the installation-complete message." ;; Assume the previous message received was 'pause'; here we send the reply ;; to that message, which lets the installer continue. (write #t port) (newline port) (force-output port) (converse port ((installation-complete) (values)))) ;;; Local Variables: ;;; eval: (put 'converse 'scheme-indent-function 1) ;;; eval: (put 'with-ellipsis 'scheme-indent-function 1) ;;; End: