Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 1 | # |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 2 | # Copyright OpenEmbedded Contributors |
| 3 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 4 | # SPDX-License-Identifier: MIT |
| 5 | # |
| 6 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 7 | # This test should cover https://bugzilla.yoctoproject.org/tr_show_case.cgi?case_id=289 testcase |
| 8 | # Note that the image under test must have logrotate installed |
| 9 | |
| 10 | from oeqa.runtime.case import OERuntimeTestCase |
| 11 | from oeqa.core.decorator.depends import OETestDepends |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 12 | from oeqa.runtime.decorator.package import OEHasPackage |
| 13 | |
| 14 | class LogrotateTest(OERuntimeTestCase): |
| 15 | |
| 16 | @classmethod |
Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 17 | def setUpClass(cls): |
| 18 | cls.tc.target.run('cp /etc/logrotate.d/wtmp $HOME/wtmp.oeqabak') |
| 19 | |
| 20 | @classmethod |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 21 | def tearDownClass(cls): |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 22 | cls.tc.target.run('mv -f $HOME/wtmp.oeqabak /etc/logrotate.d/wtmp && rm -rf /var/log//logrotate_dir') |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 23 | cls.tc.target.run('rm -rf /var/log/logrotate_testfile && rm -rf /etc/logrotate.d/logrotate_testfile') |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 24 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 25 | @OETestDepends(['ssh.SSHTest.test_ssh']) |
| 26 | @OEHasPackage(['logrotate']) |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 27 | def test_logrotate_wtmp(self): |
| 28 | |
| 29 | # /var/log/wtmp may not always exist initially, so use touch to ensure it is present |
| 30 | status, output = self.target.run('touch /var/log/wtmp') |
| 31 | msg = ('Could not create/update /var/log/wtmp with touch') |
| 32 | self.assertEqual(status, 0, msg = msg) |
| 33 | |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 34 | status, output = self.target.run('mkdir /var/log//logrotate_dir') |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 35 | msg = ('Could not create logrotate_dir. Output: %s' % output) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 36 | self.assertEqual(status, 0, msg = msg) |
| 37 | |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 38 | status, output = self.target.run('echo "create \n olddir /var/log//logrotate_dir \n include /etc/logrotate.d/wtmp" > /tmp/logrotate-test.conf') |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 39 | msg = ('Could not write to /tmp/logrotate-test.conf') |
| 40 | self.assertEqual(status, 0, msg = msg) |
| 41 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 42 | # If logrotate fails to rotate the log, view the verbose output of logrotate to see what prevented it |
| 43 | _, logrotate_output = self.target.run('logrotate -vf /tmp/logrotate-test.conf') |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 44 | status, _ = self.target.run('find /var/log//logrotate_dir -type f | grep wtmp.1') |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 45 | msg = ("logrotate did not successfully rotate the wtmp log. Output from logrotate -vf: \n%s" % (logrotate_output)) |
| 46 | self.assertEqual(status, 0, msg = msg) |
| 47 | |
| 48 | @OETestDepends(['logrotate.LogrotateTest.test_logrotate_wtmp']) |
| 49 | def test_logrotate_newlog(self): |
| 50 | |
| 51 | status, output = self.target.run('echo "oeqa logrotate test file" > /var/log/logrotate_testfile') |
| 52 | msg = ('Could not create logrotate test file in /var/log') |
| 53 | self.assertEqual(status, 0, msg = msg) |
| 54 | |
| 55 | status, output = self.target.run('echo "/var/log/logrotate_testfile {\n missingok \n monthly \n rotate 1" > /etc/logrotate.d/logrotate_testfile') |
| 56 | msg = ('Could not write to /etc/logrotate.d/logrotate_testfile') |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 57 | self.assertEqual(status, 0, msg = msg) |
| 58 | |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 59 | status, output = self.target.run('echo "create \n olddir /var/log//logrotate_dir \n include /etc/logrotate.d/logrotate_testfile" > /tmp/logrotate-test2.conf') |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 60 | msg = ('Could not write to /tmp/logrotate_test2.conf') |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 61 | self.assertEqual(status, 0, msg = msg) |
| 62 | |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 63 | status, output = self.target.run('find /var/log//logrotate_dir -type f | grep logrotate_testfile.1') |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 64 | msg = ('A rotated log for logrotate_testfile is already present in logrotate_dir') |
| 65 | self.assertEqual(status, 1, msg = msg) |
| 66 | |
| 67 | # If logrotate fails to rotate the log, view the verbose output of logrotate instead of just listing the files in olddir |
| 68 | _, logrotate_output = self.target.run('logrotate -vf /tmp/logrotate-test2.conf') |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 69 | status, _ = self.target.run('find /var/log//logrotate_dir -type f | grep logrotate_testfile.1') |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 70 | msg = ('logrotate did not successfully rotate the logrotate_test log. Output from logrotate -vf: \n%s' % (logrotate_output)) |
| 71 | self.assertEqual(status, 0, msg = msg) |
| 72 | |
| 73 | |