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

Commit

Permalink
test: unit tests for AWS security groups
Browse files Browse the repository at this point in the history
  • Loading branch information
supershal committed Apr 5, 2024
1 parent 4f1a4a1 commit 42d1feb
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 0 deletions.
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 pkg/handlers/aws/mutation/securitygroups/inject_suite_test.go
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 pkg/handlers/aws/mutation/securitygroups/inject_worker_test.go
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,
)
})
}
})

0 comments on commit 42d1feb

Please sign in to comment.