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

Fix matching objects when listing in test harness #497

Conversation

jbarrick-mesosphere
Copy link
Member

@jbarrick-mesosphere jbarrick-mesosphere commented Jul 3, 2019

What type of PR is this?

/kind bug

What this PR does / why we need it:

When listing objects in the test harness, there was a bug where the for loop variable was appended to the list of objects to compare causing only the last object (alphabetically) in the namespace to be compared when checking if any objects match the assert.

This fixes it by indexing the list instead of taking the address of the loop variable.

Found by @gkleiman :)

Does this PR introduce a user-facing change?:

NONE

When listing objects in the test harness, there was a bug where the a for loop variable was appended to the list of objects
to compare causing only the last object (alphabetically) in the namespace to be compared when checking if any objects match
the assert.

This fixes it by indexing the list instead of taking the address of the loop variable.
@@ -122,8 +122,8 @@ func (s *Step) CheckResource(expected runtime.Object, namespace string) []error

err = s.Client.List(context.TODO(), actual, listOptions...)

for _, item := range actual.Items {
actuals = append(actuals, &item)
for index := range actual.Items {
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe it's too early but I read the description several times and I still feel like these two implementations should do the same thing 🙈

Copy link
Contributor

Choose a reason for hiding this comment

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

They aren’t the same, see this post for some background: https://github.com/golang/go/wiki/CommonMistakes#using-goroutines-on-loop-iterator-variables

Before I took the address of the loop variable which is modified for each iteration of the loop, actuals was a slice of the same pointer over and over.

Now, I take the address of the actual item from the list using the index, so the loop variable isn’t used.

@jbarrick-mesosphere jbarrick-mesosphere merged commit c7bc97b into kudobuilder:master Jul 3, 2019
@jbarrick-mesosphere jbarrick-mesosphere deleted the fix-object-matching branch July 3, 2019 23:20
@jbarrick-mesosphere jbarrick-mesosphere added this to the v0.4.0 milestone Jul 17, 2019
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.

None yet

4 participants