1 import ../make-test-python.nix ({ pkgs, lib, ... } :
4 releases = import ../../release.nix {
6 # Building documentation makes the test unnecessarily take a longer time:
7 documentation.enable = lib.mkForce false;
9 # Our tests require `grep` & friends:
10 environment.systemPackages = with pkgs; [ busybox ];
14 lxd-image-metadata = releases.lxdContainerMeta.${pkgs.stdenv.hostPlatform.system};
15 lxd-image-rootfs = releases.lxdContainerImage.${pkgs.stdenv.hostPlatform.system};
16 lxd-image-rootfs-squashfs = releases.lxdContainerImageSquashfs.${pkgs.stdenv.hostPlatform.system};
19 name = "lxd-container";
21 nodes.machine = { lib, ... }: {
25 # Since we're testing `limits.cpu`, we've gotta have a known number of
29 # Ditto, for `limits.memory`
32 lxc.lxcfs.enable = true;
38 def instance_is_up(_) -> bool:
39 status, _ = machine.execute("lxc exec container --disable-stdin --force-interactive /run/current-system/sw/bin/true")
42 machine.wait_for_unit("sockets.target")
43 machine.wait_for_unit("lxd.service")
44 machine.wait_for_file("/var/lib/lxd/unix.socket")
46 # Wait for lxd to settle
47 machine.succeed("lxd waitready")
49 # no preseed should mean no service
50 machine.fail("systemctl status lxd-preseed.service")
52 machine.succeed("lxd init --minimal")
55 "lxc image import ${lxd-image-metadata}/*/*.tar.xz ${lxd-image-rootfs}/*/*.tar.xz --alias nixos"
58 with subtest("Container can be managed"):
59 machine.succeed("lxc launch nixos container")
60 with machine.nested("Waiting for instance to start and be usable"):
62 machine.succeed("echo true | lxc exec container /run/current-system/sw/bin/bash -")
63 machine.succeed("lxc delete -f container")
65 with subtest("Squashfs image is functional"):
67 "lxc image import ${lxd-image-metadata}/*/*.tar.xz ${lxd-image-rootfs-squashfs}/nixos-lxc-image-${pkgs.stdenv.hostPlatform.system}.squashfs --alias nixos-squashfs"
69 machine.succeed("lxc launch nixos-squashfs container")
70 with machine.nested("Waiting for instance to start and be usable"):
72 machine.succeed("echo true | lxc exec container /run/current-system/sw/bin/bash -")
73 machine.succeed("lxc delete -f container")
75 with subtest("Container is mounted with lxcfs inside"):
76 machine.succeed("lxc launch nixos container")
77 with machine.nested("Waiting for instance to start and be usable"):
83 machine.succeed("lxc config set container limits.cpu 1")
84 machine.succeed("lxc restart container")
85 with machine.nested("Waiting for instance to start and be usable"):
90 == machine.succeed("lxc exec container grep -- -c ^processor /proc/cpuinfo").strip()
93 machine.succeed("lxc config set container limits.cpu 2")
94 machine.succeed("lxc restart container")
95 with machine.nested("Waiting for instance to start and be usable"):
100 == machine.succeed("lxc exec container grep -- -c ^processor /proc/cpuinfo").strip()
106 machine.succeed("lxc config set container limits.memory 64MB")
107 machine.succeed("lxc restart container")
108 with machine.nested("Waiting for instance to start and be usable"):
109 retry(instance_is_up)
113 == machine.succeed("lxc exec container grep -- MemTotal /proc/meminfo").strip()
116 machine.succeed("lxc config set container limits.memory 128MB")
117 machine.succeed("lxc restart container")
118 with machine.nested("Waiting for instance to start and be usable"):
119 retry(instance_is_up)
122 "MemTotal: 125000 kB"
123 == machine.succeed("lxc exec container grep -- MemTotal /proc/meminfo").strip()
126 machine.succeed("lxc delete -f container")