summaryrefslogtreecommitdiffstats
path: root/nixos/tests/earlyoom.nix
blob: efe2b52942e42c38e5f3c754be7a9d77cf714dbd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
{ lib, ... }:
{
  name = "earlyoom";
  meta.maintainers = with lib.maintainers; [
    oxalica
  ];

  nodes.machine =
    { pkgs, ... }:
    {
      # Limit VM resource usage.
      virtualisation.memorySize = 1024;

      services.earlyoom = {
        enable = true;
        # Use SIGKILL, or `tail` will catch SIGTERM and exit successfully.
        freeMemKillThreshold = 90;
      };

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

  testScript = ''
    machine.wait_for_unit("earlyoom.service")

    with subtest("earlyoom should kill the bad service"):
        machine.fail("systemctl start --wait testbloat.service")
        assert machine.get_unit_info("testbloat.service")["Result"] == "signal"
        output = machine.succeed('journalctl -u earlyoom.service -b0')
        assert 'low memory! at or below SIGKILL limits' in output
  '';
}