-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
factory_test.go
144 lines (120 loc) · 4.33 KB
/
factory_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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package instanaexporter
import (
"path/filepath"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/config/confighttp"
"go.opentelemetry.io/collector/config/configopaque"
"go.opentelemetry.io/collector/config/configtls"
"go.opentelemetry.io/collector/confmap/confmaptest"
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/instanaexporter/internal/metadata"
)
// Test that the factory creates the default configuration
func TestCreateDefaultConfig(t *testing.T) {
factory := NewFactory()
cfg := factory.CreateDefaultConfig()
assert.Equal(t, &Config{
ClientConfig: confighttp.ClientConfig{
Endpoint: "",
Timeout: 30 * time.Second,
Headers: map[string]configopaque.String{},
WriteBufferSize: 512 * 1024,
},
}, cfg, "failed to create default config")
assert.NoError(t, componenttest.CheckConfigStruct(cfg))
}
// TestLoadConfig tests that the configuration is loaded correctly
func TestLoadConfig(t *testing.T) {
cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yml"))
require.NoError(t, err)
factory := NewFactory()
t.Run("valid config", func(t *testing.T) {
cfg := factory.CreateDefaultConfig()
sub, err := cm.Sub(component.NewIDWithName(metadata.Type, "valid").String())
require.NoError(t, err)
require.NoError(t, component.UnmarshalConfig(sub, cfg))
err = component.ValidateConfig(cfg)
require.NoError(t, err)
assert.Equal(t, &Config{
ClientConfig: confighttp.ClientConfig{
Endpoint: "https://example.com/api/",
Timeout: 30 * time.Second,
Headers: map[string]configopaque.String{},
WriteBufferSize: 512 * 1024,
},
Endpoint: "https://example.com/api/",
AgentKey: "key1",
}, cfg)
})
t.Run("valid config with ca_file", func(t *testing.T) {
cfg := factory.CreateDefaultConfig()
sub, err := cm.Sub(component.NewIDWithName(metadata.Type, "valid_with_ca_file").String())
require.NoError(t, err)
require.NoError(t, component.UnmarshalConfig(sub, cfg))
err = component.ValidateConfig(cfg)
require.NoError(t, err)
assert.Equal(t, &Config{
ClientConfig: confighttp.ClientConfig{
Endpoint: "https://example.com/api/",
Timeout: 30 * time.Second,
Headers: map[string]configopaque.String{},
WriteBufferSize: 512 * 1024,
TLSSetting: configtls.ClientConfig{
TLSSetting: configtls.Config{
CAFile: "ca.crt",
},
},
},
Endpoint: "https://example.com/api/",
AgentKey: "key1",
}, cfg)
})
t.Run("valid config without ca_file", func(t *testing.T) {
cfg := factory.CreateDefaultConfig()
sub, err := cm.Sub(component.NewIDWithName(metadata.Type, "valid_no_ca_file").String())
require.NoError(t, err)
require.NoError(t, component.UnmarshalConfig(sub, cfg))
err = component.ValidateConfig(cfg)
require.NoError(t, err)
assert.Equal(t, &Config{
ClientConfig: confighttp.ClientConfig{
Endpoint: "https://example.com/api/",
Timeout: 30 * time.Second,
Headers: map[string]configopaque.String{},
WriteBufferSize: 512 * 1024,
},
Endpoint: "https://example.com/api/",
AgentKey: "key1",
}, cfg)
})
t.Run("bad endpoint", func(t *testing.T) {
cfg := factory.CreateDefaultConfig()
sub, err := cm.Sub(component.NewIDWithName(metadata.Type, "bad_endpoint").String())
require.NoError(t, err)
require.NoError(t, component.UnmarshalConfig(sub, cfg))
err = component.ValidateConfig(cfg)
require.Error(t, err)
})
t.Run("non https endpoint", func(t *testing.T) {
cfg := factory.CreateDefaultConfig()
sub, err := cm.Sub(component.NewIDWithName(metadata.Type, "non_https_endpoint").String())
require.NoError(t, err)
require.NoError(t, component.UnmarshalConfig(sub, cfg))
err = component.ValidateConfig(cfg)
require.Error(t, err)
})
t.Run("missing agent key", func(t *testing.T) {
cfg := factory.CreateDefaultConfig()
sub, err := cm.Sub(component.NewIDWithName(metadata.Type, "missing_agent_key").String())
require.NoError(t, err)
require.NoError(t, component.UnmarshalConfig(sub, cfg))
err = component.ValidateConfig(cfg)
require.Error(t, err)
})
}