Skip to content

Commit

Permalink
[chore] small fixes in hostmetrics, no need of using mocks (#36476)
Browse files Browse the repository at this point in the history
Signed-off-by: Bogdan Drutu <bogdandrutu@gmail.com>
  • Loading branch information
bogdandrutu authored Nov 25, 2024
1 parent 4144b21 commit 5dcd37e
Showing 1 changed file with 5 additions and 19 deletions.
24 changes: 5 additions & 19 deletions receiver/hostmetricsreceiver/hostmetrics_receiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
Expand All @@ -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() {
Expand Down

0 comments on commit 5dcd37e

Please sign in to comment.