Skip to main content

Choosing a Test's Machine

By default, a test runs on its target's execution platform. A rule can run the test on another machine, for example on remote execution while the target's actions run locally, in two ways:

  • Set default_executor on the test info to a hand-written CommandExecutorConfig.
  • Put the test in an execution group that resolves to a platform of that machine.

Prefer the execution group. default_executor moves only the test command: the tools that the test runs from exec deps stay configured for the target's execution platform, a machine the test does not run on. Tools in the test's group are configured for the group's platform, and the machine is a registered execution platform instead of executor settings copied into the rule.

Example​

Two test rules run the same tool on remote execution. override_test picks the machine with a hand-written executor, and group_test with the Run group:

# rules.bzl
load("//config:defs.bzl", "REMOTE_EXECUTOR")

Run = exec_group()

def _tool_impl(ctx):
script = ctx.actions.write("tool.py", ["import sys", "sys.exit(0)"])
return [DefaultInfo(default_output = script), RunInfo(args = cmd_args("python3", script))]

tool = rule(impl = _tool_impl, attrs = {})

def _test_info(tool, **kwargs):
return ExternalRunnerTestInfo(type = "custom", command = [tool[RunInfo]], use_project_relative_paths = True, run_from_project_root = True, **kwargs)

# Picks the machine with a hand-written executor. The target still resolves to the default
# execution platform, so `_tool` is configured for `linux_local` but runs on RE.
override_test = rule(
impl = lambda ctx: [DefaultInfo(), _test_info(ctx.attrs._tool, default_executor = REMOTE_EXECUTOR)],
attrs = {"_tool": attrs.exec_dep(default = "//:tool")},
)

# Picks the machine with the `Run` group: `run_on` constrains it to a registered platform, `_tool`
# is configured for that platform, and the test runs with that platform's executor.
group_test = rule(
impl = lambda ctx: [DefaultInfo(), _test_info(ctx.attrs._tool, exec_group = Run)],
attrs = {
"run_on": attrs.exec_compatible_with(exec_group = Run, default = []),
"_tool": attrs.exec_dep(default = "//:tool", exec_group = Run),
},
exec_groups = [Run],
)
# BUCK
load(":rules.bzl", "group_test", "override_test", "tool")

tool(name = "tool")

override_test(name = "override_test")

group_test(
name = "group_test",
run_on = ["//config:remote"],
)

The example registers two execution platforms. linux_local comes first, so it is the execution platform of targets without constraints. linux_remote satisfies the constraint //config:remote, and its executor is REMOTE_EXECUTOR, which runs commands only on remote execution. REMOTE_EXECUTOR and the rules that define the platforms are small local ones, not prelude rules: see the fixture of the Buck2 test that runs this example.

Both tests run on remote execution:

$ buck2 test //:override_test //:group_test --target-platforms //config:linux_local
$ buck2 log what-ran
test.run root//:group_test re(platform=linux-remote-execution) ...
test.run root//:override_test re(platform=linux-remote-execution) ...

But only group_test runs its tool on the platform the tool is configured for:

$ buck2 cquery 'kind(tool, deps(//:override_test))' --target-platforms //config:linux_local
root//:tool (root//config:linux_local#...)
$ buck2 cquery 'kind(tool, deps(//:group_test))' --target-platforms //config:linux_local
root//:tool (root//config:linux_remote#...)

Here the two platforms differ only in their executor, so override_test still passes. If they differed in OS or CPU architecture, its tool would be built for a machine the test does not run on.

buck.exec_group_platforms shows the platform of the Run group:

$ buck2 cquery //:group_test --target-platforms //config:linux_local --output-attribute buck.exec_group_platforms
{
"root//:group_test (root//config:linux_local#...)": {
"buck.exec_group_platforms": {
"Run": "root//config:linux_remote"
}
}
}

Moving a test to a group​

  1. Declare a group for the test, list it in the rule's exec_groups, and set exec_group to it on the ExternalRunnerTestInfo or InternalRunnerTestInfo.
  2. Put the tools that the test runs in the group, with exec_group on their attrs.exec_dep() or attrs.toolchain_dep(). A rule with named groups must give every exec dep a group: build tools, such as a compiler, take exec_group = DefaultExecGroup, and the rule then lists DefaultExecGroup too. See Putting deps in a group.
  3. Make the group resolve to the test's machine: give targets an attrs.exec_compatible_with() attribute for the group, as run_on does above, or rely on the constraints of the group's tools.
  4. Remove default_executor. If a test info sets both, default_executor wins.

An executor override that the test runner picks still takes precedence over the group. See Test Execution.