Recipes

To make writing testcases as productive as possible, this page contains a number of examples which you can quickly copy and adapt to get your tests up and running. These are:

  1. Most common:

  2. For board tests

  3. For lab & general

Testcase on lab-host

import tbot

@tbot.testcase
def testcase_with_lab() -> None:
    with tbot.ctx.request(tbot.role.LabHost) as lh:
        lh.exec0("uname", "-a")

Testcase with U-Boot

import tbot

@tbot.testcase
def testcase_with_uboot() -> None:
    # This is not strictly needed but it will automatically powercycle the
    # board if it was previously in Linux.
    tbot.ctx.teardown_if_alive(tbot.role.BoardLinux)

    with tbot.ctx.request(tbot.role.BoardUBoot) as ub:
        ub.exec0("version")

Testcase with Linux (on the board)

import tbot

@tbot.testcase
def testcase_with_linux() -> None:
    with tbot.ctx.request(tbot.role.BoardLinux) as lnx:
        lnx.exec0("uname", "-a")

Powercycling the Board

If a testcase requires powercycling, you should write your testcase like this:

import tbot
from tbot.machine import linux

@tbot.testcase
def testcase_powercycle() -> None:
    with tbot.ctx.request(tbot.role.BoardLinux) as lnx:
        lnx.exec0("touch", "/tmp/this-is-a-volatile-file")

    # the reset=True will make sure to trigger a powercycle
    with tbot.ctx.request(tbot.role.BoardLinux, reset=True) as lnx:
        assert not lnx.test("test", "-e", "/tmp/this-is-a-volatile-file")

Copy files from one machine to another

This is a very common use-case so tbot provides a builtin testcase for it: tbot.tc.shell.copy(). Use it like this:

from tbot.tc import shell

# Copy a file from the selected lab-host to localhost
with tbot.ctx() as cx:
    lo = cx.request(tbot.role.LocalHost)
    lh = cx.request(tbot.role.LabHost)

    file_on_labhost = lh.fsroot / "etc" / "shadow"
    file_on_localhost = lo.workdir / "sneaky_stolen_passwords"

    shell.copy(file_on_labhost, file_on_localhost)