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

🐛 Ensure the environment.InitFlags() is called to avoid confusion. #269

Closed

Conversation

cardil
Copy link
Contributor

@cardil cardil commented Dec 23, 2021

Changes

  • 🐛 Ensure the flags are valid

/kind bug

Fixes #268

Release Note

- :bug: Ensure the flags are valid

Also, moving code to TestMain() to avoid side effects, while importing
a package.
@knative-prow-robot knative-prow-robot added kind/bug Categorizes issue or PR as related to a bug. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Dec 23, 2021
@cardil
Copy link
Contributor Author

cardil commented Jan 4, 2022

/retest

@cardil
Copy link
Contributor Author

cardil commented Jan 11, 2022

/assign @dprotaso
/assign @julz
/assign @nak3
/assign @devguyio
/assign @lionelvillard
/assign @matzew
/assign @rhuss
/assign @chizhg

pkg/feature/level.go Outdated Show resolved Hide resolved
@@ -38,13 +38,11 @@ import (
// config as well as the parsing level and state flags.
var global environment.GlobalEnvironment

func init() {
// TestMain is the first entry point for `go test`.
func TestMain(m *testing.M) {
// environment.InitFlags registers state and level filter flags.
environment.InitFlags(flag.CommandLine)
Copy link
Contributor

@dprotaso dprotaso Jan 11, 2022

Choose a reason for hiding this comment

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

I'm surprised InitFlags isn't instanced - ie. we use local variable vs. package ones global.InitFlags

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can you elaborate on this? I do not follow...

Copy link
Contributor

@dprotaso dprotaso Jan 11, 2022

Choose a reason for hiding this comment

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

InitFlags is initializing package global variables in reconciler-test

When it could simply just modify values on the declared global environment in the example. At least reconciler-test vars aren't exposed publicly so maybe that's ok. 🤷

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. I agree such refactor would be beneficial. Mind opening an issue to track it?

@knative-prow-robot knative-prow-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Jan 11, 2022
@cardil cardil requested a review from dprotaso January 11, 2022 19:46
@dprotaso
Copy link
Contributor

there's a conflict with HEAD

/lgtm
/approve

@knative-prow-robot knative-prow-robot added the lgtm Indicates that a PR is ready to be merged. label Jan 13, 2022
@knative-prow-robot
Copy link

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: cardil, dprotaso

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

@knative-prow-robot knative-prow-robot added needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. approved Indicates a PR has been approved by an approver from all required OWNERS files. labels Jan 13, 2022
@chizhg
Copy link

chizhg commented Mar 23, 2022

/retest

…-flags

Conflicts fixed:

 * README.md
 * pkg/feature/level.go
 * pkg/feature/states.go
 * test/e2e/main_test.go
 * test/example/main_test.go
@knative-prow knative-prow bot removed the lgtm Indicates that a PR is ready to be merged. label Jun 22, 2022
@knative-prow
Copy link

knative-prow bot commented Jun 22, 2022

New changes are detected. LGTM label has been removed.

@knative-prow-robot knative-prow-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jun 22, 2022
@knative-prow
Copy link

knative-prow bot commented Jun 22, 2022

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: cardil, dprotaso

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

@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Jun 22, 2022

CLA Signed

The committers listed above are authorized under a signed CLA.

@cardil
Copy link
Contributor Author

cardil commented Jul 6, 2022

@dprotaso Could you slap the PR again?

@knative-prow-robot knative-prow-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jul 23, 2022
@github-actions
Copy link

This Pull Request is stale because it has been open for 90 days with
no activity. It will automatically close after 30 more days of
inactivity. Reopen with /reopen. Mark as fresh by adding the
comment /remove-lifecycle stale.

@github-actions github-actions bot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Oct 22, 2022
@github-actions github-actions bot closed this Nov 21, 2022
@cardil
Copy link
Contributor Author

cardil commented Feb 28, 2023

/reopen
/remove-lifecycle stale

@knative-prow knative-prow bot reopened this Feb 28, 2023
@knative-prow
Copy link

knative-prow bot commented Feb 28, 2023

@cardil: Reopened this PR.

In response to this:

/reopen
/remove-lifecycle stale

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/test-infra repository.

@knative-prow knative-prow bot removed the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Feb 28, 2023
@knative-prow-robot knative-prow-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Feb 28, 2023
@cardil
Copy link
Contributor Author

cardil commented Feb 28, 2023

/cc @pierDipi

@knative-prow knative-prow bot requested a review from pierDipi February 28, 2023 18:52
pkg/environment/magic.go Outdated Show resolved Hide resolved
Comment on lines +62 to +95
// levelPowerset returns all combinations for a given array.
func levelPowerset(set []feature.Levels) (subsets [][]feature.Levels) {
length := uint(len(set))

// Go through all possible combinations of objects
// from 1 (only first object in subset) to 2^length (all objects in subset)
for subsetBits := 1; subsetBits < (1 << length); subsetBits++ {
var subset []feature.Levels

for object := uint(0); object < length; object++ {
// checks if object is contained in subset
// by checking if bit 'object' is set in subsetBits
if (subsetBits>>object)&1 == 1 {
// add object to subset
subset = append(subset, set[object])
}
}
// add subset to subsets
subsets = append(subsets, subset)
}
return subsets
}

func levelBitwiseOr(subsets [][]feature.Levels) []feature.Levels {
levels := make([]feature.Levels, len(subsets))
for i, subset := range subsets {
val := 0
for _, level := range subset {
val |= int(level)
}
levels[i] = feature.Levels(val)
}
return levels
}
Copy link
Member

Choose a reason for hiding this comment

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

This is complex and hard to read code, what's the value of running all combinations when testing the boundaries is just enough?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This code computes the all bitwise combinations of all levels. For example: MUST|MUST_NOT|SHOULD or MAY|MUST or just MUST.

How about I add such doc to the test? WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think this is solved in 7bd94c5

Comment on lines +61 to +93
func statePowerset(set []feature.States) (subsets [][]feature.States) {
length := uint(len(set))

// Go through all possible combinations of objects
// from 1 (only first object in subset) to 2^length (all objects in subset)
for subsetBits := 1; subsetBits < (1 << length); subsetBits++ {
var subset []feature.States

for object := uint(0); object < length; object++ {
// checks if object is contained in subset
// by checking if bit 'object' is set in subsetBits
if (subsetBits>>object)&1 == 1 {
// add object to subset
subset = append(subset, set[object])
}
}
// add subset to subsets
subsets = append(subsets, subset)
}
return subsets
}

func stateBitwiseOr(subsets [][]feature.States) []feature.States {
levels := make([]feature.States, len(subsets))
for i, subset := range subsets {
val := 0
for _, state := range subset {
val |= int(state)
}
levels[i] = feature.States(val)
}
return levels
}
Copy link
Member

Choose a reason for hiding this comment

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

Same here, very hard to read and follow

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think this is solved in 7bd94c5

Comment on lines +82 to +90
// Valid checks if given level is valid (not empty combination of Levels values).
func (l Levels) Valid() bool {
val := uint8(l)
if val == 0 {
return false
}
val &^= uint8(All)
return val == 0
}
Copy link
Member

Choose a reason for hiding this comment

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

I'd like to reduce bit manipulation as much as possible, a simple:


	levels := sets.NewInt32(
		int32(All),
		int32(Must),
		int32(MustNot),
		int32(Should),
		int32(ShouldNot),
		int32(May),
	)
	
	return levels.Has(int32(l))

is much more readable and can go a long way

Same comment for Valid for States

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's not the same. Now, you could use bitwise or to select multiple levels or states.

Copy link
Member

Choose a reason for hiding this comment

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

Isn't this an input from command line? how do you do bitwise from command line?

That suggests that the type is wrong and shouldn't be a single value but rather an array

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No. You could invoke any number of those levels and features. See the code:

https://github.com/knative-sandbox/reconciler-test/blob/0979798b59c810bcfc4cfa4f63f75bb8f5711020/pkg/environment/flags.go#L41-L56

Just call the tests with those flags or states:

-feature.alpha -feature.beta -requirement.must -requirement.should -requirement.may

Copy link
Member

Choose a reason for hiding this comment

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

Still, this suggests that an array to represents Levels and States is better

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's not the point of this PR, isn't?

@cardil cardil requested review from pierDipi and removed request for dprotaso March 2, 2023 11:45
@github-actions
Copy link

github-actions bot commented Jun 2, 2023

This Pull Request is stale because it has been open for 90 days with
no activity. It will automatically close after 30 more days of
inactivity. Reopen with /reopen. Mark as fresh by adding the
comment /remove-lifecycle stale.

@github-actions github-actions bot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Jun 2, 2023
@github-actions github-actions bot closed this Jul 3, 2023
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. kind/bug Categorizes issue or PR as related to a bug. lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet