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

Commit 0c95d64

Browse files
committed
test: package level unit test for HTTPProxy
1 parent 7b0b7e6 commit 0c95d64

File tree

6 files changed

+94
-11
lines changed

6 files changed

+94
-11
lines changed

common/pkg/testutils/capitest/patches.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func ValidateGeneratePatches[T mutation.GeneratePatches](
4444
testDefs ...PatchTestDef,
4545
) {
4646
t.Helper()
47-
patchTestFunc := func(tt PatchTestDef) {
47+
testFunc := func(tt *PatchTestDef) {
4848
g := gomega.NewWithT(t)
4949
h := handlerCreator()
5050
req := &runtimehooksv1.GeneratePatchesRequest{
@@ -84,12 +84,12 @@ func ValidateGeneratePatches[T mutation.GeneratePatches](
8484

8585
// compose Ginkgo table arguments
8686
// https://onsi.github.io/ginkgo/#table-specs for more details
87-
tableArgs := []interface{}{patchTestFunc}
87+
testArgs := make([]TableEntry, 0, len(testDefs))
8888
for testIdx := range testDefs {
8989
tt := testDefs[testIdx]
90-
tableArgs = append(tableArgs, Entry(tt.Name, tt))
90+
testArgs = append(testArgs, Entry(tt.Name, &tt))
9191
}
92-
DescribeTable("Patches", tableArgs...)
92+
DescribeTable("Patches", testFunc, testArgs)
9393
}
9494

9595
// VariableWithValue returns a runtimehooksv1.Variable with the passed name and value.

pkg/handlers/aws/mutation/region/patch_test.go renamed to pkg/handlers/aws/mutation/region/inject_test.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
1+
// Copyright 2023 D2iQ, Inc. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
14
package region
25

36
import (
47
"testing"
58

9+
. "github.com/onsi/ginkgo/v2"
10+
. "github.com/onsi/gomega"
11+
612
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/api/v1alpha1"
713
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/handlers/mutation"
814
regiontests "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/aws/mutation/region/tests"
915
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/clusterconfig"
10-
. "github.com/onsi/ginkgo/v2"
11-
. "github.com/onsi/gomega"
1216
)
1317

1418
func TestRegionPatch(t *testing.T) {
1519
RegisterFailHandler(Fail)
16-
RunSpecs(t, "AWS region patch suite")
20+
RunSpecs(t, "AWS Region mutator suite")
1721
}
1822

19-
var _ = Describe("AWS region patches", func() {
23+
var _ = Describe("Generate AWS Region patches", func() {
2024
// only add aws region patch
2125
regionPatchGenerator := func() mutation.GeneratePatches {
2226
return mutation.NewMetaGeneratePatchesHandler("", NewPatch()).(mutation.GeneratePatches)

pkg/handlers/generic/mutation/httpproxy/inject_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,15 @@ package httpproxy
66
import (
77
"testing"
88

9+
. "github.com/onsi/ginkgo/v2"
910
"github.com/onsi/gomega"
1011
v1 "k8s.io/api/core/v1"
1112
capiv1 "sigs.k8s.io/cluster-api/api/v1beta1"
13+
14+
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/handlers/mutation"
15+
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/clusterconfig"
16+
httpproxy "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/mutation/httpproxy/tests"
17+
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/test/helpers"
1218
)
1319

1420
func TestGenerateNoProxy(t *testing.T) {
@@ -179,3 +185,24 @@ func TestGenerateNoProxy(t *testing.T) {
179185
})
180186
}
181187
}
188+
189+
func TestHTTPProxyPatch(t *testing.T) {
190+
gomega.RegisterFailHandler(Fail)
191+
RunSpecs(t, "HTTP Proxy mutator suite")
192+
}
193+
194+
var _ = Describe("Generate HTTPProxy Patches", func() {
195+
// only add HTTPProxy patch
196+
httpProxyPatchGenerator := func() mutation.GeneratePatches {
197+
testEnv := helpers.TestEnv
198+
return mutation.NewMetaGeneratePatchesHandler(
199+
"",
200+
NewPatch(testEnv.Client)).(mutation.GeneratePatches)
201+
}
202+
httpproxy.TestGeneratePatches(
203+
GinkgoT(),
204+
httpProxyPatchGenerator,
205+
clusterconfig.MetaVariableName,
206+
VariableName,
207+
)
208+
})

pkg/handlers/generic/mutation/httpproxy/tests/generate_patches.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
package tests
55

66
import (
7-
"testing"
8-
7+
. "github.com/onsi/ginkgo/v2"
98
"github.com/onsi/gomega"
109
"k8s.io/apiserver/pkg/storage/names"
1110
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
@@ -17,7 +16,7 @@ import (
1716
)
1817

1918
func TestGeneratePatches(
20-
t *testing.T,
19+
t GinkgoTInterface,
2120
generatorFunc func() mutation.GeneratePatches,
2221
variableName string,
2322
variablePath ...string,

test/helpers/environment.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright 2023 D2iQ, Inc. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package helpers
5+
6+
import (
7+
"context"
8+
"time"
9+
10+
. "github.com/onsi/ginkgo/v2"
11+
. "github.com/onsi/gomega"
12+
"k8s.io/klog/v2"
13+
"k8s.io/klog/v2/textlogger"
14+
ctrl "sigs.k8s.io/controller-runtime"
15+
"sigs.k8s.io/controller-runtime/pkg/log"
16+
)
17+
18+
var TestEnv *TestEnvironment
19+
20+
// Initialize the test environment. BeforeSuite will be only executed if this package is loaded by the test.
21+
var _ = BeforeSuite(func(ctx SpecContext) {
22+
By("Starting test environment")
23+
testEnvConfig := NewTestEnvironmentConfiguration()
24+
var err error
25+
TestEnv, err = testEnvConfig.Build()
26+
if err != nil {
27+
panic(err)
28+
}
29+
go func() {
30+
By("Starting the manager")
31+
defer GinkgoRecover()
32+
Expect(TestEnv.StartManager(ctx)).To(Succeed())
33+
}()
34+
}, NodeTimeout(60*time.Second))
35+
36+
var _ = AfterSuite(func(ctx context.Context) {
37+
Expect(TestEnv.Stop()).To(Succeed())
38+
})
39+
40+
//nolint:gochecknoinits //needed for test initialization
41+
func init() {
42+
klog.InitFlags(nil)
43+
logger := textlogger.NewLogger(nil)
44+
// use klog as the internal logger for this envtest environment.
45+
log.SetLogger(logger)
46+
// additionally force all of the controllers to use the Ginkgo logger.
47+
ctrl.SetLogger(logger)
48+
// add logger for ginkgo
49+
klog.SetOutput(GinkgoWriter)
50+
}

test/helpers/envtest.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ func (t *TestEnvironment) StartManager(ctx context.Context) error {
137137

138138
// Stop stops the test environment.
139139
func (t *TestEnvironment) Stop() error {
140+
if t.cancel != nil {
141+
t.cancel()
142+
}
140143
return t.env.Stop()
141144
}
142145

0 commit comments

Comments
 (0)