From f1c0589e4c778cc5852a14c6776a7053177983c3 Mon Sep 17 00:00:00 2001 From: Adam Stephens Date: Thu, 10 Aug 2023 14:07:42 -0400 Subject: [PATCH] nixos/tests/lxd: move into subdir, use minimal init, remove sleeps --- nixos/tests/all-tests.nix | 4 +- nixos/tests/common/lxd/config.yaml | 24 ------------ nixos/tests/lxd-image-server.nix | 6 +-- nixos/tests/{lxd.nix => lxd/container.nix} | 38 ++++++++++--------- nixos/tests/lxd/default.nix | 9 +++++ .../{lxd-nftables.nix => lxd/nftables.nix} | 2 +- nixos/tests/{lxd-ui.nix => lxd/ui.nix} | 2 +- pkgs/tools/admin/lxd/default.nix | 2 - 8 files changed, 36 insertions(+), 51 deletions(-) delete mode 100644 nixos/tests/common/lxd/config.yaml rename nixos/tests/{lxd.nix => lxd/container.nix} (72%) create mode 100644 nixos/tests/lxd/default.nix rename nixos/tests/{lxd-nftables.nix => lxd/nftables.nix} (96%) rename nixos/tests/{lxd-ui.nix => lxd/ui.nix} (94%) diff --git a/nixos/tests/all-tests.nix b/nixos/tests/all-tests.nix index 530447b99786..227d3c0e1685 100644 --- a/nixos/tests/all-tests.nix +++ b/nixos/tests/all-tests.nix @@ -440,10 +440,8 @@ in { loki = handleTest ./loki.nix {}; luks = handleTest ./luks.nix {}; lvm2 = handleTest ./lvm2 {}; - lxd = handleTest ./lxd.nix {}; - lxd-nftables = handleTest ./lxd-nftables.nix {}; + lxd = handleTest ./lxd {}; lxd-image-server = handleTest ./lxd-image-server.nix {}; - lxd-ui = handleTest ./lxd-ui.nix {}; #logstash = handleTest ./logstash.nix {}; lorri = handleTest ./lorri/default.nix {}; maddy = discoverTests (import ./maddy { inherit handleTest; }); diff --git a/nixos/tests/common/lxd/config.yaml b/nixos/tests/common/lxd/config.yaml deleted file mode 100644 index 3bb667ed43f7..000000000000 --- a/nixos/tests/common/lxd/config.yaml +++ /dev/null @@ -1,24 +0,0 @@ -storage_pools: - - name: default - driver: dir - config: - source: /var/lxd-pool - -networks: - - name: lxdbr0 - type: bridge - config: - ipv4.address: auto - ipv6.address: none - -profiles: - - name: default - devices: - eth0: - name: eth0 - network: lxdbr0 - type: nic - root: - path: / - pool: default - type: disk diff --git a/nixos/tests/lxd-image-server.nix b/nixos/tests/lxd-image-server.nix index e5a292b61bd9..d0afa495a5b1 100644 --- a/nixos/tests/lxd-image-server.nix +++ b/nixos/tests/lxd-image-server.nix @@ -61,14 +61,14 @@ in { machine.wait_for_unit("lxd.service") machine.wait_for_file("/var/lib/lxd/unix.socket") - # It takes additional second for lxd to settle - machine.sleep(1) + # Wait for lxd to settle + machine.succeed("lxd waitready") # lxd expects the pool's directory to already exist machine.succeed("mkdir /var/lxd-pool") machine.succeed( - "cat ${./common/lxd/config.yaml} | lxd init --preseed" + "lxd init --minimal" ) machine.succeed( diff --git a/nixos/tests/lxd.nix b/nixos/tests/lxd/container.nix similarity index 72% rename from nixos/tests/lxd.nix rename to nixos/tests/lxd/container.nix index 2c2c19e0eecf..9e56f6e41e05 100644 --- a/nixos/tests/lxd.nix +++ b/nixos/tests/lxd/container.nix @@ -1,7 +1,7 @@ -import ./make-test-python.nix ({ pkgs, lib, ... } : +import ../make-test-python.nix ({ pkgs, lib, ... } : let - lxd-image = import ../release.nix { + lxd-image = import ../../release.nix { configuration = { # Building documentation makes the test unnecessarily take a longer time: documentation.enable = lib.mkForce false; @@ -38,19 +38,18 @@ in { }; testScript = '' + def instance_is_up(_) -> bool: + status, _ = machine.execute("lxc exec container --disable-stdin --force-interactive /run/current-system/sw/bin/true") + return status == 0 + machine.wait_for_unit("sockets.target") machine.wait_for_unit("lxd.service") machine.wait_for_file("/var/lib/lxd/unix.socket") - # It takes additional second for lxd to settle - machine.sleep(1) + # Wait for lxd to settle + machine.succeed("lxd waitready") - # lxd expects the pool's directory to already exist - machine.succeed("mkdir /var/lxd-pool") - - machine.succeed( - "cat ${./common/lxd/config.yaml} | lxd init --preseed" - ) + machine.succeed("lxd init --minimal") machine.succeed( "lxc image import ${lxd-image-metadata}/*/*.tar.xz ${lxd-image-rootfs}/*/*.tar.xz --alias nixos" @@ -58,21 +57,23 @@ in { with subtest("Container can be managed"): machine.succeed("lxc launch nixos container") - machine.sleep(5) + 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 -") - machine.succeed("lxc exec container true") machine.succeed("lxc delete -f container") with subtest("Container is mounted with lxcfs inside"): machine.succeed("lxc launch nixos container") - machine.sleep(5) + with machine.nested("Waiting for instance to start and be usable"): + retry(instance_is_up) ## ---------- ## ## limits.cpu ## machine.succeed("lxc config set container limits.cpu 1") machine.succeed("lxc restart container") - machine.sleep(5) + with machine.nested("Waiting for instance to start and be usable"): + retry(instance_is_up) assert ( "1" @@ -81,7 +82,8 @@ in { machine.succeed("lxc config set container limits.cpu 2") machine.succeed("lxc restart container") - machine.sleep(5) + with machine.nested("Waiting for instance to start and be usable"): + retry(instance_is_up) assert ( "2" @@ -93,7 +95,8 @@ in { machine.succeed("lxc config set container limits.memory 64MB") machine.succeed("lxc restart container") - machine.sleep(5) + with machine.nested("Waiting for instance to start and be usable"): + retry(instance_is_up) assert ( "MemTotal: 62500 kB" @@ -102,7 +105,8 @@ in { machine.succeed("lxc config set container limits.memory 128MB") machine.succeed("lxc restart container") - machine.sleep(5) + with machine.nested("Waiting for instance to start and be usable"): + retry(instance_is_up) assert ( "MemTotal: 125000 kB" diff --git a/nixos/tests/lxd/default.nix b/nixos/tests/lxd/default.nix new file mode 100644 index 000000000000..2e34907d7936 --- /dev/null +++ b/nixos/tests/lxd/default.nix @@ -0,0 +1,9 @@ +{ + system ? builtins.currentSystem, + config ? {}, + pkgs ? import ../../.. {inherit system config;}, +}: { + container = import ./container.nix {inherit system pkgs;}; + nftables = import ./nftables.nix {inherit system pkgs;}; + ui = import ./ui.nix {inherit system pkgs;}; +} diff --git a/nixos/tests/lxd-nftables.nix b/nixos/tests/lxd/nftables.nix similarity index 96% rename from nixos/tests/lxd-nftables.nix rename to nixos/tests/lxd/nftables.nix index 293065001567..b85caa9eb368 100644 --- a/nixos/tests/lxd-nftables.nix +++ b/nixos/tests/lxd/nftables.nix @@ -5,7 +5,7 @@ # iptables to nftables requires a full reboot, which is a bit hard inside NixOS # tests. -import ./make-test-python.nix ({ pkgs, ...} : { +import ../make-test-python.nix ({ pkgs, ...} : { name = "lxd-nftables"; meta = with pkgs.lib.maintainers; { diff --git a/nixos/tests/lxd-ui.nix b/nixos/tests/lxd/ui.nix similarity index 94% rename from nixos/tests/lxd-ui.nix rename to nixos/tests/lxd/ui.nix index 19eaa226c0bf..86cb30d8c2b6 100644 --- a/nixos/tests/lxd-ui.nix +++ b/nixos/tests/lxd/ui.nix @@ -1,4 +1,4 @@ -import ./make-test-python.nix ({ pkgs, lib, ... }: { +import ../make-test-python.nix ({ pkgs, lib, ... }: { name = "lxd-ui"; meta = with pkgs.lib.maintainers; { diff --git a/pkgs/tools/admin/lxd/default.nix b/pkgs/tools/admin/lxd/default.nix index 02bf8cbd4e6c..427d60f3edf6 100644 --- a/pkgs/tools/admin/lxd/default.nix +++ b/pkgs/tools/admin/lxd/default.nix @@ -76,8 +76,6 @@ buildGoModule rec { ''; passthru.tests.lxd = nixosTests.lxd; - passthru.tests.lxd-nftables = nixosTests.lxd-nftables; - passthru.tests.lxd-ui = nixosTests.lxd-ui; passthru.ui = callPackage ./ui.nix { }; passthru.updateScript = gitUpdater { url = "https://github.com/canonical/lxd.git";