-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathconfig_test.go
155 lines (139 loc) · 3.67 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
package llama
import (
"testing"
)
var exampleTargetConfig = TargetConfig{
IP: "1.2.3.4",
Port: 1234,
Tags: Tags{"tag1": "value1", "tag2": "value2"},
}
var exampleTargetConfig2 = TargetConfig{
IP: "127.0.0.1",
Port: 32000,
Tags: Tags{"cat": "land", "bird": "air"},
}
var exampleTargetConfig3 = TargetConfig{
IP: "192.168.0.1",
Port: 0,
Tags: Tags{"foo": "bar", "baz": "qux"},
}
var exampleTargetSet = TargetSet{exampleTargetConfig, exampleTargetConfig2}
var exampleTargetSet2 = TargetSet{exampleTargetConfig3}
var exampleTargetsConfig = TargetsConfig{
"example": exampleTargetSet,
"moar": exampleTargetSet2,
}
var exampleLegacyConfig = `
1.2.3.4:
my_tag: my_value
foo: bar
127.0.0.1:
tag1: value1
baz: qux
`
func TestTargetConfigAddrString(t *testing.T) {
expected := "1.2.3.4:1234"
result := exampleTargetConfig.AddrString()
if result != expected {
t.Error("Target addr not formatted correctly. Expected",
expected, "got", result)
}
// Make sure it returns non-nil for zero values
tc := TargetConfig{}
if tc.AddrString() == "" {
t.Error("Zero values returned nil instead of string value. Got:", tc.AddrString())
}
}
func TestTargetConfigResolveUDPAddr(t *testing.T) {
_, err := exampleTargetConfig.ResolveUDPAddr()
if err != nil {
t.Error("Target couldn't be converted to UDPAddr:", err)
}
// Make sure it doesn't fail for zero values (:0)
tc := TargetConfig{}
_, err = tc.ResolveUDPAddr()
if err != nil {
t.Error("Zero value couldn't be converted to UDPAddr:", err)
}
}
func TestTargetSetTagSet(t *testing.T) {
tagset := exampleTargetSet.TagSet()
_, ok := tagset["1.2.3.4"]
if !ok {
t.Error("Parsed value was not populated")
}
// TODO(dmar): Add some deeper testing here
}
func TestTargetSetIntoTagSet(t *testing.T) {
tagset := make(TagSet)
tagset["example"] = Tags{"mytag": "myvalue"}
exampleTargetSet.IntoTagSet(tagset)
_, ok := tagset["example"]
if !ok {
t.Error("Prepopulated value was erased")
}
_, ok = tagset["1.2.3.4"]
if !ok {
t.Error("Parsed value was not populated")
}
}
func TestTargetSetListTarget(t *testing.T) {
expected := []string{"1.2.3.4:1234", "127.0.0.1:32000"}
result := exampleTargetSet.ListTargets()
// TODO(dmar): Do deeper evaluation here.
if len(expected) != len(result) {
t.Error("Expected:", expected, "but got:", result)
}
}
func TestTargetSetListResolvedTargets(t *testing.T) {
addrs, err := exampleTargetSet.ListResolvedTargets()
if err != nil {
t.Error(err)
}
if len(addrs) != 2 {
t.Error("Expected 2 UDPAddrs but got", len(addrs))
}
}
func TestTargetsConfig(t *testing.T) {
tagset := exampleTargetsConfig.TagSet()
_, ok := tagset["1.2.3.4"]
if !ok {
t.Error("Parsed value was not populated")
}
}
func TestTargetsConfigIntoTagSet(t *testing.T) {
tagset := make(TagSet)
tagset["example"] = Tags{"mytag": "myvalue"}
exampleTargetsConfig.IntoTagSet(tagset)
_, ok := tagset["example"]
if !ok {
t.Error("Prepopulated value was erased")
}
_, ok = tagset["1.2.3.4"]
if !ok {
t.Error("Parsed value was not populated")
}
}
func TestNewDefaultCollectorConfig(t *testing.T) {
// Just make sure it comes back with no errors.
// This also tests `NewCollectorConfig
_, err := NewDefaultCollectorConfig()
if err != nil {
t.Error(err)
}
}
func TestNewLegacyCollectorConfig(t *testing.T) {
// Just make sure it returns with no errors.
_, err := NewLegacyCollectorConfig([]byte(exampleLegacyConfig))
if err != nil {
t.Error(err)
}
}
func TestLegacyCollectorConfigToDefaultCollectorConfig(t *testing.T) {
lcc, _ := NewLegacyCollectorConfig([]byte(exampleLegacyConfig))
// Just make sure it converts
_, err := lcc.ToDefaultCollectorConfig(1234)
if err != nil {
t.Error(err)
}
}