refactor(test): use t.Context() instead of context.Background() - #12523
refactor(test): use t.Context() instead of context.Background()#12523DariuszPorowski wants to merge 2 commits into
Conversation
Dependency Review✅ No vulnerabilities or license issues or OpenSSF Scorecard issues found.Scanned FilesNone |
There was a problem hiding this comment.
Pull request overview
This PR updates Go tests across the Radius codebase to prefer t.Context() over context.Background(), aligning cancellations and lifecycles with each test’s execution and modern Go testing APIs.
Changes:
- Replaced many
context.Background()usages in unit/integration/functional tests witht.Context(). - Updated some helpers to thread test-scoped context into request creation (e.g.,
getTestHTTPRequest). - Removed now-unneeded
contextimports from several test files.
Reviewed changes
Copilot reviewed 44 out of 44 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| test/functional-portable/ucp/cloud/aws_test.go | Updates functional AWS test context usage; cleanup currently uses t.Context() (needs fix). |
| pkg/upgrade/preflight/resource_check_test.go | Uses t.Context() for preflight resource check tests. |
| pkg/upgrade/preflight/kubernetes_check_test.go | Uses t.Context() for preflight connectivity tests. |
| pkg/ucp/aws/servicecontext/awsrequestcontext_test.go | Uses t.Context() in AWS request context tests. |
| pkg/statearchive/oci/oci_test.go | Uses t.Context() throughout OCI archive tests. |
| pkg/statearchive/git/git_test.go | Uses t.Context() throughout git archive tests. |
| pkg/schema/validator_test.go | Uses t.Context() in schema validator tests and trims unused imports. |
| pkg/schema/annotations_test.go | Uses t.Context() in schema annotations tests. |
| pkg/retry/retry_test.go | Uses t.Context() in retry tests. |
| pkg/recipes/terraform/install_test.go | Uses t.Context() in terraform install tests and trims unused imports. |
| pkg/recipes/configloader/environment_test.go | Uses t.Context() in config loader tests. |
| pkg/messagingrp/frontend/controller/rabbitmqqueues/listsecretsrabbitmq_test.go | Uses t.Context() in RabbitMQ secrets list tests. |
| pkg/graph/persistence/git/git_store_test.go | Uses t.Context() in git store tests. |
| pkg/dynamicrp/integrationtest/dynamic/resource_test.go | Replaces contexts in dynamic RP integration helpers; currently introduces non-compiling t.Context() usage (needs fix). |
| pkg/dynamicrp/frontend/encryptionfilter_test.go | Updates helper context creation; currently introduces non-compiling t.Context() usage (needs fix). |
| pkg/dynamicrp/backend/processor/dynamicresource_test.go | Uses t.Context() in dynamic resource processor tests. |
| pkg/datastoresrp/frontend/controller/sqldatabases/listsecretssqldatabase_test.go | Uses t.Context() in SQL secrets list tests. |
| pkg/datastoresrp/frontend/controller/rediscaches/listsecretsrediscache_test.go | Uses t.Context() in Redis secrets list tests. |
| pkg/datastoresrp/frontend/controller/mongodatabases/listsecretsmongodatabase_test.go | Uses t.Context() in MongoDB secrets list tests. |
| pkg/crypto/encryption/sensitive_test.go | Uses t.Context() in sensitive encryption tests. |
| pkg/crypto/encryption/keyprovider_test.go | Uses t.Context() in key provider tests and trims unused imports. |
| pkg/corerp/renderers/volume/azure/keyvault_test.go | Uses t.Context() in keyvault renderer tests and trims unused imports. |
| pkg/corerp/handlers/kubernetes_test.go | Uses t.Context() in Kubernetes handler tests and trims unused imports. |
| pkg/corerp/handlers/kubernetes_http_proxy_waiter_test.go | Uses t.Context() in HTTPProxy waiter tests. |
| pkg/corerp/handlers/kubernetes_deployment_waiter_test.go | Uses t.Context() in deployment waiter tests. |
| pkg/corerp/frontend/controller/extenders/listsecretsextender_test.go | Uses t.Context() in extender secrets list tests. |
| pkg/corerp/frontend/controller/environments/v20250801preview/createorupdateenvironment_test.go | Uses t.Context() in environment controller tests. |
| pkg/corerp/frontend/controller/environments/getrecipemetadata_test.go | Uses t.Context() in get recipe metadata tests. |
| pkg/corerp/frontend/controller/environments/createorupdateenvironment_test.go | Uses t.Context() in environment controller tests (older API version). |
| pkg/corerp/backend/deployment/deploymentprocessor_test.go | Uses t.Context() in deployment processor tests. |
| pkg/components/secret/kubernetes/client_test.go | Uses t.Context() in Kubernetes secret client tests and trims unused imports. |
| pkg/components/secret/inmemory/client_test.go | Uses t.Context() in in-memory secret client tests and trims unused imports. |
| pkg/components/secret/client_test.go | Uses t.Context() in secret client tests. |
| pkg/cli/manifest/validation_test.go | Uses t.Context() in CLI manifest validation tests and trims unused imports. |
| pkg/cli/kubernetes/kubernetes_test.go | Uses t.Context() in CLI Kubernetes helper tests. |
| pkg/cli/helm/cluster_test.go | Uses t.Context() in Helm cluster tests. |
| pkg/cli/cmd/uninstall/kubernetes/kubernetes_test.go | Uses t.Context() in uninstall command tests. |
| pkg/cli/cmd/install/kubernetes/kubernetes_test.go | Uses t.Context() in install command tests. |
| pkg/armrpc/frontend/defaultoperation/listresources_test.go | Uses t.Context() in default list operation tests. |
| pkg/armrpc/frontend/defaultoperation/getresource_test.go | Uses t.Context() in default get operation tests. |
| pkg/armrpc/frontend/defaultoperation/getoperationstatus_test.go | Uses t.Context() in default operation status tests. |
| pkg/armrpc/authentication/certvalidator_test.go | Uses t.Context() in cert validator tests. |
| pkg/armrpc/asyncoperation/worker/worker_test.go | Uses t.Context() in async worker tests. |
| pkg/armrpc/api/v1/armrequestcontext_test.go | Uses t.Context() in ARM request context tests; updates helper to take t *testing.T. |
Comments suppressed due to low confidence (6)
pkg/dynamicrp/integrationtest/dynamic/resource_test.go:385
- t is not in scope in this helper (only server is). This will not compile. Use the test host's *testing.T (server.T()) to get a test-scoped context.
pkg/dynamicrp/integrationtest/dynamic/resource_test.go:401 - t is not in scope in this helper (only server is). This will not compile. Use the test host's *testing.T (server.T()) to get a test-scoped context.
pkg/dynamicrp/integrationtest/dynamic/resource_test.go:420 - t is not in scope in this helper (only server is). This will not compile. Use the test host's *testing.T (server.T()) to get a test-scoped context.
pkg/dynamicrp/integrationtest/dynamic/resource_test.go:435 - t is not in scope in this helper (only server is). This will not compile. Use the test host's *testing.T (server.T()) to get a test-scoped context.
pkg/dynamicrp/integrationtest/dynamic/resource_test.go:452 - t is not in scope in this helper (only server is). This will not compile. Use the test host's *testing.T (server.T()) to get a test-scoped context.
pkg/dynamicrp/integrationtest/dynamic/resource_test.go:475 - t is not in scope in this helper (only server is). This will not compile. Use the test host's *testing.T (server.T()) to get a test-scoped context.
| // Use a fresh context because t.Context() is cancelled before cleanup runs. | ||
| cleanupCtx := context.Background() | ||
| cleanupCtx := t.Context() | ||
|
|
| func createTestContext() context.Context { | ||
| ctx := context.Background() | ||
| ctx := t.Context() | ||
| // Add ARM request context |
|
|
||
| func createRadiusPlane(server *ucptesthost.TestHost) v20231001preview.RadiusPlanesClientCreateOrUpdateResponse { | ||
| ctx := context.Background() | ||
| ctx := t.Context() |
Unit Tests 1 files - 1 363 suites - 94 6m 53s ⏱️ -44s For more details on these failures, see this check. Results for commit 147252c. ± Comparison against base commit d85be5b. This pull request removes 54 and adds 1 tests. Note that renamed tests count towards both.♻️ This comment has been updated with latest results. |
- Updated multiple test files across various packages to replace the use of context.Background() with t.Context() for better context management during testing. - This change enhances the consistency of context usage in tests and aligns with the recommended practices for Go testing. Signed-off-by: Dariusz Porowski <3431813+DariuszPorowski@users.noreply.github.com>
4ef68a5 to
04e2096
Compare
Signed-off-by: Dariusz Porowski <3431813+DariuszPorowski@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 44 out of 44 changed files in this pull request and generated no new comments.
Comments suppressed due to low confidence (9)
test/functional-portable/ucp/cloud/aws_test.go:172
- t.Cleanup comment says the test context is cancelled before cleanup runs, but cleanupCtx now uses t.Context(); cleanup may be canceled and skip deleting AWS resources (potential leaks/flaky tests). Use a non-cancelled context for cleanup (e.g., context.Background(), or context.WithoutCancel(t.Context()) if you want to retain values).
pkg/dynamicrp/frontend/encryptionfilter_test.go:199 - createTestContext uses t.Context() but createTestContext has no *testing.T in scope, so this won't compile. Either pass *testing.T into createTestContext (and update call sites) or keep using context.Background() inside this helper.
func createTestContext() context.Context {
ctx := t.Context()
// Add ARM request context
pkg/dynamicrp/integrationtest/dynamic/resource_test.go:363
- This helper uses t.Context() but there's no *testing.T in scope, so the test file won't compile. Use the test host's *testing.T: server.T().Context().
pkg/dynamicrp/integrationtest/dynamic/resource_test.go:386 - This helper uses t.Context() but there's no *testing.T in scope, so the test file won't compile. Use the test host's *testing.T: server.T().Context().
pkg/dynamicrp/integrationtest/dynamic/resource_test.go:402 - This helper uses t.Context() but there's no *testing.T in scope, so the test file won't compile. Use the test host's *testing.T: server.T().Context().
pkg/dynamicrp/integrationtest/dynamic/resource_test.go:421 - This helper uses t.Context() but there's no *testing.T in scope, so the test file won't compile. Use the test host's *testing.T: server.T().Context().
pkg/dynamicrp/integrationtest/dynamic/resource_test.go:436 - This helper uses t.Context() but there's no *testing.T in scope, so the test file won't compile. Use the test host's *testing.T: server.T().Context().
pkg/dynamicrp/integrationtest/dynamic/resource_test.go:453 - This helper uses t.Context() but there's no *testing.T in scope, so the test file won't compile. Use the test host's *testing.T: server.T().Context().
pkg/dynamicrp/integrationtest/dynamic/resource_test.go:476 - This helper uses t.Context() but there's no *testing.T in scope, so the test file won't compile. Use the test host's *testing.T: server.T().Context().
Radius functional test overviewClick here to see the test run details
Test Status⌛ Building Radius and pushing container images for functional tests... |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 44 out of 44 changed files in this pull request and generated no new comments.
Comments suppressed due to low confidence (8)
pkg/dynamicrp/integrationtest/dynamic/resource_test.go:363
tis not in scope here, sot.Context()will not compile. Use the TestHost's*testing.T(server.T()) to get a test-scoped context.
pkg/dynamicrp/integrationtest/dynamic/resource_test.go:386tis not in scope here, sot.Context()will not compile. Useserver.T().Context()instead.
pkg/dynamicrp/integrationtest/dynamic/resource_test.go:402tis not in scope here, sot.Context()will not compile. Useserver.T().Context()instead.
pkg/dynamicrp/integrationtest/dynamic/resource_test.go:421tis not in scope here, sot.Context()will not compile. Useserver.T().Context()instead.
pkg/dynamicrp/integrationtest/dynamic/resource_test.go:436tis not in scope here, sot.Context()will not compile. Useserver.T().Context()instead.
pkg/dynamicrp/integrationtest/dynamic/resource_test.go:453tis not in scope here, sot.Context()will not compile. Useserver.T().Context()instead.
pkg/dynamicrp/integrationtest/dynamic/resource_test.go:476tis not in scope here, sot.Context()will not compile. Useserver.T().Context()instead.
pkg/dynamicrp/frontend/encryptionfilter_test.go:199tis not in scope in this helper, sot.Context()will not compile. Either passt *testing.TintocreateTestContext(and update all call sites), or keep this helper usingcontext.Background()and let callers supply a test-scoped context when needed.
func createTestContext() context.Context {
ctx := t.Context()
// Add ARM request context
Summary
Replaces many
context.Background()usages in tests witht.Context()to align test context lifecycle with each test and reduce detached background contexts.Reason for change
Test-scoped contexts improve cancellation behavior and make tests more idiomatic with modern Go test APIs.
Fixes #
How to test
go test ./pkg/...go test ./test/functional-portable/ucp/cloud -run AWSt.Context()instead ofcontext.Background().File change summary
pkg/**/**_test.go(43 files)context.Background()witht.Context()in unit/integration tests across ARMRPC, CLI, core RP, dynamic RP, crypto, recipes, schema, state archive, and upgrade preflight test suites.test/functional-portable/ucp/cloud/aws_test.got.Context()for request/test context handling.