Skip to content

Commit 7418217

Browse files
authored
Improve Bazel test coverage to get it closer to the legacy (#54628)
<!--Please give us some feedback on your experience writing this PR ! https://app.datadoghq.com/forms/43db4c02-6837-400c-8083-692e141b1b88 !--> ### What does this PR do? This PR introduces some changes here and there to bridge the gap in the reported code coverage between Bazel and the legacy approach. Changes worth mentioning: - There was a bug in the fallback mechanism in our custom Gazelle extension that sometimes returned no go tags to set at all. Now the fallback will come back with a minimal set of tags that is needed to build and run a test binary. - `cel` tag wasn't set in some cases at all, so we add 2 new custom tag sets. - `gazelle:dd_agent_go_test off` directives were removed in files that were regenerated by Gazelle. Co-authored-by: joseph.gette <joseph.gette@datadoghq.com>
1 parent cc47859 commit 7418217

53 files changed

Lines changed: 223 additions & 83 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

BUILD.bazel

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
# Global build settings
22
# gazelle:dd_agent_go_test on
3-
# gazelle:go_canonical_test_tag_set docker kubelet
43
# gazelle:go_canonical_test_tag_set cel clusterchecks kubeapiserver kubelet orchestrator
54
# gazelle:go_canonical_test_tag_set linux_bpf
5+
# gazelle:go_canonical_test_tag_set containerd cel
6+
# gazelle:go_canonical_test_tag_set docker kubelet cel
67

78
load("@bazel_lib//lib:write_source_files.bzl", "write_source_file", "write_source_files")
89
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag", "string_flag")

bazel/AGENTS.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,26 @@ package-local tag combinations from `//go:build` constraints. Dependency-only op
302302
tags it names. Gazelle uses those combinations instead of unsafe partial modes and only considers
303303
embedded library constraints when a configured combination satisfies them.
304304

305+
Some `//go:build` constraints name a tag that a canonical set covers *and* a tag that no canonical
306+
set mentions. `//go:build trivy && containerd` is one: `containerd` belongs to the canonical set
307+
`containerd cel`, but no canonical set mentions `trivy`, so that set on its own cannot compile the
308+
file. Gazelle does not give up there — it derives the minimal combination the constraint needs
309+
(`containerd trivy`) and adds every canonical set that shares a tag with it, producing
310+
`cel containerd trivy`. The canonical grouping is still honoured, and the sources still get a test
311+
target.
312+
313+
This only applies when the canonical set and the constraint can coexist. A constraint that
314+
contradicts a canonical set produces no combination at all, and its sources stay out of the wildcard
315+
test runs. For example `//go:build kubeapiserver && !kubelet` grows to include `kubelet`, because
316+
`kubeapiserver` and `kubelet` share the canonical set
317+
`cel clusterchecks kubeapiserver kubelet orchestrator` — and the result then fails the constraint's
318+
own `!kubelet`.
319+
320+
Combinations built this way can be long, and target names are budgeted against the Windows runfiles
321+
path length (see the Windows section). When `dd_agent_go_test` fails with a path-length error, add a
322+
short suffix for the combination to `_TAG_SET_SUFFIX_ALIASES` in
323+
`//bazel/rules/go:dd_agent_go_test.bzl`.
324+
305325
## Starlark language
306326

307327
Starlark is Python-like but with deliberate restrictions for hermeticity and parallelism. Key divergences:

bazel/rules/go/_gazelle_extension.go

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -486,12 +486,42 @@ func tagSetsForExpression(expr constraint.Expr, baseTags map[string]bool, config
486486
if len(matches) > 0 {
487487
return mergeTagSets(matches)
488488
}
489-
if referencesAnyTag(expr, configuredTags) || !deriveTagSets {
490-
return mergeTagSets(matches)
489+
if !deriveTagSets {
490+
return nil
491+
}
492+
if referencesAnyTag(expr, configuredTags) {
493+
return canonicalizedTagSets(expr, baseTags, configuredTagSets)
491494
}
492495
return minimalTagSets(expr, baseTags)
493496
}
494497

498+
// canonicalizedTagSets handles constraints that pair a canonical tag with tags
499+
// no canonical set declares, such as `trivy && containerd` where the only set
500+
// naming containerd is {cel, containerd}. The set alone cannot satisfy the
501+
// constraint, but the grouping it expresses still holds, so each minimal set is
502+
// grown by the canonical sets it touches. Growth is measured against the
503+
// minimal set rather than the accumulating result: canonical sets frequently
504+
// share a tag, and re-scanning would chain them all together.
505+
//
506+
// A constraint that contradicts the grouping — `kubeapiserver && !kubelet`
507+
// against {cel, clusterchecks, kubeapiserver, kubelet, orchestrator} — survives
508+
// no such growth and still yields nothing.
509+
func canonicalizedTagSets(expr constraint.Expr, baseTags map[string]bool, configuredTagSets [][]string) [][]string {
510+
var out [][]string
511+
for _, minimal := range minimalTagSets(expr, baseTags) {
512+
candidate := minimal
513+
for _, tagSet := range configuredTagSets {
514+
if stringSetsIntersect(minimal, tagSet) {
515+
candidate = normalizeTagSet(append(append([]string(nil), candidate...), tagSet...))
516+
}
517+
}
518+
if canSatisfy(expr, activeTagSet(baseTags, candidate)) {
519+
out = append(out, candidate)
520+
}
521+
}
522+
return mergeTagSets(out)
523+
}
524+
495525
func referencesAnyTag(expr constraint.Expr, tags map[string]bool) bool {
496526
switch e := expr.(type) {
497527
case *constraint.TagExpr:

bazel/rules/go/_gazelle_extension_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,7 @@ func TestApplicableTagSets(t *testing.T) {
707707
tagCombined := write("combo_test.go", "//go:build kubeapiserver && linux")
708708
twoTags := write("two_tags_test.go", "//go:build trivy && containerd")
709709
relatedTags := write("related_tags_test.go", "//go:build trivy && docker")
710+
crioTags := write("crio_tags_test.go", "//go:build trivy && crio")
710711
oneTag := write("one_tag_test.go", "//go:build trivy")
711712
negativeTag := write("negative_tag_test.go", "//go:build zlib && !zstd")
712713
compression := write("compression_test.go", "//go:build zlib && zstd")
@@ -717,6 +718,8 @@ func TestApplicableTagSets(t *testing.T) {
717718
orchestrator := write("orchestrator_test.go", "//go:build orchestrator")
718719
kubeAPIServerWithoutKubelet := write("kube_no_kubelet_test.go", "//go:build kubeapiserver && !kubelet")
719720
kubernetesTagSet := tagsList("cel", "clusterchecks", "kubeapiserver", "kubelet", "orchestrator")
721+
containerdTagSet := tagsList("cel", "containerd")
722+
dockerTagSet := tagsList("cel", "docker", "kubelet")
720723
var manyTagNames []string
721724
for tag := range AutoTestTags {
722725
manyTagNames = append(manyTagNames, tag)
@@ -861,6 +864,24 @@ func TestApplicableTagSets(t *testing.T) {
861864
configuredTagSets: [][]string{kubernetesTagSet},
862865
wantTagSets: [][]string{tagsList("linux_bpf")},
863866
},
867+
{
868+
name: "configured set grows to cover tags it does not name",
869+
srcs: []string{twoTags},
870+
configuredTagSets: [][]string{containerdTagSet},
871+
wantTagSets: [][]string{tagsList("cel", "containerd", "trivy")},
872+
},
873+
{
874+
name: "only the configured sets a constraint touches are added",
875+
srcs: []string{relatedTags},
876+
configuredTagSets: [][]string{containerdTagSet, dockerTagSet},
877+
wantTagSets: [][]string{tagsList("cel", "docker", "kubelet", "trivy")},
878+
},
879+
{
880+
name: "unconfigured alternative coalesces with the grown set",
881+
srcs: []string{twoTags, crioTags},
882+
configuredTagSets: [][]string{containerdTagSet},
883+
wantTagSets: [][]string{tagsList("cel", "containerd", "crio", "trivy")},
884+
},
864885
{
865886
name: "embedded library selects configured set",
866887
srcs: []string{noConstraint},

bazel/rules/go/dd_agent_go_test.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ load(
1515
_TAG_SET_SUFFIX_ALIASES = {
1616
"cel+clusterchecks+kubeapiserver+kubelet+orchestrator": "dca",
1717
"cel+clusterchecks+docker+kubeapiserver+kubelet+orchestrator": "dca_docker",
18+
"cel+clusterchecks+containerd+docker+kubeapiserver+kubelet+orchestrator": "dca_containerd_docker",
19+
"cel+clusterchecks+docker+kubeapiserver+kubelet+orchestrator+python": "dca_docker_python",
1820
}
1921

2022
# Windows caps a process's current directory at MAX_PATH even where longer paths

cmd/agent/common/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ dd_agent_go_test(
6666
data = glob(["testdata/**"]),
6767
embed = [":common"],
6868
gotags_sets = [[
69+
"cel",
6970
"docker",
7071
"kubelet",
7172
]],

cmd/cluster-agent/subcommands/coverage/BUILD.bazel

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
load("@rules_go//go:def.bzl", "go_library", "go_test")
2-
3-
# gazelle:dd_agent_go_test off
1+
load("@rules_go//go:def.bzl", "go_library")
2+
load("//bazel/rules/go:dd_agent_go_test.bzl", "dd_agent_go_test")
43

54
go_library(
65
name = "coverage",
@@ -195,15 +194,18 @@ go_library(
195194
}),
196195
)
197196

198-
go_test(
197+
dd_agent_go_test(
199198
name = "coverage_test",
200199
srcs = ["command_test.go"],
201200
embed = [":coverage"],
202-
gotags = [
203-
"test",
201+
gotags_sets = [[
202+
"cel",
203+
"clusterchecks",
204204
"kubeapiserver",
205-
"e2ecoverage",
206-
], # keep
205+
"kubelet",
206+
"orchestrator",
207+
]],
208+
include_default = False,
207209
deps = select({
208210
"@rules_go//go/platform:aix": [
209211
"//cmd/cluster-agent/command",

cmd/installer/BUILD.bazel

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
load("@rules_go//go:def.bzl", "go_library", "go_test")
1+
load("@rules_go//go:def.bzl", "go_library")
2+
load("//bazel/rules/go:dd_agent_go_test.bzl", "dd_agent_go_test")
23
load("//bazel/rules/go:go_binary.bzl", "dd_agent_go_binary")
34
load("//tasks:build_tags.bzl", "INSTALLER_TAGS")
45

5-
# gazelle:dd_agent_go_test off
6-
76
go_library(
87
name = "installer_lib",
98
srcs = ["main.go"],
@@ -37,11 +36,10 @@ dd_agent_go_binary(
3736
visibility = ["//visibility:public"],
3837
)
3938

40-
go_test(
39+
dd_agent_go_test(
4140
name = "installer_test",
4241
srcs = ["main_test.go"],
4342
embed = [":installer_lib"],
44-
gotags = sorted(["test"] + list(INSTALLER_TAGS)),
4543
deps = [
4644
"//pkg/fleet/installer/commands",
4745
"@com_github_spf13_cobra//:cobra",

cmd/system-probe/modules/BUILD.bazel

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,13 @@ dd_agent_go_test(
261261
"traceroute_test.go",
262262
],
263263
embed = [":modules"],
264-
gotags_sets = [["linux_bpf"]],
264+
gotags_sets = [
265+
["linux_bpf"],
266+
[
267+
"linux_bpf",
268+
"nvml",
269+
],
270+
],
265271
deps = select({
266272
"@rules_go//go/platform:android": [
267273
"//pkg/compliance/dbconfig",

comp/core/autodiscovery/common/utils/BUILD.bazel

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
load("@rules_go//go:def.bzl", "go_library")
22
load("//bazel/rules/go:dd_agent_go_test.bzl", "dd_agent_go_test")
33

4-
# gazelle:dd_agent_go_test on
5-
64
go_library(
75
name = "utils",
86
srcs = [
@@ -62,6 +60,7 @@ dd_agent_go_test(
6260
"orchestrator",
6361
],
6462
[
63+
"cel",
6564
"docker",
6665
"kubelet",
6766
],

0 commit comments

Comments
 (0)