// Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 package saphanareceiver import ( "errors" "path/filepath" "testing" "time" "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/stretchr/testify/require" "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/confmap/confmaptest" "go.uber.org/multierr" "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/saphanareceiver/internal/metadata" ) func TestValidate(t *testing.T) { testCases := []struct { desc string defaultConfigModifier func(cfg *Config) expected error }{ { desc: "missing username and password", defaultConfigModifier: func(cfg *Config) {}, expected: multierr.Combine( errors.New(ErrNoUsername), errors.New(ErrNoPassword), ), }, { desc: "missing password", defaultConfigModifier: func(cfg *Config) { cfg.Username = "otel" }, expected: multierr.Combine( errors.New(ErrNoPassword), ), }, { desc: "missing username", defaultConfigModifier: func(cfg *Config) { cfg.Password = "otel" }, expected: multierr.Combine( errors.New(ErrNoUsername), ), }, { desc: "no error", defaultConfigModifier: func(cfg *Config) { cfg.Username = "otel" cfg.Password = "otel" }, expected: nil, }, } for _, tC := range testCases { t.Run(tC.desc, func(t *testing.T) { factory := NewFactory() cfg := factory.CreateDefaultConfig().(*Config) tC.defaultConfigModifier(cfg) actual := component.ValidateConfig(cfg) require.Equal(t, tC.expected, actual) }) } } func TestLoadConfig(t *testing.T) { cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml")) require.NoError(t, err) factory := NewFactory() cfg := factory.CreateDefaultConfig() sub, err := cm.Sub(component.NewIDWithName(metadata.Type, "").String()) require.NoError(t, err) require.NoError(t, component.UnmarshalConfig(sub, cfg)) expected := factory.CreateDefaultConfig().(*Config) expected.MetricsBuilderConfig = metadata.DefaultMetricsBuilderConfig() expected.MetricsBuilderConfig.Metrics.SaphanaCPUUsed.Enabled = false expected.Endpoint = "example.com:30015" expected.Username = "otel" expected.Password = "password" expected.CollectionInterval = 2 * time.Minute if diff := cmp.Diff(expected, cfg, cmpopts.IgnoreUnexported(metadata.MetricConfig{}), cmpopts.IgnoreUnexported(metadata.ResourceAttributeConfig{})); diff != "" { t.Errorf("Config mismatch (-expected +actual):\n%s", diff) } }