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

Commit 1c6a306

Browse files
committed
test: move region and httpproxy patch generator unit test invocation
1 parent f3883c3 commit 1c6a306

File tree

8 files changed

+169
-200
lines changed

8 files changed

+169
-200
lines changed

common/pkg/testutils/capitest/patches.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,52 @@ func ValidateGeneratePatches[T mutation.GeneratePatches](
9292
DescribeTable("Patches", testFunc, testArgs)
9393
}
9494

95+
// TODO(shalinpatel): AssertGeneratePatches is a replacement of ValidateGeneratePatches function.
96+
// remove ValidateGeneratePatches once all the shared test cases are moved to their own patch package.
97+
func AssertGeneratePatches[T mutation.GeneratePatches](
98+
t GinkgoTInterface,
99+
handlerCreator func() T,
100+
tt *PatchTestDef,
101+
) {
102+
t.Helper()
103+
104+
g := gomega.NewWithT(t)
105+
h := handlerCreator()
106+
req := &runtimehooksv1.GeneratePatchesRequest{
107+
Variables: tt.Vars,
108+
Items: []runtimehooksv1.GeneratePatchesRequestItem{
109+
tt.RequestItem,
110+
{
111+
HolderReference: runtimehooksv1.HolderReference{
112+
APIVersion: capiv1.GroupVersion.String(),
113+
Kind: "Cluster",
114+
Namespace: request.Namespace,
115+
Name: request.ClusterName,
116+
},
117+
},
118+
},
119+
}
120+
resp := &runtimehooksv1.GeneratePatchesResponse{}
121+
h.GeneratePatches(context.Background(), req, resp)
122+
expectedStatus := runtimehooksv1.ResponseStatusSuccess
123+
if tt.ExpectedFailure {
124+
expectedStatus = runtimehooksv1.ResponseStatusFailure
125+
}
126+
g.Expect(resp.Status).
127+
To(gomega.Equal(expectedStatus), fmt.Sprintf("Message: %s", resp.Message))
128+
129+
if len(tt.ExpectedPatchMatchers) == 0 {
130+
g.Expect(resp.Items).To(gomega.BeEmpty())
131+
return
132+
}
133+
g.Expect(resp.Items).To(containPatches(&tt.RequestItem, tt.ExpectedPatchMatchers...))
134+
135+
if len(tt.UnexpectedPatchMatchers) > 0 {
136+
g.Expect(resp.Items).
137+
ToNot(containPatches(&tt.RequestItem, tt.UnexpectedPatchMatchers...))
138+
}
139+
}
140+
95141
// VariableWithValue returns a runtimehooksv1.Variable with the passed name and value.
96142
func VariableWithValue(
97143
variableName string,

pkg/handlers/aws/mutation/metapatch_handler_test.go

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,12 @@ import (
2121
instancetypetests "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/aws/mutation/instancetype/tests"
2222
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/aws/mutation/network"
2323
networktests "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/aws/mutation/network/tests"
24-
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/aws/mutation/region"
25-
regiontests "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/aws/mutation/region/tests"
2624
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/clusterconfig"
2725
auditpolicytests "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/mutation/auditpolicy/tests"
2826
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/mutation/etcd"
2927
etcdtests "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/mutation/etcd/tests"
3028
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/mutation/extraapiservercertsans"
3129
extraapiservercertsanstests "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/mutation/extraapiservercertsans/tests"
32-
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/mutation/httpproxy"
33-
httpproxytests "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/mutation/httpproxy/tests"
3430
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/mutation/imageregistries"
3531
imageregistrycredentialstests "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/mutation/imageregistries/credentials/tests"
3632
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/mutation/kubernetesimagerepository"
@@ -59,14 +55,6 @@ func TestGeneratePatches(t *testing.T) {
5955

6056
mgr := testEnv.Manager
6157

62-
regiontests.TestGeneratePatches(
63-
t,
64-
metaPatchGeneratorFunc(mgr),
65-
clusterconfig.MetaVariableName,
66-
v1alpha1.AWSVariableName,
67-
region.VariableName,
68-
)
69-
7058
iaminstanceprofiletests.TestControlPlaneGeneratePatches(
7159
t,
7260
metaPatchGeneratorFunc(mgr),
@@ -114,13 +102,6 @@ func TestGeneratePatches(t *testing.T) {
114102
metaPatchGeneratorFunc(mgr),
115103
)
116104

117-
httpproxytests.TestGeneratePatches(
118-
t,
119-
metaPatchGeneratorFunc(mgr),
120-
clusterconfig.MetaVariableName,
121-
httpproxy.VariableName,
122-
)
123-
124105
etcdtests.TestGeneratePatches(
125106
t,
126107
metaPatchGeneratorFunc(mgr),

pkg/handlers/aws/mutation/region/inject_test.go

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@ import (
77
"testing"
88

99
. "github.com/onsi/ginkgo/v2"
10+
"github.com/onsi/gomega"
1011
. "github.com/onsi/gomega"
12+
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
1113

1214
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/api/v1alpha1"
1315
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/handlers/mutation"
14-
regiontests "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/aws/mutation/region/tests"
16+
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/common/pkg/testutils/capitest"
17+
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/common/pkg/testutils/capitest/request"
1518
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/clusterconfig"
1619
)
1720

@@ -25,11 +28,39 @@ var _ = Describe("Generate AWS Region patches", func() {
2528
patchGenerator := func() mutation.GeneratePatches {
2629
return mutation.NewMetaGeneratePatchesHandler("", NewPatch()).(mutation.GeneratePatches)
2730
}
28-
regiontests.TestGeneratePatches(
29-
GinkgoT(),
30-
patchGenerator,
31-
clusterconfig.MetaVariableName,
32-
v1alpha1.AWSVariableName,
33-
VariableName,
34-
)
31+
32+
testDefs := []capitest.PatchTestDef{
33+
{
34+
Name: "unset variable",
35+
},
36+
{
37+
Name: "region set",
38+
Vars: []runtimehooksv1.Variable{
39+
capitest.VariableWithValue(
40+
clusterconfig.MetaVariableName,
41+
"a-specific-region",
42+
v1alpha1.AWSVariableName,
43+
VariableName,
44+
),
45+
},
46+
RequestItem: request.NewAWSClusterTemplateRequestItem("1234"),
47+
ExpectedPatchMatchers: []capitest.JSONPatchMatcher{{
48+
Operation: "add",
49+
Path: "/spec/template/spec/region",
50+
ValueMatcher: gomega.Equal("a-specific-region"),
51+
}},
52+
},
53+
}
54+
55+
// create test node for each case
56+
for testIdx := range testDefs {
57+
tt := testDefs[testIdx]
58+
It(tt.Name, func() {
59+
capitest.AssertGeneratePatches(
60+
GinkgoT(),
61+
patchGenerator,
62+
&tt,
63+
)
64+
})
65+
}
3566
})

pkg/handlers/aws/mutation/region/tests/generate_patches.go

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

pkg/handlers/docker/mutation/metapatch_handler_test.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ import (
1919
etcdtests "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/mutation/etcd/tests"
2020
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/mutation/extraapiservercertsans"
2121
extraapiservercertsanstests "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/mutation/extraapiservercertsans/tests"
22-
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/mutation/httpproxy"
23-
httpproxytests "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/mutation/httpproxy/tests"
2422
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/mutation/imageregistries"
2523
imageregistrycredentialstests "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/mutation/imageregistries/credentials/tests"
2624
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/mutation/kubernetesimagerepository"
@@ -71,13 +69,6 @@ func TestGeneratePatches(t *testing.T) {
7169
metaPatchGeneratorFunc(mgr),
7270
)
7371

74-
httpproxytests.TestGeneratePatches(
75-
t,
76-
metaPatchGeneratorFunc(mgr),
77-
clusterconfig.MetaVariableName,
78-
httpproxy.VariableName,
79-
)
80-
8172
etcdtests.TestGeneratePatches(
8273
t,
8374
metaPatchGeneratorFunc(mgr),

pkg/handlers/generic/mutation/httpproxy/inject_test.go

Lines changed: 84 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,15 @@ import (
99
. "github.com/onsi/ginkgo/v2"
1010
"github.com/onsi/gomega"
1111
v1 "k8s.io/api/core/v1"
12+
"k8s.io/apiserver/pkg/storage/names"
1213
capiv1 "sigs.k8s.io/cluster-api/api/v1beta1"
14+
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
1315

16+
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/api/v1alpha1"
1417
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/handlers/mutation"
18+
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/common/pkg/testutils/capitest"
19+
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/common/pkg/testutils/capitest/request"
1520
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/clusterconfig"
16-
httpproxy "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/mutation/httpproxy/tests"
1721
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/test/helpers"
1822
)
1923

@@ -201,10 +205,83 @@ var _ = Describe("Generate HTTPProxy Patches", func() {
201205
"",
202206
NewPatch(testEnv.Client)).(mutation.GeneratePatches)
203207
}
204-
httpproxy.TestGeneratePatches(
205-
GinkgoT(),
206-
patchGenerator,
207-
clusterconfig.MetaVariableName,
208-
VariableName,
209-
)
208+
209+
testDefs := []capitest.PatchTestDef{
210+
{
211+
Name: "unset variable",
212+
},
213+
{
214+
Name: "http proxy set for KubeadmConfigTemplate generic worker",
215+
Vars: []runtimehooksv1.Variable{
216+
capitest.VariableWithValue(
217+
clusterconfig.MetaVariableName,
218+
v1alpha1.HTTPProxy{
219+
HTTP: "http://example.com",
220+
HTTPS: "https://example.com",
221+
AdditionalNo: []string{"no-proxy.example.com"},
222+
},
223+
VariableName,
224+
),
225+
capitest.VariableWithValue(
226+
"builtin",
227+
map[string]any{
228+
"machineDeployment": map[string]any{
229+
"class": names.SimpleNameGenerator.GenerateName("worker-"),
230+
},
231+
},
232+
),
233+
},
234+
RequestItem: request.NewKubeadmConfigTemplateRequestItem(""),
235+
ExpectedPatchMatchers: []capitest.JSONPatchMatcher{{
236+
Operation: "add",
237+
Path: "/spec/template/spec/files",
238+
ValueMatcher: gomega.ContainElements(
239+
gomega.HaveKeyWithValue(
240+
"path", "/etc/systemd/system/containerd.service.d/http-proxy.conf",
241+
),
242+
gomega.HaveKeyWithValue(
243+
"path", "/etc/systemd/system/kubelet.service.d/http-proxy.conf",
244+
),
245+
),
246+
}},
247+
},
248+
{
249+
Name: "http proxy set for KubeadmControlPlaneTemplate",
250+
Vars: []runtimehooksv1.Variable{
251+
capitest.VariableWithValue(
252+
clusterconfig.MetaVariableName,
253+
v1alpha1.HTTPProxy{
254+
HTTP: "http://example.com",
255+
HTTPS: "https://example.com",
256+
AdditionalNo: []string{"no-proxy.example.com"},
257+
},
258+
VariableName,
259+
),
260+
},
261+
RequestItem: request.NewKubeadmControlPlaneTemplateRequestItem(""),
262+
ExpectedPatchMatchers: []capitest.JSONPatchMatcher{{
263+
Operation: "add",
264+
Path: "/spec/template/spec/kubeadmConfigSpec/files",
265+
ValueMatcher: gomega.ContainElements(
266+
gomega.HaveKeyWithValue(
267+
"path", "/etc/systemd/system/containerd.service.d/http-proxy.conf",
268+
),
269+
gomega.HaveKeyWithValue(
270+
"path", "/etc/systemd/system/kubelet.service.d/http-proxy.conf",
271+
),
272+
),
273+
}},
274+
},
275+
}
276+
// create test node for each case
277+
for testIdx := range testDefs {
278+
tt := testDefs[testIdx]
279+
It(tt.Name, func() {
280+
capitest.AssertGeneratePatches(
281+
GinkgoT(),
282+
patchGenerator,
283+
&tt,
284+
)
285+
})
286+
}
210287
})

0 commit comments

Comments
 (0)