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

Commit 184e7d6

Browse files
authored
test: move all patch generator unit tests to their own packages (#23)
* test: unit test for individual patch generator * test: package level unit test for HTTPProxy * test: move region and httpproxy patch generator unit test invocation * fix: linting errors * test: move all AWS patch unit tests to their own packages (#24) * test: move instanceprofile tests to its own package * test: move instancetype unit tests to its own package * test: move ami unit tests to its own package * test: move aws network tests to its own package * test: move controlplaneloadbalancer unit tests to its own package * test: move aws cni unit tests to its own package * test: fix linting errors * test: unit tests for AWS security groups * test: move customimage unit tests to their own package (#30) * test: move all Nutanix patch handler unit tests (#32) * test: move controlplane endpoint unit tests * test: move PC endpoint unit tests * test: nove machinedetails unit tests * test: move generic patch unit tests to own packages (#31) * test: move audit policy tests to their own package * test: move etcd unit tests to their own package * test: move extra api server cert sans to its own package * test: move image registry unit tests to its own package * test: move kubernetes image repository unit tests * test: move mirror unit tests * test: move users unit tests * test: remove gereric unit tests from nutanix meta patch handler * test: cleaned up meta level unit test suites
1 parent f8c47e2 commit 184e7d6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1775
-1791
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,
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
// Copyright 2023 D2iQ, Inc. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package ami
5+
6+
import (
7+
. "github.com/onsi/ginkgo/v2"
8+
"github.com/onsi/gomega"
9+
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
10+
11+
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/api/v1alpha1"
12+
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/handlers/mutation"
13+
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/common/pkg/testutils/capitest"
14+
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/common/pkg/testutils/capitest/request"
15+
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/clusterconfig"
16+
)
17+
18+
var _ = Describe("Generate AMI patches for ControlPlane", func() {
19+
patchGenerator := func() mutation.GeneratePatches {
20+
return mutation.NewMetaGeneratePatchesHandler("", NewControlPlanePatch()).(mutation.GeneratePatches)
21+
}
22+
23+
testDefs := []capitest.PatchTestDef{
24+
{
25+
Name: "AMI set for control plane",
26+
Vars: []runtimehooksv1.Variable{
27+
capitest.VariableWithValue(
28+
clusterconfig.MetaVariableName,
29+
v1alpha1.AMISpec{ID: "ami-controlplane"},
30+
clusterconfig.MetaControlPlaneConfigName,
31+
v1alpha1.AWSVariableName,
32+
VariableName,
33+
),
34+
},
35+
RequestItem: request.NewCPAWSMachineTemplateRequestItem("1234"),
36+
ExpectedPatchMatchers: []capitest.JSONPatchMatcher{
37+
{
38+
Operation: "add",
39+
Path: "/spec/template/spec/ami/id",
40+
ValueMatcher: gomega.Equal("ami-controlplane"),
41+
},
42+
},
43+
},
44+
{
45+
Name: "AMI lookup format set for control plane",
46+
Vars: []runtimehooksv1.Variable{
47+
capitest.VariableWithValue(
48+
clusterconfig.MetaVariableName,
49+
v1alpha1.AMISpec{
50+
Lookup: &v1alpha1.AMILookup{
51+
Format: "test-{{.kubernetesVersion}}-format",
52+
Org: "1234",
53+
BaseOS: "testOS",
54+
},
55+
},
56+
clusterconfig.MetaControlPlaneConfigName,
57+
v1alpha1.AWSVariableName,
58+
VariableName,
59+
),
60+
},
61+
RequestItem: request.NewCPAWSMachineTemplateRequestItem("1234"),
62+
ExpectedPatchMatchers: []capitest.JSONPatchMatcher{
63+
{
64+
Operation: "add",
65+
Path: "/spec/template/spec/imageLookupFormat",
66+
ValueMatcher: gomega.Equal("test-{{.kubernetesVersion}}-format"),
67+
},
68+
{
69+
Operation: "add",
70+
Path: "/spec/template/spec/imageLookupOrg",
71+
ValueMatcher: gomega.Equal("1234"),
72+
},
73+
{
74+
Operation: "add",
75+
Path: "/spec/template/spec/imageLookupBaseOS",
76+
ValueMatcher: gomega.Equal("testOS"),
77+
},
78+
},
79+
UnexpectedPatchMatchers: []capitest.JSONPatchMatcher{
80+
{
81+
Operation: "add",
82+
Path: "/spec/template/spec/ami/id",
83+
ValueMatcher: gomega.Equal(""),
84+
},
85+
},
86+
},
87+
}
88+
89+
// create test node for each case
90+
for testIdx := range testDefs {
91+
tt := testDefs[testIdx]
92+
It(tt.Name, func() {
93+
capitest.AssertGeneratePatches(
94+
GinkgoT(),
95+
patchGenerator,
96+
&tt,
97+
)
98+
})
99+
}
100+
})
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2023 D2iQ, Inc. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package ami
5+
6+
import (
7+
"testing"
8+
9+
. "github.com/onsi/ginkgo/v2"
10+
. "github.com/onsi/gomega"
11+
)
12+
13+
func TestAMIPatch(t *testing.T) {
14+
RegisterFailHandler(Fail)
15+
RunSpecs(t, "AMI patches for ControlPlane and Workers suite")
16+
}
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
// Copyright 2023 D2iQ, Inc. All rights reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
package tests
4+
package ami
55

66
import (
7-
"testing"
8-
7+
. "github.com/onsi/ginkgo/v2"
98
"github.com/onsi/gomega"
109
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
1110
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
@@ -14,99 +13,23 @@ import (
1413
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/handlers/mutation"
1514
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/common/pkg/testutils/capitest"
1615
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/common/pkg/testutils/capitest/request"
16+
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/workerconfig"
1717
)
1818

19-
func TestControlPlaneGeneratePatches(
20-
t *testing.T,
21-
generatorFunc func() mutation.GeneratePatches,
22-
variableName string,
23-
variablePath ...string,
24-
) {
25-
t.Helper()
26-
27-
capitest.ValidateGeneratePatches(
28-
t,
29-
generatorFunc,
30-
capitest.PatchTestDef{
31-
Name: "AMI set for control plane",
32-
Vars: []runtimehooksv1.Variable{
33-
capitest.VariableWithValue(
34-
variableName,
35-
v1alpha1.AMISpec{ID: "ami-controlplane"},
36-
variablePath...,
37-
),
38-
},
39-
RequestItem: request.NewCPAWSMachineTemplateRequestItem("1234"),
40-
ExpectedPatchMatchers: []capitest.JSONPatchMatcher{
41-
{
42-
Operation: "add",
43-
Path: "/spec/template/spec/ami/id",
44-
ValueMatcher: gomega.Equal("ami-controlplane"),
45-
},
46-
},
47-
},
48-
capitest.PatchTestDef{
49-
Name: "AMI lookup format set for control plane",
50-
Vars: []runtimehooksv1.Variable{
51-
capitest.VariableWithValue(
52-
variableName,
53-
v1alpha1.AMISpec{
54-
Lookup: &v1alpha1.AMILookup{
55-
Format: "test-{{.kubernetesVersion}}-format",
56-
Org: "12345",
57-
BaseOS: "testOS",
58-
},
59-
},
60-
variablePath...,
61-
),
62-
},
63-
RequestItem: request.NewCPAWSMachineTemplateRequestItem("1234"),
64-
ExpectedPatchMatchers: []capitest.JSONPatchMatcher{
65-
{
66-
Operation: "add",
67-
Path: "/spec/template/spec/imageLookupFormat",
68-
ValueMatcher: gomega.Equal("test-{{.kubernetesVersion}}-format"),
69-
},
70-
{
71-
Operation: "add",
72-
Path: "/spec/template/spec/imageLookupOrg",
73-
ValueMatcher: gomega.Equal("12345"),
74-
},
75-
{
76-
Operation: "add",
77-
Path: "/spec/template/spec/imageLookupBaseOS",
78-
ValueMatcher: gomega.Equal("testOS"),
79-
},
80-
},
81-
UnexpectedPatchMatchers: []capitest.JSONPatchMatcher{
82-
{
83-
Operation: "add",
84-
Path: "/spec/template/spec/ami/id",
85-
ValueMatcher: gomega.Equal(""),
86-
},
87-
},
88-
},
89-
)
90-
}
91-
92-
func TestWorkerGeneratePatches(
93-
t *testing.T,
94-
generatorFunc func() mutation.GeneratePatches,
95-
variableName string,
96-
variablePath ...string,
97-
) {
98-
t.Helper()
19+
var _ = Describe("Generate AMI patches for Worker", func() {
20+
patchGenerator := func() mutation.GeneratePatches {
21+
return mutation.NewMetaGeneratePatchesHandler("", NewWorkerPatch()).(mutation.GeneratePatches)
22+
}
9923

100-
capitest.ValidateGeneratePatches(
101-
t,
102-
generatorFunc,
103-
capitest.PatchTestDef{
24+
testDefs := []capitest.PatchTestDef{
25+
{
10426
Name: "AMI set for workers",
10527
Vars: []runtimehooksv1.Variable{
10628
capitest.VariableWithValue(
107-
variableName,
29+
workerconfig.MetaVariableName,
10830
v1alpha1.AMISpec{ID: "ami-controlplane"},
109-
variablePath...,
31+
v1alpha1.AWSVariableName,
32+
VariableName,
11033
),
11134
capitest.VariableWithValue(
11235
"builtin",
@@ -124,20 +47,20 @@ func TestWorkerGeneratePatches(
12447
},
12548
},
12649
},
127-
capitest.PatchTestDef{
50+
{
12851
Name: "AMI lookup format set for worker",
12952
Vars: []runtimehooksv1.Variable{
13053
capitest.VariableWithValue(
131-
variableName,
54+
workerconfig.MetaVariableName,
13255
v1alpha1.AMISpec{
13356
Lookup: &v1alpha1.AMILookup{
13457
Format: "test-{{.kubernetesVersion}}-format",
13558
Org: "12345",
13659
BaseOS: "testOS",
13760
},
13861
},
139-
140-
variablePath...,
62+
v1alpha1.AWSVariableName,
63+
VariableName,
14164
),
14265
capitest.VariableWithValue(
14366
"builtin",
@@ -172,5 +95,17 @@ func TestWorkerGeneratePatches(
17295
},
17396
},
17497
},
175-
)
176-
}
98+
}
99+
100+
// create test node for each case
101+
for testIdx := range testDefs {
102+
tt := testDefs[testIdx]
103+
It(tt.Name, func() {
104+
capitest.AssertGeneratePatches(
105+
GinkgoT(),
106+
patchGenerator,
107+
&tt,
108+
)
109+
})
110+
}
111+
})

0 commit comments

Comments
 (0)