Skip to content

Commit

Permalink
nixos/swapspace: add tests
Browse files Browse the repository at this point in the history
Signed-off-by: phanirithvij <[email protected]>
  • Loading branch information
phanirithvij committed Oct 27, 2024
1 parent 85fa07b commit 8b294f1
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
1 change: 1 addition & 0 deletions nixos/tests/all-tests.nix
Original file line number Diff line number Diff line change
Expand Up @@ -957,6 +957,7 @@ in {
swap-file-btrfs = handleTest ./swap-file-btrfs.nix {};
swap-partition = handleTest ./swap-partition.nix {};
swap-random-encryption = handleTest ./swap-random-encryption.nix {};
swapspace = handleTest ./swapspace.nix {};
sway = handleTest ./sway.nix {};
swayfx = handleTest ./swayfx.nix {};
switchTest = handleTest ./switch-test.nix { ng = false; };
Expand Down
73 changes: 73 additions & 0 deletions nixos/tests/swapspace.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import ./make-test-python.nix (
{ pkgs, lib, ... }:

{
name = "swapspace";

meta = with pkgs.lib.maintainers; {
maintainers = [
Luflosi
phanirithvij
];
};

nodes.machine = {
virtualisation.memorySize = 256;
systemd.oomd.extraConfig.DefaultMemoryPressureDurationSec = "1s";

services.swapspace = {
enable = true;
extraArgs = [ "-v" ];
settings = {
# test outside /var/lib/swapspace
swappath = "/root/swamp";
cooldown = 1;
};
};

# adopted from systemd-oomd test
systemd.slices.workload = {
description = "Test slice for memory pressure kills";
sliceConfig = {
MemoryAccounting = true;
ManagedOOMMemoryPressure = "kill";
ManagedOOMMemoryPressureLimit = "90%";
};
};

systemd.services.testbloat = {
description = "Create a lot of memory pressure";
serviceConfig = {
Slice = "workload.slice";
MemoryHigh = "128M";
ExecStart = "${pkgs.coreutils}/bin/tail /dev/zero";
};
};
};

testScript = ''
machine.wait_for_unit("multi-user.target")
machine.wait_for_unit("swapspace.service")
print(machine.succeed("systemctl status swapspace.service"))
print(machine.succeed("swapon --show"))
swamp = False
with subtest("OOMd should kill the bad service"):
machine.succeed("systemctl start testbloat.service")
# wait for a maximum of 45sec
for i in range(1, 11):
status = machine.get_unit_info("testbloat.service")["Result"]
out = machine.succeed("swapon --show")
print(out)
swamp = "/swamp" in out
if status == "oom-kill":
break
machine.sleep(i)
print(machine.succeed("swapspace -e -s /root/swamp"))
assert machine.succeed("swapon --show") == ""
assert swamp
'';
}
)

0 comments on commit 8b294f1

Please sign in to comment.