at 23.11-pre 4.7 kB view raw
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 failingMachine = { ... }: { 21 services.logrotate.configFile = pkgs.writeText "logrotate.conf" '' 22 # self-written config file 23 su notarealuser notagroupeither 24 ''; 25 }; 26 machine = { config, ... }: { 27 imports = [ importTest ]; 28 29 services.logrotate.settings = { 30 # remove default frequency header and add another 31 header = { 32 frequency = null; 33 delaycompress = true; 34 }; 35 # extra global setting... affecting nothing 36 last_line = { 37 global = true; 38 priority = 2000; 39 shred = true; 40 }; 41 # using mail somewhere should add --mail to logrotate invocation 42 sendmail = { 43 mail = "user@domain.tld"; 44 }; 45 # postrotate should be suffixed by 'endscript' 46 postrotate = { 47 postrotate = "touch /dev/null"; 48 }; 49 # check checkConfig works as expected: there is nothing to check here 50 # except that the file build passes 51 checkConf = { 52 su = "root utmp"; 53 createolddir = "0750 root utmp"; 54 create = "root utmp"; 55 "create " = "0750 root utmp"; 56 }; 57 # multiple paths should be aggregated 58 multipath = { 59 files = [ "file1" "file2" ]; 60 }; 61 # overriding imported path should keep existing attributes 62 # (e.g. olddir is still set) 63 import = { 64 notifempty = true; 65 }; 66 }; 67 }; 68 }; 69 70 testScript = 71 '' 72 with subtest("whether logrotate works"): 73 # we must rotate once first to create logrotate stamp 74 defaultMachine.succeed("systemctl start logrotate.service") 75 # we need to wait for console text once here to 76 # clear console buffer up to this point for next wait 77 defaultMachine.wait_for_console_text('logrotate.service: Deactivated successfully') 78 79 defaultMachine.succeed( 80 # wtmp is present in default config. 81 "rm -f /var/log/wtmp*", 82 # we need to give it at least 1MB 83 "dd if=/dev/zero of=/var/log/wtmp bs=2M count=1", 84 85 # move into the future and check rotation. 86 "date -s 'now + 1 month + 1 day'") 87 defaultMachine.wait_for_console_text('logrotate.service: Deactivated successfully') 88 defaultMachine.succeed( 89 # check rotate worked 90 "[ -e /var/log/wtmp.1 ]", 91 ) 92 with subtest("default config does not have mail"): 93 defaultMachine.fail("systemctl cat logrotate.service | grep -- --mail") 94 with subtest("using mails adds mail option"): 95 machine.succeed("systemctl cat logrotate.service | grep -- --mail") 96 with subtest("check generated config matches expectation"): 97 machine.succeed( 98 # copy conf to /tmp/logrotate.conf for easy grep 99 "conf=$(systemctl cat logrotate | grep -oE '/nix/store[^ ]*logrotate.conf'); cp $conf /tmp/logrotate.conf", 100 "! grep weekly /tmp/logrotate.conf", 101 "grep -E '^delaycompress' /tmp/logrotate.conf", 102 "tail -n 1 /tmp/logrotate.conf | grep shred", 103 "sed -ne '/\"sendmail\" {/,/}/p' /tmp/logrotate.conf | grep 'mail user@domain.tld'", 104 "sed -ne '/\"postrotate\" {/,/}/p' /tmp/logrotate.conf | grep endscript", 105 "grep '\"file1\"\n\"file2\" {' /tmp/logrotate.conf", 106 "sed -ne '/\"import\" {/,/}/p' /tmp/logrotate.conf | grep noolddir", 107 ) 108 # also check configFile option 109 failingMachine.succeed( 110 "conf=$(systemctl cat logrotate | grep -oE '/nix/store[^ ]*logrotate.conf'); cp $conf /tmp/logrotate.conf", 111 "grep 'self-written config' /tmp/logrotate.conf", 112 ) 113 with subtest("Check logrotate-checkconf service"): 114 machine.wait_for_unit("logrotate-checkconf.service") 115 # wait_for_unit also asserts for success, so wait for 116 # parent target instead and check manually. 117 failingMachine.wait_for_unit("multi-user.target") 118 info = failingMachine.get_unit_info("logrotate-checkconf.service") 119 if info["ActiveState"] != "failed": 120 raise Exception('logrotate-checkconf.service was not failed') 121 122 ''; 123})