Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use testing.TB.Context() instead of context.Background() #4754

Merged
merged 2 commits into from
Mar 24, 2025

Conversation

Horiodino
Copy link
Contributor

What type of PR is this?

/kind cleanup

What this PR does / why we need it:

Go 1.24, testing.TB.Context(t) is supported, allowing us to replace all instances of context.Background() in tests

Which issue(s) this PR fixes:

Fixes #4655 (comment)

Special notes for your reviewer:

Does this PR introduce a user-facing change?

NONE

@k8s-ci-robot k8s-ci-robot added release-note-none Denotes a PR that doesn't merit a release note. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. labels Mar 22, 2025
@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Mar 22, 2025
@k8s-ci-robot
Copy link
Contributor

Hi @Horiodino. Thanks for your PR.

I'm waiting for a kubernetes-sigs member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@k8s-ci-robot k8s-ci-robot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Mar 22, 2025
Copy link

netlify bot commented Mar 22, 2025

Deploy Preview for kubernetes-sigs-kueue canceled.

Name Link
🔨 Latest commit 69dc204
🔍 Latest deploy log https://app.netlify.com/sites/kubernetes-sigs-kueue/deploys/67e02ded3b655c0008486c61

@mbobrovskyi
Copy link
Contributor

/ok-to-test

@k8s-ci-robot k8s-ci-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Mar 22, 2025
@Horiodino Horiodino force-pushed the t-ctx branch 3 times, most recently from 9be1959 to d7cd901 Compare March 22, 2025 16:29
Copy link
Contributor

@mbobrovskyi mbobrovskyi left a comment

Choose a reason for hiding this comment

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

Can we update it here as well?

return ctrl.LoggerInto(context.Background(), logger), logger

Copy link
Contributor

@mbobrovskyi mbobrovskyi left a comment

Choose a reason for hiding this comment

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

For follow up we need to update integration and e2e tests as well.

@Horiodino
Copy link
Contributor Author

For follow up we need to update integration and e2e tests as well.

E2E test? It's using Ginkgo

@mbobrovskyi
Copy link
Contributor

For follow up we need to update integration and e2e tests as well.

E2E test? It's using Ginkgo

Yeah. I think we can get context from ginkgo.GinkgoTB().Context().

@Horiodino
Copy link
Contributor Author

Oh, I thought you were referring to using t.Context(). My bad!

@mbobrovskyi
Copy link
Contributor

Oh, I thought you were referring to using t.Context(). My bad!

Yeah ginkgo.GinkgoTB() is a wrapper for testing.TB().

Verified

This commit was signed with the committer’s verified signature.
Horiodino Praful Khanduri
Signed-off-by: Horiodino <[email protected]>

Using ctx.Background() cant parse nil.

Signed-off-by: Horiodino <[email protected]>

Remove unused context return value from getClientBuilder function

Signed-off-by: Horiodino <[email protected]>

Use ginkgo.GinkgoTB().Context() instead of context.Background()

Signed-off-by: Horiodino <[email protected]>
@Horiodino Horiodino marked this pull request as draft March 23, 2025 12:11
@k8s-ci-robot k8s-ci-robot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Mar 23, 2025
@Horiodino Horiodino marked this pull request as ready for review March 23, 2025 12:55
@k8s-ci-robot k8s-ci-robot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Mar 23, 2025
@k8s-ci-robot k8s-ci-robot requested a review from mimowo March 23, 2025 12:55
@mbobrovskyi
Copy link
Contributor

mbobrovskyi commented Mar 23, 2025

@Horiodino try upgrading Ginkgo to v2.23.3. It looks like they fixed this bug.

@@ -103,7 +105,7 @@ func main() {
log.Info("Start runner", "outputDir", outputDir, "crdsPath", crdsPath)
errCh := make(chan error, 3)
wg := &sync.WaitGroup{}
ctx, ctxCancel := context.WithCancel(ctrl.LoggerInto(context.Background(), log))
ctx, ctxCancel := context.WithCancel(ctrl.LoggerInto(ginkgo.GinkgoT().Context(), log))
Copy link
Contributor

Choose a reason for hiding this comment

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

And here?

@Horiodino
Copy link
Contributor Author

Horiodino commented Mar 23, 2025

@Horiodino try upgrading Ginkgo to v2.23.3. It looks like they fixed this bug.

GinkgoTB().Context() is picking up a different instance of Context()—one that’s valid at compile time but panics at runtime due to the interface embedding. if you run ctx = ginkgo.GinkgoT().Context() it will run .


  [PANICKED] Test Panicked
  In [BeforeSuite] at: /usr/lib/go/src/runtime/panic.go:262 @ 03/23/25 19:21:24.159

  runtime error: invalid memory address or nil pointer dereference

  Full Stack Trace
    sigs.k8s.io/kueue/test/integration/framework.(*Framework).SetupClient(0xc00030e0e0, 0xc000218908)
    	/home/yoi/miku/kueue-ctx/test/integration/framework/framework.go:136 +0x7d7
    sigs.k8s.io/kueue/test/integration/singlecluster/controller/admissionchecks/provisioning.init.func2()
    	/home/yoi/miku/kueue-ctx/test/integration/singlecluster/controller/admissionchecks/provisioning/suite_test.go:63 +0x26e
------------------------------
[BeforeSuite] [PANICKED] [10.206 seconds]
[BeforeSuite] 

ginkgo version
Ginkgo Version 2.23.3

Verified

This commit was signed with the committer’s verified signature.
Horiodino Praful Khanduri
Signed-off-by: Horiodino <[email protected]>
@mbobrovskyi
Copy link
Contributor

GinkgoTB().Context() is picking up a different instance of Context()—one that’s valid at compile time but panics at runtime due to the interface embedding. if you run ctx = ginkgo.GinkgoT().Context() it will run .

It's very strange - locally, v2.23.3 works fine with ginkgo.GinkgoTB(). But okay, ginkgo.GinkgoT() is good.

Copy link
Contributor

@mbobrovskyi mbobrovskyi left a comment

Choose a reason for hiding this comment

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

/lgtm

Awesome! Thank you so much!

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Mar 23, 2025
@k8s-ci-robot
Copy link
Contributor

LGTM label has been added.

Git tree hash: 3e5c2c38a5c675b3ce77eb4e34d08230eda9c675

@mimowo
Copy link
Contributor

mimowo commented Mar 24, 2025

/approve
Thanks!

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: Horiodino, mbobrovskyi, mimowo

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Mar 24, 2025
@k8s-ci-robot k8s-ci-robot merged commit e8c167e into kubernetes-sigs:main Mar 24, 2025
19 of 21 checks passed
@k8s-ci-robot k8s-ci-robot added this to the v0.12 milestone Mar 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. lgtm "Looks good to me", indicates that a PR is ready to be merged. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. release-note-none Denotes a PR that doesn't merit a release note. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Replace context.Background() with testing.TB.Context() in tests
4 participants