Skip to content
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

[chore] small fixes in hostmetrics, no need of using mocks #36476

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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