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

Commit

Permalink
test: package level unit test for HTTPProxy
Browse files Browse the repository at this point in the history
  • Loading branch information
supershal committed Apr 2, 2024
1 parent 7b0b7e6 commit aa467be
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 11 deletions.
8 changes: 4 additions & 4 deletions common/pkg/testutils/capitest/patches.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
27 changes: 27 additions & 0 deletions pkg/handlers/generic/mutation/httpproxy/inject_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
)
})
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -17,7 +16,7 @@ import (
)

func TestGeneratePatches(
t *testing.T,
t GinkgoTInterface,
generatorFunc func() mutation.GeneratePatches,
variableName string,
variablePath ...string,
Expand Down
50 changes: 50 additions & 0 deletions test/helpers/environment.go
Original file line number Diff line number Diff line change
@@ -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)
}
3 changes: 3 additions & 0 deletions test/helpers/envtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down

0 comments on commit aa467be

Please sign in to comment.