Skip to content

Commit bcaf71c

Browse files
🐛 (go/v4): add missing cancel context to controller's suite-test (#4067)
fix: add missing cancel context to controller's suite-test
1 parent bb31df0 commit bcaf71c

File tree

35 files changed

+137
-28
lines changed

35 files changed

+137
-28
lines changed

docs/book/src/cronjob-tutorial/testdata/project/api/v1/webhook_suite_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,8 @@ var _ = BeforeSuite(func() {
138138
})
139139

140140
var _ = AfterSuite(func() {
141-
cancel()
142141
By("tearing down the test environment")
142+
cancel()
143143
err := testEnv.Stop()
144144
Expect(err).NotTo(HaveOccurred())
145145
})

docs/book/src/cronjob-tutorial/testdata/project/internal/controller/suite_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ var (
6161
cfg *rest.Config
6262
k8sClient client.Client // You'll be using this client in your tests.
6363
testEnv *envtest.Environment
64-
ctx context.Context
65-
cancel context.CancelFunc
6664
)
65+
var ctx context.Context
66+
var cancel context.CancelFunc
6767

6868
func TestControllers(t *testing.T) {
6969
RegisterFailHandler(Fail)
@@ -169,8 +169,8 @@ You won't need to touch these.
169169
*/
170170

171171
var _ = AfterSuite(func() {
172-
cancel()
173172
By("tearing down the test environment")
173+
cancel()
174174
err := testEnv.Stop()
175175
Expect(err).NotTo(HaveOccurred())
176176
})

docs/book/src/getting-started/testdata/project/internal/controller/suite_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package controller
1818

1919
import (
20+
"context"
2021
"fmt"
2122
"path/filepath"
2223
"runtime"
@@ -42,6 +43,8 @@ import (
4243
var cfg *rest.Config
4344
var k8sClient client.Client
4445
var testEnv *envtest.Environment
46+
var ctx context.Context
47+
var cancel context.CancelFunc
4548

4649
func TestControllers(t *testing.T) {
4750
RegisterFailHandler(Fail)
@@ -52,6 +55,8 @@ func TestControllers(t *testing.T) {
5255
var _ = BeforeSuite(func() {
5356
logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)))
5457

58+
ctx, cancel = context.WithCancel(context.TODO())
59+
5560
By("bootstrapping test environment")
5661
testEnv = &envtest.Environment{
5762
CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "crd", "bases")},
@@ -85,6 +90,7 @@ var _ = BeforeSuite(func() {
8590

8691
var _ = AfterSuite(func() {
8792
By("tearing down the test environment")
93+
cancel()
8894
err := testEnv.Stop()
8995
Expect(err).NotTo(HaveOccurred())
9096
})

hack/docs/internal/cronjob-tutorial/generate_cronjob.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -470,12 +470,6 @@ func updateSuiteTest(sp *Sample) {
470470
*/`, SuiteTestIntro)
471471
CheckError("updating suite_test.go to add license intro", err)
472472

473-
err = pluginutil.InsertCode(
474-
filepath.Join(sp.ctx.Dir, "internal/controller/suite_test.go"),
475-
`import (`, `
476-
"context"`)
477-
CheckError("updating suite_test.go to add context", err)
478-
479473
err = pluginutil.InsertCode(
480474
filepath.Join(sp.ctx.Dir, "internal/controller/suite_test.go"),
481475
`
@@ -496,8 +490,7 @@ var testEnv *envtest.Environment
496490

497491
err = pluginutil.InsertCode(
498492
filepath.Join(sp.ctx.Dir, "internal/controller/suite_test.go"),
499-
`
500-
logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)))
493+
`ctx, cancel = context.WithCancel(context.TODO())
501494
`, SuiteTestReadCRD)
502495
CheckError("updating suite_test.go to add text about CRD", err)
503496

@@ -535,6 +528,7 @@ var testEnv *envtest.Environment
535528
`
536529
var _ = AfterSuite(func() {
537530
By("tearing down the test environment")
531+
cancel()
538532
err := testEnv.Stop()
539533
Expect(err).NotTo(HaveOccurred())
540534
})

hack/docs/internal/cronjob-tutorial/writing_tests_env.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,10 @@ var (
3838
cfg *rest.Config
3939
k8sClient client.Client // You'll be using this client in your tests.
4040
testEnv *envtest.Environment
41-
ctx context.Context
42-
cancel context.CancelFunc
4341
)
4442
`
4543

4644
const SuiteTestReadCRD = `
47-
ctx, cancel = context.WithCancel(context.TODO())
48-
4945
/*
5046
First, the envtest cluster is configured to read CRDs from the CRD directory Kubebuilder scaffolds for you.
5147
*/`
@@ -116,8 +112,8 @@ You won't need to touch these.
116112
*/
117113
118114
var _ = AfterSuite(func() {
119-
cancel()
120115
By("tearing down the test environment")
116+
cancel()
121117
err := testEnv.Stop()
122118
Expect(err).NotTo(HaveOccurred())
123119
})

pkg/plugins/golang/v4/scaffolds/internal/templates/api/webhook_suitetest.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,8 @@ var _ = BeforeSuite(func() {
255255
})
256256
257257
var _ = AfterSuite(func() {
258-
cancel()
259258
By("tearing down the test environment")
259+
cancel()
260260
err := testEnv.Stop()
261261
Expect(err).NotTo(HaveOccurred())
262262
})

pkg/plugins/golang/v4/scaffolds/internal/templates/controllers/controller_suitetest.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ import (
159159
var cfg *rest.Config
160160
var k8sClient client.Client
161161
var testEnv *envtest.Environment
162+
var ctx context.Context
163+
var cancel context.CancelFunc
162164
163165
func TestControllers(t *testing.T) {
164166
RegisterFailHandler(Fail)
@@ -169,6 +171,8 @@ func TestControllers(t *testing.T) {
169171
var _ = BeforeSuite(func() {
170172
logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)))
171173
174+
ctx, cancel = context.WithCancel(context.TODO())
175+
172176
By("bootstrapping test environment")
173177
testEnv = &envtest.Environment{
174178
CRDDirectoryPaths: []string{filepath.Join({{ .CRDDirectoryRelativePath }}, "config", "crd", "bases")},
@@ -199,6 +203,7 @@ var _ = BeforeSuite(func() {
199203
200204
var _ = AfterSuite(func() {
201205
By("tearing down the test environment")
206+
cancel()
202207
err := testEnv.Stop()
203208
Expect(err).NotTo(HaveOccurred())
204209
})

testdata/project-v4-multigroup-with-deploy-image/api/crew/v1/webhook_suite_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,8 @@ var _ = BeforeSuite(func() {
138138
})
139139

140140
var _ = AfterSuite(func() {
141-
cancel()
142141
By("tearing down the test environment")
142+
cancel()
143143
err := testEnv.Stop()
144144
Expect(err).NotTo(HaveOccurred())
145145
})

testdata/project-v4-multigroup-with-deploy-image/api/ship/v1/webhook_suite_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,8 @@ var _ = BeforeSuite(func() {
138138
})
139139

140140
var _ = AfterSuite(func() {
141-
cancel()
142141
By("tearing down the test environment")
142+
cancel()
143143
err := testEnv.Stop()
144144
Expect(err).NotTo(HaveOccurred())
145145
})

testdata/project-v4-multigroup-with-deploy-image/api/ship/v2alpha1/webhook_suite_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,8 @@ var _ = BeforeSuite(func() {
138138
})
139139

140140
var _ = AfterSuite(func() {
141-
cancel()
142141
By("tearing down the test environment")
142+
cancel()
143143
err := testEnv.Stop()
144144
Expect(err).NotTo(HaveOccurred())
145145
})

0 commit comments

Comments
 (0)