nixosTests.logrotate: migrate to runTest

Part of #386873
This commit is contained in:
Piotr Kwiecinski 2025-04-19 22:57:48 +02:00
parent 4db241dede
commit 22356f510d
No known key found for this signature in database
GPG key ID: EC0DE1CB9D5258B4
2 changed files with 121 additions and 124 deletions

View file

@ -740,7 +740,7 @@ in
localsend = handleTest ./localsend.nix { }; localsend = handleTest ./localsend.nix { };
locate = handleTest ./locate.nix { }; locate = handleTest ./locate.nix { };
login = handleTest ./login.nix { }; login = handleTest ./login.nix { };
logrotate = handleTest ./logrotate.nix { }; logrotate = runTest ./logrotate.nix;
loki = handleTest ./loki.nix { }; loki = handleTest ./loki.nix { };
luks = handleTest ./luks.nix { }; luks = handleTest ./luks.nix { };
lvm2 = handleTest ./lvm2 { }; lvm2 = handleTest ./lvm2 { };

View file

@ -10,137 +10,134 @@ let
}; };
in in
{ pkgs, ... }:
{
name = "logrotate";
meta = with pkgs.lib.maintainers; {
maintainers = [ martinetd ];
};
import ./make-test-python.nix ( nodes = {
{ pkgs, ... }: defaultMachine =
rec { { ... }:
name = "logrotate"; {
meta = with pkgs.lib.maintainers; { services.logrotate.enable = true;
maintainers = [ martinetd ]; };
}; failingMachine =
{ ... }:
nodes = { {
defaultMachine = services.logrotate = {
{ ... }: enable = true;
{ configFile = pkgs.writeText "logrotate.conf" ''
services.logrotate.enable = true; # self-written config file
su notarealuser notagroupeither
'';
}; };
failingMachine = };
{ ... }: machine =
{ { config, ... }:
services.logrotate = { {
enable = true; imports = [ importTest ];
configFile = pkgs.writeText "logrotate.conf" ''
# self-written config file
su notarealuser notagroupeither
'';
};
};
machine =
{ config, ... }:
{
imports = [ importTest ];
services.logrotate = { services.logrotate = {
enable = true; enable = true;
settings = { settings = {
# remove default frequency header and add another # remove default frequency header and add another
header = { header = {
frequency = null; frequency = null;
delaycompress = true; delaycompress = true;
}; };
# extra global setting... affecting nothing # extra global setting... affecting nothing
last_line = { last_line = {
global = true; global = true;
priority = 2000; priority = 2000;
shred = true; shred = true;
}; };
# using mail somewhere should add --mail to logrotate invocation # using mail somewhere should add --mail to logrotate invocation
sendmail = { sendmail = {
mail = "user@domain.tld"; mail = "user@domain.tld";
}; };
# postrotate should be suffixed by 'endscript' # postrotate should be suffixed by 'endscript'
postrotate = { postrotate = {
postrotate = "touch /dev/null"; postrotate = "touch /dev/null";
}; };
# check checkConfig works as expected: there is nothing to check here # check checkConfig works as expected: there is nothing to check here
# except that the file build passes # except that the file build passes
checkConf = { checkConf = {
su = "root utmp"; su = "root utmp";
createolddir = "0750 root utmp"; createolddir = "0750 root utmp";
create = "root utmp"; create = "root utmp";
"create " = "0750 root utmp"; "create " = "0750 root utmp";
}; };
# multiple paths should be aggregated # multiple paths should be aggregated
multipath = { multipath = {
files = [ files = [
"file1" "file1"
"file2" "file2"
]; ];
}; };
# overriding imported path should keep existing attributes # overriding imported path should keep existing attributes
# (e.g. olddir is still set) # (e.g. olddir is still set)
import = { import = {
notifempty = true; notifempty = true;
};
}; };
}; };
}; };
}; };
};
testScript = '' testScript = ''
with subtest("whether logrotate works"): with subtest("whether logrotate works"):
# we must rotate once first to create logrotate stamp # we must rotate once first to create logrotate stamp
defaultMachine.succeed("systemctl start logrotate.service") defaultMachine.succeed("systemctl start logrotate.service")
# we need to wait for console text once here to # we need to wait for console text once here to
# clear console buffer up to this point for next wait # clear console buffer up to this point for next wait
defaultMachine.wait_for_console_text('logrotate.service: Deactivated successfully') defaultMachine.wait_for_console_text('logrotate.service: Deactivated successfully')
defaultMachine.succeed( defaultMachine.succeed(
# wtmp is present in default config. # wtmp is present in default config.
"rm -f /var/log/wtmp*", "rm -f /var/log/wtmp*",
# we need to give it at least 1MB # we need to give it at least 1MB
"dd if=/dev/zero of=/var/log/wtmp bs=2M count=1", "dd if=/dev/zero of=/var/log/wtmp bs=2M count=1",
# move into the future and check rotation. # move into the future and check rotation.
"date -s 'now + 1 month + 1 day'") "date -s 'now + 1 month + 1 day'")
defaultMachine.wait_for_console_text('logrotate.service: Deactivated successfully') defaultMachine.wait_for_console_text('logrotate.service: Deactivated successfully')
defaultMachine.succeed( defaultMachine.succeed(
# check rotate worked # check rotate worked
"[ -e /var/log/wtmp.1 ]", "[ -e /var/log/wtmp.1 ]",
) )
with subtest("default config does not have mail"): with subtest("default config does not have mail"):
defaultMachine.fail("systemctl cat logrotate.service | grep -- --mail") defaultMachine.fail("systemctl cat logrotate.service | grep -- --mail")
with subtest("using mails adds mail option"): with subtest("using mails adds mail option"):
machine.succeed("systemctl cat logrotate.service | grep -- --mail") machine.succeed("systemctl cat logrotate.service | grep -- --mail")
with subtest("check generated config matches expectation"): with subtest("check generated config matches expectation"):
machine.succeed( machine.succeed(
# copy conf to /tmp/logrotate.conf for easy grep # copy conf to /tmp/logrotate.conf for easy grep
"conf=$(systemctl cat logrotate | grep -oE '/nix/store[^ ]*logrotate.conf'); cp $conf /tmp/logrotate.conf", "conf=$(systemctl cat logrotate | grep -oE '/nix/store[^ ]*logrotate.conf'); cp $conf /tmp/logrotate.conf",
"! grep weekly /tmp/logrotate.conf", "! grep weekly /tmp/logrotate.conf",
"grep -E '^delaycompress' /tmp/logrotate.conf", "grep -E '^delaycompress' /tmp/logrotate.conf",
"tail -n 1 /tmp/logrotate.conf | grep shred", "tail -n 1 /tmp/logrotate.conf | grep shred",
"sed -ne '/\"sendmail\" {/,/}/p' /tmp/logrotate.conf | grep 'mail user@domain.tld'", "sed -ne '/\"sendmail\" {/,/}/p' /tmp/logrotate.conf | grep 'mail user@domain.tld'",
"sed -ne '/\"postrotate\" {/,/}/p' /tmp/logrotate.conf | grep endscript", "sed -ne '/\"postrotate\" {/,/}/p' /tmp/logrotate.conf | grep endscript",
"grep '\"file1\"\n\"file2\" {' /tmp/logrotate.conf", "grep '\"file1\"\n\"file2\" {' /tmp/logrotate.conf",
"sed -ne '/\"import\" {/,/}/p' /tmp/logrotate.conf | grep noolddir", "sed -ne '/\"import\" {/,/}/p' /tmp/logrotate.conf | grep noolddir",
) )
# also check configFile option # also check configFile option
failingMachine.succeed( failingMachine.succeed(
"conf=$(systemctl cat logrotate | grep -oE '/nix/store[^ ]*logrotate.conf'); cp $conf /tmp/logrotate.conf", "conf=$(systemctl cat logrotate | grep -oE '/nix/store[^ ]*logrotate.conf'); cp $conf /tmp/logrotate.conf",
"grep 'self-written config' /tmp/logrotate.conf", "grep 'self-written config' /tmp/logrotate.conf",
) )
with subtest("Check logrotate-checkconf service"): with subtest("Check logrotate-checkconf service"):
machine.wait_for_unit("logrotate-checkconf.service") machine.wait_for_unit("logrotate-checkconf.service")
# wait_for_unit also asserts for success, so wait for # wait_for_unit also asserts for success, so wait for
# parent target instead and check manually. # parent target instead and check manually.
failingMachine.wait_for_unit("multi-user.target") failingMachine.wait_for_unit("multi-user.target")
info = failingMachine.get_unit_info("logrotate-checkconf.service") info = failingMachine.get_unit_info("logrotate-checkconf.service")
if info["ActiveState"] != "failed": if info["ActiveState"] != "failed":
raise Exception('logrotate-checkconf.service was not 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])
''; '';
} }
)