Skip to content

Commit

Permalink
add missing return
Browse files Browse the repository at this point in the history
I noticed that I did not include a `return` if the user simply searches for a non-existent pod. So,
I added the `return` statement. In order to make a test for this, I realized I would need to parse
the output of `warnet logs` to search for "Traceback (" in order to see if there is some kind of
error. Otherwise, if I simple "expect" a message, the test will pass even though the program errors
out with some kind of stack trace.

That is why I made `expect_without_traceback`. It will catch the missing `return` statement. I will
add expect_without_traceback to my other pexpect statements in the next commit.
  • Loading branch information
mplsgrant committed Oct 17, 2024
1 parent 5eb0c81 commit 94a8429
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/warnet/control.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@ def format_pods(pods: list[V1Pod], namespace: Optional[str]) -> list[str]:

if not namespaces:
click.echo(f"Could not find pod in any namespaces: {pod_name}")
return

namespace = namespaces[0]

Expand Down
31 changes: 31 additions & 0 deletions test/namespace_admin_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,11 @@ def bob_checks_logs(self):
)
self.sut.expect(bitcoin_version_slug, timeout=10)
self.sut.close()
self.sut = pexpect.spawn("warnet logs this_does_not_exist", maxread=4096 * 10)
assert expect_without_traceback(
"Could not find pod in any namespaces", self.sut, timeout=10
)
self.sut.close()
self.log.info("Bob has checked the logs")
assert self.this_is_the_current_context(self.bob_context)

Expand All @@ -226,6 +231,11 @@ def admin_checks_logs(self):
)
self.sut.expect(bitcoin_version_slug, timeout=10)
self.sut.close()
self.sut = pexpect.spawn("warnet logs this_does_not_exist", maxread=4096 * 10)
assert expect_without_traceback(
"Could not find pod in any namespaces", self.sut, timeout=10
)
self.sut.close()
self.log.info("The admin has checked the logs")
assert self.this_is_the_current_context(self.initial_context)

Expand All @@ -244,6 +254,27 @@ def remove_context(kubeconfig_data: dict, context_name: str) -> dict:
return kubeconfig_data


class StackTraceFoundException(Exception):
"""Custom exception raised when a stack trace is found in the output."""

pass


def expect_without_traceback(expectation: str, sut: pexpect.spawn, timeout: int = 10) -> bool:
expectation_found = False
while True:
try:
sut.expect("\n", timeout=timeout)
line = sut.before.decode("utf-8")
if "Traceback (" in line:
raise StackTraceFoundException
if expectation in line:
expectation_found = True
except pexpect.exceptions.EOF:
break
return expectation_found


if __name__ == "__main__":
test = NamespaceAdminTest()
test.run_test()

0 comments on commit 94a8429

Please sign in to comment.