Skip to content

Commit c1f1e62

Browse files
committed
Update golden test framework
* Move to envtest by default (instead of mock-kubeapiserver) * Create shared code for capturing requests / normalization. * Use a more conventional env var WRITE_GOLDEN_OUTPUT * Support object rewriting
1 parent b3bb66f commit c1f1e62

File tree

16 files changed

+635
-129
lines changed

16 files changed

+635
-129
lines changed

dev/test

+3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ cd "${REPO_ROOT}"
2727

2828
set -x
2929

30+
# Download the kubebuilder assets for envtest
31+
export KUBEBUILDER_ASSETS=$(go run sigs.k8s.io/controller-runtime/tools/setup-envtest@latest use -p path)
32+
3033
pushd mockkubeapiserver
3134
CGO_ENABLED=0 go test -count=1 -v ./...
3235
popd

docs/addon/walkthrough/tests.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ This means that the output is materialized and checked in to the repo; this
3333
proves to be very handy for understanding the impact of a change.
3434

3535
There's also a helpful "cheat" function, which rewrite the output when you run
36-
the tests locally - set the HACK_AUTOFIX_EXPECTED_OUTPUT env var to a non-empty
36+
the tests locally - set the WRITE_GOLDEN_OUTPUT env var to a non-empty
3737
string. This is useful when you have a big set of changes; it's just as easy to
3838
review the changes yourself in the diff and there's not a ton of value in typing
3939
them out.
@@ -87,7 +87,7 @@ the env-var cheat code.
8787
```bash
8888
cd pkg/controller/{{operator}}/tests
8989
touch tests/simple.out.yaml
90-
HACK_AUTOFIX_EXPECTED_OUTPUT=1 go test ./...
90+
WRITE_GOLDEN_OUTPUT=1 go test ./...
9191
```
9292

9393
1. Verify the output is reproducible

examples/guestbook-operator/controllers/guestbook_controller_test.go

+16-2
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,28 @@ limitations under the License.
1717
package controllers
1818

1919
import (
20+
"path/filepath"
2021
"testing"
2122

23+
"k8s.io/klog/v2/klogr"
24+
"sigs.k8s.io/controller-runtime/pkg/envtest"
25+
"sigs.k8s.io/controller-runtime/pkg/log"
2226
api "sigs.k8s.io/kubebuilder-declarative-pattern/examples/guestbook-operator/api/v1alpha1"
2327
"sigs.k8s.io/kubebuilder-declarative-pattern/pkg/test/golden"
2428
)
2529

2630
func TestGuestbook(t *testing.T) {
27-
v := golden.NewValidator(t, api.SchemeBuilder)
31+
log.SetLogger(klogr.New())
32+
33+
env := &envtest.Environment{
34+
CRDInstallOptions: envtest.CRDInstallOptions{
35+
ErrorIfPathMissing: true,
36+
Paths: []string{
37+
filepath.Join("..", "config", "crd", "bases"),
38+
},
39+
},
40+
}
41+
v := golden.NewValidator(t, env, api.AddToScheme)
2842
dr := &GuestbookReconciler{
2943
Client: v.Client(),
3044
}
@@ -33,5 +47,5 @@ func TestGuestbook(t *testing.T) {
3347
t.Fatalf("creating reconciler: %v", err)
3448
}
3549

36-
v.Validate(dr.Reconciler)
50+
v.Validate(&dr.Reconciler, golden.ValidateOptions{})
3751
}

examples/guestbook-operator/controllers/tests/patches-stable.in.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ apiVersion: addons.example.org/v1alpha1
22
kind: Guestbook
33
metadata:
44
name: guestbook-sample
5+
namespace: default
56
spec:
67
channel: stable
78
patches:

examples/guestbook-operator/controllers/tests/simple-stable.in.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ apiVersion: addons.example.org/v1alpha1
22
kind: Guestbook
33
metadata:
44
name: guestbook-sample
5+
namespace: default
56
spec:
67
channel: stable

examples/guestbook-operator/go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require (
88
github.com/go-logr/logr v1.4.2
99
k8s.io/apimachinery v0.31.1
1010
k8s.io/client-go v0.31.1
11+
k8s.io/klog/v2 v2.130.1
1112
sigs.k8s.io/controller-runtime v0.19.0
1213
sigs.k8s.io/kubebuilder-declarative-pattern v0.0.0-20210922163802-cac4a6cf1977
1314
)
@@ -104,14 +105,13 @@ require (
104105
k8s.io/apiextensions-apiserver v0.31.1 // indirect
105106
k8s.io/cli-runtime v0.29.1 // indirect
106107
k8s.io/component-base v0.31.1 // indirect
107-
k8s.io/klog/v2 v2.130.1 // indirect
108108
k8s.io/kube-openapi v0.0.0-20240903163716-9e1beecbcb38 // indirect
109109
k8s.io/kubectl v0.29.1 // indirect
110110
k8s.io/utils v0.0.0-20240902221715-702e33fdd3c3 // indirect
111111
sigs.k8s.io/cli-utils v0.33.0 // indirect
112112
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
113113
sigs.k8s.io/kubebuilder-declarative-pattern/applylib v0.0.0-20230420203711-4abaa68e1923 // indirect
114-
sigs.k8s.io/kubebuilder-declarative-pattern/mockkubeapiserver v0.0.0-20230303024857-d1f76c15e05b // indirect
114+
sigs.k8s.io/kubebuilder-declarative-pattern/ktest v0.0.0-20240909164454-57a043fb3ad5 // indirect
115115
sigs.k8s.io/kustomize/api v0.14.0 // indirect
116116
sigs.k8s.io/kustomize/kstatus v0.0.2-0.20200509233124-065f70705d4d // indirect
117117
sigs.k8s.io/kustomize/kyaml v0.14.3 // indirect

go.work.sum

+1
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,7 @@ golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxb
430430
golang.org/x/tools v0.24.0/go.mod h1:YhNqVBIfWHdzvTLs0d8LCuMhkKUgSUKldakyV7W/WDQ=
431431
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2/go.mod h1:K8+ghG5WaK9qNqU5K3HdILfMLy1f3aNYFI/wnl100a8=
432432
google.golang.org/api v0.126.0/go.mod h1:mBwVAtz+87bEN6CbA1GtZPDOqY2R5ONPqJeIlvyo4Aw=
433+
google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6c=
433434
google.golang.org/appengine v1.6.7/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=
434435
google.golang.org/genproto v0.0.0-20200423170343-7949de9c1215/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c=
435436
google.golang.org/genproto v0.0.0-20200513103714-09dca8ec2884/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c=

ktest/go.mod

+30-2
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,53 @@ toolchain go1.23.5
77
require (
88
github.com/google/go-cmp v0.6.0
99
k8s.io/apimachinery v0.31.1
10+
k8s.io/client-go v0.31.1
1011
k8s.io/klog/v2 v2.130.1
12+
sigs.k8s.io/controller-runtime v0.19.0
13+
sigs.k8s.io/kubebuilder-declarative-pattern/applylib v0.0.0-20250315190147-cfd0b42f94ef
1114
sigs.k8s.io/yaml v1.4.0
1215
)
1316

1417
require (
18+
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
19+
github.com/emicklei/go-restful/v3 v3.12.1 // indirect
20+
github.com/evanphx/json-patch/v5 v5.9.0 // indirect
1521
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
1622
github.com/go-logr/logr v1.4.2 // indirect
23+
github.com/go-openapi/jsonpointer v0.21.0 // indirect
24+
github.com/go-openapi/jsonreference v0.21.0 // indirect
25+
github.com/go-openapi/swag v0.23.0 // indirect
1726
github.com/gogo/protobuf v1.3.2 // indirect
27+
github.com/golang/protobuf v1.5.4 // indirect
28+
github.com/google/gnostic-models v0.6.8 // indirect
1829
github.com/google/gofuzz v1.2.0 // indirect
30+
github.com/google/uuid v1.6.0 // indirect
31+
github.com/imdario/mergo v0.3.16 // indirect
32+
github.com/josharian/intern v1.0.0 // indirect
1933
github.com/json-iterator/go v1.1.12 // indirect
20-
github.com/kr/text v0.2.0 // indirect
34+
github.com/mailru/easyjson v0.7.7 // indirect
2135
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
2236
github.com/modern-go/reflect2 v1.0.2 // indirect
37+
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
38+
github.com/pkg/errors v0.9.1 // indirect
39+
github.com/prometheus/client_golang v1.20.4 // indirect
40+
github.com/prometheus/common v0.59.1 // indirect
41+
github.com/spf13/pflag v1.0.5 // indirect
2342
github.com/x448/float16 v0.8.4 // indirect
2443
golang.org/x/net v0.29.0 // indirect
44+
golang.org/x/oauth2 v0.23.0 // indirect
45+
golang.org/x/sys v0.25.0 // indirect
46+
golang.org/x/term v0.24.0 // indirect
2547
golang.org/x/text v0.18.0 // indirect
26-
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
48+
golang.org/x/time v0.6.0 // indirect
49+
golang.org/x/tools v0.25.0 // indirect
50+
google.golang.org/protobuf v1.34.2 // indirect
2751
gopkg.in/inf.v0 v0.9.1 // indirect
2852
gopkg.in/yaml.v2 v2.4.0 // indirect
53+
gopkg.in/yaml.v3 v3.0.1 // indirect
54+
k8s.io/api v0.31.1 // indirect
55+
k8s.io/apiextensions-apiserver v0.31.1 // indirect
56+
k8s.io/kube-openapi v0.0.0-20240903163716-9e1beecbcb38 // indirect
2957
k8s.io/utils v0.0.0-20240902221715-702e33fdd3c3 // indirect
3058
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
3159
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect

0 commit comments

Comments
 (0)