-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_test.go
195 lines (167 loc) · 5.82 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Copyright 2024 SolarWinds Worldwide, LLC. All rights reserved.
//
// 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://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 solarwindsexporter
import (
"fmt"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/config/configopaque"
"go.opentelemetry.io/collector/config/configretry"
"go.opentelemetry.io/collector/confmap"
"go.opentelemetry.io/collector/confmap/confmaptest"
"go.opentelemetry.io/collector/exporter/exporterhelper"
)
// loadConfigTestdata is a helper function to load a testdata
// file by its name.
func loadConfigTestdata(t *testing.T, name string) *confmap.Conf {
t.Helper()
filename := fmt.Sprintf("%s.yaml", name)
cm, err := confmaptest.LoadConf(filepath.Join("testdata", filename))
require.NoError(t, err)
return cm
}
// TestConfigUnmarshalFull tries to parse a configuration file
// with all values provided and verifies the configuration.
func TestConfigUnmarshalFull(t *testing.T) {
cfgFile := loadConfigTestdata(t, "full")
// Parse configuration.
factory := NewFactory()
cfg := factory.CreateDefaultConfig()
require.NoError(t, cfgFile.Unmarshal(&cfg))
// Verify the values.
assert.Equal(t, &Config{
DataCenter: "na-01",
EndpointURLOverride: "127.0.0.1:1234",
IngestionToken: "TOKEN",
BackoffSettings: configretry.BackOffConfig{
Enabled: false,
InitialInterval: 15000000000,
RandomizationFactor: 0.7,
Multiplier: 2.4,
MaxInterval: 40000000000,
MaxElapsedTime: 400000000000,
},
QueueSettings: exporterhelper.QueueConfig{
Enabled: true,
NumConsumers: 20,
QueueSize: 2000,
},
Timeout: exporterhelper.TimeoutConfig{
Timeout: 20000000000,
},
}, cfg)
}
// TestConfigValidateOK verifies that a configuration
// file containing only the mandatory values successfully
// validates.
func TestConfigValidateOK(t *testing.T) {
cfgFile := loadConfigTestdata(t, "minimal")
// Parse configuration.
factory := NewFactory()
cfg := factory.CreateDefaultConfig()
require.NoError(t, cfgFile.Unmarshal(&cfg))
// Try to validate it.
assert.NoError(t, cfg.(*Config).Validate())
}
// TestConfigValidateMissingToken verifies that
// the validation of a configuration file with
// the token missing fails as expected.
func TestConfigValidateMissingToken(t *testing.T) {
cfgFile := loadConfigTestdata(t, "missing_token")
// Parse configuration.
factory := NewFactory()
cfg := factory.CreateDefaultConfig()
require.NoError(t, cfgFile.Unmarshal(&cfg))
assert.ErrorContains(
t,
cfg.(*Config).Validate(),
"invalid configuration: token must be set",
)
}
// TestConfigValidateMissingDataCenter verifies that
// the validation of a configuration file with
// the dataCenter ID missing fails as expected.
func TestConfigValidateMissingDataCenter(t *testing.T) {
cfgFile := loadConfigTestdata(t, "missing_dc")
// Parse configuration.
factory := NewFactory()
cfg := factory.CreateDefaultConfig()
require.NoError(t, cfgFile.Unmarshal(&cfg))
assert.ErrorContains(
t,
cfg.(*Config).Validate(),
"invalid configuration: data center must be provided",
)
}
// TestConfigValidateDataCenters verifies mappings
// for data centers (the mapping is case-insensitive).
func TestConfigValidateDataCenters(t *testing.T) {
type test struct {
dataCenter string
url string
ok bool
}
tests := []test{
{dataCenter: "na-01", url: "otel.collector.na-01.cloud.solarwinds.com:443", ok: true},
{dataCenter: "na-02", url: "otel.collector.na-02.cloud.solarwinds.com:443", ok: true},
{dataCenter: "eu-01", url: "otel.collector.eu-01.cloud.solarwinds.com:443", ok: true},
{dataCenter: "NA-01", url: "otel.collector.na-01.cloud.solarwinds.com:443", ok: true},
{dataCenter: "apj-01", url: "", ok: false},
}
for _, tc := range tests {
// Try to find a dataCenter URL for its ID.
url, err := lookupDataCenterURL(tc.dataCenter)
if tc.ok { // A URL should be returned.
require.NoError(t, err)
assert.Equal(t, tc.url, url)
} else { // It should fail.
assert.ErrorContains(t, err, "unknown data center ID")
}
}
}
// TestConfigTokenRedacted checks that the configuration
// type doesn't leak its secret token unless it is accessed explicitly.
func TestConfigTokenRedacted(t *testing.T) {
cfg := &Config{
DataCenter: "eu-01",
IngestionToken: "SECRET",
}
// This is the only way of accessing the actual token.
require.Equal(t, "SECRET", string(cfg.IngestionToken))
// It is redacted when printed.
assert.Equal(t, "[REDACTED]", cfg.IngestionToken.String())
}
// TestConfigOTLPWithOverride converts exporter configuration to
// OTLP gRPC Exporter configuration and verifies that overridden
// endpoint and token propagate correctly.
func TestConfigOTLPWithOverride(t *testing.T) {
cfgFile := loadConfigTestdata(t, "url_override")
// Parse configuration.
factory := NewFactory()
cfg := factory.CreateDefaultConfig()
require.NoError(t, cfgFile.Unmarshal(&cfg))
// Convert it to the OTLP Exporter configuration.
otlpCfg, err := cfg.(*Config).OTLPConfig()
require.NoError(t, err)
// Verify that both the token and overridden URL were propagated
// to the OTLP configuration.
assert.Equal(t, "127.0.0.1:1234", otlpCfg.Endpoint)
assert.Equal(
t,
map[string]configopaque.String{"Authorization": "Bearer YOUR-INGESTION-TOKEN"},
otlpCfg.Headers,
)
}