Skip to content

Commit

Permalink
[receivers] Add missing LoadConfig tests to validate sample configs (o…
Browse files Browse the repository at this point in the history
  • Loading branch information
djaglowski committed Sep 29, 2022
1 parent 672a51a commit 70d6dee
Show file tree
Hide file tree
Showing 13 changed files with 319 additions and 2 deletions.
21 changes: 21 additions & 0 deletions receiver/aerospikereceiver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@
package aerospikereceiver

import (
"path/filepath"
"testing"
"time"

"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/config/configtls"
"go.opentelemetry.io/collector/confmap/confmaptest"
)

func TestValidate(t *testing.T) {
Expand Down Expand Up @@ -130,3 +133,21 @@ func TestValidate(t *testing.T) {
})
}
}

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(config.NewComponentIDWithName(typeStr, "").String())
require.NoError(t, err)
require.NoError(t, config.UnmarshalReceiver(sub, cfg))

expected := factory.CreateDefaultConfig().(*Config)
expected.Endpoint = "localhost:3000"
expected.CollectionInterval = 30 * time.Second

require.Equal(t, expected, cfg)
}
22 changes: 22 additions & 0 deletions receiver/apachereceiver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@
package apachereceiver

import (
"path/filepath"
"testing"
"time"

"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/confmap/confmaptest"
)

func TestValidate(t *testing.T) {
Expand Down Expand Up @@ -85,3 +89,21 @@ func TestValidate(t *testing.T) {
})
}
}

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(config.NewComponentIDWithName(typeStr, "").String())
require.NoError(t, err)
require.NoError(t, config.UnmarshalReceiver(sub, cfg))

expected := factory.CreateDefaultConfig().(*Config)
expected.Endpoint = "http:https://localhost:8080/server-status?auto"
expected.CollectionInterval = 10 * time.Second

require.Equal(t, expected, cfg)
}
24 changes: 24 additions & 0 deletions receiver/couchdbreceiver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@ package couchdbreceiver // import "github.com/open-telemetry/opentelemetry-colle

import (
"fmt"
"path/filepath"
"testing"
"time"

"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/config/confighttp"
"go.opentelemetry.io/collector/confmap/confmaptest"
"go.uber.org/multierr"
)

Expand Down Expand Up @@ -98,3 +102,23 @@ func TestValidate(t *testing.T) {
})
}
}

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(config.NewComponentIDWithName(typeStr, "").String())
require.NoError(t, err)
require.NoError(t, config.UnmarshalReceiver(sub, cfg))

expected := factory.CreateDefaultConfig().(*Config)
expected.Endpoint = "http:https://localhost:5984"
expected.Username = "otelu"
expected.Password = "$COUCHDB_PASSWORD"
expected.CollectionInterval = time.Minute

require.Equal(t, expected, cfg)
}
22 changes: 22 additions & 0 deletions receiver/flinkmetricsreceiver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@ package flinkmetricsreceiver // import "github.com/open-telemetry/opentelemetry-
import (
"errors"
"fmt"
"path/filepath"
"testing"
"time"

"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/config/confighttp"
"go.opentelemetry.io/collector/confmap/confmaptest"
)

func TestValidate(t *testing.T) {
Expand Down Expand Up @@ -61,3 +65,21 @@ func TestValidate(t *testing.T) {
})
}
}

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(config.NewComponentIDWithName(typeStr, "").String())
require.NoError(t, err)
require.NoError(t, config.UnmarshalReceiver(sub, cfg))

expected := factory.CreateDefaultConfig().(*Config)
expected.Endpoint = "http:https://localhost:8081"
expected.CollectionInterval = 10 * time.Second

require.Equal(t, expected, cfg)
}
26 changes: 26 additions & 0 deletions receiver/mongodbreceiver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ import (
"time"

"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/config/confignet"
"go.opentelemetry.io/collector/config/configtls"
"go.opentelemetry.io/collector/confmap/confmaptest"
)

func TestValidate(t *testing.T) {
Expand Down Expand Up @@ -197,3 +199,27 @@ func TestOptionsTLS(t *testing.T) {
opts := cfg.ClientOptions()
require.NotNil(t, opts.TLSConfig)
}

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(config.NewComponentIDWithName(typeStr, "").String())
require.NoError(t, err)
require.NoError(t, config.UnmarshalReceiver(sub, cfg))

expected := factory.CreateDefaultConfig().(*Config)
expected.Hosts = []confignet.NetAddr{
{
Endpoint: "localhost:27017",
},
}
expected.Username = "otel"
expected.Password = "$MONGO_PASSWORD"
expected.CollectionInterval = time.Minute

require.Equal(t, expected, cfg)
}
46 changes: 46 additions & 0 deletions receiver/mysqlreceiver/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http:https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package mysqlreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/mysqlreceiver"

import (
"path/filepath"
"testing"
"time"

"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/confmap/confmaptest"
)

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(config.NewComponentIDWithName(typeStr, "").String())
require.NoError(t, err)
require.NoError(t, config.UnmarshalReceiver(sub, cfg))

expected := factory.CreateDefaultConfig().(*Config)
expected.Endpoint = "localhost:3306"
expected.Username = "otel"
expected.Password = "$MYSQL_PASSWORD"
expected.Database = "otel"
expected.CollectionInterval = 10 * time.Second

require.Equal(t, expected, cfg)
}
25 changes: 25 additions & 0 deletions receiver/nsxtreceiver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@ package nsxtreceiver // import "github.com/open-telemetry/opentelemetry-collecto

import (
"errors"
"path/filepath"
"testing"
"time"

"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/config/confighttp"
"go.opentelemetry.io/collector/confmap/confmaptest"
)

func TestMetricValidation(t *testing.T) {
Expand Down Expand Up @@ -84,3 +88,24 @@ func TestMetricValidation(t *testing.T) {
})
}
}

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(config.NewComponentIDWithName(typeStr, "").String())
require.NoError(t, err)
require.NoError(t, config.UnmarshalReceiver(sub, cfg))

expected := factory.CreateDefaultConfig().(*Config)
expected.Endpoint = "https://nsx-manager-endpoint"
expected.Username = "admin"
expected.Password = "$NSXT_PASSWORD"
expected.TLSSetting.Insecure = true
expected.CollectionInterval = time.Minute

require.Equal(t, expected, cfg)
}
2 changes: 1 addition & 1 deletion receiver/nsxtreceiver/testdata/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ nsxt:
collection_interval: 1m
endpoint: https://nsx-manager-endpoint
username: admin
password: password123
password: $NSXT_PASSWORD
tls:
insecure: true
51 changes: 51 additions & 0 deletions receiver/postgresqlreceiver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@ package postgresqlreceiver
import (
"errors"
"fmt"
"path/filepath"
"testing"
"time"

"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/config/configtls"
"go.opentelemetry.io/collector/confmap/confmaptest"
"go.uber.org/multierr"
)

Expand Down Expand Up @@ -111,3 +116,49 @@ func TestValidate(t *testing.T) {
})
}
}

func TestLoadConfig(t *testing.T) {
cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
require.NoError(t, err)

factory := NewFactory()
cfg := factory.CreateDefaultConfig()

t.Run("postgresql", func(t *testing.T) {
sub, err := cm.Sub(config.NewComponentIDWithName(typeStr, "").String())
require.NoError(t, err)
require.NoError(t, config.UnmarshalReceiver(sub, cfg))

expected := factory.CreateDefaultConfig().(*Config)
expected.Endpoint = "localhost:5432"
expected.Username = "otel"
expected.Password = "$POSTGRESQL_PASSWORD"

require.Equal(t, expected, cfg)
})

t.Run("postgresql/all", func(t *testing.T) {
sub, err := cm.Sub(config.NewComponentIDWithName(typeStr, "all").String())
require.NoError(t, err)
require.NoError(t, config.UnmarshalReceiver(sub, cfg))

expected := factory.CreateDefaultConfig().(*Config)
expected.Endpoint = "localhost:5432"
expected.NetAddr.Transport = "tcp"
expected.Username = "otel"
expected.Password = "$POSTGRESQL_PASSWORD"
expected.Databases = []string{"otel"}
expected.CollectionInterval = 10 * time.Second
expected.TLSClientSetting = configtls.TLSClientSetting{
Insecure: false,
InsecureSkipVerify: false,
TLSSetting: configtls.TLSSetting{
CAFile: "/home/otel/authorities.crt",
CertFile: "/home/otel/mypostgrescert.crt",
KeyFile: "/home/otel/mypostgreskey.key",
},
}

require.Equal(t, expected, cfg)
})
}
2 changes: 1 addition & 1 deletion receiver/postgresqlreceiver/testdata/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ postgresql/all:
collection_interval: 10s
tls:
insecure: false
unsecure_skip_verify: false
insecure_skip_verify: false
ca_file: /home/otel/authorities.crt
cert_file: /home/otel/mypostgrescert.crt
key_file: /home/otel/mypostgreskey.key
24 changes: 24 additions & 0 deletions receiver/rabbitmqreceiver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@ package rabbitmqreceiver // import "github.com/open-telemetry/opentelemetry-coll
import (
"errors"
"fmt"
"path/filepath"
"testing"
"time"

"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/config/confighttp"
"go.opentelemetry.io/collector/confmap/confmaptest"
"go.uber.org/multierr"
)

Expand Down Expand Up @@ -107,3 +111,23 @@ func TestValidate(t *testing.T) {
})
}
}

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(config.NewComponentIDWithName(typeStr, "").String())
require.NoError(t, err)
require.NoError(t, config.UnmarshalReceiver(sub, cfg))

expected := factory.CreateDefaultConfig().(*Config)
expected.Endpoint = "http:https://localhost:15672"
expected.Username = "otelu"
expected.Password = "$RABBITMQ_PASSWORD"
expected.CollectionInterval = 10 * time.Second

require.Equal(t, expected, cfg)
}
Loading

0 comments on commit 70d6dee

Please sign in to comment.