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

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

Merged
merged 8 commits into from
Apr 5, 2024
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,
supershal marked this conversation as resolved.
Show resolved Hide resolved
),
},
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
Loading