blob: 6ad980cb6adc0cc1660989228442633db3a9827d [file] [log] [blame]
Brad Bishopc342db32019-05-15 21:57:59 -04001#
Patrick Williams92b42cb2022-09-03 06:53:57 -05002# Copyright OpenEmbedded Contributors
3#
Brad Bishopc342db32019-05-15 21:57:59 -04004# SPDX-License-Identifier: MIT
5#
6
Brad Bishop6e60e8b2018-02-01 10:27:11 -05007# 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
10from oeqa.runtime.case import OERuntimeTestCase
11from oeqa.core.decorator.depends import OETestDepends
Brad Bishop6e60e8b2018-02-01 10:27:11 -050012from oeqa.runtime.decorator.package import OEHasPackage
13
14class LogrotateTest(OERuntimeTestCase):
15
16 @classmethod
Andrew Geissler99467da2019-02-25 18:54:23 -060017 def setUpClass(cls):
18 cls.tc.target.run('cp /etc/logrotate.d/wtmp $HOME/wtmp.oeqabak')
19
20 @classmethod
Brad Bishop6e60e8b2018-02-01 10:27:11 -050021 def tearDownClass(cls):
Andrew Geissler595f6302022-01-24 19:11:47 +000022 cls.tc.target.run('mv -f $HOME/wtmp.oeqabak /etc/logrotate.d/wtmp && rm -rf /var/log//logrotate_dir')
Andrew Geissler82c905d2020-04-13 13:39:40 -050023 cls.tc.target.run('rm -rf /var/log/logrotate_testfile && rm -rf /etc/logrotate.d/logrotate_testfile')
Brad Bishop6e60e8b2018-02-01 10:27:11 -050024
Brad Bishop6e60e8b2018-02-01 10:27:11 -050025 @OETestDepends(['ssh.SSHTest.test_ssh'])
26 @OEHasPackage(['logrotate'])
Andrew Geissler82c905d2020-04-13 13:39:40 -050027 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 Geissler595f6302022-01-24 19:11:47 +000034 status, output = self.target.run('mkdir /var/log//logrotate_dir')
Andrew Geissler82c905d2020-04-13 13:39:40 -050035 msg = ('Could not create logrotate_dir. Output: %s' % output)
Brad Bishop6e60e8b2018-02-01 10:27:11 -050036 self.assertEqual(status, 0, msg = msg)
37
Andrew Geissler595f6302022-01-24 19:11:47 +000038 status, output = self.target.run('echo "create \n olddir /var/log//logrotate_dir \n include /etc/logrotate.d/wtmp" > /tmp/logrotate-test.conf')
Andrew Geissler82c905d2020-04-13 13:39:40 -050039 msg = ('Could not write to /tmp/logrotate-test.conf')
40 self.assertEqual(status, 0, msg = msg)
41
Andrew Geissler82c905d2020-04-13 13:39:40 -050042 # 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 Geissler595f6302022-01-24 19:11:47 +000044 status, _ = self.target.run('find /var/log//logrotate_dir -type f | grep wtmp.1')
Andrew Geissler82c905d2020-04-13 13:39:40 -050045 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 Bishop6e60e8b2018-02-01 10:27:11 -050057 self.assertEqual(status, 0, msg = msg)
58
Andrew Geissler595f6302022-01-24 19:11:47 +000059 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 Geissler82c905d2020-04-13 13:39:40 -050060 msg = ('Could not write to /tmp/logrotate_test2.conf')
Brad Bishop6e60e8b2018-02-01 10:27:11 -050061 self.assertEqual(status, 0, msg = msg)
62
Andrew Geissler595f6302022-01-24 19:11:47 +000063 status, output = self.target.run('find /var/log//logrotate_dir -type f | grep logrotate_testfile.1')
Andrew Geissler82c905d2020-04-13 13:39:40 -050064 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 Geissler595f6302022-01-24 19:11:47 +000069 status, _ = self.target.run('find /var/log//logrotate_dir -type f | grep logrotate_testfile.1')
Andrew Geissler82c905d2020-04-13 13:39:40 -050070 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