1import ./make-test-python.nix ({ pkgs, lib, ... } :
2
3let
4 lxd-image = 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 = lxd-image.lxdMeta.${pkgs.stdenv.hostPlatform.system};
15 lxd-image-rootfs = lxd-image.lxdImage.${pkgs.stdenv.hostPlatform.system};
16
17in {
18 name = "lxd";
19
20 meta = with pkgs.lib.maintainers; {
21 maintainers = [ patryk27 ];
22 };
23
24 nodes.machine = { lib, ... }: {
25 virtualisation = {
26 diskSize = 4096;
27
28 # Since we're testing `limits.cpu`, we've gotta have a known number of
29 # cores to lean on
30 cores = 2;
31
32 # Ditto, for `limits.memory`
33 memorySize = 512;
34
35 lxc.lxcfs.enable = true;
36 lxd.enable = true;
37 };
38 };
39
40 testScript = ''
41 machine.wait_for_unit("sockets.target")
42 machine.wait_for_unit("lxd.service")
43 machine.wait_for_file("/var/lib/lxd/unix.socket")
44
45 # It takes additional second for lxd to settle
46 machine.sleep(1)
47
48 # lxd expects the pool's directory to already exist
49 machine.succeed("mkdir /var/lxd-pool")
50
51 machine.succeed(
52 "cat ${./common/lxd/config.yaml} | lxd init --preseed"
53 )
54
55 machine.succeed(
56 "lxc image import ${lxd-image-metadata}/*/*.tar.xz ${lxd-image-rootfs}/*/*.tar.xz --alias nixos"
57 )
58
59 with subtest("Container can be managed"):
60 machine.succeed("lxc launch nixos container")
61 machine.sleep(5)
62 machine.succeed("echo true | lxc exec container /run/current-system/sw/bin/bash -")
63 machine.succeed("lxc exec container true")
64 machine.succeed("lxc delete -f container")
65
66 with subtest("Container is mounted with lxcfs inside"):
67 machine.succeed("lxc launch nixos container")
68 machine.sleep(5)
69
70 ## ---------- ##
71 ## limits.cpu ##
72
73 machine.succeed("lxc config set container limits.cpu 1")
74 machine.succeed("lxc restart container")
75 machine.sleep(5)
76
77 assert (
78 "1"
79 == machine.succeed("lxc exec container grep -- -c ^processor /proc/cpuinfo").strip()
80 )
81
82 machine.succeed("lxc config set container limits.cpu 2")
83 machine.succeed("lxc restart container")
84 machine.sleep(5)
85
86 assert (
87 "2"
88 == machine.succeed("lxc exec container grep -- -c ^processor /proc/cpuinfo").strip()
89 )
90
91 ## ------------- ##
92 ## limits.memory ##
93
94 machine.succeed("lxc config set container limits.memory 64MB")
95 machine.succeed("lxc restart container")
96 machine.sleep(5)
97
98 assert (
99 "MemTotal: 62500 kB"
100 == machine.succeed("lxc exec container grep -- MemTotal /proc/meminfo").strip()
101 )
102
103 machine.succeed("lxc config set container limits.memory 128MB")
104 machine.succeed("lxc restart container")
105 machine.sleep(5)
106
107 assert (
108 "MemTotal: 125000 kB"
109 == machine.succeed("lxc exec container grep -- MemTotal /proc/meminfo").strip()
110 )
111
112 machine.succeed("lxc delete -f container")
113 '';
114})