From 6ccda11a98afd6f4459e9ff1c24de4ad4450de23 Mon Sep 17 00:00:00 2001 From: Neels Hofmeyr Date: Tue, 6 Jun 2017 19:41:17 +0200 Subject: refactor: fix error handling; fix log.Origin; only one trial A bit of refactoring to fix logging and error reporting, and simplify the code. This transmogrifies some of the things committed in 0ffb41440661631fa1d520c152be4cf8ebd4c46b "Add JUnit XML reports; refactor test reporting", which did not fully match the code structuring ideas used in osmo-gsm-tester. Also solve some problems present from the start of the code base. Though this is a bit of a code bomb, it would take a lot of time to separate this into smaller bits: these changes are closely related and resulted incrementally from testing error handling and logging details. I hope it's ok. Things changed / problems fixed: Allow only a single trial to be run per cmdline invocation: unbloat trial and suite invocation in osmo-gsm-tester.py. There is a SuiteDefinition, intended to be immutable, and a mutable SuiteRun. SuiteDefinition had a list of tests, which was modified by the SuiteRun to record test results. Instead, have only the test basenames in the SuiteDefinition and create a new set of Test() instances for each SuiteRun, to ensure that no state leaks between separate suite runs. State leaking across runs can be seen in http://jenkins.osmocom.org/jenkins/view/osmo-gsm-tester/job/osmo-gsm-tester_run/453/ where an earlier sms test for sysmo succeeds, but its state gets overwritten by the later sms test for trx that fails. The end result is that both tests failed, although the first run was successful. Fix a problem with Origin: log.Origin allowed to be __enter__ed more than once, skipping the second entry. The problem there is that we'd still __exit__ twice or more, popping the Origin off the stack even though it should still remain. We could count __enter__ recurrences, but instead, completely disallow entering a second time. A code path should have one 'with' statement per object, at pivotal points like run_suites or run_tests. Individual utility functions should not do 'with' on a central object. The structure needed is, in pseudo code: try: with trial: try: with suite_run: try: with test: test_actions() The 'with' needs to be inside the 'try', so that the exception can be handled in __exit__ before it reaches the exception logging. To clarify this, like test exceptions caught in Test.run(), also move suite exception handling from Trial into SuiteRun.run_tests(). There are 'with self' in Test.run() and SuiteRun.run_tests(), which are well placed, because these are pivotal points in the main code path. Log output: clearly separate logging of distinct suites and test scripts, by adding more large_separator() calls at the start of each test. Place these separator calls in more logical places. Add separator size and spacing args. Log output: print tracebacks only once, for the test script where they happen. Have less state that duplicates other state: drop SuiteRun.test_failed_ctr and suite.test_skipped_ctr, instead add SuiteRun.count_test_results(). For test failure reporting, store the traceback text in a separate member var. In the text report, apply above changes and unclutter to achieve a brief and easy to read result overview: print less filler characters, drop the starting times, drop the tracebacks. This can be found in the individual test logs. Because the tracebacks are no longer in the text report, the suite_test.py can just print the reports and expect that output instead of asserting individual contents. In the text report, print duration in precision of .1 seconds. Add origin information and a traceback text to the junit XML result to give more context when browsing the result XML. For 'AssertionError', add the source line of where the assertion hit. Drop the explicit Failure exception. We don't need one specific exception to mark a failure, instead any arbitrary exception is treated as a failure. Use the exception's class name as fail_type. Though my original idea was to use raising exceptions as the only way to cause a test failure, I'm keeping the set_fail() function as an alternative way, because it allows test specific cleanup and may come in handy later. To have both ways integrate seamlessly, shift some result setting into 'finally' clauses and make sure higher levels (suite, trial) count the contained items' stati. Minor tweak: write the 'pass' and 'skip' reports in lower case so that the 'FAIL' stands out. Minor tweak: pass the return code that the program exit should return further outward, so that the exit(1) call does not cause a SystemExit exception to be logged. The aims of this patch are: - Logs are readable so that it is clear which logging belongs to which test and suite. - The logging origins are correct (vs. parents gone missing as previously) - A single test error does not cause following tests or suites to be skipped. - An exception "above" Exception, i.e. SystemExit and the like, *does* immediately abort all tests and suites, and the results for tests that were not run are reported as "unknown" (rather than skipped on purpose): - Raising a SystemExit aborts all. - Hitting ctrl-c aborts all. - The resulting summary in the log is brief and readable. Change-Id: Ibf0846d457cab26f54c25e6906a8bb304724e2d8 --- src/osmo_gsm_tester/log.py | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) (limited to 'src/osmo_gsm_tester/log.py') diff --git a/src/osmo_gsm_tester/log.py b/src/osmo_gsm_tester/log.py index fb6d55b..f098f2b 100644 --- a/src/osmo_gsm_tester/log.py +++ b/src/osmo_gsm_tester/log.py @@ -206,13 +206,16 @@ class LogTarget: log_str = log_str + '\n' self.log_write_func(log_str) - def large_separator(self, *msgs): + def large_separator(self, *msgs, sublevel=1, space_above=True): + sublevel = max(1, min(3, sublevel)) msg = ' '.join(msgs) + sep = '-' * int(23 * (5 - sublevel)) if not msg: - msg = '------------------------------------------' - self.log_write_func('------------------------------------------\n' - '%s\n' - '------------------------------------------\n' % msg) + msg = sep + lines = [sep, msg, sep, ''] + if space_above: + lines.insert(0, '') + self.log_write_func('\n'.join(lines)) def level_str(level): if level == L_TRACEBACK: @@ -231,9 +234,9 @@ def _log_all_targets(origin, category, level, src, messages, named_items=None): for target in LogTarget.all_targets: target.log(origin, category, level, src, messages, named_items) -def large_separator(*msgs): +def large_separator(*msgs, sublevel=1, space_above=True): for target in LogTarget.all_targets: - target.large_separator(*msgs) + target.large_separator(*msgs, sublevel=sublevel, space_above=space_above) def get_src_from_caller(levels_up=1): caller = getframeinfo(stack()[levels_up][0]) @@ -327,6 +330,9 @@ class Origin: def err(self, *messages, **named_items): self._log(L_ERR, messages, named_items) + def trace(self, *messages, **named_items): + self._log(L_TRACEBACK, messages, named_items) + def log_exn(self, exc_info=None): log_exn(self, self._log_category, exc_info) @@ -373,10 +379,8 @@ class Origin: def set_child_of(self, parent_origin): # avoid loops - if self._parent_origin is not None: - return False - if parent_origin == self: - return False + assert self._parent_origin is None + assert parent_origin is not self self._parent_origin = parent_origin return True -- cgit v1.2.3