flapjack 0.8.10 → 0.8.11

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +9 -0
  3. data/Gemfile +1 -1
  4. data/bin/flapjack +10 -1
  5. data/bin/flapjack-nagios-receiver +1 -2
  6. data/bin/simulate-failed-check +12 -4
  7. data/etc/flapjack_config.yaml.example +2 -1
  8. data/flapjack.gemspec +1 -0
  9. data/lib/flapjack/data/contact.rb +46 -26
  10. data/lib/flapjack/data/entity.rb +28 -0
  11. data/lib/flapjack/data/entity_check.rb +52 -11
  12. data/lib/flapjack/data/event.rb +9 -3
  13. data/lib/flapjack/data/notification_rule.rb +8 -0
  14. data/lib/flapjack/gateways/api.rb +0 -1
  15. data/lib/flapjack/gateways/api/entity_check_presenter.rb +2 -1
  16. data/lib/flapjack/gateways/email.rb +1 -2
  17. data/lib/flapjack/gateways/jabber.rb +3 -3
  18. data/lib/flapjack/gateways/jsonapi.rb +186 -38
  19. data/lib/flapjack/gateways/jsonapi/check_methods.rb +120 -0
  20. data/lib/flapjack/gateways/jsonapi/{entity_check_presenter.rb → check_presenter.rb} +7 -6
  21. data/lib/flapjack/gateways/jsonapi/contact_methods.rb +61 -352
  22. data/lib/flapjack/gateways/jsonapi/entity_methods.rb +117 -248
  23. data/lib/flapjack/gateways/jsonapi/medium_methods.rb +179 -0
  24. data/lib/flapjack/gateways/jsonapi/notification_rule_methods.rb +124 -0
  25. data/lib/flapjack/gateways/jsonapi/pagerduty_credential_methods.rb +128 -0
  26. data/lib/flapjack/gateways/jsonapi/rack/json_params_parser.rb +4 -5
  27. data/lib/flapjack/gateways/jsonapi/report_methods.rb +143 -0
  28. data/lib/flapjack/gateways/web.rb +1 -0
  29. data/lib/flapjack/gateways/web/public/js/backbone.jsonapi.js +165 -101
  30. data/lib/flapjack/gateways/web/public/js/contacts.js +34 -46
  31. data/lib/flapjack/gateways/web/public/js/select2.js +232 -90
  32. data/lib/flapjack/gateways/web/public/js/select2.min.js +4 -4
  33. data/lib/flapjack/gateways/web/views/check.html.erb +11 -2
  34. data/lib/flapjack/processor.rb +6 -6
  35. data/lib/flapjack/version.rb +1 -1
  36. data/spec/lib/flapjack/data/entity_check_spec.rb +1 -1
  37. data/spec/lib/flapjack/data/event_spec.rb +10 -9
  38. data/spec/lib/flapjack/gateways/api/entity_methods_spec.rb +25 -25
  39. data/spec/lib/flapjack/gateways/api_spec.rb +23 -1
  40. data/spec/lib/flapjack/gateways/email_spec.rb +40 -2
  41. data/spec/lib/flapjack/gateways/jabber_spec.rb +1 -1
  42. data/spec/lib/flapjack/gateways/jsonapi/check_methods_spec.rb +134 -0
  43. data/spec/lib/flapjack/gateways/jsonapi/{entity_check_presenter_spec.rb → check_presenter_spec.rb} +17 -17
  44. data/spec/lib/flapjack/gateways/jsonapi/contact_methods_spec.rb +27 -232
  45. data/spec/lib/flapjack/gateways/jsonapi/entity_methods_spec.rb +217 -687
  46. data/spec/lib/flapjack/gateways/jsonapi/medium_methods_spec.rb +232 -0
  47. data/spec/lib/flapjack/gateways/jsonapi/notification_rule_methods_spec.rb +131 -0
  48. data/spec/lib/flapjack/gateways/jsonapi/pagerduty_credential_methods_spec.rb +113 -0
  49. data/spec/lib/flapjack/gateways/jsonapi/report_methods_spec.rb +546 -0
  50. data/spec/lib/flapjack/gateways/jsonapi_spec.rb +10 -1
  51. data/spec/lib/flapjack/gateways/web_spec.rb +1 -0
  52. data/spec/support/jsonapi_helper.rb +62 -0
  53. metadata +36 -8
  54. data/lib/flapjack/gateways/jsonapi/entity_presenter.rb +0 -75
  55. data/spec/lib/flapjack/gateways/jsonapi/entity_presenter_spec.rb +0 -108
@@ -0,0 +1,546 @@
1
+ require 'spec_helper'
2
+ require 'flapjack/gateways/jsonapi'
3
+
4
+ describe 'Flapjack::Gateways::JSONAPI::ReportMethods', :sinatra => true, :logger => true do
5
+
6
+ include_context "jsonapi"
7
+
8
+ let(:entity) { double(Flapjack::Data::Entity) }
9
+ let(:entity_check) { double(Flapjack::Data::EntityCheck) }
10
+
11
+ let(:entity_id) { '457' }
12
+ let(:entity_name) { 'www.example.net'}
13
+ let(:entity_name_esc) { URI.escape(entity_name) }
14
+ let(:entity_check_name) { 'ping' }
15
+
16
+ let(:check_presenter) { double(Flapjack::Gateways::JSONAPI::CheckPresenter) }
17
+
18
+ it "returns the status for all entities" do
19
+ status = double('status')
20
+ expect(status).to receive(:as_json).and_return({:status => 'data'})
21
+ expect(check_presenter).to receive(:status).and_return(status)
22
+
23
+ expect(Flapjack::Gateways::JSONAPI::CheckPresenter).to receive(:new).
24
+ with(entity_check).and_return(check_presenter)
25
+
26
+ expect(Flapjack::Data::EntityCheck).to receive(:for_entity_name).
27
+ with(entity_name, 'SSH', :redis => redis).and_return(entity_check)
28
+
29
+ expect(entity).to receive(:check_list).and_return(['SSH'])
30
+
31
+ expect(Flapjack::Data::Entity).to receive(:all).
32
+ with(:redis => redis).and_return([entity])
33
+
34
+ expect(entity).to receive(:name).exactly(3).times.and_return(entity_name)
35
+ expect(entity).to receive(:id).and_return(entity_id)
36
+
37
+ expect(entity_check).to receive(:check).twice.and_return('SSH')
38
+
39
+ result = {
40
+ :status_reports => [{
41
+ 'id' => entity_id,
42
+ 'name' => entity_name,
43
+ 'links' => {
44
+ 'checks' => ["#{entity_name}:SSH"],
45
+ }
46
+ }],
47
+ :linked => {
48
+ :checks => [{
49
+ :id => "#{entity_name}:SSH",
50
+ :name => 'SSH',
51
+ :statuses => {'status' => 'data'}
52
+ }]
53
+ }
54
+ }
55
+
56
+ aget "/status_report/entities"
57
+ expect(last_response).to be_ok
58
+ expect(last_response.body).to eq(result.to_json)
59
+ end
60
+
61
+ it "returns the status for all checks on an entity" do
62
+ status = double('status')
63
+ expect(status).to receive(:as_json).and_return({:status => 'data'})
64
+ expect(check_presenter).to receive(:status).and_return(status)
65
+
66
+ expect(Flapjack::Gateways::JSONAPI::CheckPresenter).to receive(:new).
67
+ with(entity_check).and_return(check_presenter)
68
+
69
+ expect(Flapjack::Data::EntityCheck).to receive(:for_entity_name).
70
+ with(entity_name, 'SSH', :redis => redis).and_return(entity_check)
71
+
72
+ expect(entity).to receive(:check_list).and_return(['SSH'])
73
+
74
+ expect(Flapjack::Data::Entity).to receive(:find_by_id).
75
+ with(entity_id, :redis => redis).and_return(entity)
76
+
77
+ expect(entity).to receive(:name).exactly(3).times.and_return(entity_name)
78
+ expect(entity).to receive(:id).and_return(entity_id)
79
+
80
+ expect(entity_check).to receive(:check).twice.and_return('SSH')
81
+
82
+ result = {
83
+ :status_reports => [{
84
+ 'id' => entity_id,
85
+ 'name' => entity_name,
86
+ 'links' => {
87
+ 'checks' => ["#{entity_name}:SSH"],
88
+ }
89
+ }],
90
+ :linked => {
91
+ :checks => [{
92
+ :id => "#{entity_name}:SSH",
93
+ :name => 'SSH',
94
+ :statuses => {'status' => 'data'}
95
+ }]
96
+ }
97
+ }
98
+
99
+ aget "/status_report/entities/#{entity_id}"
100
+ expect(last_response).to be_ok
101
+ expect(last_response.body).to eq(result.to_json)
102
+ end
103
+
104
+ it "should not show the status for an entity that's not found" do
105
+ expect(Flapjack::Data::Entity).to receive(:find_by_id).
106
+ with(entity_id, :redis => redis).and_return(nil)
107
+
108
+ aget "/status_report/entities/#{entity_id}"
109
+ expect(last_response.status).to eq(404)
110
+ end
111
+
112
+ it "returns the status for an entity check" do
113
+ status = double('status')
114
+ expect(status).to receive(:as_json).and_return({:status => 'data'})
115
+ expect(check_presenter).to receive(:status).and_return(status)
116
+
117
+ expect(Flapjack::Gateways::JSONAPI::CheckPresenter).to receive(:new).
118
+ with(entity_check).and_return(check_presenter)
119
+
120
+ expect(Flapjack::Data::EntityCheck).to receive(:for_entity_name).
121
+ with(entity_name, 'SSH', :redis => redis).and_return(entity_check)
122
+
123
+ expect(entity).to receive(:id).and_return(entity_id)
124
+
125
+ expect(entity).to receive(:name).and_return(entity_name)
126
+ expect(entity_check).to receive(:entity).and_return(entity)
127
+ expect(entity_check).to receive(:check).twice.and_return('SSH')
128
+
129
+ result = {
130
+ :status_reports => [{
131
+ 'id' => entity_id,
132
+ 'name' => entity_name,
133
+ 'links' => {
134
+ 'checks' => ["#{entity_name}:SSH"],
135
+ }
136
+ }],
137
+ :linked => {
138
+ :checks => [{
139
+ :id => "#{entity_name}:SSH",
140
+ :name => 'SSH',
141
+ :statuses => {'status' => 'data'}
142
+ }]
143
+ }
144
+ }
145
+
146
+ aget "/status_report/checks/#{entity_name}:SSH"
147
+ expect(last_response).to be_ok
148
+ expect(last_response.body).to eq(result.to_json)
149
+ end
150
+
151
+ it "should not show the status for a check on an entity that's not found" do
152
+ expect(Flapjack::Data::EntityCheck).to receive(:for_entity_name).
153
+ with(entity_name, 'SSH', :redis => redis).and_return(nil)
154
+
155
+ aget "/status_report/checks/#{entity_name}:SSH"
156
+ expect(last_response).to be_not_found
157
+ end
158
+
159
+ it "should not show the status for a check that's not found on an entity" do
160
+ expect(Flapjack::Data::EntityCheck).to receive(:for_entity_name).
161
+ with(entity_name, 'SSH', :redis => redis).and_return(nil)
162
+
163
+ aget "/status_report/checks/#{entity_name}:SSH"
164
+ expect(last_response).to be_not_found
165
+ end
166
+
167
+ it "returns a list of scheduled maintenance periods for an entity" do
168
+ sched_maint = double('scheduled_maintenances')
169
+ expect(sched_maint).to receive(:as_json).and_return({:scheduled_maintenances => 'data'})
170
+ expect(check_presenter).to receive(:scheduled_maintenance).and_return(sched_maint)
171
+
172
+ expect(Flapjack::Gateways::JSONAPI::CheckPresenter).to receive(:new).
173
+ with(entity_check).and_return(check_presenter)
174
+
175
+ expect(Flapjack::Data::EntityCheck).to receive(:for_entity_name).
176
+ with(entity_name, 'SSH', :redis => redis).and_return(entity_check)
177
+
178
+ expect(entity).to receive(:check_list).and_return(['SSH'])
179
+
180
+ expect(Flapjack::Data::Entity).to receive(:find_by_id).
181
+ with(entity_id, :redis => redis).and_return(entity)
182
+
183
+ expect(entity).to receive(:name).exactly(3).times.and_return(entity_name)
184
+ expect(entity).to receive(:id).and_return(entity_id)
185
+
186
+ expect(entity_check).to receive(:check).twice.and_return('SSH')
187
+
188
+ result = {
189
+ :scheduled_maintenance_reports => [{
190
+ 'id' => entity_id,
191
+ 'name' => entity_name,
192
+ 'links' => {
193
+ 'checks' => ["#{entity_name}:SSH"],
194
+ }
195
+ }],
196
+ :linked => {
197
+ :checks => [{
198
+ :id => "#{entity_name}:SSH",
199
+ :name => 'SSH',
200
+ :scheduled_maintenances => {'scheduled_maintenances' => 'data'}
201
+ }]
202
+ }
203
+ }
204
+
205
+ aget "/scheduled_maintenance_report/entities/#{entity_id}"
206
+ expect(last_response).to be_ok
207
+ expect(last_response.body).to eq(result.to_json)
208
+ end
209
+
210
+ it "returns a list of scheduled maintenance periods within a time window for an entity" do
211
+ start = Time.parse('1 Jan 2012')
212
+ finish = Time.parse('6 Jan 2012')
213
+
214
+ sched_maint = double('scheduled_maintenances')
215
+ expect(sched_maint).to receive(:as_json).and_return({:scheduled_maintenances => 'data'})
216
+ expect(check_presenter).to receive(:scheduled_maintenance).
217
+ with(start.to_i, finish.to_i).and_return(sched_maint)
218
+
219
+ expect(Flapjack::Gateways::JSONAPI::CheckPresenter).to receive(:new).
220
+ with(entity_check).and_return(check_presenter)
221
+
222
+ expect(Flapjack::Data::EntityCheck).to receive(:for_entity_name).
223
+ with(entity_name, 'SSH', :redis => redis).and_return(entity_check)
224
+
225
+ expect(entity).to receive(:check_list).and_return(['SSH'])
226
+
227
+ expect(Flapjack::Data::Entity).to receive(:find_by_id).
228
+ with(entity_id, :redis => redis).and_return(entity)
229
+
230
+ expect(entity).to receive(:name).exactly(3).times.and_return(entity_name)
231
+ expect(entity).to receive(:id).and_return(entity_id)
232
+
233
+ expect(entity_check).to receive(:check).twice.and_return('SSH')
234
+
235
+ result = {
236
+ :scheduled_maintenance_reports => [{
237
+ 'id' => entity_id,
238
+ 'name' => entity_name,
239
+ 'links' => {
240
+ 'checks' => ["#{entity_name}:SSH"],
241
+ }
242
+ }],
243
+ :linked => {
244
+ :checks => [{
245
+ :id => "#{entity_name}:SSH",
246
+ :name => 'SSH',
247
+ :scheduled_maintenances => {'scheduled_maintenances' => 'data'}
248
+ }]
249
+ }
250
+ }
251
+
252
+ aget "/scheduled_maintenance_report/entities/#{entity_id}",
253
+ :start_time => start.iso8601, :end_time => finish.iso8601
254
+ expect(last_response).to be_ok
255
+ expect(last_response.body).to eq(result.to_json)
256
+ end
257
+
258
+ it "returns a list of scheduled maintenance periods for a check on an entity" do
259
+ sched_maint = double('scheduled_maintenances')
260
+ expect(sched_maint).to receive(:as_json).and_return({:scheduled_maintenances => 'data'})
261
+ expect(check_presenter).to receive(:scheduled_maintenance).and_return(sched_maint)
262
+
263
+ expect(Flapjack::Gateways::JSONAPI::CheckPresenter).to receive(:new).
264
+ with(entity_check).and_return(check_presenter)
265
+
266
+ expect(Flapjack::Data::EntityCheck).to receive(:for_entity_name).
267
+ with(entity_name, 'SSH', :redis => redis).and_return(entity_check)
268
+
269
+ expect(entity).to receive(:id).and_return(entity_id)
270
+
271
+ expect(entity).to receive(:name).and_return(entity_name)
272
+ expect(entity_check).to receive(:entity).and_return(entity)
273
+ expect(entity_check).to receive(:check).twice.and_return('SSH')
274
+
275
+ result = {
276
+ :scheduled_maintenance_reports => [{
277
+ 'id' => entity_id,
278
+ 'name' => entity_name,
279
+ 'links' => {
280
+ 'checks' => ["#{entity_name}:SSH"],
281
+ }
282
+ }],
283
+ :linked => {
284
+ :checks => [{
285
+ :id => "#{entity_name}:SSH",
286
+ :name => 'SSH',
287
+ :scheduled_maintenances => {'scheduled_maintenances' => 'data'}
288
+ }]
289
+ }
290
+ }
291
+
292
+ aget "/scheduled_maintenance_report/checks/#{entity_name}:SSH"
293
+ expect(last_response).to be_ok
294
+ expect(last_response.body).to eq(result.to_json)
295
+ end
296
+
297
+ it "returns a list of unscheduled maintenance periods for an entity" do
298
+ unsched_maint = double('unscheduled_maintenances')
299
+ expect(unsched_maint).to receive(:as_json).and_return({:unscheduled_maintenances => 'data'})
300
+ expect(check_presenter).to receive(:unscheduled_maintenance).and_return(unsched_maint)
301
+
302
+ expect(Flapjack::Gateways::JSONAPI::CheckPresenter).to receive(:new).
303
+ with(entity_check).and_return(check_presenter)
304
+
305
+ expect(Flapjack::Data::EntityCheck).to receive(:for_entity_name).
306
+ with(entity_name, 'SSH', :redis => redis).and_return(entity_check)
307
+
308
+ expect(entity).to receive(:check_list).and_return(['SSH'])
309
+
310
+ expect(Flapjack::Data::Entity).to receive(:find_by_id).
311
+ with(entity_id, :redis => redis).and_return(entity)
312
+
313
+ expect(entity).to receive(:name).exactly(3).times.and_return(entity_name)
314
+ expect(entity).to receive(:id).and_return(entity_id)
315
+
316
+ expect(entity_check).to receive(:check).twice.and_return('SSH')
317
+
318
+ result = {
319
+ :unscheduled_maintenance_reports => [{
320
+ 'id' => entity_id,
321
+ 'name' => entity_name,
322
+ 'links' => {
323
+ 'checks' => ["#{entity_name}:SSH"],
324
+ }
325
+ }],
326
+ :linked => {
327
+ :checks => [{
328
+ :id => "#{entity_name}:SSH",
329
+ :name => 'SSH',
330
+ :unscheduled_maintenances => {'unscheduled_maintenances' => 'data'}
331
+ }]
332
+ }
333
+ }
334
+
335
+ aget "/unscheduled_maintenance_report/entities/#{entity_id}"
336
+ expect(last_response).to be_ok
337
+ expect(last_response.body).to eq(result.to_json)
338
+ end
339
+
340
+ it "returns a list of unscheduled maintenance periods for a check on an entity" do
341
+ unsched_maint = double('unscheduled_maintenances')
342
+ expect(unsched_maint).to receive(:as_json).and_return({:unscheduled_maintenances => 'data'})
343
+ expect(check_presenter).to receive(:unscheduled_maintenance).and_return(unsched_maint)
344
+
345
+ expect(Flapjack::Gateways::JSONAPI::CheckPresenter).to receive(:new).
346
+ with(entity_check).and_return(check_presenter)
347
+
348
+ expect(Flapjack::Data::EntityCheck).to receive(:for_entity_name).
349
+ with(entity_name, 'SSH', :redis => redis).and_return(entity_check)
350
+
351
+
352
+ expect(entity).to receive(:id).and_return(entity_id)
353
+
354
+ expect(entity).to receive(:name).and_return(entity_name)
355
+ expect(entity_check).to receive(:entity).and_return(entity)
356
+ expect(entity_check).to receive(:check).twice.and_return('SSH')
357
+
358
+ result = {
359
+ :unscheduled_maintenance_reports => [{
360
+ 'id' => entity_id,
361
+ 'name' => entity_name,
362
+ 'links' => {
363
+ 'checks' => ["#{entity_name}:SSH"],
364
+ }
365
+ }],
366
+ :linked => {
367
+ :checks => [{
368
+ :id => "#{entity_name}:SSH",
369
+ :name => 'SSH',
370
+ :unscheduled_maintenances => {'unscheduled_maintenances' => 'data'}
371
+ }]
372
+ }
373
+ }
374
+
375
+ aget "/unscheduled_maintenance_report/checks/#{entity_name}:SSH"
376
+ expect(last_response).to be_ok
377
+ expect(last_response.body).to eq(result.to_json)
378
+ end
379
+
380
+ it "returns a list of unscheduled maintenance periods within a time window for a check an entity" do
381
+ start = Time.parse('1 Jan 2012')
382
+ finish = Time.parse('6 Jan 2012')
383
+
384
+ unsched_maint = double('scheduled_maintenances')
385
+ expect(unsched_maint).to receive(:as_json).and_return({:unscheduled_maintenances => 'data'})
386
+ expect(check_presenter).to receive(:unscheduled_maintenance).
387
+ with(start.to_i, finish.to_i).and_return(unsched_maint)
388
+
389
+ expect(Flapjack::Gateways::JSONAPI::CheckPresenter).to receive(:new).
390
+ with(entity_check).and_return(check_presenter)
391
+
392
+ expect(Flapjack::Data::EntityCheck).to receive(:for_entity_name).
393
+ with(entity_name, 'SSH', :redis => redis).and_return(entity_check)
394
+
395
+ expect(entity).to receive(:id).and_return(entity_id)
396
+
397
+ expect(entity).to receive(:name).and_return(entity_name)
398
+ expect(entity_check).to receive(:entity).and_return(entity)
399
+ expect(entity_check).to receive(:check).twice.and_return('SSH')
400
+
401
+ result = {
402
+ :unscheduled_maintenance_reports => [{
403
+ 'id' => entity_id,
404
+ 'name' => entity_name,
405
+ 'links' => {
406
+ 'checks' => ["#{entity_name}:SSH"],
407
+ }
408
+ }],
409
+ :linked => {
410
+ :checks => [{
411
+ :id => "#{entity_name}:SSH",
412
+ :name => 'SSH',
413
+ :unscheduled_maintenances => {'unscheduled_maintenances' => 'data'}
414
+ }]
415
+ }
416
+ }
417
+
418
+ aget "/unscheduled_maintenance_report/checks/#{entity_name}:SSH",
419
+ :start_time => start.iso8601, :end_time => finish.iso8601
420
+ expect(last_response).to be_ok
421
+ expect(last_response.body).to eq(result.to_json)
422
+ end
423
+
424
+ it "returns a list of outages for a check on an entity" do
425
+ outages = double('outages')
426
+ expect(outages).to receive(:as_json).and_return({:outages => 'data'})
427
+ expect(check_presenter).to receive(:outage).and_return(outages)
428
+
429
+ expect(Flapjack::Gateways::JSONAPI::CheckPresenter).to receive(:new).
430
+ with(entity_check).and_return(check_presenter)
431
+
432
+ expect(Flapjack::Data::EntityCheck).to receive(:for_entity_name).
433
+ with(entity_name, 'SSH', :redis => redis).and_return(entity_check)
434
+
435
+ expect(entity).to receive(:id).and_return(entity_id)
436
+
437
+ expect(entity).to receive(:name).and_return(entity_name)
438
+ expect(entity_check).to receive(:entity).and_return(entity)
439
+ expect(entity_check).to receive(:check).twice.and_return('SSH')
440
+
441
+ result = {
442
+ :outage_reports => [{
443
+ 'id' => entity_id,
444
+ 'name' => entity_name,
445
+ 'links' => {
446
+ 'checks' => ["#{entity_name}:SSH"],
447
+ }
448
+ }],
449
+ :linked => {
450
+ :checks => [{
451
+ :id => "#{entity_name}:SSH",
452
+ :name => 'SSH',
453
+ :outages => {'outages' => 'data'}
454
+ }]
455
+ }
456
+ }
457
+
458
+ aget "/outage_report/checks/#{entity_name}:SSH"
459
+ expect(last_response).to be_ok
460
+ expect(last_response.body).to eq(result.to_json)
461
+ end
462
+
463
+ it "returns a list of downtimes for an entity" do
464
+ downtime = double('downtime')
465
+ expect(downtime).to receive(:as_json).and_return({:downtime => 'data'})
466
+ expect(check_presenter).to receive(:downtime).and_return(downtime)
467
+
468
+ expect(Flapjack::Gateways::JSONAPI::CheckPresenter).to receive(:new).
469
+ with(entity_check).and_return(check_presenter)
470
+
471
+ expect(Flapjack::Data::EntityCheck).to receive(:for_entity_name).
472
+ with(entity_name, 'SSH', :redis => redis).and_return(entity_check)
473
+
474
+ expect(entity).to receive(:check_list).and_return(['SSH'])
475
+
476
+ expect(Flapjack::Data::Entity).to receive(:find_by_id).
477
+ with(entity_id, :redis => redis).and_return(entity)
478
+
479
+ expect(entity).to receive(:name).exactly(3).times.and_return(entity_name)
480
+
481
+ expect(entity).to receive(:id).and_return(entity_id)
482
+
483
+ expect(entity_check).to receive(:check).twice.and_return('SSH')
484
+
485
+ result = {
486
+ :downtime_reports => [{
487
+ 'id' => entity_id,
488
+ 'name' => entity_name,
489
+ 'links' => {
490
+ 'checks' => ["#{entity_name}:SSH"],
491
+ }
492
+ }],
493
+ :linked => {
494
+ :checks => [{
495
+ :id => "#{entity_name}:SSH",
496
+ :name => 'SSH',
497
+ :downtimes => {'downtime' => 'data'}
498
+ }]
499
+ }
500
+ }
501
+
502
+ aget "/downtime_report/entities/#{entity_id}"
503
+ expect(last_response).to be_ok
504
+ expect(last_response.body).to eq(result.to_json)
505
+ end
506
+
507
+ it "returns a list of downtimes for a check on an entity" do
508
+ downtime = double('downtime')
509
+ expect(downtime).to receive(:as_json).and_return({:downtime => 'data'})
510
+ expect(check_presenter).to receive(:downtime).and_return(downtime)
511
+
512
+ expect(Flapjack::Gateways::JSONAPI::CheckPresenter).to receive(:new).
513
+ with(entity_check).and_return(check_presenter)
514
+
515
+ expect(Flapjack::Data::EntityCheck).to receive(:for_entity_name).
516
+ with(entity_name, 'SSH', :redis => redis).and_return(entity_check)
517
+
518
+ expect(entity).to receive(:id).and_return(entity_id)
519
+
520
+ expect(entity).to receive(:name).and_return(entity_name)
521
+ expect(entity_check).to receive(:entity).and_return(entity)
522
+ expect(entity_check).to receive(:check).twice.and_return('SSH')
523
+
524
+ result = {
525
+ :downtime_reports => [{
526
+ 'id' => entity_id,
527
+ 'name' => entity_name,
528
+ 'links' => {
529
+ 'checks' => ["#{entity_name}:SSH"],
530
+ }
531
+ }],
532
+ :linked => {
533
+ :checks => [{
534
+ :id => "#{entity_name}:SSH",
535
+ :name => 'SSH',
536
+ :downtimes => {'downtime' => 'data'}
537
+ }]
538
+ }
539
+ }
540
+
541
+ aget "/downtime_report/checks/#{entity_name}:SSH"
542
+ expect(last_response).to be_ok
543
+ expect(last_response.body).to eq(result.to_json)
544
+ end
545
+
546
+ end