nixosTests.logrotate: migrate to runTest (#400176)

authored by Pol Dellaiera and committed by GitHub 92581855 b3c060a0

+121 -124
+1 -1
nixos/tests/all-tests.nix
··· 740 740 localsend = handleTest ./localsend.nix { }; 741 741 locate = handleTest ./locate.nix { }; 742 742 login = handleTest ./login.nix { }; 743 - logrotate = handleTest ./logrotate.nix { }; 743 + logrotate = runTest ./logrotate.nix; 744 744 loki = handleTest ./loki.nix { }; 745 745 luks = handleTest ./luks.nix { }; 746 746 lvm2 = handleTest ./lvm2 { };
+120 -123
nixos/tests/logrotate.nix
··· 10 10 }; 11 11 12 12 in 13 + { pkgs, ... }: 14 + { 15 + name = "logrotate"; 16 + meta = with pkgs.lib.maintainers; { 17 + maintainers = [ martinetd ]; 18 + }; 13 19 14 - import ./make-test-python.nix ( 15 - { pkgs, ... }: 16 - rec { 17 - name = "logrotate"; 18 - meta = with pkgs.lib.maintainers; { 19 - maintainers = [ martinetd ]; 20 - }; 21 - 22 - nodes = { 23 - defaultMachine = 24 - { ... }: 25 - { 26 - services.logrotate.enable = true; 20 + nodes = { 21 + defaultMachine = 22 + { ... }: 23 + { 24 + services.logrotate.enable = true; 25 + }; 26 + failingMachine = 27 + { ... }: 28 + { 29 + services.logrotate = { 30 + enable = true; 31 + configFile = pkgs.writeText "logrotate.conf" '' 32 + # self-written config file 33 + su notarealuser notagroupeither 34 + ''; 27 35 }; 28 - failingMachine = 29 - { ... }: 30 - { 31 - services.logrotate = { 32 - enable = true; 33 - configFile = pkgs.writeText "logrotate.conf" '' 34 - # self-written config file 35 - su notarealuser notagroupeither 36 - ''; 37 - }; 38 - }; 39 - machine = 40 - { config, ... }: 41 - { 42 - imports = [ importTest ]; 36 + }; 37 + machine = 38 + { config, ... }: 39 + { 40 + imports = [ importTest ]; 43 41 44 - services.logrotate = { 45 - enable = true; 46 - settings = { 47 - # remove default frequency header and add another 48 - header = { 49 - frequency = null; 50 - delaycompress = true; 51 - }; 52 - # extra global setting... affecting nothing 53 - last_line = { 54 - global = true; 55 - priority = 2000; 56 - shred = true; 57 - }; 58 - # using mail somewhere should add --mail to logrotate invocation 59 - sendmail = { 60 - mail = "user@domain.tld"; 61 - }; 62 - # postrotate should be suffixed by 'endscript' 63 - postrotate = { 64 - postrotate = "touch /dev/null"; 65 - }; 66 - # check checkConfig works as expected: there is nothing to check here 67 - # except that the file build passes 68 - checkConf = { 69 - su = "root utmp"; 70 - createolddir = "0750 root utmp"; 71 - create = "root utmp"; 72 - "create " = "0750 root utmp"; 73 - }; 74 - # multiple paths should be aggregated 75 - multipath = { 76 - files = [ 77 - "file1" 78 - "file2" 79 - ]; 80 - }; 81 - # overriding imported path should keep existing attributes 82 - # (e.g. olddir is still set) 83 - import = { 84 - notifempty = true; 85 - }; 42 + services.logrotate = { 43 + enable = true; 44 + settings = { 45 + # remove default frequency header and add another 46 + header = { 47 + frequency = null; 48 + delaycompress = true; 49 + }; 50 + # extra global setting... affecting nothing 51 + last_line = { 52 + global = true; 53 + priority = 2000; 54 + shred = true; 55 + }; 56 + # using mail somewhere should add --mail to logrotate invocation 57 + sendmail = { 58 + mail = "user@domain.tld"; 59 + }; 60 + # postrotate should be suffixed by 'endscript' 61 + postrotate = { 62 + postrotate = "touch /dev/null"; 63 + }; 64 + # check checkConfig works as expected: there is nothing to check here 65 + # except that the file build passes 66 + checkConf = { 67 + su = "root utmp"; 68 + createolddir = "0750 root utmp"; 69 + create = "root utmp"; 70 + "create " = "0750 root utmp"; 71 + }; 72 + # multiple paths should be aggregated 73 + multipath = { 74 + files = [ 75 + "file1" 76 + "file2" 77 + ]; 78 + }; 79 + # overriding imported path should keep existing attributes 80 + # (e.g. olddir is still set) 81 + import = { 82 + notifempty = true; 86 83 }; 87 84 }; 88 85 }; 89 - }; 86 + }; 87 + }; 90 88 91 - testScript = '' 92 - with subtest("whether logrotate works"): 93 - # we must rotate once first to create logrotate stamp 94 - defaultMachine.succeed("systemctl start logrotate.service") 95 - # we need to wait for console text once here to 96 - # clear console buffer up to this point for next wait 97 - defaultMachine.wait_for_console_text('logrotate.service: Deactivated successfully') 89 + testScript = '' 90 + with subtest("whether logrotate works"): 91 + # we must rotate once first to create logrotate stamp 92 + defaultMachine.succeed("systemctl start logrotate.service") 93 + # we need to wait for console text once here to 94 + # clear console buffer up to this point for next wait 95 + defaultMachine.wait_for_console_text('logrotate.service: Deactivated successfully') 98 96 99 - defaultMachine.succeed( 100 - # wtmp is present in default config. 101 - "rm -f /var/log/wtmp*", 102 - # we need to give it at least 1MB 103 - "dd if=/dev/zero of=/var/log/wtmp bs=2M count=1", 97 + defaultMachine.succeed( 98 + # wtmp is present in default config. 99 + "rm -f /var/log/wtmp*", 100 + # we need to give it at least 1MB 101 + "dd if=/dev/zero of=/var/log/wtmp bs=2M count=1", 104 102 105 - # move into the future and check rotation. 106 - "date -s 'now + 1 month + 1 day'") 107 - defaultMachine.wait_for_console_text('logrotate.service: Deactivated successfully') 108 - defaultMachine.succeed( 109 - # check rotate worked 110 - "[ -e /var/log/wtmp.1 ]", 111 - ) 112 - with subtest("default config does not have mail"): 113 - defaultMachine.fail("systemctl cat logrotate.service | grep -- --mail") 114 - with subtest("using mails adds mail option"): 115 - machine.succeed("systemctl cat logrotate.service | grep -- --mail") 116 - with subtest("check generated config matches expectation"): 117 - machine.succeed( 118 - # copy conf to /tmp/logrotate.conf for easy grep 119 - "conf=$(systemctl cat logrotate | grep -oE '/nix/store[^ ]*logrotate.conf'); cp $conf /tmp/logrotate.conf", 120 - "! grep weekly /tmp/logrotate.conf", 121 - "grep -E '^delaycompress' /tmp/logrotate.conf", 122 - "tail -n 1 /tmp/logrotate.conf | grep shred", 123 - "sed -ne '/\"sendmail\" {/,/}/p' /tmp/logrotate.conf | grep 'mail user@domain.tld'", 124 - "sed -ne '/\"postrotate\" {/,/}/p' /tmp/logrotate.conf | grep endscript", 125 - "grep '\"file1\"\n\"file2\" {' /tmp/logrotate.conf", 126 - "sed -ne '/\"import\" {/,/}/p' /tmp/logrotate.conf | grep noolddir", 127 - ) 128 - # also check configFile option 129 - failingMachine.succeed( 130 - "conf=$(systemctl cat logrotate | grep -oE '/nix/store[^ ]*logrotate.conf'); cp $conf /tmp/logrotate.conf", 131 - "grep 'self-written config' /tmp/logrotate.conf", 132 - ) 133 - with subtest("Check logrotate-checkconf service"): 134 - machine.wait_for_unit("logrotate-checkconf.service") 135 - # wait_for_unit also asserts for success, so wait for 136 - # parent target instead and check manually. 137 - failingMachine.wait_for_unit("multi-user.target") 138 - info = failingMachine.get_unit_info("logrotate-checkconf.service") 139 - if info["ActiveState"] != "failed": 140 - raise Exception('logrotate-checkconf.service was not failed') 103 + # move into the future and check rotation. 104 + "date -s 'now + 1 month + 1 day'") 105 + defaultMachine.wait_for_console_text('logrotate.service: Deactivated successfully') 106 + defaultMachine.succeed( 107 + # check rotate worked 108 + "[ -e /var/log/wtmp.1 ]", 109 + ) 110 + with subtest("default config does not have mail"): 111 + defaultMachine.fail("systemctl cat logrotate.service | grep -- --mail") 112 + with subtest("using mails adds mail option"): 113 + machine.succeed("systemctl cat logrotate.service | grep -- --mail") 114 + with subtest("check generated config matches expectation"): 115 + machine.succeed( 116 + # copy conf to /tmp/logrotate.conf for easy grep 117 + "conf=$(systemctl cat logrotate | grep -oE '/nix/store[^ ]*logrotate.conf'); cp $conf /tmp/logrotate.conf", 118 + "! grep weekly /tmp/logrotate.conf", 119 + "grep -E '^delaycompress' /tmp/logrotate.conf", 120 + "tail -n 1 /tmp/logrotate.conf | grep shred", 121 + "sed -ne '/\"sendmail\" {/,/}/p' /tmp/logrotate.conf | grep 'mail user@domain.tld'", 122 + "sed -ne '/\"postrotate\" {/,/}/p' /tmp/logrotate.conf | grep endscript", 123 + "grep '\"file1\"\n\"file2\" {' /tmp/logrotate.conf", 124 + "sed -ne '/\"import\" {/,/}/p' /tmp/logrotate.conf | grep noolddir", 125 + ) 126 + # also check configFile option 127 + failingMachine.succeed( 128 + "conf=$(systemctl cat logrotate | grep -oE '/nix/store[^ ]*logrotate.conf'); cp $conf /tmp/logrotate.conf", 129 + "grep 'self-written config' /tmp/logrotate.conf", 130 + ) 131 + with subtest("Check logrotate-checkconf service"): 132 + machine.wait_for_unit("logrotate-checkconf.service") 133 + # wait_for_unit also asserts for success, so wait for 134 + # parent target instead and check manually. 135 + failingMachine.wait_for_unit("multi-user.target") 136 + info = failingMachine.get_unit_info("logrotate-checkconf.service") 137 + if info["ActiveState"] != "failed": 138 + raise Exception('logrotate-checkconf.service was not failed') 141 139 142 - machine.log(machine.execute("systemd-analyze security logrotate.service | grep -v ✓")[1]) 140 + machine.log(machine.execute("systemd-analyze security logrotate.service | grep -v ✓")[1]) 143 141 144 - ''; 145 - } 146 - ) 142 + ''; 143 + }