1# Copyright (C) 2022 The Android Open Source Project 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts") 16load("//build/bazel/rules/cc:cc_library_static.bzl", "cc_library_static") 17load( 18 "//build/bazel/rules/test_common:paths.bzl", 19 "get_output_and_package_dir_based_path", 20) 21load(":tradefed.bzl", "tradefed_device_test", "tradefed_host_driven_test") 22 23tradefed_dependencies = [ 24 "atest_tradefed.sh", 25 "libatest-tradefed.jar", 26 "libbazel-result-reporter.jar", 27 "tradefed.jar", 28] 29 30def _test_tradefed_config_generation_impl(ctx): 31 env = analysistest.begin(ctx) 32 actions = analysistest.target_actions(env) 33 34 actual_outputs = [] 35 for action in actions: 36 for output in action.outputs.to_list(): 37 actual_outputs.append(output.path) 38 39 for expected_output in ctx.attr.expected_outputs: 40 expected_output = get_output_and_package_dir_based_path(env, expected_output) 41 asserts.true( 42 env, 43 expected_output in actual_outputs, 44 "Expected: " + expected_output + 45 " in outputs: " + str(actual_outputs), 46 ) 47 return analysistest.end(env) 48 49tradefed_config_generation_test = analysistest.make( 50 _test_tradefed_config_generation_impl, 51 attrs = { 52 "expected_outputs": attr.string_list(), 53 }, 54) 55 56def tradefed_cc_outputs(): 57 name = "cc" 58 target = "cc_target" 59 60 cc_library_static( 61 name = target, 62 srcs = ["foo.c"], 63 tags = ["manual"], 64 ) 65 tradefed_device_test( 66 name = name, 67 test_identifier = target, 68 tags = ["manual"], 69 test = target, 70 test_config = "//build/bazel/rules/tradefed/test:example_config.xml", 71 target_compatible_with = ["//build/bazel/platforms/os:linux"], 72 ) 73 74 # check for expected output files (.config file and .sh script) 75 tradefed_config_generation_test( 76 name = name + "_test", 77 target_under_test = name, 78 expected_outputs = [ 79 "tradefed_test_" + name + ".sh", 80 "result-reporters.xml", 81 target + ".config", 82 ] + tradefed_dependencies, 83 target_compatible_with = ["//build/bazel/platforms/os:linux"], 84 ) 85 return name + "_test" 86 87def tradefed_cc_host_outputs(): 88 name = "cc_host" 89 target = "cc_host_target" 90 91 cc_library_static( 92 name = target, 93 tags = ["manual"], 94 ) 95 tradefed_host_driven_test( 96 name = name, 97 test_identifier = target, 98 tags = ["manual"], 99 test = target, 100 test_config = "//build/bazel/rules/tradefed/test:example_config.xml", 101 target_compatible_with = ["//build/bazel/platforms/os:linux"], 102 ) 103 104 # check for expected output files (.config file and .sh script) 105 tradefed_config_generation_test( 106 name = name + "_test", 107 target_under_test = name, 108 expected_outputs = [ 109 "tradefed_test_" + name + ".sh", 110 "result-reporters.xml", 111 target + ".config", 112 ] + tradefed_dependencies, 113 target_compatible_with = ["//build/bazel/platforms/os:linux"], 114 ) 115 return name + "_test" 116 117def tradefed_cc_host_outputs_generate_test_config(): 118 name = "cc_host_generate_config" 119 target = "cc_host_target_generate_config" 120 121 cc_library_static( 122 name = target, 123 tags = ["manual"], 124 ) 125 tradefed_host_driven_test( 126 name = name, 127 test_identifier = target, 128 tags = ["manual"], 129 test = target, 130 template_test_config = "//build/make/core:native_host_test_config_template.xml", 131 template_configs = [ 132 "<option name=\"config-descriptor:metadata\" key=\"parameter\" value=\"not_multi_abi\" />", 133 "<option name=\"config-descriptor:metadata\" key=\"parameter\" value=\"secondary_user\" />", 134 ], 135 target_compatible_with = ["//build/bazel/platforms/os:linux"], 136 ) 137 138 # check for expected output files (.config file and .sh script) 139 tradefed_config_generation_test( 140 name = name + "_test", 141 target_under_test = name, 142 expected_outputs = [ 143 "tradefed_test_" + name + ".sh", 144 "result-reporters.xml", 145 target + ".config", 146 ] + tradefed_dependencies, 147 target_compatible_with = ["//build/bazel/platforms/os:linux"], 148 ) 149 return name + "_test" 150 151def tradefed_test_suite(name): 152 native.test_suite( 153 name = name, 154 tests = [ 155 tradefed_cc_outputs(), 156 tradefed_cc_host_outputs(), 157 tradefed_cc_host_outputs_generate_test_config(), 158 ], 159 ) 160