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

Commit 6bc9d1e

Browse files
committed
test: nove machinedetails unit tests
1 parent c82c8dc commit 6bc9d1e

File tree

4 files changed

+103
-81
lines changed

4 files changed

+103
-81
lines changed

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 & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +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/machinedetails"
27-
machinedetailstests "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/nutanix/mutation/machinedetails/tests"
2824
)
2925

3026
func metaPatchGeneratorFunc(mgr manager.Manager) func() mutation.GeneratePatches {
@@ -33,34 +29,11 @@ func metaPatchGeneratorFunc(mgr manager.Manager) func() mutation.GeneratePatches
3329
}
3430
}
3531

36-
func workerPatchGeneratorFunc() func() mutation.GeneratePatches {
37-
return func() mutation.GeneratePatches {
38-
return MetaWorkerPatchHandler().(mutation.GeneratePatches)
39-
}
40-
}
41-
4232
func TestGeneratePatches(t *testing.T) {
4333
t.Parallel()
4434

4535
mgr := testEnv.Manager
4636

47-
machinedetailstests.TestControlPlaneGeneratePatches(
48-
t,
49-
metaPatchGeneratorFunc(mgr),
50-
clusterconfig.MetaVariableName,
51-
clusterconfig.MetaControlPlaneConfigName,
52-
nutanixclusterconfig.NutanixVariableName,
53-
machinedetails.VariableName,
54-
)
55-
56-
machinedetailstests.TestWorkerGeneratePatches(
57-
t,
58-
workerPatchGeneratorFunc(),
59-
workerconfig.MetaVariableName,
60-
nutanixclusterconfig.NutanixVariableName,
61-
machinedetails.VariableName,
62-
)
63-
6437
auditpolicytests.TestGeneratePatches(
6538
t,
6639
metaPatchGeneratorFunc(mgr),

0 commit comments

Comments
 (0)