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

test: Diff structured YAML when possible #8432

Merged
merged 1 commit into from
May 10, 2022
Merged

test: Diff structured YAML when possible #8432

merged 1 commit into from
May 10, 2022

Conversation

olix0r
Copy link
Member

@olix0r olix0r commented May 5, 2022

When we compare generated manifests against fixtures, we do a simple
string comparison. The diffed data can be pretty hard to understand.

This change adds a new test helper, DiffTestYAML that parses strings
as arbitrary YAML data structures and uses deep.Equal to generate a
diff of the datastructures.

Now, when a test fails, we'll get output like:

install_test.go:244: YAML mismatches install_output.golden:
	slice[32].map[spec].map[template].map[spec].map[containers].slice[3].map[image]: PolicyControllerImageName:PolicyControllerVersion != SomeOtherImage:PolicyControllerVersion

While testing this, it became apparent that several of our generated
golden files were not actually valid YAML, due to the LinkerdVersion
value being unset. This has been fixed.


I opted to have DiffTestYAML return an error rather than take a *testing.T parameter. When the test utility code invokes t.Error* or t.Fatal*, the error is annotated as originating in the test utility and not in the actual test code. By returning an error that is handled in the test, we get a clearer signal of where the actual test failure occurred.

This also means that commenting and whitespace changes won't be tested (which I think is probably good?)

@olix0r olix0r requested a review from a team as a code owner May 5, 2022 21:32
When we compare generated manifests against fixtures, we do a simple
string comparison to compare output. The diffed data can be pretty hard
to understand.

This change adds a new test helper, `DiffTestYAML` that parses strings
as arbitrary YAML data structures and uses `deep.Equal` to generate a
diff of the datastructures.

Now, when a test fails, we'll get output like:

```
install_test.go:244: YAML mismatches install_output.golden:
	slice[32].map[spec].map[template].map[spec].map[containers].slice[3].map[image]: PolicyControllerImageName:PolicyControllerVersion != SomeOtherImage:PolicyControllerVersion
```

While testing this, it became apparent that several of our generated
golden files were not actually valid YAML, due to the `LinkerdVersion`
value being unset. This has been fixed.

Signed-off-by: Oliver Gould <ver@buoyant.io>
@olix0r olix0r force-pushed the ver/test-yaml-diff branch 2 times, most recently from 0b5f8da to 45b5798 Compare May 5, 2022 23:23
Copy link
Member

@adleong adleong left a comment

Choose a reason for hiding this comment

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

Nice!
If you wanted DiffTestYAML to take a testing.T, you could invoke https://pkg.go.dev/testing#T.Helper so that the comparison stack frame is skipped when reporting errors. But having it just return an error seems fine too.

Copy link
Member

@mateiidavid mateiidavid left a comment

Choose a reason for hiding this comment

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

Really cool!

@olix0r olix0r merged commit 33c1d61 into main May 10, 2022
@olix0r olix0r deleted the ver/test-yaml-diff branch May 10, 2022 15:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants