-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathconfig_test.go
157 lines (148 loc) · 4.63 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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package otlpexporter
import (
"path/filepath"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/configauth"
"go.opentelemetry.io/collector/config/configgrpc"
"go.opentelemetry.io/collector/config/configopaque"
"go.opentelemetry.io/collector/config/configretry"
"go.opentelemetry.io/collector/config/configtls"
"go.opentelemetry.io/collector/confmap"
"go.opentelemetry.io/collector/confmap/confmaptest"
"go.opentelemetry.io/collector/confmap/xconfmap"
"go.opentelemetry.io/collector/exporter/exporterhelper"
)
func TestUnmarshalDefaultConfig(t *testing.T) {
factory := NewFactory()
cfg := factory.CreateDefaultConfig()
require.NoError(t, confmap.New().Unmarshal(&cfg))
assert.Equal(t, factory.CreateDefaultConfig(), cfg)
}
func TestUnmarshalConfig(t *testing.T) {
cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
require.NoError(t, err)
factory := NewFactory()
cfg := factory.CreateDefaultConfig()
require.NoError(t, cm.Unmarshal(&cfg))
require.NoError(t, xconfmap.Validate(&cfg))
assert.Equal(t,
&Config{
TimeoutConfig: exporterhelper.TimeoutConfig{
Timeout: 10 * time.Second,
},
RetryConfig: configretry.BackOffConfig{
Enabled: true,
InitialInterval: 10 * time.Second,
RandomizationFactor: 0.7,
Multiplier: 1.3,
MaxInterval: 1 * time.Minute,
MaxElapsedTime: 10 * time.Minute,
},
QueueConfig: exporterhelper.QueueBatchConfig{
Enabled: true,
Sizer: exporterhelper.RequestSizerTypeItems,
NumConsumers: 2,
QueueSize: 100000,
Batch: &exporterhelper.BatchConfig{
FlushTimeout: 200 * time.Millisecond,
MinSize: 1000,
MaxSize: 10000,
},
},
ClientConfig: configgrpc.ClientConfig{
Headers: map[string]configopaque.String{
"can you have a . here?": "F0000000-0000-0000-0000-000000000000",
"header1": "234",
"another": "somevalue",
},
Endpoint: "1.2.3.4:1234",
Compression: "gzip",
TLSSetting: configtls.ClientConfig{
Config: configtls.Config{
CAFile: "/var/lib/mycert.pem",
},
Insecure: false,
},
Keepalive: &configgrpc.KeepaliveClientConfig{
Time: 20 * time.Second,
PermitWithoutStream: true,
Timeout: 30 * time.Second,
},
WriteBufferSize: 512 * 1024,
BalancerName: "round_robin",
Auth: &configauth.AuthenticationConfig{AuthenticatorID: component.MustNewID("nop")},
},
}, cfg)
}
func TestUnmarshalInvalidConfig(t *testing.T) {
cm, err := confmaptest.LoadConf(filepath.Join("testdata", "invalid_configs.yaml"))
require.NoError(t, err)
factory := NewFactory()
for _, tt := range []struct {
name string
errorMsg string
}{
{
name: "no_endpoint",
errorMsg: `requires a non-empty "endpoint"`,
},
{
name: "https_endpoint",
errorMsg: `requires a non-empty "endpoint"`,
},
{
name: "http_endpoint",
errorMsg: `requires a non-empty "endpoint"`,
},
{
name: "invalid_timeout",
errorMsg: `'timeout' must be non-negative`,
},
{
name: "invalid_retry",
errorMsg: `'randomization_factor' must be within [0, 1]`,
},
{
name: "invalid_tls",
errorMsg: `invalid TLS min_version: unsupported TLS version: "asd"`,
},
{
name: "missing_port",
errorMsg: `missing port in address`,
},
{
name: "invalid_port",
errorMsg: `invalid port "port"`,
},
} {
t.Run(tt.name, func(t *testing.T) {
cfg := factory.CreateDefaultConfig()
sub, err := cm.Sub(tt.name)
require.NoError(t, err)
assert.NoError(t, sub.Unmarshal(&cfg))
assert.ErrorContains(t, xconfmap.Validate(cfg), tt.errorMsg)
})
}
}
func TestValidDNSEndpoint(t *testing.T) {
factory := NewFactory()
cfg := factory.CreateDefaultConfig().(*Config)
cfg.ClientConfig.Endpoint = "dns://authority/backend.example.com:4317"
assert.NoError(t, cfg.Validate())
}
func TestSanitizeEndpoint(t *testing.T) {
factory := NewFactory()
cfg := factory.CreateDefaultConfig().(*Config)
cfg.ClientConfig.Endpoint = "dns://authority/backend.example.com:4317"
assert.Equal(t, "authority/backend.example.com:4317", cfg.sanitizedEndpoint())
cfg.ClientConfig.Endpoint = "dns:///backend.example.com:4317"
assert.Equal(t, "backend.example.com:4317", cfg.sanitizedEndpoint())
cfg.ClientConfig.Endpoint = "dns:////backend.example.com:4317"
assert.Equal(t, "/backend.example.com:4317", cfg.sanitizedEndpoint())
}