-
Notifications
You must be signed in to change notification settings - Fork 66
[*] migrate all helpers to testutil
#1071
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: master
Are you sure you want to change the base?
Conversation
Pull Request Test Coverage Report for Build 20036744139Details
💛 - Coveralls |
The setup function should be invoked once across all tests because it setups a global port listener, otherwise running tests parallely may fail with `address already in use` error.
| type mockReader struct { | ||
| toReturn sources.Sources | ||
| toErr error | ||
| } | ||
|
|
||
| func (m *mockReader) GetSources() (sources.Sources, error) { | ||
| if m.toErr != nil { | ||
| return nil, m.toErr | ||
| } | ||
| return m.toReturn, nil | ||
| } | ||
|
|
||
| func (m *mockReader) WriteSources(sources.Sources) error { return nil } | ||
| func (m *mockReader) DeleteSource(string) error { return nil } | ||
| func (m *mockReader) UpdateSource(sources.Source) error { return nil } | ||
| func (m *mockReader) CreateSource(sources.Source) error { return nil } | ||
|
|
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.
Revert this. testutil must contain only structs and functions that are used in more than one place.
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.
This sources mock was removed and I used the general sources mock defined previously in webserver (moved to testutil) instead.
refer to #1071 (comment)
internal/testutil/mocks.go
Outdated
| //---------------Sources-Metrics Mocks-------------- | ||
|
|
||
| type MockMetricsReaderWriter struct { | ||
| GetMetricsFunc func() (*metrics.Metrics, error) | ||
| UpdateMetricFunc func(name string, m metrics.Metric) error | ||
| CreateMetricFunc func(name string, m metrics.Metric) error | ||
| DeleteMetricFunc func(name string) error | ||
| DeletePresetFunc func(name string) error | ||
| UpdatePresetFunc func(name string, preset metrics.Preset) error | ||
| CreatePresetFunc func(name string, preset metrics.Preset) error | ||
| WriteMetricsFunc func(metricDefs *metrics.Metrics) error | ||
| } | ||
|
|
||
| func (m *MockMetricsReaderWriter) GetMetrics() (*metrics.Metrics, error) { | ||
| return m.GetMetricsFunc() | ||
| } | ||
| func (m *MockMetricsReaderWriter) UpdateMetric(name string, metric metrics.Metric) error { | ||
| return m.UpdateMetricFunc(name, metric) | ||
| } | ||
| func (m *MockMetricsReaderWriter) CreateMetric(name string, metric metrics.Metric) error { | ||
| return m.CreateMetricFunc(name, metric) | ||
| } | ||
| func (m *MockMetricsReaderWriter) DeleteMetric(name string) error { | ||
| return m.DeleteMetricFunc(name) | ||
| } | ||
| func (m *MockMetricsReaderWriter) DeletePreset(name string) error { | ||
| return m.DeletePresetFunc(name) | ||
| } | ||
| func (m *MockMetricsReaderWriter) UpdatePreset(name string, preset metrics.Preset) error { | ||
| return m.UpdatePresetFunc(name, preset) | ||
| } | ||
| func (m *MockMetricsReaderWriter) CreatePreset(name string, preset metrics.Preset) error { | ||
| return m.CreatePresetFunc(name, preset) | ||
| } | ||
| func (m *MockMetricsReaderWriter) WriteMetrics(metricDefs *metrics.Metrics) error { | ||
| return m.WriteMetricsFunc(metricDefs) | ||
| } | ||
|
|
||
| type MockSourcesReaderWriter struct { | ||
| GetSourcesFunc func() (sources.Sources, error) | ||
| UpdateSourceFunc func(md sources.Source) error | ||
| CreateSourceFunc func(md sources.Source) error | ||
| DeleteSourceFunc func(name string) error | ||
| WriteSourcesFunc func(sources.Sources) error | ||
| } | ||
|
|
||
| func (m *MockSourcesReaderWriter) GetSources() (sources.Sources, error) { | ||
| return m.GetSourcesFunc() | ||
| } | ||
| func (m *MockSourcesReaderWriter) UpdateSource(md sources.Source) error { | ||
| return m.UpdateSourceFunc(md) | ||
| } | ||
| func (m *MockSourcesReaderWriter) CreateSource(md sources.Source) error { | ||
| return m.CreateSourceFunc(md) | ||
| } | ||
| func (m *MockSourcesReaderWriter) DeleteSource(name string) error { | ||
| return m.DeleteSourceFunc(name) | ||
| } | ||
| func (m *MockSourcesReaderWriter) WriteSources(srcs sources.Sources) error { | ||
| return m.WriteSourcesFunc(srcs) | ||
| } | ||
|
|
||
| type MockFS struct { | ||
| OpenFunc func(name string) (fs.File, error) | ||
| } | ||
|
|
||
| func (m MockFS) Open(name string) (fs.File, error) { | ||
| return m.OpenFunc(name) | ||
| } |
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.
Revert this. testutil must contain only structs and functions that are used in more than one place.
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.
- Reverted
MockFS. MockSourcesReaderWriteris already used in multiple places as mentioned in [*] migrate all helpers totestutil#1071 (comment),- For
mockMetricsReaderWriter, I think it should stay, as it's very probable that it will get used in the future in packages other thanwebserver.
Now in this PR we have only migrated container helpers, and these Mock{Metrics,Sources}ReaderWriter mocks.
| type mockSourcesReaderWriter struct { | ||
| GetSourcesFunc func() (sources.Sources, error) | ||
| UpdateSourceFunc func(md sources.Source) error | ||
| CreateSourceFunc func(md sources.Source) error | ||
| DeleteSourceFunc func(name string) error | ||
| WriteSourcesFunc func(sources.Sources) error | ||
| } | ||
|
|
||
| func (m *mockSourcesReaderWriter) GetSources() (sources.Sources, error) { | ||
| return m.GetSourcesFunc() | ||
| } | ||
| func (m *mockSourcesReaderWriter) UpdateSource(md sources.Source) error { | ||
| return m.UpdateSourceFunc(md) | ||
| } | ||
| func (m *mockSourcesReaderWriter) CreateSource(md sources.Source) error { | ||
| return m.CreateSourceFunc(md) | ||
| } | ||
| func (m *mockSourcesReaderWriter) DeleteSource(name string) error { | ||
| return m.DeleteSourceFunc(name) | ||
| } | ||
| func (m *mockSourcesReaderWriter) WriteSources(srcs sources.Sources) error { | ||
| return m.WriteSourcesFunc(srcs) | ||
| } | ||
|
|
||
| func newTestSourceServer(mrw *mockSourcesReaderWriter) *WebUIServer { |
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.
Revert this. testutil must contain only structs and functions that are used in more than one place.
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.
mockSourcesReaderWriter is already used in more than one place. If you observe the changes at reaper_test.go, you will see that I reused it there and removed the other sources mock that existed
|
It's probably easier to start a new PR. I propose to focus only on docker container helpers first. |
27e6d46 to
9fd0919
Compare
testutilpackage #1064 and migrates all remaining helpers/mocks totestutilpackage.TODO:
SetupRPCServers()in tests multiple times.