1import ../make-test-python.nix ({ pkgs, lib, ... } :
2
3let
4 releases = import ../../release.nix {
5 configuration = {
6 # Building documentation makes the test unnecessarily take a longer time:
7 documentation.enable = lib.mkForce false;
8 };
9 };
10
11 container-image-metadata = releases.lxdContainerMeta.${pkgs.stdenv.hostPlatform.system};
12 container-image-rootfs = releases.lxdContainerImage.${pkgs.stdenv.hostPlatform.system};
13in
14{
15 name = "incus-container";
16
17 meta.maintainers = with lib.maintainers; [ adamcstephens ];
18
19 nodes.machine = { ... }: {
20 virtualisation = {
21 # Ensure test VM has enough resources for creating and managing guests
22 cores = 2;
23 memorySize = 1024;
24 diskSize = 4096;
25
26 incus.enable = true;
27 };
28 };
29
30 testScript = ''
31 def instance_is_up(_) -> bool:
32 status, _ = machine.execute("incus exec container --disable-stdin --force-interactive /run/current-system/sw/bin/true")
33 return status == 0
34
35 def set_container(config):
36 machine.succeed(f"incus config set container {config}")
37 machine.succeed("incus restart container")
38 with machine.nested("Waiting for instance to start and be usable"):
39 retry(instance_is_up)
40
41 machine.wait_for_unit("incus.service")
42
43 # no preseed should mean no service
44 machine.fail("systemctl status incus-preseed.service")
45
46 machine.succeed("incus admin init --minimal")
47
48 with subtest("Container image can be imported"):
49 machine.succeed("incus image import ${container-image-metadata}/*/*.tar.xz ${container-image-rootfs}/*/*.tar.xz --alias nixos")
50
51 with subtest("Container can be launched and managed"):
52 machine.succeed("incus launch nixos container")
53 with machine.nested("Waiting for instance to start and be usable"):
54 retry(instance_is_up)
55 machine.succeed("echo true | incus exec container /run/current-system/sw/bin/bash -")
56
57 with subtest("Container CPU limits can be managed"):
58 set_container("limits.cpu 1")
59 cpuinfo = machine.succeed("incus exec container grep -- -c ^processor /proc/cpuinfo").strip()
60 assert cpuinfo == "1", f"Wrong number of CPUs reported from /proc/cpuinfo, want: 1, got: {cpuinfo}"
61
62 set_container("limits.cpu 2")
63 cpuinfo = machine.succeed("incus exec container grep -- -c ^processor /proc/cpuinfo").strip()
64 assert cpuinfo == "2", f"Wrong number of CPUs reported from /proc/cpuinfo, want: 2, got: {cpuinfo}"
65
66 with subtest("Container memory limits can be managed"):
67 set_container("limits.memory 64MB")
68 meminfo = machine.succeed("incus exec container grep -- MemTotal /proc/meminfo").strip()
69 meminfo_bytes = " ".join(meminfo.split(' ')[-2:])
70 assert meminfo_bytes == "62500 kB", f"Wrong amount of memory reported from /proc/meminfo, want: '62500 kB', got: '{meminfo_bytes}'"
71
72 set_container("limits.memory 128MB")
73 meminfo = machine.succeed("incus exec container grep -- MemTotal /proc/meminfo").strip()
74 meminfo_bytes = " ".join(meminfo.split(' ')[-2:])
75 assert meminfo_bytes == "125000 kB", f"Wrong amount of memory reported from /proc/meminfo, want: '125000 kB', got: '{meminfo_bytes}'"
76 '';
77})