Skip to content

test: assert cmd.ExecuteContext flushes exit code to logstash#64

Merged
mwbrooks merged 3 commits into
mainfrom
mwbrooks-root-test-logstash
Apr 25, 2025
Merged

test: assert cmd.ExecuteContext flushes exit code to logstash#64
mwbrooks merged 3 commits into
mainfrom
mwbrooks-root-test-logstash

Conversation

@mwbrooks
Copy link
Copy Markdown
Member

Summary

This pull request is a follow-up for #61 (comment) that asserts cmd.ExecuteContext flushes the correct exit code to Logstash.

In order for this to work, we needed to fix the implementation of the EventTrackerMock by adding a few missing functions, writing it up to ClientsMock, and adding default mocks to allow our existing tests to pass.

I think we're at the point where we can test Logstash flushes throughout the code! 🎉

Requirements

@mwbrooks mwbrooks added code health M-T: Test improvements and anything that improves code health semver:patch Use on pull requests to describe the release version increment labels Apr 25, 2025
@mwbrooks mwbrooks added this to the Next Release milestone Apr 25, 2025
@mwbrooks mwbrooks self-assigned this Apr 25, 2025
Comment thread cmd/root_test.go
// Assertions
// TODO: Assert that the event tracker was called with the correct exit code
require.Equal(t, tt.expectedExitCode, clients.IO.GetExitCode())
clientsMock.EventTracker.AssertCalled(t, "FlushToLogstash", mock.Anything, mock.Anything, mock.Anything, tt.expectedExitCode)
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

note: This is the most important part of the PR. We now assert that the correct exit code is flushed to Logstash.

@mwbrooks mwbrooks marked this pull request as ready for review April 25, 2025 03:34
@mwbrooks mwbrooks requested a review from a team as a code owner April 25, 2025 03:34
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 25, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 63.10%. Comparing base (c9a3e57) to head (5128747).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #64      +/-   ##
==========================================
- Coverage   63.12%   63.10%   -0.02%     
==========================================
  Files         210      210              
  Lines       22159    22159              
==========================================
- Hits        13987    13984       -3     
- Misses       7086     7087       +1     
- Partials     1086     1088       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown
Member

@zimeg zimeg left a comment

Choose a reason for hiding this comment

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

@mwbrooks LGTM! 🧪 ✨

I didn't mean for additional refactor needed to test exit codes here, but it's nice to see the improvements to the testing suite.

Commit e35caa8 removed perhaps past flushes from iostreams instead of the event tracker since I don't believe that's used now, but please feel free to revert that as needed. I could not suggest it so quick otherwise 😅

I'm also bummed to see coverage drop from happenchance of the actual test run - IIRC ordering random lists is difficult to make deterministic - but the actual tests are improved so much here! 🚢 💨

Comment thread internal/tracking/tracking_mock.go Outdated
@@ -28,13 +28,32 @@ type EventTrackerMock struct {

func (m *EventTrackerMock) AddDefaultMocks() {
m.On("FlushToLogstash", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
m.On("FlushToLogstash", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil)

🪓 Ignore this suggestion but I'm now wondering if we can be confident that we're setting up all mock implementations within tests for this method, so a default is no longer needed?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Sure, I'm okay with that. We should think about how we want to approach the .DefaultMocks(). I think it saves us a lot of scaffolded code, but sometimes comes off as magical and leads to brittle tests that dependent on the default mocks. 🤔

However, since FlushToLogstash is only used on root.go, I think it's a safe one to always mock explicitly.

Commit 5128747 removes the default mock and declares it explicitly in the root_test.go table test.

Comment on lines -121 to -122

func (m *IOStreamsMock) FlushToLogstash(ctx context.Context) error { return nil }
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

🪓 Removed this implementations since this method no longer exists within iostreams!

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Ah great catch! I noticed that while refactoring, but it slipped from my mind.

@mwbrooks
Copy link
Copy Markdown
Member Author

I didn't mean for additional refactor needed to test exit codes here, but it's nice to see the improvements to the testing suite.

Ha! No worries, this is a healthy refactor, so I'm glad jumped on the opportunity 🤾🏻

Commit e35caa8 removed perhaps past flushes from iostreams instead of the event tracker since I don't believe that's used now, but please feel free to revert that as needed. I could not suggest it so quick otherwise 😅

Totally cool and this is a very good catch! Thanks for pushing up the commit!

I'm also bummed to see coverage drop from happenchance of the actual test run - IIRC ordering random lists is difficult to make deterministic - but the actual tests are improved so much here! 🚢 💨

Yea, it's unfortunate, but overall we're inching toward that 70% mark. There's still some low hanging fruit 🍒 that we can use to increase the coverage more as well!

@mwbrooks mwbrooks merged commit 48cddd3 into main Apr 25, 2025
6 checks passed
@mwbrooks mwbrooks deleted the mwbrooks-root-test-logstash branch April 25, 2025 16:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

code health M-T: Test improvements and anything that improves code health semver:patch Use on pull requests to describe the release version increment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants