forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_test.go
100 lines (90 loc) · 2.58 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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package apachereceiver
import (
"path/filepath"
"testing"
"time"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/confmap/confmaptest"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/apachereceiver/internal/metadata"
)
func TestValidate(t *testing.T) {
testCases := []struct {
desc string
endpoint string
errExpected bool
errText string
}{
{
desc: "default_endpoint",
endpoint: "http:https://localhost:8080/server-status?auto",
errExpected: false,
},
{
desc: "custom_host",
endpoint: "http:https://123.123.123.123:8080/server-status?auto",
errExpected: false,
},
{
desc: "custom_port",
endpoint: "http:https://123.123.123.123:9090/server-status?auto",
errExpected: false,
},
{
desc: "custom_path",
endpoint: "http:https://localhost:8080/my-status?auto",
errExpected: false,
},
{
desc: "empty_path",
endpoint: "",
errExpected: true,
errText: "missing hostname: ''",
},
{
desc: "missing_hostname",
endpoint: "http:https://:8080/server-status?auto",
errExpected: true,
errText: "missing hostname: 'http:https://:8080/server-status?auto'",
},
{
desc: "missing_query",
endpoint: "http:https://localhost:8080/server-status",
errExpected: true,
errText: "query must be 'auto': 'http:https://localhost:8080/server-status'",
},
{
desc: "invalid_query",
endpoint: "http:https://localhost:8080/server-status?nonsense",
errExpected: true,
errText: "query must be 'auto': 'http:https://localhost:8080/server-status?nonsense'",
},
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
cfg := NewFactory().CreateDefaultConfig().(*Config)
cfg.Endpoint = tc.endpoint
err := component.ValidateConfig(cfg)
if tc.errExpected {
require.EqualError(t, err, tc.errText)
return
}
require.NoError(t, err)
})
}
}
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.Endpoint = "http:https://localhost:8080/server-status?auto"
expected.CollectionInterval = 10 * time.Second
require.Equal(t, expected, cfg)
}