This repository has been archived by the owner on Apr 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: unit tests for AWS security groups
- Loading branch information
Showing
3 changed files
with
172 additions
and
0 deletions.
There are no files selected for viewing
75 changes: 75 additions & 0 deletions
75
pkg/handlers/aws/mutation/securitygroups/inject_control_plane_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// Copyright 2023 D2iQ, Inc. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package securitygroups | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo/v2" | ||
"github.com/onsi/gomega" | ||
"k8s.io/utils/ptr" | ||
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1" | ||
|
||
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/api/v1alpha1" | ||
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/handlers/mutation" | ||
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/common/pkg/testutils/capitest" | ||
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/common/pkg/testutils/capitest/request" | ||
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/clusterconfig" | ||
) | ||
|
||
var _ = Describe("Generate SecurityGroup patches for ControlPlane", func() { | ||
patchGenerator := func() mutation.GeneratePatches { | ||
return mutation.NewMetaGeneratePatchesHandler("", NewControlPlanePatch()).(mutation.GeneratePatches) | ||
} | ||
|
||
testDefs := []capitest.PatchTestDef{ | ||
{ | ||
Name: "unset variable", | ||
}, | ||
{ | ||
Name: "SecurityGroups for controlplane set", | ||
Vars: []runtimehooksv1.Variable{ | ||
capitest.VariableWithValue( | ||
clusterconfig.MetaVariableName, | ||
v1alpha1.AdditionalSecurityGroup{ | ||
{ID: ptr.To("sg-1")}, | ||
{ID: ptr.To("sg-2")}, | ||
{ID: ptr.To("sg-3")}, | ||
}, | ||
clusterconfig.MetaControlPlaneConfigName, | ||
v1alpha1.AWSVariableName, | ||
VariableName, | ||
), | ||
}, | ||
RequestItem: request.NewCPAWSMachineTemplateRequestItem("1234"), | ||
ExpectedPatchMatchers: []capitest.JSONPatchMatcher{ | ||
{ | ||
Operation: "add", | ||
Path: "/spec/template/spec/additionalSecurityGroups", | ||
ValueMatcher: gomega.HaveLen(3), | ||
}, | ||
// TODO(shalinpatel): add matcher to check if all SG are set | ||
// { | ||
// Operation: "add", | ||
// Path: "/spec/template/spec/additionalSecurityGroups", | ||
// ValueMatcher: gomega.ContainElements( | ||
// gomega.HaveKeyWithValue("id", "sg-1"), | ||
// gomega.HaveKeyWithValue("id", "sg-2"), | ||
// gomega.HaveKeyWithValue("id", "sg-3"), | ||
// ), | ||
// }, | ||
}, | ||
}, | ||
} | ||
|
||
// create test node for each case | ||
for testIdx := range testDefs { | ||
tt := testDefs[testIdx] | ||
It(tt.Name, func() { | ||
capitest.AssertGeneratePatches( | ||
GinkgoT(), | ||
patchGenerator, | ||
&tt, | ||
) | ||
}) | ||
} | ||
}) |
16 changes: 16 additions & 0 deletions
16
pkg/handlers/aws/mutation/securitygroups/inject_suite_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Copyright 2023 D2iQ, Inc. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package securitygroups | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestSecurityGroupsPatch(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "AWS security groups patches for ControlPlane and Workers suite") | ||
} |
81 changes: 81 additions & 0 deletions
81
pkg/handlers/aws/mutation/securitygroups/inject_worker_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// Copyright 2023 D2iQ, Inc. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package securitygroups | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo/v2" | ||
"github.com/onsi/gomega" | ||
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" | ||
"k8s.io/utils/ptr" | ||
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1" | ||
|
||
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/api/v1alpha1" | ||
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/handlers/mutation" | ||
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/common/pkg/testutils/capitest" | ||
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/common/pkg/testutils/capitest/request" | ||
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/workerconfig" | ||
) | ||
|
||
var _ = Describe("Generate AWS SecurityGroups patches for Worker", func() { | ||
patchGenerator := func() mutation.GeneratePatches { | ||
return mutation.NewMetaGeneratePatchesHandler("", NewWorkerPatch()).(mutation.GeneratePatches) | ||
} | ||
|
||
testDefs := []capitest.PatchTestDef{ | ||
{ | ||
Name: "unset variable", | ||
}, | ||
{ | ||
Name: "SecurityGroups for workers set", | ||
Vars: []runtimehooksv1.Variable{ | ||
capitest.VariableWithValue( | ||
workerconfig.MetaVariableName, | ||
v1alpha1.AdditionalSecurityGroup{ | ||
{ID: ptr.To("sg-1")}, | ||
{ID: ptr.To("sg-2")}, | ||
{ID: ptr.To("sg-3")}, | ||
}, | ||
v1alpha1.AWSVariableName, | ||
VariableName, | ||
), | ||
capitest.VariableWithValue( | ||
"builtin", | ||
apiextensionsv1.JSON{ | ||
Raw: []byte(`{"machineDeployment": {"class": "a-worker"}}`), | ||
}, | ||
), | ||
}, | ||
RequestItem: request.NewWorkerAWSMachineTemplateRequestItem("1234"), | ||
ExpectedPatchMatchers: []capitest.JSONPatchMatcher{ | ||
{ | ||
Operation: "add", | ||
Path: "/spec/template/spec/additionalSecurityGroups", | ||
ValueMatcher: gomega.HaveLen(3), | ||
}, | ||
// TODO(shalinpatel): add matcher to check if all SG are set | ||
// { | ||
// Operation: "add", | ||
// Path: "/spec/template/spec/additionalSecurityGroups", | ||
// ValueMatcher: gomega.ContainElements( | ||
// gomega.HaveKeyWithValue("id", "sg-1"), | ||
// gomega.HaveKeyWithValue("id", "sg-2"), | ||
// gomega.HaveKeyWithValue("id", "sg-3"), | ||
// ), | ||
// }, | ||
}, | ||
}, | ||
} | ||
|
||
// create test node for each case | ||
for testIdx := range testDefs { | ||
tt := testDefs[testIdx] | ||
It(tt.Name, func() { | ||
capitest.AssertGeneratePatches( | ||
GinkgoT(), | ||
patchGenerator, | ||
&tt, | ||
) | ||
}) | ||
} | ||
}) |