summaryrefslogtreecommitdiffstats
path: root/nixos/tests/libvirtd.nix
blob: f5af32af01945971add4241f91aa1f15a44c43ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
{ pkgs, ... }:
{
  name = "libvirtd";
  meta.maintainers = with pkgs.lib.maintainers; [ fpletz ];

  nodes = {
    virthost =
      { pkgs, ... }:
      {
        virtualisation = {
          cores = 2;
          memorySize = 2048;

          libvirtd.enable = true;
          libvirtd.hooks.qemu.is_working = "${pkgs.writeShellScript "testHook.sh" ''
            touch /tmp/qemu_hook_is_working
          ''}";
          libvirtd.nss.enable = true;
          libvirtd.dbus.enable = true;
        };
        boot.supportedFilesystems = [ "zfs" ];
        networking.hostId = "deadbeef"; # needed for zfs
        security.polkit.enable = true;
        environment.systemPackages = with pkgs; [ virt-manager ];

        # This adds `resolve` to the `hosts` line of /etc/nsswitch.conf; NSS modules placed after it
        # will not be consulted. Therefore this tests that the libvirtd NSS modules will be
        # be placed early enough for name resolution to work.
        services.resolved.enable = true;
      };
  };

  testScript =
    let
      nixosInstallISO = (import ../release.nix { }).iso_minimal.${pkgs.stdenv.hostPlatform.system};
      virshShutdownCmd = if pkgs.stdenv.hostPlatform.isx86_64 then "shutdown" else "destroy";
    in
    ''
      start_all()

      virthost.wait_for_unit("multi-user.target")

      with subtest("enable default network"):
        virthost.succeed("virsh net-start default")
        virthost.succeed("virsh net-autostart default")
        virthost.succeed("virsh net-info default")

      with subtest("check if partition disk pools works with parted"):
        virthost.succeed("fallocate -l100m /tmp/foo; losetup /dev/loop0 /tmp/foo; echo 'label: dos' | sfdisk /dev/loop0")
        virthost.succeed("virsh pool-create-as foo disk --source-dev /dev/loop0 --target /dev")
        virthost.succeed("virsh vol-create-as foo loop0p1 25MB")
        virthost.succeed("virsh vol-create-as foo loop0p2 50MB")

      with subtest("check if virsh zfs pools work"):
        virthost.succeed("fallocate -l100m /tmp/zfs; losetup /dev/loop1 /tmp/zfs;")
        virthost.succeed("zpool create zfs_loop /dev/loop1")
        virthost.succeed("virsh pool-define-as --name zfs_storagepool --source-name zfs_loop --type zfs")
        virthost.succeed("virsh pool-start zfs_storagepool")
        virthost.succeed("virsh vol-create-as zfs_storagepool disk1 25MB")

      with subtest("check if nixos install iso boots, network and autostart works"):
        # The guest runs sshd on port 22. We use passt-based networking to
        # forward guest port 22 to host port 2222 and `nc -z` to assert that
        # the host port can only be connected to once the guest is running.
        virthost.fail("nc -z localhost 2222")
        virthost.succeed(
          "virt-install -n nixos --osinfo nixos-unstable --memory 1024 --graphics none --network default --network passt,portForward=2222:22 --disk `find ${nixosInstallISO}/iso -type f | head -n1`,readonly=on --import --noautoconsole --autostart"
        )
        virthost.succeed("virsh domstate nixos | grep running")
        virthost.wait_until_succeeds("ping -c 1 nixos")
        virthost.succeed("nc -z localhost 2222")
        virthost.succeed("virsh ${virshShutdownCmd} nixos")
        virthost.wait_until_succeeds("virsh domstate nixos | grep 'shut off'")
        virthost.shutdown()
        virthost.wait_for_unit("multi-user.target")
        virthost.wait_until_succeeds("ping -c 1 nixos")
        virthost.succeed("nc -z localhost 2222")

      with subtest("test if hooks are linked and run"):
        virthost.succeed("ls /var/lib/libvirt/hooks/qemu.d/is_working")
        virthost.succeed("ls /tmp/qemu_hook_is_working")

      with subtest("check libvirt over dbus"):
        # The service is activated on-demand and shouldn't be active yet
        virthost.fail("systemctl is-active libvirt-dbus.service")

        # Get the hostname property of the Test connection (this will activate the service)
        virthost.succeed("busctl get-property org.libvirt /org/libvirt/Test org.libvirt.Connect Hostname")

        # Now the service should be active
        virthost.succeed("systemctl is-active libvirt-dbus.service")
    '';
}