Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.

Commit d3855b4

Browse files
author
Craig Furman
committed
Bazel integration for appliance tests
1 parent e01cda6 commit d3855b4

File tree

8 files changed

+109
-22
lines changed

8 files changed

+109
-22
lines changed

dev/tool_deps.bzl

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive", "http_file"
44

55
DOCSITE_VERSION = "1.9.4"
66
SRC_CLI_VERSION = "5.3.0"
7+
KUBEBUILDER_ASSETS_VERSION = "1.28.0"
78
CTAGS_VERSION = "6.0.0.2783f009"
89
PACKER_VERSION = "1.8.3"
910
P4_FUSION_VERSION = "v1.13.2-sg.04a293a"
@@ -26,6 +27,14 @@ filegroup(
2627
)
2728
"""
2829

30+
KUBEBUILDER_ASSETS_BUILDFILE = """
31+
filegroup(
32+
name = "kubebuilder-assets",
33+
srcs = glob(["*"]),
34+
visibility = ["//visibility:public"],
35+
)
36+
"""
37+
2938
GCLOUD_VERSION = "456.0.0"
3039
GCLOUD_BUILDFILE = """
3140
package(default_visibility = ["//visibility:public"])
@@ -102,6 +111,31 @@ def tool_deps():
102111
url = "https://github.com/sourcegraph/src-cli/releases/download/{0}/src-cli_{0}_darwin_arm64.tar.gz".format(SRC_CLI_VERSION),
103112
)
104113

114+
# Needed for internal/appliance tests
115+
http_archive(
116+
name = "kubebuilder-assets-darwin-arm64",
117+
build_file_content = KUBEBUILDER_ASSETS_BUILDFILE,
118+
sha256 = "c87c6b3c0aec4233e68a12dc9690bcbe2f8d6cd72c23e670602b17b2d7118325",
119+
urls = ["https://storage.googleapis.com/kubebuilder-tools/kubebuilder-tools-{}-darwin-arm64.tar.gz".format(KUBEBUILDER_ASSETS_VERSION)],
120+
strip_prefix = "kubebuilder/bin",
121+
)
122+
123+
http_archive(
124+
name = "kubebuilder-assets-darwin-amd64",
125+
build_file_content = KUBEBUILDER_ASSETS_BUILDFILE,
126+
sha256 = "a02e33a3981712c8d2702520f95357bd6c7d03d24b83a4f8ac1c89a9ba4d78c1",
127+
urls = ["https://storage.googleapis.com/kubebuilder-tools/kubebuilder-tools-{}-darwin-amd64.tar.gz".format(KUBEBUILDER_ASSETS_VERSION)],
128+
strip_prefix = "kubebuilder/bin",
129+
)
130+
131+
http_archive(
132+
name = "kubebuilder-assets-linux-amd64",
133+
build_file_content = KUBEBUILDER_ASSETS_BUILDFILE,
134+
sha256 = "8c816871604cbe119ca9dd8072b576552ae369b96eebc3cdaaf50edd7e3c0c7b",
135+
urls = ["https://storage.googleapis.com/kubebuilder-tools/kubebuilder-tools-{}-linux-amd64.tar.gz".format(KUBEBUILDER_ASSETS_VERSION)],
136+
strip_prefix = "kubebuilder/bin",
137+
)
138+
105139
# universal-ctags
106140
#
107141
# Two step process to update these. First land a commit in main updating

dev/tools/BUILD.bazel

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,13 @@ sh_binary(
107107
}),
108108
visibility = ["//visibility:public"],
109109
)
110+
111+
alias(
112+
name = "kubebuilder-assets",
113+
actual = select({
114+
"@bazel_tools//src/conditions:darwin_x86_64": "@kubebuilder-assets-darwin-amd64//:kubebuilder-assets",
115+
"@bazel_tools//src/conditions:darwin_arm64": "@kubebuilder-assets-darwin-arm64//:kubebuilder-assets",
116+
"@bazel_tools//src/conditions:linux_x86_64": "@kubebuilder-assets-linux-amd64//:kubebuilder-assets",
117+
}),
118+
visibility = ["//visibility:public"],
119+
)

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ require (
252252
github.com/aws/constructs-go/constructs/v10 v10.2.69
253253
github.com/aws/jsii-runtime-go v1.84.0
254254
github.com/bazelbuild/bazel-gazelle v0.35.0
255+
github.com/bazelbuild/rules_go v0.47.0
255256
github.com/derision-test/go-mockgen/v2 v2.0.1
256257
github.com/dghubble/gologin/v2 v2.4.0
257258
github.com/edsrzf/mmap-go v1.1.0

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -298,8 +298,8 @@ github.com/bazelbuild/bazel-gazelle v0.35.0 h1:Bvg+zEHWYwWrhJT4WxyvcU3y1DEJpT/Xl
298298
github.com/bazelbuild/bazel-gazelle v0.35.0/go.mod h1:o2+s90f3w3U6jjw0gcdok0EJOfNK0AK/9RyVP7QkRDk=
299299
github.com/bazelbuild/buildtools v0.0.0-20231115204819-d4c9dccdfbb1 h1:2Gc2Q6hVR1SJ8bBI9Ybzoggp8u/ED2WkM4MfvEIn9+c=
300300
github.com/bazelbuild/buildtools v0.0.0-20231115204819-d4c9dccdfbb1/go.mod h1:689QdV3hBP7Vo9dJMmzhoYIyo/9iMhEmHkJcnaPRCbo=
301-
github.com/bazelbuild/rules_go v0.44.0 h1:uJStI9o5obVWSwquy9WxKNWfZxf2sKA2rpEsX6x5RVM=
302-
github.com/bazelbuild/rules_go v0.44.0/go.mod h1:z7Y8GZ90V4mwgYizbNbEQKmOmx93Q3Btcel4vX7pXoc=
301+
github.com/bazelbuild/rules_go v0.47.0 h1:TTl2buKt0lqJe5s6up5FtaB1F95Wg997Lv15MWetU88=
302+
github.com/bazelbuild/rules_go v0.47.0/go.mod h1:Dhcz716Kqg1RHNWos+N6MlXNkjNP2EwZQ0LukRKJfMs=
303303
github.com/becheran/wildmatch-go v1.0.0 h1:mE3dGGkTmpKtT4Z+88t8RStG40yN9T+kFEGj2PZFSzA=
304304
github.com/becheran/wildmatch-go v1.0.0/go.mod h1:gbMvj0NtVdJ15Mg/mH9uxk2R1QCistMyU7d9KFzroX4=
305305
github.com/beevik/etree v1.1.0/go.mod h1:r8Aw8JqVegEf0w2fDnATrX9VpkMcyFeM0FhwO62wh+A=
@@ -847,8 +847,8 @@ github.com/golang/mock v1.4.1/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt
847847
github.com/golang/mock v1.4.3/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw=
848848
github.com/golang/mock v1.4.4/go.mod h1:l3mdAwkq5BuhzHwde/uurv3sEJeZMXNpwsxVWU71h+4=
849849
github.com/golang/mock v1.5.0/go.mod h1:CWnOUgYIOo4TcNZ0wHX3YZCqsaM1I1Jvs6v3mP3KVu8=
850-
github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=
851-
github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs=
850+
github.com/golang/mock v1.7.0-rc.1 h1:YojYx61/OLFsiv6Rw1Z96LpldJIy31o+UHmwAUMJ6/U=
851+
github.com/golang/mock v1.7.0-rc.1/go.mod h1:s42URUywIqd+OcERslBJvOjepvNymP31m3q8d/GkuRs=
852852
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
853853
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
854854
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=

internal/appliance/BUILD.bazel

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,31 @@ go_library(
3737
],
3838
)
3939

40+
filegroup(
41+
name = "testdata",
42+
srcs = glob(["testdata/**"]),
43+
)
44+
4045
go_test(
4146
name = "appliance_test",
4247
srcs = [
4348
"blobstore_test.go",
4449
"golden_test.go",
45-
"kubernetes_test.go",
46-
"reconcile_test.go",
50+
"helpers_test.go",
51+
],
52+
data = [
53+
":testdata",
54+
"//dev/tools:kubebuilder-assets",
4755
],
48-
data = glob(["testdata/**"]),
4956
embed = [":appliance"],
57+
env = {
58+
"KUBEBUILDER_ASSET_PATHS": "$(rlocationpaths //dev/tools:kubebuilder-assets)",
59+
},
5060
deps = [
5161
"@com_github_go_logr_stdr//:stdr",
5262
"@com_github_stretchr_testify//require",
5363
"@com_github_stretchr_testify//suite",
64+
"@io_bazel_rules_go//go/runfiles:go_default_library",
5465
"@io_k8s_api//core/v1:core",
5566
"@io_k8s_apimachinery//pkg/apis/meta/v1:meta",
5667
"@io_k8s_apimachinery//pkg/runtime/schema",

internal/appliance/development.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Appliance development
2+
3+
## Running tests
4+
5+
To (re)generate golden fixtures, pass the following argument to `go test`:
6+
7+
```
8+
go test -args appliance-update-golden-files
9+
```
10+
11+
In order to run `go test` (with or without arguments), you must have
12+
`setup-envtest` available:
13+
14+
```
15+
go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest
16+
```
17+
18+
This is not a requirement for the Bazel environment (`bazel test
19+
:appliance_test` in this module).

internal/appliance/kubernetes_test.go renamed to internal/appliance/helpers_test.go

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"strings"
1313
"testing"
1414

15+
"github.com/bazelbuild/rules_go/go/runfiles"
1516
"github.com/go-logr/stdr"
1617
"github.com/stretchr/testify/require"
1718
"github.com/stretchr/testify/suite"
@@ -58,19 +59,8 @@ func (suite *ApplianceTestSuite) setupEnvtest() {
5859
t := suite.T()
5960
logger := stdr.New(stdlog.New(os.Stderr, "", stdlog.LstdFlags))
6061

61-
// TODO figure out a way to make this work with bazel. Either download the
62-
// etcd/k8s executables that setup-envtest downloads and pass that directory
63-
// into BinaryAssetsDirectory below, or download setup-envtest without
64-
// assuming it's already on the PATH.
65-
// Ideally these assets could be cached in buildkite too.
66-
setupEnvTestCmd := exec.Command("setup-envtest", "use", "1.28.0", "--bin-dir", "/tmp/envtest", "-p", "path")
67-
var envtestOut bytes.Buffer
68-
setupEnvTestCmd.Stdout = &envtestOut
69-
err := setupEnvTestCmd.Run()
70-
require.NoError(t, err)
71-
7262
suite.testEnv = &envtest.Environment{
73-
BinaryAssetsDirectory: strings.TrimSpace(envtestOut.String()),
63+
BinaryAssetsDirectory: suite.kubebuilderAssetPath(),
7464
}
7565
cfg, err := suite.testEnv.Start()
7666
require.NoError(t, err)
@@ -107,6 +97,31 @@ func (suite *ApplianceTestSuite) setupEnvtest() {
10797
}()
10898
}
10999

100+
func (suite *ApplianceTestSuite) kubebuilderAssetPath() string {
101+
if os.Getenv("BAZEL_TEST") == "" {
102+
return suite.kubebuilderAssetPathLocalDev()
103+
}
104+
105+
assetPaths := strings.Split(os.Getenv("KUBEBUILDER_ASSET_PATHS"), " ")
106+
suite.Require().Greater(len(assetPaths), 0)
107+
arbAssetPath, err := runfiles.Rlocation(assetPaths[0])
108+
suite.Require().NoError(err)
109+
return filepath.Dir(arbAssetPath)
110+
}
111+
112+
// In the hermetic bazel environment, we skip setup-envtest, handling the assets
113+
// directly in a hermetic and cachable way.
114+
// If we're using `go test`, which can be convenient for local dev, we fall back
115+
// on expecting setup-envtest to be present on the developer machine.
116+
func (suite *ApplianceTestSuite) kubebuilderAssetPathLocalDev() string {
117+
setupEnvTestCmd := exec.Command("setup-envtest", "use", "1.28.0", "--bin-dir", "/tmp/envtest", "-p", "path")
118+
var envtestOut bytes.Buffer
119+
setupEnvTestCmd.Stdout = &envtestOut
120+
err := setupEnvTestCmd.Run()
121+
suite.Require().NoError(err, "Did you remember to `go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest`?")
122+
return strings.TrimSpace(envtestOut.String())
123+
}
124+
110125
func (suite *ApplianceTestSuite) createConfigMap(fixtureFileName string) string {
111126
// Create a random namespace for each test
112127
namespace := "test-appliance-" + suite.randomSlug()

internal/appliance/reconcile_test.go

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)