2022-05-03 11:24:13 +01:00
|
|
|
import ./make-test-python.nix ({ pkgs, lib, ... } :
|
2020-06-08 20:33:21 +01:00
|
|
|
|
|
|
|
let
|
2022-05-03 11:24:13 +01:00
|
|
|
lxd-image = import ../release.nix {
|
|
|
|
configuration = {
|
|
|
|
# Building documentation makes the test unnecessarily take a longer time:
|
|
|
|
documentation.enable = lib.mkForce false;
|
2020-06-08 20:33:21 +01:00
|
|
|
|
2022-05-03 11:24:13 +01:00
|
|
|
# Our tests require `grep` & friends:
|
|
|
|
environment.systemPackages = with pkgs; [ busybox ];
|
2022-04-26 11:12:01 +01:00
|
|
|
};
|
2022-05-03 11:24:13 +01:00
|
|
|
};
|
2020-06-08 20:33:21 +01:00
|
|
|
|
2022-05-03 11:24:13 +01:00
|
|
|
lxd-image-metadata = lxd-image.lxdMeta.${pkgs.system};
|
|
|
|
lxd-image-rootfs = lxd-image.lxdImage.${pkgs.system};
|
2020-09-09 16:25:27 +01:00
|
|
|
|
2020-06-08 20:33:21 +01:00
|
|
|
in {
|
|
|
|
name = "lxd";
|
2020-09-09 16:25:27 +01:00
|
|
|
|
2021-01-10 19:08:30 +00:00
|
|
|
meta = with pkgs.lib.maintainers; {
|
2020-06-08 20:33:21 +01:00
|
|
|
maintainers = [ patryk27 ];
|
|
|
|
};
|
|
|
|
|
2022-03-20 23:15:30 +00:00
|
|
|
nodes.machine = { lib, ... }: {
|
2020-06-08 20:33:21 +01:00
|
|
|
virtualisation = {
|
2022-05-03 11:24:13 +01:00
|
|
|
diskSize = 2048;
|
|
|
|
|
2020-06-08 20:33:21 +01:00
|
|
|
# Since we're testing `limits.cpu`, we've gotta have a known number of
|
2020-09-09 16:25:27 +01:00
|
|
|
# cores to lean on
|
2020-06-08 20:33:21 +01:00
|
|
|
cores = 2;
|
|
|
|
|
|
|
|
# Ditto, for `limits.memory`
|
|
|
|
memorySize = 512;
|
|
|
|
|
|
|
|
lxc.lxcfs.enable = true;
|
|
|
|
lxd.enable = true;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
testScript = ''
|
|
|
|
machine.wait_for_unit("sockets.target")
|
|
|
|
machine.wait_for_unit("lxd.service")
|
2020-09-09 18:46:21 +01:00
|
|
|
machine.wait_for_file("/var/lib/lxd/unix.socket")
|
2020-06-08 20:33:21 +01:00
|
|
|
|
|
|
|
# It takes additional second for lxd to settle
|
|
|
|
machine.sleep(1)
|
|
|
|
|
|
|
|
# lxd expects the pool's directory to already exist
|
|
|
|
machine.succeed("mkdir /var/lxd-pool")
|
|
|
|
|
|
|
|
machine.succeed(
|
2022-05-03 11:24:13 +01:00
|
|
|
"cat ${./common/lxd/config.yaml} | lxd init --preseed"
|
2020-06-08 20:33:21 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
machine.succeed(
|
2022-05-03 11:24:13 +01:00
|
|
|
"lxc image import ${lxd-image-metadata}/*/*.tar.xz ${lxd-image-rootfs}/*/*.tar.xz --alias nixos"
|
2020-06-08 20:33:21 +01:00
|
|
|
)
|
|
|
|
|
2022-05-03 11:24:13 +01:00
|
|
|
with subtest("Container can be managed"):
|
|
|
|
machine.succeed("lxc launch nixos container")
|
|
|
|
machine.sleep(5)
|
|
|
|
machine.succeed("echo true | lxc exec container /run/current-system/sw/bin/bash -")
|
|
|
|
machine.succeed("lxc exec container true")
|
|
|
|
machine.succeed("lxc delete -f container")
|
2020-06-08 20:33:21 +01:00
|
|
|
|
2022-05-03 11:24:13 +01:00
|
|
|
with subtest("Container is mounted with lxcfs inside"):
|
|
|
|
machine.succeed("lxc launch nixos container")
|
|
|
|
machine.sleep(5)
|
2020-06-08 20:33:21 +01:00
|
|
|
|
|
|
|
## ---------- ##
|
|
|
|
## limits.cpu ##
|
|
|
|
|
2022-05-03 11:24:13 +01:00
|
|
|
machine.succeed("lxc config set container limits.cpu 1")
|
|
|
|
machine.succeed("lxc restart container")
|
|
|
|
machine.sleep(5)
|
2020-06-08 20:33:21 +01:00
|
|
|
|
|
|
|
assert (
|
|
|
|
"1"
|
2022-05-03 11:24:13 +01:00
|
|
|
== machine.succeed("lxc exec container grep -- -c ^processor /proc/cpuinfo").strip()
|
2020-06-08 20:33:21 +01:00
|
|
|
)
|
|
|
|
|
2022-05-03 11:24:13 +01:00
|
|
|
machine.succeed("lxc config set container limits.cpu 2")
|
|
|
|
machine.succeed("lxc restart container")
|
|
|
|
machine.sleep(5)
|
2020-06-08 20:33:21 +01:00
|
|
|
|
|
|
|
assert (
|
|
|
|
"2"
|
2022-05-03 11:24:13 +01:00
|
|
|
== machine.succeed("lxc exec container grep -- -c ^processor /proc/cpuinfo").strip()
|
2020-06-08 20:33:21 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
## ------------- ##
|
|
|
|
## limits.memory ##
|
|
|
|
|
2022-05-03 11:24:13 +01:00
|
|
|
machine.succeed("lxc config set container limits.memory 64MB")
|
|
|
|
machine.succeed("lxc restart container")
|
|
|
|
machine.sleep(5)
|
2020-06-08 20:33:21 +01:00
|
|
|
|
|
|
|
assert (
|
|
|
|
"MemTotal: 62500 kB"
|
2022-05-03 11:24:13 +01:00
|
|
|
== machine.succeed("lxc exec container grep -- MemTotal /proc/meminfo").strip()
|
2020-06-08 20:33:21 +01:00
|
|
|
)
|
|
|
|
|
2022-05-03 11:24:13 +01:00
|
|
|
machine.succeed("lxc config set container limits.memory 128MB")
|
|
|
|
machine.succeed("lxc restart container")
|
|
|
|
machine.sleep(5)
|
2020-06-08 20:33:21 +01:00
|
|
|
|
|
|
|
assert (
|
|
|
|
"MemTotal: 125000 kB"
|
2022-05-03 11:24:13 +01:00
|
|
|
== machine.succeed("lxc exec container grep -- MemTotal /proc/meminfo").strip()
|
2020-06-08 20:33:21 +01:00
|
|
|
)
|
|
|
|
|
2022-05-03 11:24:13 +01:00
|
|
|
machine.succeed("lxc delete -f container")
|
2020-06-08 20:33:21 +01:00
|
|
|
'';
|
|
|
|
})
|