-
Notifications
You must be signed in to change notification settings - Fork 54
Allow asserting againt TestBase log file #384
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
bb75df6
d99c17b
f23ff7b
2f8e64c
040a566
8742f7a
d8d2184
c1c6d27
b35cfb2
be345fe
c6539c6
9f2279e
d4fde4a
9e16a20
52f04c9
f9c5713
96c7978
4098f6e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,9 +3,10 @@ | |
| import logging | ||
| import logging.config | ||
| import os | ||
| import re | ||
| import threading | ||
| from pathlib import Path | ||
| from subprocess import PIPE, STDOUT, Popen, run | ||
| from subprocess import PIPE, Popen, run | ||
| from tempfile import mkdtemp | ||
| from time import sleep | ||
|
|
||
|
|
@@ -20,6 +21,9 @@ def __init__(self): | |
| self.setup_environment() | ||
| self.setup_logging() | ||
| atexit.register(self.cleanup) | ||
| self.log_expected_msgs: None | [str] = None | ||
| self.log_unexpected_msgs: None | [str] = None | ||
| self.log_msg_assertions_passed = False | ||
| self.log.info("Warnet test base initialized") | ||
|
|
||
| def setup_environment(self): | ||
|
|
@@ -59,6 +63,19 @@ def cleanup(self, signum=None, frame=None): | |
| self.server_thread.join() | ||
| self.server = None | ||
|
|
||
| def _print_and_assert_msgs(self, message): | ||
| print(message) | ||
| if (self.log_expected_msgs or self.log_unexpected_msgs) and assert_log( | ||
| message, self.log_expected_msgs, self.log_unexpected_msgs | ||
| ): | ||
| self.log_msg_assertions_passed = True | ||
|
|
||
| def assert_log_msgs(self): | ||
| assert ( | ||
| self.log_msg_assertions_passed | ||
| ), f"Log assertion failed. Expected message not found: {self.log_expected_msgs}" | ||
| self.log_msg_assertions_passed = False | ||
|
Comment on lines
+73
to
+77
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I ran a test with an unexpected message which aborted early, as expected. Funny thing though, because it exited early the test never got to the part where it prints the expected message. So even though its true the expected message was not found in the log, the more relevant error is the unexpected message which occurred first. I'm not sure if there's anything we really need to change about this, unless you have an idea:
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like the unexpected message AssertionError gets buried in the log, and the less meaningful message not found AssertionError gets highlighted by virtue of being the last message. To fix this, I would probably use an enum named "Found": Nothing, Expected, Unexpected, and UnexpectedExpected. Then lift the assertion call out of the assert_log helper function and plug it into the TestBase's asset_log_msgs function. Then I would have the helper assert_log functions return the enum. |
||
|
|
||
| def warcli(self, cmd, network=True): | ||
| self.log.debug(f"Executing warcli command: {cmd}") | ||
| command = ["warcli"] + cmd.split() | ||
|
|
@@ -94,19 +111,19 @@ def start_server(self): | |
| # TODO: check for conflicting warnet process | ||
| # maybe also ensure that no conflicting docker networks exist | ||
|
|
||
| # For kubernetes we assume the server is started outside test base | ||
| # For kubernetes we assume the server is started outside test base, | ||
| # but we can still read its log output | ||
| self.log.info("Starting Warnet server") | ||
| self.server = Popen( | ||
| ["kubectl", "logs", "-f", "rpc-0"], | ||
| ["kubectl", "logs", "-f", "rpc-0", "--since=1s"], | ||
| stdout=PIPE, | ||
| stderr=STDOUT, | ||
| stderr=PIPE, | ||
| bufsize=1, | ||
| universal_newlines=True, | ||
| ) | ||
|
|
||
| self.server_thread = threading.Thread( | ||
| target=self.output_reader, args=(self.server.stdout, print) | ||
| target=self.output_reader, args=(self.server.stdout, self._print_and_assert_msgs) | ||
| ) | ||
| self.server_thread.daemon = True | ||
| self.server_thread.start() | ||
|
|
@@ -176,3 +193,26 @@ def get_scenario_return_code(self, scenario_name): | |
| if len(scns) == 0: | ||
| raise Exception(f"Scenario {scenario_name} not found in running scenarios") | ||
| return scns[0]["return_code"] | ||
|
|
||
|
|
||
| def assert_equal(thing1, thing2, *args): | ||
| if thing1 != thing2 or any(thing1 != arg for arg in args): | ||
| raise AssertionError( | ||
| "not({})".format(" == ".join(str(arg) for arg in (thing1, thing2) + args)) | ||
| ) | ||
|
|
||
|
|
||
| def assert_log(log_message, expected_msgs, unexpected_msgs=None) -> bool: | ||
| if unexpected_msgs is None: | ||
| unexpected_msgs = [] | ||
| assert_equal(type(expected_msgs), list) | ||
| assert_equal(type(unexpected_msgs), list) | ||
|
|
||
| found = True | ||
| for unexpected_msg in unexpected_msgs: | ||
| if re.search(re.escape(unexpected_msg), log_message, flags=re.MULTILINE): | ||
| raise AssertionError(f"Unexpected message found in log: {unexpected_msg}") | ||
mplsgrant marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| for expected_msg in expected_msgs: | ||
| if re.search(re.escape(expected_msg), log_message, flags=re.MULTILINE) is None: | ||
| found = False | ||
mplsgrant marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return found | ||
Uh oh!
There was an error while loading. Please reload this page.