123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- // Copyright The OpenTelemetry Authors
- // SPDX-License-Identifier: Apache-2.0
- package mongodbreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/mongodbreceiver"
- import (
- "errors"
- "path/filepath"
- "testing"
- "time"
- "github.com/stretchr/testify/require"
- "go.opentelemetry.io/collector/component"
- "go.opentelemetry.io/collector/config/confignet"
- "go.opentelemetry.io/collector/config/configopaque"
- "go.opentelemetry.io/collector/config/configtls"
- "go.opentelemetry.io/collector/confmap/confmaptest"
- "go.opentelemetry.io/collector/receiver/scraperhelper"
- "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/mongodbreceiver/internal/metadata"
- )
- func TestValidate(t *testing.T) {
- testCases := []struct {
- endpoints []string
- desc string
- username string
- password string
- expected error
- }{
- {
- desc: "no username, no password",
- endpoints: []string{"localhost:27107"},
- username: "",
- password: "",
- expected: nil,
- },
- {
- desc: "no username, with password",
- endpoints: []string{"localhost:27107"},
- username: "",
- password: "pass",
- expected: errors.New("password provided without user"),
- },
- {
- desc: "with username, no password",
- endpoints: []string{"localhost:27107"},
- username: "user",
- password: "",
- expected: errors.New("username provided without password"),
- },
- {
- desc: "with username and password",
- endpoints: []string{"localhost:27107"},
- username: "user",
- password: "pass",
- expected: nil,
- },
- {
- desc: "no hosts",
- username: "user",
- password: "pass",
- expected: errors.New("no hosts were specified in the config"),
- },
- {
- desc: "valid hostname",
- endpoints: []string{"localhost"},
- expected: nil,
- },
- {
- desc: "empty host",
- username: "user",
- endpoints: []string{""},
- expected: errors.New("no endpoint specified for one of the hosts"),
- },
- }
- for _, tc := range testCases {
- t.Run(tc.desc, func(t *testing.T) {
- var hosts []confignet.NetAddr
- for _, ep := range tc.endpoints {
- hosts = append(hosts, confignet.NetAddr{
- Endpoint: ep,
- })
- }
- cfg := &Config{
- Username: tc.username,
- Password: configopaque.String(tc.password),
- Hosts: hosts,
- ScraperControllerSettings: scraperhelper.NewDefaultScraperControllerSettings(metadata.Type),
- }
- err := component.ValidateConfig(cfg)
- if tc.expected == nil {
- require.Nil(t, err)
- } else {
- require.Contains(t, err.Error(), tc.expected.Error())
- }
- })
- }
- }
- func TestBadTLSConfigs(t *testing.T) {
- testCases := []struct {
- desc string
- tlsConfig configtls.TLSClientSetting
- expectError bool
- }{
- {
- desc: "CA file not found",
- tlsConfig: configtls.TLSClientSetting{
- TLSSetting: configtls.TLSSetting{
- CAFile: "not/a/real/file.pem",
- },
- Insecure: false,
- InsecureSkipVerify: false,
- ServerName: "",
- },
- expectError: true,
- },
- {
- desc: "no issues",
- tlsConfig: configtls.TLSClientSetting{
- TLSSetting: configtls.TLSSetting{},
- Insecure: false,
- InsecureSkipVerify: false,
- ServerName: "",
- },
- expectError: false,
- },
- }
- for _, tc := range testCases {
- t.Run(tc.desc, func(t *testing.T) {
- cfg := &Config{
- Username: "otel",
- Password: "pword",
- Hosts: []confignet.NetAddr{
- {
- Endpoint: "localhost:27017",
- },
- },
- ScraperControllerSettings: scraperhelper.NewDefaultScraperControllerSettings(metadata.Type),
- TLSClientSetting: tc.tlsConfig,
- }
- err := component.ValidateConfig(cfg)
- if tc.expectError {
- require.Error(t, err)
- } else {
- require.NoError(t, err)
- }
- })
- }
- }
- func TestOptions(t *testing.T) {
- cfg := &Config{
- Hosts: []confignet.NetAddr{
- {
- Endpoint: "localhost:27017",
- },
- },
- Username: "uname",
- Password: "password",
- Timeout: 2 * time.Minute,
- ReplicaSet: "rs-1",
- }
- clientOptions := cfg.ClientOptions()
- require.Equal(t, clientOptions.Auth.Username, cfg.Username)
- require.Equal(t,
- clientOptions.ConnectTimeout.Milliseconds(),
- (2 * time.Minute).Milliseconds(),
- )
- require.Equal(t, "rs-1", *clientOptions.ReplicaSet)
- }
- func TestOptionsTLS(t *testing.T) {
- // loading valid ca file
- caFile := filepath.Join("testdata", "certs", "ca.crt")
- cfg := &Config{
- Hosts: []confignet.NetAddr{
- {
- Endpoint: "localhost:27017",
- },
- },
- TLSClientSetting: configtls.TLSClientSetting{
- Insecure: false,
- TLSSetting: configtls.TLSSetting{
- CAFile: caFile,
- },
- },
- }
- 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(component.NewIDWithName(metadata.Type, "").String())
- require.NoError(t, err)
- require.NoError(t, component.UnmarshalConfig(sub, cfg))
- expected := factory.CreateDefaultConfig().(*Config)
- expected.Hosts = []confignet.NetAddr{
- {
- Endpoint: "localhost:27017",
- },
- }
- expected.Username = "otel"
- expected.Password = "${env:MONGO_PASSWORD}"
- expected.CollectionInterval = time.Minute
- require.Equal(t, expected, cfg)
- }
|