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

*: force forbiding to use mock.NewContext in production code #56654

Merged
merged 3 commits into from
Oct 17, 2024

Conversation

lcwangchao
Copy link
Collaborator

What problem does this PR solve?

Issue Number: close #53388

What changed and how does it work?

Add checks to avoid to use mock.NewContext in production code.

  1. Move the function mock.NewContext to a standalone file which has a //go:build !codes header to avoid to build this function in production code. So if some one uses mock.NewContext in some production code, make server will fail.
  2. Introduce a function mock.NewContextDeprecated and add a // Deprecated comment for it. This method should only be used in some legacy code.
  3. Remove mock.NewContext usage in other production codes.

Check List

Tests

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
  • No need to test
    • I checked and no code files have been changed.

Side effects

  • Performance regression: Consumes more CPU
  • Performance regression: Consumes more Memory
  • Breaking backward compatibility

Documentation

  • Affects user behaviors
  • Contains syntax changes
  • Contains variable changes
  • Contains experimental features
  • Changes MySQL compatibility

Release note

Please refer to Release Notes Language Style Guide to write a quality release note.

None

@ti-chi-bot ti-chi-bot bot added 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. sig/planner SIG: Planner labels Oct 15, 2024
Copy link

tiprow bot commented Oct 15, 2024

Hi @lcwangchao. Thanks for your PR.

PRs from untrusted users cannot be marked as trusted with /ok-to-test in this repo meaning untrusted PR authors can never trigger tests themselves. Collaborators can still trigger tests on the PR using /test all.

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.

@lcwangchao lcwangchao changed the title *: add some checks to forbid to use mock.Context in release code *: force to forbid using mock.NewContext in production code Oct 15, 2024
Copy link

codecov bot commented Oct 15, 2024

Codecov Report

Attention: Patch coverage is 77.41935% with 7 lines in your changes missing coverage. Please review.

Project coverage is 57.5422%. Comparing base (e020f6a) to head (066dee6).
Report is 33 commits behind head on master.

Additional details and impacted files
@@                Coverage Diff                @@
##             master     #56654         +/-   ##
=================================================
- Coverage   73.3452%   57.5422%   -15.8030%     
=================================================
  Files          1628       1779        +151     
  Lines        449555     645030     +195475     
=================================================
+ Hits         329727     371165      +41438     
- Misses        99598     248838     +149240     
- Partials      20230      25027       +4797     
Flag Coverage Δ
integration 39.6996% <58.0645%> (?)
unit 72.9072% <77.4193%> (+0.4535%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
dumpling 52.9478% <ø> (ø)
parser ∅ <ø> (∅)
br 63.1008% <ø> (+17.4930%) ⬆️

@lcwangchao lcwangchao changed the title *: force to forbid using mock.NewContext in production code *: force forbiding to use mock.NewContext in production code Oct 15, 2024
// RestrictedSQLExecutorKey is the key to represent MockRestrictedSQLExecutorMockRecorder in ctx.
type RestrictedSQLExecutorKey struct{}

// String implements the string.Stringer interface.
func (k RestrictedSQLExecutorKey) String() string {
return "__MockRestrictedSQLExecutor"
}

// WrapAsSCtx wraps the MockRestrictedSQLExecutor into sessionctx.Context.
func WrapAsSCtx(exec *MockRestrictedSQLExecutor) sessionctx.Context {
Copy link
Collaborator Author

@lcwangchao lcwangchao Oct 15, 2024

Choose a reason for hiding this comment

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

@lcwangchao
Copy link
Collaborator Author

/retest

Copy link

tiprow bot commented Oct 15, 2024

@lcwangchao: Cannot trigger testing until a trusted user reviews the PR and leaves an /ok-to-test message.

In response to this:

/retest

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.

Copy link
Member

@YangKeao YangKeao left a comment

Choose a reason for hiding this comment

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

LGTM

@ti-chi-bot ti-chi-bot bot added the needs-1-more-lgtm Indicates a PR needs 1 more LGTM. label Oct 15, 2024
@lcwangchao
Copy link
Collaborator Author

/retest

Copy link

tiprow bot commented Oct 15, 2024

@lcwangchao: Cannot trigger testing until a trusted user reviews the PR and leaves an /ok-to-test message.

In response to this:

/retest

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.

@lcwangchao
Copy link
Collaborator Author

/retest

Copy link

tiprow bot commented Oct 15, 2024

@lcwangchao: Cannot trigger testing until a trusted user reviews the PR and leaves an /ok-to-test message.

In response to this:

/retest

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.

@lcwangchao
Copy link
Collaborator Author

/retest

Copy link

tiprow bot commented Oct 15, 2024

@lcwangchao: Cannot trigger testing until a trusted user reviews the PR and leaves an /ok-to-test message.

In response to this:

/retest

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.

@lcwangchao
Copy link
Collaborator Author

/retest

Copy link

tiprow bot commented Oct 15, 2024

@lcwangchao: Cannot trigger testing until a trusted user reviews the PR and leaves an /ok-to-test message.

In response to this:

/retest

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.

@ti-chi-bot ti-chi-bot bot added lgtm and removed needs-1-more-lgtm Indicates a PR needs 1 more LGTM. labels Oct 15, 2024
Copy link

ti-chi-bot bot commented Oct 15, 2024

[LGTM Timeline notifier]

Timeline:

  • 2024-10-15 08:45:24.740238811 +0000 UTC m=+347121.889148643: ☑️ agreed by YangKeao.
  • 2024-10-15 16:08:58.177574808 +0000 UTC m=+373735.326484640: ☑️ agreed by CbcWestwolf.

Makefile Show resolved Hide resolved
@lcwangchao
Copy link
Collaborator Author

/retest

Copy link

tiprow bot commented Oct 16, 2024

@lcwangchao: Cannot trigger testing until a trusted user reviews the PR and leaves an /ok-to-test message.

In response to this:

/retest

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.

Copy link

ti-chi-bot bot commented Oct 16, 2024

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: CbcWestwolf, hawkingrei, tangenta, YangKeao

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

@ti-chi-bot ti-chi-bot bot added the approved label Oct 16, 2024
@lcwangchao
Copy link
Collaborator Author

/retest

Copy link

tiprow bot commented Oct 17, 2024

@lcwangchao: Cannot trigger testing until a trusted user reviews the PR and leaves an /ok-to-test message.

In response to this:

/retest

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.

@ti-chi-bot ti-chi-bot bot merged commit 1052a55 into pingcap:master Oct 17, 2024
24 checks passed
@lcwangchao lcwangchao deleted the forbidmockctx branch October 17, 2024 02:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved lgtm release-note-none Denotes a PR that doesn't merit a release note. sig/planner SIG: Planner 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.

remove mock.Context usage in production code
5 participants