forked from youliao/opcua_exporter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
parse_config_test.go
66 lines (58 loc) · 1.93 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
63
64
65
66
package main
import (
"bytes"
"encoding/base64"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v2"
)
var testNodes = GlobalConf {
EndPoint: "opc.tcp://localhost:4096",
Nodes: []NodeConfig {
{
NodeName: "foo",
MetricName: "bar",
},{
NodeName: "baz",
MetricName: "bak",
ExtractBit: 4,
},
},
}
func TestReadNodeFile(t *testing.T) {
data, _ := yaml.Marshal(testNodes)
results, endpoint, err := parseConfigYAML(bytes.NewReader(data))
assert.NoError(t, err)
assert.Equal(t, testNodes.EndPoint, endpoint)
assert.Equal(t, len(testNodes.Nodes), len(results))
assert.IsType(t, NodeConfig{}, results[0])
assert.Equal(t, testNodes.Nodes[0].NodeName, results[0].NodeName)
assert.Equal(t, testNodes.Nodes[0].MetricName, results[0].MetricName)
assert.Nil(t, results[0].ExtractBit)
assert.Equal(t, 4, results[1].ExtractBit)
results, endpoint, 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, endpoint, err := readConfigBase64(&encodedData)
assert.NoError(t, err)
assert.Equal(t, testNodes.EndPoint, endpoint)
assert.Equal(t, len(testNodes.Nodes), len(results))
assert.IsType(t, NodeConfig{}, results[0])
assert.Equal(t, testNodes.Nodes[0].NodeName, results[0].NodeName)
assert.Equal(t, testNodes.Nodes[0].MetricName, results[0].MetricName)
}
func TestJsonConfig(t *testing.T) {
// Luckily JSON is also YAML
json := `{"endpoint":"opc.tcp://localhost:4096", "nodes":[{"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)
}