Skip to content

Commit 5a6079b

Browse files
committed
test: unit test for prenodeadmcommands for placementgroup
1 parent 7680909 commit 5a6079b

File tree

9 files changed

+149
-22
lines changed

9 files changed

+149
-22
lines changed

pkg/handlers/aws/mutation/placementgroupnfd/embedded/placementgroup_discovery.sh

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,30 @@ PLACEMENT_GROUP_FEATURE_DIR="/etc/kubernetes/node-feature-discovery/features.d"
88
PLACEMENT_GROUP_FEATURE_FILE="${PLACEMENT_GROUP_FEATURE_DIR}/placementgroup"
99
# Fetch IMDSv2 token
1010
TOKEN=$(curl -s -X PUT "http://169.254.169.254/latest/api/token" \
11-
-H "X-aws-ec2-metadata-token-ttl-seconds: 21600")
11+
-H "X-aws-ec2-metadata-token-ttl-seconds: 21600")
1212

1313
# Get placement info with HTTP status check
1414
PARTITION_RESPONSE=$(curl -s -w "%{http_code}" -H "X-aws-ec2-metadata-token: $TOKEN" \
15-
http://169.254.169.254/latest/meta-data/placement/partition-number)
15+
http://169.254.169.254/latest/meta-data/placement/partition-number)
1616

1717
PG_RESPONSE=$(curl -s -w "%{http_code}" -H "X-aws-ec2-metadata-token: $TOKEN" \
18-
http://169.254.169.254/latest/meta-data/placement/group-name)
18+
http://169.254.169.254/latest/meta-data/placement/group-name)
1919

2020
# Extract HTTP status codes and content
2121
PARTITION_HTTP_CODE="${PARTITION_RESPONSE: -3}" # last 3 characters are the HTTP status code
22-
PARTITION_CONTENT="${PARTITION_RESPONSE%???}" # remove the last 3 characters to get the content
22+
PARTITION_CONTENT="${PARTITION_RESPONSE%???}" # remove the last 3 characters to get the content
2323

2424
mkdir -p "${PLACEMENT_GROUP_FEATURE_DIR}"
2525
touch "${PLACEMENT_GROUP_FEATURE_FILE}"
2626

2727
# Only print features if HTTP 200 response
2828
if [ "$PARTITION_HTTP_CODE" = "200" ] && [ -n "$PARTITION_CONTENT" ]; then
29-
echo "feature.node.kubernetes.io/partition=${PARTITION_CONTENT}" >> "${PLACEMENT_GROUP_FEATURE_FILE}"
29+
echo "feature.node.kubernetes.io/partition=${PARTITION_CONTENT}" >>"${PLACEMENT_GROUP_FEATURE_FILE}"
3030
fi
3131

3232
PG_HTTP_CODE="${PG_RESPONSE: -3}" # last 3 characters are the HTTP status code
33-
PG_CONTENT="${PG_RESPONSE%???}" # remove the last 3 characters to get the content
33+
PG_CONTENT="${PG_RESPONSE%???}" # remove the last 3 characters to get the content
3434

3535
if [ "$PG_HTTP_CODE" = "200" ] && [ -n "$PG_CONTENT" ]; then
36-
echo "feature.node.kubernetes.io/aws-placement-group=${PG_CONTENT}" >> "${PLACEMENT_GROUP_FEATURE_FILE}"
36+
echo "feature.node.kubernetes.io/aws-placement-group=${PG_CONTENT}" >>"${PLACEMENT_GROUP_FEATURE_FILE}"
3737
fi

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Copyright 2024 Nutanix. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
14
package placementgroupnfd
25

36
import (

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

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,22 @@ var _ = Describe("Generate AWS Placement Group NFD patches for ControlPlane", fu
4242
),
4343
},
4444
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-
}},
45+
ExpectedPatchMatchers: []capitest.JSONPatchMatcher{
46+
{
47+
Operation: "add",
48+
Path: "/spec/template/spec/kubeadmConfigSpec/files",
49+
ValueMatcher: gomega.ContainElement(gomega.HaveKeyWithValue(
50+
"path", "/etc/kubernetes/node-feature-discovery/source.d/placementgroup_discovery.sh",
51+
)),
52+
},
53+
{
54+
Operation: "add",
55+
Path: "/spec/template/spec/kubeadmConfigSpec/preKubeadmCommands",
56+
ValueMatcher: gomega.ContainElement(
57+
"/etc/kubernetes/node-feature-discovery/source.d/placementgroup_discovery.sh",
58+
),
59+
},
60+
},
5261
},
5362
}
5463

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Copyright 2024 Nutanix. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
14
package placementgroupnfd
25

36
import (

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

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,22 @@ var _ = Describe("Generate AWS Placement Group NFD patches for Worker", func() {
4545
),
4646
},
4747
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-
}},
48+
ExpectedPatchMatchers: []capitest.JSONPatchMatcher{
49+
{
50+
Operation: "add",
51+
Path: "/spec/template/spec/files",
52+
ValueMatcher: gomega.ContainElement(gomega.HaveKeyWithValue(
53+
"path", "/etc/kubernetes/node-feature-discovery/source.d/placementgroup_discovery.sh",
54+
)),
55+
},
56+
{
57+
Operation: "add",
58+
Path: "/spec/template/spec/preKubeadmCommands",
59+
ValueMatcher: gomega.ContainElement(
60+
"/etc/kubernetes/node-feature-discovery/source.d/placementgroup_discovery.sh",
61+
),
62+
},
63+
},
5564
},
5665
}
5766

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Copyright 2024 Nutanix. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
14
package placementgroupnfd
25

36
import (
@@ -7,6 +10,6 @@ import (
710
var (
811
//go:embed embedded/placementgroup_discovery.sh
912
PlacementgroupDiscoveryScript []byte
10-
13+
//nolint:lll // this is a constant with long file path
1114
PlacementGroupDiscoveryScriptFileOnRemote = "/etc/kubernetes/node-feature-discovery/source.d/placementgroup_discovery.sh"
1215
)
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, "EKS Placement Group NFD mutator suite")
16+
}

pkg/handlers/eks/mutation/placementgroupnfd/inject_worker.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Copyright 2024 Nutanix. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
14
package placementgroupnfd
25

36
import (
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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/pkg/handlers/eks/mutation/testutils"
15+
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/test/helpers"
16+
)
17+
18+
var _ = Describe("Generate EKS Placement Group NFD patches for Worker", func() {
19+
patchGenerator := func() mutation.GeneratePatches {
20+
return mutation.NewMetaGeneratePatchesHandler(
21+
"",
22+
helpers.TestEnv.Client,
23+
NewWorkerPatch(),
24+
).(mutation.GeneratePatches)
25+
}
26+
27+
testDefs := []capitest.PatchTestDef{
28+
{
29+
Name: "unset variable",
30+
},
31+
{
32+
Name: "placement group set for EKS workers",
33+
Vars: []runtimehooksv1.Variable{
34+
capitest.VariableWithValue(
35+
v1alpha1.WorkerConfigVariableName,
36+
v1alpha1.PlacementGroup{
37+
Name: "test-placement-group",
38+
},
39+
v1alpha1.EKSVariableName,
40+
"placementGroup",
41+
),
42+
capitest.VariableWithValue(
43+
runtimehooksv1.BuiltinsName,
44+
map[string]any{
45+
"machineDeployment": map[string]any{
46+
"class": "a-worker",
47+
},
48+
},
49+
),
50+
},
51+
RequestItem: testutils.NewNodeadmConfigTemplateRequestItem("1234"),
52+
ExpectedPatchMatchers: []capitest.JSONPatchMatcher{
53+
{
54+
Operation: "add",
55+
Path: "/spec/template/spec/files",
56+
ValueMatcher: gomega.ContainElement(gomega.HaveKeyWithValue(
57+
"path", "/etc/kubernetes/node-feature-discovery/source.d/placementgroup_discovery.sh",
58+
)),
59+
},
60+
{
61+
Operation: "add",
62+
Path: "/spec/template/spec/PreNodeadmCommands",
63+
ValueMatcher: gomega.ContainElement(
64+
"/etc/kubernetes/node-feature-discovery/source.d/placementgroup_discovery.sh",
65+
),
66+
},
67+
},
68+
},
69+
}
70+
71+
// create test node for each case
72+
for _, tt := range testDefs {
73+
It(tt.Name, func() {
74+
capitest.AssertGeneratePatches(
75+
GinkgoT(),
76+
patchGenerator,
77+
&tt,
78+
)
79+
})
80+
}
81+
})

0 commit comments

Comments
 (0)