xref: /aosp_15_r20/build/bazel/rules/bpf/bpf_test.bzl (revision 7594170e27e0732bc44b93d1440d87a54b6ffe7c)
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(":bpf.bzl", "bpf")
17
18def _basic_bpf_test_impl(ctx):
19    env = analysistest.begin(ctx)
20    actions = analysistest.target_actions(env)
21    bpf_target = analysistest.target_under_test(env)
22
23    if len(ctx.attr.expected_flags) > 0:
24        for flag in ctx.attr.expected_flags:
25            asserts.true(
26                env,
27                flag in actions[0].argv,
28                "Expected flag (%s) is not in actual flags" % (flag),
29            )
30
31    if len(ctx.attr.unexpected_flags) > 0:
32        for flag in ctx.attr.unexpected_flags:
33            asserts.true(
34                env,
35                flag not in actions[0].argv,
36                "Unexpected flag (%s) is in actual flags" % (flag),
37            )
38
39    if len(ctx.attr.includes) > 0:
40        for dir in ctx.attr.includes:
41            index = actions[0].argv.index(dir)
42            asserts.true(
43                env,
44                actions[0].argv[index - 1] == "-I",
45                "Directory %s is not after '-I' tag in clang command" % (dir),
46            )
47
48    asserts.equals(
49        env,
50        expected = 2 if ctx.attr.expect_strip else 1,
51        actual = len(actions),
52    )
53
54    if ctx.attr.expect_strip:
55        asserts.true(
56            env,
57            actions[-1].argv[0].endswith("llvm-strip"),
58            "No strip action is executed when btf is True",
59        )
60
61    asserts.true(
62        env,
63        "unstripped" not in bpf_target[DefaultInfo].files.to_list()[0].path,
64        "'unstripped' is in the output file path",
65    )
66
67    return analysistest.end(env)
68
69basic_bpf_test = analysistest.make(
70    _basic_bpf_test_impl,
71    attrs = {
72        "expected_flags": attr.string_list(),
73        "unexpected_flags": attr.string_list(),
74        "includes": attr.string_list(),
75        "expect_strip": attr.bool(),
76    },
77)
78
79def bpf_fail_test_impl(ctx):
80    env = analysistest.begin(ctx)
81
82    asserts.expect_failure(
83        env,
84        "Invalid character '_' in source name",
85    )
86
87    return analysistest.end(env)
88
89bpf_fail_test = analysistest.make(
90    bpf_fail_test_impl,
91    expect_failure = True,
92)
93
94def test_all_attrs_btf_true():
95    name = "all_attrs_btf_true_test"
96    copts = ["cflag1", "cflag2"]
97    absolute_includes = ["foo/bar1", "foo/bar2"]
98    bpf(
99        name = name + "_target",
100        srcs = ["testAllAttrsBtfTrueSrc.c"],
101        copts = copts,
102        absolute_includes = absolute_includes,
103        btf = True,
104        tags = ["manual"],
105    )
106    basic_bpf_test(
107        name = name,
108        target_under_test = name + "_target",
109        expected_flags = ["-g"] + copts,
110        includes = absolute_includes,
111        expect_strip = True,
112    )
113    return name
114
115def test_btf_false():
116    name = "btf_false_test"
117    bpf(
118        name = name + "_target",
119        srcs = ["testBtfFalse.c"],
120        copts = ["copts1", "copts2"],
121        absolute_includes = ["foo/bar1", "foo/bar2"],
122        btf = False,
123        tags = ["manual"],
124    )
125    basic_bpf_test(
126        name = name,
127        target_under_test = name + "_target",
128        unexpected_flags = ["-g"],
129        expect_strip = False,
130    )
131    return name
132
133def test_invalid_src_name():
134    name = "invalid_src_name_test"
135    bpf(
136        name = name + "_target",
137        srcs = [name + "_src.c"],
138        copts = ["copts1", "copts2"],
139        absolute_includes = ["foo/bar1", "foo/bar2"],
140        btf = True,
141        tags = ["manual"],
142    )
143    bpf_fail_test(
144        name = name,
145        target_under_test = name + "_target",
146    )
147    return name
148
149def bpf_test_suite(name):
150    native.test_suite(
151        name = name,
152        tests = [
153            test_all_attrs_btf_true(),
154            test_btf_false(),
155            test_invalid_src_name(),
156        ],
157    )
158