Skip to content

Commit 091b854

Browse files
authored
build(images): run the tests of the packages the patches touch (#4326)
## What this PR does Twelve first-party images that are built from an upstream tree with patches on top now run `go test` on the packages those patches touch. The step sits in the builder stage, right after the patches are applied. Before this, ten of them ran `go build` only, and `go build` never compiles a `_test.go`, so a test carried inside a patch was proven once by hand and never again. The other two, opensearch-operator and kube-ovn, ran a `go test` line written by hand, which a patch reaching a new directory would slip past; the loop replaces both lines. The package list is read from the patches themselves with `git apply --numstat`, so a new patch is covered without an edit to the Dockerfile. Each package is tested from its own directory, which finds nested modules: in objectstorage, `internal/` is a module of its own and the root module cannot name `./internal/runtime`. An empty list runs nothing. The list is read into a variable before the loop, because a `for` loop drops the exit status of a command substitution, and a patch that git cannot read would otherwise give an empty list and a green step. A few images need more than the plain loop: - opensearch-operator skips `controllers/`. A patch touches it, but its suite starts an envtest control plane from etcd and kube-apiserver binaries the builder does not have. The loop runs with `-race` and replaces the hand-picked `go test -race` line, which also covered `pkg/reconcilers/util`, a package no patch touches. - kube-ovn drops its `go test -run '^TestVMIMigrationSchedulingRetry$'` line. The test it named is copied into `pkg/controller`, which a patch touches, so the loop runs it along with the rest of that package. - redis-operator also runs `go vet -tags integration` in each touched directory. Two of the files its patches edit are upstream integration tests behind that tag, and `go test` compiles none of them. - cluster-autoscaler and kubevirt-cloud-provider export `GOOS` and `GOARCH` for the target in the whole builder stage, so their tests unset both. A local `make image` builds amd64 and arm64 by default, and a test binary for the other architecture runs on the build host only under binfmt emulation. CI is not affected either way: the jobs that build package images each pin one `PLATFORM` that matches the runner's architecture. The first run of the step on cluster-autoscaler found the carried `fix-downscale.diff` out of step with the upstream tests it leaves in place. The patch removes the `maxSize` refusal from `SetSize`, and one case of `TestNodeGroupIncreaseSizeErrors` still expected that refusal. The patch now drops that case and carries `TestNodeGroupDeleteNodesAboveMaxSize` instead. That test covers the downscale the patch is named for: `DeleteNodes` lowers a group through `SetSize`, so with the refusal in place a group standing above its max size could not shed a node. Against the unpatched `SetSize` the new test fails. The image's behaviour does not change, and scale-up stays bounded by the core scale-up orchestrator, which skips a node group already at its max size. CI builds all twelve images, and the step reports `ok` or `no test files` for every package it tests. Locally, the redis-operator, opensearch-operator and kube-ovn builder stages pass. The cluster-autoscaler package was checked from the `cluster-autoscaler-1.32.0` tag tarball, because the Dockerfile's full clone of kubernetes/autoscaler does not survive my network. As negative controls, an undefined call added to a redis integration test fails `go vet -tags integration` while `go test` still passes, and the new autoscaler test fails without the patch. Left out on purpose, and the Dockerfile says so: `multus`. Its only touched package, `pkg/multus`, creates network namespaces in its tests, which a build container cannot do (`mount --make-rshared /var/run/netns: operation not permitted`). `cilium` (C, no compile step) and `piraeus-server` (Java) are out of scope. Closes #4318 ### Screenshots Not a UI change. ### Downstream repositories Walked the trigger map against the diff: Dockerfiles under `packages/*/images/` and one patch file. No values key, schema, make target, `hack/` file or documented workflow changes. - [x] No downstream repository is affected by this change - [ ] [cozystack/website](https://github.com/cozystack/website) - follow-up: - [ ] [cozystack/terraform-provider-cozystack](https://github.com/cozystack/terraform-provider-cozystack) - follow-up: - [ ] [cozystack/ansible-cozystack](https://github.com/cozystack/ansible-cozystack) - follow-up: - [ ] [cozystack/ccp](https://github.com/cozystack/ccp) - follow-up: - [ ] [cozystack/talm](https://github.com/cozystack/talm) - follow-up: - [ ] [cozystack/cozyhr](https://github.com/cozystack/cozyhr) - follow-up: - [ ] [cozystack/cozy-proxy](https://github.com/cozystack/cozy-proxy) - follow-up: - [ ] [cozystack/cozystack-telemetry-server](https://github.com/cozystack/cozystack-telemetry-server) - follow-up: - [ ] [cozystack/external-apps-example](https://github.com/cozystack/external-apps-example) - follow-up: - [ ] [cozystack/examples](https://github.com/cozystack/examples) - follow-up: - [ ] [cozystack/community](https://github.com/cozystack/community) - follow-up: ### Release note ```release-note NONE ```
2 parents 309875a + 89a1fe5 commit 091b854

14 files changed

Lines changed: 233 additions & 8 deletions

File tree

‎packages/apps/kubernetes/images/cluster-autoscaler/Dockerfile‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,22 @@ RUN git clone https://github.com/kubernetes/autoscaler /src/autoscaler \
1616
WORKDIR /src/autoscaler/cluster-autoscaler
1717
COPY fix-downscale.diff /fix-downscale.diff
1818
RUN git apply /fix-downscale.diff
19+
20+
# go build never compiles a _test.go, so nothing else runs the tests of the
21+
# packages the patch touches. Each package is tested from its own directory,
22+
# below the clone root because the patch names paths from there: that finds
23+
# the module it belongs to, and an empty list runs nothing.
24+
# The list is read into a variable first: a for loop drops the exit status
25+
# of a command substitution, so a patch git cannot read would leave the list
26+
# empty and the step green.
27+
# The stage sets GOOS and GOARCH to the target for the whole stage, so the
28+
# tests drop them: a test binary built for another architecture cannot run
29+
# on the build host without emulation.
30+
RUN cd /src/autoscaler && files=$(git apply --numstat /fix-downscale.diff) || exit 1; \
31+
for dir in $(printf '%s\n' "$files" | cut -f3 | grep '\.go$' | xargs -r -n1 dirname | sort -u); do \
32+
(cd "$dir" && env -u GOOS -u GOARCH go test .) || exit 1; \
33+
done
34+
1935
RUN make build
2036

2137
FROM $BASEIMAGE

‎packages/apps/kubernetes/images/cluster-autoscaler/fix-downscale.diff‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,78 @@ index 4eec0e4bf..f28fd9241 100644
1111
case nreplicas < r.minSize:
1212
return fmt.Errorf("size decrease too large - desired:%d min:%d", nreplicas, r.minSize)
1313
}
14+
diff --git a/cluster-autoscaler/cloudprovider/clusterapi/clusterapi_nodegroup_test.go b/cluster-autoscaler/cloudprovider/clusterapi/clusterapi_nodegroup_test.go
15+
--- a/cluster-autoscaler/cloudprovider/clusterapi/clusterapi_nodegroup_test.go
16+
+++ b/cluster-autoscaler/cloudprovider/clusterapi/clusterapi_nodegroup_test.go
17+
@@ -241,11 +241,6 @@
18+
delta: -1,
19+
initial: 3,
20+
errorMsg: "size increase must be positive",
21+
- }, {
22+
- description: "errors because initial+delta > maxSize",
23+
- delta: 8,
24+
- initial: 3,
25+
- errorMsg: "size increase too large - desired:11 max:10",
26+
}}
27+
28+
test := func(t *testing.T, tc *testCase, testConfig *testConfig) {
29+
@@ -767,7 +762,59 @@
30+
)
31+
})
32+
}
33+
+
34+
+// A group can stand above its max size, for one when the annotation is
35+
+// lowered under it, and deleting a node from it must not be refused as an
36+
+// increase.
37+
+func TestNodeGroupDeleteNodesAboveMaxSize(t *testing.T) {
38+
+ test := func(t *testing.T, testConfig *testConfig) {
39+
+ controller, stop := mustCreateTestController(t, testConfig)
40+
+ defer stop()
41+
+
42+
+ nodegroups, err := controller.nodeGroups()
43+
+ if err != nil {
44+
+ t.Fatalf("unexpected error: %v", err)
45+
+ }
46+
47+
+ if l := len(nodegroups); l != 1 {
48+
+ t.Fatalf("expected 1 nodegroup, got %d", l)
49+
+ }
50+
+
51+
+ ng := nodegroups[0].(*nodegroup)
52+
+ if err := ng.DeleteNodes(testConfig.nodes[:1]); err != nil {
53+
+ t.Fatalf("unexpected error: %v", err)
54+
+ }
55+
+
56+
+ gvr, err := ng.scalableResource.GroupVersionResource()
57+
+ if err != nil {
58+
+ t.Fatalf("unexpected error: %v", err)
59+
+ }
60+
+
61+
+ scalableResource, err := ng.machineController.managementScaleClient.Scales(testConfig.spec.namespace).
62+
+ Get(context.TODO(), gvr.GroupResource(), ng.scalableResource.Name(), metav1.GetOptions{})
63+
+ if err != nil {
64+
+ t.Fatalf("unexpected error: %v", err)
65+
+ }
66+
+
67+
+ if scalableResource.Spec.Replicas != 3 {
68+
+ t.Errorf("expected 3, got %v", scalableResource.Spec.Replicas)
69+
+ }
70+
+ }
71+
+
72+
+ annotations := map[string]string{
73+
+ nodeGroupMinSizeAnnotationKey: "1",
74+
+ nodeGroupMaxSizeAnnotationKey: "2",
75+
+ }
76+
+
77+
+ t.Run("MachineSet", func(t *testing.T) {
78+
+ test(t, createMachineSetTestConfig(RandomString(6), RandomString(6), RandomString(6), 4, annotations, nil))
79+
+ })
80+
+
81+
+ t.Run("MachineDeployment", func(t *testing.T) {
82+
+ test(t, createMachineDeploymentTestConfig(RandomString(6), RandomString(6), RandomString(6), 4, annotations, nil))
83+
+ })
84+
+}
85+
+
86+
func TestNodeGroupMachineSetDeleteNodesWithMismatchedNodes(t *testing.T) {
87+
test := func(t *testing.T, expected int, testConfigs []*testConfig) {
88+
testConfig0, testConfig1 := testConfigs[0], testConfigs[1]

‎packages/apps/kubernetes/images/kubevirt-cloud-provider/Dockerfile‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,20 @@ RUN go get 'k8s.io/endpointslice/[email protected]' 'k8s.io/[email protected]'
1919
RUN go mod tidy
2020
RUN go mod vendor
2121

22+
# go build never compiles a _test.go, so nothing else runs the tests of the
23+
# packages the patches touch. Each package is tested from its own directory:
24+
# that finds the module it belongs to, and an empty list runs nothing.
25+
# The list is read into a variable first: a for loop drops the exit status
26+
# of a command substitution, so a patch git cannot read would leave the list
27+
# empty and the step green.
28+
# The stage sets GOOS and GOARCH to the target for the whole stage, so the
29+
# tests drop them: a test binary built for another architecture cannot run
30+
# on the build host without emulation.
31+
RUN files=$(git apply --numstat /patches/*.diff) || exit 1; \
32+
for dir in $(printf '%s\n' "$files" | cut -f3 | grep '\.go$' | xargs -r -n1 dirname | sort -u); do \
33+
(cd "$dir" && env -u GOOS -u GOARCH go test -mod=vendor .) || exit 1; \
34+
done
35+
2236
RUN CGO_ENABLED=0 go build -mod=vendor -ldflags="-s -w" -o bin/kubevirt-cloud-controller-manager ./cmd/kubevirt-cloud-controller-manager
2337

2438
FROM scratch

‎packages/system/bucket/images/s3manager/Dockerfile‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,18 @@ RUN wget -O- https://github.com/cloudlena/s3manager/archive/9a7c8e446b422f8973b8
1010
ADD cozystack.patch /
1111
RUN git apply /cozystack.patch
1212
RUN go mod tidy
13+
14+
# go build never compiles a _test.go, so nothing else runs the tests of the
15+
# packages the patch touches. Each package is tested from its own directory:
16+
# that finds the module it belongs to, and an empty list runs nothing.
17+
# The list is read into a variable first: a for loop drops the exit status
18+
# of a command substitution, so a patch git cannot read would leave the list
19+
# empty and the step green.
20+
RUN files=$(git apply --numstat /cozystack.patch) || exit 1; \
21+
for dir in $(printf '%s\n' "$files" | cut -f3 | grep '\.go$' | xargs -r -n1 dirname | sort -u); do \
22+
(cd "$dir" && go test .) || exit 1; \
23+
done
24+
1325
RUN GOOS=$TARGETOS GOARCH=$TARGETARCH CGO_ENABLED=0 go build -ldflags="-s -w" -a -installsuffix cgo -o bin/s3manager
1426

1527
FROM docker.io/library/alpine:3.24@sha256:294b683cb724975bec92580e1e685676bd4b50bda910ddb8c51d4cabeaec77e6

‎packages/system/capi-providers-cpprovider/images/cluster-api-control-plane-provider-kamaji/Dockerfile‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,17 @@ RUN curl -sSL "https://github.com/clastix/cluster-api-control-plane-provider-kam
2525
COPY patches /patches
2626
RUN git apply --recount /patches/*.diff
2727

28+
# go build never compiles a _test.go, so nothing else runs the tests of the
29+
# packages the patches touch. Each package is tested from its own directory:
30+
# that finds the module it belongs to, and an empty list runs nothing.
31+
# The list is read into a variable first: a for loop drops the exit status
32+
# of a command substitution, so a patch git cannot read would leave the list
33+
# empty and the step green.
34+
RUN files=$(git apply --numstat --recount /patches/*.diff) || exit 1; \
35+
for dir in $(printf '%s\n' "$files" | cut -f3 | grep '\.go$' | xargs -r -n1 dirname | sort -u); do \
36+
(cd "$dir" && go test .) || exit 1; \
37+
done
38+
2839
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} \
2940
go build -a -o manager main.go
3041

‎packages/system/kamaji/images/kamaji/Dockerfile‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,17 @@ RUN curl -sSL https://github.com/clastix/kamaji/archive/refs/tags/${VERSION}.tar
1212
COPY patches /patches
1313
RUN git apply /patches/*.diff
1414

15+
# go build never compiles a _test.go, so nothing else runs the tests of the
16+
# packages the patches touch. Each package is tested from its own directory:
17+
# that finds the module it belongs to, and an empty list runs nothing.
18+
# The list is read into a variable first: a for loop drops the exit status
19+
# of a command substitution, so a patch git cannot read would leave the list
20+
# empty and the step green.
21+
RUN files=$(git apply --numstat /patches/*.diff) || exit 1; \
22+
for dir in $(printf '%s\n' "$files" | cut -f3 | grep '\.go$' | xargs -r -n1 dirname | sort -u); do \
23+
(cd "$dir" && go test .) || exit 1; \
24+
done
25+
1526
RUN GOOS=$TARGETOS GOARCH=$TARGETARCH CGO_ENABLED=0 go build \
1627
-ldflags "-X github.com/clastix/kamaji/internal.GitRepo=$GIT_REPO -X github.com/clastix/kamaji/internal.GitTag=$GIT_LAST_TAG -X github.com/clastix/kamaji/internal.GitCommit=$GIT_HEAD_COMMIT -X github.com/clastix/kamaji/internal.GitDirty=$GIT_MODIFIED -X github.com/clastix/kamaji/internal.BuildTime=$BUILD_DATE" \
1728
-a -o kamaji main.go

‎packages/system/keycloak-operator/images/keycloak-operator/Dockerfile‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,20 @@ COPY --from=source /src/go.mod /src/go.sum ./
2121
RUN go mod download
2222

2323
COPY --from=source /src .
24+
25+
COPY --from=source /patches /patches
26+
27+
# go build never compiles a _test.go, so nothing else runs the tests of the
28+
# packages the patches touch. Each package is tested from its own directory:
29+
# that finds the module it belongs to, and an empty list runs nothing.
30+
# The list is read into a variable first: a for loop drops the exit status
31+
# of a command substitution, so a patch git cannot read would leave the list
32+
# empty and the step green.
33+
RUN files=$(git apply --numstat /patches/*.diff) || exit 1; \
34+
for dir in $(printf '%s\n' "$files" | cut -f3 | grep '\.go$' | xargs -r -n1 dirname | sort -u); do \
35+
(cd "$dir" && go test .) || exit 1; \
36+
done
37+
2438
RUN CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH go build -o /build/manager ./cmd
2539

2640
FROM gcr.io/distroless/static:nonroot@sha256:e2e927ec666bae08560abb3c55d0659eceabb657f56b6782ab500a9fc7f555e3

‎packages/system/kilo/images/kilo/Dockerfile‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,17 @@ RUN curl -sSL https://github.com/squat/kilo/archive/${VERSION}.tar.gz | tar -xzv
1212
COPY patches /patches
1313
RUN git apply /patches/*.diff
1414

15+
# go build never compiles a _test.go, so nothing else runs the tests of the
16+
# packages the patches touch. Each package is tested from its own directory:
17+
# that finds the module it belongs to, and an empty list runs nothing.
18+
# The list is read into a variable first: a for loop drops the exit status
19+
# of a command substitution, so a patch git cannot read would leave the list
20+
# empty and the step green.
21+
RUN files=$(git apply --numstat /patches/*.diff) || exit 1; \
22+
for dir in $(printf '%s\n' "$files" | cut -f3 | grep '\.go$' | xargs -r -n1 dirname | sort -u); do \
23+
(cd "$dir" && go test .) || exit 1; \
24+
done
25+
1526
RUN GOOS=$TARGETOS GOARCH=$TARGETARCH CGO_ENABLED=0 go build \
1627
-trimpath \
1728
-ldflags "-X github.com/squat/kilo/pkg/version.Version=${VERSION}" \

‎packages/system/kubeovn/images/kubeovn/Dockerfile‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,20 @@ WORKDIR /source
1212

1313
COPY patches /patches
1414
RUN git apply /patches/*.diff
15+
# The loop below runs this test: a patch touches pkg/controller.
1516
COPY tests/kubevirt_migration_test.go.txt /source/pkg/controller/kubevirt_migration_test.go
16-
RUN go test ./pkg/controller -run '^TestVMIMigrationSchedulingRetry$' -count=1
17+
18+
# go build never compiles a _test.go, so nothing else runs the tests of the
19+
# packages the patches touch. Each package is tested from its own directory:
20+
# that finds the module it belongs to, and an empty list runs nothing.
21+
# The list is read into a variable first: a for loop drops the exit status
22+
# of a command substitution, so a patch git cannot read would leave the list
23+
# empty and the step green.
24+
RUN files=$(git apply --numstat /patches/*.diff) || exit 1; \
25+
for dir in $(printf '%s\n' "$files" | cut -f3 | grep '\.go$' | xargs -r -n1 dirname | sort -u); do \
26+
(cd "$dir" && go test .) || exit 1; \
27+
done
28+
1729
# Upstream hardcodes GOARCH in each target: build-go is amd64 only, and
1830
# build-go-arm is its arm64 twin minus the `go mod tidy` and the test-server
1931
# binary, which this image never copies.

‎packages/system/linstor/images/linstor-csi/Dockerfile‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@ RUN go mod download
2121
RUN go get google.golang.org/[email protected] && \
2222
go mod tidy
2323

24+
# go build never compiles a _test.go, so nothing else runs the tests of the
25+
# packages the patches touch. Each package is tested from its own directory:
26+
# that finds the module it belongs to, and an empty list runs nothing.
27+
# The list is read into a variable first: a for loop drops the exit status
28+
# of a command substitution, so a patch git cannot read would leave the list
29+
# empty and the step green.
30+
RUN files=$(git apply --numstat /patches/*.diff) || exit 1; \
31+
for dir in $(printf '%s\n' "$files" | cut -f3 | grep '\.go$' | xargs -r -n1 dirname | sort -u); do \
32+
(cd "$dir" && go test .) || exit 1; \
33+
done
34+
2435
RUN GOOS=$TARGETOS GOARCH=$TARGETARCH CGO_ENABLED=0 \
2536
go build \
2637
-a \

0 commit comments

Comments
 (0)