-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
config_test.go
156 lines (145 loc) · 3.77 KB
/
config_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package aerospikereceiver
import (
"path/filepath"
"testing"
"time"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component"
"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/aerospikereceiver/internal/metadata"
)
func TestValidate(t *testing.T) {
testCases := []struct {
name string
config *Config
expected error
}{
{
name: "blank endpoint",
config: &Config{
Endpoint: "",
ControllerConfig: scraperhelper.NewDefaultControllerConfig(),
},
expected: errEmptyEndpoint,
},
{
name: "missing port",
config: &Config{
Endpoint: "localhost",
ControllerConfig: scraperhelper.NewDefaultControllerConfig(),
},
expected: errBadEndpoint,
},
{
name: "bad endpoint",
config: &Config{
Endpoint: "x;;ef;s;d:::ss:23423423423423423",
ControllerConfig: scraperhelper.NewDefaultControllerConfig(),
},
expected: errBadEndpoint,
},
{
name: "missing host",
config: &Config{
Endpoint: ":3001",
ControllerConfig: scraperhelper.NewDefaultControllerConfig(),
},
expected: errBadEndpoint,
},
{
name: "negative port",
config: &Config{
Endpoint: "localhost:-2",
ControllerConfig: scraperhelper.NewDefaultControllerConfig(),
},
expected: errBadPort,
},
{
name: "bad port",
config: &Config{
Endpoint: "localhost:9999999999999999999",
ControllerConfig: scraperhelper.NewDefaultControllerConfig(),
},
expected: errBadPort,
},
{
name: "negative timeout",
config: &Config{
Endpoint: "localhost:3000",
Timeout: -1 * time.Second,
ControllerConfig: scraperhelper.NewDefaultControllerConfig(),
},
expected: errNegativeTimeout,
},
{
name: "password but no username",
config: &Config{
Endpoint: "localhost:3000",
Username: "",
Password: "secret",
ControllerConfig: scraperhelper.NewDefaultControllerConfig(),
},
expected: errEmptyUsername,
},
{
name: "username but no password",
config: &Config{
Endpoint: "localhost:3000",
Username: "ro_user",
ControllerConfig: scraperhelper.NewDefaultControllerConfig(),
},
expected: errEmptyPassword,
},
{
name: "bad TLS config",
config: &Config{
Endpoint: "localhost:3000",
TLSName: "tls1",
TLS: &configtls.ClientConfig{
Insecure: false,
Config: configtls.Config{
CAFile: "BADCAFILE",
},
},
ControllerConfig: scraperhelper.NewDefaultControllerConfig(),
},
expected: errFailedTLSLoad,
},
{
name: "empty tls name",
config: &Config{
Endpoint: "localhost:3000",
TLSName: "",
TLS: &configtls.ClientConfig{
Insecure: false,
Config: configtls.Config{},
},
ControllerConfig: scraperhelper.NewDefaultControllerConfig(),
},
expected: errEmptyEndpointTLSName,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := tc.config.Validate()
require.ErrorIs(t, err, tc.expected)
})
}
}
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, sub.Unmarshal(cfg))
expected := factory.CreateDefaultConfig().(*Config)
expected.Endpoint = "localhost:3000"
expected.CollectionInterval = 30 * time.Second
require.Equal(t, expected, cfg)
}