Skip to content

Commit 01f8ed6

Browse files
committed
test: unit test for placement group discovery
1 parent a8f5b6f commit 01f8ed6

File tree

4 files changed

+153
-3
lines changed

4 files changed

+153
-3
lines changed

pkg/handlers/aws/mutation/placementgroupnfd/inject_controlplane.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
77
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
88
cabpkv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1"
9+
controlplanev1 "sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1beta1"
910
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
1011
ctrl "sigs.k8s.io/controller-runtime"
1112
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -73,12 +74,12 @@ func (h *controlPlanePatchHandler) Mutate(
7374
vars,
7475
&holderRef,
7576
selectors.ControlPlane(), log,
76-
func(obj *cabpkv1.KubeadmConfigTemplate) error {
77+
func(obj *controlplanev1.KubeadmControlPlaneTemplate) error {
7778
log.WithValues(
7879
"patchedObjectKind", obj.GetObjectKind().GroupVersionKind().String(),
7980
"patchedObjectName", client.ObjectKeyFromObject(obj),
80-
).Info("setting placement group for local node feature discovery in AWS controlplane KubeadmConfigTemplate")
81-
obj.Spec.Template.Spec.Files = append(obj.Spec.Template.Spec.Files, cabpkv1.File{
81+
).Info("setting placement group for local node feature discovery in AWS controlplane KubeadmControlPlaneTemplate")
82+
obj.Spec.Template.Spec.KubeadmConfigSpec.Files = append(obj.Spec.Template.Spec.KubeadmConfigSpec.Files, cabpkv1.File{
8283
Path: PlacementGroupDiscoveryScriptFileOnRemote,
8384
Content: string(PlacementgroupDiscoveryScript),
8485
Permissions: "0700",
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright 2025 Nutanix. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package placementgroupnfd
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/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/v1alpha1"
12+
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/handlers/mutation"
13+
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/testutils/capitest"
14+
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/testutils/capitest/request"
15+
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/test/helpers"
16+
)
17+
18+
var _ = Describe("Generate AWS Placement Group NFD patches for ControlPlane", func() {
19+
patchGenerator := func() mutation.GeneratePatches {
20+
return mutation.NewMetaGeneratePatchesHandler(
21+
"",
22+
helpers.TestEnv.Client,
23+
NewControlPlanePatch(),
24+
).(mutation.GeneratePatches)
25+
}
26+
27+
testDefs := []capitest.PatchTestDef{
28+
{
29+
Name: "unset variable",
30+
},
31+
{
32+
Name: "placement group set for control plane",
33+
Vars: []runtimehooksv1.Variable{
34+
capitest.VariableWithValue(
35+
v1alpha1.ClusterConfigVariableName,
36+
v1alpha1.PlacementGroup{
37+
Name: "test-placement-group",
38+
},
39+
v1alpha1.ControlPlaneConfigVariableName,
40+
v1alpha1.AWSVariableName,
41+
"placementGroup",
42+
),
43+
},
44+
RequestItem: request.NewKubeadmControlPlaneTemplateRequestItem("1234"),
45+
ExpectedPatchMatchers: []capitest.JSONPatchMatcher{{
46+
Operation: "add",
47+
Path: "/spec/template/spec/kubeadmConfigSpec/files",
48+
ValueMatcher: gomega.ContainElement(gomega.HaveKeyWithValue(
49+
"path", PlacementGroupDiscoveryScriptFileOnRemote,
50+
)),
51+
}},
52+
},
53+
}
54+
55+
// create test node for each case
56+
for _, tt := range testDefs {
57+
It(tt.Name, func() {
58+
capitest.AssertGeneratePatches(
59+
GinkgoT(),
60+
patchGenerator,
61+
&tt,
62+
)
63+
})
64+
}
65+
})
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2025 Nutanix. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package placementgroupnfd
5+
6+
import (
7+
"testing"
8+
9+
. "github.com/onsi/ginkgo/v2"
10+
. "github.com/onsi/gomega"
11+
)
12+
13+
func TestPlacementGroupNFDPatch(t *testing.T) {
14+
RegisterFailHandler(Fail)
15+
RunSpecs(t, "AWS Placement Group NFD mutator suite")
16+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright 2025 Nutanix. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package placementgroupnfd
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/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/v1alpha1"
12+
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/handlers/mutation"
13+
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/testutils/capitest"
14+
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/testutils/capitest/request"
15+
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/test/helpers"
16+
)
17+
18+
var _ = Describe("Generate AWS Placement Group NFD patches for Worker", func() {
19+
patchGenerator := func() mutation.GeneratePatches {
20+
return mutation.NewMetaGeneratePatchesHandler("", helpers.TestEnv.Client, NewWorkerPatch()).(mutation.GeneratePatches)
21+
}
22+
23+
testDefs := []capitest.PatchTestDef{
24+
{
25+
Name: "unset variable",
26+
},
27+
{
28+
Name: "placement group set for workers",
29+
Vars: []runtimehooksv1.Variable{
30+
capitest.VariableWithValue(
31+
v1alpha1.WorkerConfigVariableName,
32+
v1alpha1.PlacementGroup{
33+
Name: "test-placement-group",
34+
},
35+
v1alpha1.AWSVariableName,
36+
"placementGroup",
37+
),
38+
capitest.VariableWithValue(
39+
runtimehooksv1.BuiltinsName,
40+
map[string]any{
41+
"machineDeployment": map[string]any{
42+
"class": "a-worker",
43+
},
44+
},
45+
),
46+
},
47+
RequestItem: request.NewKubeadmConfigTemplateRequestItem("1234"),
48+
ExpectedPatchMatchers: []capitest.JSONPatchMatcher{{
49+
Operation: "add",
50+
Path: "/spec/template/spec/files",
51+
ValueMatcher: gomega.ContainElement(gomega.HaveKeyWithValue(
52+
"path", PlacementGroupDiscoveryScriptFileOnRemote,
53+
)),
54+
}},
55+
},
56+
}
57+
58+
// create test node for each case
59+
for _, tt := range testDefs {
60+
It(tt.Name, func() {
61+
capitest.AssertGeneratePatches(
62+
GinkgoT(),
63+
patchGenerator,
64+
&tt,
65+
)
66+
})
67+
}
68+
})

0 commit comments

Comments
 (0)