diff options
author | Yifan Hong <elsk@google.com> | 2023-11-02 16:01:06 -0700 |
---|---|---|
committer | Isaac J. Manjarres <isaacmanjarres@google.com> | 2023-12-07 13:57:47 -0800 |
commit | 59d70ec8cdc522f0bd63625f6f7d63ef5220be72 (patch) | |
tree | b1dcaf5ae3d4621720736a7a42ff391e6e8cafc1 | |
parent | a7e3f62f6638c0c53a76ac4e3917a2965522e7ea (diff) | |
download | build-59d70ec8cdc522f0bd63625f6f7d63ef5220be72.tar.gz |
kleaf: artifacts test do not depend on hermetic_tools aliases.
Drop references to
//build/kernel:hermetic-tools/...
Such aliases have been deprecated. This ensures
that running these tests won't produce warning
messages because they depend on the deprecated
aliases.
To achieve this, I defined py_binary for each
test script, then replaced py_test with hermetic_exec_test
that invokes the script with the given arguments.
in PATH before running the test. Then I modified
the tests to search for the binary in PATH.
Test: TH
Test: bazel test //common-modules/virtual-device:x86_64/goldfish_drivers/goldfish_sync_test //common:kernel_aarch64_test //common:kernel_aarch64_additional_tests --test_output=all --config=stamp
Bug: 234125794
Change-Id: Id84321227e6af04e2433516c30f8b11cbb9bcafe
-rw-r--r-- | kleaf/artifact_tests/BUILD.bazel | 51 | ||||
-rw-r--r-- | kleaf/artifact_tests/check_module_signature.py | 7 | ||||
-rw-r--r-- | kleaf/artifact_tests/device_modules_test.bzl | 24 | ||||
-rw-r--r-- | kleaf/artifact_tests/initramfs_modules_options_test.py | 9 | ||||
-rw-r--r-- | kleaf/artifact_tests/kernel_build_test.py | 4 | ||||
-rw-r--r-- | kleaf/artifact_tests/kernel_module_test.py | 3 | ||||
-rw-r--r-- | kleaf/artifact_tests/kernel_test.bzl | 70 | ||||
-rw-r--r-- | kleaf/artifact_tests/py_test_hack.bzl | 31 |
8 files changed, 117 insertions, 82 deletions
diff --git a/kleaf/artifact_tests/BUILD.bazel b/kleaf/artifact_tests/BUILD.bazel index 81be4c4a..30306ef3 100644 --- a/kleaf/artifact_tests/BUILD.bazel +++ b/kleaf/artifact_tests/BUILD.bazel @@ -16,11 +16,7 @@ load("@bazel_skylib//:bzl_library.bzl", "bzl_library") exports_files([ "build.config.modules", - "check_module_signature.py", "fake_modules_options.txt", - "kernel_build_test.py", - "kernel_module_test.py", - "initramfs_modules_options_test.py", ]) bzl_library( @@ -28,6 +24,7 @@ bzl_library( srcs = [ "device_modules_test.bzl", "kernel_test.bzl", + "py_test_hack.bzl", ], visibility = ["//build/kernel/kleaf:__subpackages__"], deps = [ @@ -36,3 +33,49 @@ bzl_library( "@bazel_skylib//rules:write_file", ], ) + +py_binary( + name = "kernel_module_test", + srcs = ["kernel_module_test.py"], + python_version = "PY3", + # All packages that uses kernel_module must be able to see this. + visibility = ["//visibility:public"], + deps = [ + "@io_abseil_py//absl/testing:absltest", + ], +) + +py_binary( + name = "kernel_build_test", + srcs = ["kernel_build_test.py"], + python_version = "PY3", + # All packages that uses kernel_module must be able to see this. + visibility = ["//visibility:public"], + deps = [ + "@io_abseil_py//absl/testing:absltest", + "@io_abseil_py//absl/testing:parameterized", + ], +) + +py_binary( + name = "initramfs_modules_options_test", + srcs = ["initramfs_modules_options_test.py"], + python_version = "PY3", + # All packages that uses kernel_module must be able to see this. + visibility = ["//visibility:public"], + deps = [ + "@io_abseil_py//absl/testing:absltest", + ], +) + +py_binary( + name = "check_module_signature", + srcs = ["check_module_signature.py"], + python_version = "PY3", + # All packages that uses kernel_module must be able to see this. + visibility = ["//visibility:public"], + deps = [ + "@io_abseil_py//absl/flags", + "@io_abseil_py//absl/testing:absltest", + ], +) diff --git a/kleaf/artifact_tests/check_module_signature.py b/kleaf/artifact_tests/check_module_signature.py index 816d4842..5ccc2241 100644 --- a/kleaf/artifact_tests/check_module_signature.py +++ b/kleaf/artifact_tests/check_module_signature.py @@ -23,9 +23,8 @@ import unittest flags.DEFINE_string("dir", None, "Directory of modules") flags.DEFINE_string("module", None, "name of module to check") flags.DEFINE_boolean("expect_signature", None, "Whether to expect signature from the module") -flags.DEFINE_string("modinfo", None, "Location of modinfo") flags.mark_flags_as_required( - ["dir", "module", "expect_signature", "modinfo"] + ["dir", "module", "expect_signature"] ) FLAGS = flags.FLAGS @@ -52,8 +51,8 @@ class CheckModuleSignatureTest(unittest.TestCase): def assert_signature(self, file_path): # TODO(b/250667773): Use signer or signature - sig_id=subprocess.check_output([FLAGS.modinfo, "-F", "sig_id", file_path], - text=True).strip() + sig_id=subprocess.check_output(["modinfo", "-F", "sig_id", file_path], + text=True).strip() expected_sig_id = "PKCS#7" if FLAGS.expect_signature else "" self.assertEqual(expected_sig_id, sig_id) diff --git a/kleaf/artifact_tests/device_modules_test.bzl b/kleaf/artifact_tests/device_modules_test.bzl index a8f35a42..3ef5c326 100644 --- a/kleaf/artifact_tests/device_modules_test.bzl +++ b/kleaf/artifact_tests/device_modules_test.bzl @@ -15,11 +15,13 @@ """This test checks that device targets contains proper modules.""" load("@bazel_skylib//rules:write_file.bzl", "write_file") -load("//build/kernel/kleaf/tests:empty_test.bzl", "empty_test") load("//build/kernel/kleaf/impl:common_providers.bzl", "KernelModuleInfo") +load("//build/kernel/kleaf/impl:hermetic_exec.bzl", "hermetic_exec_test") load("//build/kernel/kleaf/impl:kernel_build.bzl", "kernel_build") load("//build/kernel/kleaf/impl:kernel_modules_install.bzl", "kernel_modules_install") load("//build/kernel/kleaf/impl:utils.bzl", "kernel_utils") +load("//build/kernel/kleaf/tests:empty_test.bzl", "empty_test") +load(":py_test_hack.bzl", "run_py_binary_cmd") visibility("//build/kernel/kleaf/...") @@ -46,34 +48,26 @@ def _check_signature( base_kernel_module, expect_signature, directory): - script = "//build/kernel/kleaf/artifact_tests:check_module_signature.py" - modinfo = "//build/kernel:hermetic-tools/modinfo" + test_binary = "//build/kernel/kleaf/artifact_tests:check_module_signature" args = [ "--module", base_kernel_module, "--expect_signature" if expect_signature else "--noexpect_signature", - "--modinfo", - "$(location {})".format(modinfo), ] - data = [modinfo] + data = [test_binary] if directory: args += [ "--dir", - "$(location {})".format(directory), + "$(rootpath {})".format(directory), ] data.append(directory) - native.py_test( + + hermetic_exec_test( name = name, - main = script, - srcs = [script], - python_version = "PY3", data = data, + script = run_py_binary_cmd(test_binary), args = args, timeout = "short", - deps = [ - "@io_abseil_py//absl/flags", - "@io_abseil_py//absl/testing:absltest", - ], ) def _check_signature_for_modules_install( diff --git a/kleaf/artifact_tests/initramfs_modules_options_test.py b/kleaf/artifact_tests/initramfs_modules_options_test.py index 5b259657..8610aa03 100644 --- a/kleaf/artifact_tests/initramfs_modules_options_test.py +++ b/kleaf/artifact_tests/initramfs_modules_options_test.py @@ -14,21 +14,16 @@ import argparse import os -import re import subprocess import sys import tempfile import unittest -import time from absl.testing import absltest def load_arguments(): parser = argparse.ArgumentParser() - parser.add_argument("--cpio", default="cpio") - parser.add_argument("--diff", default="diff") - parser.add_argument("--gzip", default="gzip") parser.add_argument("--expected") parser.add_argument("files", nargs="*", default=[]) return parser.parse_known_args() @@ -46,10 +41,10 @@ class InitramfsModulesOptions(unittest.TestCase): with open(arguments.expected) as expected: with tempfile.TemporaryDirectory() as temp_dir: with open(initramfs) as initramfs_file: - with subprocess.Popen([os.path.abspath(arguments.cpio), "-i"], cwd=temp_dir, + with subprocess.Popen(["cpio", "-i"], cwd=temp_dir, stdin=subprocess.PIPE, stdout=subprocess.PIPE) as cpio_sp: # Assume LZ4_RAMDISK is not set for this target. - with subprocess.Popen([arguments.gzip, "-c", "-d"], stdin=initramfs_file, + with subprocess.Popen(["gzip", "-c", "-d"], stdin=initramfs_file, stdout=cpio_sp.stdin) as gzip_sp: gzip_sp.communicate() self.assertEqual(0, gzip_sp.returncode) diff --git a/kleaf/artifact_tests/kernel_build_test.py b/kleaf/artifact_tests/kernel_build_test.py index 06acc266..6e09d9d0 100644 --- a/kleaf/artifact_tests/kernel_build_test.py +++ b/kleaf/artifact_tests/kernel_build_test.py @@ -17,7 +17,6 @@ import os import re import sys import subprocess -import unittest from absl.testing import absltest from absl.testing import parameterized @@ -25,7 +24,6 @@ from absl.testing import parameterized def load_arguments(): parser = argparse.ArgumentParser() - parser.add_argument("--strings", default="strings") parser.add_argument("--artifacts", nargs="*", default=[]) return parser.parse_known_args() @@ -66,7 +64,7 @@ class ScmVersionTestCase(parameterized.TestCase): for artifact in arguments.artifacts: if os.path.basename(artifact) != "vmlinux": continue - strings = subprocess.check_output([arguments.strings, artifact], + strings = subprocess.check_output(["llvm-strings", artifact], text=True).strip().splitlines() matches = any(self.matches_any_pattern(s) for s in strings) msg = "scmversion not found for vmlinux, found {}".format( diff --git a/kleaf/artifact_tests/kernel_module_test.py b/kleaf/artifact_tests/kernel_module_test.py index 7f3a7108..9511160a 100644 --- a/kleaf/artifact_tests/kernel_module_test.py +++ b/kleaf/artifact_tests/kernel_module_test.py @@ -24,7 +24,6 @@ from absl.testing import absltest def load_arguments(): parser = argparse.ArgumentParser() - parser.add_argument("--modinfo", default="modinfo") parser.add_argument("--modules", nargs="*", default=[]) return parser.parse_known_args() @@ -47,7 +46,7 @@ class ScmVersionTestCase(unittest.TestCase): self.skipTest("{} is not a kernel module".format(basename)) try: scmversion = subprocess.check_output( - [arguments.modinfo, module, "-F", "scmversion"], + ["modinfo", module, "-F", "scmversion"], text=True, stderr=subprocess.PIPE).strip() except subprocess.CalledProcessError as e: self.fail("modinfo returns {}: {}".format(e.returncode, e.stderr)) diff --git a/kleaf/artifact_tests/kernel_test.bzl b/kleaf/artifact_tests/kernel_test.bzl index 8ec992f6..cf76035b 100644 --- a/kleaf/artifact_tests/kernel_test.bzl +++ b/kleaf/artifact_tests/kernel_test.bzl @@ -15,6 +15,9 @@ Tests for artifacts produced by kernel_module. """ +load("//build/kernel/kleaf/impl:hermetic_exec.bzl", "hermetic_exec_test") +load(":py_test_hack.bzl", "run_py_binary_cmd") + visibility("//build/kernel/kleaf/...") def kernel_module_test( @@ -32,26 +35,20 @@ def kernel_module_test( See complete list [here](https://docs.bazel.build/versions/main/be/common-definitions.html#common-attributes). """ - script = "//build/kernel/kleaf/artifact_tests:kernel_module_test.py" - modinfo = "//build/kernel:hermetic-tools/modinfo" - args = ["--modinfo", "$(location {})".format(modinfo)] - data = [modinfo] + test_binary = "//build/kernel/kleaf/artifact_tests:kernel_module_test" + args = [] + data = [test_binary] if modules: args.append("--modules") - args += ["$(locations {})".format(module) for module in modules] + args += ["$(rootpaths {})".format(module) for module in modules] data += modules - native.py_test( + hermetic_exec_test( name = name, - main = script, - srcs = [script], - python_version = "PY3", data = data, + script = run_py_binary_cmd(test_binary), args = args, timeout = "short", - deps = [ - "@io_abseil_py//absl/testing:absltest", - ], **kwargs ) @@ -69,24 +66,19 @@ def kernel_build_test( See complete list [here](https://docs.bazel.build/versions/main/be/common-definitions.html#common-attributes). """ - script = "//build/kernel/kleaf/artifact_tests:kernel_build_test.py" - strings = "//build/kernel:hermetic-tools/llvm-strings" - args = ["--strings", "$(location {})".format(strings)] + test_binary = "//build/kernel/kleaf/artifact_tests:kernel_build_test" + args = [] + data = [test_binary] if target: - args += ["--artifacts", "$(locations {})".format(target)] + args += ["--artifacts", "$(rootpaths {})".format(target)] + data.append(target) - native.py_test( + hermetic_exec_test( name = name, - main = script, - srcs = [script], - python_version = "PY3", - data = [target, strings], + data = data, + script = run_py_binary_cmd(test_binary), args = args, timeout = "short", - deps = [ - "@io_abseil_py//absl/testing:absltest", - "@io_abseil_py//absl/testing:parameterized", - ], **kwargs ) @@ -106,38 +98,22 @@ def initramfs_modules_options_test( See complete list [here](https://docs.bazel.build/versions/main/be/common-definitions.html#common-attributes). """ - script = "//build/kernel/kleaf/artifact_tests:initramfs_modules_options_test.py" - cpio = "//build/kernel:hermetic-tools/cpio" - diff = "//build/kernel:hermetic-tools/diff" - gzip = "//build/kernel:hermetic-tools/gzip" + test_binary = "//build/kernel/kleaf/artifact_tests:initramfs_modules_options_test" args = [ - "--cpio", - "$(location {})".format(cpio), - "--diff", - "$(location {})".format(diff), - "--gzip", - "$(location {})".format(gzip), "--expected", - "$(location {})".format(expected_modules_options), - "$(locations {})".format(kernel_images), + "$(rootpath {})".format(expected_modules_options), + "$(rootpaths {})".format(kernel_images), ] - native.py_test( + hermetic_exec_test( name = name, - main = script, - srcs = [script], - python_version = "PY3", data = [ - cpio, - diff, expected_modules_options, - gzip, kernel_images, + test_binary, ], + script = run_py_binary_cmd(test_binary), args = args, timeout = "short", - deps = [ - "@io_abseil_py//absl/testing:absltest", - ], **kwargs ) diff --git a/kleaf/artifact_tests/py_test_hack.bzl b/kleaf/artifact_tests/py_test_hack.bzl new file mode 100644 index 00000000..c6cc05b0 --- /dev/null +++ b/kleaf/artifact_tests/py_test_hack.bzl @@ -0,0 +1,31 @@ +# Copyright (C) 2023 The Android Open Source Project +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Hacks for running `py_binary` in custom rules.""" + +def run_py_binary_cmd(test_binary): + """Returns a cmd that runs `test_binary` with $@. + + Args: + test_binary: label to a `py_binary`. + """ + + # https://github.com/bazelbuild/bazel/issues/20038 + # HACK: py_binary puts multiple File's in DefaultInfo, including the + # source files. Only filter the one with executable bit. + return """ + $(find $(rootpaths {test_binary}) $(rootpaths {test_binary}) -executable | head -n1) "$@" + """.format( + test_binary = test_binary, + ) |