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:paths.bzl", "paths")
16load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts")
17load("//build/bazel/rules/cc:cc_binary.bzl", "cc_binary")
18load("//build/bazel/rules/cc:cc_library_shared.bzl", "cc_library_shared")
19load("//build/bazel/rules/cc:cc_library_static.bzl", "cc_library_static")
20load(
21    "//build/bazel/rules/test_common:paths.bzl",
22    "get_output_and_package_dir_based_path",
23)
24load(":tradefed.bzl", "tradefed_device_driven_test", "tradefed_deviceless_test", "tradefed_host_driven_device_test")
25
26def _test_tradefed_config_generation_impl(ctx):
27    env = analysistest.begin(ctx)
28    actions = analysistest.target_actions(env)
29
30    actual_outputs = []
31    for action in actions:
32        for output in action.outputs.to_list():
33            actual_outputs.append(output.path)
34
35    for expected_output in ctx.attr.expected_outputs:
36        expected_output = get_output_and_package_dir_based_path(env, expected_output)
37        asserts.true(
38            env,
39            expected_output in actual_outputs,
40            "Expected: " + expected_output +
41            " in outputs: " + str(actual_outputs),
42        )
43    return analysistest.end(env)
44
45tradefed_config_generation_test = analysistest.make(
46    _test_tradefed_config_generation_impl,
47    attrs = {
48        "expected_outputs": attr.string_list(),
49    },
50)
51
52def tradefed_cc_outputs():
53    name = "cc"
54    target = "cc_target"
55    dep_name = name + "_lib"
56
57    cc_library_static(
58        name = dep_name,
59        tags = ["manual"],
60    )
61    native.cc_test(
62        name = target,
63        deps = [dep_name],
64        tags = ["manual"],
65    )
66    tradefed_device_driven_test(
67        name = name,
68        tags = ["manual"],
69        test = target,
70        test_config = "//build/bazel/rules/tradefed/test:example_config.xml",
71        dynamic_config = "//build/bazel/rules/tradefed/test:dynamic_config.xml",
72        target_compatible_with = ["//build/bazel_common_rules/platforms/os:linux"],
73    )
74
75    # check for expected output files (.config file  and .sh script)
76    tradefed_config_generation_test(
77        name = name + "_test",
78        target_under_test = name,
79        expected_outputs = [
80            name + ".sh",
81            "result-reporters.xml",
82            paths.join(name, "testcases", target + ".config"),
83            paths.join(name, "testcases", target + ".dynamic"),
84        ],
85        target_compatible_with = ["//build/bazel_common_rules/platforms/os:linux"],
86    )
87    return name + "_test"
88
89def tradefed_cc_host_outputs():
90    name = "cc_host"
91    target = "cc_host_target"
92    dep_name = name + "_lib"
93
94    cc_library_static(
95        name = dep_name,
96        tags = ["manual"],
97    )
98    native.cc_test(
99        name = target,
100        deps = [dep_name],
101        tags = ["manual"],
102    )
103    tradefed_host_driven_device_test(
104        name = name,
105        tags = ["manual"],
106        test = target,
107        test_config = "//build/bazel/rules/tradefed/test:example_config.xml",
108        dynamic_config = "//build/bazel/rules/tradefed/test:dynamic_config.xml",
109        target_compatible_with = ["//build/bazel_common_rules/platforms/os:linux"],
110    )
111
112    # check for expected output files (.config file  and .sh script)
113    tradefed_config_generation_test(
114        name = name + "_test",
115        target_under_test = name,
116        expected_outputs = [
117            name + ".sh",
118            "result-reporters.xml",
119            paths.join(name, "testcases", target + ".config"),
120            paths.join(name, "testcases", target + ".dynamic"),
121        ],
122        target_compatible_with = ["//build/bazel_common_rules/platforms/os:linux"],
123    )
124    return name + "_test"
125
126def tradefed_cc_host_outputs_generate_test_config():
127    name = "cc_host_generate_config"
128    target = "cc_host_target_generate_config"
129    dep_name = name + "_lib"
130
131    cc_library_static(
132        name = dep_name,
133        tags = ["manual"],
134    )
135    native.cc_test(
136        name = target,
137        deps = [dep_name],
138        tags = ["manual"],
139    )
140    tradefed_host_driven_device_test(
141        name = name,
142        tags = ["manual"],
143        test = target,
144        template_test_config = "//build/make/core:native_host_test_config_template.xml",
145        template_configs = [
146            "<option name=\"config-descriptor:metadata\" key=\"parameter\" value=\"not_multi_abi\" />",
147            "<option name=\"config-descriptor:metadata\" key=\"parameter\" value=\"secondary_user\" />",
148        ],
149        dynamic_config = "//build/bazel/rules/tradefed/test:dynamic_config.xml",
150        target_compatible_with = ["//build/bazel_common_rules/platforms/os:linux"],
151    )
152
153    # check for expected output files (.config file  and .sh script)
154    tradefed_config_generation_test(
155        name = name + "_test",
156        target_under_test = name,
157        expected_outputs = [
158            name + ".sh",
159            "result-reporters.xml",
160            paths.join(name, "testcases", target + ".config"),
161            paths.join(name, "testcases", target + ".dynamic"),
162        ],
163        target_compatible_with = ["//build/bazel_common_rules/platforms/os:linux"],
164    )
165    return name + "_test"
166
167def _tradefed_cc_copy_runfiles_test_impl(ctx):
168    env = analysistest.begin(ctx)
169    target = analysistest.target_under_test(env)
170    actions = analysistest.target_actions(env)
171    copy_actions = [a for a in actions if a.mnemonic == "CopyFile"]
172    outputs = []
173    for action in copy_actions:
174        for output in action.outputs.to_list():
175            outputs.append(output.path)
176
177    for expect in ctx.attr.expected_files:
178        expect = get_output_and_package_dir_based_path(env, paths.join(target.label.name, "testcases", expect))
179        asserts.true(
180            env,
181            expect in outputs,
182            "Expected: " + expect +
183            " in outputs: " + str(outputs),
184        )
185
186    return analysistest.end(env)
187
188tradefed_cc_copy_runfiles_test = analysistest.make(
189    _tradefed_cc_copy_runfiles_test_impl,
190    attrs = {
191        "expected_files": attr.string_list(
192            doc = "Files should be copied.",
193        ),
194    },
195    config_settings = {
196        "//command_line_option:platforms": "@//build/bazel/tests/products:aosp_x86_64_for_testing",
197    },
198)
199
200def tradefed_cc_copy_runfiles():
201    name = "tradefed_cc_copy_runfiles"
202    test_name = name + "_test"
203
204    cc_library_shared(
205        name = name + "_shared_lib_1",
206        srcs = [name + "_shared_lib_1.cc"],
207        dynamic_deps = [name + "_shared_lib_2"],
208        tags = ["manual"],
209    )
210
211    cc_library_shared(
212        name = name + "_shared_lib_2",
213        srcs = [name + "_shared_lib_2.cc"],
214        tags = ["manual"],
215    )
216
217    cc_library_shared(
218        name = name + "_shared_lib_3",
219        srcs = [name + "_shared_lib_3.cc"],
220        tags = ["manual"],
221    )
222
223    cc_library_shared(
224        name = name + "_shared_lib_4",
225        srcs = [name + "_shared_lib_4.cc"],
226        tags = ["manual"],
227    )
228
229    cc_library_static(
230        name = name + "_static_lib_1",
231        srcs = [name + "_static_lib_1.cc"],
232        dynamic_deps = [name + "_shared_lib_3"],
233        tags = ["manual"],
234    )
235
236    cc_binary(
237        name = name + "__tf_internal",
238        generate_cc_test = True,
239        deps = [name + "_static_lib_1"],
240        dynamic_deps = [name + "_shared_lib_1"],
241        runtime_deps = [name + "_shared_lib_4"],
242        data = ["data/a.text"],
243        tags = ["manual"],
244    )
245
246    tradefed_deviceless_test(
247        name = name,
248        tags = ["manual"],
249        test = name + "__tf_internal",
250        test_config = "//build/bazel/rules/tradefed/test:example_config.xml",
251        dynamic_config = "//build/bazel/rules/tradefed/test:dynamic_config.xml",
252    )
253
254    tradefed_cc_copy_runfiles_test(
255        name = test_name,
256        target_under_test = name,
257        expected_files = [
258            "tradefed_cc_copy_runfiles.config",
259            "tradefed_cc_copy_runfiles.dynamic",
260            "tradefed_cc_copy_runfiles",
261            "data/a.text",
262            "lib64/tradefed_cc_copy_runfiles_shared_lib_1.so",
263            "lib64/tradefed_cc_copy_runfiles_shared_lib_2.so",
264            "lib64/tradefed_cc_copy_runfiles_shared_lib_3.so",
265            "lib64/tradefed_cc_copy_runfiles_shared_lib_4.so",
266        ],
267    )
268
269    return test_name
270
271def tradefed_cc_copy_runfiles_with_suffix():
272    name = "tradefed_cc_copy_runfiles_with_suffix"
273    test_name = name + "_test"
274    suffix = "64"
275
276    cc_library_shared(
277        name = name + "_shared_lib_1",
278        srcs = [name + "_shared_lib_1.cc"],
279        tags = ["manual"],
280    )
281
282    cc_binary(
283        name = name + "__tf_internal",
284        generate_cc_test = True,
285        dynamic_deps = [name + "_shared_lib_1"],
286        data = ["data/a.text"],
287        tags = ["manual"],
288        suffix = suffix,
289    )
290
291    tradefed_deviceless_test(
292        name = name,
293        tags = ["manual"],
294        test = name + "__tf_internal",
295        test_config = "//build/bazel/rules/tradefed/test:example_config.xml",
296        dynamic_config = "//build/bazel/rules/tradefed/test:dynamic_config.xml",
297        suffix = suffix,
298    )
299
300    tradefed_cc_copy_runfiles_test(
301        name = test_name,
302        target_under_test = name,
303        expected_files = [
304            "tradefed_cc_copy_runfiles_with_suffix64.config",
305            "tradefed_cc_copy_runfiles_with_suffix64.dynamic",
306            "tradefed_cc_copy_runfiles_with_suffix64",
307            "data/a.text",
308            "lib64/tradefed_cc_copy_runfiles_with_suffix_shared_lib_1.so",
309        ],
310    )
311
312    return test_name
313
314def _tradefed_cc_compat_suffix_test_impl(ctx):
315    env = analysistest.begin(ctx)
316    target = analysistest.target_under_test(env)
317    actions = analysistest.target_actions(env)
318    symlink_actions = [a for a in actions if a.mnemonic == "Symlink"]
319    outputs = []
320    for action in symlink_actions:
321        for output in action.outputs.to_list():
322            outputs.append(output.path)
323
324    for expect in ctx.attr.expected_files:
325        expect = get_output_and_package_dir_based_path(env, paths.join(target.label.name, "testcases", expect))
326        asserts.true(
327            env,
328            expect in outputs,
329            "Expected: " + expect +
330            " in outputs: " + str(outputs),
331        )
332
333    return analysistest.end(env)
334
335tradefed_cc_compat_suffix_test = analysistest.make(
336    _tradefed_cc_compat_suffix_test_impl,
337    attrs = {
338        "expected_files": attr.string_list(
339            doc = "Files to be symlinked",
340        ),
341    },
342    config_settings = {
343        "//command_line_option:platforms": "@//build/bazel/tests/products:aosp_x86_64_for_testing",
344    },
345)
346
347def tradefed_cc_test_suffix_has_suffixless_compat_symlink():
348    name = "tradefed_cc_test_suffix_has_suffixless_compat_symlink"
349    test_name = name + "_test"
350    suffix = "64"
351
352    cc_binary(
353        name = name + "__tf_internal",
354        generate_cc_test = True,
355        tags = ["manual"],
356        suffix = suffix,
357    )
358
359    tradefed_deviceless_test(
360        name = name,
361        tags = ["manual"],
362        test = name + "__tf_internal",
363        test_config = "//build/bazel/rules/tradefed/test:example_config.xml",
364        suffix = suffix,
365    )
366
367    tradefed_cc_compat_suffix_test(
368        name = test_name,
369        target_under_test = name,
370        expected_files = [
371            "tradefed_cc_test_suffix_has_suffixless_compat_symlink",
372        ],
373    )
374
375    return test_name
376
377def tradefed_test_suite(name):
378    native.test_suite(
379        name = name,
380        tests = [
381            tradefed_cc_outputs(),
382            tradefed_cc_host_outputs(),
383            tradefed_cc_host_outputs_generate_test_config(),
384            tradefed_cc_copy_runfiles(),
385            tradefed_cc_copy_runfiles_with_suffix(),
386            tradefed_cc_test_suffix_has_suffixless_compat_symlink(),
387        ],
388    )
389