Testing use SSH fails if not deactivation when a previous test reboots #801
-
I came across a weird behavior that was counterintuitive when writing a test that cause the remote device to reboot. Given the following test file: # strategy is "ShellStrategy"
import pytest
@pytest.fixture(scope="function")
def shell_command(target, strategy, capsys):
with capsys.disabled():
strategy.transition("shell")
return target.get_active_driver("ShellDriver")
@pytest.fixture(scope="function")
def ssh_command(target, strategy, capsys):
with capsys.disabled():
strategy.transition("shell")
try:
return target.get_driver("SSHDriver")
except NoDriverFoundError:
pytest.skip("No SSHDriver found")
def test_network(ssh_command):
ssh_command.run_check("true")
def test_reboot(shell_command, strategy):
shell_command.run_check("true")
strategy.transition("off")
shell_command.target.deactivate(shell_command)
strategy.transition("shell")
shell_command.target.activate(shell_command)
shell_command.run_check("true")
def test_post_network(ssh_command):
# This test fails because the ssh_command fixture was not deactivated
ssh_command.run_check("true") The |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
The intention is to not deactivate every single driver, but let the strategy handle the this via invalidation. labgrid/labgrid/strategy/graphstrategy.py Lines 101 to 110 in a6571a4 the important part being that a call to invalidate also deactivates all drivers. The strategies are really only included within labgrid as a starting point for your own individual strategy which is device specific. We include some basic ones and the Invalidate for your strategy could look like this: def invalidate(self):
self.status = Status.off # off instead of unknown to skip the power cycle and use a real reboot.
self.target.deactivate_all_drivers() Given your example above and an implementation for # strategy based on "ShellStrategy" with invalidate implemented
import pytest
@pytest.fixture(scope="function")
def shell_command(target, strategy, capsys):
with capsys.disabled():
strategy.transition("shell")
return target.get_active_driver("ShellDriver")
@pytest.fixture(scope="function")
def ssh_command(target, strategy, capsys):
with capsys.disabled():
strategy.transition("shell")
try:
return target.get_driver("SSHDriver")
except NoDriverFoundError:
pytest.skip("No SSHDriver found")
def test_network(ssh_command):
ssh_command.run_check("true")
def test_reboot(shell_command, strategy):
shell_command.run_check("true")
strategy.console.sendline("reboot") # Since reboot won't return to a shell (could also be sent using SSH)
strategy.invalidate()
strategy.transition("shell")
shell_command.run_check("true")
def test_post_network(ssh_command):
# This test should no longer fail
ssh_command.run_check("true") |
Beta Was this translation helpful? Give feedback.
The intention is to not deactivate every single driver, but let the strategy handle the this via invalidation.
As an example, the
Graphstrategy
implementsinvalidate
like thislabgrid/labgrid/strategy/graphstrategy.py
Lines 101 to 110 in a6571a4
the important part being that a call to invalidate also deactivates all drivers.
The …