Ejemplo n.º 1
0
def test_keep_alive_context_reconfigured() -> None:
    """
    Reconfiguring a context should leave it in the expected state after restoring the old config.

    This means any machines that were kept alive need to be torn down.
    """
    ctx = tbot.Context(keep_alive=False)
    testmachines.register_machines(ctx)
    with ctx:
        assert tc_context_simple_usage(ctx, "first") == ""
        assert tc_context_simple_usage(ctx, "second") == ""

        # Here, the machine is _not_ alive, so it also shouldn't be after the reconfig ends.
        with ctx.reconfigure(keep_alive=True):
            assert tc_context_simple_usage(ctx, "third") == ""
            assert tc_context_simple_usage(ctx, "fourth") == "third"

        assert tc_context_simple_usage(ctx, "fifth") == ""

        with ctx.request(tbot.role.LabHost) as _:
            assert tc_context_simple_usage(ctx, "sixth") == ""
            assert tc_context_simple_usage(ctx, "seventh") == "sixth"

            # But here, the machine _is_ alive, so it should not be destroyed in this case.
            with ctx.reconfigure(keep_alive=True):
                assert tc_context_simple_usage(ctx, "eighth") == "seventh"

            assert tc_context_simple_usage(ctx, "nineth") == "eighth"
Ejemplo n.º 2
0
def tbot_context() -> Iterator[tbot.Context]:
    tbot.log.VERBOSITY = tbot.log.Verbosity.STDOUT
    tbot.log.NESTING = 1
    with tbot.Context(keep_alive=True, reset_on_error_by_default=True) as ctx:
        testmachines.register_machines(ctx)

        yield ctx
Ejemplo n.º 3
0
def test_illegal_keep_alive_context() -> None:
    """
    A keep-alive context **must** have its own context-manager active.
    """
    ctx = tbot.Context(keep_alive=True)
    with pytest.raises(tbot.error.TbotException):
        with ctx.request(tbot.role.LabHost):
            pass
Ejemplo n.º 4
0
def test_simple_context() -> None:
    """
    Simply attempt requesting an instance two times without any interaction.
    """
    ctx = tbot.Context()
    testmachines.register_machines(ctx)
    with ctx:
        assert tc_context_simple_usage(ctx, "first") == ""
        assert tc_context_simple_usage(ctx, "second") == ""
Ejemplo n.º 5
0
def test_keep_alive_context() -> None:
    """
    A keep-alive context should behave as if it was nested for all instances.
    """
    ctx = tbot.Context(keep_alive=True)
    testmachines.register_machines(ctx)
    with ctx:
        assert tc_context_simple_usage(ctx, "first") == ""
        assert tc_context_simple_usage(ctx, "second") == "first"
        assert tc_context_simple_usage(ctx, "third") == "second"
Ejemplo n.º 6
0
def test_keep_alive_exclusive_context() -> None:
    """
    Test invariants of exclusive access for a keep-alive context.
    """
    ctx = tbot.Context(keep_alive=True)
    testmachines.register_machines(ctx)
    with ctx:
        assert tc_context_simple_usage(ctx, "first") == ""
        assert tc_context_exclusive_usage(ctx, "second") == "first"
        assert tc_context_simple_usage(ctx, "third") == ""
Ejemplo n.º 7
0
def test_keep_alive_resetting_context() -> None:
    """
    Test invariants of resetting access for a keep-alive context.
    """
    ctx = tbot.Context(keep_alive=True)
    testmachines.register_machines(ctx)
    with ctx:
        assert tc_context_simple_usage(ctx, "first") == ""
        tc_context_resetting_usage(ctx, "second")
        assert tc_context_simple_usage(ctx, "third") == "second"
        tc_context_resetting_usage(ctx, "fourth")
Ejemplo n.º 8
0
def test_nested_context() -> None:
    """
    In a nested scenario, the instance should be kept alive and reused for
    later requests.
    """
    ctx = tbot.Context()
    testmachines.register_machines(ctx)
    with ctx:
        with ctx.request(tbot.role.LabHost) as outer:
            assert tc_context_simple_usage(ctx, "first") == ""
            assert outer.env("TBOT_CTX_TESTS") == "first"
            assert tc_context_simple_usage(ctx, "second") == "first"
            assert outer.env("TBOT_CTX_TESTS") == "second"
            assert tc_context_simple_usage(ctx, "third") == "second"
Ejemplo n.º 9
0
def test_reset_on_error_by_default_context() -> None:
    """
    Make sure the reset_on_error mode works as intended, when keep_alive is enabled.
    """
    ctx = tbot.Context(keep_alive=True, reset_on_error_by_default=True)
    testmachines.register_machines(ctx)
    with ctx:
        with pytest.raises(ContextTestException):
            with ctx.request(tbot.role.LabHost) as inner1:
                inner1.env("TBOT_CTX_TESTS", "inner1")
                raise ContextTestException

        with ctx.request(tbot.role.LabHost) as inner2:
            assert inner2.env("TBOT_CTX_TESTS") != "inner1"
Ejemplo n.º 10
0
def test_exclusive_context() -> None:
    """
    An exclusive instance cannot be re-requested and is torn down on context exit.
    """
    ctx = tbot.Context()
    testmachines.register_machines(ctx)
    with ctx:
        with ctx.request(tbot.role.LabHost) as outer:
            assert tc_context_simple_usage(ctx, "first") == ""
            outer.exec0("true")
            assert tc_context_exclusive_usage(ctx, "second") == "first"
            with pytest.raises(tbot.machine.channel.ChannelClosedException):
                outer.exec0("true")
            assert tc_context_simple_usage(ctx, "third") == ""
Ejemplo n.º 11
0
def test_resetting_context() -> None:
    """
    An request(reset=True) should always get a pristine instance.
    """
    ctx = tbot.Context()
    testmachines.register_machines(ctx)
    with ctx:
        with ctx.request(tbot.role.LabHost) as outer:
            assert tc_context_simple_usage(ctx, "first") == ""
            outer.exec0("true")
            tc_context_resetting_usage(ctx, "second")
            with pytest.raises(tbot.machine.channel.ChannelClosedException):
                outer.exec0("true")
            assert tc_context_simple_usage(ctx, "third") == "second"
            tc_context_resetting_usage(ctx, "fourth")
Ejemplo n.º 12
0
def test_reset_on_error_context() -> None:
    """
    Make sure the reset_on_error mode works as intended.
    """
    ctx = tbot.Context()
    testmachines.register_machines(ctx)
    with ctx:
        with ctx.request(tbot.role.LabHost) as outer:
            with pytest.raises(ContextTestException):
                with ctx.request(tbot.role.LabHost, reset_on_error=True) as inner1:
                    inner1.env("TBOT_CTX_TESTS", "inner1")
                    raise ContextTestException

            with ctx.request(tbot.role.LabHost) as inner2:
                assert inner2.env("TBOT_CTX_TESTS") != "inner1"

            with pytest.raises(tbot.machine.channel.ChannelClosedException):
                outer.exec0("true")