-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcustom_test.go
187 lines (177 loc) · 9.99 KB
/
custom_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
package main
import (
"reflect"
"testing"
)
var lookupUseCases = []struct {
custom *customFields
key *fieldClassifier
initialMap map[string]interface{}
expectedMap map[string]interface{}
}{
// Emtpy custom fields map & passing empty map
{custom: &customFields{}, key: &fieldClassifier{hostgroup: "web", service: "serviceA"}, initialMap: map[string]interface{}{}, expectedMap: map[string]interface{}{}},
// Emtpy custom fields map & passing non empty map
{custom: &customFields{}, key: &fieldClassifier{hostgroup: "web", service: "serviceA"}, initialMap: map[string]interface{}{"foo": "Bar"}, expectedMap: map[string]interface{}{"foo": "Bar"}},
// Lookup for an existing classifier with data and passing a empty map
{
custom: &customFields{fields: map[fieldClassifier]map[string]interface{}{fieldClassifier{hostgroup: "web", service: "serviceA"}: map[string]interface{}{"foo": "Bar"}}},
key: &fieldClassifier{hostgroup: "web", service: "serviceA"},
initialMap: map[string]interface{}{},
expectedMap: map[string]interface{}{"foo": "Bar"},
},
// Lookup for an existing classifier with data and passing a non empty map
{
custom: &customFields{fields: map[fieldClassifier]map[string]interface{}{fieldClassifier{hostgroup: "web", service: "serviceA"}: map[string]interface{}{"foo": "NewVal"}}},
key: &fieldClassifier{hostgroup: "web", service: "serviceA"},
initialMap: map[string]interface{}{"foo": "Bar"},
expectedMap: map[string]interface{}{"foo": "NewVal"},
},
// Lookup for a non existing classifier with data and passing a non empty map
{
custom: &customFields{fields: map[fieldClassifier]map[string]interface{}{fieldClassifier{hostgroup: "db", service: "serviceB"}: map[string]interface{}{"foo": "Unrelated val"}}},
key: &fieldClassifier{hostgroup: "web", service: "serviceA"},
initialMap: map[string]interface{}{"foo": "Bar"},
expectedMap: map[string]interface{}{"foo": "Bar"},
},
// Lookup for a partially existing classifier with data and passing a non empty map
{
custom: &customFields{fields: map[fieldClassifier]map[string]interface{}{fieldClassifier{hostgroup: "web", service: "serviceB"}: map[string]interface{}{"foo": "Unrelated val"}}},
key: &fieldClassifier{hostgroup: "web", service: "serviceA"},
initialMap: map[string]interface{}{"foo": "Bar"},
expectedMap: map[string]interface{}{"foo": "Bar"},
},
// Lookup for a partially existing classifier with data and passing a non empty map
{
custom: &customFields{fields: map[fieldClassifier]map[string]interface{}{fieldClassifier{hostgroup: "db", service: "serviceA"}: map[string]interface{}{"foo": "Unrelated val"}}},
key: &fieldClassifier{hostgroup: "web", service: "serviceA"},
initialMap: map[string]interface{}{"foo": "Bar"},
expectedMap: map[string]interface{}{"foo": "Bar"},
},
}
func TestLookup(t *testing.T) {
for _, uc := range lookupUseCases {
uc.custom.lookup(uc.initialMap, uc.key)
if !reflect.DeepEqual(uc.initialMap, uc.expectedMap) {
t.Fatalf("Expecting map to be %v after lookup. Got: %v\n", uc.expectedMap, uc.initialMap)
}
}
}
var getUseCases = []struct {
custom *customFields
hostName string
checkName string
expectedMap map[string]interface{}
}{
{custom: &customFields{}, hostName: "web01", checkName: "serviceA", expectedMap: map[string]interface{}{}},
// One level of hierarchy matching at a time
{
custom: &customFields{fields: map[fieldClassifier]map[string]interface{}{fieldClassifier{hostgroup: "##common##", service: "all"}: map[string]interface{}{"foo": "Bar"}}},
hostName: "web01", checkName: "serviceA", expectedMap: map[string]interface{}{"foo": "Bar"},
},
{
custom: &customFields{fields: map[fieldClassifier]map[string]interface{}{fieldClassifier{hostgroup: "web", service: "all"}: map[string]interface{}{"foo": "Bar"}}},
hostName: "web01", checkName: "serviceA", expectedMap: map[string]interface{}{"foo": "Bar"},
},
{
custom: &customFields{fields: map[fieldClassifier]map[string]interface{}{fieldClassifier{hostgroup: "web", service: "serviceA"}: map[string]interface{}{"foo": "Bar", "my beautiful field": "Value 2"}}},
hostName: "web01", checkName: "serviceA", expectedMap: map[string]interface{}{"foo": "Bar", "my beautiful field": "Value 2"},
},
// several levels matching at a time
{
custom: &customFields{fields: map[fieldClassifier]map[string]interface{}{
fieldClassifier{hostgroup: "##common##", service: "all"}: map[string]interface{}{"foo": "Low level"},
fieldClassifier{hostgroup: "web", service: "all"}: map[string]interface{}{"foo": "Bar"},
fieldClassifier{hostgroup: "web", service: "serviceB"}: map[string]interface{}{"foo": "Not Matching"},
}},
hostName: "web01", checkName: "serviceA", expectedMap: map[string]interface{}{"foo": "Bar"},
},
{
custom: &customFields{fields: map[fieldClassifier]map[string]interface{}{
fieldClassifier{hostgroup: "##common##", service: "all"}: map[string]interface{}{"foo": "Low level"},
fieldClassifier{hostgroup: "web", service: "all"}: map[string]interface{}{"my beautiful field": "Value 2"},
fieldClassifier{hostgroup: "web", service: "serviceA"}: map[string]interface{}{"foo": "Bar"},
fieldClassifier{hostgroup: "db", service: "all"}: map[string]interface{}{"Other value": "Non matching"},
fieldClassifier{hostgroup: "db", service: "serviceA"}: map[string]interface{}{"foo": "db custom field"},
}},
hostName: "web01", checkName: "serviceA", expectedMap: map[string]interface{}{"foo": "Bar", "my beautiful field": "Value 2"},
},
{
custom: &customFields{fields: map[fieldClassifier]map[string]interface{}{
fieldClassifier{hostgroup: "##common##", service: "all"}: map[string]interface{}{"foo": "Low level"},
fieldClassifier{hostgroup: "web", service: "all"}: map[string]interface{}{"foo": "still low level", "my beautiful field": "Value 2"},
fieldClassifier{hostgroup: "web", service: "serviceA"}: map[string]interface{}{"foo": "Bar"},
fieldClassifier{hostgroup: "db", service: "all"}: map[string]interface{}{"Other value": "Non matching"},
fieldClassifier{hostgroup: "db", service: "serviceA"}: map[string]interface{}{"foo": "db custom field"},
}},
hostName: "web01", checkName: "serviceA", expectedMap: map[string]interface{}{"foo": "Bar", "my beautiful field": "Value 2"},
},
}
func TestGet(t *testing.T) {
for _, uc := range getUseCases {
returnedMap := uc.custom.get(uc.hostName, uc.checkName)
if !reflect.DeepEqual(returnedMap, uc.expectedMap) {
t.Fatalf("Expecting get to return %v. Got: %v\n", uc.expectedMap, returnedMap)
}
}
}
var processYamlFileUseCases = []struct {
filePath string
expectedMap map[string]interface{}
expectedErr string
}{
{filePath: "testData/customFields/common.yaml", expectedMap: map[string]interface{}{"paging": false, "team": []interface{}{"ops"}}, expectedErr: ""},
{filePath: "testData/customFields/service/db/all.yaml", expectedMap: map[string]interface{}{"paging": true, "team": []interface{}{"dba", "ops"}, "runbook": "https://wiki.example.org/teams/dba/runbooks.html"}, expectedErr: ""},
{filePath: "testData/customFields/service/db/nonExistingFile.yaml", expectedMap: map[string]interface{}{}, expectedErr: "open testData/customFields/service/db/nonExistingFile.yaml: no such file or directory"},
}
func TestProcessYamlFile(t *testing.T) {
for _, uc := range processYamlFileUseCases {
custom := &customFields{}
returnedMap, err := custom.processYamlFile(uc.filePath)
if !reflect.DeepEqual(returnedMap, uc.expectedMap) {
t.Fatalf("Expecting processYamlFile of %s to return %v. Got: %v\n", uc.filePath, uc.expectedMap, returnedMap)
}
if err != nil {
if err.Error() != uc.expectedErr {
t.Fatalf("Expecting processYamlFile of %s to return the error \"%s\". Got: \"%s\"\n", uc.filePath, uc.expectedErr, err)
}
} else {
if uc.expectedErr != "" {
t.Fatalf("Expecting processYamlFile of %s to return the error \"%s\". Got: \"%s\"\n", uc.filePath, uc.expectedErr, err)
}
}
}
}
var loadUseCases = []struct {
filePath string
expectedMap *customFields
expectedErr string
}{
{filePath: "testData/customFields", expectedMap: &customFields{fields: map[fieldClassifier]map[string]interface{}{
fieldClassifier{hostgroup: "##common##", service: "all"}: map[string]interface{}{"paging": false, "team": []interface{}{"ops"}},
fieldClassifier{hostgroup: "web", service: "all"}: map[string]interface{}{"team": []interface{}{"webdev"}, "runbook": "https://wiki.example.org/teams/web/runbooks.html"},
fieldClassifier{hostgroup: "web", service: "apache"}: map[string]interface{}{"paging": true, "runbook": "https://wiki.example.org/teams/cross/runbooks/apache.html"},
fieldClassifier{hostgroup: "web", service: "nginx_port"}: map[string]interface{}{"paging": true},
fieldClassifier{hostgroup: "db", service: "all"}: map[string]interface{}{"paging": true, "team": []interface{}{"dba", "ops"}, "runbook": "https://wiki.example.org/teams/dba/runbooks.html"},
fieldClassifier{hostgroup: "app", service: "swiftWF_error_rate"}: map[string]interface{}{"paging": true, "team": []interface{}{"stats"}, "runbook": "https://wiki.example.org/teams/cross/runbooks/swif_workflows.html", "alertGroup": "AWS_Swift"},
}}, expectedErr: ""},
{filePath: "nonExistentPath", expectedMap: &customFields{fields: map[fieldClassifier]map[string]interface{}{}}, expectedErr: "stat nonExistentPath: no such file or directory"},
}
func TestLoad(t *testing.T) {
for _, uc := range loadUseCases {
custom := &customFields{}
err := custom.load(uc.filePath)
if !reflect.DeepEqual(uc.expectedMap, custom) {
t.Fatalf("Expecting load to generate the map %v. Got: %v\n", uc.expectedMap, custom)
}
if err != nil {
if err.Error() != uc.expectedErr {
t.Fatalf("Expecting load of %s to return the error \"%s\". Got: \"%s\"\n", uc.filePath, uc.expectedErr, err)
}
} else {
if uc.expectedErr != "" {
t.Fatalf("Expecting load of %s to return the error \"%s\". Got: \"%s\"\n", uc.filePath, uc.expectedErr, err)
}
}
}
}