Skip to content

Conversation

@0xgouda
Copy link
Collaborator

@0xgouda 0xgouda commented Dec 5, 2025


TODO:

  • Fix race condition that may originate from using SetupRPCServers() in tests multiple times.
  • Add more tests to increase test coverage.

@coveralls
Copy link

coveralls commented Dec 5, 2025

Pull Request Test Coverage Report for Build 20036744139

Details

  • 64 of 64 (100.0%) changed or added relevant lines in 2 files are covered.
  • 26 unchanged lines in 1 file lost coverage.
  • Overall coverage decreased (-0.2%) to 73.807%

Files with Coverage Reduction New Missed Lines %
internal/testutil/setup.go 26 65.17%
Totals Coverage Status
Change from base Build 20032611416: -0.2%
Covered Lines: 3804
Relevant Lines: 5154

💛 - Coveralls

@0xgouda 0xgouda marked this pull request as draft December 5, 2025 18:38
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.
@0xgouda 0xgouda added enhancement New feature or request test New test case or request labels Dec 8, 2025
@0xgouda 0xgouda self-assigned this Dec 8, 2025
@0xgouda 0xgouda marked this pull request as ready for review December 8, 2025 05:13
@0xgouda 0xgouda requested a review from pashagolub December 8, 2025 05:13
Comment on lines -17 to -33
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 }

Copy link
Collaborator

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.

Copy link
Collaborator Author

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)

Comment on lines 48 to 116
//---------------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)
}
Copy link
Collaborator

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.

Copy link
Collaborator Author

@0xgouda 0xgouda Dec 8, 2025

Choose a reason for hiding this comment

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

  1. Reverted MockFS.
  2. MockSourcesReaderWriter is already used in multiple places as mentioned in [*] migrate all helpers to testutil #1071 (comment),
  3. For mockMetricsReaderWriter, I think it should stay, as it's very probable that it will get used in the future in packages other than webserver.

Now in this PR we have only migrated container helpers, and these Mock{Metrics,Sources}ReaderWriter mocks.

Comment on lines -17 to -41
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 {
Copy link
Collaborator

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.

Copy link
Collaborator Author

@0xgouda 0xgouda Dec 8, 2025

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

@pashagolub
Copy link
Collaborator

It's probably easier to start a new PR. I propose to focus only on docker container helpers first.

@0xgouda 0xgouda force-pushed the migrate-all-helpers-to-testutil branch from 27e6d46 to 9fd0919 Compare December 8, 2025 17:18
@0xgouda 0xgouda requested review from pashagolub December 8, 2025 17:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request test New test case or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants