gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/test/packetdrill/defs.bzl (about) 1 """Defines a rule for packetdrill test targets.""" 2 3 def _packetdrill_test_impl(ctx): 4 test_runner = ctx.executable._test_runner 5 runner = ctx.actions.declare_file("%s-runner" % ctx.label.name) 6 7 script_paths = [] 8 for script in ctx.files.scripts: 9 script_paths.append(script.short_path) 10 runner_content = "\n".join([ 11 "#!/bin/bash", 12 # This test will run part in a distinct user namespace. This can cause 13 # permission problems, because all runfiles may not be owned by the 14 # current user, and no other users will be mapped in that namespace. 15 # Make sure that everything is readable here. 16 "find . -type f -exec chmod a+rx {} \\;", 17 "find . -type d -exec chmod a+rx {} \\;", 18 "%s %s --init_script %s \"$@\" -- %s\n" % ( 19 test_runner.short_path, 20 " ".join(ctx.attr.flags), 21 ctx.files._init_script[0].short_path, 22 " ".join(script_paths), 23 ), 24 ]) 25 ctx.actions.write(runner, runner_content, is_executable = True) 26 27 transitive_files = depset() 28 if hasattr(ctx.attr._test_runner, "data_runfiles"): 29 transitive_files = ctx.attr._test_runner[DefaultInfo].data_runfiles.files 30 runfiles = ctx.runfiles( 31 files = [test_runner] + ctx.files._init_script + ctx.files.scripts, 32 transitive_files = transitive_files, 33 collect_default = True, 34 collect_data = True, 35 ) 36 return [DefaultInfo(executable = runner, runfiles = runfiles)] 37 38 _packetdrill_test = rule( 39 attrs = { 40 "_test_runner": attr.label( 41 executable = True, 42 cfg = "exec", 43 allow_files = True, 44 default = "packetdrill_test.sh", 45 ), 46 "_init_script": attr.label( 47 allow_single_file = True, 48 default = "packetdrill_setup.sh", 49 ), 50 "flags": attr.string_list( 51 mandatory = False, 52 default = [], 53 ), 54 "scripts": attr.label_list( 55 mandatory = True, 56 allow_files = True, 57 ), 58 }, 59 test = True, 60 implementation = _packetdrill_test_impl, 61 ) 62 63 PACKETDRILL_TAGS = [ 64 "local", 65 "manual", 66 "packetdrill", 67 ] 68 69 def packetdrill_linux_test(name, **kwargs): 70 if "tags" not in kwargs: 71 kwargs["tags"] = PACKETDRILL_TAGS 72 _packetdrill_test( 73 name = name, 74 flags = ["--dut_platform", "linux"], 75 **kwargs 76 ) 77 78 def packetdrill_netstack_test(name, **kwargs): 79 if "tags" not in kwargs: 80 kwargs["tags"] = PACKETDRILL_TAGS 81 _packetdrill_test( 82 name = name, 83 flags = ["--dut_platform", "netstack"], 84 **kwargs 85 ) 86 87 def packetdrill_test(name, **kwargs): 88 packetdrill_linux_test(name + "_linux_test", **kwargs) 89 packetdrill_netstack_test(name + "_netstack_test", **kwargs)