diff --git a/nixos/tests/all-tests.nix b/nixos/tests/all-tests.nix index dd6519ff9361e5..41939be99a43a8 100644 --- a/nixos/tests/all-tests.nix +++ b/nixos/tests/all-tests.nix @@ -956,6 +956,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; }; diff --git a/nixos/tests/swapspace.nix b/nixos/tests/swapspace.nix new file mode 100644 index 00000000000000..3327ad23bd29ab --- /dev/null +++ b/nixos/tests/swapspace.nix @@ -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 + ''; + } +)