From c53bcb4aa93a69e9dd74eb3b4ce701ec30b934d3 Mon Sep 17 00:00:00 2001 From: Jordan Bonser Date: Tue, 15 Apr 2025 10:48:16 +0100 Subject: [PATCH] feat: make the local harness work standalone the local harness was only usable as a context manager. this commit adds a new target: `bazelisk run //bazel/labgrid/local:harness` this makes the harness available as a standalone program, which sets up a dummy place and tags for it to be used by the labgrid client. the harness chooses a free port on the machine and outputs its url to stdout. the program is terminated using keyboard interrupt. --- bazel/labgrid/client/common.py | 1 + bazel/labgrid/local/BUILD.bazel | 9 ++++++- bazel/labgrid/local/harness.py | 44 +++++++++++++++++++++++++++++++++ bazel/labgrid/local/local.py | 33 ++++++++++++++----------- 4 files changed, 72 insertions(+), 15 deletions(-) create mode 100644 bazel/labgrid/local/harness.py diff --git a/bazel/labgrid/client/common.py b/bazel/labgrid/client/common.py index 3995fa4..05f106b 100644 --- a/bazel/labgrid/client/common.py +++ b/bazel/labgrid/client/common.py @@ -27,6 +27,7 @@ def labgrid_client(*args, env=None, timeout=10, output_as_yaml=True, url=""): ) except (CalledProcessError, TimeoutExpired) as e: stderr.write(e.output) + stderr.write(e.stderr) raise if process.stdout: diff --git a/bazel/labgrid/local/BUILD.bazel b/bazel/labgrid/local/BUILD.bazel index 0575aea..fc69814 100644 --- a/bazel/labgrid/local/BUILD.bazel +++ b/bazel/labgrid/local/BUILD.bazel @@ -1,4 +1,4 @@ -load("@rules_python//python:defs.bzl", "py_library", "py_test") +load("@rules_python//python:defs.bzl", "py_binary", "py_library", "py_test") py_library( name = "local", @@ -23,6 +23,13 @@ py_library( ], ) +py_binary( + name = "harness", + srcs = ["harness.py"], + visibility = ["//visibility:public"], + deps = [":local"], +) + py_test( name = "test", size = "small", diff --git a/bazel/labgrid/local/harness.py b/bazel/labgrid/local/harness.py new file mode 100644 index 0000000..4bc0c83 --- /dev/null +++ b/bazel/labgrid/local/harness.py @@ -0,0 +1,44 @@ +from argparse import ArgumentParser +from subprocess import CalledProcessError +from sys import stderr, stdout +from time import sleep + +from bazel.labgrid.local import harness + + +def arguments(prsr: ArgumentParser) -> None: + prsr.add_argument( + "--place", + help="The name of a place to use.", + type=str, + default="test_place", + ) + prsr.add_argument( + "tags", + metavar="KEY=VALUE", + nargs="*", + help="Tags to set on the place.", + default={"fake": "device"}, + ) + + +def main(): + prsr = ArgumentParser() + arguments(prsr) + parsed = prsr.parse_args() + + try: + with harness(parsed.tags, parsed.place) as test_harness: + stdout.write(f"Coordinator URL: {test_harness.coordinator_url}\n") + while True: + sleep(1) + continue + except KeyboardInterrupt: + exit(0) + except CalledProcessError as e: + stderr.write(e.stderr) + exit(1) + + +if __name__ == "__main__": + main() diff --git a/bazel/labgrid/local/local.py b/bazel/labgrid/local/local.py index 8112abb..11eca19 100644 --- a/bazel/labgrid/local/local.py +++ b/bazel/labgrid/local/local.py @@ -4,7 +4,6 @@ import socket from contextlib import contextmanager from dataclasses import dataclass from typing import Iterator, Mapping - from bazel.labgrid.local.coordinator import coordinator from bazel.labgrid.local.exporter import exporter from bazel.labgrid.client.common import labgrid_client @@ -28,17 +27,17 @@ def harness( place: str = "test_place", ) -> Iterator[Harness]: with coordinator() as url: - labgrid_client("--place", place, "create", url=url) - try: - labgrid_client( - "--place", - place, - "set-tags", - *(f"{k}={v}" for k, v in tags.items()), - url=url, - ) - - with exporter(url=url): + with exporter(url=url): + delete_place = True + try: + labgrid_client("--place", place, "create", url=url) + labgrid_client( + "--place", + place, + "set-tags", + *(f"{k}={v}" for k, v in tags.items()), + url=url, + ) # add resources to the "place" labgrid_client( "--place", @@ -52,5 +51,11 @@ def harness( tags=tags, place=place, ) - finally: - labgrid_client("--place", place, "delete", url=url) + except KeyboardInterrupt: + # A keyboard interrupt sends`SIGINT` to subprocesses (including the coordinator), + # so we shouldn't try to communicate with it as it won't be available anymore + delete_place = False + raise + finally: + if delete_place: + labgrid_client("--place", place, "delete", url=url) -- GitLab