blob: 150b70d9f0336b62dff678a45ac2c59bf5251d93 [file] [log] [blame]
Brad Bishopc342db32019-05-15 21:57:59 -04001#
2# SPDX-License-Identifier: MIT
3#
4
Brad Bishop6e60e8b2018-02-01 10:27:11 -05005from oeqa.runtime.case import OERuntimeTestCase
6from oeqa.core.decorator.depends import OETestDepends
Brad Bishop6e60e8b2018-02-01 10:27:11 -05007from oeqa.core.decorator.data import skipIfDataVar
8from oeqa.runtime.decorator.package import OEHasPackage
Brad Bishop96ff1982019-08-19 13:50:42 -04009import time
Brad Bishop6e60e8b2018-02-01 10:27:11 -050010
11class SyslogTest(OERuntimeTestCase):
12
Brad Bishop6e60e8b2018-02-01 10:27:11 -050013 @OETestDepends(['ssh.SSHTest.test_ssh'])
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080014 @OEHasPackage(["busybox-syslog", "sysklogd", "rsyslog", "syslog-ng"])
Brad Bishop6e60e8b2018-02-01 10:27:11 -050015 def test_syslog_running(self):
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080016 status, output = self.target.run(self.tc.target_cmds['ps'])
17 msg = "Failed to execute %s" % self.tc.target_cmds['ps']
Brad Bishop6e60e8b2018-02-01 10:27:11 -050018 self.assertEqual(status, 0, msg=msg)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080019 msg = "No syslog daemon process; %s output:\n%s" % (self.tc.target_cmds['ps'], output)
Brad Bishop79641f22019-09-10 07:20:22 -040020 hasdaemon = "syslogd" in output or "syslog-ng" in output or "svlogd" in output
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080021 self.assertTrue(hasdaemon, msg=msg)
Brad Bishop6e60e8b2018-02-01 10:27:11 -050022
23class SyslogTestConfig(OERuntimeTestCase):
24
Brad Bishop96ff1982019-08-19 13:50:42 -040025 def verif_not_running(self, pids):
26 for pid in pids:
27 status, err_output = self.target.run('kill -0 %s' %pid)
28 if not status:
29 self.logger.debug("previous %s is still running" %pid)
30 return 1
31
32 def verify_running(self, names):
33 pids = []
34 for name in names:
35 status, pid = self.target.run('pidof %s' %name)
36 if status:
37 self.logger.debug("%s is not running" %name)
38 return 1, pids
39 pids.append(pid)
40 return 0, pids
41
42
43 def restart_sanity(self, names, restart_cmd, pidchange=True):
44 status, original_pids = self.verify_running(names)
45 if status:
46 return False
47
48 status, output = self.target.run(restart_cmd)
49
50 msg = ('Could not restart %s service. Status and output: %s and %s' % (names, status, output))
51 self.assertEqual(status, 0, msg)
52
53 if not pidchange:
54 return True
55
56 # Always check for an error, most likely a race between shutting down and starting up
57 timeout = time.time() + 30
58
59 restarted = False
60 status = ""
61 while time.time() < timeout:
62 # Verify the previous ones are no longer running
63 status = self.verif_not_running(original_pids)
64 if status:
65 status = "Original syslog processes still running"
66 continue
67
68 status, pids = self.verify_running(names)
69 if status:
70 status = "New syslog processes not running"
71 continue
72
73 # Everything is fine now, so exit to continue the test
74 restarted = True
75 break
76
77 msg = ('%s didn\'t appear to restart: %s' % (names, status))
78 self.assertTrue(restarted, msg)
79
80 return True
81
Brad Bishop6e60e8b2018-02-01 10:27:11 -050082 @OETestDepends(['oe_syslog.SyslogTest.test_syslog_running'])
83 def test_syslog_logger(self):
84 status, output = self.target.run('logger foobar')
85 msg = "Can't log into syslog. Output: %s " % output
86 self.assertEqual(status, 0, msg=msg)
87
Brad Bishop96ff1982019-08-19 13:50:42 -040088 # There is no way to flush the logger to disk in all cases
89 time.sleep(1)
90
Brad Bishop6e60e8b2018-02-01 10:27:11 -050091 status, output = self.target.run('grep foobar /var/log/messages')
92 if status != 0:
93 if self.tc.td.get("VIRTUAL-RUNTIME_init_manager") == "systemd":
94 status, output = self.target.run('journalctl -o cat | grep foobar')
95 else:
96 status, output = self.target.run('logread | grep foobar')
97 msg = ('Test log string not found in /var/log/messages or logread.'
98 ' Output: %s ' % output)
99 self.assertEqual(status, 0, msg=msg)
100
Brad Bishop96ff1982019-08-19 13:50:42 -0400101
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500102 @OETestDepends(['oe_syslog.SyslogTest.test_syslog_running'])
103 def test_syslog_restart(self):
Brad Bishop96ff1982019-08-19 13:50:42 -0400104 if self.restart_sanity(['systemd-journald'], 'systemctl restart syslog.service', pidchange=False):
105 pass
106 elif self.restart_sanity(['rsyslogd'], '/etc/init.d/rsyslog restart'):
107 pass
108 elif self.restart_sanity(['syslogd', 'klogd'], '/etc/init.d/syslog restart'):
109 pass
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500110 else:
Brad Bishop96ff1982019-08-19 13:50:42 -0400111 self.logger.info("No syslog found to restart, ignoring")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500112
113
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500114 @OETestDepends(['oe_syslog.SyslogTestConfig.test_syslog_logger'])
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800115 @OEHasPackage(["busybox-syslog"])
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500116 @skipIfDataVar('VIRTUAL-RUNTIME_init_manager', 'systemd',
117 'Not appropiate for systemd image')
118 def test_syslog_startup_config(self):
119 cmd = 'echo "LOGFILE=/var/log/test" >> /etc/syslog-startup.conf'
120 self.target.run(cmd)
Brad Bishop96ff1982019-08-19 13:50:42 -0400121
122 self.test_syslog_restart()
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500123
Andrew Geissler7e0e3c02022-02-25 20:34:39 +0000124 cmd = 'logger foobar'
125 status, output = self.target.run(cmd)
126 msg = 'Logger command failed, %s. Output: %s ' % (status, output)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500127 self.assertEqual(status, 0, msg=msg)
128
Andrew Geissler7e0e3c02022-02-25 20:34:39 +0000129 cmd = 'cat /var/log/test'
130 status, output = self.target.run(cmd)
131 if "foobar" not in output or status:
132 self.fail("'foobar' not found in logfile, status %s, contents %s" % (status, output))
133
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500134 cmd = "sed -i 's#LOGFILE=/var/log/test##' /etc/syslog-startup.conf"
135 self.target.run(cmd)
Brad Bishop96ff1982019-08-19 13:50:42 -0400136 self.test_syslog_restart()