-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
trigger_agent_spec.rb
450 lines (359 loc) · 13.5 KB
/
trigger_agent_spec.rb
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
require 'rails_helper'
describe Agents::TriggerAgent do
before do
@valid_params = {
'name' => "my trigger agent",
'options' => {
'expected_receive_period_in_days' => 2,
'rules' => [{
'type' => "regex",
'value' => "a\\db",
'path' => "foo.bar.baz",
}],
'message' => "I saw '{{foo.bar.baz}}' from {{name}}"
}
}
@checker = Agents::TriggerAgent.new(@valid_params)
@checker.user = users(:bob)
@checker.save!
@event = Event.new
@event.agent = agents(:bob_rain_notifier_agent)
@event.payload = { 'foo' => { "bar" => { 'baz' => "a2b" } },
'name' => "Joe" }
end
describe "validation" do
before do
expect(@checker).to be_valid
end
it "should validate presence of message" do
@checker.options['message'] = nil
expect(@checker).not_to be_valid
@checker.options['message'] = ''
expect(@checker).not_to be_valid
end
it "should be valid without a message when 'keep_event' is set" do
@checker.options['keep_event'] = 'true'
@checker.options['message'] = ''
expect(@checker).to be_valid
end
it "if present, 'keep_event' must equal true or false" do
@checker.options['keep_event'] = 'true'
expect(@checker).to be_valid
@checker.options['keep_event'] = 'false'
expect(@checker).to be_valid
@checker.options['keep_event'] = ''
expect(@checker).to be_valid
@checker.options['keep_event'] = 'tralse'
expect(@checker).not_to be_valid
end
it "validates that 'must_match' is a positive integer, not greater than the number of rules, if provided" do
@checker.options['must_match'] = '1'
expect(@checker).to be_valid
@checker.options['must_match'] = '0'
expect(@checker).not_to be_valid
@checker.options['must_match'] = 'wrong'
expect(@checker).not_to be_valid
@checker.options['must_match'] = ''
expect(@checker).to be_valid
@checker.options.delete('must_match')
expect(@checker).to be_valid
@checker.options['must_match'] = '2'
expect(@checker).not_to be_valid
expect(@checker.errors[:base].first).to match(/equal to or less than the number of rules/)
end
it "should validate the three fields in each rule" do
@checker.options['rules'] << { 'path' => "foo", 'type' => "fake", 'value' => "6" }
expect(@checker).not_to be_valid
@checker.options['rules'].last['type'] = "field!=value"
expect(@checker).to be_valid
@checker.options['rules'].last.delete('value')
expect(@checker).not_to be_valid
@checker.options['rules'].last['value'] = ''
expect(@checker).to be_valid
@checker.options['rules'].last['path'] = ''
expect(@checker).not_to be_valid
end
it "should validate non-hash rules" do
@checker.options['rules'] << "{% if status == 'ok' %}true{% endif %}"
expect(@checker).to be_valid
@checker.options['rules'] << []
expect(@checker).not_to be_valid
end
end
describe "#working?" do
it "checks to see if the Agent has received any events in the last 'expected_receive_period_in_days' days" do
@event.save!
expect(@checker).not_to be_working # no events have ever been received
Agents::TriggerAgent.async_receive(@checker.id, [@event.id])
expect(@checker.reload).to be_working # Events received
three_days_from_now = 3.days.from_now
allow(Time).to receive(:now) { three_days_from_now }
expect(@checker.reload).not_to be_working # too much time has passed
end
end
describe "#receive" do
it "handles regex" do
@event.payload['foo']['bar']['baz'] = "a222b"
expect {
@checker.receive([@event])
}.not_to(change { Event.count })
@event.payload['foo']['bar']['baz'] = "a2b"
expect {
@checker.receive([@event])
}.to change { Event.count }.by(1)
end
it "handles array of regex" do
@event.payload['foo']['bar']['baz'] = "a222b"
@checker.options['rules'][0] = {
'type' => "regex",
'value' => ["a\\db", "a\\Wb"],
'path' => "foo.bar.baz",
}
expect {
@checker.receive([@event])
}.not_to(change { Event.count })
@event.payload['foo']['bar']['baz'] = "a2b"
expect {
@checker.receive([@event])
}.to change { Event.count }.by(1)
@event.payload['foo']['bar']['baz'] = "a b"
expect {
@checker.receive([@event])
}.to change { Event.count }.by(1)
end
it "handles negated regex" do
@event.payload['foo']['bar']['baz'] = "a2b"
@checker.options['rules'][0] = {
'type' => "!regex",
'value' => "a\\db",
'path' => "foo.bar.baz",
}
expect {
@checker.receive([@event])
}.not_to(change { Event.count })
@event.payload['foo']['bar']['baz'] = "a22b"
expect {
@checker.receive([@event])
}.to change { Event.count }.by(1)
end
it "handles array of negated regex" do
@event.payload['foo']['bar']['baz'] = "a2b"
@checker.options['rules'][0] = {
'type' => "!regex",
'value' => ["a\\db", "a2b"],
'path' => "foo.bar.baz",
}
expect {
@checker.receive([@event])
}.not_to(change { Event.count })
@event.payload['foo']['bar']['baz'] = "a3b"
expect {
@checker.receive([@event])
}.to change { Event.count }.by(1)
end
it "puts can extract values into the message based on paths" do
@checker.receive([@event])
expect(Event.last.payload['message']).to eq("I saw 'a2b' from Joe")
end
it "handles numerical comparisons" do
@event.payload['foo']['bar']['baz'] = "5"
@checker.options['rules'].first['value'] = 6
@checker.options['rules'].first['type'] = "field<value"
expect {
@checker.receive([@event])
}.to change { Event.count }.by(1)
@checker.options['rules'].first['value'] = 3
expect {
@checker.receive([@event])
}.not_to(change { Event.count })
end
it "handles array of numerical comparisons" do
@event.payload['foo']['bar']['baz'] = "5"
@checker.options['rules'].first['value'] = [6, 3]
@checker.options['rules'].first['type'] = "field<value"
expect {
@checker.receive([@event])
}.to change { Event.count }.by(1)
@checker.options['rules'].first['value'] = [4, 3]
expect {
@checker.receive([@event])
}.not_to(change { Event.count })
end
it "handles exact comparisons" do
@event.payload['foo']['bar']['baz'] = "hello world"
@checker.options['rules'].first['type'] = "field==value"
@checker.options['rules'].first['value'] = "hello there"
expect {
@checker.receive([@event])
}.not_to(change { Event.count })
@checker.options['rules'].first['value'] = "hello world"
expect {
@checker.receive([@event])
}.to change { Event.count }.by(1)
end
it "handles array of exact comparisons" do
@event.payload['foo']['bar']['baz'] = "hello world"
@checker.options['rules'].first['type'] = "field==value"
@checker.options['rules'].first['value'] = ["hello there", "hello universe"]
expect {
@checker.receive([@event])
}.not_to(change { Event.count })
@checker.options['rules'].first['value'] = ["hello world", "hello universe"]
expect {
@checker.receive([@event])
}.to change { Event.count }.by(1)
end
it "handles negated comparisons" do
@event.payload['foo']['bar']['baz'] = "hello world"
@checker.options['rules'].first['type'] = "field!=value"
@checker.options['rules'].first['value'] = "hello world"
expect {
@checker.receive([@event])
}.not_to(change { Event.count })
@checker.options['rules'].first['value'] = "hello there"
expect {
@checker.receive([@event])
}.to change { Event.count }.by(1)
end
it "handles array of negated comparisons" do
@event.payload['foo']['bar']['baz'] = "hello world"
@checker.options['rules'].first['type'] = "field!=value"
@checker.options['rules'].first['value'] = ["hello world", "hello world"]
expect {
@checker.receive([@event])
}.not_to(change { Event.count })
@checker.options['rules'].first['value'] = ["hello there", "hello world"]
expect {
@checker.receive([@event])
}.to change { Event.count }.by(1)
end
it "handles array of `not in` comparisons" do
@event.payload['foo']['bar']['baz'] = "hello world"
@checker.options['rules'].first['type'] = "not in"
@checker.options['rules'].first['value'] = ["hello world", "hello world"]
expect {
@checker.receive([@event])
}.not_to(change { Event.count })
@checker.options['rules'].first['value'] = ["hello there", "hello world"]
expect {
@checker.receive([@event])
}.not_to(change { Event.count })
@checker.options['rules'].first['value'] = ["hello there", "hello here"]
expect {
@checker.receive([@event])
}.to change { Event.count }.by(1)
end
it "does fine without dots in the path" do
@event.payload = { 'hello' => "world" }
@checker.options['rules'].first['type'] = "field==value"
@checker.options['rules'].first['path'] = "hello"
@checker.options['rules'].first['value'] = "world"
expect {
@checker.receive([@event])
}.to change { Event.count }.by(1)
@checker.options['rules'].first['path'] = "foo"
expect {
@checker.receive([@event])
}.not_to(change { Event.count })
@checker.options['rules'].first['value'] = "hi"
expect {
@checker.receive([@event])
}.not_to(change { Event.count })
end
it "handles multiple events" do
event2 = Event.new
event2.agent = agents(:bob_weather_agent)
event2.payload = { 'foo' => { 'bar' => { 'baz' => "a2b" } } }
event3 = Event.new
event3.agent = agents(:bob_weather_agent)
event3.payload = { 'foo' => { 'bar' => { 'baz' => "a222b" } } }
expect {
@checker.receive([@event, event2, event3])
}.to change { Event.count }.by(2)
end
it "handles Liquid rules" do
event1 = Event.create!(
agent: agents(:bob_rain_notifier_agent),
payload: { 'hello' => 'world1', 'created_at' => '2019-03-27T08:54:12+09:00' }
)
event2 = Event.create!(
agent: agents(:bob_rain_notifier_agent),
payload: { 'hello' => 'world2', 'created_at' => '2019-03-27T09:17:01+09:00' }
)
@checker.options['message'] = '{{hello}}'
@checker.options['rules'] = [
"{% assign value = created_at | date: '%s' | plus: 0 %}{% assign threshold = 'now' | date: '%s' | minus: 86400 %}{% if value > threshold %}true{% endif %}"
]
expect {
travel_to(Time.parse('2019-03-28T00:00:00+00:00')) {
@checker.receive([event1, event2])
}
}.to change { Event.count }.by(1)
expect(Event.last.payload['message']).to eq("world2")
end
describe "with multiple rules" do
before do
@checker.options['rules'] << {
'type' => "field>=value",
'value' => "4",
'path' => "foo.bing"
}
end
it "handles ANDing rules together" do
@event.payload['foo']["bing"] = "5"
expect {
@checker.receive([@event])
}.to change { Event.count }.by(1)
@event.payload['foo']["bing"] = "2"
expect {
@checker.receive([@event])
}.not_to(change { Event.count })
end
it "can accept a partial rule set match when 'must_match' is present and less than the total number of rules" do
@checker.options['must_match'] = "1"
@event.payload['foo']["bing"] = "5" # 5 > 4
expect {
@checker.receive([@event])
}.to change { Event.count }.by(1)
@event.payload['foo']["bing"] = "2" # 2 !> 4
expect {
@checker.receive([@event])
}.to(change { Event.count }) # but the first one matches
@checker.options['must_match'] = "2"
@event.payload['foo']["bing"] = "5" # 5 > 4
expect {
@checker.receive([@event])
}.to change { Event.count }.by(1)
@event.payload['foo']["bing"] = "2" # 2 !> 4
expect {
@checker.receive([@event])
}.not_to(change { Event.count }) # only 1 matches, we needed 2
end
end
describe "when 'keep_event' is true" do
before do
@checker.options['keep_event'] = 'true'
@event.payload['foo']['bar']['baz'] = "5"
@checker.options['rules'].first['type'] = "field<value"
end
it "can re-emit the origin event" do
@checker.options['rules'].first['value'] = 3
@checker.options['message'] = ''
@event.payload['message'] = 'hi there'
expect {
@checker.receive([@event])
}.not_to(change { Event.count })
@checker.options['rules'].first['value'] = 6
expect {
@checker.receive([@event])
}.to change { Event.count }.by(1)
expect(@checker.most_recent_event.payload).to eq(@event.payload)
end
it "merges 'message' into the original event when present" do
@checker.options['rules'].first['value'] = 6
@checker.receive([@event])
expect(@checker.most_recent_event.payload).to eq(@event.payload.merge(message: "I saw '5' from Joe"))
end
end
end
end