From 5dcd37e2524861b7d7e6ee669ebedf8bb418b305 Mon Sep 17 00:00:00 2001 From: Bogdan Drutu Date: Mon, 25 Nov 2024 07:51:45 -0800 Subject: [PATCH] [chore] small fixes in hostmetrics, no need of using mocks (#36476) Signed-off-by: Bogdan Drutu --- .../hostmetrics_receiver_test.go | 24 ++++--------------- 1 file changed, 5 insertions(+), 19 deletions(-) diff --git a/receiver/hostmetricsreceiver/hostmetrics_receiver_test.go b/receiver/hostmetricsreceiver/hostmetrics_receiver_test.go index 43bc911c8e65..ce1cff1a5cfd 100644 --- a/receiver/hostmetricsreceiver/hostmetrics_receiver_test.go +++ b/receiver/hostmetricsreceiver/hostmetrics_receiver_test.go @@ -12,9 +12,7 @@ import ( "github.com/shirou/gopsutil/v4/common" "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" - "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/component/componenttest" "go.opentelemetry.io/collector/consumer" "go.opentelemetry.io/collector/consumer/consumertest" @@ -222,22 +220,11 @@ func (m *mockConfig) SetRootPath(_ string) {} func (m *mockConfig) SetEnvMap(_ common.EnvMap) {} -type ( - mockFactory struct{ mock.Mock } - mockScraper struct{ mock.Mock } -) - -func (m *mockFactory) CreateDefaultConfig() internal.Config { return &mockConfig{} } -func (m *mockFactory) CreateMetricsScraper(context.Context, receiver.Settings, internal.Config) (scraperhelper.Scraper, error) { - args := m.MethodCalled("CreateMetricsScraper") - return args.Get(0).(scraperhelper.Scraper), args.Error(1) -} +type errFactory struct{} -func (m *mockScraper) ID() component.ID { return component.MustNewID("mock_scraper") } -func (m *mockScraper) Start(context.Context, component.Host) error { return nil } -func (m *mockScraper) Shutdown(context.Context) error { return nil } -func (m *mockScraper) Scrape(context.Context) (pmetric.Metrics, error) { - return pmetric.NewMetrics(), errors.New("err1") +func (m *errFactory) CreateDefaultConfig() internal.Config { return &mockConfig{} } +func (m *errFactory) CreateMetricsScraper(context.Context, receiver.Settings, internal.Config) (scraperhelper.Scraper, error) { + return nil, errors.New("err1") } func TestGatherMetrics_ScraperKeyConfigError(t *testing.T) { @@ -254,8 +241,7 @@ func TestGatherMetrics_ScraperKeyConfigError(t *testing.T) { } func TestGatherMetrics_CreateMetricsScraperError(t *testing.T) { - mFactory := &mockFactory{} - mFactory.On("CreateMetricsScraper").Return(&mockScraper{}, errors.New("err1")) + mFactory := &errFactory{} tmp := scraperFactories scraperFactories = map[string]internal.ScraperFactory{mockTypeStr: mFactory} defer func() {