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

Commit

Permalink
test: move all patch generator unit tests to their own packages (#23)
Browse files Browse the repository at this point in the history
* test: unit test for individual patch generator

* test: package level unit test for HTTPProxy

* test: move region and httpproxy patch generator unit test invocation

* fix: linting errors

* test: move all AWS patch unit tests to their own packages (#24)

* test: move instanceprofile tests to its own package

* test: move instancetype unit tests to its own package

* test: move ami unit tests to its own package

* test: move aws network tests to its own package

* test: move controlplaneloadbalancer unit tests to its own package

* test: move aws cni unit tests to its own package

* test: fix linting errors

* test: unit tests for AWS security groups

* test: move customimage unit tests to their own package (#30)

* test:  move all Nutanix patch handler unit tests (#32)

* test: move controlplane endpoint unit tests

* test: move PC endpoint unit tests

* test: nove machinedetails unit tests

* test: move generic patch unit tests to own packages (#31)

* test: move audit policy tests to their own package

* test: move etcd unit tests to their own package

* test: move extra api server cert sans to its own package

* test: move image registry unit tests to its own package

* test: move kubernetes image repository unit tests

* test: move mirror unit tests

* test: move users unit tests

* test: remove gereric unit tests from nutanix meta patch handler

* test: cleaned up meta level unit test suites
  • Loading branch information
supershal authored and jimmidyson committed Apr 11, 2024
1 parent 5e7fe95 commit c6cdfba
Show file tree
Hide file tree
Showing 46 changed files with 1,775 additions and 1,791 deletions.
46 changes: 46 additions & 0 deletions common/pkg/testutils/capitest/patches.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,52 @@ func ValidateGeneratePatches[T mutation.GeneratePatches](
DescribeTable("Patches", testFunc, testArgs)
}

// TODO(shalinpatel): AssertGeneratePatches is a replacement of ValidateGeneratePatches function.
// remove ValidateGeneratePatches once all the shared test cases are moved to their own patch package.
func AssertGeneratePatches[T mutation.GeneratePatches](
t GinkgoTInterface,
handlerCreator func() T,
tt *PatchTestDef,
) {
t.Helper()

g := gomega.NewWithT(t)
h := handlerCreator()
req := &runtimehooksv1.GeneratePatchesRequest{
Variables: tt.Vars,
Items: []runtimehooksv1.GeneratePatchesRequestItem{
tt.RequestItem,
{
HolderReference: runtimehooksv1.HolderReference{
APIVersion: capiv1.GroupVersion.String(),
Kind: "Cluster",
Namespace: request.Namespace,
Name: request.ClusterName,
},
},
},
}
resp := &runtimehooksv1.GeneratePatchesResponse{}
h.GeneratePatches(context.Background(), req, resp)
expectedStatus := runtimehooksv1.ResponseStatusSuccess
if tt.ExpectedFailure {
expectedStatus = runtimehooksv1.ResponseStatusFailure
}
g.Expect(resp.Status).
To(gomega.Equal(expectedStatus), fmt.Sprintf("Message: %s", resp.Message))

if len(tt.ExpectedPatchMatchers) == 0 {
g.Expect(resp.Items).To(gomega.BeEmpty())
return
}
g.Expect(resp.Items).To(containPatches(&tt.RequestItem, tt.ExpectedPatchMatchers...))

if len(tt.UnexpectedPatchMatchers) > 0 {
g.Expect(resp.Items).
ToNot(containPatches(&tt.RequestItem, tt.UnexpectedPatchMatchers...))
}
}

// VariableWithValue returns a runtimehooksv1.Variable with the passed name and value.
func VariableWithValue(
variableName string,
Expand Down
100 changes: 100 additions & 0 deletions pkg/handlers/aws/mutation/ami/inject_control_plane_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// Copyright 2023 D2iQ, Inc. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package ami

import (
. "github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
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 AMI patches for ControlPlane", func() {
patchGenerator := func() mutation.GeneratePatches {
return mutation.NewMetaGeneratePatchesHandler("", NewControlPlanePatch()).(mutation.GeneratePatches)
}

testDefs := []capitest.PatchTestDef{
{
Name: "AMI set for control plane",
Vars: []runtimehooksv1.Variable{
capitest.VariableWithValue(
clusterconfig.MetaVariableName,
v1alpha1.AMISpec{ID: "ami-controlplane"},
clusterconfig.MetaControlPlaneConfigName,
v1alpha1.AWSVariableName,
VariableName,
),
},
RequestItem: request.NewCPAWSMachineTemplateRequestItem("1234"),
ExpectedPatchMatchers: []capitest.JSONPatchMatcher{
{
Operation: "add",
Path: "/spec/template/spec/ami/id",
ValueMatcher: gomega.Equal("ami-controlplane"),
},
},
},
{
Name: "AMI lookup format set for control plane",
Vars: []runtimehooksv1.Variable{
capitest.VariableWithValue(
clusterconfig.MetaVariableName,
v1alpha1.AMISpec{
Lookup: &v1alpha1.AMILookup{
Format: "test-{{.kubernetesVersion}}-format",
Org: "1234",
BaseOS: "testOS",
},
},
clusterconfig.MetaControlPlaneConfigName,
v1alpha1.AWSVariableName,
VariableName,
),
},
RequestItem: request.NewCPAWSMachineTemplateRequestItem("1234"),
ExpectedPatchMatchers: []capitest.JSONPatchMatcher{
{
Operation: "add",
Path: "/spec/template/spec/imageLookupFormat",
ValueMatcher: gomega.Equal("test-{{.kubernetesVersion}}-format"),
},
{
Operation: "add",
Path: "/spec/template/spec/imageLookupOrg",
ValueMatcher: gomega.Equal("1234"),
},
{
Operation: "add",
Path: "/spec/template/spec/imageLookupBaseOS",
ValueMatcher: gomega.Equal("testOS"),
},
},
UnexpectedPatchMatchers: []capitest.JSONPatchMatcher{
{
Operation: "add",
Path: "/spec/template/spec/ami/id",
ValueMatcher: gomega.Equal(""),
},
},
},
}

// 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/ami/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 ami

import (
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

func TestAMIPatch(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "AMI patches for ControlPlane and Workers suite")
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
// Copyright 2023 D2iQ, Inc. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package tests
package ami

import (
"testing"

. "github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
Expand All @@ -14,99 +13,23 @@ import (
"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"
)

func TestControlPlaneGeneratePatches(
t *testing.T,
generatorFunc func() mutation.GeneratePatches,
variableName string,
variablePath ...string,
) {
t.Helper()

capitest.ValidateGeneratePatches(
t,
generatorFunc,
capitest.PatchTestDef{
Name: "AMI set for control plane",
Vars: []runtimehooksv1.Variable{
capitest.VariableWithValue(
variableName,
v1alpha1.AMISpec{ID: "ami-controlplane"},
variablePath...,
),
},
RequestItem: request.NewCPAWSMachineTemplateRequestItem("1234"),
ExpectedPatchMatchers: []capitest.JSONPatchMatcher{
{
Operation: "add",
Path: "/spec/template/spec/ami/id",
ValueMatcher: gomega.Equal("ami-controlplane"),
},
},
},
capitest.PatchTestDef{
Name: "AMI lookup format set for control plane",
Vars: []runtimehooksv1.Variable{
capitest.VariableWithValue(
variableName,
v1alpha1.AMISpec{
Lookup: &v1alpha1.AMILookup{
Format: "test-{{.kubernetesVersion}}-format",
Org: "12345",
BaseOS: "testOS",
},
},
variablePath...,
),
},
RequestItem: request.NewCPAWSMachineTemplateRequestItem("1234"),
ExpectedPatchMatchers: []capitest.JSONPatchMatcher{
{
Operation: "add",
Path: "/spec/template/spec/imageLookupFormat",
ValueMatcher: gomega.Equal("test-{{.kubernetesVersion}}-format"),
},
{
Operation: "add",
Path: "/spec/template/spec/imageLookupOrg",
ValueMatcher: gomega.Equal("12345"),
},
{
Operation: "add",
Path: "/spec/template/spec/imageLookupBaseOS",
ValueMatcher: gomega.Equal("testOS"),
},
},
UnexpectedPatchMatchers: []capitest.JSONPatchMatcher{
{
Operation: "add",
Path: "/spec/template/spec/ami/id",
ValueMatcher: gomega.Equal(""),
},
},
},
)
}

func TestWorkerGeneratePatches(
t *testing.T,
generatorFunc func() mutation.GeneratePatches,
variableName string,
variablePath ...string,
) {
t.Helper()
var _ = Describe("Generate AMI patches for Worker", func() {
patchGenerator := func() mutation.GeneratePatches {
return mutation.NewMetaGeneratePatchesHandler("", NewWorkerPatch()).(mutation.GeneratePatches)
}

capitest.ValidateGeneratePatches(
t,
generatorFunc,
capitest.PatchTestDef{
testDefs := []capitest.PatchTestDef{
{
Name: "AMI set for workers",
Vars: []runtimehooksv1.Variable{
capitest.VariableWithValue(
variableName,
workerconfig.MetaVariableName,
v1alpha1.AMISpec{ID: "ami-controlplane"},
variablePath...,
v1alpha1.AWSVariableName,
VariableName,
),
capitest.VariableWithValue(
"builtin",
Expand All @@ -124,20 +47,20 @@ func TestWorkerGeneratePatches(
},
},
},
capitest.PatchTestDef{
{
Name: "AMI lookup format set for worker",
Vars: []runtimehooksv1.Variable{
capitest.VariableWithValue(
variableName,
workerconfig.MetaVariableName,
v1alpha1.AMISpec{
Lookup: &v1alpha1.AMILookup{
Format: "test-{{.kubernetesVersion}}-format",
Org: "12345",
BaseOS: "testOS",
},
},

variablePath...,
v1alpha1.AWSVariableName,
VariableName,
),
capitest.VariableWithValue(
"builtin",
Expand Down Expand Up @@ -172,5 +95,17 @@ func TestWorkerGeneratePatches(
},
},
},
)
}
}

// create test node for each case
for testIdx := range testDefs {
tt := testDefs[testIdx]
It(tt.Name, func() {
capitest.AssertGeneratePatches(
GinkgoT(),
patchGenerator,
&tt,
)
})
}
})
Loading

0 comments on commit c6cdfba

Please sign in to comment.