Skip to content

Commit 5e7fe95

Browse files
supershaljimmidyson
authored andcommitted
test: unit test for individual patch generator (#21)
* test: unit test for individual patch generator * test: package level unit test for HTTPProxy * test: fix data race between multiple unit test that use envtest * test: make patchgenerator generic function * fix: linting errors after rebase from main
1 parent bf9280c commit 5e7fe95

File tree

12 files changed

+196
-56
lines changed

12 files changed

+196
-56
lines changed

api/v1alpha1/nutanix_node_types.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,27 @@ func (NutanixMachineDetails) VariableSchema() clusterv1.VariableSchema {
8080
Description: "memorySize is the memory size (in Quantity format) of the VM eg. 4Gi",
8181
Type: "string",
8282
},
83-
"image": NutanixResourceIdentifier{}.VariableSchema().OpenAPIV3Schema,
84-
"cluster": NutanixResourceIdentifier{}.VariableSchema().OpenAPIV3Schema,
85-
"subnets": NutanixResourceIdentifiers{}.VariableSchema().OpenAPIV3Schema,
86-
"bootType": NutanixBootType(capxv1.NutanixBootTypeLegacy).VariableSchema().OpenAPIV3Schema,
83+
"image": NutanixResourceIdentifier{}.VariableSchema().OpenAPIV3Schema,
84+
"cluster": NutanixResourceIdentifier{}.VariableSchema().OpenAPIV3Schema,
85+
"subnets": NutanixResourceIdentifiers{}.VariableSchema().OpenAPIV3Schema,
86+
"bootType": NutanixBootType(
87+
capxv1.NutanixBootTypeLegacy,
88+
).VariableSchema().
89+
OpenAPIV3Schema,
8790
"systemDiskSize": {
8891
Description: "systemDiskSize is size (in Quantity format) of the system disk of the VM eg. 20Gi",
8992
Type: "string",
9093
},
9194
},
92-
Required: []string{"vcpusPerSocket", "vcpuSockets", "memorySize", "image", "cluster", "subnets", "systemDiskSize"},
95+
Required: []string{
96+
"vcpusPerSocket",
97+
"vcpuSockets",
98+
"memorySize",
99+
"image",
100+
"cluster",
101+
"subnets",
102+
"systemDiskSize",
103+
},
93104
},
94105
}
95106
}
@@ -134,7 +145,10 @@ func (NutanixResourceIdentifier) VariableSchema() clusterv1.VariableSchema {
134145
Description: "Nutanix Resource Identifier",
135146
Type: "object",
136147
Properties: map[string]clusterv1.JSONSchemaProps{
137-
"type": NutanixIdentifierType(capxv1.NutanixIdentifierName).VariableSchema().OpenAPIV3Schema,
148+
"type": NutanixIdentifierType(
149+
capxv1.NutanixIdentifierName,
150+
).VariableSchema().
151+
OpenAPIV3Schema,
138152
"uuid": {
139153
Type: "string",
140154
Description: "uuid is the UUID of the resource in the PC.",

common/go.mod

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ require (
1111
github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/api v0.0.0-00010101000000-000000000000
1212
github.com/evanphx/json-patch/v5 v5.9.0
1313
github.com/go-logr/logr v1.4.1
14+
github.com/onsi/ginkgo/v2 v2.15.0
1415
github.com/onsi/gomega v1.31.1
1516
github.com/spf13/pflag v1.0.5
1617
github.com/stretchr/testify v1.9.0
@@ -36,13 +37,15 @@ require (
3637
github.com/go-openapi/jsonpointer v0.19.6 // indirect
3738
github.com/go-openapi/jsonreference v0.20.2 // indirect
3839
github.com/go-openapi/swag v0.22.3 // indirect
40+
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
3941
github.com/gogo/protobuf v1.3.2 // indirect
4042
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
4143
github.com/golang/protobuf v1.5.4 // indirect
4244
github.com/google/cel-go v0.17.7 // indirect
4345
github.com/google/gnostic-models v0.6.8 // indirect
4446
github.com/google/go-cmp v0.6.0 // indirect
4547
github.com/google/gofuzz v1.2.0 // indirect
48+
github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1 // indirect
4649
github.com/google/uuid v1.3.1 // indirect
4750
github.com/imdario/mergo v0.3.13 // indirect
4851
github.com/josharian/intern v1.0.0 // indirect
@@ -69,6 +72,7 @@ require (
6972
golang.org/x/term v0.15.0 // indirect
7073
golang.org/x/text v0.14.0 // indirect
7174
golang.org/x/time v0.3.0 // indirect
75+
golang.org/x/tools v0.16.1 // indirect
7276
google.golang.org/appengine v1.6.7 // indirect
7377
google.golang.org/genproto/googleapis/api v0.0.0-20230913181813-007df8e322eb // indirect
7478
google.golang.org/genproto/googleapis/rpc v0.0.0-20230920204549-e6e6cdab5c13 // indirect

common/go.sum

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,6 @@ github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+W
365365
github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk=
366366
github.com/onsi/ginkgo v1.14.0/go.mod h1:iSB4RoI2tjJc9BBv4NKIKWKya62Rps+oPG/Lv9klQyY=
367367
github.com/onsi/ginkgo v1.16.4/go.mod h1:dX+/inL/fNMqNlz0e9LfyB9TswhZpCVdJM/Z6Vvnwo0=
368-
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
369368
github.com/onsi/ginkgo v1.16.5/go.mod h1:+E8gABHa3K6zRBolWtd+ROzc/U5bkGt0FwiG042wbpU=
370369
github.com/onsi/ginkgo/v2 v2.1.3/go.mod h1:vw5CSIxN1JObi/U8gcbwft7ZxR2dgaR70JSE3/PpL4c=
371370
github.com/onsi/ginkgo/v2 v2.1.4/go.mod h1:um6tUpWM/cxCK3/FK8BXqEiUMUwRgSM4JXG47RKZmLU=

common/pkg/testutils/capitest/patches.go

Lines changed: 44 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import (
77
"context"
88
"encoding/json"
99
"fmt"
10-
"testing"
1110

11+
. "github.com/onsi/ginkgo/v2"
1212
"github.com/onsi/gomega"
1313
"github.com/onsi/gomega/gstruct"
1414
gomegatypes "github.com/onsi/gomega/types"
@@ -39,55 +39,57 @@ type JSONPatchMatcher struct {
3939
}
4040

4141
func ValidateGeneratePatches[T mutation.GeneratePatches](
42-
t *testing.T,
42+
t GinkgoTInterface,
4343
handlerCreator func() T,
4444
testDefs ...PatchTestDef,
4545
) {
4646
t.Helper()
47+
testFunc := func(tt *PatchTestDef) {
48+
g := gomega.NewWithT(t)
49+
h := handlerCreator()
50+
req := &runtimehooksv1.GeneratePatchesRequest{
51+
Variables: tt.Vars,
52+
Items: []runtimehooksv1.GeneratePatchesRequestItem{
53+
tt.RequestItem,
54+
{
55+
HolderReference: runtimehooksv1.HolderReference{
56+
APIVersion: capiv1.GroupVersion.String(),
57+
Kind: "Cluster",
58+
Namespace: request.Namespace,
59+
Name: request.ClusterName,
60+
},
61+
},
62+
},
63+
}
64+
resp := &runtimehooksv1.GeneratePatchesResponse{}
65+
h.GeneratePatches(context.Background(), req, resp)
66+
expectedStatus := runtimehooksv1.ResponseStatusSuccess
67+
if tt.ExpectedFailure {
68+
expectedStatus = runtimehooksv1.ResponseStatusFailure
69+
}
70+
g.Expect(resp.Status).
71+
To(gomega.Equal(expectedStatus), fmt.Sprintf("Message: %s", resp.Message))
72+
73+
if len(tt.ExpectedPatchMatchers) == 0 {
74+
g.Expect(resp.Items).To(gomega.BeEmpty())
75+
return
76+
}
77+
g.Expect(resp.Items).To(containPatches(&tt.RequestItem, tt.ExpectedPatchMatchers...))
4778

79+
if len(tt.UnexpectedPatchMatchers) > 0 {
80+
g.Expect(resp.Items).
81+
ToNot(containPatches(&tt.RequestItem, tt.UnexpectedPatchMatchers...))
82+
}
83+
}
84+
85+
// compose Ginkgo table arguments
86+
// https://onsi.github.io/ginkgo/#table-specs for more details
87+
testArgs := make([]TableEntry, 0, len(testDefs))
4888
for testIdx := range testDefs {
4989
tt := testDefs[testIdx]
50-
51-
t.Run(tt.Name, func(t *testing.T) {
52-
t.Parallel()
53-
54-
g := gomega.NewWithT(t)
55-
h := handlerCreator()
56-
req := &runtimehooksv1.GeneratePatchesRequest{
57-
Variables: tt.Vars,
58-
Items: []runtimehooksv1.GeneratePatchesRequestItem{
59-
tt.RequestItem,
60-
{
61-
HolderReference: runtimehooksv1.HolderReference{
62-
APIVersion: capiv1.GroupVersion.String(),
63-
Kind: "Cluster",
64-
Namespace: request.Namespace,
65-
Name: request.ClusterName,
66-
},
67-
},
68-
},
69-
}
70-
resp := &runtimehooksv1.GeneratePatchesResponse{}
71-
h.GeneratePatches(context.Background(), req, resp)
72-
expectedStatus := runtimehooksv1.ResponseStatusSuccess
73-
if tt.ExpectedFailure {
74-
expectedStatus = runtimehooksv1.ResponseStatusFailure
75-
}
76-
g.Expect(resp.Status).
77-
To(gomega.Equal(expectedStatus), fmt.Sprintf("Message: %s", resp.Message))
78-
79-
if len(tt.ExpectedPatchMatchers) == 0 {
80-
g.Expect(resp.Items).To(gomega.BeEmpty())
81-
return
82-
}
83-
g.Expect(resp.Items).To(containPatches(&tt.RequestItem, tt.ExpectedPatchMatchers...))
84-
85-
if len(tt.UnexpectedPatchMatchers) > 0 {
86-
g.Expect(resp.Items).
87-
ToNot(containPatches(&tt.RequestItem, tt.UnexpectedPatchMatchers...))
88-
}
89-
})
90+
testArgs = append(testArgs, Entry(tt.Name, &tt))
9091
}
92+
DescribeTable("Patches", testFunc, testArgs)
9193
}
9294

9395
// VariableWithValue returns a runtimehooksv1.Variable with the passed name and value.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2023 D2iQ, Inc. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package region
5+
6+
import (
7+
"testing"
8+
9+
. "github.com/onsi/ginkgo/v2"
10+
. "github.com/onsi/gomega"
11+
12+
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/api/v1alpha1"
13+
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/handlers/mutation"
14+
regiontests "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/aws/mutation/region/tests"
15+
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/clusterconfig"
16+
)
17+
18+
func TestRegionPatch(t *testing.T) {
19+
RegisterFailHandler(Fail)
20+
RunSpecs(t, "AWS Region mutator suite")
21+
}
22+
23+
var _ = Describe("Generate AWS Region patches", func() {
24+
// only add aws region patch
25+
patchGenerator := func() mutation.GeneratePatches {
26+
return mutation.NewMetaGeneratePatchesHandler("", NewPatch()).(mutation.GeneratePatches)
27+
}
28+
regiontests.TestGeneratePatches(
29+
GinkgoT(),
30+
patchGenerator,
31+
clusterconfig.MetaVariableName,
32+
v1alpha1.AWSVariableName,
33+
VariableName,
34+
)
35+
})

pkg/handlers/aws/mutation/region/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
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
1110

@@ -15,7 +14,7 @@ import (
1514
)
1615

1716
func TestGeneratePatches(
18-
t *testing.T,
17+
t GinkgoTInterface,
1918
generatorFunc func() mutation.GeneratePatches,
2019
variableName string,
2120
variablePath ...string,

pkg/handlers/generic/lifecycle/ccm/aws/handler.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,13 @@ func (a *AWSCCM) Apply(
9696
)
9797
}
9898

99-
err = lifecycleutils.EnsureCRSForClusterFromObjects(ctx, ccmConfigMap.Name, a.client, cluster, ccmConfigMap)
99+
err = lifecycleutils.EnsureCRSForClusterFromObjects(
100+
ctx,
101+
ccmConfigMap.Name,
102+
a.client,
103+
cluster,
104+
ccmConfigMap,
105+
)
100106
if err != nil {
101107
return fmt.Errorf("failed to generate CCM CRS for cluster: %w", err)
102108
}

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

Lines changed: 29 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,26 @@ 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+
patchGenerator := func() mutation.GeneratePatches {
197+
// Always initialize the testEnv variable in the closure.
198+
// This will allow ginkgo to initialize testEnv variable during test execution time.
199+
testEnv := helpers.TestEnv
200+
return mutation.NewMetaGeneratePatchesHandler(
201+
"",
202+
NewPatch(testEnv.Client)).(mutation.GeneratePatches)
203+
}
204+
httpproxy.TestGeneratePatches(
205+
GinkgoT(),
206+
patchGenerator,
207+
clusterconfig.MetaVariableName,
208+
VariableName,
209+
)
210+
})

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,

pkg/handlers/nutanix/mutation/prismcentralendpoint/tests/generate_patches.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/common/pkg/testutils/capitest/request"
1818
)
1919

20+
//
2021
//nolint:lll // just a long string
2122
const testCertBundle = "LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUVjekNDQTF1Z0F3SUJBZ0lCQURBTkJna3Foa2lHOXcwQkFRUUZBRC4uQWtHQTFVRUJoTUNSMEl4CkV6QVJCZ05WQkFnVENsTnZiV1V0VTNSaGRHVXhGREFTQmdOVkJBb1RDMC4uMEVnVEhSa01UY3dOUVlEClZRUUxFeTVEYkdGemN5QXhJRkIxWW14cFl5QlFjbWx0WVhKNUlFTmxjbi4uWFJwYjI0Z1FYVjBhRzl5CmFYUjVNUlF3RWdZRFZRUURFd3RDWlhOMElFTkJJRXgwWkRBZUZ3MHdNRC4uVFV3TVRaYUZ3MHdNVEF5Ck1EUXhPVFV3TVRaYU1JR0hNUXN3Q1FZRFZRUUdFd0pIUWpFVE1CRUdBMS4uMjl0WlMxVGRHRjBaVEVVCk1CSUdBMVVFQ2hNTFFtVnpkQ0JEUVNCTWRHUXhOekExQmdOVkJBc1RMay4uREVnVUhWaWJHbGpJRkJ5CmFXMWhjbmtnUTJWeWRHbG1hV05oZEdsdmJpQkJkWFJvYjNKcGRIa3hGRC4uQU1UQzBKbGMzUWdRMEVnClRIUmtNSUlCSWpBTkJna3Foa2lHOXcwQkFRRUZBQU9DQVE4QU1JSUJDZy4uVHoybXI3U1ppQU1mUXl1CnZCak05T2lKalJhelhCWjFCalA1Q0UvV20vUnI1MDBQUksrTGg5eDVlSi4uL0FOQkUwc1RLMFpzREdNCmFrMm0xZzdvcnVJM2RZM1ZIcUl4RlR6MFRhMWQrTkFqd25MZTRuT2I3Ly4uazA1U2hoQnJKR0JLS3hiCjhuMTA0by81cDhIQXNaUGR6YkZNSXlOakp6Qk0ybzV5NUExM3dpTGl0RS4uZnlZa1F6YXhDdzBBd3psCmtWSGlJeUN1YUY0d2o1NzFwU3prdjZzdis0SURNYlQvWHBDbzhMNndUYS4uc2grZXRMRDZGdFRqWWJiCnJ2WjhSUU0xdGxLZG9NSGcycXhyYUFWKytITkJZbU5XczBkdUVkalViSi4uWEk5VHRuUzRvMUNrajdQCk9mbGppUUlEQVFBQm80SG5NSUhrTUIwR0ExVWREZ1FXQkJROHVyTUNSTC4uNUFrSXA5TkpISnc1VENCCnRBWURWUjBqQklHc01JR3BnQlE4dXJNQ1JMWVlNSFVLVTVBa0lwOU5KSC4uYVNCaWpDQmh6RUxNQWtHCkExVUVCaE1DUjBJeEV6QVJCZ05WQkFnVENsTnZiV1V0VTNSaGRHVXhGRC4uQW9UQzBKbGMzUWdRMEVnClRIUmtNVGN3TlFZRFZRUUxFeTVEYkdGemN5QXhJRkIxWW14cFl5QlFjbS4uRU5sY25ScFptbGpZWFJwCmIyNGdRWFYwYUc5eWFYUjVNUlF3RWdZRFZRUURFd3RDWlhOMElFTkJJRS4uREFNQmdOVkhSTUVCVEFECkFRSC9NQTBHQ1NxR1NJYjNEUUVCQkFVQUE0SUJBUUMxdVlCY3NTbmN3QS4uRENzUWVyNzcyQzJ1Y3BYCnhRVUUvQzBwV1dtNmdEa3dkNUQwRFNNREpScVYvd2VvWjR3QzZCNzNmNS4uYkxoR1lIYVhKZVNENktyClhjb093TGRTYUdtSllzbExLWkIzWklERXAwd1lUR2hndGViNkpGaVR0bi4uc2YyeGRyWWZQQ2lJQjdnCkJNQVY3R3pkYzRWc3BTNmxqckFoYmlpYXdkQmlRbFFtc0JlRno5SmtGNC4uYjNsOEJvR04rcU1hNTZZCkl0OHVuYTJnWTRsMk8vL29uODhyNUlXSmxtMUwwb0E4ZTRmUjJ5ckJIWC4uYWRzR2VGS2t5TnJ3R2kvCjd2UU1mWGRHc1JyWE5HUkduWCt2V0RaMy96V0kwam9EdENrTm5xRXBWbi4uSG9YCi0tLS0tRU5EIENFUlRJRklDQVRFLS0tLS0="
2223

test/helpers/environment.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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("Initialize loggers for testing")
23+
// Uninitialized logger spits stacktrace as warning during test execution
24+
logger := textlogger.NewLogger(textlogger.NewConfig())
25+
// use klog as the internal logger for this envtest environment.
26+
log.SetLogger(logger)
27+
// additionally force all of the controllers to use the Ginkgo logger.
28+
ctrl.SetLogger(logger)
29+
klog.InitFlags(nil)
30+
// add logger for ginkgo
31+
klog.SetOutput(GinkgoWriter)
32+
33+
By("Starting test environment")
34+
testEnvConfig := NewTestEnvironmentConfiguration()
35+
var err error
36+
TestEnv, err = testEnvConfig.Build()
37+
if err != nil {
38+
panic(err)
39+
}
40+
By("Starting the manager")
41+
go func() {
42+
defer GinkgoRecover()
43+
Expect(TestEnv.StartManager(ctx)).To(Succeed())
44+
}()
45+
}, NodeTimeout(60*time.Second))
46+
47+
var _ = AfterSuite(func(ctx context.Context) {
48+
Expect(TestEnv.Stop()).To(Succeed())
49+
})

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)