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

Logcheck: Separate correct usage and migration tests #247

Merged
merged 2 commits into from
Jun 28, 2021

Conversation

umangachapagain
Copy link

What this PR does / why we need it:

logcheck: add flag for migration test

Migration test is done by explicitly passing -migration flag to
logcheck. It runs correctness test but reports an error when
unstructured logging function is used in a migrated package.

Correctness test is run by default to check for correct usage
of structured logging patterns. It reports an error if structured
logging function is used incorrectly. It does not report an error
when structured logging function is not used.

Which issue(s) this PR fixes (optional, in fixes #<issue number>(, fixes #<issue_number>, ...) format, will close the issue(s) when PR gets merged):
Fixes kubernetes/kubernetes/issues/102439

Special notes for your reviewer:

Please confirm that if this PR changes any image versions, then that's the sole change this PR makes.

Release note:

NONE

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Jun 21, 2021
@k8s-ci-robot k8s-ci-robot added the size/S Denotes a PR that changes 10-29 lines, ignoring generated files. label Jun 21, 2021
@umangachapagain
Copy link
Author

/cc @serathius

@@ -27,14 +28,26 @@ import (
"golang.org/x/tools/go/analysis/singlechecker"
)

var logcheckFlags = flag.NewFlagSet("", flag.ExitOnError)

Choose a reason for hiding this comment

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

Let's avoid adding more globals, could you move Analyser and flag registration to functions?
I know globals are simpler, but they create a bad pattern in code. Let me know if you need help with this.

Copy link
Author

Choose a reason for hiding this comment

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

Discussion here: #247 (comment)

@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Jun 28, 2021
}
var (
logcheckFlags = flag.NewFlagSet("", flag.ExitOnError)
allowUnstructured = logcheckFlags.Bool("allow-unstructured", false, allowUnstructuredFlagHelpText)

Choose a reason for hiding this comment

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

Please avoid using global variables.

Copy link
Author

Choose a reason for hiding this comment

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

@serathius I tried finding a way to do this, but it's either really tricky or not possible.

For flags, I had to use global variable as I couldn't find a way to pass it into run() where it's actually used.

For Analyzer, I had to make it global so that anyone can access it from outside. In our case, unit tests can't access it if it's not global and fails.

If you have suggestions on how to do it, I can add those changes to the PR.

Choose a reason for hiding this comment

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

Yea, it's a little harder then I thought at the beginning.

Try this:

type config struct {
	allowUnstructured bool
}

func main() {
	singlechecker.Main(analyser())
}

func analyser() *analysis.Analyzer {
	c := config{}
	logcheckFlags     := flag.NewFlagSet("", flag.ExitOnError)
	logcheckFlags.BoolVar(&c.allowUnstructured, "allow-unstructured", c.allowUnstructured, `suppress error reports when unstructured logging pattern is used`)

	return &analysis.Analyzer{
		Name:  "logcheck",
		Doc:   "Tool to check use of unstructured logging patterns.",
		Run: func(pass *analysis.Pass) (interface{}, error) {
			return run(pass, &c)
		},
		Flags: *logcheckFlags,
	}
}

func run(pass *analysis.Pass, c *config) (interface{}, error) {

Copy link
Author

Choose a reason for hiding this comment

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

This is neat. Made the changes.

By default, logcheck will check for use of unstructured logging
and use of incorrect structured logging pattern, and
report errors.

allow-unstructured flag can be passed to logcheck to suppress
errors when unstructured logging is used. It is usefull in
cases when we want to test for correct use of structured
logging patterns but not enfore it.

Signed-off-by: Umanga Chapagain <chapagainumanga@gmail.com>
Signed-off-by: Umanga Chapagain <chapagainumanga@gmail.com>
@serathius
Copy link

/lgtm
/assign @dims

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jun 28, 2021
@serathius
Copy link

/assign @brancz

@dims
Copy link
Member

dims commented Jun 28, 2021

/approve

@k8s-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: dims, serathius, umangachapagain

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 Jun 28, 2021
@k8s-ci-robot k8s-ci-robot merged commit dfd1411 into kubernetes:main Jun 28, 2021
@umangachapagain umangachapagain deleted the split-tests branch June 28, 2021 12:47
pohly pushed a commit to pohly/klog that referenced this pull request Jun 10, 2022
Logcheck: Separate correct usage and migration tests
pohly pushed a commit to pohly/logtools that referenced this pull request Jun 14, 2022
Logcheck: Separate correct usage and migration tests
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. lgtm "Looks good to me", indicates that a PR is ready to be merged. 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.

Correctness of InfoS usage should be checked on whole codebase
5 participants