gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/test/runtimes/defs.bzl (about)

     1  """Defines a rule for runtime test targets."""
     2  
     3  load("//tools:defs.bzl", "go_test", "local_test_tags")
     4  
     5  def _runtime_test_impl(ctx):
     6      # Construct arguments.
     7      args = [
     8          "--lang",
     9          ctx.attr.lang,
    10          "--image",
    11          ctx.attr.image,
    12          "--batch",
    13          str(ctx.attr.batch),
    14      ]
    15      if ctx.attr.exclude_file:
    16          args += [
    17              "--exclude_file",
    18              ctx.files.exclude_file[0].short_path,
    19          ]
    20  
    21      # Build a runner.
    22      runner = ctx.actions.declare_file("%s-executer" % ctx.label.name)
    23      runner_content = "\n".join([
    24          "#!/bin/bash",
    25          "%s %s $@\n" % (ctx.files._runner[0].short_path, " ".join(args)),
    26      ])
    27      ctx.actions.write(runner, runner_content, is_executable = True)
    28  
    29      # Return the runner.
    30      return [DefaultInfo(
    31          executable = runner,
    32          runfiles = ctx.runfiles(
    33              files = ctx.files._runner + ctx.files.exclude_file + ctx.files._proctor,
    34              collect_default = True,
    35              collect_data = True,
    36          ),
    37      )]
    38  
    39  _runtime_test = rule(
    40      implementation = _runtime_test_impl,
    41      attrs = {
    42          "image": attr.string(
    43              mandatory = False,
    44          ),
    45          "lang": attr.string(
    46              mandatory = True,
    47          ),
    48          "exclude_file": attr.label(
    49              mandatory = False,
    50              allow_single_file = True,
    51          ),
    52          "batch": attr.int(
    53              default = 50,
    54              mandatory = False,
    55          ),
    56          "_runner": attr.label(
    57              default = "//test/runtimes/runner:runner",
    58              executable = True,
    59              cfg = "target",
    60          ),
    61          "_proctor": attr.label(
    62              default = "//test/runtimes/proctor:proctor",
    63              executable = True,
    64              cfg = "target",
    65          ),
    66          # runsc is needed to invalidate the bazel cache in case of any code changes.
    67          "_runsc": attr.label(
    68              default = "//runsc:runsc",
    69              executable = True,
    70              cfg = "target",
    71          ),
    72      },
    73      test = True,
    74  )
    75  
    76  def runtime_test(name, **kwargs):
    77      _runtime_test(
    78          name = name,
    79          image = name,  # Resolved as images/runtimes/%s.
    80          tags = [
    81              "no-sandbox",
    82              "manual",
    83          ] + local_test_tags,
    84          **kwargs
    85      )
    86  
    87  def exclude_test(name, exclude_file):
    88      """Test that a exclude file parses correctly."""
    89      go_test(
    90          name = name + "_exclude_test",
    91          library = ":runner",
    92          srcs = ["exclude_test.go"],
    93          args = ["--exclude_file", "test/runtimes/" + exclude_file],
    94          data = [exclude_file],
    95      )