From aa467be424728921f21ca22f77f3ae77b218c267 Mon Sep 17 00:00:00 2001 From: Shalin Patel Date: Mon, 1 Apr 2024 20:32:32 -0700 Subject: [PATCH] test: package level unit test for HTTPProxy --- common/pkg/testutils/capitest/patches.go | 8 +-- .../region/{patch_test.go => inject_test.go} | 12 +++-- .../generic/mutation/httpproxy/inject_test.go | 27 ++++++++++ .../httpproxy/tests/generate_patches.go | 5 +- test/helpers/environment.go | 50 +++++++++++++++++++ test/helpers/envtest.go | 3 ++ 6 files changed, 94 insertions(+), 11 deletions(-) rename pkg/handlers/aws/mutation/region/{patch_test.go => inject_test.go} (82%) create mode 100644 test/helpers/environment.go diff --git a/common/pkg/testutils/capitest/patches.go b/common/pkg/testutils/capitest/patches.go index 80e27bdff..fa817d25f 100644 --- a/common/pkg/testutils/capitest/patches.go +++ b/common/pkg/testutils/capitest/patches.go @@ -44,7 +44,7 @@ func ValidateGeneratePatches[T mutation.GeneratePatches]( testDefs ...PatchTestDef, ) { t.Helper() - patchTestFunc := func(tt PatchTestDef) { + testFunc := func(tt *PatchTestDef) { g := gomega.NewWithT(t) h := handlerCreator() req := &runtimehooksv1.GeneratePatchesRequest{ @@ -84,12 +84,12 @@ func ValidateGeneratePatches[T mutation.GeneratePatches]( // compose Ginkgo table arguments // https://onsi.github.io/ginkgo/#table-specs for more details - tableArgs := []interface{}{patchTestFunc} + testArgs := make([]TableEntry, 0, len(testDefs)) for testIdx := range testDefs { tt := testDefs[testIdx] - tableArgs = append(tableArgs, Entry(tt.Name, tt)) + testArgs = append(testArgs, Entry(tt.Name, &tt)) } - DescribeTable("Patches", tableArgs...) + DescribeTable("Patches", testFunc, testArgs) } // VariableWithValue returns a runtimehooksv1.Variable with the passed name and value. diff --git a/pkg/handlers/aws/mutation/region/patch_test.go b/pkg/handlers/aws/mutation/region/inject_test.go similarity index 82% rename from pkg/handlers/aws/mutation/region/patch_test.go rename to pkg/handlers/aws/mutation/region/inject_test.go index 97fe8e49b..40b8d52db 100644 --- a/pkg/handlers/aws/mutation/region/patch_test.go +++ b/pkg/handlers/aws/mutation/region/inject_test.go @@ -1,22 +1,26 @@ +// Copyright 2023 D2iQ, Inc. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + package region import ( "testing" + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + "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" regiontests "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/aws/mutation/region/tests" "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/clusterconfig" - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" ) func TestRegionPatch(t *testing.T) { RegisterFailHandler(Fail) - RunSpecs(t, "AWS region patch suite") + RunSpecs(t, "AWS Region mutator suite") } -var _ = Describe("AWS region patches", func() { +var _ = Describe("Generate AWS Region patches", func() { // only add aws region patch regionPatchGenerator := func() mutation.GeneratePatches { return mutation.NewMetaGeneratePatchesHandler("", NewPatch()).(mutation.GeneratePatches) diff --git a/pkg/handlers/generic/mutation/httpproxy/inject_test.go b/pkg/handlers/generic/mutation/httpproxy/inject_test.go index caae06f15..064765538 100644 --- a/pkg/handlers/generic/mutation/httpproxy/inject_test.go +++ b/pkg/handlers/generic/mutation/httpproxy/inject_test.go @@ -6,9 +6,15 @@ package httpproxy import ( "testing" + . "github.com/onsi/ginkgo/v2" "github.com/onsi/gomega" v1 "k8s.io/api/core/v1" capiv1 "sigs.k8s.io/cluster-api/api/v1beta1" + + "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" + httpproxy "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/mutation/httpproxy/tests" + "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/test/helpers" ) func TestGenerateNoProxy(t *testing.T) { @@ -179,3 +185,24 @@ func TestGenerateNoProxy(t *testing.T) { }) } } + +func TestHTTPProxyPatch(t *testing.T) { + gomega.RegisterFailHandler(Fail) + RunSpecs(t, "HTTP Proxy mutator suite") +} + +var _ = Describe("Generate HTTPProxy Patches", func() { + // only add HTTPProxy patch + httpProxyPatchGenerator := func() mutation.GeneratePatches { + testEnv := helpers.TestEnv + return mutation.NewMetaGeneratePatchesHandler( + "", + NewPatch(testEnv.Client)).(mutation.GeneratePatches) + } + httpproxy.TestGeneratePatches( + GinkgoT(), + httpProxyPatchGenerator, + clusterconfig.MetaVariableName, + VariableName, + ) +}) diff --git a/pkg/handlers/generic/mutation/httpproxy/tests/generate_patches.go b/pkg/handlers/generic/mutation/httpproxy/tests/generate_patches.go index f3b437ed5..302cc96d3 100644 --- a/pkg/handlers/generic/mutation/httpproxy/tests/generate_patches.go +++ b/pkg/handlers/generic/mutation/httpproxy/tests/generate_patches.go @@ -4,8 +4,7 @@ package tests import ( - "testing" - + . "github.com/onsi/ginkgo/v2" "github.com/onsi/gomega" "k8s.io/apiserver/pkg/storage/names" runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1" @@ -17,7 +16,7 @@ import ( ) func TestGeneratePatches( - t *testing.T, + t GinkgoTInterface, generatorFunc func() mutation.GeneratePatches, variableName string, variablePath ...string, diff --git a/test/helpers/environment.go b/test/helpers/environment.go new file mode 100644 index 000000000..d40ef8c8b --- /dev/null +++ b/test/helpers/environment.go @@ -0,0 +1,50 @@ +// Copyright 2023 D2iQ, Inc. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +package helpers + +import ( + "context" + "time" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + "k8s.io/klog/v2" + "k8s.io/klog/v2/textlogger" + ctrl "sigs.k8s.io/controller-runtime" + "sigs.k8s.io/controller-runtime/pkg/log" +) + +var TestEnv *TestEnvironment + +// Initialize the test environment. BeforeSuite will be only executed if this package is loaded by the test. +var _ = BeforeSuite(func(ctx SpecContext) { + By("Starting test environment") + testEnvConfig := NewTestEnvironmentConfiguration() + var err error + TestEnv, err = testEnvConfig.Build() + if err != nil { + panic(err) + } + go func() { + By("Starting the manager") + defer GinkgoRecover() + Expect(TestEnv.StartManager(ctx)).To(Succeed()) + }() +}, NodeTimeout(60*time.Second)) + +var _ = AfterSuite(func(ctx context.Context) { + Expect(TestEnv.Stop()).To(Succeed()) +}) + +//nolint:gochecknoinits //needed for test initialization +func init() { + klog.InitFlags(nil) + logger := textlogger.NewLogger(textlogger.NewConfig()) + // use klog as the internal logger for this envtest environment. + log.SetLogger(logger) + // additionally force all of the controllers to use the Ginkgo logger. + ctrl.SetLogger(logger) + // add logger for ginkgo + klog.SetOutput(GinkgoWriter) +} diff --git a/test/helpers/envtest.go b/test/helpers/envtest.go index 26257229d..190b8f009 100644 --- a/test/helpers/envtest.go +++ b/test/helpers/envtest.go @@ -137,6 +137,9 @@ func (t *TestEnvironment) StartManager(ctx context.Context) error { // Stop stops the test environment. func (t *TestEnvironment) Stop() error { + if t.cancel != nil { + t.cancel() + } return t.env.Stop() }