forked from dedalusj/cwmonitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_test.go
87 lines (76 loc) · 2.23 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
package monitor
import (
"strconv"
"testing"
"time"
"github.com/dedalusj/cwmonitor/metrics"
"github.com/stretchr/testify/assert"
)
func TestConfig_Validate(t *testing.T) {
t.Run("validates name", func(t *testing.T) {
c := Config{}
err := c.Validate()
assert.Error(t, err)
assert.Contains(t, err.Error(), "name")
})
t.Run("validates interval", func(t *testing.T) {
c := Config{}
err := c.Validate()
assert.Error(t, err)
assert.Contains(t, err.Error(), "interval")
})
t.Run("validates id", func(t *testing.T) {
c := Config{}
err := c.Validate()
assert.Error(t, err)
assert.Contains(t, err.Error(), "id")
})
t.Run("validates metrics", func(t *testing.T) {
c := Config{}
err := c.Validate()
assert.Error(t, err)
assert.Contains(t, err.Error(), "metrics")
})
t.Run("valid", func(t *testing.T) {
c := Config{
Namespace: "namespace",
Interval: time.Minute,
HostId: "id",
Metrics: "cpu,memory",
}
err := c.Validate()
assert.NoError(t, err)
})
}
func TestConfig_GetRequestedMetrics(t *testing.T) {
testCases := []struct {
input string
expected []metrics.Metric
}{
{input: "", expected: []metrics.Metric{}},
{input: "memory", expected: []metrics.Metric{metrics.Memory{}}},
{input: "swap", expected: []metrics.Metric{metrics.Swap{}}},
{input: "cpu", expected: []metrics.Metric{metrics.CPU{}}},
{input: "disk", expected: []metrics.Metric{metrics.Disk{}}},
{input: "docker-stats", expected: []metrics.Metric{metrics.DockerStat{}}},
{input: "docker-health", expected: []metrics.Metric{metrics.DockerHealth{}}},
{input: "cpu,memory", expected: []metrics.Metric{metrics.CPU{}, metrics.Memory{}}},
{input: "cpu,foo", expected: []metrics.Metric{metrics.CPU{}}},
{input: ",", expected: []metrics.Metric{}},
{input: "cpu,", expected: []metrics.Metric{metrics.CPU{}}},
}
for i, tc := range testCases {
t.Run(strconv.Itoa(i), func(t *testing.T) {
c := Config{Metrics: tc.input}
output := c.GetRequestedMetrics()
assert.ElementsMatch(t, tc.expected, output)
})
}
}
func TestConfig_GetExtraDimensions(t *testing.T) {
c := Config{HostId: "id"}
dim := c.GetExtraDimensions()
assert.Len(t, dim, 1)
assert.Equal(t, dim[0].Name, "Host")
assert.Equal(t, dim[0].Value, "id")
}