1# 2# SPDX-License-Identifier: MIT 3# 4 5from oeqa.runtime.case import OERuntimeTestCase 6from oeqa.core.decorator.depends import OETestDepends 7from oeqa.core.decorator.data import skipIfDataVar 8from oeqa.runtime.decorator.package import OEHasPackage 9import time 10 11class SyslogTest(OERuntimeTestCase): 12 13 @OETestDepends(['ssh.SSHTest.test_ssh']) 14 @OEHasPackage(["busybox-syslog", "sysklogd", "rsyslog", "syslog-ng"]) 15 def test_syslog_running(self): 16 status, output = self.target.run(self.tc.target_cmds['ps']) 17 msg = "Failed to execute %s" % self.tc.target_cmds['ps'] 18 self.assertEqual(status, 0, msg=msg) 19 msg = "No syslog daemon process; %s output:\n%s" % (self.tc.target_cmds['ps'], output) 20 hasdaemon = "syslogd" in output or "syslog-ng" in output or "svlogd" in output 21 self.assertTrue(hasdaemon, msg=msg) 22 23class SyslogTestConfig(OERuntimeTestCase): 24 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 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 88 # There is no way to flush the logger to disk in all cases 89 time.sleep(1) 90 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 101 102 @OETestDepends(['oe_syslog.SyslogTest.test_syslog_running']) 103 def test_syslog_restart(self): 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 110 else: 111 self.logger.info("No syslog found to restart, ignoring") 112 113 114 @OETestDepends(['oe_syslog.SyslogTestConfig.test_syslog_logger']) 115 @OEHasPackage(["busybox-syslog"]) 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) 121 122 self.test_syslog_restart() 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) 131 self.test_syslog_restart() 132