···
6
+
# This test is a simplified version of systemd's testsuite-55.
7
+
# https://github.com/systemd/systemd/blob/v251/test/units/testsuite-55.sh
nodes.machine = { pkgs, ... }: {
7
-
systemd.oomd.extraConfig.DefaultMemoryPressureDurationSec = "1s"; # makes the test faster
8
-
# Kill cgroups when more than 1% pressure is encountered
9
-
systemd.slices."-".sliceConfig = {
10
-
ManagedOOMMemoryPressure = "kill";
11
-
ManagedOOMMemoryPressureLimit = "1%";
9
+
# Limit VM resource usage.
10
+
virtualisation.memorySize = 1024;
11
+
systemd.oomd.extraConfig.DefaultMemoryPressureDurationSec = "1s";
13
+
systemd.slices.workload = {
14
+
description = "Test slice for memory pressure kills";
16
+
MemoryAccounting = true;
17
+
ManagedOOMMemoryPressure = "kill";
18
+
ManagedOOMMemoryPressureLimit = "10%";
13
-
# A service to bring the system under memory pressure
14
-
systemd.services.testservice = {
15
-
serviceConfig.ExecStart = "${pkgs.coreutils}/bin/tail /dev/zero";
22
+
systemd.services.testbloat = {
23
+
description = "Create a lot of memory pressure";
25
+
Slice = "workload.slice";
27
+
ExecStart = "${pkgs.coreutils}/bin/tail /dev/zero";
17
-
# Do not kill the backdoor
18
-
systemd.services.backdoor.serviceConfig.ManagedOOMMemoryPressure = "auto";
20
-
virtualisation.memorySize = 1024;
31
+
systemd.services.testchill = {
32
+
description = "No memory pressure";
34
+
Slice = "workload.slice";
36
+
ExecStart = "${pkgs.coreutils}/bin/sleep infinity";
machine.wait_for_unit("multi-user.target")
machine.succeed("oomctl")
28
-
# Bring the system into memory pressure
29
-
machine.succeed("echo 0 > /proc/sys/vm/panic_on_oom") # NixOS tests kill the VM when the OOM killer is invoked - override this
30
-
machine.succeed("systemctl start testservice")
46
+
machine.succeed("systemctl start testchill.service")
47
+
with subtest("OOMd should kill the bad service"):
48
+
machine.fail("systemctl start --wait testbloat.service")
49
+
assert machine.get_unit_info("testbloat.service")["Result"] == "oom-kill"
32
-
# Wait for oomd to kill something
33
-
# Matches these lines:
34
-
# systemd-oomd[508]: Killed /system.slice/systemd-udevd.service due to memory pressure for / being 3.26% > 1.00% for > 1s with reclaim activity
35
-
machine.wait_until_succeeds("journalctl -b | grep -q 'due to memory pressure for'")
51
+
with subtest("Service without memory pressure should be untouched"):
52
+
machine.require_unit_state("testchill.service", "active")