// Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 package sshcheckreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/sshcheckreceiver" import ( "context" "testing" "time" "github.com/stretchr/testify/require" "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/consumer/consumertest" "go.opentelemetry.io/collector/receiver/receivertest" "go.opentelemetry.io/collector/receiver/scraperhelper" "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/sshcheckreceiver/internal/configssh" "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/sshcheckreceiver/internal/metadata" ) func TestNewFactory(t *testing.T) { if !supportedOS() { t.Skip("Skip tests if not running on one of: [linux, darwin, freebsd, openbsd]") } t.Parallel() testCases := []struct { desc string testFunc func(*testing.T) }{ { desc: "creates a new factory with correct type", testFunc: func(t *testing.T) { factory := NewFactory() require.EqualValues(t, metadata.Type, factory.Type()) }, }, { desc: "creates a new factory with default config", testFunc: func(t *testing.T) { factory := NewFactory() var expectedCfg component.Config = &Config{ ScraperControllerSettings: scraperhelper.ScraperControllerSettings{ CollectionInterval: 10 * time.Second, InitialDelay: time.Second, }, SSHClientSettings: configssh.SSHClientSettings{ Timeout: 10 * time.Second, }, MetricsBuilderConfig: metadata.DefaultMetricsBuilderConfig(), } require.Equal(t, expectedCfg, factory.CreateDefaultConfig()) }, }, { desc: "creates a new factory and CreateMetricsReceiver returns no error", testFunc: func(t *testing.T) { factory := NewFactory() cfg := factory.CreateDefaultConfig() _, err := factory.CreateMetricsReceiver( context.Background(), receivertest.NewNopCreateSettings(), cfg, consumertest.NewNop(), ) require.NoError(t, err) }, }, { desc: "creates a new factory and CreateMetricsReceiver returns error with incorrect config", testFunc: func(t *testing.T) { factory := NewFactory() _, err := factory.CreateMetricsReceiver( context.Background(), receivertest.NewNopCreateSettings(), nil, consumertest.NewNop(), ) require.ErrorIs(t, err, errConfigNotSSHCheck) }, }, } for _, tc := range testCases { t.Run(tc.desc, tc.testFunc) } } func TestWindowsReceiverUnsupported(t *testing.T) { if supportedOS() { t.Skip("Skip test if not running windows.") } factory := NewFactory() _, err := factory.CreateMetricsReceiver( context.Background(), receivertest.NewNopCreateSettings(), nil, consumertest.NewNop(), ) require.ErrorIs(t, err, errWindowsUnsupported) }