def tc_context_exclusive_usage(ctx: tbot.Context, new: str) -> str: with ctx.request(tbot.role.LabHost, exclusive=True) as lh: value = lh.env("TBOT_CTX_TESTS") lh.env("TBOT_CTX_TESTS", new) with pytest.raises(Exception, match=".*not available.*"): with ctx.request(tbot.role.LabHost): pass return value
def from_context(cls, ctx: tbot.Context) -> Iterator: with contextlib.ExitStack() as cx: # Start the server srv = cx.enter_context(ctx.request(MocksshServer)) # Get a localhost lo = cx.enter_context(ctx.request(tbot.role.LocalHost)) # Set authenticator cls.authenticator = linux.auth.PrivateKeyAuthenticator( srv.userkey.at_host(srv)) # Instanciate self m = cx.enter_context(cls(lo)) yield m
def test_gdb_machine(tbot_context: tbot.Context) -> None: with tbot_context.request(testmachines.Localhost) as lh: program = lh.exec0("which", "echo").strip() if not lh.test("which", "gdb"): pytest.skip("GDB is missing.") with tbot_contrib.gdb.GDB(lh, program, "hello", "world") as gdb: out = gdb.exec("break", "getenv") if "not defined" in out: pytest.skip("Debug symbols missing.") gdb.exec("run") while True: # First argument is in RDI out = gdb.exec("print", "(char*)($rdi)").strip() match = RESULT_PATTERN.match(out) assert match is not None val = match.group(1) tbot.log.message( f'{program}: {tbot.log.c("getenv").green}("{tbot.log.c(val).bold}")' ) if "exited" in gdb.exec("continue"): break lh.exec0("echo", "back out of gdb!")
def test_find_ip_address(tbot_context: tbot.Context) -> None: with tbot_context.request(testmachines.Localhost) as lo: # Check whether it is able to find a "default" address tbot_contrib.utils.find_ip_address(lo) # Check whether find_ip_address() works with a local address addr = tbot_contrib.utils.find_ip_address(lo, "127.0.0.1") assert addr == "127.0.0.1"
def test_purepath(tbot_context: tbot.Context) -> None: with tbot_context.request(testmachines.Localhost) as lo: p = lo.workdir / "foo" / "bar" / "baz" assert p.parent.name == "bar" assert p.parents[0].name == "bar" assert p.parents[1].name == "foo" assert p.parents[2] == lo.workdir
def test_clone_hash(tbot_context: tbot.Context) -> None: """ A clone should not be the same machine but should have the same hash. """ with tbot_context.request(testmachines.Localhost) as lo: with lo.clone() as lo2: assert id(lo) != id(lo2) assert hash(lo) == hash(lo2)
def test_uboot_simple_control(tbot_context: tbot.Context) -> None: with tbot_context.request(testmachines.MockhwBoardUBoot) as ub: out = ub.exec0("false", board.AndThen, "echo", "FOO", board.OrElse, "echo", "BAR").strip() assert out == "BAR" out = ub.exec0("true", board.AndThen, "echo", "FOO", board.OrElse, "echo", "BAR").strip() assert out == "FOO"
def test_integrity(tbot_context: tbot.Context) -> None: with tbot_context.request(testmachines.Localhost) as lo: p = lo.workdir / "abcdef" with testmachines.Localhost() as lo2: with pytest.raises(tbot.error.WrongHostError): # MYPY knows this is wrong :) lo2.exec0("echo", p) # type: ignore with lo.clone() as lo3: lo3.exec0("echo", p)
def test_reentrancy(tbot_context: tbot.Context) -> None: """ Check whether you can properly enter a machine context multiple times. """ with tbot_context.request(testmachines.Localhost) as lo: with lo as h1: h1.env("TBOT_REENTRANCY", "foobarbaz") with lo as h2: assert h2.env("TBOT_REENTRANCY") == "foobarbaz" assert lo.env("TBOT_REENTRANCY") == "foobarbaz"
def from_context(cls, ctx: tbot.Context) -> Iterator: with contextlib.ExitStack() as cx: lh = cx.enter_context(ctx.request(LabHost)) with lh.build() as bh: # It's a common bug that people just return `self` from # lh.build(). Detect this and manually create a bh clone: if bh is lh: tbot.log.warning( """\ The build() method for the selected lab should not return `self` but instead `self.clone()`. Attempting to call build_host.clone() automatically now ...""" ) bh = cx.enter_context(bh.clone()) # Do a dirty trick to make `bh` actually a ProxyBuildHost # so the Context doesn't get alarmed by us returning an # instance of the wrong type. wrapper_class = types.new_class( "BuildHostProxy", (bh.__class__, BuildHostProxy) ) bh.__class__ = wrapper_class yield bh
def test_uboot_long_output(tbot_context: tbot.Context) -> None: with tbot_context.request(testmachines.MockhwBoardUBoot) as ub: s = "_".join(map(lambda i: f"{i:02}", range(80))) assert ub.exec0("echo", s) == f"{s}\n"
def test_uboot_return_code(tbot_context: tbot.Context) -> None: with tbot_context.request(testmachines.MockhwBoardUBoot) as ub: assert ub.test("true") assert not ub.test("false")
def register_machines(ctx: tbot.Context) -> None: ctx.register(Localhost, [Localhost, tbot.role.LabHost, tbot.role.LocalHost]) ctx.register(LocalhostBash, [LocalhostBash]) ctx.register(LocalhostSlowBash, [LocalhostSlowBash]) ctx.register(LocalhostAsh, [LocalhostAsh]) ctx.register(MocksshServer, [MocksshServer]) ctx.register(MocksshClient, [MocksshClient]) ctx.register(MockhwBoard, [MockhwBoard, tbot.role.Board]) ctx.register(MockhwBoardUBoot, [MockhwBoardUBoot, tbot.role.BoardUBoot])
def test_uboot_simple_environment(tbot_context: tbot.Context) -> None: with tbot_context.request(testmachines.MockhwBoardUBoot) as ub: value = "12 foo !? # true; exit" ub.env("tbot_test_env_var", value) assert ub.env("tbot_test_env_var") == value
def tc_context_resetting_usage(ctx: tbot.Context, new: str) -> None: with ctx.request(tbot.role.LabHost, reset=True) as lh: value = lh.env("TBOT_CTX_TESTS") assert value == "" lh.env("TBOT_CTX_TESTS", new)
def test_uboot_simple_commands(tbot_context: tbot.Context) -> None: with tbot_context.request(testmachines.MockhwBoardUBoot) as ub: ub.exec0("version") assert ub.exec0("echo", "Hello World") == "Hello World\n" assert ub.exec0("echo", "$?", "!#") == "$? !#\n"
def tc_context_simple_usage(ctx: tbot.Context, new: str) -> str: with ctx.request(tbot.role.LabHost) as lh: value = lh.env("TBOT_CTX_TESTS") lh.env("TBOT_CTX_TESTS", new) return value