This repository has been archived by the owner on Apr 21, 2023. It is now read-only.
/
config_test.go
134 lines (122 loc) · 2.98 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
package kkok
import (
"reflect"
"testing"
"time"
"github.com/BurntSushi/toml"
)
func testConfigDefault(t *testing.T) {
t.Parallel()
c := NewConfig()
if c.InitialInterval != defaultInitialInterval {
t.Error(`c.InitialInterval != defaultInitialInterval`)
}
if c.MaxInterval != defaultMaxInterval {
t.Error(`c.MaxInterval != defaultMaxInterval`)
}
if c.Addr != defaultAddr {
t.Error(`c.Addr != defaultAddr`)
}
}
func testConfigLoad(t *testing.T) {
t.Parallel()
c := NewConfig()
md, err := toml.DecodeFile("testdata/config.toml", c)
if err != nil {
t.Fatal(err)
}
if len(md.Undecoded()) > 0 {
// As md reports values decoded with toml.Unmarshaler as undecoded,
// this is just not an error.
t.Log("undecoded:", md.Undecoded())
}
if c.InitialInterval != 100 {
t.Error(`c.InitialInterval != 100`)
}
if c.InitialDuration() != 100*time.Second {
t.Error(`c.InitialDuration() != 100 * time.Second`)
}
if c.MaxInterval != 1000 {
t.Error(`c.MaxInterval != 1000`)
}
if c.MaxDuration() != 1000*time.Second {
t.Error(`c.MaxDuration() != 1000 * time.Second`)
}
if c.Addr != "localhost:12229" {
t.Error(`c.Addr != "localhost:12229"`)
}
if len(c.APIToken) != 0 {
t.Error(`len(c.APIToken) != 0`)
}
// log
if c.Log.Level != "debug" {
t.Error(`c.Log.Level != "debug"`)
}
// sources
if len(c.Sources) != 1 {
t.Fatal(`len(c.Sources) != 1`)
}
s := c.Sources[0]
if s.Type != "maildir" {
t.Error(`s.Type != "maildir"`)
}
if s.Params["dir"] != "/var/mail/ymmt2005" {
t.Error(`s.Params["dir"] != "/var/mail/ymmt2005"`)
}
// routes
if len(c.Routes) != 2 {
t.Fatal(`len(c.Routes) != 2`)
}
rnotify, ok := c.Routes["notify"]
if !ok {
t.Fatal(`no "notify" route`)
}
if len(rnotify) != 2 {
t.Fatal(`len(rnotify) != 2`)
}
if rnotify[0].Type != "slack" {
t.Error(`rnotify[0].Type != "slack"`)
}
if rnotify[0].Params["url"] != "https://hooks.slack.com/xxxx" {
t.Error(`rnotify[0].Params["url"] != "https://hooks.slack.com/xxxx"`)
}
if rnotify[1].Type != "email" {
t.Error(`rnotify[1].Type != "email"`)
}
if !reflect.DeepEqual(rnotify[1].Params["to"], []interface{}{
"ymmt2005@example.com", "ymmt@example.org",
}) {
t.Error(`rnotify[1].Params["to"] != ["ymmt2005@example.com", "ymmt@example.org"]`)
}
remerg, ok := c.Routes["emergency"]
if !ok {
t.Fatal(`no "emergency" route`)
}
if len(remerg) != 1 {
t.Fatal(`len(remerg) != 1`)
}
if remerg[0].Type != "twilio" {
t.Error(`remerg.Type != "twilio"`)
}
if remerg[0].Params["token"] != "yyyy" {
t.Error(`remerg.Params["token"] != "yyyy"`)
}
// filters
if len(c.Filters) != 2 {
t.Fatal(`len(c.Filters) != 2`)
}
f1 := c.Filters[0]
if f1.Type != "route" {
t.Error(`f1.Type != "route"`)
}
if f1.Params["id"] != "default" {
t.Error(`f1.Params["id"] != "default"`)
}
if !reflect.DeepEqual(f1.Params["routes"], []interface{}{"notify"}) {
t.Error(`f1.Params["routes"] != ["notify"]`)
}
}
func TestConfig(t *testing.T) {
t.Run("Default", testConfigDefault)
t.Run("Load", testConfigLoad)
}