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: sort slices before comparing in test #142

Merged
merged 1 commit into from
Sep 18, 2024
Merged

Conversation

catsby
Copy link
Contributor

@catsby catsby commented Sep 18, 2024

Description

The Runner test TestRunner_GetBaseActionCfg/extraEnv_adds_and_overrides_defaults in src/pkg/runner/actions_test.go is flaky because we don't sort the string slices before comparing them, and occasionally they get created in slightly different orders:

=== RUN   TestRunner_GetBaseActionCfg/extraEnv_adds_and_overrides_defaults
    actions_test.go:502:
                Error Trace:    /Users/clint/go/github.com/defenseunicorns/maru-runner/src/pkg/runner/actions_test.go:502
                Error:          Not equal:
                                expected: []string{"ENV1=fromDefault", "ENV2=xyz1", "ENV4=fromSet", "ENV2=alsoFromEnv", "ENV3=fromExtra"}
                                actual  : []string{"ENV1=fromDefault", "ENV2=xyz1", "ENV4=fromSet", "ENV3=fromExtra", "ENV2=alsoFromEnv"}

note the expected "ENV2=alsoFromEnv", "ENV3=fromExtra" vs. the actual "ENV3=fromExtra", "ENV2=alsoFromEnv".

Assuming a successful test run produces this output:

=== RUN   TestRunner_GetBaseActionCfg
=== RUN   TestRunner_GetBaseActionCfg/extraEnv_adds_and_overrides_defaults
--- PASS: TestRunner_GetBaseActionCfg (0.00s)
    --- PASS: TestRunner_GetBaseActionCfg/extraEnv_adds_and_overrides_defaults (0.00s)

If we run the test enough times it will fail, as shown below where we would expect 200 lines of -- PASS here when running the test 100 times (2x per run), and 0 for --- FAIL:

 github.com/defenseunicorns/maru-runner on  main [$]
 ➜ go test ./src/pkg/runner/... -v -run=TestRunner_GetBaseActionCfg/extraEnv_adds_and_overrides_defaults -count=100 | grep '\-\-\- PASS' | wc -l
     142

 github.com/defenseunicorns/maru-runner on  main
 ➜ go test ./src/pkg/runner/... -v -run=TestRunner_GetBaseActionCfg/extraEnv_adds_and_overrides_defaults -count=100 | grep '\-\-\- FAIL' | wc -l
      46

It's flaky, so your numbers will vary each time you run it.

In this PR we sort both slices before comparing.
Here's the updated output:

 github.com/defenseunicorns/maru-runner on  fix-runner-test-sort
 ➜ go test ./src/pkg/runner/... -v -run=TestRunner_GetBaseActionCfg/extraEnv_adds_and_overrides_defaults -count=100 | grep '\-\-\- PASS' | wc -l
     200

 github.com/defenseunicorns/maru-runner on  fix-runner-test-sort
 ➜ go test ./src/pkg/runner/... -v -run=TestRunner_GetBaseActionCfg/extraEnv_adds_and_overrides_defaults -count=100 | grep '\-\-\- FAIL' | wc -l
       0

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Other (security config, docs update, etc)

Checklist before merging

Signed-off-by: catsby <clint@ctshryock.com>
@ericwyles
Copy link
Contributor

Thanks for this!

@catsby catsby changed the title fix: sort slices before comparing in test TestRunner_GetBaseActionCfg/extraEnv_adds_and_overrides_defaults fix: sort slices before comparing in test Sep 18, 2024
@catsby catsby merged commit 77f52c9 into main Sep 18, 2024
8 of 9 checks passed
@catsby catsby deleted the fix-runner-test-sort branch September 18, 2024 21:59
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.

2 participants