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

Commit c662b02

Browse files
committed
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
1 parent 13fbf50 commit c662b02

File tree

6 files changed

+175
-139
lines changed

6 files changed

+175
-139
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,48 @@
11
// Copyright 2023 D2iQ, Inc. All rights reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
package tests
4+
package controlplaneendpoint
55

66
import (
77
"testing"
88

9+
. "github.com/onsi/ginkgo/v2"
910
"github.com/onsi/gomega"
1011
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
1112
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
1213

1314
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/handlers/mutation"
1415
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/common/pkg/testutils/capitest"
1516
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/common/pkg/testutils/capitest/request"
17+
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/clusterconfig"
18+
nutanixclusterconfig "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/nutanix/clusterconfig"
1619
)
1720

18-
func TestGeneratePatches(
19-
t *testing.T,
20-
generatorFunc func() mutation.GeneratePatches,
21-
variableName string,
22-
variablePath ...string,
23-
) {
24-
t.Helper()
25-
26-
capitest.ValidateGeneratePatches(
27-
t,
28-
generatorFunc,
29-
capitest.PatchTestDef{
21+
func TestControlPlaneEndpointPatch(t *testing.T) {
22+
gomega.RegisterFailHandler(Fail)
23+
RunSpecs(t, "Nutanix ControlPlane endpoint suite")
24+
}
25+
26+
var _ = Describe("Generate Nutanix ControlPlane endpoint patches", func() {
27+
patchGenerator := func() mutation.GeneratePatches {
28+
return mutation.NewMetaGeneratePatchesHandler("", NewPatch()).(mutation.GeneratePatches)
29+
}
30+
31+
testDefs := []capitest.PatchTestDef{
32+
{
3033
Name: "unset variable",
3134
},
32-
capitest.PatchTestDef{
35+
{
3336
Name: "ControlPlaneEndpoint set to valid host and port",
3437
Vars: []runtimehooksv1.Variable{
3538
capitest.VariableWithValue(
36-
variableName,
39+
clusterconfig.MetaVariableName,
3740
clusterv1.APIEndpoint{
3841
Host: "10.20.100.10",
3942
Port: 6443,
4043
},
41-
variablePath...,
44+
nutanixclusterconfig.NutanixVariableName,
45+
VariableName,
4246
),
4347
},
4448
RequestItem: request.NewNutanixClusterTemplateRequestItem(""),
@@ -55,5 +59,17 @@ func TestGeneratePatches(
5559
},
5660
},
5761
},
58-
)
59-
}
62+
}
63+
64+
// create test node for each case
65+
for testIdx := range testDefs {
66+
tt := testDefs[testIdx]
67+
It(tt.Name, func() {
68+
capitest.AssertGeneratePatches(
69+
GinkgoT(),
70+
patchGenerator,
71+
&tt,
72+
)
73+
})
74+
}
75+
})

pkg/handlers/nutanix/mutation/machinedetails/tests/generate_patches.go renamed to pkg/handlers/nutanix/mutation/machinedetails/inject_control_plane_test.go

Lines changed: 28 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
// Copyright 2023 D2iQ, Inc. All rights reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
package tests
4+
package machinedetails
55

66
import (
7-
"testing"
8-
7+
. "github.com/onsi/ginkgo/v2"
98
"github.com/onsi/gomega"
10-
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
119
"k8s.io/apimachinery/pkg/api/resource"
1210
"k8s.io/utils/ptr"
1311
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
@@ -17,6 +15,8 @@ import (
1715
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/handlers/mutation"
1816
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/common/pkg/testutils/capitest"
1917
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/common/pkg/testutils/capitest/request"
18+
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/clusterconfig"
19+
nutanixclusterconfig "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/nutanix/clusterconfig"
2020
)
2121

2222
var (
@@ -96,66 +96,40 @@ var (
9696
}
9797
)
9898

99-
func TestControlPlaneGeneratePatches(
100-
t *testing.T,
101-
generatorFunc func() mutation.GeneratePatches,
102-
variableName string,
103-
variablePath ...string,
104-
) {
105-
t.Helper()
99+
var _ = Describe("Generate Nutanix Machine Details patches for ControlPlane", func() {
100+
patchGenerator := func() mutation.GeneratePatches {
101+
return mutation.NewMetaGeneratePatchesHandler("", NewControlPlanePatch()).(mutation.GeneratePatches)
102+
}
106103

107-
capitest.ValidateGeneratePatches(
108-
t,
109-
generatorFunc,
110-
capitest.PatchTestDef{
104+
testDefs := []capitest.PatchTestDef{
105+
{
111106
Name: "unset variable",
112107
},
113-
capitest.PatchTestDef{
108+
{
114109
Name: "all fields set for control-plane",
115110
Vars: []runtimehooksv1.Variable{
116111
capitest.VariableWithValue(
117-
variableName,
112+
clusterconfig.MetaVariableName,
118113
variableWithAllFieldsSet,
119-
variablePath...,
114+
clusterconfig.MetaControlPlaneConfigName,
115+
nutanixclusterconfig.NutanixVariableName,
116+
VariableName,
120117
),
121118
},
122119
RequestItem: request.NewCPNutanixMachineTemplateRequestItem(""),
123120
ExpectedPatchMatchers: matchersForAllFieldsSet,
124121
},
125-
)
126-
}
127-
128-
func TestWorkerGeneratePatches(
129-
t *testing.T,
130-
generatorFunc func() mutation.GeneratePatches,
131-
variableName string,
132-
variablePath ...string,
133-
) {
134-
t.Helper()
122+
}
135123

136-
capitest.ValidateGeneratePatches(
137-
t,
138-
generatorFunc,
139-
capitest.PatchTestDef{
140-
Name: "unset variable",
141-
},
142-
capitest.PatchTestDef{
143-
Name: "all fields set for workers",
144-
Vars: []runtimehooksv1.Variable{
145-
capitest.VariableWithValue(
146-
variableName,
147-
variableWithAllFieldsSet,
148-
variablePath...,
149-
),
150-
capitest.VariableWithValue(
151-
"builtin",
152-
apiextensionsv1.JSON{
153-
Raw: []byte(`{"machineDeployment": {"class": "a-worker"}}`),
154-
},
155-
),
156-
},
157-
RequestItem: request.NewWorkerNutanixMachineTemplateRequestItem(""),
158-
ExpectedPatchMatchers: matchersForAllFieldsSet,
159-
},
160-
)
161-
}
124+
// create test node for each case
125+
for testIdx := range testDefs {
126+
tt := testDefs[testIdx]
127+
It(tt.Name, func() {
128+
capitest.AssertGeneratePatches(
129+
GinkgoT(),
130+
patchGenerator,
131+
&tt,
132+
)
133+
})
134+
}
135+
})
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 machinedetails
5+
6+
import (
7+
"testing"
8+
9+
. "github.com/onsi/ginkgo/v2"
10+
. "github.com/onsi/gomega"
11+
)
12+
13+
func TestMachineDetailsPatch(t *testing.T) {
14+
RegisterFailHandler(Fail)
15+
RunSpecs(t, "Nutanix Machine Details patches for ControlPlane and Workers suite")
16+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright 2023 D2iQ, Inc. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package machinedetails
5+
6+
import (
7+
. "github.com/onsi/ginkgo/v2"
8+
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
9+
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
10+
11+
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/handlers/mutation"
12+
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/common/pkg/testutils/capitest"
13+
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/common/pkg/testutils/capitest/request"
14+
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/workerconfig"
15+
nutanixclusterconfig "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/nutanix/clusterconfig"
16+
)
17+
18+
var _ = Describe("Generate Nutanix Machine Details patches for Worker", func() {
19+
patchGenerator := func() mutation.GeneratePatches {
20+
return mutation.NewMetaGeneratePatchesHandler("", NewWorkerPatch()).(mutation.GeneratePatches)
21+
}
22+
23+
testDefs := []capitest.PatchTestDef{
24+
{
25+
Name: "unset variable",
26+
},
27+
{
28+
Name: "all fields set for workers",
29+
Vars: []runtimehooksv1.Variable{
30+
capitest.VariableWithValue(
31+
workerconfig.MetaVariableName,
32+
variableWithAllFieldsSet,
33+
nutanixclusterconfig.NutanixVariableName,
34+
VariableName,
35+
),
36+
capitest.VariableWithValue(
37+
"builtin",
38+
apiextensionsv1.JSON{
39+
Raw: []byte(`{"machineDeployment": {"class": "a-worker"}}`),
40+
},
41+
),
42+
},
43+
RequestItem: request.NewWorkerNutanixMachineTemplateRequestItem(""),
44+
ExpectedPatchMatchers: matchersForAllFieldsSet,
45+
},
46+
}
47+
48+
// create test node for each case
49+
for testIdx := range testDefs {
50+
tt := testDefs[testIdx]
51+
It(tt.Name, func() {
52+
capitest.AssertGeneratePatches(
53+
GinkgoT(),
54+
patchGenerator,
55+
&tt,
56+
)
57+
})
58+
}
59+
})

pkg/handlers/nutanix/mutation/metapatch_handler_test.go

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,6 @@ import (
2121
kubernetesimagerepositorytests "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/mutation/kubernetesimagerepository/tests"
2222
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/mutation/mirrors"
2323
globalimageregistrymirrortests "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/mutation/mirrors/tests"
24-
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/workerconfig"
25-
nutanixclusterconfig "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/nutanix/clusterconfig"
26-
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/nutanix/mutation/controlplaneendpoint"
27-
controlplaneendpointtests "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/nutanix/mutation/controlplaneendpoint/tests"
28-
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/nutanix/mutation/machinedetails"
29-
machinedetailstests "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/nutanix/mutation/machinedetails/tests"
30-
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/nutanix/mutation/prismcentralendpoint"
31-
prismcentralendpointtests "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/nutanix/mutation/prismcentralendpoint/tests"
3224
)
3325

3426
func metaPatchGeneratorFunc(mgr manager.Manager) func() mutation.GeneratePatches {
@@ -37,50 +29,11 @@ func metaPatchGeneratorFunc(mgr manager.Manager) func() mutation.GeneratePatches
3729
}
3830
}
3931

40-
func workerPatchGeneratorFunc() func() mutation.GeneratePatches {
41-
return func() mutation.GeneratePatches {
42-
return MetaWorkerPatchHandler().(mutation.GeneratePatches)
43-
}
44-
}
45-
4632
func TestGeneratePatches(t *testing.T) {
4733
t.Parallel()
4834

4935
mgr := testEnv.Manager
5036

51-
controlplaneendpointtests.TestGeneratePatches(
52-
t,
53-
metaPatchGeneratorFunc(mgr),
54-
clusterconfig.MetaVariableName,
55-
nutanixclusterconfig.NutanixVariableName,
56-
controlplaneendpoint.VariableName,
57-
)
58-
59-
prismcentralendpointtests.TestGeneratePatches(
60-
t,
61-
metaPatchGeneratorFunc(mgr),
62-
clusterconfig.MetaVariableName,
63-
nutanixclusterconfig.NutanixVariableName,
64-
prismcentralendpoint.VariableName,
65-
)
66-
67-
machinedetailstests.TestControlPlaneGeneratePatches(
68-
t,
69-
metaPatchGeneratorFunc(mgr),
70-
clusterconfig.MetaVariableName,
71-
clusterconfig.MetaControlPlaneConfigName,
72-
nutanixclusterconfig.NutanixVariableName,
73-
machinedetails.VariableName,
74-
)
75-
76-
machinedetailstests.TestWorkerGeneratePatches(
77-
t,
78-
workerPatchGeneratorFunc(),
79-
workerconfig.MetaVariableName,
80-
nutanixclusterconfig.NutanixVariableName,
81-
machinedetails.VariableName,
82-
)
83-
8437
auditpolicytests.TestGeneratePatches(
8538
t,
8639
metaPatchGeneratorFunc(mgr),

0 commit comments

Comments
 (0)