1{ pkgs, ... }:
2
3{
4 name = "systemd-oomd";
5
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
8 nodes.machine =
9 { pkgs, ... }:
10 {
11 # Limit VM resource usage.
12 virtualisation.memorySize = 1024;
13 systemd.oomd.extraConfig.DefaultMemoryPressureDurationSec = "1s";
14
15 systemd.slices.workload = {
16 description = "Test slice for memory pressure kills";
17 sliceConfig = {
18 MemoryAccounting = true;
19 ManagedOOMMemoryPressure = "kill";
20 ManagedOOMMemoryPressureLimit = "10%";
21 };
22 };
23
24 systemd.services.testbloat = {
25 description = "Create a lot of memory pressure";
26 serviceConfig = {
27 Slice = "workload.slice";
28 MemoryHigh = "5M";
29 ExecStart = "${pkgs.coreutils}/bin/tail /dev/zero";
30 };
31 };
32
33 systemd.services.testchill = {
34 description = "No memory pressure";
35 serviceConfig = {
36 Slice = "workload.slice";
37 MemoryHigh = "3M";
38 ExecStart = "${pkgs.coreutils}/bin/sleep infinity";
39 };
40 };
41 };
42
43 testScript = ''
44 # Start the system.
45 machine.wait_for_unit("multi-user.target")
46 machine.succeed("oomctl")
47
48 machine.succeed("systemctl start testchill.service")
49 with subtest("OOMd should kill the bad service"):
50 machine.fail("systemctl start --wait testbloat.service")
51 assert machine.get_unit_info("testbloat.service")["Result"] == "oom-kill"
52
53 with subtest("Service without memory pressure should be untouched"):
54 machine.require_unit_state("testchill.service", "active")
55 '';
56}