summaryrefslogtreecommitdiffhomepage
path: root/test/unit/main.py
diff options
context:
space:
mode:
Diffstat (limited to 'test/unit/main.py')
-rw-r--r--test/unit/main.py175
1 files changed, 93 insertions, 82 deletions
diff --git a/test/unit/main.py b/test/unit/main.py
index 69234dcc..4507f71a 100644
--- a/test/unit/main.py
+++ b/test/unit/main.py
@@ -4,6 +4,7 @@ import sys
import stat
import time
import fcntl
+import atexit
import shutil
import signal
import argparse
@@ -151,48 +152,6 @@ class TestUnit(unittest.TestCase):
def setUp(self):
self._run()
- def tearDown(self):
- self.stop()
-
- # detect errors and failures for current test
-
- def list2reason(exc_list):
- if exc_list and exc_list[-1][0] is self:
- return exc_list[-1][1]
-
- if hasattr(self, '_outcome'):
- result = self.defaultTestResult()
- self._feedErrorsToResult(result, self._outcome.errors)
- else:
- result = getattr(
- self, '_outcomeForDoCleanups', self._resultForDoCleanups
- )
-
- success = not list2reason(result.errors) and not list2reason(
- result.failures
- )
-
- # check unit.log for alerts
-
- unit_log = self.testdir + '/unit.log'
-
- with open(unit_log, 'r', encoding='utf-8', errors='ignore') as f:
- self._check_alerts(f.read())
-
- # remove unit.log
-
- if not TestUnit.save_log and success:
- shutil.rmtree(self.testdir)
-
- else:
- self._print_log()
-
- def stop(self):
- if self._started:
- self._stop()
-
- self.stop_processes()
-
def _run(self):
build_dir = self.pardir + '/build'
self.unitd = build_dir + '/unitd'
@@ -224,62 +183,81 @@ class TestUnit(unittest.TestCase):
stderr=log,
)
+ atexit.register(self.stop)
+
if not self.waitforfiles(self.testdir + '/control.unit.sock'):
exit("Could not start unit")
- self._started = True
-
self.skip_alerts = [
r'read signalfd\(4\) failed',
- r'last message send failed',
r'sendmsg.+failed',
r'recvmsg.+failed',
]
self.skip_sanitizer = False
- def _stop(self):
- with self._p as p:
- p.send_signal(signal.SIGQUIT)
+ def tearDown(self):
+ stop_errs = self.stop()
- try:
- retcode = p.wait(15)
- if retcode:
- self.fail(
- "Child process terminated with code " + str(retcode)
- )
- except:
- self.fail("Could not terminate unit")
- p.kill()
+ # detect errors and failures for current test
- self._started = False
+ def list2reason(exc_list):
+ if exc_list and exc_list[-1][0] is self:
+ return exc_list[-1][1]
- def _check_alerts(self, log):
- found = False
+ if hasattr(self, '_outcome'):
+ result = self.defaultTestResult()
+ self._feedErrorsToResult(result, self._outcome.errors)
+ else:
+ result = getattr(
+ self, '_outcomeForDoCleanups', self._resultForDoCleanups
+ )
- alerts = re.findall('.+\[alert\].+', log)
+ success = not list2reason(result.errors) and not list2reason(
+ result.failures
+ )
- if alerts:
- print('All alerts/sanitizer errors found in log:')
- [print(alert) for alert in alerts]
- found = True
+ # check unit.log for alerts
- if self.skip_alerts:
- for skip in self.skip_alerts:
- alerts = [al for al in alerts if re.search(skip, al) is None]
+ unit_log = self.testdir + '/unit.log'
- if alerts:
- self._print_log(log)
- self.assertFalse(alerts, 'alert(s)')
+ with open(unit_log, 'r', encoding='utf-8', errors='ignore') as f:
+ self._check_alerts(f.read())
- if not self.skip_sanitizer:
- sanitizer_errors = re.findall('.+Sanitizer.+', log)
+ # remove unit.log
- if sanitizer_errors:
- self._print_log(log)
- self.assertFalse(sanitizer_errors, 'sanitizer error(s)')
+ if not TestUnit.save_log and success:
+ shutil.rmtree(self.testdir)
- if found:
- print('skipped.')
+ else:
+ self._print_log()
+
+ self.assertListEqual(stop_errs, [None, None], 'stop errors')
+
+ def stop(self):
+ errors = []
+
+ errors.append(self._stop())
+
+ errors.append(self.stop_processes())
+
+ atexit.unregister(self.stop)
+
+ return errors
+
+ def _stop(self):
+ if self._p.poll() is not None:
+ return
+
+ with self._p as p:
+ p.send_signal(signal.SIGQUIT)
+
+ try:
+ retcode = p.wait(15)
+ if retcode:
+ return 'Child process terminated with code ' + str(retcode)
+ except:
+ p.kill()
+ return 'Could not terminate unit'
def run_process(self, target, *args):
if not hasattr(self, '_processes'):
@@ -294,12 +272,17 @@ class TestUnit(unittest.TestCase):
if not hasattr(self, '_processes'):
return
+ fail = False
for process in self._processes:
- process.terminate()
- process.join(timeout=5)
-
if process.is_alive():
- self.fail('Fail to stop process')
+ process.terminate()
+ process.join(timeout=15)
+
+ if process.is_alive():
+ fail = True
+
+ if fail:
+ return 'Fail to stop process'
def waitforfiles(self, *files):
for i in range(50):
@@ -329,6 +312,34 @@ class TestUnit(unittest.TestCase):
for f in files:
os.chmod(os.path.join(root, f), 0o777)
+ def _check_alerts(self, log):
+ found = False
+
+ alerts = re.findall('.+\[alert\].+', log)
+
+ if alerts:
+ print('All alerts/sanitizer errors found in log:')
+ [print(alert) for alert in alerts]
+ found = True
+
+ if self.skip_alerts:
+ for skip in self.skip_alerts:
+ alerts = [al for al in alerts if re.search(skip, al) is None]
+
+ if alerts:
+ self._print_log(log)
+ self.assertFalse(alerts, 'alert(s)')
+
+ if not self.skip_sanitizer:
+ sanitizer_errors = re.findall('.+Sanitizer.+', log)
+
+ if sanitizer_errors:
+ self._print_log(log)
+ self.assertFalse(sanitizer_errors, 'sanitizer error(s)')
+
+ if found:
+ print('skipped.')
+
@staticmethod
def _parse_args():
parser = argparse.ArgumentParser(add_help=False)