Skip to content

Commit 7f62fcb

Browse files
ryanofskySjors
authored andcommitted
test framework: expand expected_stderr, expected_ret_code options
Let expected_stderr option passed to wait_until_stopped and is_node_stopped helper functions be a regex pattern instead of just a fixed string. Let expected_ret_code be list of possible exit codes instead of a single error code to handle the case where exit codes vary depending on OS and libc.
1 parent 07b9247 commit 7f62fcb

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

test/functional/test_framework/test_node.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import shlex
2323
import shutil
2424
import sys
25+
from collections.abc import Iterable
2526
from pathlib import Path
2627

2728
from .authproxy import (
@@ -469,6 +470,12 @@ def is_node_stopped(self, *, expected_stderr="", expected_ret_code=0):
469470
"""Checks whether the node has stopped.
470471
471472
Returns True if the node has stopped. False otherwise.
473+
474+
If the process has exited, asserts that the exit code matches
475+
`expected_ret_code` (which may be a single value or an iterable of values),
476+
and that stderr matches `expected_stderr` exactly or, if a regex pattern is
477+
provided, contains the pattern.
478+
472479
This method is responsible for freeing resources (self.process)."""
473480
if not self.running:
474481
return True
@@ -477,12 +484,17 @@ def is_node_stopped(self, *, expected_stderr="", expected_ret_code=0):
477484
return False
478485

479486
# process has stopped. Assert that it didn't return an error code.
480-
assert return_code == expected_ret_code, self._node_msg(
487+
if not isinstance(expected_ret_code, Iterable):
488+
expected_ret_code = (expected_ret_code,)
489+
assert return_code in expected_ret_code, self._node_msg(
481490
f"Node returned unexpected exit code ({return_code}) vs ({expected_ret_code}) when stopping")
482491
# Check that stderr is as expected
483492
self.stderr.seek(0)
484493
stderr = self.stderr.read().decode('utf-8').strip()
485-
if stderr != expected_stderr:
494+
if isinstance(expected_stderr, re.Pattern):
495+
if not expected_stderr.search(stderr):
496+
raise AssertionError(f"Unexpected stderr {stderr!r} does not contain {expected_stderr.pattern!r}")
497+
elif stderr != expected_stderr:
486498
raise AssertionError("Unexpected stderr {} != {}".format(stderr, expected_stderr))
487499

488500
self.stdout.close()

0 commit comments

Comments
 (0)