Rahix / tbot

Automation/Testing tool for Embedded Linux Development
https://tbot.tools
GNU General Public License v3.0
91 stars 21 forks source link

Introduce "Context" mechanism for machine instance management #40

Closed Rahix closed 4 years ago

Rahix commented 4 years ago

The existing tbot.selectable module is a glorified hack for bridging between the selected config and the testcases. As tbot aims to support more and more complex use-cases, it becomes apparent that this existing mechanism is not sufficient.

The "Context" is an attempt to solve this problem: It is a central "machine manager" where the config registers available machine classes and testcases request instances from. The idea is to have global management of instances so testcases need not concern themselves with creating and passing instances around. Instead, the manager keeps the instances at the ready for any testcase that needs access.

A few quick examples:

Testcase with just Linux on the board

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

Testcase with Lab-Host and board's U-Boot

@tbot.testcase
def test_multiple_machines() -> None:
    with tbot.ctx() as cx:
        lh = cx.request(tbot.role.LabHost)
        ub = cx.request(tbot.role.BoardUBoot)

        lh.exec0("uname", "-a")
        ub.exec0("version")

New-style lab configuration

class MyLabHost(connector.SSHConnector, linux.Bash):
    name = "foo-lab-host"

    @property
    def workdir(self):
        return linux.Workdir.xdg_data("tbot-foo-lab")

# This function is called by tbot when loading the configuration and should
# register all machines into the given context
def register_machines(ctx):
    ctx.register(MyLabHost, tbot.role.LabHost)

The "context" API is compatible with tbot.selectble for the time being. This means, old configuration will work with new testcases and new configuration will work with old testcases.

For more details, please refer to the context documentation in this PR.

niecore commented 4 years ago

Looks like a very good change. Any plans when this will be merged?

Rahix commented 4 years ago

I'm still ironing out a few more details, to make this new design work well for even more situations. Should not be too far off, though!

Rahix commented 4 years ago

I'd consider this implementation feature complete now. I still want to let it sit for a bit to see how well it works in practice, but apart from that I think it is ready. :)

The next release will include this new API as an optional feature and after that I will start migrating more code towards it and deprecating the "old" mechanisms which are now superseded.

Rahix commented 4 years ago

(Fixed some minor bugs and documentation issues).