Skip to content

refactor(test): use t.Context() instead of context.Background() - #12523

Draft
DariuszPorowski wants to merge 2 commits into
mainfrom
dp/comparable-lark-maroon
Draft

refactor(test): use t.Context() instead of context.Background()#12523
DariuszPorowski wants to merge 2 commits into
mainfrom
dp/comparable-lark-maroon

Conversation

@DariuszPorowski

Copy link
Copy Markdown
Member

Summary

Replaces many context.Background() usages in tests with t.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

  • Run targeted tests for changed packages, for example:
    • go test ./pkg/...
    • go test ./test/functional-portable/ucp/cloud -run AWS
  • Confirm updated tests pass using t.Context() instead of context.Background().

File change summary

File Summary of change
pkg/**/**_test.go (43 files) Replaced context.Background() with t.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.go Updated functional AWS cloud test to use t.Context() for request/test context handling.

@DariuszPorowski
DariuszPorowski requested review from a team as code owners July 23, 2026 15:39
Copilot AI review requested due to automatic review settings July 23, 2026 15:39
@github-actions

Copy link
Copy Markdown

Dependency Review

✅ No vulnerabilities or license issues or OpenSSF Scorecard issues found.

Scanned Files

None

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 with t.Context().
  • Updated some helpers to thread test-scoped context into request creation (e.g., getTestHTTPRequest).
  • Removed now-unneeded context imports 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.

Comment on lines 170 to 172
// Use a fresh context because t.Context() is cancelled before cleanup runs.
cleanupCtx := context.Background()
cleanupCtx := t.Context()

Comment on lines 197 to 199
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()
@github-actions

github-actions Bot commented Jul 23, 2026

Copy link
Copy Markdown

Unit Tests

    1 files   -     1    363 suites   - 94   6m 53s ⏱️ -44s
6 010 tests  -    53  6 003 ✅  -    58  2 💤 ±0  5 ❌ +5 
6 009 runs   - 1 261  6 001 ✅  - 1 267  2 💤 ±0  6 ❌ +6 

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.
github.com/radius-project/radius/cmd/rad/cmd ‑ Test_HandlePanic
github.com/radius-project/radius/cmd/rad/cmd ‑ Test_prettyPrintJSON
github.com/radius-project/radius/cmd/rad/cmd ‑ Test_prettyPrintJSON/formats_JSON_correctly
github.com/radius-project/radius/cmd/rad/cmd ‑ Test_prettyPrintJSON/formats_complex_objects
github.com/radius-project/radius/cmd/rad/cmd ‑ Test_prettyPrintJSON/handles_invalid_JSON
github.com/radius-project/radius/cmd/rad/cmd ‑ Test_prettyPrintRPError
github.com/radius-project/radius/cmd/rad/cmd ‑ Test_wirePreviewSubcommand
github.com/radius-project/radius/cmd/rad/cmd ‑ Test_wirePreviewSubcommand/routes_to_legacy_runner_when_--preview_is_not_set
github.com/radius-project/radius/cmd/rad/cmd ‑ Test_wirePreviewSubcommand/routes_to_legacy_runner_when_RADIUS_PREVIEW_is_not_true
github.com/radius-project/radius/cmd/rad/cmd ‑ Test_wirePreviewSubcommand/routes_to_preview_runner_when_--preview_is_set
…
TestMain

♻️ This comment has been updated with latest results.

@github-actions

github-actions Bot commented Jul 23, 2026

Copy link
Copy Markdown

Functional Tests - ucp-cloud

4 tests  ±0   4 ✅ ±0   31s ⏱️ -2s
1 suites ±0   0 💤 ±0 
1 files   ±0   0 ❌ ±0 

Results for commit 147252c. ± Comparison against base commit d85be5b.

♻️ 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>
Copilot AI review requested due to automatic review settings July 23, 2026 16:38
@DariuszPorowski
DariuszPorowski force-pushed the dp/comparable-lark-maroon branch from 4ef68a5 to 04e2096 Compare July 23, 2026 16:38
@DariuszPorowski DariuszPorowski self-assigned this Jul 23, 2026
Signed-off-by: Dariusz Porowski <3431813+DariuszPorowski@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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().

Copilot AI review requested due to automatic review settings July 23, 2026 16:43
@radius-functional-tests

radius-functional-tests Bot commented Jul 23, 2026

Copy link
Copy Markdown

Radius functional test overview

🔍 Go to test action run

Click here to see the test run details
Name Value
Repository radius-project/radius
Commit ref 147252c
Unique ID funce3b2f809ae
Image tag pr-funce3b2f809ae
  • Dapr: 1.14.4
  • Azure KeyVault CSI driver: 1.4.2
  • Azure Workload identity webhook: 1.3.0
  • Bicep recipe location ghcr.io/radius-project/dev/test/testrecipes/test-bicep-recipes/<name>:pr-funce3b2f809ae
  • Terraform recipe location http://tf-module-server.radius-test-tf-module-server.svc.cluster.local/<name>.zip (in cluster)
  • applications-rp test image location: ghcr.io/radius-project/dev/applications-rp:pr-funce3b2f809ae
  • dynamic-rp test image location: ghcr.io/radius-project/dev/dynamic-rp:pr-funce3b2f809ae
  • controller test image location: ghcr.io/radius-project/dev/controller:pr-funce3b2f809ae
  • ucp test image location: ghcr.io/radius-project/dev/ucpd:pr-funce3b2f809ae
  • deployment-engine test image location: ghcr.io/radius-project/deployment-engine:latest

Test Status

⌛ Building Radius and pushing container images for functional tests...
✅ Recipe publishing succeeded
⌛ Starting corerp-cloud functional tests...
⌛ Starting ucp-cloud functional tests...
✅ Container images build succeeded
⌛ Publishing Bicep Recipes for functional tests...
✅ Recipe publishing succeeded
⌛ Starting corerp-cloud functional tests...
⌛ Starting ucp-cloud functional tests...
❌ ucp-cloud functional test failed. Please check the logs for more details
✅ corerp-cloud functional tests succeeded
✅ ucp-cloud functional tests succeeded
✅ corerp-cloud functional tests succeeded

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

  • t is not in scope here, so t.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:386
  • t is not in scope here, so t.Context() will not compile. Use server.T().Context() instead.
    pkg/dynamicrp/integrationtest/dynamic/resource_test.go:402
  • t is not in scope here, so t.Context() will not compile. Use server.T().Context() instead.
    pkg/dynamicrp/integrationtest/dynamic/resource_test.go:421
  • t is not in scope here, so t.Context() will not compile. Use server.T().Context() instead.
    pkg/dynamicrp/integrationtest/dynamic/resource_test.go:436
  • t is not in scope here, so t.Context() will not compile. Use server.T().Context() instead.
    pkg/dynamicrp/integrationtest/dynamic/resource_test.go:453
  • t is not in scope here, so t.Context() will not compile. Use server.T().Context() instead.
    pkg/dynamicrp/integrationtest/dynamic/resource_test.go:476
  • t is not in scope here, so t.Context() will not compile. Use server.T().Context() instead.
    pkg/dynamicrp/frontend/encryptionfilter_test.go:199
  • t is not in scope in this helper, so t.Context() will not compile. Either pass t *testing.T into createTestContext (and update all call sites), or keep this helper using context.Background() and let callers supply a test-scoped context when needed.
func createTestContext() context.Context {
	ctx := t.Context()
	// Add ARM request context

@DariuszPorowski
DariuszPorowski marked this pull request as draft July 23, 2026 16:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants