Skip to content

Commit

Permalink
Merge pull request GoogleCloudPlatform#1114 from justinsb/cancel_cont…
Browse files Browse the repository at this point in the history
…exts_timeouts_envtest

Tests: close manager context before envtest context
  • Loading branch information
google-oss-prow[bot] authored Jan 24, 2024
2 parents 8a11c22 + 8a61e42 commit e452e5d
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions config/tests/samples/create/harness.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,14 @@ func NewHarness(t *testing.T, ctx context.Context) *Harness {

roundTripper := http.RoundTripper(mockCloud)

h.Ctx = context.WithValue(h.Ctx, httpRoundTripperKey, roundTripper)
ctx = context.WithValue(ctx, httpRoundTripperKey, roundTripper)
h.Ctx = ctx

kccConfig.HTTPClient = &http.Client{Transport: roundTripper}

// Also hook the oauth2 library
h.Ctx = context.WithValue(h.Ctx, oauth2.HTTPClient, kccConfig.HTTPClient)
ctx = context.WithValue(ctx, oauth2.HTTPClient, kccConfig.HTTPClient)
h.Ctx = ctx

h.gcpAccessToken = "dummytoken"
kccConfig.GCPAccessToken = h.gcpAccessToken
Expand Down Expand Up @@ -265,7 +267,15 @@ func NewHarness(t *testing.T, ctx context.Context) *Harness {
}

h.kccConfig = kccConfig
mgr, err := kccmanager.New(h.Ctx, h.restConfig, kccConfig)
// We must cancel the manager Context before cancelling the envtest Context
// Create a context specifically for this, and register the test cleanup function
// after the envtest cleanup function (these run last-in, first-out).
// See https://github.com/kubernetes-sigs/controller-runtime/issues/1571#issuecomment-945535598
mgrContext, cancel := context.WithCancel(ctx)
t.Cleanup(func() {
cancel()
})
mgr, err := kccmanager.New(mgrContext, h.restConfig, kccConfig)
if err != nil {
t.Fatalf("error creating new manager: %v", err)
}
Expand Down

0 comments on commit e452e5d

Please sign in to comment.