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

appliance: golden/snapshot testing #62034

Merged
merged 15 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -643,8 +643,8 @@ def go_dependencies():
name = "com_github_bazelbuild_rules_go",
build_file_proto_mode = "disable_global",
importpath = "github.com/bazelbuild/rules_go",
sum = "h1:uJStI9o5obVWSwquy9WxKNWfZxf2sKA2rpEsX6x5RVM=",
version = "v0.44.0",
sum = "h1:TTl2buKt0lqJe5s6up5FtaB1F95Wg997Lv15MWetU88=",
version = "v0.47.0",
)
go_repository(
name = "com_github_becheran_wildmatch_go",
Expand Down Expand Up @@ -2398,8 +2398,8 @@ def go_dependencies():
name = "com_github_golang_mock",
build_file_proto_mode = "disable_global",
importpath = "github.com/golang/mock",
sum = "h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=",
version = "v1.6.0",
sum = "h1:YojYx61/OLFsiv6Rw1Z96LpldJIy31o+UHmwAUMJ6/U=",
version = "v1.7.0-rc.1",
)
go_repository(
name = "com_github_golang_protobuf",
Expand Down
34 changes: 34 additions & 0 deletions dev/tool_deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive", "http_file"

DOCSITE_VERSION = "1.9.4"
SRC_CLI_VERSION = "5.3.0"
KUBEBUILDER_ASSETS_VERSION = "1.28.0"
CTAGS_VERSION = "6.0.0.2783f009"
PACKER_VERSION = "1.8.3"
P4_FUSION_VERSION = "v1.13.2-sg.04a293a"
Expand All @@ -26,6 +27,14 @@ filegroup(
)
"""

KUBEBUILDER_ASSETS_BUILDFILE = """
filegroup(
name = "kubebuilder-assets",
srcs = glob(["*"]),
visibility = ["//visibility:public"],
)
"""

GCLOUD_VERSION = "456.0.0"
GCLOUD_BUILDFILE = """
package(default_visibility = ["//visibility:public"])
Expand Down Expand Up @@ -102,6 +111,31 @@ def tool_deps():
url = "https://github.com/sourcegraph/src-cli/releases/download/{0}/src-cli_{0}_darwin_arm64.tar.gz".format(SRC_CLI_VERSION),
)

# Needed for internal/appliance tests
http_archive(
name = "kubebuilder-assets-darwin-arm64",
build_file_content = KUBEBUILDER_ASSETS_BUILDFILE,
sha256 = "c87c6b3c0aec4233e68a12dc9690bcbe2f8d6cd72c23e670602b17b2d7118325",
urls = ["https://storage.googleapis.com/kubebuilder-tools/kubebuilder-tools-{}-darwin-arm64.tar.gz".format(KUBEBUILDER_ASSETS_VERSION)],
strip_prefix = "kubebuilder/bin",
)

http_archive(
name = "kubebuilder-assets-darwin-amd64",
build_file_content = KUBEBUILDER_ASSETS_BUILDFILE,
sha256 = "a02e33a3981712c8d2702520f95357bd6c7d03d24b83a4f8ac1c89a9ba4d78c1",
urls = ["https://storage.googleapis.com/kubebuilder-tools/kubebuilder-tools-{}-darwin-amd64.tar.gz".format(KUBEBUILDER_ASSETS_VERSION)],
strip_prefix = "kubebuilder/bin",
)

http_archive(
name = "kubebuilder-assets-linux-amd64",
build_file_content = KUBEBUILDER_ASSETS_BUILDFILE,
sha256 = "8c816871604cbe119ca9dd8072b576552ae369b96eebc3cdaaf50edd7e3c0c7b",
urls = ["https://storage.googleapis.com/kubebuilder-tools/kubebuilder-tools-{}-linux-amd64.tar.gz".format(KUBEBUILDER_ASSETS_VERSION)],
strip_prefix = "kubebuilder/bin",
)

# universal-ctags
#
# Two step process to update these. First land a commit in main updating
Expand Down
10 changes: 10 additions & 0 deletions dev/tools/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,13 @@ sh_binary(
}),
visibility = ["//visibility:public"],
)

alias(
name = "kubebuilder-assets",
actual = select({
"@bazel_tools//src/conditions:darwin_x86_64": "@kubebuilder-assets-darwin-amd64//:kubebuilder-assets",
"@bazel_tools//src/conditions:darwin_arm64": "@kubebuilder-assets-darwin-arm64//:kubebuilder-assets",
"@bazel_tools//src/conditions:linux_x86_64": "@kubebuilder-assets-linux-amd64//:kubebuilder-assets",
}),
visibility = ["//visibility:public"],
)
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ require (
github.com/aws/constructs-go/constructs/v10 v10.2.69
github.com/aws/jsii-runtime-go v1.84.0
github.com/bazelbuild/bazel-gazelle v0.35.0
github.com/bazelbuild/rules_go v0.47.0
github.com/derision-test/go-mockgen/v2 v2.0.1
github.com/dghubble/gologin/v2 v2.4.0
github.com/edsrzf/mmap-go v1.1.0
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,8 @@ github.com/bazelbuild/bazel-gazelle v0.35.0 h1:Bvg+zEHWYwWrhJT4WxyvcU3y1DEJpT/Xl
github.com/bazelbuild/bazel-gazelle v0.35.0/go.mod h1:o2+s90f3w3U6jjw0gcdok0EJOfNK0AK/9RyVP7QkRDk=
github.com/bazelbuild/buildtools v0.0.0-20231115204819-d4c9dccdfbb1 h1:2Gc2Q6hVR1SJ8bBI9Ybzoggp8u/ED2WkM4MfvEIn9+c=
github.com/bazelbuild/buildtools v0.0.0-20231115204819-d4c9dccdfbb1/go.mod h1:689QdV3hBP7Vo9dJMmzhoYIyo/9iMhEmHkJcnaPRCbo=
github.com/bazelbuild/rules_go v0.44.0 h1:uJStI9o5obVWSwquy9WxKNWfZxf2sKA2rpEsX6x5RVM=
github.com/bazelbuild/rules_go v0.44.0/go.mod h1:z7Y8GZ90V4mwgYizbNbEQKmOmx93Q3Btcel4vX7pXoc=
github.com/bazelbuild/rules_go v0.47.0 h1:TTl2buKt0lqJe5s6up5FtaB1F95Wg997Lv15MWetU88=
github.com/bazelbuild/rules_go v0.47.0/go.mod h1:Dhcz716Kqg1RHNWos+N6MlXNkjNP2EwZQ0LukRKJfMs=
github.com/becheran/wildmatch-go v1.0.0 h1:mE3dGGkTmpKtT4Z+88t8RStG40yN9T+kFEGj2PZFSzA=
github.com/becheran/wildmatch-go v1.0.0/go.mod h1:gbMvj0NtVdJ15Mg/mH9uxk2R1QCistMyU7d9KFzroX4=
github.com/beevik/etree v1.1.0/go.mod h1:r8Aw8JqVegEf0w2fDnATrX9VpkMcyFeM0FhwO62wh+A=
Expand Down Expand Up @@ -847,8 +847,8 @@ github.com/golang/mock v1.4.1/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt
github.com/golang/mock v1.4.3/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw=
github.com/golang/mock v1.4.4/go.mod h1:l3mdAwkq5BuhzHwde/uurv3sEJeZMXNpwsxVWU71h+4=
github.com/golang/mock v1.5.0/go.mod h1:CWnOUgYIOo4TcNZ0wHX3YZCqsaM1I1Jvs6v3mP3KVu8=
github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=
github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs=
github.com/golang/mock v1.7.0-rc.1 h1:YojYx61/OLFsiv6Rw1Z96LpldJIy31o+UHmwAUMJ6/U=
github.com/golang/mock v1.7.0-rc.1/go.mod h1:s42URUywIqd+OcERslBJvOjepvNymP31m3q8d/GkuRs=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
Expand Down
34 changes: 33 additions & 1 deletion internal/appliance/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,40 @@ go_library(
],
)

filegroup(
name = "testdata",
srcs = glob(["testdata/**"]),
)

go_test(
name = "appliance_test",
srcs = ["blobstore_test.go"],
srcs = [
"blobstore_test.go",
"golden_test.go",
"helpers_test.go",
],
data = [
":testdata",
"//dev/tools:kubebuilder-assets",
],
embed = [":appliance"],
env = {
"KUBEBUILDER_ASSET_PATHS": "$(rlocationpaths //dev/tools:kubebuilder-assets)",
},
deps = [
"@com_github_go_logr_stdr//:stdr",
"@com_github_stretchr_testify//require",
"@com_github_stretchr_testify//suite",
"@io_bazel_rules_go//go/runfiles:go_default_library",
"@io_k8s_api//core/v1:core",
"@io_k8s_apimachinery//pkg/apis/meta/v1:meta",
"@io_k8s_apimachinery//pkg/runtime/schema",
"@io_k8s_client_go//kubernetes",
"@io_k8s_client_go//kubernetes/scheme",
"@io_k8s_sigs_controller_runtime//:controller-runtime",
"@io_k8s_sigs_controller_runtime//pkg/client",
"@io_k8s_sigs_controller_runtime//pkg/envtest",
"@io_k8s_sigs_controller_runtime//pkg/metrics/server",
"@io_k8s_sigs_yaml//:yaml",
],
)
16 changes: 9 additions & 7 deletions internal/appliance/blobstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@ package appliance
import (
"context"

appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/util/intstr"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/sourcegraph/sourcegraph/internal/k8s/resource/container"
"github.com/sourcegraph/sourcegraph/internal/k8s/resource/deployment"
"github.com/sourcegraph/sourcegraph/internal/k8s/resource/pod"
"github.com/sourcegraph/sourcegraph/internal/k8s/resource/pvc"
"github.com/sourcegraph/sourcegraph/internal/k8s/resource/service"
"github.com/sourcegraph/sourcegraph/lib/errors"
"github.com/sourcegraph/sourcegraph/lib/pointers"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/util/intstr"
"sigs.k8s.io/controller-runtime/pkg/client"
)

func (r *Reconciler) reconcileBlobstore(ctx context.Context, sg *Sourcegraph, owner client.Object) error {
Expand Down Expand Up @@ -108,7 +109,8 @@ func (r *Reconciler) reconcileBlobstoreServices(ctx context.Context, sg *Sourceg
func buildBlobstoreDeployment(sg *Sourcegraph) (appsv1.Deployment, error) {
name := "blobstore"

containerImage := ""
// TODO: https://github.com/sourcegraph/sourcegraph/issues/62076
containerImage := "index.docker.io/sourcegraph/blobstore:5.3.2@sha256:d625be1eefe61cc42f94498e3c588bf212c4159c8b20c519db84eae4ff715efa"

containerPorts := corev1.ContainerPort{
Name: name,
Expand Down Expand Up @@ -186,7 +188,7 @@ func buildBlobstoreDeployment(sg *Sourcegraph) (appsv1.Deployment, error) {
},
}

podTemplate, err := pod.NewPodTemplate(name, sg.Namespace,
podTemplate, err := pod.NewPodTemplate(name,
pod.WithContainers(defaultContainer),
pod.WithVolumes(podVolumes),
)
Expand Down
45 changes: 45 additions & 0 deletions internal/appliance/blobstore_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,46 @@
package appliance

import (
"time"
)

// Simple test cases in which we want to assert that a given configmap causes a
// certain set of resources to be deployed can go here. sg and golden fixtures
// are in testdata/ and named after the test case name.
func (suite *ApplianceTestSuite) TestDeployBlobstore() {
for _, tc := range []struct {
name string
}{
{
name: "blobstore-default",
},
} {
suite.Run(tc.name, func() {
namespace := suite.createConfigMap(tc.name)

// Wait for reconciliation to be finished.
suite.Require().Eventually(func() bool {
return suite.getConfigMapReconcileEventCount(namespace) > 0
}, time.Second*10, time.Millisecond*200)

suite.makeGoldenAssertions(namespace, tc.name)
})
}
}
Comment on lines +7 to +29
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Example of what I am guessing 80% of appliance tests will be: deploy some config, assert on the resultant resource set.

Adding new cases is as simple as:

  1. Craft a config file in testdata/sg/
  2. Reference it in a new test case here (1 line of go!)
  3. Regenerate golden files: go test ./internal/appliance -args appliance-update-golden-files
  4. Eyeball the golden file, decide whether you're happy with it

Failing tests print golden file diffs, and reconciler logs.


// More complex test cases involving updates to the configmap can have their own
// test blocks
func (suite *ApplianceTestSuite) TestBlobstoreResourcesDeletedWhenDisabled() {
namespace := suite.createConfigMap("blobstore-default")
suite.Require().Eventually(func() bool {
return suite.getConfigMapReconcileEventCount(namespace) > 0
}, time.Second*10, time.Millisecond*200)

eventsSeenSoFar := suite.getConfigMapReconcileEventCount(namespace)
suite.updateConfigMap(namespace, "everything-disabled")
suite.Require().Eventually(func() bool {
return suite.getConfigMapReconcileEventCount(namespace) > eventsSeenSoFar
}, time.Second*10, time.Millisecond*200)

suite.makeGoldenAssertions(namespace, "blobstore-subsequent-disable")
}
Comment on lines +31 to +46
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Example of a more bespoke test that updates the configmap after creating it. If we ever find we need to do complex k8s resource upgrades (not just bumping versions), we can hopefully test that in this way.

19 changes: 19 additions & 0 deletions internal/appliance/development.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Appliance development

## Running tests

To (re)generate golden fixtures, pass the following argument to `go test`:

```
go test -args appliance-update-golden-files
```

In order to run `go test` (with or without arguments), you must have
`setup-envtest` available:

```
go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest
```

This is not a requirement for the Bazel environment (`bazel test
:appliance_test` in this module).
Loading