1# Test logrotate service works and is enabled by default
2
3let
4 importTest = { ... }: {
5 services.logrotate.settings.import = {
6 olddir = false;
7 };
8 };
9
10in
11
12import ./make-test-python.nix ({ pkgs, ... }: rec {
13 name = "logrotate";
14 meta = with pkgs.lib.maintainers; {
15 maintainers = [ martinetd ];
16 };
17
18 nodes = {
19 defaultMachine = { ... }: {
20 services.logrotate.enable = true;
21 };
22 failingMachine = { ... }: {
23 services.logrotate = {
24 enable = true;
25 configFile = pkgs.writeText "logrotate.conf" ''
26 # self-written config file
27 su notarealuser notagroupeither
28 '';
29 };
30 };
31 machine = { config, ... }: {
32 imports = [ importTest ];
33
34 services.logrotate = {
35 enable = true;
36 settings = {
37 # remove default frequency header and add another
38 header = {
39 frequency = null;
40 delaycompress = true;
41 };
42 # extra global setting... affecting nothing
43 last_line = {
44 global = true;
45 priority = 2000;
46 shred = true;
47 };
48 # using mail somewhere should add --mail to logrotate invocation
49 sendmail = {
50 mail = "user@domain.tld";
51 };
52 # postrotate should be suffixed by 'endscript'
53 postrotate = {
54 postrotate = "touch /dev/null";
55 };
56 # check checkConfig works as expected: there is nothing to check here
57 # except that the file build passes
58 checkConf = {
59 su = "root utmp";
60 createolddir = "0750 root utmp";
61 create = "root utmp";
62 "create " = "0750 root utmp";
63 };
64 # multiple paths should be aggregated
65 multipath = {
66 files = [ "file1" "file2" ];
67 };
68 # overriding imported path should keep existing attributes
69 # (e.g. olddir is still set)
70 import = {
71 notifempty = true;
72 };
73 };
74 };
75 };
76 };
77
78 testScript =
79 ''
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')
86
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",
92
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(
97 # check rotate worked
98 "[ -e /var/log/wtmp.1 ]",
99 )
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"):
105 machine.succeed(
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",
115 )
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",
120 )
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')
129
130 '';
131})