Merge pull request #78670 from tfc/port-installer-test

nixosTests.installer: Port installer and ZFS test to python
This commit is contained in:
Florian Klink 2020-02-08 15:36:28 +01:00 committed by GitHub
commit dd5f92f20b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 583 additions and 536 deletions

View File

@ -1,13 +1,17 @@
#! /somewhere/python3 #! /somewhere/python3
from contextlib import contextmanager, _GeneratorContextManager from contextlib import contextmanager, _GeneratorContextManager
from queue import Queue, Empty
from typing import Tuple, Any, Callable, Dict, Iterator, Optional, List
from xml.sax.saxutils import XMLGenerator from xml.sax.saxutils import XMLGenerator
import _thread import _thread
import atexit import atexit
import base64
import os import os
import pathlib
import ptpython.repl import ptpython.repl
import pty import pty
from queue import Queue, Empty
import re import re
import shlex
import shutil import shutil
import socket import socket
import subprocess import subprocess
@ -15,9 +19,6 @@ import sys
import tempfile import tempfile
import time import time
import unicodedata import unicodedata
from typing import Tuple, Any, Callable, Dict, Iterator, Optional, List
import shlex
import pathlib
CHAR_TO_KEY = { CHAR_TO_KEY = {
"A": "shift-a", "A": "shift-a",
@ -566,6 +567,41 @@ class Machine:
if ret.returncode != 0: if ret.returncode != 0:
raise Exception("Cannot convert screenshot") raise Exception("Cannot convert screenshot")
def copy_from_host_via_shell(self, source: str, target: str) -> None:
"""Copy a file from the host into the guest by piping it over the
shell into the destination file. Works without host-guest shared folder.
Prefer copy_from_host for whenever possible.
"""
with open(source, "rb") as fh:
content_b64 = base64.b64encode(fh.read()).decode()
self.succeed(
f"mkdir -p $(dirname {target})",
f"echo -n {content_b64} | base64 -d > {target}",
)
def copy_from_host(self, source: str, target: str) -> None:
"""Copy a file from the host into the guest via the `shared_dir` shared
among all the VMs (using a temporary directory).
"""
host_src = pathlib.Path(source)
vm_target = pathlib.Path(target)
with tempfile.TemporaryDirectory(dir=self.shared_dir) as shared_td:
shared_temp = pathlib.Path(shared_td)
host_intermediate = shared_temp / host_src.name
vm_shared_temp = pathlib.Path("/tmp/shared") / shared_temp.name
vm_intermediate = vm_shared_temp / host_src.name
self.succeed(make_command(["mkdir", "-p", vm_shared_temp]))
if host_src.is_dir():
shutil.copytree(host_src, host_intermediate)
else:
shutil.copy(host_src, host_intermediate)
self.succeed("sync")
self.succeed(make_command(["mkdir", "-p", vm_target.parent]))
self.succeed(make_command(["cp", "-r", vm_intermediate, vm_target]))
# Make sure the cleanup is synced into VM
self.succeed("sync")
def copy_from_vm(self, source: str, target_dir: str = "") -> None: def copy_from_vm(self, source: str, target_dir: str = "") -> None:
"""Copy a file from the VM (specified by an in-VM source path) to a path """Copy a file from the VM (specified by an in-VM source path) to a path
relative to `$out`. The file is copied via the `shared_dir` shared among relative to `$out`. The file is copied via the `shared_dir` shared among

View File

@ -308,6 +308,7 @@ in
xss-lock = handleTest ./xss-lock.nix {}; xss-lock = handleTest ./xss-lock.nix {};
yabar = handleTest ./yabar.nix {}; yabar = handleTest ./yabar.nix {};
yggdrasil = handleTest ./yggdrasil.nix {}; yggdrasil = handleTest ./yggdrasil.nix {};
zfs = handleTest ./zfs.nix {};
zsh-history = handleTest ./zsh-history.nix {}; zsh-history = handleTest ./zsh-history.nix {};
zookeeper = handleTest ./zookeeper.nix {}; zookeeper = handleTest ./zookeeper.nix {};
} }

View File

@ -3,7 +3,7 @@
pkgs ? import ../.. { inherit system config; } pkgs ? import ../.. { inherit system config; }
}: }:
with import ../lib/testing.nix { inherit system pkgs; }; with import ../lib/testing-python.nix { inherit system pkgs; };
with pkgs.lib; with pkgs.lib;
let let
@ -67,161 +67,193 @@ let
, grubIdentifier, preBootCommands, extraConfig , grubIdentifier, preBootCommands, extraConfig
, testCloneConfig , testCloneConfig
}: }:
let let iface = if grubVersion == 1 then "ide" else "virtio";
iface = if grubVersion == 1 then "ide" else "virtio";
isEfi = bootLoader == "systemd-boot" || (bootLoader == "grub" && grubUseEfi); isEfi = bootLoader == "systemd-boot" || (bootLoader == "grub" && grubUseEfi);
bios = if pkgs.stdenv.isAarch64 then "QEMU_EFI.fd" else "OVMF.fd";
# FIXME don't duplicate the -enable-kvm etc. flags here yet again!
qemuFlags =
(if system == "x86_64-linux" then "-m 768 " else "-m 512 ") +
(optionalString (system == "x86_64-linux") "-cpu host ") +
(optionalString (system == "aarch64-linux") "-enable-kvm -machine virt,gic-version=host -cpu host ");
hdFlags = ''hda => "vm-state-machine/machine.qcow2", hdaInterface => "${iface}", ''
+ optionalString isEfi (if pkgs.stdenv.isAarch64
then ''bios => "${pkgs.OVMF.fd}/FV/QEMU_EFI.fd", ''
else ''bios => "${pkgs.OVMF.fd}/FV/OVMF.fd", '');
in if !isEfi && !(pkgs.stdenv.isi686 || pkgs.stdenv.isx86_64) then in if !isEfi && !(pkgs.stdenv.isi686 || pkgs.stdenv.isx86_64) then
throw "Non-EFI boot methods are only supported on i686 / x86_64" throw "Non-EFI boot methods are only supported on i686 / x86_64"
else '' else ''
def assemble_qemu_flags():
flags = "-cpu host"
${if system == "x86_64-linux"
then ''flags += " -m 768"''
else ''flags += " -m 512 -enable-kvm -machine virt,gic-version=host"''
}
return flags
$machine->start;
# Make sure that we get a login prompt etc. qemu_flags = {"qemuFlags": assemble_qemu_flags()}
$machine->succeed("echo hello");
#$machine->waitForUnit('getty@tty2');
#$machine->waitForUnit("rogue");
$machine->waitForUnit("nixos-manual");
# Wait for hard disks to appear in /dev hd_flags = {
$machine->succeed("udevadm settle"); "hdaInterface": "${iface}",
"hda": "vm-state-machine/machine.qcow2",
}
${optionalString isEfi ''
hd_flags.update(
bios="${pkgs.OVMF.fd}/FV/${bios}"
)''
}
default_flags = {**hd_flags, **qemu_flags}
def create_machine_named(name):
return create_machine({**default_flags, "name": "boot-after-install"})
machine.start()
with subtest("Assert readiness of login prompt"):
machine.succeed("echo hello")
machine.wait_for_unit("nixos-manual")
with subtest("Wait for hard disks to appear in /dev"):
machine.succeed("udevadm settle")
# Partition the disk.
${createPartitions} ${createPartitions}
# Create the NixOS configuration. with subtest("Create the NixOS configuration"):
$machine->succeed("nixos-generate-config --root /mnt"); machine.succeed("nixos-generate-config --root /mnt")
machine.succeed("cat /mnt/etc/nixos/hardware-configuration.nix >&2")
machine.copy_from_host(
"${ makeConfig {
inherit bootLoader grubVersion grubDevice grubIdentifier
grubUseEfi extraConfig;
}
}",
"/mnt/etc/nixos/configuration.nix",
)
$machine->succeed("cat /mnt/etc/nixos/hardware-configuration.nix >&2"); with subtest("Perform the installation"):
machine.succeed("nixos-install < /dev/null >&2")
$machine->copyFileFromHost( with subtest("Do it again to make sure it's idempotent"):
"${ makeConfig { inherit bootLoader grubVersion grubDevice grubIdentifier grubUseEfi extraConfig; } }", machine.succeed("nixos-install < /dev/null >&2")
"/mnt/etc/nixos/configuration.nix");
# Perform the installation. with subtest("Shutdown system after installation"):
$machine->succeed("nixos-install < /dev/null >&2"); machine.succeed("umount /mnt/boot || true")
machine.succeed("umount /mnt")
# Do it again to make sure it's idempotent. machine.succeed("sync")
$machine->succeed("nixos-install < /dev/null >&2"); machine.shutdown()
$machine->succeed("umount /mnt/boot || true");
$machine->succeed("umount /mnt");
$machine->succeed("sync");
$machine->shutdown;
# Now see if we can boot the installation. # Now see if we can boot the installation.
$machine = createMachine({ ${hdFlags} qemuFlags => "${qemuFlags}", name => "boot-after-install" }); machine = create_machine_named("boot-after-install")
# For example to enter LUKS passphrase. # For example to enter LUKS passphrase.
${preBootCommands} ${preBootCommands}
# Did /boot get mounted? with subtest("Assert that /boot get mounted"):
$machine->waitForUnit("local-fs.target"); machine.wait_for_unit("local-fs.target")
${if bootLoader == "grub"
${if bootLoader == "grub" then then ''machine.succeed("test -e /boot/grub")''
''$machine->succeed("test -e /boot/grub");'' else ''machine.succeed("test -e /boot/loader/loader.conf")''
else
''$machine->succeed("test -e /boot/loader/loader.conf");''
} }
# Check whether /root has correct permissions. with subtest("Check whether /root has correct permissions"):
$machine->succeed("stat -c '%a' /root") =~ /700/ or die; assert "700" in machine.succeed("stat -c '%a' /root")
# Did the swap device get activated? with subtest("Assert swap device got activated"):
# uncomment once https://bugs.freedesktop.org/show_bug.cgi?id=86930 is resolved # uncomment once https://bugs.freedesktop.org/show_bug.cgi?id=86930 is resolved
$machine->waitForUnit("swap.target"); machine.wait_for_unit("swap.target")
$machine->succeed("cat /proc/swaps | grep -q /dev"); machine.succeed("cat /proc/swaps | grep -q /dev")
# Check that the store is in good shape with subtest("Check that the store is in good shape"):
$machine->succeed("nix-store --verify --check-contents >&2"); machine.succeed("nix-store --verify --check-contents >&2")
# Check whether the channel works. with subtest("Check whether the channel works"):
$machine->succeed("nix-env -iA nixos.procps >&2"); machine.succeed("nix-env -iA nixos.procps >&2")
$machine->succeed("type -tP ps | tee /dev/stderr") =~ /.nix-profile/ assert ".nix-profile" in machine.succeed("type -tP ps | tee /dev/stderr")
or die "nix-env failed";
# Check that the daemon works, and that non-root users can run builds (this will build a new profile generation through the daemon) with subtest(
$machine->succeed("su alice -l -c 'nix-env -iA nixos.procps' >&2"); "Check that the daemon works, and that non-root users can run builds "
"(this will build a new profile generation through the daemon)"
):
machine.succeed("su alice -l -c 'nix-env -iA nixos.procps' >&2")
# We need a writable Nix store on next boot. with subtest("Configure system with writable Nix store on next boot"):
$machine->copyFileFromHost( # we're not using copy_from_host here because the installer image
"${ makeConfig { inherit bootLoader grubVersion grubDevice grubIdentifier grubUseEfi extraConfig; forceGrubReinstallCount = 1; } }", # doesn't know about the host-guest sharing mechanism.
"/etc/nixos/configuration.nix"); machine.copy_from_host_via_shell(
"${ makeConfig {
inherit bootLoader grubVersion grubDevice grubIdentifier
grubUseEfi extraConfig;
forceGrubReinstallCount = 1;
}
}",
"/etc/nixos/configuration.nix",
)
# Check whether nixos-rebuild works. with subtest("Check whether nixos-rebuild works"):
$machine->succeed("nixos-rebuild switch >&2"); machine.succeed("nixos-rebuild switch >&2")
# Test nixos-option. with subtest("Test nixos-option"):
$machine->succeed("nixos-option boot.initrd.kernelModules | grep virtio_console"); kernel_modules = machine.succeed("nixos-option boot.initrd.kernelModules")
$machine->succeed("nixos-option boot.initrd.kernelModules | grep 'List of modules'"); assert "virtio_console" in kernel_modules
$machine->succeed("nixos-option boot.initrd.kernelModules | grep qemu-guest.nix"); assert "List of modules" in kernel_modules
assert "qemu-guest.nix" in kernel_modules
$machine->shutdown; machine.shutdown()
# Check whether a writable store build works # Check whether a writable store build works
$machine = createMachine({ ${hdFlags} qemuFlags => "${qemuFlags}", name => "rebuild-switch" }); machine = create_machine_named("rebuild-switch")
${preBootCommands} ${preBootCommands}
$machine->waitForUnit("multi-user.target"); machine.wait_for_unit("multi-user.target")
$machine->copyFileFromHost(
"${ makeConfig { inherit bootLoader grubVersion grubDevice grubIdentifier grubUseEfi extraConfig; forceGrubReinstallCount = 2; } }", # we're not using copy_from_host here because the installer image
"/etc/nixos/configuration.nix"); # doesn't know about the host-guest sharing mechanism.
$machine->succeed("nixos-rebuild boot >&2"); machine.copy_from_host_via_shell(
$machine->shutdown; "${ makeConfig {
inherit bootLoader grubVersion grubDevice grubIdentifier
grubUseEfi extraConfig;
forceGrubReinstallCount = 2;
}
}",
"/etc/nixos/configuration.nix",
)
machine.succeed("nixos-rebuild boot >&2")
machine.shutdown()
# And just to be sure, check that the machine still boots after # And just to be sure, check that the machine still boots after
# "nixos-rebuild switch". # "nixos-rebuild switch".
$machine = createMachine({ ${hdFlags} qemuFlags => "${qemuFlags}", "boot-after-rebuild-switch" }); machine = create_machine_named("boot-after-rebuild-switch")
${preBootCommands} ${preBootCommands}
$machine->waitForUnit("network.target"); machine.wait_for_unit("network.target")
$machine->shutdown; machine.shutdown()
# Tests for validating clone configuration entries in grub menu # Tests for validating clone configuration entries in grub menu
${optionalString testCloneConfig '' ''
+ optionalString testCloneConfig ''
# Reboot Machine # Reboot Machine
$machine = createMachine({ ${hdFlags} qemuFlags => "${qemuFlags}", name => "clone-default-config" }); machine = create_machine_named("clone-default-config")
${preBootCommands} ${preBootCommands}
$machine->waitForUnit("multi-user.target"); machine.wait_for_unit("multi-user.target")
# Booted configuration name should be Home with subtest("Booted configuration name should be 'Home'"):
# This is not the name that shows in the grub menu. # This is not the name that shows in the grub menu.
# The default configuration is always shown as "Default" # The default configuration is always shown as "Default"
$machine->succeed("cat /run/booted-system/configuration-name >&2"); machine.succeed("cat /run/booted-system/configuration-name >&2")
$machine->succeed("cat /run/booted-system/configuration-name | grep Home"); assert "Home" in machine.succeed("cat /run/booted-system/configuration-name")
# We should find **not** a file named /etc/gitconfig with subtest("We should **not** find a file named /etc/gitconfig"):
$machine->fail("test -e /etc/gitconfig"); machine.fail("test -e /etc/gitconfig")
# Set grub to boot the second configuration with subtest("Set grub to boot the second configuration"):
$machine->succeed("grub-reboot 1"); machine.succeed("grub-reboot 1")
$machine->shutdown; machine.shutdown()
# Reboot Machine # Reboot Machine
$machine = createMachine({ ${hdFlags} qemuFlags => "${qemuFlags}", name => "clone-alternate-config" }); machine = create_machine_named("clone-alternate-config")
${preBootCommands} ${preBootCommands}
$machine->waitForUnit("multi-user.target"); machine.wait_for_unit("multi-user.target")
# Booted configuration name should be Work with subtest("Booted configuration name should be Work"):
$machine->succeed("cat /run/booted-system/configuration-name >&2"); machine.succeed("cat /run/booted-system/configuration-name >&2")
$machine->succeed("cat /run/booted-system/configuration-name | grep Work"); assert "Work" in machine.succeed("cat /run/booted-system/configuration-name")
# We should find a file named /etc/gitconfig with subtest("We should find a file named /etc/gitconfig"):
$machine->succeed("test -e /etc/gitconfig"); machine.succeed("test -e /etc/gitconfig")
$machine->shutdown;
''}
machine.shutdown()
''; '';
@ -243,11 +275,9 @@ let
nodes = { nodes = {
# The configuration of the machine used to run "nixos-install". # The configuration of the machine used to run "nixos-install".
machine = machine = { pkgs, ... }: {
{ pkgs, ... }: imports = [
../modules/profiles/installation-device.nix
{ imports =
[ ../modules/profiles/installation-device.nix
../modules/profiles/base.nix ../modules/profiles/base.nix
extraInstallerConfig extraInstallerConfig
]; ];
@ -270,20 +300,20 @@ let
# The test cannot access the network, so any packages we # The test cannot access the network, so any packages we
# need must be included in the VM. # need must be included in the VM.
system.extraDependencies = with pkgs; system.extraDependencies = with pkgs; [
[ sudo
libxml2.bin
libxslt.bin
desktop-file-utils desktop-file-utils
docbook5 docbook5
docbook_xsl_ns docbook_xsl_ns
unionfs-fuse libxml2.bin
ntp libxslt.bin
nixos-artwork.wallpapers.simple-dark-gray-bottom nixos-artwork.wallpapers.simple-dark-gray-bottom
perlPackages.XMLLibXML ntp
perlPackages.ListCompare perlPackages.ListCompare
perlPackages.XMLLibXML
shared-mime-info shared-mime-info
sudo
texinfo texinfo
unionfs-fuse
xorg.lndir xorg.lndir
# add curl so that rather than seeing the test attempt to download # add curl so that rather than seeing the test attempt to download
@ -291,11 +321,13 @@ let
curl curl
] ]
++ optional (bootLoader == "grub" && grubVersion == 1) pkgs.grub ++ optional (bootLoader == "grub" && grubVersion == 1) pkgs.grub
++ optionals (bootLoader == "grub" && grubVersion == 2) [ pkgs.grub2 pkgs.grub2_efi ]; ++ optionals (bootLoader == "grub" && grubVersion == 2) [
pkgs.grub2
pkgs.grub2_efi
];
nix.binaryCaches = mkForce [ ]; nix.binaryCaches = mkForce [ ];
nix.extraOptions = nix.extraOptions = ''
''
hashed-mirrors = hashed-mirrors =
connect-timeout = 1 connect-timeout = 1
''; '';
@ -310,13 +342,13 @@ let
}; };
}; };
makeLuksRootTest = name: luksFormatOpts: makeInstallerTest name makeLuksRootTest = name: luksFormatOpts: makeInstallerTest name {
{ createPartitions = '' createPartitions = ''
$machine->succeed( machine.succeed(
"flock /dev/vda parted --script /dev/vda -- mklabel msdos" "flock /dev/vda parted --script /dev/vda -- mklabel msdos"
. " mkpart primary ext2 1M 50MB" # /boot + " mkpart primary ext2 1M 50MB" # /boot
. " mkpart primary linux-swap 50M 1024M" + " mkpart primary linux-swap 50M 1024M"
. " mkpart primary 1024M -1s", # LUKS + " mkpart primary 1024M -1s", # LUKS
"udevadm settle", "udevadm settle",
"mkswap /dev/vda2 -L swap", "mkswap /dev/vda2 -L swap",
"swapon -L swap", "swapon -L swap",
@ -328,45 +360,44 @@ let
"mkfs.ext3 -L boot /dev/vda1", "mkfs.ext3 -L boot /dev/vda1",
"mkdir -p /mnt/boot", "mkdir -p /mnt/boot",
"mount LABEL=boot /mnt/boot", "mount LABEL=boot /mnt/boot",
); )
''; '';
extraConfig = '' extraConfig = ''
boot.kernelParams = lib.mkAfter [ "console=tty0" ]; boot.kernelParams = lib.mkAfter [ "console=tty0" ];
''; '';
enableOCR = true; enableOCR = true;
preBootCommands = '' preBootCommands = ''
$machine->start; machine.start()
$machine->waitForText(qr/Passphrase for/); machine.wait_for_text("Passphrase for")
$machine->sendChars("supersecret\n"); machine.send_chars("supersecret\n")
''; '';
}; };
# The (almost) simplest partitioning scheme: a swap partition and # The (almost) simplest partitioning scheme: a swap partition and
# one big filesystem partition. # one big filesystem partition.
simple-test-config = { createPartitions = simple-test-config = {
'' createPartitions = ''
$machine->succeed( machine.succeed(
"flock /dev/vda parted --script /dev/vda -- mklabel msdos" "flock /dev/vda parted --script /dev/vda -- mklabel msdos"
. " mkpart primary linux-swap 1M 1024M" + " mkpart primary linux-swap 1M 1024M"
. " mkpart primary ext2 1024M -1s", + " mkpart primary ext2 1024M -1s",
"udevadm settle", "udevadm settle",
"mkswap /dev/vda1 -L swap", "mkswap /dev/vda1 -L swap",
"swapon -L swap", "swapon -L swap",
"mkfs.ext3 -L nixos /dev/vda2", "mkfs.ext3 -L nixos /dev/vda2",
"mount LABEL=nixos /mnt", "mount LABEL=nixos /mnt",
); )
''; '';
}; };
simple-uefi-grub-config = simple-uefi-grub-config = {
{ createPartitions = createPartitions = ''
'' machine.succeed(
$machine->succeed(
"flock /dev/vda parted --script /dev/vda -- mklabel gpt" "flock /dev/vda parted --script /dev/vda -- mklabel gpt"
. " mkpart ESP fat32 1M 50MiB" # /boot + " mkpart ESP fat32 1M 50MiB" # /boot
. " set 1 boot on" + " set 1 boot on"
. " mkpart primary linux-swap 50MiB 1024MiB" + " mkpart primary linux-swap 50MiB 1024MiB"
. " mkpart primary ext2 1024MiB -1MiB", # / + " mkpart primary ext2 1024MiB -1MiB", # /
"udevadm settle", "udevadm settle",
"mkswap /dev/vda2 -L swap", "mkswap /dev/vda2 -L swap",
"swapon -L swap", "swapon -L swap",
@ -375,18 +406,17 @@ let
"mkfs.vfat -n BOOT /dev/vda1", "mkfs.vfat -n BOOT /dev/vda1",
"mkdir -p /mnt/boot", "mkdir -p /mnt/boot",
"mount LABEL=BOOT /mnt/boot", "mount LABEL=BOOT /mnt/boot",
); )
''; '';
bootLoader = "grub"; bootLoader = "grub";
grubUseEfi = true; grubUseEfi = true;
}; };
clone-test-extraconfig = { extraConfig = clone-test-extraconfig = {
'' extraConfig = ''
environment.systemPackages = [ pkgs.grub2 ]; environment.systemPackages = [ pkgs.grub2 ];
boot.loader.grub.configurationName = "Home"; boot.loader.grub.configurationName = "Home";
nesting.clone = [ nesting.clone = [ {
{
boot.loader.grub.configurationName = lib.mkForce "Work"; boot.loader.grub.configurationName = lib.mkForce "Work";
environment.etc = { environment.etc = {
@ -395,8 +425,7 @@ let
gitproxy = none for work.com gitproxy = none for work.com
"; ";
}; };
} } ];
];
''; '';
testCloneConfig = true; testCloneConfig = true;
}; };
@ -415,15 +444,14 @@ in {
simpleClone = makeInstallerTest "simpleClone" (simple-test-config // clone-test-extraconfig); simpleClone = makeInstallerTest "simpleClone" (simple-test-config // clone-test-extraconfig);
# Simple GPT/UEFI configuration using systemd-boot with 3 partitions: ESP, swap & root filesystem # Simple GPT/UEFI configuration using systemd-boot with 3 partitions: ESP, swap & root filesystem
simpleUefiSystemdBoot = makeInstallerTest "simpleUefiSystemdBoot" simpleUefiSystemdBoot = makeInstallerTest "simpleUefiSystemdBoot" {
{ createPartitions = createPartitions = ''
'' machine.succeed(
$machine->succeed(
"flock /dev/vda parted --script /dev/vda -- mklabel gpt" "flock /dev/vda parted --script /dev/vda -- mklabel gpt"
. " mkpart ESP fat32 1M 50MiB" # /boot + " mkpart ESP fat32 1M 50MiB" # /boot
. " set 1 boot on" + " set 1 boot on"
. " mkpart primary linux-swap 50MiB 1024MiB" + " mkpart primary linux-swap 50MiB 1024MiB"
. " mkpart primary ext2 1024MiB -1MiB", # / + " mkpart primary ext2 1024MiB -1MiB", # /
"udevadm settle", "udevadm settle",
"mkswap /dev/vda2 -L swap", "mkswap /dev/vda2 -L swap",
"swapon -L swap", "swapon -L swap",
@ -432,7 +460,7 @@ in {
"mkfs.vfat -n BOOT /dev/vda1", "mkfs.vfat -n BOOT /dev/vda1",
"mkdir -p /mnt/boot", "mkdir -p /mnt/boot",
"mount LABEL=BOOT /mnt/boot", "mount LABEL=BOOT /mnt/boot",
); )
''; '';
bootLoader = "systemd-boot"; bootLoader = "systemd-boot";
}; };
@ -443,14 +471,13 @@ in {
simpleUefiGrubClone = makeInstallerTest "simpleUefiGrubClone" (simple-uefi-grub-config // clone-test-extraconfig); simpleUefiGrubClone = makeInstallerTest "simpleUefiGrubClone" (simple-uefi-grub-config // clone-test-extraconfig);
# Same as the previous, but now with a separate /boot partition. # Same as the previous, but now with a separate /boot partition.
separateBoot = makeInstallerTest "separateBoot" separateBoot = makeInstallerTest "separateBoot" {
{ createPartitions = createPartitions = ''
'' machine.succeed(
$machine->succeed(
"flock /dev/vda parted --script /dev/vda -- mklabel msdos" "flock /dev/vda parted --script /dev/vda -- mklabel msdos"
. " mkpart primary ext2 1M 50MB" # /boot + " mkpart primary ext2 1M 50MB" # /boot
. " mkpart primary linux-swap 50MB 1024M" + " mkpart primary linux-swap 50MB 1024M"
. " mkpart primary ext2 1024M -1s", # / + " mkpart primary ext2 1024M -1s", # /
"udevadm settle", "udevadm settle",
"mkswap /dev/vda2 -L swap", "mkswap /dev/vda2 -L swap",
"swapon -L swap", "swapon -L swap",
@ -459,19 +486,18 @@ in {
"mkfs.ext3 -L boot /dev/vda1", "mkfs.ext3 -L boot /dev/vda1",
"mkdir -p /mnt/boot", "mkdir -p /mnt/boot",
"mount LABEL=boot /mnt/boot", "mount LABEL=boot /mnt/boot",
); )
''; '';
}; };
# Same as the previous, but with fat32 /boot. # Same as the previous, but with fat32 /boot.
separateBootFat = makeInstallerTest "separateBootFat" separateBootFat = makeInstallerTest "separateBootFat" {
{ createPartitions = createPartitions = ''
'' machine.succeed(
$machine->succeed(
"flock /dev/vda parted --script /dev/vda -- mklabel msdos" "flock /dev/vda parted --script /dev/vda -- mklabel msdos"
. " mkpart primary ext2 1M 50MB" # /boot + " mkpart primary ext2 1M 50MB" # /boot
. " mkpart primary linux-swap 50MB 1024M" + " mkpart primary linux-swap 50MB 1024M"
. " mkpart primary ext2 1024M -1s", # / + " mkpart primary ext2 1024M -1s", # /
"udevadm settle", "udevadm settle",
"mkswap /dev/vda2 -L swap", "mkswap /dev/vda2 -L swap",
"swapon -L swap", "swapon -L swap",
@ -480,13 +506,12 @@ in {
"mkfs.vfat -n BOOT /dev/vda1", "mkfs.vfat -n BOOT /dev/vda1",
"mkdir -p /mnt/boot", "mkdir -p /mnt/boot",
"mount LABEL=BOOT /mnt/boot", "mount LABEL=BOOT /mnt/boot",
); )
''; '';
}; };
# zfs on / with swap # zfs on / with swap
zfsroot = makeInstallerTest "zfs-root" zfsroot = makeInstallerTest "zfs-root" {
{
extraInstallerConfig = { extraInstallerConfig = {
boot.supportedFilesystems = [ "zfs" ]; boot.supportedFilesystems = [ "zfs" ];
}; };
@ -501,37 +526,32 @@ in {
networking.hostId = "00000000"; networking.hostId = "00000000";
''; '';
createPartitions = createPartitions = ''
'' machine.succeed(
$machine->succeed(
"flock /dev/vda parted --script /dev/vda -- mklabel msdos" "flock /dev/vda parted --script /dev/vda -- mklabel msdos"
. " mkpart primary linux-swap 1M 1024M" + " mkpart primary linux-swap 1M 1024M"
. " mkpart primary 1024M -1s", + " mkpart primary 1024M -1s",
"udevadm settle", "udevadm settle",
"mkswap /dev/vda1 -L swap", "mkswap /dev/vda1 -L swap",
"swapon -L swap", "swapon -L swap",
"zpool create rpool /dev/vda2", "zpool create rpool /dev/vda2",
"zfs create -o mountpoint=legacy rpool/root", "zfs create -o mountpoint=legacy rpool/root",
"mount -t zfs rpool/root /mnt", "mount -t zfs rpool/root /mnt",
"udevadm settle",
"udevadm settle" )
);
''; '';
}; };
# Create two physical LVM partitions combined into one volume group # Create two physical LVM partitions combined into one volume group
# that contains the logical swap and root partitions. # that contains the logical swap and root partitions.
lvm = makeInstallerTest "lvm" lvm = makeInstallerTest "lvm" {
{ createPartitions = createPartitions = ''
'' machine.succeed(
$machine->succeed(
"flock /dev/vda parted --script /dev/vda -- mklabel msdos" "flock /dev/vda parted --script /dev/vda -- mklabel msdos"
. " mkpart primary 1M 2048M" # PV1 + " mkpart primary 1M 2048M" # PV1
. " set 1 lvm on" + " set 1 lvm on"
. " mkpart primary 2048M -1s" # PV2 + " mkpart primary 2048M -1s" # PV2
. " set 2 lvm on", + " set 2 lvm on",
"udevadm settle", "udevadm settle",
"pvcreate /dev/vda1 /dev/vda2", "pvcreate /dev/vda1 /dev/vda2",
"vgcreate MyVolGroup /dev/vda1 /dev/vda2", "vgcreate MyVolGroup /dev/vda1 /dev/vda2",
@ -541,7 +561,7 @@ in {
"swapon -L swap", "swapon -L swap",
"mkfs.xfs -L nixos /dev/MyVolGroup/nixos", "mkfs.xfs -L nixos /dev/MyVolGroup/nixos",
"mount LABEL=nixos /mnt", "mount LABEL=nixos /mnt",
); )
''; '';
}; };
@ -557,14 +577,14 @@ in {
# Test whether opening encrypted filesystem with keyfile # Test whether opening encrypted filesystem with keyfile
# Checks for regression of missing cryptsetup, when no luks device without # Checks for regression of missing cryptsetup, when no luks device without
# keyfile is configured # keyfile is configured
encryptedFSWithKeyfile = makeInstallerTest "encryptedFSWithKeyfile" encryptedFSWithKeyfile = makeInstallerTest "encryptedFSWithKeyfile" {
{ createPartitions = '' createPartitions = ''
$machine->succeed( machine.succeed(
"flock /dev/vda parted --script /dev/vda -- mklabel msdos" "flock /dev/vda parted --script /dev/vda -- mklabel msdos"
. " mkpart primary ext2 1M 50MB" # /boot + " mkpart primary ext2 1M 50MB" # /boot
. " mkpart primary linux-swap 50M 1024M" + " mkpart primary linux-swap 50M 1024M"
. " mkpart primary 1024M 1280M" # LUKS with keyfile + " mkpart primary 1024M 1280M" # LUKS with keyfile
. " mkpart primary 1280M -1s", + " mkpart primary 1280M -1s",
"udevadm settle", "udevadm settle",
"mkswap /dev/vda2 -L swap", "mkswap /dev/vda2 -L swap",
"swapon -L swap", "swapon -L swap",
@ -579,12 +599,12 @@ in {
"cryptsetup luksOpen --key-file /mnt/keyfile /dev/vda3 crypt", "cryptsetup luksOpen --key-file /mnt/keyfile /dev/vda3 crypt",
"mkfs.ext3 -L test /dev/mapper/crypt", "mkfs.ext3 -L test /dev/mapper/crypt",
"cryptsetup luksClose crypt", "cryptsetup luksClose crypt",
"mkdir -p /mnt/test" "mkdir -p /mnt/test",
); )
''; '';
extraConfig = '' extraConfig = ''
fileSystems."/test" = fileSystems."/test" = {
{ device = "/dev/disk/by-label/test"; device = "/dev/disk/by-label/test";
fsType = "ext3"; fsType = "ext3";
encrypted.enable = true; encrypted.enable = true;
encrypted.blkDev = "/dev/vda3"; encrypted.blkDev = "/dev/vda3";
@ -594,25 +614,25 @@ in {
''; '';
}; };
swraid = makeInstallerTest "swraid" {
swraid = makeInstallerTest "swraid" createPartitions = ''
{ createPartitions = machine.succeed(
''
$machine->succeed(
"flock /dev/vda parted --script /dev/vda --" "flock /dev/vda parted --script /dev/vda --"
. " mklabel msdos" + " mklabel msdos"
. " mkpart primary ext2 1M 100MB" # /boot + " mkpart primary ext2 1M 100MB" # /boot
. " mkpart extended 100M -1s" + " mkpart extended 100M -1s"
. " mkpart logical 102M 2102M" # md0 (root), first device + " mkpart logical 102M 2102M" # md0 (root), first device
. " mkpart logical 2103M 4103M" # md0 (root), second device + " mkpart logical 2103M 4103M" # md0 (root), second device
. " mkpart logical 4104M 4360M" # md1 (swap), first device + " mkpart logical 4104M 4360M" # md1 (swap), first device
. " mkpart logical 4361M 4617M", # md1 (swap), second device + " mkpart logical 4361M 4617M", # md1 (swap), second device
"udevadm settle", "udevadm settle",
"ls -l /dev/vda* >&2", "ls -l /dev/vda* >&2",
"cat /proc/partitions >&2", "cat /proc/partitions >&2",
"udevadm control --stop-exec-queue", "udevadm control --stop-exec-queue",
"mdadm --create --force /dev/md0 --metadata 1.2 --level=raid1 --raid-devices=2 /dev/vda5 /dev/vda6", "mdadm --create --force /dev/md0 --metadata 1.2 --level=raid1 "
"mdadm --create --force /dev/md1 --metadata 1.2 --level=raid1 --raid-devices=2 /dev/vda7 /dev/vda8", + "--raid-devices=2 /dev/vda5 /dev/vda6",
"mdadm --create --force /dev/md1 --metadata 1.2 --level=raid1 "
+ "--raid-devices=2 /dev/vda7 /dev/vda8",
"udevadm control --start-exec-queue", "udevadm control --start-exec-queue",
"udevadm settle", "udevadm settle",
"mkswap -f /dev/md1 -L swap", "mkswap -f /dev/md1 -L swap",
@ -623,29 +643,28 @@ in {
"mkdir /mnt/boot", "mkdir /mnt/boot",
"mount LABEL=boot /mnt/boot", "mount LABEL=boot /mnt/boot",
"udevadm settle", "udevadm settle",
); )
''; '';
preBootCommands = '' preBootCommands = ''
$machine->start; machine.start()
$machine->fail("dmesg | grep 'immediate safe mode'"); machine.fail("dmesg | grep 'immediate safe mode'")
''; '';
}; };
# Test a basic install using GRUB 1. # Test a basic install using GRUB 1.
grub1 = makeInstallerTest "grub1" grub1 = makeInstallerTest "grub1" {
{ createPartitions = createPartitions = ''
'' machine.succeed(
$machine->succeed(
"flock /dev/sda parted --script /dev/sda -- mklabel msdos" "flock /dev/sda parted --script /dev/sda -- mklabel msdos"
. " mkpart primary linux-swap 1M 1024M" + " mkpart primary linux-swap 1M 1024M"
. " mkpart primary ext2 1024M -1s", + " mkpart primary ext2 1024M -1s",
"udevadm settle", "udevadm settle",
"mkswap /dev/sda1 -L swap", "mkswap /dev/sda1 -L swap",
"swapon -L swap", "swapon -L swap",
"mkfs.ext3 -L nixos /dev/sda2", "mkfs.ext3 -L nixos /dev/sda2",
"mount LABEL=nixos /mnt", "mount LABEL=nixos /mnt",
"mkdir -p /mnt/tmp", "mkdir -p /mnt/tmp",
); )
''; '';
grubVersion = 1; grubVersion = 1;
grubDevice = "/dev/sda"; grubDevice = "/dev/sda";
@ -654,14 +673,14 @@ in {
# Test using labels to identify volumes in grub # Test using labels to identify volumes in grub
simpleLabels = makeInstallerTest "simpleLabels" { simpleLabels = makeInstallerTest "simpleLabels" {
createPartitions = '' createPartitions = ''
$machine->succeed( machine.succeed(
"sgdisk -Z /dev/vda", "sgdisk -Z /dev/vda",
"sgdisk -n 1:0:+1M -n 2:0:+1G -N 3 -t 1:ef02 -t 2:8200 -t 3:8300 -c 3:root /dev/vda", "sgdisk -n 1:0:+1M -n 2:0:+1G -N 3 -t 1:ef02 -t 2:8200 -t 3:8300 -c 3:root /dev/vda",
"mkswap /dev/vda2 -L swap", "mkswap /dev/vda2 -L swap",
"swapon -L swap", "swapon -L swap",
"mkfs.ext4 -L root /dev/vda3", "mkfs.ext4 -L root /dev/vda3",
"mount LABEL=root /mnt", "mount LABEL=root /mnt",
); )
''; '';
grubIdentifier = "label"; grubIdentifier = "label";
}; };
@ -670,22 +689,23 @@ in {
# TODO: Fix udev so the symlinks are unneeded in /dev/disks # TODO: Fix udev so the symlinks are unneeded in /dev/disks
simpleProvided = makeInstallerTest "simpleProvided" { simpleProvided = makeInstallerTest "simpleProvided" {
createPartitions = '' createPartitions = ''
my $UUID = "\$(blkid -s UUID -o value /dev/vda2)"; uuid = "$(blkid -s UUID -o value /dev/vda2)"
$machine->succeed( machine.succeed(
"sgdisk -Z /dev/vda", "sgdisk -Z /dev/vda",
"sgdisk -n 1:0:+1M -n 2:0:+100M -n 3:0:+1G -N 4 -t 1:ef02 -t 2:8300 -t 3:8200 -t 4:8300 -c 2:boot -c 4:root /dev/vda", "sgdisk -n 1:0:+1M -n 2:0:+100M -n 3:0:+1G -N 4 -t 1:ef02 -t 2:8300 "
+ "-t 3:8200 -t 4:8300 -c 2:boot -c 4:root /dev/vda",
"mkswap /dev/vda3 -L swap", "mkswap /dev/vda3 -L swap",
"swapon -L swap", "swapon -L swap",
"mkfs.ext4 -L boot /dev/vda2", "mkfs.ext4 -L boot /dev/vda2",
"mkfs.ext4 -L root /dev/vda4", "mkfs.ext4 -L root /dev/vda4",
); )
$machine->execute("ln -s ../../vda2 /dev/disk/by-uuid/$UUID"); machine.execute(f"ln -s ../../vda2 /dev/disk/by-uuid/{uuid}")
$machine->execute("ln -s ../../vda4 /dev/disk/by-label/root"); machine.execute("ln -s ../../vda4 /dev/disk/by-label/root")
$machine->succeed( machine.succeed(
"mount /dev/disk/by-label/root /mnt", "mount /dev/disk/by-label/root /mnt",
"mkdir /mnt/boot", "mkdir /mnt/boot",
"mount /dev/disk/by-uuid/$UUID /mnt/boot" f"mount /dev/disk/by-uuid/{uuid} /mnt/boot",
); )
''; '';
grubIdentifier = "provided"; grubIdentifier = "provided";
}; };
@ -693,21 +713,21 @@ in {
# Simple btrfs grub testing # Simple btrfs grub testing
btrfsSimple = makeInstallerTest "btrfsSimple" { btrfsSimple = makeInstallerTest "btrfsSimple" {
createPartitions = '' createPartitions = ''
$machine->succeed( machine.succeed(
"sgdisk -Z /dev/vda", "sgdisk -Z /dev/vda",
"sgdisk -n 1:0:+1M -n 2:0:+1G -N 3 -t 1:ef02 -t 2:8200 -t 3:8300 -c 3:root /dev/vda", "sgdisk -n 1:0:+1M -n 2:0:+1G -N 3 -t 1:ef02 -t 2:8200 -t 3:8300 -c 3:root /dev/vda",
"mkswap /dev/vda2 -L swap", "mkswap /dev/vda2 -L swap",
"swapon -L swap", "swapon -L swap",
"mkfs.btrfs -L root /dev/vda3", "mkfs.btrfs -L root /dev/vda3",
"mount LABEL=root /mnt", "mount LABEL=root /mnt",
); )
''; '';
}; };
# Test to see if we can detect /boot and /nix on subvolumes # Test to see if we can detect /boot and /nix on subvolumes
btrfsSubvols = makeInstallerTest "btrfsSubvols" { btrfsSubvols = makeInstallerTest "btrfsSubvols" {
createPartitions = '' createPartitions = ''
$machine->succeed( machine.succeed(
"sgdisk -Z /dev/vda", "sgdisk -Z /dev/vda",
"sgdisk -n 1:0:+1M -n 2:0:+1G -N 3 -t 1:ef02 -t 2:8200 -t 3:8300 -c 3:root /dev/vda", "sgdisk -n 1:0:+1M -n 2:0:+1G -N 3 -t 1:ef02 -t 2:8200 -t 3:8300 -c 3:root /dev/vda",
"mkswap /dev/vda2 -L swap", "mkswap /dev/vda2 -L swap",
@ -722,14 +742,14 @@ in {
"mount -o defaults,subvol=nixos/default LABEL=root /mnt", "mount -o defaults,subvol=nixos/default LABEL=root /mnt",
"mkdir /mnt/boot", "mkdir /mnt/boot",
"mount -o defaults,subvol=boot LABEL=root /mnt/boot", "mount -o defaults,subvol=boot LABEL=root /mnt/boot",
); )
''; '';
}; };
# Test to see if we can detect default and aux subvolumes correctly # Test to see if we can detect default and aux subvolumes correctly
btrfsSubvolDefault = makeInstallerTest "btrfsSubvolDefault" { btrfsSubvolDefault = makeInstallerTest "btrfsSubvolDefault" {
createPartitions = '' createPartitions = ''
$machine->succeed( machine.succeed(
"sgdisk -Z /dev/vda", "sgdisk -Z /dev/vda",
"sgdisk -n 1:0:+1M -n 2:0:+1G -N 3 -t 1:ef02 -t 2:8200 -t 3:8300 -c 3:root /dev/vda", "sgdisk -n 1:0:+1M -n 2:0:+1G -N 3 -t 1:ef02 -t 2:8200 -t 3:8300 -c 3:root /dev/vda",
"mkswap /dev/vda2 -L swap", "mkswap /dev/vda2 -L swap",
@ -740,14 +760,15 @@ in {
"btrfs subvol create /mnt/badpath", "btrfs subvol create /mnt/badpath",
"btrfs subvol create /mnt/badpath/boot", "btrfs subvol create /mnt/badpath/boot",
"btrfs subvol create /mnt/nixos", "btrfs subvol create /mnt/nixos",
"btrfs subvol set-default \$(btrfs subvol list /mnt | grep 'nixos' | awk '{print \$2}') /mnt", "btrfs subvol set-default "
+ "$(btrfs subvol list /mnt | grep 'nixos' | awk '{print \$2}') /mnt",
"umount /mnt", "umount /mnt",
"mount -o defaults LABEL=root /mnt", "mount -o defaults LABEL=root /mnt",
"mkdir -p /mnt/badpath/boot", # Help ensure the detection mechanism is actually looking up subvolumes "mkdir -p /mnt/badpath/boot", # Help ensure the detection mechanism
# is actually looking up subvolumes
"mkdir /mnt/boot", "mkdir /mnt/boot",
"mount -o defaults,subvol=badpath/boot LABEL=root /mnt/boot", "mount -o defaults,subvol=badpath/boot LABEL=root /mnt/boot",
); )
''; '';
}; };
} }

View File

@ -3,12 +3,10 @@
pkgs ? import ../.. { inherit system config; } pkgs ? import ../.. { inherit system config; }
}: }:
with import ../lib/testing.nix { inherit system pkgs; }; with import ../lib/testing-python.nix { inherit system pkgs; };
let let
makeTest = import ./make-test-python.nix;
makeZfsTest = name: makeZfsTest = name:
{ kernelPackage ? pkgs.linuxPackages_latest { kernelPackage ? pkgs.linuxPackages_latest
, enableUnstable ? false , enableUnstable ? false
@ -20,41 +18,33 @@ let
maintainers = [ adisbladis ]; maintainers = [ adisbladis ];
}; };
machine = { pkgs, ... }: machine = { pkgs, ... }: {
{
virtualisation.emptyDiskImages = [ 4096 ]; virtualisation.emptyDiskImages = [ 4096 ];
networking.hostId = "deadbeef"; networking.hostId = "deadbeef";
boot.kernelPackages = kernelPackage; boot.kernelPackages = kernelPackage;
boot.supportedFilesystems = [ "zfs" ]; boot.supportedFilesystems = [ "zfs" ];
boot.zfs.enableUnstable = enableUnstable; boot.zfs.enableUnstable = enableUnstable;
environment.systemPackages = with pkgs; [ environment.systemPackages = [ pkgs.parted ];
parted
];
}; };
testScript = '' testScript = ''
machine.succeed("modprobe zfs")
machine.succeed("zpool status")
machine.succeed("ls /dev")
machine.succeed( machine.succeed(
"modprobe zfs",
"zpool status",
"ls /dev",
"mkdir /tmp/mnt", "mkdir /tmp/mnt",
"udevadm settle", "udevadm settle",
"parted --script /dev/vdb mklabel msdos", "parted --script /dev/vdb mklabel msdos",
"parted --script /dev/vdb -- mkpart primary 1024M -1s", "parted --script /dev/vdb -- mkpart primary 1024M -1s",
"udevadm settle", "udevadm settle",
"zpool create rpool /dev/vdb1", "zpool create rpool /dev/vdb1",
"zfs create -o mountpoint=legacy rpool/root", "zfs create -o mountpoint=legacy rpool/root",
"mount -t zfs rpool/root /tmp/mnt", "mount -t zfs rpool/root /tmp/mnt",
"udevadm settle", "udevadm settle",
"umount /tmp/mnt", "umount /tmp/mnt",
"zpool destroy rpool", "zpool destroy rpool",
"udevadm settle" "udevadm settle",
) )
'' + extraTest; '' + extraTest;
@ -69,18 +59,17 @@ in {
enableUnstable = true; enableUnstable = true;
extraTest = '' extraTest = ''
machine.succeed( machine.succeed(
"echo password | zpool create -o altroot=\"/tmp/mnt\" -O encryption=aes-256-gcm -O keyformat=passphrase rpool /dev/vdb1", 'echo password | zpool create -o altroot="/tmp/mnt" '
+ "-O encryption=aes-256-gcm -O keyformat=passphrase rpool /dev/vdb1",
"zfs create -o mountpoint=legacy rpool/root", "zfs create -o mountpoint=legacy rpool/root",
"mount -t zfs rpool/root /tmp/mnt", "mount -t zfs rpool/root /tmp/mnt",
"udevadm settle", "udevadm settle",
"umount /tmp/mnt", "umount /tmp/mnt",
"zpool destroy rpool", "zpool destroy rpool",
"udevadm settle" "udevadm settle",
) )
''; '';
}; };
installer = (import ./installer.nix { }).zfsroot; installer = (import ./installer.nix { }).zfsroot;
} }