forked from kaplanelad/veneur
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_test.go
178 lines (154 loc) · 4.76 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package veneur
import (
"os"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestReadConfig(t *testing.T) {
exampleConfig, err := os.Open("example.yaml")
assert.NoError(t, err)
defer exampleConfig.Close()
c, err := readConfig(exampleConfig)
if err != nil {
t.Fatal(err)
}
c.applyDefaults()
assert.Equal(t, "https://app.datadoghq.com", c.DatadogAPIHostname)
assert.Equal(t, 96, c.NumWorkers)
interval, err := c.ParseInterval()
assert.NoError(t, err)
assert.Equal(t, interval, 10*time.Second)
}
func TestReadBadConfig(t *testing.T) {
const exampleConfig = `--- api_hostname: :bad`
r := strings.NewReader(exampleConfig)
c, err := readConfig(r)
assert.NotNil(t, err, "Should have encountered parsing error when reading invalid config file")
assert.Equal(t, c, Config{}, "Parsing invalid config file should return zero struct")
}
func TestReadUnknownKeysConfig(t *testing.T) {
const config = `---
no_such_key: 1
hostname: foobar
`
r := strings.NewReader(config)
c, err := readConfig(r)
assert.Error(t, err)
_, ok := err.(*UnknownConfigKeys)
t.Log(err)
assert.True(t, ok, "Returned error should indicate a strictness error")
assert.Equal(t, "foobar", c.Hostname)
}
func TestReadUnknownKeysProxyConfig(t *testing.T) {
const config = `---
no_such_key: 1
debug: true
`
r := strings.NewReader(config)
c, err := readProxyConfig(r)
assert.Error(t, err)
_, ok := err.(*UnknownConfigKeys)
t.Log(err)
assert.True(t, ok, "Returned error should indicate a strictness error")
assert.Equal(t, true, c.Debug)
}
func TestHostname(t *testing.T) {
const hostnameConfig = "hostname: foo"
r := strings.NewReader(hostnameConfig)
c, err := readConfig(r)
assert.Nil(t, err, "Should parsed valid config file: %s", hostnameConfig)
assert.Equal(t, c.Hostname, "foo", "Should have parsed hostname into Config")
const noHostname = "hostname: ''"
r = strings.NewReader(noHostname)
c, err = readConfig(r)
assert.Nil(t, err, "Should parsed valid config file: %s", noHostname)
currentHost, err := os.Hostname()
assert.Nil(t, err, "Could not get current hostname")
c.applyDefaults()
assert.Equal(t, c.Hostname, currentHost, "Should have used current hostname in Config")
const omitHostname = "omit_empty_hostname: true"
r = strings.NewReader(omitHostname)
c, err = readConfig(r)
assert.Nil(t, err, "Should parsed valid config file: %s", omitHostname)
c.applyDefaults()
assert.Equal(t, c.Hostname, "", "Should have respected omit_empty_hostname")
}
func TestConfigDefaults(t *testing.T) {
const emptyConfig = "---"
r := strings.NewReader(emptyConfig)
c, err := readConfig(r)
assert.Nil(t, err, "Should parsed empty config file: %s", emptyConfig)
expectedConfig := defaultConfig
currentHost, err := os.Hostname()
assert.Nil(t, err, "Could not get current hostname")
expectedConfig.Hostname = currentHost
c.applyDefaults()
assert.Equal(t, c, expectedConfig, "Should have applied all config defaults")
}
func TestDefaultConfigEquivalence(t *testing.T) {
assert.Equal(t,
defaultConfig.Interval,
defaultConfig.SplunkHecMaxConnectionLifetime,
"splunk_hec_max_connection_lifetime should default to interval")
}
func TestProxyConfigDefaults(t *testing.T) {
const emptyConfig = "---"
r := strings.NewReader(emptyConfig)
c, err := readProxyConfig(r)
assert.Nil(t, err, "Should parsed empty config file: %s", emptyConfig)
expectedConfig := defaultProxyConfig
c.applyDefaults()
assert.Equal(t, c, expectedConfig, "Should have applied all config defaults")
}
func TestVeneurExamples(t *testing.T) {
tests := []string{
"example.yaml",
"example_host.yaml",
}
for _, elt := range tests {
test := elt
t.Run(test, func(t *testing.T) {
t.Parallel()
_, err := ReadConfig(test)
assert.NoError(t, err)
})
}
}
func TestProxyExamples(t *testing.T) {
tests := []string{"example_proxy.yaml"}
for _, elt := range tests {
test := elt
t.Run(test, func(t *testing.T) {
t.Parallel()
_, err := ReadProxyConfig(test)
assert.NoError(t, err)
})
}
}
func TestReadConfigBackwardsCompatible(t *testing.T) {
// set the deprecated config options
const config = `
flush_max_per_body: 1234
ssf_buffer_size: 3456
trace_lightstep_access_token: "123"
trace_lightstep_collector_host: "456"
trace_lightstep_reconnect_period: "789"
trace_lightstep_maximum_spans: 1
trace_lightstep_num_clients: 2
`
c, err := readConfig(strings.NewReader(config))
c.applyDefaults()
if err != nil {
t.Fatal(err)
}
// they should get copied to the new config options
assert.Equal(t, 1234, c.DatadogFlushMaxPerBody)
assert.Equal(t, 3456, c.DatadogSpanBufferSize)
assert.Equal(t, "123", c.LightstepAccessToken)
assert.Equal(t, "456", c.LightstepCollectorHost)
assert.Equal(t, "789", c.LightstepReconnectPeriod)
assert.Equal(t, 1, c.LightstepMaximumSpans)
assert.Equal(t, 2, c.LightstepNumClients)
}