From 22356f510d6774c36f022cc7f8b294f8fe7f7509 Mon Sep 17 00:00:00 2001 From: Piotr Kwiecinski <2151333+piotrkwiecinski@users.noreply.github.com> Date: Sat, 19 Apr 2025 22:57:48 +0200 Subject: [PATCH] nixosTests.logrotate: migrate to runTest Part of #386873 --- nixos/tests/all-tests.nix | 2 +- nixos/tests/logrotate.nix | 243 +++++++++++++++++++------------------- 2 files changed, 121 insertions(+), 124 deletions(-) diff --git a/nixos/tests/all-tests.nix b/nixos/tests/all-tests.nix index 27cda8ba09f2..22ff078c1cf5 100644 --- a/nixos/tests/all-tests.nix +++ b/nixos/tests/all-tests.nix @@ -740,7 +740,7 @@ in localsend = handleTest ./localsend.nix { }; locate = handleTest ./locate.nix { }; login = handleTest ./login.nix { }; - logrotate = handleTest ./logrotate.nix { }; + logrotate = runTest ./logrotate.nix; loki = handleTest ./loki.nix { }; luks = handleTest ./luks.nix { }; lvm2 = handleTest ./lvm2 { }; diff --git a/nixos/tests/logrotate.nix b/nixos/tests/logrotate.nix index 1efbcc4cfed5..603a0f164c46 100644 --- a/nixos/tests/logrotate.nix +++ b/nixos/tests/logrotate.nix @@ -10,137 +10,134 @@ let }; in +{ pkgs, ... }: +{ + name = "logrotate"; + meta = with pkgs.lib.maintainers; { + maintainers = [ martinetd ]; + }; -import ./make-test-python.nix ( - { pkgs, ... }: - rec { - name = "logrotate"; - meta = with pkgs.lib.maintainers; { - maintainers = [ martinetd ]; - }; - - nodes = { - defaultMachine = - { ... }: - { - services.logrotate.enable = true; + nodes = { + defaultMachine = + { ... }: + { + services.logrotate.enable = true; + }; + failingMachine = + { ... }: + { + services.logrotate = { + enable = true; + configFile = pkgs.writeText "logrotate.conf" '' + # self-written config file + su notarealuser notagroupeither + ''; }; - failingMachine = - { ... }: - { - services.logrotate = { - enable = true; - configFile = pkgs.writeText "logrotate.conf" '' - # self-written config file - su notarealuser notagroupeither - ''; - }; - }; - machine = - { config, ... }: - { - imports = [ importTest ]; + }; + machine = + { config, ... }: + { + imports = [ importTest ]; - services.logrotate = { - enable = true; - settings = { - # remove default frequency header and add another - header = { - frequency = null; - delaycompress = true; - }; - # extra global setting... affecting nothing - last_line = { - global = true; - priority = 2000; - shred = true; - }; - # using mail somewhere should add --mail to logrotate invocation - sendmail = { - mail = "user@domain.tld"; - }; - # postrotate should be suffixed by 'endscript' - postrotate = { - postrotate = "touch /dev/null"; - }; - # check checkConfig works as expected: there is nothing to check here - # except that the file build passes - checkConf = { - su = "root utmp"; - createolddir = "0750 root utmp"; - create = "root utmp"; - "create " = "0750 root utmp"; - }; - # multiple paths should be aggregated - multipath = { - files = [ - "file1" - "file2" - ]; - }; - # overriding imported path should keep existing attributes - # (e.g. olddir is still set) - import = { - notifempty = true; - }; + services.logrotate = { + enable = true; + settings = { + # remove default frequency header and add another + header = { + frequency = null; + delaycompress = true; + }; + # extra global setting... affecting nothing + last_line = { + global = true; + priority = 2000; + shred = true; + }; + # using mail somewhere should add --mail to logrotate invocation + sendmail = { + mail = "user@domain.tld"; + }; + # postrotate should be suffixed by 'endscript' + postrotate = { + postrotate = "touch /dev/null"; + }; + # check checkConfig works as expected: there is nothing to check here + # except that the file build passes + checkConf = { + su = "root utmp"; + createolddir = "0750 root utmp"; + create = "root utmp"; + "create " = "0750 root utmp"; + }; + # multiple paths should be aggregated + multipath = { + files = [ + "file1" + "file2" + ]; + }; + # overriding imported path should keep existing attributes + # (e.g. olddir is still set) + import = { + notifempty = true; }; }; }; - }; + }; + }; - testScript = '' - with subtest("whether logrotate works"): - # we must rotate once first to create logrotate stamp - defaultMachine.succeed("systemctl start logrotate.service") - # we need to wait for console text once here to - # clear console buffer up to this point for next wait - defaultMachine.wait_for_console_text('logrotate.service: Deactivated successfully') + testScript = '' + with subtest("whether logrotate works"): + # we must rotate once first to create logrotate stamp + defaultMachine.succeed("systemctl start logrotate.service") + # we need to wait for console text once here to + # clear console buffer up to this point for next wait + defaultMachine.wait_for_console_text('logrotate.service: Deactivated successfully') - defaultMachine.succeed( - # wtmp is present in default config. - "rm -f /var/log/wtmp*", - # we need to give it at least 1MB - "dd if=/dev/zero of=/var/log/wtmp bs=2M count=1", + defaultMachine.succeed( + # wtmp is present in default config. + "rm -f /var/log/wtmp*", + # we need to give it at least 1MB + "dd if=/dev/zero of=/var/log/wtmp bs=2M count=1", - # move into the future and check rotation. - "date -s 'now + 1 month + 1 day'") - defaultMachine.wait_for_console_text('logrotate.service: Deactivated successfully') - defaultMachine.succeed( - # check rotate worked - "[ -e /var/log/wtmp.1 ]", - ) - with subtest("default config does not have mail"): - defaultMachine.fail("systemctl cat logrotate.service | grep -- --mail") - with subtest("using mails adds mail option"): - machine.succeed("systemctl cat logrotate.service | grep -- --mail") - with subtest("check generated config matches expectation"): - machine.succeed( - # copy conf to /tmp/logrotate.conf for easy grep - "conf=$(systemctl cat logrotate | grep -oE '/nix/store[^ ]*logrotate.conf'); cp $conf /tmp/logrotate.conf", - "! grep weekly /tmp/logrotate.conf", - "grep -E '^delaycompress' /tmp/logrotate.conf", - "tail -n 1 /tmp/logrotate.conf | grep shred", - "sed -ne '/\"sendmail\" {/,/}/p' /tmp/logrotate.conf | grep 'mail user@domain.tld'", - "sed -ne '/\"postrotate\" {/,/}/p' /tmp/logrotate.conf | grep endscript", - "grep '\"file1\"\n\"file2\" {' /tmp/logrotate.conf", - "sed -ne '/\"import\" {/,/}/p' /tmp/logrotate.conf | grep noolddir", - ) - # also check configFile option - failingMachine.succeed( - "conf=$(systemctl cat logrotate | grep -oE '/nix/store[^ ]*logrotate.conf'); cp $conf /tmp/logrotate.conf", - "grep 'self-written config' /tmp/logrotate.conf", - ) - with subtest("Check logrotate-checkconf service"): - machine.wait_for_unit("logrotate-checkconf.service") - # wait_for_unit also asserts for success, so wait for - # parent target instead and check manually. - failingMachine.wait_for_unit("multi-user.target") - info = failingMachine.get_unit_info("logrotate-checkconf.service") - if info["ActiveState"] != "failed": - raise Exception('logrotate-checkconf.service was not failed') + # move into the future and check rotation. + "date -s 'now + 1 month + 1 day'") + defaultMachine.wait_for_console_text('logrotate.service: Deactivated successfully') + defaultMachine.succeed( + # check rotate worked + "[ -e /var/log/wtmp.1 ]", + ) + with subtest("default config does not have mail"): + defaultMachine.fail("systemctl cat logrotate.service | grep -- --mail") + with subtest("using mails adds mail option"): + machine.succeed("systemctl cat logrotate.service | grep -- --mail") + with subtest("check generated config matches expectation"): + machine.succeed( + # copy conf to /tmp/logrotate.conf for easy grep + "conf=$(systemctl cat logrotate | grep -oE '/nix/store[^ ]*logrotate.conf'); cp $conf /tmp/logrotate.conf", + "! grep weekly /tmp/logrotate.conf", + "grep -E '^delaycompress' /tmp/logrotate.conf", + "tail -n 1 /tmp/logrotate.conf | grep shred", + "sed -ne '/\"sendmail\" {/,/}/p' /tmp/logrotate.conf | grep 'mail user@domain.tld'", + "sed -ne '/\"postrotate\" {/,/}/p' /tmp/logrotate.conf | grep endscript", + "grep '\"file1\"\n\"file2\" {' /tmp/logrotate.conf", + "sed -ne '/\"import\" {/,/}/p' /tmp/logrotate.conf | grep noolddir", + ) + # also check configFile option + failingMachine.succeed( + "conf=$(systemctl cat logrotate | grep -oE '/nix/store[^ ]*logrotate.conf'); cp $conf /tmp/logrotate.conf", + "grep 'self-written config' /tmp/logrotate.conf", + ) + with subtest("Check logrotate-checkconf service"): + machine.wait_for_unit("logrotate-checkconf.service") + # wait_for_unit also asserts for success, so wait for + # parent target instead and check manually. + failingMachine.wait_for_unit("multi-user.target") + info = failingMachine.get_unit_info("logrotate-checkconf.service") + if info["ActiveState"] != "failed": + raise Exception('logrotate-checkconf.service was not failed') - machine.log(machine.execute("systemd-analyze security logrotate.service | grep -v ✓")[1]) + machine.log(machine.execute("systemd-analyze security logrotate.service | grep -v ✓")[1]) - ''; - } -) + ''; +}