1#
2# Copyright OpenEmbedded Contributors
3#
4# SPDX-License-Identifier: MIT
5#
6
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
10from oeqa.runtime.case import OERuntimeTestCase
11from oeqa.core.decorator.depends import OETestDepends
12from oeqa.runtime.decorator.package import OEHasPackage
13
14class LogrotateTest(OERuntimeTestCase):
15
16    @classmethod
17    def setUpClass(cls):
18        cls.tc.target.run('cp /etc/logrotate.d/wtmp $HOME/wtmp.oeqabak')
19
20    @classmethod
21    def tearDownClass(cls):
22        cls.tc.target.run('mv -f $HOME/wtmp.oeqabak /etc/logrotate.d/wtmp && rm -rf /var/log//logrotate_dir')
23        cls.tc.target.run('rm -rf /var/log/logrotate_testfile && rm -rf /etc/logrotate.d/logrotate_testfile')
24
25    @OETestDepends(['ssh.SSHTest.test_ssh'])
26    @OEHasPackage(['logrotate'])
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
34        status, output = self.target.run('mkdir /var/log//logrotate_dir')
35        msg = ('Could not create logrotate_dir. Output: %s' % output)
36        self.assertEqual(status, 0, msg = msg)
37
38        status, output = self.target.run('echo "create \n olddir /var/log//logrotate_dir \n include /etc/logrotate.d/wtmp" > /tmp/logrotate-test.conf')
39        msg = ('Could not write to /tmp/logrotate-test.conf')
40        self.assertEqual(status, 0, msg = msg)
41
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')
44        status, _ = self.target.run('find /var/log//logrotate_dir -type f | grep wtmp.1')
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')
57        self.assertEqual(status, 0, msg = msg)
58
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')
60        msg = ('Could not write to /tmp/logrotate_test2.conf')
61        self.assertEqual(status, 0, msg = msg)
62
63        status, output = self.target.run('find /var/log//logrotate_dir -type f | grep logrotate_testfile.1')
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')
69        status, _ = self.target.run('find /var/log//logrotate_dir -type f | grep logrotate_testfile.1')
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
74