Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 1 | # |
| 2 | # SPDX-License-Identifier: MIT |
| 3 | # |
| 4 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 5 | from oeqa.runtime.case import OERuntimeTestCase |
| 6 | from oeqa.core.decorator.depends import OETestDepends |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 7 | from oeqa.core.decorator.data import skipIfDataVar |
| 8 | from oeqa.runtime.decorator.package import OEHasPackage |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 9 | import time |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 10 | |
| 11 | class SyslogTest(OERuntimeTestCase): |
| 12 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 13 | @OETestDepends(['ssh.SSHTest.test_ssh']) |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 14 | @OEHasPackage(["busybox-syslog", "sysklogd", "rsyslog", "syslog-ng"]) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 15 | def test_syslog_running(self): |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 16 | status, output = self.target.run(self.tc.target_cmds['ps']) |
| 17 | msg = "Failed to execute %s" % self.tc.target_cmds['ps'] |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 18 | self.assertEqual(status, 0, msg=msg) |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 19 | msg = "No syslog daemon process; %s output:\n%s" % (self.tc.target_cmds['ps'], output) |
Brad Bishop | 79641f2 | 2019-09-10 07:20:22 -0400 | [diff] [blame] | 20 | hasdaemon = "syslogd" in output or "syslog-ng" in output or "svlogd" in output |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 21 | self.assertTrue(hasdaemon, msg=msg) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 22 | |
| 23 | class SyslogTestConfig(OERuntimeTestCase): |
| 24 | |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 25 | 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 Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 82 | @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 Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 88 | # There is no way to flush the logger to disk in all cases |
| 89 | time.sleep(1) |
| 90 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 91 | 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 Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 101 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 102 | @OETestDepends(['oe_syslog.SyslogTest.test_syslog_running']) |
| 103 | def test_syslog_restart(self): |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 104 | 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 Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 110 | else: |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 111 | self.logger.info("No syslog found to restart, ignoring") |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 112 | |
| 113 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 114 | @OETestDepends(['oe_syslog.SyslogTestConfig.test_syslog_logger']) |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 115 | @OEHasPackage(["busybox-syslog"]) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 116 | @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 Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 121 | |
| 122 | self.test_syslog_restart() |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 123 | |
| 124 | cmd = 'logger foobar && grep foobar /var/log/test' |
| 125 | status,output = self.target.run(cmd) |
| 126 | msg = 'Test log string not found. Output: %s ' % output |
| 127 | self.assertEqual(status, 0, msg=msg) |
| 128 | |
| 129 | cmd = "sed -i 's#LOGFILE=/var/log/test##' /etc/syslog-startup.conf" |
| 130 | self.target.run(cmd) |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 131 | self.test_syslog_restart() |