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 # Our tests require `grep` & friends:
10 environment.systemPackages = with pkgs; [ busybox ];
11 };
12 };
13
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};
17
18in {
19 name = "lxd-container";
20
21 meta = with pkgs.lib.maintainers; {
22 maintainers = [ patryk27 adamcstephens ];
23 };
24
25 nodes.machine = { lib, ... }: {
26 virtualisation = {
27 diskSize = 6144;
28
29 # Since we're testing `limits.cpu`, we've gotta have a known number of
30 # cores to lean on
31 cores = 2;
32
33 # Ditto, for `limits.memory`
34 memorySize = 512;
35
36 lxc.lxcfs.enable = true;
37 lxd.enable = true;
38 };
39 };
40
41 testScript = ''
42 def instance_is_up(_) -> bool:
43 status, _ = machine.execute("lxc exec container --disable-stdin --force-interactive /run/current-system/sw/bin/true")
44 return status == 0
45
46 machine.wait_for_unit("sockets.target")
47 machine.wait_for_unit("lxd.service")
48 machine.wait_for_file("/var/lib/lxd/unix.socket")
49
50 # Wait for lxd to settle
51 machine.succeed("lxd waitready")
52
53 # no preseed should mean no service
54 machine.fail("systemctl status lxd-preseed.service")
55
56 machine.succeed("lxd init --minimal")
57
58 machine.succeed(
59 "lxc image import ${lxd-image-metadata}/*/*.tar.xz ${lxd-image-rootfs}/*/*.tar.xz --alias nixos"
60 )
61
62 with subtest("Container can be managed"):
63 machine.succeed("lxc launch nixos container")
64 with machine.nested("Waiting for instance to start and be usable"):
65 retry(instance_is_up)
66 machine.succeed("echo true | lxc exec container /run/current-system/sw/bin/bash -")
67 machine.succeed("lxc delete -f container")
68
69 with subtest("Squashfs image is functional"):
70 machine.succeed(
71 "lxc image import ${lxd-image-metadata}/*/*.tar.xz ${lxd-image-rootfs-squashfs} --alias nixos-squashfs"
72 )
73 machine.succeed("lxc launch nixos-squashfs container")
74 with machine.nested("Waiting for instance to start and be usable"):
75 retry(instance_is_up)
76 machine.succeed("echo true | lxc exec container /run/current-system/sw/bin/bash -")
77 machine.succeed("lxc delete -f container")
78
79 with subtest("Container is mounted with lxcfs inside"):
80 machine.succeed("lxc launch nixos container")
81 with machine.nested("Waiting for instance to start and be usable"):
82 retry(instance_is_up)
83
84 ## ---------- ##
85 ## limits.cpu ##
86
87 machine.succeed("lxc config set container limits.cpu 1")
88 machine.succeed("lxc restart container")
89 with machine.nested("Waiting for instance to start and be usable"):
90 retry(instance_is_up)
91
92 assert (
93 "1"
94 == machine.succeed("lxc exec container grep -- -c ^processor /proc/cpuinfo").strip()
95 )
96
97 machine.succeed("lxc config set container limits.cpu 2")
98 machine.succeed("lxc restart container")
99 with machine.nested("Waiting for instance to start and be usable"):
100 retry(instance_is_up)
101
102 assert (
103 "2"
104 == machine.succeed("lxc exec container grep -- -c ^processor /proc/cpuinfo").strip()
105 )
106
107 ## ------------- ##
108 ## limits.memory ##
109
110 machine.succeed("lxc config set container limits.memory 64MB")
111 machine.succeed("lxc restart container")
112 with machine.nested("Waiting for instance to start and be usable"):
113 retry(instance_is_up)
114
115 assert (
116 "MemTotal: 62500 kB"
117 == machine.succeed("lxc exec container grep -- MemTotal /proc/meminfo").strip()
118 )
119
120 machine.succeed("lxc config set container limits.memory 128MB")
121 machine.succeed("lxc restart container")
122 with machine.nested("Waiting for instance to start and be usable"):
123 retry(instance_is_up)
124
125 assert (
126 "MemTotal: 125000 kB"
127 == machine.succeed("lxc exec container grep -- MemTotal /proc/meminfo").strip()
128 )
129
130 machine.succeed("lxc delete -f container")
131 '';
132})