From 71d92a7a4053ef2f37fde6b00cab7f35058d9e8e Mon Sep 17 00:00:00 2001 From: Shalin Patel Date: Wed, 3 Apr 2024 12:28:07 -0700 Subject: [PATCH 1/8] test: move instanceprofile tests to its own package --- .../inject_control_plane_test.go | 58 +++++++++++++ .../iaminstanceprofile/inject_suite_test.go | 13 +++ ...erate_patches.go => inject_worker_test.go} | 83 +++++++------------ .../aws/mutation/metapatch_handler_test.go | 20 +---- 4 files changed, 102 insertions(+), 72 deletions(-) create mode 100644 pkg/handlers/aws/mutation/iaminstanceprofile/inject_control_plane_test.go create mode 100644 pkg/handlers/aws/mutation/iaminstanceprofile/inject_suite_test.go rename pkg/handlers/aws/mutation/iaminstanceprofile/{tests/generate_patches.go => inject_worker_test.go} (50%) diff --git a/pkg/handlers/aws/mutation/iaminstanceprofile/inject_control_plane_test.go b/pkg/handlers/aws/mutation/iaminstanceprofile/inject_control_plane_test.go new file mode 100644 index 000000000..11d7b0ed9 --- /dev/null +++ b/pkg/handlers/aws/mutation/iaminstanceprofile/inject_control_plane_test.go @@ -0,0 +1,58 @@ +// Copyright 2023 D2iQ, Inc. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +package iaminstanceprofile + +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 IAMInstanceProfile patches for ControlPlane", func() { + patchGenerator := func() mutation.GeneratePatches { + return mutation.NewMetaGeneratePatchesHandler("", NewControlPlanePatch()).(mutation.GeneratePatches) + } + + testDefs := []capitest.PatchTestDef{ + { + Name: "unset variable", + }, + { + Name: "iamInstanceProfile for control plane set", + Vars: []runtimehooksv1.Variable{ + capitest.VariableWithValue( + clusterconfig.MetaVariableName, + "control-plane.cluster-api-provider-aws.sigs.k8s.io", + clusterconfig.MetaControlPlaneConfigName, + v1alpha1.AWSVariableName, + VariableName, + ), + }, + RequestItem: request.NewCPAWSMachineTemplateRequestItem("1234"), + ExpectedPatchMatchers: []capitest.JSONPatchMatcher{{ + Operation: "add", + Path: "/spec/template/spec/iamInstanceProfile", + ValueMatcher: gomega.Equal("control-plane.cluster-api-provider-aws.sigs.k8s.io"), + }}, + }, + } + + // create test node for each case + for testIdx := range testDefs { + tt := testDefs[testIdx] + It(tt.Name, func() { + capitest.AssertGeneratePatches( + GinkgoT(), + patchGenerator, + &tt, + ) + }) + } +}) diff --git a/pkg/handlers/aws/mutation/iaminstanceprofile/inject_suite_test.go b/pkg/handlers/aws/mutation/iaminstanceprofile/inject_suite_test.go new file mode 100644 index 000000000..0617978da --- /dev/null +++ b/pkg/handlers/aws/mutation/iaminstanceprofile/inject_suite_test.go @@ -0,0 +1,13 @@ +package iaminstanceprofile + +import ( + "testing" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" +) + +func TestIAMInstnaceProfilePatch(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "IAMInstanceProfile patches for ControlPlane and Workers suite") +} diff --git a/pkg/handlers/aws/mutation/iaminstanceprofile/tests/generate_patches.go b/pkg/handlers/aws/mutation/iaminstanceprofile/inject_worker_test.go similarity index 50% rename from pkg/handlers/aws/mutation/iaminstanceprofile/tests/generate_patches.go rename to pkg/handlers/aws/mutation/iaminstanceprofile/inject_worker_test.go index 6f36d196f..bcd8bdcd9 100644 --- a/pkg/handlers/aws/mutation/iaminstanceprofile/tests/generate_patches.go +++ b/pkg/handlers/aws/mutation/iaminstanceprofile/inject_worker_test.go @@ -1,74 +1,39 @@ // Copyright 2023 D2iQ, Inc. All rights reserved. // SPDX-License-Identifier: Apache-2.0 -package tests +package iaminstanceprofile 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" + "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" ) -func TestControlPlaneGeneratePatches( - t *testing.T, - generatorFunc func() mutation.GeneratePatches, - variableName string, - variablePath ...string, -) { - t.Helper() - - capitest.ValidateGeneratePatches( - t, - generatorFunc, - capitest.PatchTestDef{ - Name: "unset variable", - }, - capitest.PatchTestDef{ - Name: "iamInstanceProfile set", - Vars: []runtimehooksv1.Variable{ - capitest.VariableWithValue( - variableName, - "control-plane.cluster-api-provider-aws.sigs.k8s.io", - variablePath..., - ), - }, - RequestItem: request.NewCPAWSMachineTemplateRequestItem("1234"), - ExpectedPatchMatchers: []capitest.JSONPatchMatcher{{ - Operation: "add", - Path: "/spec/template/spec/iamInstanceProfile", - ValueMatcher: gomega.Equal("control-plane.cluster-api-provider-aws.sigs.k8s.io"), - }}, - }, - ) -} - -func TestWorkerGeneratePatches( - t *testing.T, - generatorFunc func() mutation.GeneratePatches, - variableName string, - variablePath ...string, -) { - t.Helper() +var _ = Describe("Generate IAMInstanceProfile patches for Worker", func() { + patchGenerator := func() mutation.GeneratePatches { + return mutation.NewMetaGeneratePatchesHandler("", NewWorkerPatch()).(mutation.GeneratePatches) + } - capitest.ValidateGeneratePatches( - t, - generatorFunc, - capitest.PatchTestDef{ + testDefs := []capitest.PatchTestDef{ + { Name: "unset variable", }, - capitest.PatchTestDef{ - Name: "iamInstanceProfile set", + { + Name: "iamInstanceProfile for worker set", Vars: []runtimehooksv1.Variable{ capitest.VariableWithValue( - variableName, + workerconfig.MetaVariableName, "nodes.cluster-api-provider-aws.sigs.k8s.io", - variablePath..., + v1alpha1.AWSVariableName, + VariableName, ), capitest.VariableWithValue( "builtin", @@ -84,5 +49,17 @@ func TestWorkerGeneratePatches( ValueMatcher: gomega.Equal("nodes.cluster-api-provider-aws.sigs.k8s.io"), }}, }, - ) -} + } + + // create test node for each case + for testIdx := range testDefs { + tt := testDefs[testIdx] + It(tt.Name, func() { + capitest.AssertGeneratePatches( + GinkgoT(), + patchGenerator, + &tt, + ) + }) + } +}) diff --git a/pkg/handlers/aws/mutation/metapatch_handler_test.go b/pkg/handlers/aws/mutation/metapatch_handler_test.go index 7f2aaa53a..b1eb6ff49 100644 --- a/pkg/handlers/aws/mutation/metapatch_handler_test.go +++ b/pkg/handlers/aws/mutation/metapatch_handler_test.go @@ -15,8 +15,7 @@ import ( calicotests "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/aws/mutation/cni/calico/tests" "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/aws/mutation/controlplaneloadbalancer" controlplaneloadbalancertests "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/aws/mutation/controlplaneloadbalancer/tests" - "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/aws/mutation/iaminstanceprofile" - iaminstanceprofiletests "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/aws/mutation/iaminstanceprofile/tests" + "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/aws/mutation/instancetype" instancetypetests "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/aws/mutation/instancetype/tests" "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/aws/mutation/network" @@ -55,23 +54,6 @@ func TestGeneratePatches(t *testing.T) { mgr := testEnv.Manager - iaminstanceprofiletests.TestControlPlaneGeneratePatches( - t, - metaPatchGeneratorFunc(mgr), - clusterconfig.MetaVariableName, - clusterconfig.MetaControlPlaneConfigName, - v1alpha1.AWSVariableName, - iaminstanceprofile.VariableName, - ) - - iaminstanceprofiletests.TestWorkerGeneratePatches( - t, - workerPatchGeneratorFunc(), - workerconfig.MetaVariableName, - v1alpha1.AWSVariableName, - iaminstanceprofile.VariableName, - ) - instancetypetests.TestControlPlaneGeneratePatches( t, metaPatchGeneratorFunc(mgr), From 78b9214819cdf95771d2c1c6e635ab119a8e830b Mon Sep 17 00:00:00 2001 From: Shalin Patel Date: Wed, 3 Apr 2024 12:38:01 -0700 Subject: [PATCH 2/8] test: move instancetype unit tests to its own package --- .../instancetype/inject_control_plane_test.go | 58 +++++++++++++ .../instancetype/inject_suite_test.go | 13 +++ ...erate_patches.go => inject_worker_test.go} | 83 +++++++------------ .../aws/mutation/metapatch_handler_test.go | 19 ----- 4 files changed, 101 insertions(+), 72 deletions(-) create mode 100644 pkg/handlers/aws/mutation/instancetype/inject_control_plane_test.go create mode 100644 pkg/handlers/aws/mutation/instancetype/inject_suite_test.go rename pkg/handlers/aws/mutation/instancetype/{tests/generate_patches.go => inject_worker_test.go} (50%) diff --git a/pkg/handlers/aws/mutation/instancetype/inject_control_plane_test.go b/pkg/handlers/aws/mutation/instancetype/inject_control_plane_test.go new file mode 100644 index 000000000..e13189a47 --- /dev/null +++ b/pkg/handlers/aws/mutation/instancetype/inject_control_plane_test.go @@ -0,0 +1,58 @@ +// Copyright 2023 D2iQ, Inc. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +package instancetype + +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 InstanceType patches for ControlPlane", func() { + patchGenerator := func() mutation.GeneratePatches { + return mutation.NewMetaGeneratePatchesHandler("", NewControlPlanePatch()).(mutation.GeneratePatches) + } + + testDefs := []capitest.PatchTestDef{ + { + Name: "unset variable", + }, + { + Name: "instanceType for controlplane set", + Vars: []runtimehooksv1.Variable{ + capitest.VariableWithValue( + clusterconfig.MetaVariableName, + "m5.xlarge", + clusterconfig.MetaControlPlaneConfigName, + v1alpha1.AWSVariableName, + VariableName, + ), + }, + RequestItem: request.NewCPAWSMachineTemplateRequestItem("1234"), + ExpectedPatchMatchers: []capitest.JSONPatchMatcher{{ + Operation: "replace", + Path: "/spec/template/spec/instanceType", + ValueMatcher: gomega.Equal("m5.xlarge"), + }}, + }, + } + + // create test node for each case + for testIdx := range testDefs { + tt := testDefs[testIdx] + It(tt.Name, func() { + capitest.AssertGeneratePatches( + GinkgoT(), + patchGenerator, + &tt, + ) + }) + } +}) diff --git a/pkg/handlers/aws/mutation/instancetype/inject_suite_test.go b/pkg/handlers/aws/mutation/instancetype/inject_suite_test.go new file mode 100644 index 000000000..87f8febd5 --- /dev/null +++ b/pkg/handlers/aws/mutation/instancetype/inject_suite_test.go @@ -0,0 +1,13 @@ +package instancetype + +import ( + "testing" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" +) + +func TestInstanceTypePatch(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "InstanceType patches for ControlPlane and Workers suite") +} diff --git a/pkg/handlers/aws/mutation/instancetype/tests/generate_patches.go b/pkg/handlers/aws/mutation/instancetype/inject_worker_test.go similarity index 50% rename from pkg/handlers/aws/mutation/instancetype/tests/generate_patches.go rename to pkg/handlers/aws/mutation/instancetype/inject_worker_test.go index ef9959535..da73dd919 100644 --- a/pkg/handlers/aws/mutation/instancetype/tests/generate_patches.go +++ b/pkg/handlers/aws/mutation/instancetype/inject_worker_test.go @@ -1,74 +1,39 @@ // Copyright 2023 D2iQ, Inc. All rights reserved. // SPDX-License-Identifier: Apache-2.0 -package tests +package instancetype 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" + "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" ) -func TestControlPlaneGeneratePatches( - t *testing.T, - generatorFunc func() mutation.GeneratePatches, - variableName string, - variablePath ...string, -) { - t.Helper() - - capitest.ValidateGeneratePatches( - t, - generatorFunc, - capitest.PatchTestDef{ - Name: "unset variable", - }, - capitest.PatchTestDef{ - Name: "instanceType set", - Vars: []runtimehooksv1.Variable{ - capitest.VariableWithValue( - variableName, - "m5.xlarge", - variablePath..., - ), - }, - RequestItem: request.NewCPAWSMachineTemplateRequestItem("1234"), - ExpectedPatchMatchers: []capitest.JSONPatchMatcher{{ - Operation: "replace", - Path: "/spec/template/spec/instanceType", - ValueMatcher: gomega.Equal("m5.xlarge"), - }}, - }, - ) -} - -func TestWorkerGeneratePatches( - t *testing.T, - generatorFunc func() mutation.GeneratePatches, - variableName string, - variablePath ...string, -) { - t.Helper() +var _ = Describe("Generate InstanceType patches for Worker", func() { + patchGenerator := func() mutation.GeneratePatches { + return mutation.NewMetaGeneratePatchesHandler("", NewWorkerPatch()).(mutation.GeneratePatches) + } - capitest.ValidateGeneratePatches( - t, - generatorFunc, - capitest.PatchTestDef{ + testDefs := []capitest.PatchTestDef{ + { Name: "unset variable", }, - capitest.PatchTestDef{ - Name: "instanceType set", + { + Name: "instanceType for workers set", Vars: []runtimehooksv1.Variable{ capitest.VariableWithValue( - variableName, + workerconfig.MetaVariableName, "m5.2xlarge", - variablePath..., + v1alpha1.AWSVariableName, + VariableName, ), capitest.VariableWithValue( "builtin", @@ -84,5 +49,17 @@ func TestWorkerGeneratePatches( ValueMatcher: gomega.Equal("m5.2xlarge"), }}, }, - ) -} + } + + // create test node for each case + for testIdx := range testDefs { + tt := testDefs[testIdx] + It(tt.Name, func() { + capitest.AssertGeneratePatches( + GinkgoT(), + patchGenerator, + &tt, + ) + }) + } +}) diff --git a/pkg/handlers/aws/mutation/metapatch_handler_test.go b/pkg/handlers/aws/mutation/metapatch_handler_test.go index b1eb6ff49..c23db4a6f 100644 --- a/pkg/handlers/aws/mutation/metapatch_handler_test.go +++ b/pkg/handlers/aws/mutation/metapatch_handler_test.go @@ -16,8 +16,6 @@ import ( "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/aws/mutation/controlplaneloadbalancer" controlplaneloadbalancertests "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/aws/mutation/controlplaneloadbalancer/tests" - "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/aws/mutation/instancetype" - instancetypetests "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/aws/mutation/instancetype/tests" "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/aws/mutation/network" networktests "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/aws/mutation/network/tests" "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/clusterconfig" @@ -54,23 +52,6 @@ func TestGeneratePatches(t *testing.T) { mgr := testEnv.Manager - instancetypetests.TestControlPlaneGeneratePatches( - t, - metaPatchGeneratorFunc(mgr), - clusterconfig.MetaVariableName, - clusterconfig.MetaControlPlaneConfigName, - v1alpha1.AWSVariableName, - instancetype.VariableName, - ) - - instancetypetests.TestWorkerGeneratePatches( - t, - workerPatchGeneratorFunc(), - workerconfig.MetaVariableName, - v1alpha1.AWSVariableName, - instancetype.VariableName, - ) - calicotests.TestGeneratePatches( t, metaPatchGeneratorFunc(mgr), From de13650318a73b0730e35478c3fdacade95cd879 Mon Sep 17 00:00:00 2001 From: Shalin Patel Date: Wed, 3 Apr 2024 12:59:12 -0700 Subject: [PATCH 3/8] test: move ami unit tests to its own package --- .../mutation/ami/inject_control_plane_test.go | 100 ++++++++++++++ .../aws/mutation/ami/inject_suite_test.go | 13 ++ ...erate_patches.go => inject_worker_test.go} | 126 +++++------------- .../aws/mutation/metapatch_handler_test.go | 31 +---- 4 files changed, 145 insertions(+), 125 deletions(-) create mode 100644 pkg/handlers/aws/mutation/ami/inject_control_plane_test.go create mode 100644 pkg/handlers/aws/mutation/ami/inject_suite_test.go rename pkg/handlers/aws/mutation/ami/{tests/generate_patches.go => inject_worker_test.go} (52%) diff --git a/pkg/handlers/aws/mutation/ami/inject_control_plane_test.go b/pkg/handlers/aws/mutation/ami/inject_control_plane_test.go new file mode 100644 index 000000000..638446022 --- /dev/null +++ b/pkg/handlers/aws/mutation/ami/inject_control_plane_test.go @@ -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, + ) + }) + } +}) diff --git a/pkg/handlers/aws/mutation/ami/inject_suite_test.go b/pkg/handlers/aws/mutation/ami/inject_suite_test.go new file mode 100644 index 000000000..f9d823f89 --- /dev/null +++ b/pkg/handlers/aws/mutation/ami/inject_suite_test.go @@ -0,0 +1,13 @@ +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") +} diff --git a/pkg/handlers/aws/mutation/ami/tests/generate_patches.go b/pkg/handlers/aws/mutation/ami/inject_worker_test.go similarity index 52% rename from pkg/handlers/aws/mutation/ami/tests/generate_patches.go rename to pkg/handlers/aws/mutation/ami/inject_worker_test.go index 36f86066b..eac848d9f 100644 --- a/pkg/handlers/aws/mutation/ami/tests/generate_patches.go +++ b/pkg/handlers/aws/mutation/ami/inject_worker_test.go @@ -1,12 +1,12 @@ // 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" @@ -14,99 +14,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() +var _ = Describe("Generate AMI patches for Worker", func() { + patchGenerator := func() mutation.GeneratePatches { + return mutation.NewMetaGeneratePatchesHandler("", NewWorkerPatch()).(mutation.GeneratePatches) + } - 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() - - 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", @@ -124,11 +48,11 @@ 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", @@ -136,8 +60,8 @@ func TestWorkerGeneratePatches( BaseOS: "testOS", }, }, - - variablePath..., + v1alpha1.AWSVariableName, + VariableName, ), capitest.VariableWithValue( "builtin", @@ -172,5 +96,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, + ) + }) + } +}) diff --git a/pkg/handlers/aws/mutation/metapatch_handler_test.go b/pkg/handlers/aws/mutation/metapatch_handler_test.go index c23db4a6f..5c8ce03da 100644 --- a/pkg/handlers/aws/mutation/metapatch_handler_test.go +++ b/pkg/handlers/aws/mutation/metapatch_handler_test.go @@ -10,14 +10,11 @@ import ( "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/pkg/handlers/aws/mutation/ami" - amitests "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/aws/mutation/ami/tests" + calicotests "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/aws/mutation/cni/calico/tests" "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/aws/mutation/controlplaneloadbalancer" controlplaneloadbalancertests "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/aws/mutation/controlplaneloadbalancer/tests" - "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/aws/mutation/network" - networktests "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/aws/mutation/network/tests" "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/clusterconfig" auditpolicytests "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/mutation/auditpolicy/tests" "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/mutation/etcd" @@ -32,7 +29,6 @@ import ( globalimageregistrymirrortests "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/mutation/mirrors/tests" "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/mutation/users" userstests "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/mutation/users/tests" - "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/workerconfig" ) func metaPatchGeneratorFunc(mgr manager.Manager) func() mutation.GeneratePatches { @@ -102,31 +98,6 @@ func TestGeneratePatches(t *testing.T) { mirrors.GlobalMirrorVariableName, ) - amitests.TestControlPlaneGeneratePatches( - t, - metaPatchGeneratorFunc(mgr), - clusterconfig.MetaVariableName, - clusterconfig.MetaControlPlaneConfigName, - v1alpha1.AWSVariableName, - ami.VariableName, - ) - - amitests.TestWorkerGeneratePatches( - t, - workerPatchGeneratorFunc(), - workerconfig.MetaVariableName, - v1alpha1.AWSVariableName, - ami.VariableName, - ) - - networktests.TestGeneratePatches( - t, - metaPatchGeneratorFunc(mgr), - clusterconfig.MetaVariableName, - v1alpha1.AWSVariableName, - network.VariableName, - ) - controlplaneloadbalancertests.TestGeneratePatches( t, metaPatchGeneratorFunc(mgr), From 06d676a114a08014085ccb8fd884679b6001dd2f Mon Sep 17 00:00:00 2001 From: Shalin Patel Date: Wed, 3 Apr 2024 12:59:39 -0700 Subject: [PATCH 4/8] test: move aws network tests to its own package --- .../generate_patches.go => inject_test.go} | 64 ++++++++++++------- 1 file changed, 41 insertions(+), 23 deletions(-) rename pkg/handlers/aws/mutation/network/{tests/generate_patches.go => inject_test.go} (69%) diff --git a/pkg/handlers/aws/mutation/network/tests/generate_patches.go b/pkg/handlers/aws/mutation/network/inject_test.go similarity index 69% rename from pkg/handlers/aws/mutation/network/tests/generate_patches.go rename to pkg/handlers/aws/mutation/network/inject_test.go index 09dcdfd78..0a76d02b2 100644 --- a/pkg/handlers/aws/mutation/network/tests/generate_patches.go +++ b/pkg/handlers/aws/mutation/network/inject_test.go @@ -1,45 +1,49 @@ // Copyright 2023 D2iQ, Inc. All rights reserved. // SPDX-License-Identifier: Apache-2.0 -package tests +package network import ( "testing" + . "github.com/onsi/ginkgo/v2" "github.com/onsi/gomega" + . "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" ) -func TestGeneratePatches( - t *testing.T, - generatorFunc func() mutation.GeneratePatches, - variableName string, - variablePath ...string, -) { - t.Helper() +func TestNetworkPatch(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "AWS Network mutator suite") +} + +var _ = Describe("Generate AWS Network patches", func() { + patchGenerator := func() mutation.GeneratePatches { + return mutation.NewMetaGeneratePatchesHandler("", NewPatch()).(mutation.GeneratePatches) + } - capitest.ValidateGeneratePatches( - t, - generatorFunc, - capitest.PatchTestDef{ + testDefs := []capitest.PatchTestDef{ + { Name: "unset variable", }, - capitest.PatchTestDef{ + { Name: "VPC ID set", Vars: []runtimehooksv1.Variable{ capitest.VariableWithValue( - variableName, + clusterconfig.MetaVariableName, v1alpha1.AWSNetwork{ VPC: &v1alpha1.VPC{ ID: "vpc-1234", }, }, - variablePath..., + v1alpha1.AWSVariableName, + VariableName, ), }, RequestItem: request.NewAWSClusterTemplateRequestItem("1234"), @@ -49,11 +53,11 @@ func TestGeneratePatches( ValueMatcher: gomega.Equal("vpc-1234"), }}, }, - capitest.PatchTestDef{ + { Name: "Subnet IDs set", Vars: []runtimehooksv1.Variable{ capitest.VariableWithValue( - variableName, + clusterconfig.MetaVariableName, v1alpha1.AWSNetwork{ Subnets: v1alpha1.Subnets{ {ID: "subnet-1"}, @@ -61,7 +65,8 @@ func TestGeneratePatches( {ID: "subnet-3"}, }, }, - variablePath..., + v1alpha1.AWSVariableName, + VariableName, ), }, RequestItem: request.NewAWSClusterTemplateRequestItem("1234"), @@ -71,11 +76,11 @@ func TestGeneratePatches( ValueMatcher: gomega.HaveLen(3), }}, }, - capitest.PatchTestDef{ + { Name: "both VPC ID and Subnet IDs set", Vars: []runtimehooksv1.Variable{ capitest.VariableWithValue( - variableName, + clusterconfig.MetaVariableName, v1alpha1.AWSNetwork{ VPC: &v1alpha1.VPC{ ID: "vpc-1234", @@ -86,7 +91,8 @@ func TestGeneratePatches( {ID: "subnet-3"}, }, }, - variablePath..., + v1alpha1.AWSVariableName, + VariableName, ), }, RequestItem: request.NewAWSClusterTemplateRequestItem("1234"), @@ -100,5 +106,17 @@ func TestGeneratePatches( ValueMatcher: gomega.HaveLen(3), }}, }, - ) -} + } + + // create test node for each case + for testIdx := range testDefs { + tt := testDefs[testIdx] + It(tt.Name, func() { + capitest.AssertGeneratePatches( + GinkgoT(), + patchGenerator, + &tt, + ) + }) + } +}) From 106a13ce107479a24363c8b614f4fd0895aae977 Mon Sep 17 00:00:00 2001 From: Shalin Patel Date: Wed, 3 Apr 2024 13:07:42 -0700 Subject: [PATCH 5/8] test: move controlplaneloadbalancer unit tests to its own package --- .../generate_patches.go => inject_test.go} | 57 ++++++++++++------- .../aws/mutation/metapatch_handler_test.go | 10 ---- 2 files changed, 37 insertions(+), 30 deletions(-) rename pkg/handlers/aws/mutation/controlplaneloadbalancer/{tests/generate_patches.go => inject_test.go} (64%) diff --git a/pkg/handlers/aws/mutation/controlplaneloadbalancer/tests/generate_patches.go b/pkg/handlers/aws/mutation/controlplaneloadbalancer/inject_test.go similarity index 64% rename from pkg/handlers/aws/mutation/controlplaneloadbalancer/tests/generate_patches.go rename to pkg/handlers/aws/mutation/controlplaneloadbalancer/inject_test.go index f799d0624..48f5695e8 100644 --- a/pkg/handlers/aws/mutation/controlplaneloadbalancer/tests/generate_patches.go +++ b/pkg/handlers/aws/mutation/controlplaneloadbalancer/inject_test.go @@ -1,12 +1,14 @@ // Copyright 2023 D2iQ, Inc. All rights reserved. // SPDX-License-Identifier: Apache-2.0 -package tests +package controlplaneloadbalancer import ( "testing" + . "github.com/onsi/ginkgo/v2" "github.com/onsi/gomega" + . "github.com/onsi/gomega" runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1" capav1 "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/api/external/sigs.k8s.io/cluster-api-provider-aws/v2/api/v1beta2" @@ -14,31 +16,33 @@ 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/clusterconfig" ) -func TestGeneratePatches( - t *testing.T, - generatorFunc func() mutation.GeneratePatches, - variableName string, - variablePath ...string, -) { - t.Helper() +func TestControlPlaneLoadBalancerPatch(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "AWS ControlPlane LoadBalancer mutator suite") +} + +var _ = Describe("Generate AWS ControlPlane LoadBalancer patches", func() { + patchGenerator := func() mutation.GeneratePatches { + return mutation.NewMetaGeneratePatchesHandler("", NewPatch()).(mutation.GeneratePatches) + } - capitest.ValidateGeneratePatches( - t, - generatorFunc, - capitest.PatchTestDef{ + testDefs := []capitest.PatchTestDef{ + { Name: "unset variable", }, - capitest.PatchTestDef{ + { Name: "ControlPlaneLoadbalancer scheme set to internet-facing", Vars: []runtimehooksv1.Variable{ capitest.VariableWithValue( - variableName, + clusterconfig.MetaVariableName, v1alpha1.AWSLoadBalancerSpec{ Scheme: &capav1.ELBSchemeInternetFacing, }, - variablePath..., + v1alpha1.AWSVariableName, + VariableName, ), }, RequestItem: request.NewAWSClusterTemplateRequestItem("1234"), @@ -50,15 +54,16 @@ func TestGeneratePatches( ), }}, }, - capitest.PatchTestDef{ + { Name: "ControlPlaneLoadbalancer scheme set to internal", Vars: []runtimehooksv1.Variable{ capitest.VariableWithValue( - variableName, + clusterconfig.MetaVariableName, v1alpha1.AWSLoadBalancerSpec{ Scheme: &capav1.ELBSchemeInternal, }, - variablePath..., + v1alpha1.AWSVariableName, + VariableName, ), }, RequestItem: request.NewAWSClusterTemplateRequestItem("1234"), @@ -70,5 +75,17 @@ func TestGeneratePatches( ), }}, }, - ) -} + } + + // create test node for each case + for testIdx := range testDefs { + tt := testDefs[testIdx] + It(tt.Name, func() { + capitest.AssertGeneratePatches( + GinkgoT(), + patchGenerator, + &tt, + ) + }) + } +}) diff --git a/pkg/handlers/aws/mutation/metapatch_handler_test.go b/pkg/handlers/aws/mutation/metapatch_handler_test.go index 5c8ce03da..6f4a2a0b8 100644 --- a/pkg/handlers/aws/mutation/metapatch_handler_test.go +++ b/pkg/handlers/aws/mutation/metapatch_handler_test.go @@ -12,8 +12,6 @@ import ( "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/handlers/mutation" calicotests "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/aws/mutation/cni/calico/tests" - "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/aws/mutation/controlplaneloadbalancer" - controlplaneloadbalancertests "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/aws/mutation/controlplaneloadbalancer/tests" "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/clusterconfig" auditpolicytests "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/mutation/auditpolicy/tests" @@ -98,14 +96,6 @@ func TestGeneratePatches(t *testing.T) { mirrors.GlobalMirrorVariableName, ) - controlplaneloadbalancertests.TestGeneratePatches( - t, - metaPatchGeneratorFunc(mgr), - clusterconfig.MetaVariableName, - v1alpha1.AWSVariableName, - controlplaneloadbalancer.VariableName, - ) - userstests.TestGeneratePatches( t, metaPatchGeneratorFunc(mgr), From fb1fb657eaf31e223e241072993005731fc5ff4d Mon Sep 17 00:00:00 2001 From: Shalin Patel Date: Wed, 3 Apr 2024 14:00:29 -0700 Subject: [PATCH 6/8] test: move aws cni unit tests to its own package --- .../generate_patches.go => inject_test.go} | 68 +++++++++++-------- .../aws/mutation/metapatch_handler_test.go | 11 --- 2 files changed, 41 insertions(+), 38 deletions(-) rename pkg/handlers/aws/mutation/cni/calico/{tests/generate_patches.go => inject_test.go} (89%) diff --git a/pkg/handlers/aws/mutation/cni/calico/tests/generate_patches.go b/pkg/handlers/aws/mutation/cni/calico/inject_test.go similarity index 89% rename from pkg/handlers/aws/mutation/cni/calico/tests/generate_patches.go rename to pkg/handlers/aws/mutation/cni/calico/inject_test.go index d9bf7be65..93008bc17 100644 --- a/pkg/handlers/aws/mutation/cni/calico/tests/generate_patches.go +++ b/pkg/handlers/aws/mutation/cni/calico/inject_test.go @@ -1,48 +1,48 @@ // Copyright 2023 D2iQ, Inc. All rights reserved. // SPDX-License-Identifier: Apache-2.0 -package tests +package calico import ( "testing" + capav1 "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/api/external/sigs.k8s.io/cluster-api-provider-aws/v2/api/v1beta2" + . "github.com/onsi/ginkgo/v2" "github.com/onsi/gomega" - "github.com/onsi/gomega/format" + runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1" - capav1 "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/api/external/sigs.k8s.io/cluster-api-provider-aws/v2/api/v1beta2" "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" ) -func TestGeneratePatches( - t *testing.T, - generatorFunc func() mutation.GeneratePatches, - variableName string, - variablePath ...string, -) { - t.Helper() +func TestCalicoPatch(t *testing.T) { + gomega.RegisterFailHandler(Fail) + RunSpecs(t, "AWS Calico CNI ingress mutator suite") +} - format.MaxLength = 0 - format.TruncatedDiff = false +var _ = Describe("Generate AWS Calico CNI ingress patches", func() { + patchGenerator := func() mutation.GeneratePatches { + return mutation.NewMetaGeneratePatchesHandler("", NewPatch()).(mutation.GeneratePatches) + } - capitest.ValidateGeneratePatches( - t, - generatorFunc, - capitest.PatchTestDef{ + testDefs := []capitest.PatchTestDef{ + { Name: "unset variable", }, - capitest.PatchTestDef{ + { Name: "provider set with AWSClusterTemplate", Vars: []runtimehooksv1.Variable{ capitest.VariableWithValue( - variableName, + clusterconfig.MetaVariableName, v1alpha1.CNI{ Provider: v1alpha1.CNIProviderCalico, }, - variablePath..., + "addons", + v1alpha1.CNIVariableName, ), }, RequestItem: request.NewAWSClusterTemplateRequestItem("1234"), @@ -101,15 +101,16 @@ func TestGeneratePatches( ), }}, }, - capitest.PatchTestDef{ + { Name: "provider set with AWSClusterTemplate pre-existing rules", Vars: []runtimehooksv1.Variable{ capitest.VariableWithValue( - variableName, + clusterconfig.MetaVariableName, v1alpha1.CNI{ Provider: v1alpha1.CNIProviderCalico, }, - variablePath..., + "addons", + v1alpha1.CNIVariableName, ), }, RequestItem: request.NewAWSClusterTemplateRequestItem( @@ -193,15 +194,16 @@ func TestGeneratePatches( ), }}, }, - capitest.PatchTestDef{ + { Name: "provider set with AWSClusterTemplate conflicting pre-existing rules", Vars: []runtimehooksv1.Variable{ capitest.VariableWithValue( - variableName, + clusterconfig.MetaVariableName, v1alpha1.CNI{ Provider: v1alpha1.CNIProviderCalico, }, - variablePath..., + "addons", + v1alpha1.CNIVariableName, ), }, RequestItem: request.NewAWSClusterTemplateRequestItem( @@ -273,5 +275,17 @@ func TestGeneratePatches( ), }}, }, - ) -} + } + + // create test node for each case + for testIdx := range testDefs { + tt := testDefs[testIdx] + It(tt.Name, func() { + capitest.AssertGeneratePatches( + GinkgoT(), + patchGenerator, + &tt, + ) + }) + } +}) diff --git a/pkg/handlers/aws/mutation/metapatch_handler_test.go b/pkg/handlers/aws/mutation/metapatch_handler_test.go index 6f4a2a0b8..38398b280 100644 --- a/pkg/handlers/aws/mutation/metapatch_handler_test.go +++ b/pkg/handlers/aws/mutation/metapatch_handler_test.go @@ -8,11 +8,8 @@ import ( "sigs.k8s.io/controller-runtime/pkg/manager" - "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" - calicotests "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/aws/mutation/cni/calico/tests" - "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/clusterconfig" auditpolicytests "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/mutation/auditpolicy/tests" "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/mutation/etcd" @@ -46,14 +43,6 @@ func TestGeneratePatches(t *testing.T) { mgr := testEnv.Manager - calicotests.TestGeneratePatches( - t, - metaPatchGeneratorFunc(mgr), - clusterconfig.MetaVariableName, - "addons", - v1alpha1.CNIVariableName, - ) - auditpolicytests.TestGeneratePatches( t, metaPatchGeneratorFunc(mgr), From 4f1a4a13ff1ec3fe600b2bf9b36a0b86e3365736 Mon Sep 17 00:00:00 2001 From: Shalin Patel Date: Wed, 3 Apr 2024 14:02:50 -0700 Subject: [PATCH 7/8] test: fix linting errors --- pkg/handlers/aws/mutation/ami/inject_suite_test.go | 3 +++ pkg/handlers/aws/mutation/ami/inject_worker_test.go | 1 - pkg/handlers/aws/mutation/cni/calico/inject_test.go | 3 +-- .../aws/mutation/controlplaneloadbalancer/inject_test.go | 3 +-- .../aws/mutation/iaminstanceprofile/inject_suite_test.go | 3 +++ .../aws/mutation/iaminstanceprofile/inject_worker_test.go | 1 - .../aws/mutation/instancetype/inject_suite_test.go | 3 +++ .../aws/mutation/instancetype/inject_worker_test.go | 1 - pkg/handlers/aws/mutation/metapatch_handler_test.go | 7 ------- pkg/handlers/aws/mutation/network/inject_test.go | 3 +-- 10 files changed, 12 insertions(+), 16 deletions(-) diff --git a/pkg/handlers/aws/mutation/ami/inject_suite_test.go b/pkg/handlers/aws/mutation/ami/inject_suite_test.go index f9d823f89..bb77ba57e 100644 --- a/pkg/handlers/aws/mutation/ami/inject_suite_test.go +++ b/pkg/handlers/aws/mutation/ami/inject_suite_test.go @@ -1,3 +1,6 @@ +// Copyright 2023 D2iQ, Inc. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + package ami import ( diff --git a/pkg/handlers/aws/mutation/ami/inject_worker_test.go b/pkg/handlers/aws/mutation/ami/inject_worker_test.go index eac848d9f..237f0963a 100644 --- a/pkg/handlers/aws/mutation/ami/inject_worker_test.go +++ b/pkg/handlers/aws/mutation/ami/inject_worker_test.go @@ -6,7 +6,6 @@ package ami import ( . "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" diff --git a/pkg/handlers/aws/mutation/cni/calico/inject_test.go b/pkg/handlers/aws/mutation/cni/calico/inject_test.go index 93008bc17..2bba0f09f 100644 --- a/pkg/handlers/aws/mutation/cni/calico/inject_test.go +++ b/pkg/handlers/aws/mutation/cni/calico/inject_test.go @@ -6,12 +6,11 @@ package calico import ( "testing" - capav1 "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/api/external/sigs.k8s.io/cluster-api-provider-aws/v2/api/v1beta2" . "github.com/onsi/ginkgo/v2" "github.com/onsi/gomega" - runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1" + capav1 "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/api/external/sigs.k8s.io/cluster-api-provider-aws/v2/api/v1beta2" "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" diff --git a/pkg/handlers/aws/mutation/controlplaneloadbalancer/inject_test.go b/pkg/handlers/aws/mutation/controlplaneloadbalancer/inject_test.go index 48f5695e8..60a5c4ec5 100644 --- a/pkg/handlers/aws/mutation/controlplaneloadbalancer/inject_test.go +++ b/pkg/handlers/aws/mutation/controlplaneloadbalancer/inject_test.go @@ -8,7 +8,6 @@ import ( . "github.com/onsi/ginkgo/v2" "github.com/onsi/gomega" - . "github.com/onsi/gomega" runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1" capav1 "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/api/external/sigs.k8s.io/cluster-api-provider-aws/v2/api/v1beta2" @@ -20,7 +19,7 @@ import ( ) func TestControlPlaneLoadBalancerPatch(t *testing.T) { - RegisterFailHandler(Fail) + gomega.RegisterFailHandler(Fail) RunSpecs(t, "AWS ControlPlane LoadBalancer mutator suite") } diff --git a/pkg/handlers/aws/mutation/iaminstanceprofile/inject_suite_test.go b/pkg/handlers/aws/mutation/iaminstanceprofile/inject_suite_test.go index 0617978da..6cf5402dc 100644 --- a/pkg/handlers/aws/mutation/iaminstanceprofile/inject_suite_test.go +++ b/pkg/handlers/aws/mutation/iaminstanceprofile/inject_suite_test.go @@ -1,3 +1,6 @@ +// Copyright 2023 D2iQ, Inc. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + package iaminstanceprofile import ( diff --git a/pkg/handlers/aws/mutation/iaminstanceprofile/inject_worker_test.go b/pkg/handlers/aws/mutation/iaminstanceprofile/inject_worker_test.go index bcd8bdcd9..a3044de6d 100644 --- a/pkg/handlers/aws/mutation/iaminstanceprofile/inject_worker_test.go +++ b/pkg/handlers/aws/mutation/iaminstanceprofile/inject_worker_test.go @@ -6,7 +6,6 @@ package iaminstanceprofile import ( . "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" diff --git a/pkg/handlers/aws/mutation/instancetype/inject_suite_test.go b/pkg/handlers/aws/mutation/instancetype/inject_suite_test.go index 87f8febd5..393d232d7 100644 --- a/pkg/handlers/aws/mutation/instancetype/inject_suite_test.go +++ b/pkg/handlers/aws/mutation/instancetype/inject_suite_test.go @@ -1,3 +1,6 @@ +// Copyright 2023 D2iQ, Inc. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + package instancetype import ( diff --git a/pkg/handlers/aws/mutation/instancetype/inject_worker_test.go b/pkg/handlers/aws/mutation/instancetype/inject_worker_test.go index da73dd919..03cbec647 100644 --- a/pkg/handlers/aws/mutation/instancetype/inject_worker_test.go +++ b/pkg/handlers/aws/mutation/instancetype/inject_worker_test.go @@ -6,7 +6,6 @@ package instancetype import ( . "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" diff --git a/pkg/handlers/aws/mutation/metapatch_handler_test.go b/pkg/handlers/aws/mutation/metapatch_handler_test.go index 38398b280..7b709e945 100644 --- a/pkg/handlers/aws/mutation/metapatch_handler_test.go +++ b/pkg/handlers/aws/mutation/metapatch_handler_test.go @@ -9,7 +9,6 @@ import ( "sigs.k8s.io/controller-runtime/pkg/manager" "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/handlers/mutation" - "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/clusterconfig" auditpolicytests "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/mutation/auditpolicy/tests" "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/mutation/etcd" @@ -32,12 +31,6 @@ func metaPatchGeneratorFunc(mgr manager.Manager) func() mutation.GeneratePatches } } -func workerPatchGeneratorFunc() func() mutation.GeneratePatches { - return func() mutation.GeneratePatches { - return MetaWorkerPatchHandler().(mutation.GeneratePatches) - } -} - func TestGeneratePatches(t *testing.T) { t.Parallel() diff --git a/pkg/handlers/aws/mutation/network/inject_test.go b/pkg/handlers/aws/mutation/network/inject_test.go index 0a76d02b2..f91185cf3 100644 --- a/pkg/handlers/aws/mutation/network/inject_test.go +++ b/pkg/handlers/aws/mutation/network/inject_test.go @@ -8,7 +8,6 @@ import ( . "github.com/onsi/ginkgo/v2" "github.com/onsi/gomega" - . "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" @@ -19,7 +18,7 @@ import ( ) func TestNetworkPatch(t *testing.T) { - RegisterFailHandler(Fail) + gomega.RegisterFailHandler(Fail) RunSpecs(t, "AWS Network mutator suite") } From 42d1feb1b3181fe04d89a1b13f1a3214267473aa Mon Sep 17 00:00:00 2001 From: Shalin Patel Date: Wed, 3 Apr 2024 15:11:41 -0700 Subject: [PATCH 8/8] test: unit tests for AWS security groups --- .../inject_control_plane_test.go | 75 +++++++++++++++++ .../securitygroups/inject_suite_test.go | 16 ++++ .../securitygroups/inject_worker_test.go | 81 +++++++++++++++++++ 3 files changed, 172 insertions(+) create mode 100644 pkg/handlers/aws/mutation/securitygroups/inject_control_plane_test.go create mode 100644 pkg/handlers/aws/mutation/securitygroups/inject_suite_test.go create mode 100644 pkg/handlers/aws/mutation/securitygroups/inject_worker_test.go diff --git a/pkg/handlers/aws/mutation/securitygroups/inject_control_plane_test.go b/pkg/handlers/aws/mutation/securitygroups/inject_control_plane_test.go new file mode 100644 index 000000000..8c67e96f2 --- /dev/null +++ b/pkg/handlers/aws/mutation/securitygroups/inject_control_plane_test.go @@ -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, + ) + }) + } +}) diff --git a/pkg/handlers/aws/mutation/securitygroups/inject_suite_test.go b/pkg/handlers/aws/mutation/securitygroups/inject_suite_test.go new file mode 100644 index 000000000..09ae3ad05 --- /dev/null +++ b/pkg/handlers/aws/mutation/securitygroups/inject_suite_test.go @@ -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") +} diff --git a/pkg/handlers/aws/mutation/securitygroups/inject_worker_test.go b/pkg/handlers/aws/mutation/securitygroups/inject_worker_test.go new file mode 100644 index 000000000..2e4d90844 --- /dev/null +++ b/pkg/handlers/aws/mutation/securitygroups/inject_worker_test.go @@ -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, + ) + }) + } +})