Skip to content
This repository was 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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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