Commit 22b6f785 authored by Will Fancher's avatar Will Fancher
Browse files

nixos/tests/zfs: Represent real world usage better

It's better to utilize the boot process and systemd mechanisms to test
these zfs features, rather than manually simulating the same behavior
with testScript.
parent a45968c1
Loading
Loading
Loading
Loading
+91 −63
Original line number Diff line number Diff line
@@ -17,19 +17,32 @@ let
    makeTest {
      name = "zfs-" + name;
      meta = with pkgs.lib.maintainers; {
        maintainers = [ adisbladis ];
        maintainers = [ adisbladis elvishjerricco ];
      };

      nodes.machine = { pkgs, lib, ... }:
        let
          usersharePath = "/var/lib/samba/usershares";
        in {
        virtualisation.emptyDiskImages = [ 4096 ];
        virtualisation = {
          emptyDiskImages = [ 4096 ];
          useBootLoader = true;
          useEFIBoot = true;
        };
        boot.loader.systemd-boot.enable = true;
        boot.loader.timeout = 0;
        boot.loader.efi.canTouchEfiVariables = true;
        networking.hostId = "deadbeef";
        boot.kernelPackages = kernelPackage;
        boot.supportedFilesystems = [ "zfs" ];
        boot.zfs.enableUnstable = enableUnstable;

        environment.systemPackages = [ pkgs.parted ];

        # /dev/disk/by-id doesn't get populated in the NixOS test framework
        boot.zfs.devNodes = "/dev/disk/by-uuid";

        specialisation.samba.configuration = {
          services.samba = {
            enable = true;
            extraConfig = ''
@@ -42,78 +55,93 @@ let
          };
          systemd.services.samba-smbd.serviceConfig.ExecStartPre =
            "${pkgs.coreutils}/bin/mkdir -m +t -p ${usersharePath}";

        environment.systemPackages = [ pkgs.parted ];

        # Setup regular fileSystems machinery to ensure forceImportAll can be
        # tested via the regular service units.
          virtualisation.fileSystems = {
          "/forcepool" = {
            device = "forcepool";
            "/tmp/mnt" = {
              device = "rpool/root";
              fsType = "zfs";
            options = [ "noauto" ];
            };
          };
        };

        # forcepool doesn't exist at first boot, and we need to manually test
        # the import after tweaking the hostId.
        systemd.services.zfs-import-forcepool.wantedBy = lib.mkVMOverride [];
        specialisation.encryption.configuration = {
          virtualisation.fileSystems."/automatic" = {
            device = "automatic";
            fsType = "zfs";
          };
        };

        specialisation.forcepool.configuration = {
          systemd.services.zfs-import-forcepool.wantedBy = lib.mkVMOverride [ "forcepool.mount" ];
          systemd.targets.zfs.wantedBy = lib.mkVMOverride [];
          boot.zfs.forceImportAll = true;
        # /dev/disk/by-id doesn't get populated in the NixOS test framework
        boot.zfs.devNodes = "/dev/disk/by-uuid";
          virtualisation.fileSystems."/forcepool" = {
            device = "forcepool";
            fsType = "zfs";
            options = [ "noauto" ];
          };
        };
      };

      testScript = ''
        machine.wait_for_unit("multi-user.target")
        machine.succeed(
            "modprobe zfs",
            "zpool status",
            "ls /dev",
            "mkdir /tmp/mnt",
            "udevadm settle",
            "parted --script /dev/vdb mklabel msdos",
            "parted --script /dev/vdb -- mkpart primary 1024M -1s",
            "udevadm settle",
            "zpool create rpool /dev/vdb1",
            "parted --script /dev/vdc mklabel msdos",
            "parted --script /dev/vdc -- mkpart primary 1024M -1s",
        )

        with subtest("sharesmb works"):
            machine.succeed(
                "zpool create rpool /dev/vdc1",
                "zfs create -o mountpoint=legacy rpool/root",
                # shared datasets cannot have legacy mountpoint
                "zfs create rpool/shared_smb",
            "mount -t zfs rpool/root /tmp/mnt",
            "udevadm settle",
            # wait for samba services
            "systemctl is-system-running --wait",
                "bootctl set-default nixos-generation-1-specialisation-samba.conf",
                "sync",
            )
            machine.crash()
            machine.wait_for_unit("multi-user.target")
            machine.succeed(
                "zfs set sharesmb=on rpool/shared_smb",
                "zfs share rpool/shared_smb",
                "smbclient -gNL localhost | grep rpool_shared_smb",
                "umount /tmp/mnt",
                "zpool destroy rpool",
            "udevadm settle",
            )

        with subtest("encryption works"):
            machine.succeed(
            '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",
            "mount -t zfs rpool/root /tmp/mnt",
            "udevadm settle",
            "umount /tmp/mnt",
            "zpool destroy rpool",
            "udevadm settle",
                'echo password | zpool create -O mountpoint=legacy '
                + "-O encryption=aes-256-gcm -O keyformat=passphrase automatic /dev/vdc1",
                "bootctl set-default nixos-generation-1-specialisation-encryption.conf",
                "sync",
                "zpool export automatic",
            )
            machine.crash()
            machine.start()
            machine.wait_for_console_text("Starting password query on")
            machine.send_console("password\n")
            machine.wait_for_unit("multi-user.target")
            machine.succeed(
                "umount /automatic",
                "zpool destroy automatic",
            )

        with subtest("boot.zfs.forceImportAll works"):
            machine.succeed(
                "rm /etc/hostid",
                "zgenhostid deadcafe",
                "zpool create forcepool /dev/vdb1 -O mountpoint=legacy",
                "zpool create forcepool /dev/vdc1 -O mountpoint=legacy",
                "bootctl set-default nixos-generation-1-specialisation-forcepool.conf",
                "rm /etc/hostid",
                "sync",
            )
            machine.shutdown()
            machine.start()
            machine.succeed("udevadm settle")
            machine.crash()
            machine.wait_for_unit("multi-user.target")
            machine.fail("zpool import forcepool")
            machine.succeed(
                "systemctl start zfs-import-forcepool.service",
                "mount -t zfs forcepool /tmp/mnt",
                "systemctl start forcepool.mount",
                "mount | grep forcepool",
            )
      '' + extraTest;