-
Notifications
You must be signed in to change notification settings - Fork 6
/
parse_config_test.go
62 lines (54 loc) · 1.65 KB
/
parse_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
package main
import (
"bytes"
"encoding/base64"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v2"
)
var testNodes = []NodeConfig{
{
NodeName: "foo",
MetricName: "bar",
},
{
NodeName: "baz",
MetricName: "bak",
ExtractBit: 4,
},
}
func TestReadNodeFile(t *testing.T) {
data, _ := yaml.Marshal(testNodes)
results, err := parseConfigYAML(bytes.NewReader(data))
assert.NoError(t, err)
assert.Equal(t, len(testNodes), len(results))
assert.IsType(t, NodeConfig{}, results[0])
assert.Equal(t, testNodes[0].NodeName, results[0].NodeName)
assert.Equal(t, testNodes[0].MetricName, results[0].MetricName)
assert.Nil(t, results[0].ExtractBit)
assert.Equal(t, 4, results[1].ExtractBit)
results, err = parseConfigYAML(strings.NewReader("foooob not valid json here"))
assert.Error(t, err)
assert.Empty(t, results)
}
func TestB64Config(t *testing.T) {
data, _ := yaml.Marshal(testNodes)
encodedData := base64.StdEncoding.EncodeToString(data)
results, err := readConfigBase64(&encodedData)
assert.NoError(t, err)
assert.Equal(t, len(testNodes), len(results))
assert.IsType(t, NodeConfig{}, results[0])
assert.Equal(t, testNodes[0].NodeName, results[0].NodeName)
assert.Equal(t, testNodes[0].MetricName, results[0].MetricName)
}
func TestJsonConfig(t *testing.T) {
// Luckily JSON is also YAML
json := `[{"metricName": "foo", "nodeName": "whatever", "extractBit": 1}]`
results, err := parseConfigYAML(strings.NewReader((json)))
assert.NoError(t, err)
assert.Equal(t, 1, len(results))
assert.Equal(t, "foo", results[0].MetricName)
assert.Equal(t, "whatever", results[0].NodeName)
assert.Equal(t, 1, results[0].ExtractBit)
}