github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/test/runtimes/defs.bzl (about)

     1  """Defines a rule for runtime test targets."""
     2  
     3  load("//tools:defs.bzl", "go_test")
     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      },
    67      test = True,
    68  )
    69  
    70  def runtime_test(name, **kwargs):
    71      _runtime_test(
    72          name = name,
    73          image = name,  # Resolved as images/runtimes/%s.
    74          tags = [
    75              "local",
    76              "manual",
    77          ],
    78          **kwargs
    79      )
    80  
    81  def exclude_test(name, exclude_file):
    82      """Test that a exclude file parses correctly."""
    83      go_test(
    84          name = name + "_exclude_test",
    85          library = ":runner",
    86          srcs = ["exclude_test.go"],
    87          args = ["--exclude_file", "test/runtimes/" + exclude_file],
    88          data = [exclude_file],
    89      )