-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
config_test.go
88 lines (78 loc) · 1.9 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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package grafanacloudconnector
import (
"path/filepath"
"testing"
"time"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/confmap/confmaptest"
"gotest.tools/assert"
"github.com/open-telemetry/opentelemetry-collector-contrib/connector/grafanacloudconnector/internal/metadata"
)
func TestLoadConfig(t *testing.T) {
testCases := []struct {
name string
expect *Config
}{
{
name: "",
expect: createDefaultConfig().(*Config),
},
{
name: "custom",
expect: &Config{
HostIdentifiers: []string{"k8s.node.name", "host.name", "host.id"},
MetricsFlushInterval: 30 * time.Second,
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
assert.NilError(t, err)
factory := NewFactory()
cfg := factory.CreateDefaultConfig()
sub, err := cm.Sub(component.NewIDWithName(metadata.Type, tc.name).String())
assert.NilError(t, err)
assert.NilError(t, sub.Unmarshal(cfg))
assert.DeepEqual(t, tc.expect, cfg)
})
}
}
func TestValidate(t *testing.T) {
testCases := []struct {
name string
cfg *Config
errMsg string
}{
{
name: "valid",
cfg: createDefaultConfig().(*Config),
errMsg: "",
},
{
name: "missing identifiers",
cfg: &Config{},
errMsg: "at least one host identifier is required",
},
{
name: "invalid flush interval",
cfg: &Config{
HostIdentifiers: []string{"host.id"},
MetricsFlushInterval: time.Hour,
},
errMsg: "\"1h0m0s\" is not a valid flush interval between 15s and 5m",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := tc.cfg.Validate()
if tc.errMsg != "" {
assert.Equal(t, tc.errMsg, err.Error())
} else {
assert.NilError(t, err)
}
})
}
}