-
Notifications
You must be signed in to change notification settings - Fork 0
/
ticket_test.go
228 lines (207 loc) · 5.03 KB
/
ticket_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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package main
import (
"fmt"
"strings"
"testing"
"github.com/bytemine/go-icinga2/event"
"github.com/bytemine/icinga2rt/rt"
)
const testMappingsCSV = `# state, old state, owned, action
# ignore OK events if no old state is known
OK,,false,ignore
# delete ticket if unowned and was WARNING, CRITICAL or UNKNOWN
OK,WARNING,false,delete
OK,CRITICAL,false,delete
OK,UNKNOWN,false,delete
# comment ticket if unowned and was WARNING, CRITICAL or UNKNOWN
OK,WARNING,true,comment
OK,CRITICAL,true,comment
OK,UNKNOWN,true,comment
# create tickets for WARNING, CRITICAL or UNKNOWN if not exisiting
WARNING,,false,create
CRITICAL,,false,create
UNKNOWN,,false,create
# ignore if state hasn't changed
WARNING,WARNING,false,ignore
WARNING,WARNING,true,ignore
CRITICAL,CRITICAL,false,ignore
CRITICAL,CRITICAL,true,ignore
UNKNOWN,UNKNOWN,false,ignore
UNKNOWN,UNKNOWN,true,ignore
# comment tickets on state changes
WARNING,CRITICAL,false,comment
WARNING,CRITICAL,true,comment
WARNING,UNKNOWN,false,comment
WARNING,UNKNOWN,true,comment
CRITICAL,WARNING,false,comment
CRITICAL,WARNING,true,comment
CRITICAL,UNKNOWN,false,comment
CRITICAL,UNKNOWN,true,comment
UNKNOWN,WARNING,false,comment
UNKNOWN,WARNING,true,comment
UNKNOWN,CRITICAL,false,comment
UNKNOWN,CRITICAL,true,comment
`
// the order is important.
// we can't check everything here. it isn't checked if the comments are really attached to a ticket, or the status of a ticket,
// as that would require a complete RT-mock. maybe using an interface would be good for the rt client.
var tests = []struct {
Event *event.Notification
ExistsBefore bool // exists in cache before processing, checks event and ticket id
ExistsAfter bool // exists in cache after processing, checks event and ticket id
}{
{
Event: &event.Notification{
Host: "example.com",
Service: "example",
CheckResult: event.CheckResultData{
State: event.StateWarning,
},
},
ExistsBefore: false,
ExistsAfter: true,
},
{
Event: &event.Notification{
Host: "example.com",
Service: "example",
CheckResult: event.CheckResultData{
State: event.StateCritical,
},
},
ExistsBefore: true,
ExistsAfter: true,
},
{
Event: &event.Notification{
Host: "example.com",
Service: "example",
CheckResult: event.CheckResultData{
State: event.StateOK,
},
},
ExistsBefore: true,
ExistsAfter: false,
},
{
Event: &event.Notification{
Host: "example.com",
Service: "example",
CheckResult: event.CheckResultData{
State: event.StateOK,
},
},
ExistsBefore: false,
ExistsAfter: false,
},
{
Event: &event.Notification{
Host: "example.com",
Service: "example",
CheckResult: event.CheckResultData{
State: event.StateCritical,
},
},
ExistsBefore: false,
ExistsAfter: true,
},
{
Event: &event.Notification{
Host: "example.com",
Service: "example",
CheckResult: event.CheckResultData{
State: event.StateWarning,
},
},
ExistsBefore: true,
ExistsAfter: true,
},
{
Event: &event.Notification{
Host: "example.com",
Service: "example",
CheckResult: event.CheckResultData{
State: event.StateOK,
},
},
ExistsBefore: true,
ExistsAfter: false,
},
}
func TestTicketUpdaterUpdate(t *testing.T) {
testMappings, err := readMappings(strings.NewReader(testMappingsCSV))
if err != nil {
t.Error(err)
}
rt := NewDummyRT()
cache, cachePath, err := tempCache()
if err != nil {
t.Error(err)
}
defer removeCache(cache, cachePath)
tu := newTicketUpdater(cache, rt, testMappings, "", "Test-Queue", []string{"deleted"})
for _, v := range tests {
t.Logf("%+v", v)
if v.ExistsBefore {
x, ticketID, err := cache.getEventTicket(v.Event)
if err != nil {
t.Error(err)
}
if x == nil {
t.Log("before: event in cache is nil")
t.Fail()
}
if ticketID == -1 {
t.Log("before: ticket id is nil")
t.Fail()
}
}
err := tu.update(v.Event)
if err != nil {
t.Error(err)
}
if v.ExistsAfter {
x, ticketID, err := cache.getEventTicket(v.Event)
if err != nil {
t.Error(err)
}
if x == nil {
t.Log("after: event in cache is nil")
t.Fail()
}
if ticketID == -1 {
t.Log("after: ticket id is nil")
t.Fail()
}
if v.Event.CheckResult.State != x.CheckResult.State {
t.Logf("after: event state: %v expected: %v", x.CheckResult.State, v.Event.CheckResult.State)
t.Fail()
}
}
}
}
// DummyClient is a mock RT client used for testing.
type DummyRT struct {
tickets []rt.Ticket
}
func NewDummyRT() *DummyRT {
return &DummyRT{tickets: make([]rt.Ticket, 0)}
}
func (d *DummyRT) Ticket(id int) (*rt.Ticket, error) {
if len(d.tickets) > id {
return &d.tickets[id], nil
}
return nil, fmt.Errorf("no ticket")
}
func (d *DummyRT) NewTicket(ticket *rt.Ticket) (*rt.Ticket, error) {
ticket.ID = len(d.tickets)
d.tickets = append(d.tickets, *ticket)
return ticket, nil
}
func (d *DummyRT) UpdateTicket(ticket *rt.Ticket) (*rt.Ticket, error) {
d.tickets[ticket.ID] = *ticket
return ticket, nil
}
func (d *DummyRT) CommentTicket(id int, comment string) error {
return nil
}