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

Commit d6a71f6

Browse files
committed
test: unit tests for AWS security groups
1 parent 6360797 commit d6a71f6

File tree

3 files changed

+172
-0
lines changed

3 files changed

+172
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Copyright 2023 D2iQ, Inc. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package securitygroups
5+
6+
import (
7+
. "github.com/onsi/ginkgo/v2"
8+
"github.com/onsi/gomega"
9+
"k8s.io/utils/ptr"
10+
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
11+
12+
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/api/v1alpha1"
13+
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/handlers/mutation"
14+
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/common/pkg/testutils/capitest"
15+
"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/clusterconfig"
17+
)
18+
19+
var _ = Describe("Generate SecurityGroup patches for ControlPlane", func() {
20+
patchGenerator := func() mutation.GeneratePatches {
21+
return mutation.NewMetaGeneratePatchesHandler("", NewControlPlanePatch()).(mutation.GeneratePatches)
22+
}
23+
24+
testDefs := []capitest.PatchTestDef{
25+
{
26+
Name: "unset variable",
27+
},
28+
{
29+
Name: "SecurityGroups for controlplane set",
30+
Vars: []runtimehooksv1.Variable{
31+
capitest.VariableWithValue(
32+
clusterconfig.MetaVariableName,
33+
v1alpha1.AdditionalSecurityGroup{
34+
{ID: ptr.To("sg-1")},
35+
{ID: ptr.To("sg-2")},
36+
{ID: ptr.To("sg-3")},
37+
},
38+
clusterconfig.MetaControlPlaneConfigName,
39+
v1alpha1.AWSVariableName,
40+
VariableName,
41+
),
42+
},
43+
RequestItem: request.NewCPAWSMachineTemplateRequestItem("1234"),
44+
ExpectedPatchMatchers: []capitest.JSONPatchMatcher{
45+
{
46+
Operation: "add",
47+
Path: "/spec/template/spec/additionalSecurityGroups",
48+
ValueMatcher: gomega.HaveLen(3),
49+
},
50+
// TODO(shalinpatel): add matcher to check if all SG are set
51+
// {
52+
// Operation: "add",
53+
// Path: "/spec/template/spec/additionalSecurityGroups",
54+
// ValueMatcher: gomega.ContainElements(
55+
// gomega.HaveKeyWithValue("id", "sg-1"),
56+
// gomega.HaveKeyWithValue("id", "sg-2"),
57+
// gomega.HaveKeyWithValue("id", "sg-3"),
58+
// ),
59+
// },
60+
},
61+
},
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+
})
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 securitygroups
5+
6+
import (
7+
"testing"
8+
9+
. "github.com/onsi/ginkgo/v2"
10+
. "github.com/onsi/gomega"
11+
)
12+
13+
func TestSecurityGroupsPatch(t *testing.T) {
14+
RegisterFailHandler(Fail)
15+
RunSpecs(t, "AWS security groups patches for ControlPlane and Workers suite")
16+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// Copyright 2023 D2iQ, Inc. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package securitygroups
5+
6+
import (
7+
. "github.com/onsi/ginkgo/v2"
8+
"github.com/onsi/gomega"
9+
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
10+
"k8s.io/utils/ptr"
11+
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
12+
13+
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/api/v1alpha1"
14+
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/handlers/mutation"
15+
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/common/pkg/testutils/capitest"
16+
"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/workerconfig"
18+
)
19+
20+
var _ = Describe("Generate AWS SecurityGroups patches for Worker", func() {
21+
patchGenerator := func() mutation.GeneratePatches {
22+
return mutation.NewMetaGeneratePatchesHandler("", NewWorkerPatch()).(mutation.GeneratePatches)
23+
}
24+
25+
testDefs := []capitest.PatchTestDef{
26+
{
27+
Name: "unset variable",
28+
},
29+
{
30+
Name: "SecurityGroups for workers set",
31+
Vars: []runtimehooksv1.Variable{
32+
capitest.VariableWithValue(
33+
workerconfig.MetaVariableName,
34+
v1alpha1.AdditionalSecurityGroup{
35+
{ID: ptr.To("sg-1")},
36+
{ID: ptr.To("sg-2")},
37+
{ID: ptr.To("sg-3")},
38+
},
39+
v1alpha1.AWSVariableName,
40+
VariableName,
41+
),
42+
capitest.VariableWithValue(
43+
"builtin",
44+
apiextensionsv1.JSON{
45+
Raw: []byte(`{"machineDeployment": {"class": "a-worker"}}`),
46+
},
47+
),
48+
},
49+
RequestItem: request.NewWorkerAWSMachineTemplateRequestItem("1234"),
50+
ExpectedPatchMatchers: []capitest.JSONPatchMatcher{
51+
{
52+
Operation: "add",
53+
Path: "/spec/template/spec/additionalSecurityGroups",
54+
ValueMatcher: gomega.HaveLen(3),
55+
},
56+
// TODO(shalinpatel): add matcher to check if all SG are set
57+
// {
58+
// Operation: "add",
59+
// Path: "/spec/template/spec/additionalSecurityGroups",
60+
// ValueMatcher: gomega.ContainElements(
61+
// gomega.HaveKeyWithValue("id", "sg-1"),
62+
// gomega.HaveKeyWithValue("id", "sg-2"),
63+
// gomega.HaveKeyWithValue("id", "sg-3"),
64+
// ),
65+
// },
66+
},
67+
},
68+
}
69+
70+
// create test node for each case
71+
for testIdx := range testDefs {
72+
tt := testDefs[testIdx]
73+
It(tt.Name, func() {
74+
capitest.AssertGeneratePatches(
75+
GinkgoT(),
76+
patchGenerator,
77+
&tt,
78+
)
79+
})
80+
}
81+
})

0 commit comments

Comments
 (0)