-
Notifications
You must be signed in to change notification settings - Fork 48
Add TempDir and NewLogFile test utilities #369
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
base: main
Are you sure you want to change the base?
Conversation
| // | ||
| // When tests are run with -v, the temporary directory absolute | ||
| // path will be logged. | ||
| func TempDir(t *testing.T, path ...string) string { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Q: what's the difference with the stdlib t tmpdir ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TempDir creates a folder that when the test fails is kept and the path logged. This allows us to debug the state the test was running both locally and on CI.
We have number of tests in Beats and Elastic Agent that create configuration files, logs to be ingested, etc. When those tests fail and all state is gone, debugging becomes extremely hard.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have similar functionality spread across many tests/test frameworks in Beats and Elastic Agent, I'm trying to consolidate them.
I've heavily needed and relayed on this functionality in the past couple of weeks to debug a number of tests, after duplicating the code in a few PRs I realised I needed it in a central place, with a single implementation.
| // | ||
| // When tests are run with -v, the temporary directory absolute | ||
| // path will be logged. | ||
| func TempDir(t *testing.T, path ...string) string { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not a fan of copying so much of the code that exists in t.TempDir(). Can we use t.TempDir and then just have the cleanup function copy or archive the contents in the case of a failure?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Personally I like that the temp dir is already created on its final destination, so no matter how wrong the test goes, the files are kept.
Copying/archiving the temp dir after a test failure adds another point of failure and complexity that I'd like to avoid.
I'm not a fan of copying so much of the code that exists in t.TempDir()
I haven't copied much of it, well, the bit I copied was only the part sanitising the test name to create the folder.
| // subsequent call to WaitLogsContains will only check logs not yet evaluated. | ||
| // msgAndArgs should be a format string and arguments that will be printed | ||
| // if the logs are not found, providing additional context for debugging. | ||
| func (l *LogFile) WaitLogsContains(t testing.TB, s string, timeout time.Duration, msgAndArgs ...any) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this the same as the beats repo?
https://github.com/elastic/beats/blob/main/libbeat/tests/integration/framework.go
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I've needed it in different places, like Elastic Agent. So this PR is a first attempt to centralise some helpers in a single place.
Co-authored-by: Anderson Queiroz <[email protected]>
💚 Build Succeeded
History
cc @belimawr |
|
@leehinman @kruskall did you folks have a chance to see my replies to your comments? |
What does this PR do?
This PR adds two helpers for tests:
fs.TempDircreates a temporary folder that is kept when tests fails.NewLogFilecreates a file that can be used as output for a logger or a a process. Methods to search strings in the file are also providedWhy is it important?
It helps us to keep temporary files and logs generated by tests when the test fails for easier debugging.
Checklist
## Author's Checklist## Related issues