Skip to content

Commit

Permalink
[chore] small fixes in hostmetrics, no need of using mocks
Browse files Browse the repository at this point in the history
Signed-off-by: Bogdan Drutu <bogdandrutu@gmail.com>
  • Loading branch information
bogdandrutu committed Nov 20, 2024
1 parent 522cc27 commit 149ad61
Showing 1 changed file with 2 additions and 13 deletions.
15 changes: 2 additions & 13 deletions receiver/hostmetricsreceiver/hostmetrics_receiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"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"
Expand Down Expand Up @@ -222,24 +221,14 @@ func (m *mockConfig) SetRootPath(_ string) {}

func (m *mockConfig) SetEnvMap(_ common.EnvMap) {}

type (
mockFactory struct{ mock.Mock }
mockScraper struct{ mock.Mock }
)
type mockFactory 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)
}

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 TestGatherMetrics_ScraperKeyConfigError(t *testing.T) {
tmp := scraperFactories
scraperFactories = map[string]internal.ScraperFactory{}
Expand All @@ -255,7 +244,7 @@ func TestGatherMetrics_ScraperKeyConfigError(t *testing.T) {

func TestGatherMetrics_CreateMetricsScraperError(t *testing.T) {
mFactory := &mockFactory{}
mFactory.On("CreateMetricsScraper").Return(&mockScraper{}, errors.New("err1"))
mFactory.On("CreateMetricsScraper").Return(nil, errors.New("err1"))
tmp := scraperFactories
scraperFactories = map[string]internal.ScraperFactory{mockTypeStr: mFactory}
defer func() {
Expand Down

0 comments on commit 149ad61

Please sign in to comment.