12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- // 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)
- }
- }
|