Skip to content

Commit 3f7ec8e

Browse files
pedjakclaude
andcommitted
refactor: replace hand-written test mocks with gomock-generated mocks
Introduce go.uber.org/mock (gomock) with mockgen code generation to replace all hand-written mock structs across the test suite. This eliminates duplicated mocks, establishes a single consistent mocking pattern, and ensures mocks stay in sync with interface changes automatically via make generate-mocks. Infrastructure: - Add mockgen via bingo with unversioned symlink for go generate - Add generate-mocks Makefile target (go generate ./...) - Wire into existing generate/verify targets for CI enforcement - Centralized //go:generate directives in internal/testutil/mock/ for exported interfaces (external and internal) - Source-mode directives at interface definitions for unexported interfaces (trackingCache, sourcerer), output to _test.go files to keep gomock out of the production binary - Mock helpers accept shared *gomock.Controller to avoid controller proliferation in nested mock scenarios Rename MockPuller/MockCache to FakePuller/FakeCache in image/fakes.go to clarify they are test fakes (preconfigured return values), not mocks (interaction verification). Remove duplicated FakeCertProvider from render/fake.go and rukpak/util/testing.go, replaced by generated MockCertificateProvider. FakeBundleSource (a function type, not an interface) is retained as-is. Co-Authored-By: Claude <noreply@anthropic.com>
1 parent a12398a commit 3f7ec8e

60 files changed

Lines changed: 5534 additions & 2278 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.

.bingo/Variables.mk

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ $(KUBE_SCORE): $(BINGO_DIR)/kube-score.mod
7777
@echo "(re)installing $(GOBIN)/kube-score-v1.20.0"
7878
@cd $(BINGO_DIR) && GOWORK=off $(GO) build -mod=mod -modfile=kube-score.mod -o=$(GOBIN)/kube-score-v1.20.0 "github.com/zegl/kube-score/cmd/kube-score"
7979

80+
MOCKGEN := $(GOBIN)/mockgen-v0.6.0
81+
$(MOCKGEN): $(BINGO_DIR)/mockgen.mod
82+
@# Install binary/ries using Go 1.14+ build command. This is using bwplotka/bingo-controlled, separate go module with pinned dependencies.
83+
@echo "(re)installing $(GOBIN)/mockgen-v0.6.0"
84+
@cd $(BINGO_DIR) && GOWORK=off $(GO) build -mod=mod -modfile=mockgen.mod -o=$(GOBIN)/mockgen-v0.6.0 "go.uber.org/mock/mockgen"
85+
8086
OPERATOR_SDK := $(GOBIN)/operator-sdk-v1.41.1
8187
$(OPERATOR_SDK): $(BINGO_DIR)/operator-sdk.mod
8288
@# Install binary/ries using Go 1.14+ build command. This is using bwplotka/bingo-controlled, separate go module with pinned dependencies.

.bingo/mockgen.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module _ // Auto generated by https://github.com/bwplotka/bingo. DO NOT EDIT
2+
3+
go 1.26.3
4+
5+
require go.uber.org/mock v0.6.0 // mockgen

.bingo/mockgen.sum

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
go.uber.org/mock v0.6.0 h1:hyF9dfmbgIX5EfOdasqLsWD6xqpNZlXblLB/Dbnwv3Y=
2+
go.uber.org/mock v0.6.0/go.mod h1:KiVJ4BqZJaMj4svdfmHM0AUx4NJYO8ZNpPnZn1Z+BBU=
3+
golang.org/x/mod v0.27.0 h1:kb+q2PyFnEADO2IEF935ehFUXlWiNjJWtRNgBLSfbxQ=
4+
golang.org/x/mod v0.27.0/go.mod h1:rWI627Fq0DEoudcK+MBkNkCe0EetEaDSwJJkCcjpazc=
5+
golang.org/x/sync v0.16.0 h1:ycBJEhp9p4vXvUZNszeOq0kGTPghopOL8q0fq3vstxw=
6+
golang.org/x/sync v0.16.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
7+
golang.org/x/tools v0.36.0 h1:kWS0uv/zsvHEle1LbV5LE8QujrxB3wfQyxHfhOk0Qkg=
8+
golang.org/x/tools v0.36.0/go.mod h1:WBDiHKJK8YgLHlcQPYQzNCkUxUypCaa5ZegCVutKm+s=

.bingo/variables.env

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ KIND="${GOBIN}/kind-v0.32.0"
2828

2929
KUBE_SCORE="${GOBIN}/kube-score-v1.20.0"
3030

31+
MOCKGEN="${GOBIN}/mockgen-v0.6.0"
32+
3133
OPERATOR_SDK="${GOBIN}/operator-sdk-v1.41.1"
3234

3335
OPM="${GOBIN}/opm-v1.60.0"

AGENTS.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,15 @@ install and manage cluster extensions. The project follows a microservices archi
5454
- `containers_image_openpgp` - required for image handling
5555

5656
**Tools (managed via .bingo/):**
57-
- controller-gen, golangci-lint, goreleaser, helm, kind, kustomize, setup-envtest, operator-sdk
57+
- controller-gen, golangci-lint, goreleaser, helm, kind, kustomize, mockgen, setup-envtest, operator-sdk
58+
59+
**Test Mocking:**
60+
- **gomock** (`go.uber.org/mock`): Used for generated mock implementations. `//go:generate mockgen` directives
61+
live in `internal/testutil/mock/generate.go` (external and internal exported interfaces) and at interface
62+
definitions for internal unexported interfaces (source mode). Run `make generate-mocks` to regenerate.
63+
All interface mocks should be gomock-generated. Test fakes that return preconfigured values without
64+
interaction verification (e.g., `FakePuller`, `FakeCache` in `image/fakes.go`) are not mocks and stay
65+
hand-written.
5866

5967
---
6068

@@ -140,8 +148,11 @@ make manifests
140148
# Update CRDs and reference docs (when Go-based API definitions change)
141149
make update-crds crd-ref-docs
142150

143-
# Generate code (DeepCopy methods)
151+
# Generate code (DeepCopy methods and mock implementations)
144152
make generate
153+
154+
# Regenerate mock implementations only
155+
make generate-mocks
145156
```
146157

147158
---

Makefile

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ endif
5858
# bingo manages consistent tooling versions for things like kind, etc.
5959
include .bingo/Variables.mk
6060

61+
# mockgen is installed by bingo with a versioned name (mockgen-v0.6.0).
62+
# Redefine MOCKGEN to an unversioned symlink so that go generate can find it on PATH.
63+
_MOCKGEN_BINGO := $(MOCKGEN)
64+
MOCKGEN := $(ROOT_DIR)/bin/mockgen
65+
$(MOCKGEN): $(_MOCKGEN_BINGO)
66+
@mkdir -p $(dir $@)
67+
@ln -sf $< $@
68+
6169
ifeq ($(origin KIND_CLUSTER_NAME), undefined)
6270
KIND_CLUSTER_NAME := operator-controller
6371
endif
@@ -201,8 +209,12 @@ manifests: update-crds $(MANIFESTS) $(HELM) #EXHELP Generate OLMv1 manifests
201209
$(HELM) template olmv1 helm/olmv1 --values helm/tilt.yaml $(addprefix --set ,$(HELM_SETTINGS)) > /dev/null
202210
$(HELM) template olmv1 helm/olmv1 --set "options.openshift.enabled=true" > /dev/null
203211

212+
.PHONY: generate-mocks
213+
generate-mocks: $(MOCKGEN) #EXHELP Generate mock implementations for testing.
214+
PATH="$(ROOT_DIR)/bin:$$PATH" go generate ./...
215+
204216
.PHONY: generate
205-
generate: $(CONTROLLER_GEN) #EXHELP Generate code containing DeepCopy, DeepCopyInto, DeepCopyObject, and ApplyConfiguration type implementations.
217+
generate: $(CONTROLLER_GEN) generate-mocks #EXHELP Generate code containing DeepCopy, DeepCopyInto, DeepCopyObject, and ApplyConfiguration type implementations.
206218
# Need to delete the files for them to be generated properly
207219
@find api cmd hack internal -name "zz_generated.deepcopy.go" -not -path "*/vendor/*" -delete && rm -rf applyconfigurations
208220
$(CONTROLLER_GEN) --load-build-tags=$(GO_BUILD_TAGS) applyconfiguration:headerFile="hack/boilerplate.go.txt" paths="./api/..."

codecov.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ codecov:
77
# Configure the paths to include in coverage reports.
88
# Exclude documentation, YAML configurations, and test files.
99
coverage:
10+
ignore:
11+
- "internal/testutil/mock/**"
12+
- "**/mock_*_gen*.go"
1013
status:
1114
project:
1215
default:

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ require (
3131
github.com/spf13/pflag v1.0.10
3232
github.com/stretchr/testify v1.11.1
3333
go.podman.io/image/v5 v5.40.0
34+
go.uber.org/mock v0.6.0
3435
golang.org/x/exp v0.0.0-20260410095643-746e56fc9e2f
3536
golang.org/x/mod v0.37.0
3637
golang.org/x/sync v0.21.0
@@ -205,7 +206,6 @@ require (
205206
github.com/smallstep/pkcs7 v0.2.1 // indirect
206207
github.com/spf13/cast v1.10.0 // indirect
207208
github.com/stefanberger/go-pkcs11uri v0.0.0-20230803200340-78284954bff6 // indirect
208-
github.com/stretchr/objx v0.5.3 // indirect
209209
github.com/ulikunitz/xz v0.5.15 // indirect
210210
github.com/vbatts/tar-split v0.12.3 // indirect
211211
github.com/vbauerster/mpb/v8 v8.12.0 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,8 @@ go.podman.io/storage v1.63.0 h1:bj/pAWFhChbuBmejzno0iQLhU7FevGVXepRXm5pFGeA=
584584
go.podman.io/storage v1.63.0/go.mod h1:z4Z9K+7GhKjWL/Y1O17+4f8a1KGijVeC9hr3tymhSOs=
585585
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
586586
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
587+
go.uber.org/mock v0.6.0 h1:hyF9dfmbgIX5EfOdasqLsWD6xqpNZlXblLB/Dbnwv3Y=
588+
go.uber.org/mock v0.6.0/go.mod h1:KiVJ4BqZJaMj4svdfmHM0AUx4NJYO8ZNpPnZn1Z+BBU=
587589
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
588590
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
589591
go.uber.org/zap v1.27.1 h1:08RqriUEv8+ArZRYSTXy1LeBScaMpVSTBhCeaZYfMYc=

0 commit comments

Comments
 (0)