123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268 |
- // Copyright The OpenTelemetry Authors
- // SPDX-License-Identifier: Apache-2.0
- package snmpreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/snmpreceiver"
- import (
- "context"
- "testing"
- "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/snmpreceiver/internal/metadata"
- )
- func TestNewFactory(t *testing.T) {
- 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 valid default config",
- testFunc: func(t *testing.T) {
- factory := NewFactory()
- var expectedCfg component.Config = &Config{
- ScraperControllerSettings: scraperhelper.ScraperControllerSettings{
- CollectionInterval: defaultCollectionInterval,
- Timeout: defaultTimeout,
- },
- Endpoint: defaultEndpoint,
- Version: defaultVersion,
- Community: defaultCommunity,
- SecurityLevel: "no_auth_no_priv",
- AuthType: "MD5",
- PrivacyType: "DES",
- }
- 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()
- snmpCfg := cfg.(*Config)
- snmpCfg.Metrics = map[string]*MetricConfig{
- "m1": {
- Unit: "1",
- Gauge: &GaugeMetric{ValueType: "int"},
- ScalarOIDs: []ScalarOID{{
- OID: ".1",
- }},
- },
- }
- _, 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, errConfigNotSNMP)
- },
- },
- {
- desc: "CreateMetricsReceiver adds missing scheme to endpoint",
- testFunc: func(t *testing.T) {
- factory := NewFactory()
- cfg := factory.CreateDefaultConfig()
- snmpCfg := cfg.(*Config)
- snmpCfg.Endpoint = "localhost:161"
- snmpCfg.Metrics = map[string]*MetricConfig{
- "m1": {
- Unit: "1",
- Gauge: &GaugeMetric{ValueType: "int"},
- ScalarOIDs: []ScalarOID{{
- OID: ".1",
- }},
- },
- }
- _, err := factory.CreateMetricsReceiver(
- context.Background(),
- receivertest.NewNopCreateSettings(),
- cfg,
- consumertest.NewNop(),
- )
- require.NoError(t, err)
- require.Equal(t, "udp://localhost:161", snmpCfg.Endpoint)
- },
- },
- {
- desc: "CreateMetricsReceiver adds missing port to endpoint",
- testFunc: func(t *testing.T) {
- factory := NewFactory()
- cfg := factory.CreateDefaultConfig()
- snmpCfg := cfg.(*Config)
- snmpCfg.Endpoint = "udp://localhost"
- snmpCfg.Metrics = map[string]*MetricConfig{
- "m1": {
- Unit: "1",
- Gauge: &GaugeMetric{ValueType: "int"},
- ScalarOIDs: []ScalarOID{{
- OID: ".1",
- }},
- },
- }
- _, err := factory.CreateMetricsReceiver(
- context.Background(),
- receivertest.NewNopCreateSettings(),
- cfg,
- consumertest.NewNop(),
- )
- require.NoError(t, err)
- require.Equal(t, "udp://localhost:161", snmpCfg.Endpoint)
- },
- },
- {
- desc: "CreateMetricsReceiver adds missing port to endpoint with trailing colon",
- testFunc: func(t *testing.T) {
- factory := NewFactory()
- cfg := factory.CreateDefaultConfig()
- snmpCfg := cfg.(*Config)
- snmpCfg.Endpoint = "udp://localhost:"
- snmpCfg.Metrics = map[string]*MetricConfig{
- "m1": {
- Unit: "1",
- Gauge: &GaugeMetric{ValueType: "int"},
- ScalarOIDs: []ScalarOID{{
- OID: ".1",
- }},
- },
- }
- _, err := factory.CreateMetricsReceiver(
- context.Background(),
- receivertest.NewNopCreateSettings(),
- cfg,
- consumertest.NewNop(),
- )
- require.NoError(t, err)
- require.Equal(t, "udp://localhost:161", snmpCfg.Endpoint)
- },
- },
- {
- desc: "CreateMetricsReceiver adds missing metric gauge value type as double",
- testFunc: func(t *testing.T) {
- factory := NewFactory()
- cfg := factory.CreateDefaultConfig()
- snmpCfg := cfg.(*Config)
- snmpCfg.Metrics = map[string]*MetricConfig{
- "m1": {
- Gauge: &GaugeMetric{},
- ScalarOIDs: []ScalarOID{{
- OID: ".1",
- }},
- },
- }
- _, err := factory.CreateMetricsReceiver(
- context.Background(),
- receivertest.NewNopCreateSettings(),
- cfg,
- consumertest.NewNop(),
- )
- require.NoError(t, err)
- require.Equal(t, "double", snmpCfg.Metrics["m1"].Gauge.ValueType)
- },
- },
- {
- desc: "CreateMetricsReceiver adds missing metric sum value type as double",
- testFunc: func(t *testing.T) {
- factory := NewFactory()
- cfg := factory.CreateDefaultConfig()
- snmpCfg := cfg.(*Config)
- snmpCfg.Metrics = map[string]*MetricConfig{
- "m1": {
- Sum: &SumMetric{},
- ScalarOIDs: []ScalarOID{{
- OID: ".1",
- }},
- },
- }
- _, err := factory.CreateMetricsReceiver(
- context.Background(),
- receivertest.NewNopCreateSettings(),
- cfg,
- consumertest.NewNop(),
- )
- require.NoError(t, err)
- require.Equal(t, "double", snmpCfg.Metrics["m1"].Sum.ValueType)
- },
- },
- {
- desc: "CreateMetricsReceiver adds missing metric sum aggregation as cumulative",
- testFunc: func(t *testing.T) {
- factory := NewFactory()
- cfg := factory.CreateDefaultConfig()
- snmpCfg := cfg.(*Config)
- snmpCfg.Metrics = map[string]*MetricConfig{
- "m1": {
- Sum: &SumMetric{},
- ScalarOIDs: []ScalarOID{{
- OID: ".1",
- }},
- },
- }
- _, err := factory.CreateMetricsReceiver(
- context.Background(),
- receivertest.NewNopCreateSettings(),
- cfg,
- consumertest.NewNop(),
- )
- require.NoError(t, err)
- require.Equal(t, "cumulative", snmpCfg.Metrics["m1"].Sum.Aggregation)
- },
- },
- {
- desc: "CreateMetricsReceiver adds missing metric unit as 1",
- testFunc: func(t *testing.T) {
- factory := NewFactory()
- cfg := factory.CreateDefaultConfig()
- snmpCfg := cfg.(*Config)
- snmpCfg.Metrics = map[string]*MetricConfig{
- "m1": {
- Gauge: &GaugeMetric{ValueType: "int"},
- ScalarOIDs: []ScalarOID{{
- OID: ".1",
- }},
- },
- }
- _, err := factory.CreateMetricsReceiver(
- context.Background(),
- receivertest.NewNopCreateSettings(),
- cfg,
- consumertest.NewNop(),
- )
- require.NoError(t, err)
- require.Equal(t, "1", snmpCfg.Metrics["m1"].Unit)
- },
- },
- }
- for _, tc := range testCases {
- t.Run(tc.desc, tc.testFunc)
- }
- }
|