Skip to content

Commit

Permalink
tests: fix spawning shells/vtysh on error in xdist mode
Browse files Browse the repository at this point in the history
- Also fix the above and CLI when running in munet native mode

Signed-off-by: Christian Hopps <[email protected]>
  • Loading branch information
choppsv1 committed Sep 14, 2024
1 parent 68f25a1 commit 98aaeab
Showing 1 changed file with 27 additions and 11 deletions.
38 changes: 27 additions & 11 deletions tests/topotests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@
import lib.fixtures
import pytest
from lib.common_config import generate_support_bundle
from lib.micronet_compat import Mininet
from lib.topogen import diagnose_env, get_topogen
from lib.topolog import get_test_logdir, logger
from lib.topotest import json_cmp_result
from munet import cli
from munet.base import Commander, proc_error
from munet.base import BaseMunet, Commander, proc_error
from munet.cleanup import cleanup_current, cleanup_previous
from munet.config import ConfigOptionsProxy
from munet.testing.util import pause_test
Expand Down Expand Up @@ -86,7 +85,7 @@ def pytest_addoption(parser):
parser.addoption(
"--cli-on-error",
action="store_true",
help="Mininet cli on test failure",
help="Munet cli on test failure",
)

parser.addoption(
Expand Down Expand Up @@ -711,7 +710,7 @@ def pytest_runtest_makereport(item, call):
wait_for_procs = []
# Really would like something better than using this global here.
# Not all tests use topogen though so get_topogen() won't work.
for node in Mininet.g_mnet_inst.hosts.values():
for node in BaseMunet.g_unet.hosts.values():
pause = True

if is_tmux:
Expand All @@ -720,13 +719,15 @@ def pytest_runtest_makereport(item, call):
if not isatty
else None
)
Commander.tmux_wait_gen += 1
wait_for_channels.append(channel)
# If we don't have a tty to pause on pause for tmux windows to exit
if channel is not None:
Commander.tmux_wait_gen += 1
wait_for_channels.append(channel)

pane_info = node.run_in_window(
error_cmd,
new_window=win_info is None,
background=True,
background=not isatty,
title="{} ({})".format(title, node.name),
name=title,
tmux_target=win_info,
Expand All @@ -737,9 +738,13 @@ def pytest_runtest_makereport(item, call):
win_info = pane_info
elif is_xterm:
assert isinstance(pane_info, subprocess.Popen)
wait_for_procs.append(pane_info)
# If we don't have a tty to pause on pause for xterm procs to exit
if not isatty:
wait_for_procs.append(pane_info)

# Now wait on any channels
if wait_for_channels or wait_for_procs:
logger.info("Pausing for error command windows to exit")
for channel in wait_for_channels:
logger.debug("Waiting on TMUX channel %s", channel)
commander.cmd_raises([commander.get_exec_path("tmux"), "wait", channel])
Expand All @@ -752,10 +757,10 @@ def pytest_runtest_makereport(item, call):
if error and item.config.option.cli_on_error:
# Really would like something better than using this global here.
# Not all tests use topogen though so get_topogen() won't work.
if Mininet.g_mnet_inst:
cli.cli(Mininet.g_mnet_inst, title=title, background=False)
if BaseMunet.g_unet:
cli.cli(BaseMunet.g_unet, title=title, background=False)
else:
logger.error("Could not launch CLI b/c no mininet exists yet")
logger.error("Could not launch CLI b/c no munet exists yet")

if pause and isatty:
pause_test()
Expand Down Expand Up @@ -800,9 +805,20 @@ def coverage_finish(terminalreporter, config):
def pytest_terminal_summary(terminalreporter, exitstatus, config):
# Only run if we are the top level test runner
is_xdist_worker = "PYTEST_XDIST_WORKER" in os.environ
is_xdist = os.environ["PYTEST_XDIST_MODE"] != "no"
if config.option.cov_topotest and not is_xdist_worker:
coverage_finish(terminalreporter, config)

if (
is_xdist
and not is_xdist_worker
and (
bool(config.getoption("--pause"))
or bool(config.getoption("--pause-at-end"))
)
):
pause_test("pause-at-end")


#
# Add common fixtures available to all tests as parameters
Expand Down

0 comments on commit 98aaeab

Please sign in to comment.