2023-10-09 19:29:22 +00:00
|
|
|
import ../make-test-python.nix ({ pkgs, lib, ... } :
|
2020-06-18 07:06:33 +00:00
|
|
|
|
|
|
|
let
|
2023-10-09 19:29:22 +00:00
|
|
|
releases = import ../../release.nix {
|
2022-05-18 14:49:53 +00:00
|
|
|
configuration = {
|
|
|
|
# Building documentation makes the test unnecessarily take a longer time:
|
|
|
|
documentation.enable = lib.mkForce false;
|
2020-06-18 07:06:33 +00:00
|
|
|
|
2022-05-18 14:49:53 +00:00
|
|
|
# Our tests require `grep` & friends:
|
|
|
|
environment.systemPackages = with pkgs; [ busybox ];
|
2022-04-27 09:35:20 +00:00
|
|
|
};
|
2022-05-18 14:49:53 +00:00
|
|
|
};
|
2020-06-18 07:06:33 +00:00
|
|
|
|
2023-10-09 19:29:22 +00:00
|
|
|
lxd-image-metadata = releases.lxdContainerMeta.${pkgs.stdenv.hostPlatform.system};
|
|
|
|
lxd-image-rootfs = releases.lxdContainerImage.${pkgs.stdenv.hostPlatform.system};
|
2023-11-16 04:20:00 +00:00
|
|
|
lxd-image-rootfs-squashfs = releases.lxdContainerImageSquashfs.${pkgs.stdenv.hostPlatform.system};
|
2020-09-25 04:45:31 +00:00
|
|
|
|
2020-06-18 07:06:33 +00:00
|
|
|
in {
|
2023-10-09 19:29:22 +00:00
|
|
|
name = "lxd-container";
|
2020-09-25 04:45:31 +00:00
|
|
|
|
2024-01-02 11:29:13 +00:00
|
|
|
meta = {
|
|
|
|
maintainers = lib.teams.lxc.members;
|
2020-06-18 07:06:33 +00:00
|
|
|
};
|
|
|
|
|
2022-04-03 18:54:34 +00:00
|
|
|
nodes.machine = { lib, ... }: {
|
2020-06-18 07:06:33 +00:00
|
|
|
virtualisation = {
|
2023-11-16 04:20:00 +00:00
|
|
|
diskSize = 6144;
|
2022-05-18 14:49:53 +00:00
|
|
|
|
2020-06-18 07:06:33 +00:00
|
|
|
# Since we're testing `limits.cpu`, we've gotta have a known number of
|
2020-09-25 04:45:31 +00:00
|
|
|
# cores to lean on
|
2020-06-18 07:06:33 +00:00
|
|
|
cores = 2;
|
|
|
|
|
|
|
|
# Ditto, for `limits.memory`
|
|
|
|
memorySize = 512;
|
|
|
|
|
|
|
|
lxc.lxcfs.enable = true;
|
|
|
|
lxd.enable = true;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
testScript = ''
|
2023-10-09 19:29:22 +00:00
|
|
|
def instance_is_up(_) -> bool:
|
|
|
|
status, _ = machine.execute("lxc exec container --disable-stdin --force-interactive /run/current-system/sw/bin/true")
|
|
|
|
return status == 0
|
|
|
|
|
2020-06-18 07:06:33 +00:00
|
|
|
machine.wait_for_unit("sockets.target")
|
|
|
|
machine.wait_for_unit("lxd.service")
|
2020-09-25 04:45:31 +00:00
|
|
|
machine.wait_for_file("/var/lib/lxd/unix.socket")
|
2020-06-18 07:06:33 +00:00
|
|
|
|
2023-10-09 19:29:22 +00:00
|
|
|
# Wait for lxd to settle
|
|
|
|
machine.succeed("lxd waitready")
|
2020-06-18 07:06:33 +00:00
|
|
|
|
2023-10-09 19:29:22 +00:00
|
|
|
# no preseed should mean no service
|
|
|
|
machine.fail("systemctl status lxd-preseed.service")
|
2020-06-18 07:06:33 +00:00
|
|
|
|
2023-10-09 19:29:22 +00:00
|
|
|
machine.succeed("lxd init --minimal")
|
2020-06-18 07:06:33 +00:00
|
|
|
|
|
|
|
machine.succeed(
|
2022-05-18 14:49:53 +00:00
|
|
|
"lxc image import ${lxd-image-metadata}/*/*.tar.xz ${lxd-image-rootfs}/*/*.tar.xz --alias nixos"
|
2020-06-18 07:06:33 +00:00
|
|
|
)
|
|
|
|
|
2022-05-18 14:49:53 +00:00
|
|
|
with subtest("Container can be managed"):
|
|
|
|
machine.succeed("lxc launch nixos container")
|
2023-10-09 19:29:22 +00:00
|
|
|
with machine.nested("Waiting for instance to start and be usable"):
|
|
|
|
retry(instance_is_up)
|
2022-05-18 14:49:53 +00:00
|
|
|
machine.succeed("echo true | lxc exec container /run/current-system/sw/bin/bash -")
|
2023-11-16 04:20:00 +00:00
|
|
|
machine.succeed("lxc delete -f container")
|
|
|
|
|
|
|
|
with subtest("Squashfs image is functional"):
|
|
|
|
machine.succeed(
|
|
|
|
"lxc image import ${lxd-image-metadata}/*/*.tar.xz ${lxd-image-rootfs-squashfs} --alias nixos-squashfs"
|
|
|
|
)
|
|
|
|
machine.succeed("lxc launch nixos-squashfs container")
|
|
|
|
with machine.nested("Waiting for instance to start and be usable"):
|
|
|
|
retry(instance_is_up)
|
|
|
|
machine.succeed("echo true | lxc exec container /run/current-system/sw/bin/bash -")
|
2022-05-18 14:49:53 +00:00
|
|
|
machine.succeed("lxc delete -f container")
|
2020-06-18 07:06:33 +00:00
|
|
|
|
2022-05-18 14:49:53 +00:00
|
|
|
with subtest("Container is mounted with lxcfs inside"):
|
|
|
|
machine.succeed("lxc launch nixos container")
|
2023-10-09 19:29:22 +00:00
|
|
|
with machine.nested("Waiting for instance to start and be usable"):
|
|
|
|
retry(instance_is_up)
|
2020-06-18 07:06:33 +00:00
|
|
|
|
|
|
|
## ---------- ##
|
|
|
|
## limits.cpu ##
|
|
|
|
|
2022-05-18 14:49:53 +00:00
|
|
|
machine.succeed("lxc config set container limits.cpu 1")
|
|
|
|
machine.succeed("lxc restart container")
|
2023-10-09 19:29:22 +00:00
|
|
|
with machine.nested("Waiting for instance to start and be usable"):
|
|
|
|
retry(instance_is_up)
|
2020-06-18 07:06:33 +00:00
|
|
|
|
|
|
|
assert (
|
|
|
|
"1"
|
2022-05-18 14:49:53 +00:00
|
|
|
== machine.succeed("lxc exec container grep -- -c ^processor /proc/cpuinfo").strip()
|
2020-06-18 07:06:33 +00:00
|
|
|
)
|
|
|
|
|
2022-05-18 14:49:53 +00:00
|
|
|
machine.succeed("lxc config set container limits.cpu 2")
|
|
|
|
machine.succeed("lxc restart container")
|
2023-10-09 19:29:22 +00:00
|
|
|
with machine.nested("Waiting for instance to start and be usable"):
|
|
|
|
retry(instance_is_up)
|
2020-06-18 07:06:33 +00:00
|
|
|
|
|
|
|
assert (
|
|
|
|
"2"
|
2022-05-18 14:49:53 +00:00
|
|
|
== machine.succeed("lxc exec container grep -- -c ^processor /proc/cpuinfo").strip()
|
2020-06-18 07:06:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
## ------------- ##
|
|
|
|
## limits.memory ##
|
|
|
|
|
2022-05-18 14:49:53 +00:00
|
|
|
machine.succeed("lxc config set container limits.memory 64MB")
|
|
|
|
machine.succeed("lxc restart container")
|
2023-10-09 19:29:22 +00:00
|
|
|
with machine.nested("Waiting for instance to start and be usable"):
|
|
|
|
retry(instance_is_up)
|
2020-06-18 07:06:33 +00:00
|
|
|
|
|
|
|
assert (
|
|
|
|
"MemTotal: 62500 kB"
|
2022-05-18 14:49:53 +00:00
|
|
|
== machine.succeed("lxc exec container grep -- MemTotal /proc/meminfo").strip()
|
2020-06-18 07:06:33 +00:00
|
|
|
)
|
|
|
|
|
2022-05-18 14:49:53 +00:00
|
|
|
machine.succeed("lxc config set container limits.memory 128MB")
|
|
|
|
machine.succeed("lxc restart container")
|
2023-10-09 19:29:22 +00:00
|
|
|
with machine.nested("Waiting for instance to start and be usable"):
|
|
|
|
retry(instance_is_up)
|
2020-06-18 07:06:33 +00:00
|
|
|
|
|
|
|
assert (
|
|
|
|
"MemTotal: 125000 kB"
|
2022-05-18 14:49:53 +00:00
|
|
|
== machine.succeed("lxc exec container grep -- MemTotal /proc/meminfo").strip()
|
2020-06-18 07:06:33 +00:00
|
|
|
)
|
|
|
|
|
2022-05-18 14:49:53 +00:00
|
|
|
machine.succeed("lxc delete -f container")
|
2020-06-18 07:06:33 +00:00
|
|
|
'';
|
|
|
|
})
|