-
Notifications
You must be signed in to change notification settings - Fork 18
/
config_test.go
213 lines (178 loc) · 5.73 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"fmt"
"io/ioutil"
"os"
"testing"
"gopkg.in/yaml.v2"
)
func TestNoConfig(t *testing.T) {
noConfigFile := ""
config, err := LoadConfig(noConfigFile)
if config == nil {
t.Errorf("Expected a default config, got: %s", err)
}
}
func TestLoadGoodConfig(t *testing.T) {
expectedConfig := &Config{
HTTPHost: "test.web",
HTTPPort: 8888,
IRCNick: "foo",
IRCHost: "irc.example.com",
IRCPort: 1234,
IRCHostPass: "hostsecret",
IRCUseSSL: true,
IRCChannels: []IRCChannel{IRCChannel{Name: "#foobar"}},
MsgTemplate: defaultMsgTemplate,
MsgOnce: false,
UsePrivmsg: false,
AlertBufferSize: 666,
}
expectedData, err := yaml.Marshal(expectedConfig)
if err != nil {
t.Errorf("Could not serialize test data: %s", err)
}
tmpfile, err := ioutil.TempFile("", "airtestconfig")
if err != nil {
t.Errorf("Could not create tmpfile for testing: %s", err)
}
defer os.Remove(tmpfile.Name())
if _, err := tmpfile.Write(expectedData); err != nil {
t.Errorf("Could not write test data in tmpfile: %s", err)
}
if err := tmpfile.Close(); err != nil {
t.Errorf("Could not close tmpfile: %s", err)
}
config, err := LoadConfig(tmpfile.Name())
if config == nil {
t.Errorf("Expected a config, got: %s", err)
}
configData, err := yaml.Marshal(config)
if err != nil {
t.Errorf("Could not serialize loaded config")
}
if string(expectedData) != string(configData) {
t.Errorf("Loaded config does not match expected config: %s", configData)
}
}
func TestLoadWithEnvironmentVariables(t *testing.T) {
expectedNickPass := "mynickpass"
os.Setenv("NICKSERV_PASSWORD", expectedNickPass)
defer os.Clearenv()
tmpfile, err := ioutil.TempFile("", "airtestenvvarconfig")
if err != nil {
t.Errorf("Could not create tmpfile for testing: %s", err)
}
defer os.Remove(tmpfile.Name())
msgOnceConfigData := []byte("irc_nickname_password: $NICKSERV_PASSWORD")
if _, err := tmpfile.Write(msgOnceConfigData); err != nil {
t.Errorf("Could not write test data in tmpfile: %s", err)
}
tmpfile.Close()
config, err := LoadConfig(tmpfile.Name())
if config == nil {
t.Errorf("Expected a config, got: %s", err)
}
if config.IRCNickPass != expectedNickPass {
t.Errorf("Loaded unexpected value: %s (expected: %s)",
config.IRCNickPass, expectedNickPass)
}
}
func TestLoadBadFile(t *testing.T) {
tmpfile, err := ioutil.TempFile("", "airtestbadfile")
if err != nil {
t.Errorf("Could not create tmpfile for testing: %s", err)
}
tmpfile.Close()
os.Remove(tmpfile.Name())
config, err := LoadConfig(tmpfile.Name())
if err == nil || config != nil {
t.Errorf("Expected no config upon non-existent file. err: %s", err)
}
}
func TestLoadBadConfig(t *testing.T) {
tmpfile, err := ioutil.TempFile("", "airtestbadconfig")
if err != nil {
t.Errorf("Could not create tmpfile for testing: %s", err)
}
defer os.Remove(tmpfile.Name())
badConfigData := []byte("footest\nbarbaz\n")
if _, err := tmpfile.Write(badConfigData); err != nil {
t.Errorf("Could not write test data in tmpfile: %s", err)
}
tmpfile.Close()
config, err := LoadConfig(tmpfile.Name())
if err == nil || config != nil {
t.Errorf("Expected no config upon bad config. err: %s", err)
}
}
func TestMsgOnceDefaultTemplate(t *testing.T) {
tmpfile, err := ioutil.TempFile("", "airtesttemmplateonceconfig")
if err != nil {
t.Errorf("Could not create tmpfile for testing: %s", err)
}
defer os.Remove(tmpfile.Name())
msgOnceConfigData := []byte("msg_once_per_alert_group: yes")
if _, err := tmpfile.Write(msgOnceConfigData); err != nil {
t.Errorf("Could not write test data in tmpfile: %s", err)
}
tmpfile.Close()
config, err := LoadConfig(tmpfile.Name())
if config == nil {
t.Errorf("Expected a config, got: %s", err)
}
if config.MsgTemplate != defaultMsgOnceTemplate {
t.Errorf("Expecting defaultMsgOnceTemplate when MsgOnce is true")
}
}
func TestMsgDefaultTemplate(t *testing.T) {
tmpfile, err := ioutil.TempFile("", "airtesttemmplateconfig")
if err != nil {
t.Errorf("Could not create tmpfile for testing: %s", err)
}
defer os.Remove(tmpfile.Name())
if _, err := tmpfile.Write([]byte("")); err != nil {
t.Errorf("Could not write test data in tmpfile: %s", err)
}
tmpfile.Close()
config, err := LoadConfig(tmpfile.Name())
if config == nil {
t.Errorf("Expected a config, got: %s", err)
}
if config.MsgTemplate != defaultMsgTemplate {
t.Errorf("Expecting defaultMsgTemplate when MsgOnce is false")
}
}
func TestGivenTemplateNotOverwritten(t *testing.T) {
tmpfile, err := ioutil.TempFile("", "airtestexpectedtemmplate")
if err != nil {
t.Errorf("Could not create tmpfile for testing: %s", err)
}
defer os.Remove(tmpfile.Name())
expectedTemplate := "Alert {{ .Status }}: {{ .Annotations.SUMMARY }}"
configData := []byte(fmt.Sprintf("msg_template: \"%s\"", expectedTemplate))
if _, err := tmpfile.Write(configData); err != nil {
t.Errorf("Could not write test data in tmpfile: %s", err)
}
tmpfile.Close()
config, err := LoadConfig(tmpfile.Name())
if config == nil {
t.Errorf("Expected a config, got: %s", err)
}
if config.MsgTemplate != expectedTemplate {
t.Errorf("Template does not match configuration")
}
}