1 # Test logrotate service works and is enabled by default
4 importTest = { ... }: {
5 services.logrotate.settings.import = {
12 import ./make-test-python.nix ({ pkgs, ... }: rec {
14 meta = with pkgs.lib.maintainers; {
15 maintainers = [ martinetd ];
19 defaultMachine = { ... }: {
20 services.logrotate.enable = true;
22 failingMachine = { ... }: {
23 services.logrotate = {
25 configFile = pkgs.writeText "logrotate.conf" ''
26 # self-written config file
27 su notarealuser notagroupeither
31 machine = { config, ... }: {
32 imports = [ importTest ];
34 services.logrotate = {
37 # remove default frequency header and add another
42 # extra global setting... affecting nothing
48 # using mail somewhere should add --mail to logrotate invocation
50 mail = "user@domain.tld";
52 # postrotate should be suffixed by 'endscript'
54 postrotate = "touch /dev/null";
56 # check checkConfig works as expected: there is nothing to check here
57 # except that the file build passes
60 createolddir = "0750 root utmp";
62 "create " = "0750 root utmp";
64 # multiple paths should be aggregated
66 files = [ "file1" "file2" ];
68 # overriding imported path should keep existing attributes
69 # (e.g. olddir is still set)
80 with subtest("whether logrotate works"):
81 # we must rotate once first to create logrotate stamp
82 defaultMachine.succeed("systemctl start logrotate.service")
83 # we need to wait for console text once here to
84 # clear console buffer up to this point for next wait
85 defaultMachine.wait_for_console_text('logrotate.service: Deactivated successfully')
87 defaultMachine.succeed(
88 # wtmp is present in default config.
89 "rm -f /var/log/wtmp*",
90 # we need to give it at least 1MB
91 "dd if=/dev/zero of=/var/log/wtmp bs=2M count=1",
93 # move into the future and check rotation.
94 "date -s 'now + 1 month + 1 day'")
95 defaultMachine.wait_for_console_text('logrotate.service: Deactivated successfully')
96 defaultMachine.succeed(
98 "[ -e /var/log/wtmp.1 ]",
100 with subtest("default config does not have mail"):
101 defaultMachine.fail("systemctl cat logrotate.service | grep -- --mail")
102 with subtest("using mails adds mail option"):
103 machine.succeed("systemctl cat logrotate.service | grep -- --mail")
104 with subtest("check generated config matches expectation"):
106 # copy conf to /tmp/logrotate.conf for easy grep
107 "conf=$(systemctl cat logrotate | grep -oE '/nix/store[^ ]*logrotate.conf'); cp $conf /tmp/logrotate.conf",
108 "! grep weekly /tmp/logrotate.conf",
109 "grep -E '^delaycompress' /tmp/logrotate.conf",
110 "tail -n 1 /tmp/logrotate.conf | grep shred",
111 "sed -ne '/\"sendmail\" {/,/}/p' /tmp/logrotate.conf | grep 'mail user@domain.tld'",
112 "sed -ne '/\"postrotate\" {/,/}/p' /tmp/logrotate.conf | grep endscript",
113 "grep '\"file1\"\n\"file2\" {' /tmp/logrotate.conf",
114 "sed -ne '/\"import\" {/,/}/p' /tmp/logrotate.conf | grep noolddir",
116 # also check configFile option
117 failingMachine.succeed(
118 "conf=$(systemctl cat logrotate | grep -oE '/nix/store[^ ]*logrotate.conf'); cp $conf /tmp/logrotate.conf",
119 "grep 'self-written config' /tmp/logrotate.conf",
121 with subtest("Check logrotate-checkconf service"):
122 machine.wait_for_unit("logrotate-checkconf.service")
123 # wait_for_unit also asserts for success, so wait for
124 # parent target instead and check manually.
125 failingMachine.wait_for_unit("multi-user.target")
126 info = failingMachine.get_unit_info("logrotate-checkconf.service")
127 if info["ActiveState"] != "failed":
128 raise Exception('logrotate-checkconf.service was not failed')
130 machine.log(machine.execute("systemd-analyze security logrotate.service | grep -v ✓")[1])