flapjack 1.6.0 → 2.0.0b1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (301) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +4 -6
  3. data/.gitmodules +1 -1
  4. data/.rspec +1 -1
  5. data/.ruby-version +1 -1
  6. data/.travis.yml +12 -13
  7. data/CHANGELOG.md +2 -9
  8. data/CONTRIBUTING.md +7 -2
  9. data/Gemfile +4 -13
  10. data/LICENCE +1 -0
  11. data/README.md +8 -2
  12. data/Rakefile +2 -2
  13. data/bin/flapjack +3 -12
  14. data/build.sh +4 -2
  15. data/etc/flapjack_config.toml.example +273 -0
  16. data/features/ack_after_sched_maint.feature +18 -21
  17. data/features/cli.feature +11 -71
  18. data/features/cli_flapjack-feed-events.feature +14 -15
  19. data/features/cli_flapjack-nagios-receiver.feature +12 -41
  20. data/features/cli_flapper.feature +12 -41
  21. data/features/cli_purge.feature +5 -6
  22. data/features/cli_receive-events.feature +6 -7
  23. data/features/cli_simulate-failed-check.feature +5 -6
  24. data/features/events.feature +206 -181
  25. data/features/events_check_names.feature +4 -7
  26. data/features/notification_rules.feature +144 -223
  27. data/features/notifications.feature +65 -57
  28. data/features/rollup.feature +45 -47
  29. data/features/steps/cli_steps.rb +4 -5
  30. data/features/steps/events_steps.rb +163 -373
  31. data/features/steps/notifications_steps.rb +408 -264
  32. data/features/steps/packaging-lintian_steps.rb +0 -4
  33. data/features/steps/time_travel_steps.rb +0 -26
  34. data/features/support/daemons.rb +6 -31
  35. data/features/support/env.rb +65 -74
  36. data/flapjack.gemspec +22 -24
  37. data/lib/flapjack.rb +14 -7
  38. data/lib/flapjack/cli/flapper.rb +74 -173
  39. data/lib/flapjack/cli/maintenance.rb +278 -109
  40. data/lib/flapjack/cli/migrate.rb +950 -0
  41. data/lib/flapjack/cli/purge.rb +19 -22
  42. data/lib/flapjack/cli/receiver.rb +150 -326
  43. data/lib/flapjack/cli/server.rb +8 -235
  44. data/lib/flapjack/cli/simulate.rb +42 -57
  45. data/lib/flapjack/configuration.rb +51 -37
  46. data/lib/flapjack/coordinator.rb +138 -129
  47. data/lib/flapjack/data/acknowledgement.rb +177 -0
  48. data/lib/flapjack/data/alert.rb +97 -158
  49. data/lib/flapjack/data/check.rb +611 -0
  50. data/lib/flapjack/data/condition.rb +70 -0
  51. data/lib/flapjack/data/contact.rb +226 -456
  52. data/lib/flapjack/data/event.rb +96 -184
  53. data/lib/flapjack/data/extensions/associations.rb +59 -0
  54. data/lib/flapjack/data/extensions/short_name.rb +25 -0
  55. data/lib/flapjack/data/medium.rb +428 -0
  56. data/lib/flapjack/data/metrics.rb +194 -0
  57. data/lib/flapjack/data/notification.rb +22 -281
  58. data/lib/flapjack/data/rule.rb +473 -0
  59. data/lib/flapjack/data/scheduled_maintenance.rb +244 -0
  60. data/lib/flapjack/data/state.rb +221 -0
  61. data/lib/flapjack/data/statistic.rb +112 -0
  62. data/lib/flapjack/data/tag.rb +277 -0
  63. data/lib/flapjack/data/test_notification.rb +182 -0
  64. data/lib/flapjack/data/unscheduled_maintenance.rb +159 -0
  65. data/lib/flapjack/data/validators/id_validator.rb +20 -0
  66. data/lib/flapjack/exceptions.rb +6 -0
  67. data/lib/flapjack/filters/acknowledgement.rb +23 -16
  68. data/lib/flapjack/filters/base.rb +0 -5
  69. data/lib/flapjack/filters/delays.rb +53 -43
  70. data/lib/flapjack/filters/ok.rb +23 -14
  71. data/lib/flapjack/filters/scheduled_maintenance.rb +3 -3
  72. data/lib/flapjack/filters/unscheduled_maintenance.rb +12 -3
  73. data/lib/flapjack/gateways/aws_sns.rb +65 -49
  74. data/lib/flapjack/gateways/aws_sns/alert.text.erb +2 -2
  75. data/lib/flapjack/gateways/aws_sns/alert_subject.text.erb +2 -2
  76. data/lib/flapjack/gateways/aws_sns/rollup_subject.text.erb +1 -1
  77. data/lib/flapjack/gateways/email.rb +107 -90
  78. data/lib/flapjack/gateways/email/alert.html.erb +19 -18
  79. data/lib/flapjack/gateways/email/alert.text.erb +20 -14
  80. data/lib/flapjack/gateways/email/alert_subject.text.erb +2 -1
  81. data/lib/flapjack/gateways/email/rollup.html.erb +14 -13
  82. data/lib/flapjack/gateways/email/rollup.text.erb +13 -10
  83. data/lib/flapjack/gateways/jabber.rb +679 -671
  84. data/lib/flapjack/gateways/jabber/alert.text.erb +9 -6
  85. data/lib/flapjack/gateways/jsonapi.rb +164 -350
  86. data/lib/flapjack/gateways/jsonapi/data/join_descriptor.rb +44 -0
  87. data/lib/flapjack/gateways/jsonapi/data/method_descriptor.rb +21 -0
  88. data/lib/flapjack/gateways/jsonapi/helpers/headers.rb +63 -0
  89. data/lib/flapjack/gateways/jsonapi/helpers/miscellaneous.rb +136 -0
  90. data/lib/flapjack/gateways/jsonapi/helpers/resources.rb +227 -0
  91. data/lib/flapjack/gateways/jsonapi/helpers/serialiser.rb +313 -0
  92. data/lib/flapjack/gateways/jsonapi/helpers/swagger_docs.rb +322 -0
  93. data/lib/flapjack/gateways/jsonapi/methods/association_delete.rb +115 -0
  94. data/lib/flapjack/gateways/jsonapi/methods/association_get.rb +288 -0
  95. data/lib/flapjack/gateways/jsonapi/methods/association_patch.rb +178 -0
  96. data/lib/flapjack/gateways/jsonapi/methods/association_post.rb +116 -0
  97. data/lib/flapjack/gateways/jsonapi/methods/metrics.rb +71 -0
  98. data/lib/flapjack/gateways/jsonapi/methods/resource_delete.rb +119 -0
  99. data/lib/flapjack/gateways/jsonapi/methods/resource_get.rb +186 -0
  100. data/lib/flapjack/gateways/jsonapi/methods/resource_patch.rb +239 -0
  101. data/lib/flapjack/gateways/jsonapi/methods/resource_post.rb +197 -0
  102. data/lib/flapjack/gateways/jsonapi/middleware/array_param_fixer.rb +27 -0
  103. data/lib/flapjack/gateways/jsonapi/{rack → middleware}/json_params_parser.rb +7 -6
  104. data/lib/flapjack/gateways/jsonapi/middleware/request_timestamp.rb +18 -0
  105. data/lib/flapjack/gateways/oobetet.rb +222 -170
  106. data/lib/flapjack/gateways/pager_duty.rb +388 -0
  107. data/lib/flapjack/gateways/pager_duty/alert.text.erb +13 -0
  108. data/lib/flapjack/gateways/slack.rb +56 -48
  109. data/lib/flapjack/gateways/slack/alert.text.erb +1 -1
  110. data/lib/flapjack/gateways/slack/rollup.text.erb +1 -1
  111. data/lib/flapjack/gateways/sms_aspsms.rb +155 -0
  112. data/lib/flapjack/gateways/sms_aspsms/alert.text.erb +7 -0
  113. data/lib/flapjack/gateways/sms_aspsms/rollup.text.erb +2 -0
  114. data/lib/flapjack/gateways/sms_messagenet.rb +77 -57
  115. data/lib/flapjack/gateways/sms_messagenet/alert.text.erb +3 -2
  116. data/lib/flapjack/gateways/sms_nexmo.rb +53 -51
  117. data/lib/flapjack/gateways/sms_nexmo/alert.text.erb +2 -2
  118. data/lib/flapjack/gateways/sms_nexmo/rollup.text.erb +1 -1
  119. data/lib/flapjack/gateways/sms_twilio.rb +79 -62
  120. data/lib/flapjack/gateways/sms_twilio/alert.text.erb +3 -2
  121. data/lib/flapjack/gateways/web.rb +437 -345
  122. data/lib/flapjack/gateways/web/middleware/request_timestamp.rb +18 -0
  123. data/lib/flapjack/gateways/web/public/css/bootstrap.css +3793 -4340
  124. data/lib/flapjack/gateways/web/public/css/bootstrap.css.map +1 -0
  125. data/lib/flapjack/gateways/web/public/fonts/glyphicons-halflings-regular.eot +0 -0
  126. data/lib/flapjack/gateways/web/public/fonts/glyphicons-halflings-regular.svg +273 -214
  127. data/lib/flapjack/gateways/web/public/fonts/glyphicons-halflings-regular.ttf +0 -0
  128. data/lib/flapjack/gateways/web/public/fonts/glyphicons-halflings-regular.woff +0 -0
  129. data/lib/flapjack/gateways/web/public/fonts/glyphicons-halflings-regular.woff2 +0 -0
  130. data/lib/flapjack/gateways/web/public/js/bootstrap.js +1637 -1607
  131. data/lib/flapjack/gateways/web/public/js/self_stats.js +1 -2
  132. data/lib/flapjack/gateways/web/views/_pagination.html.erb +19 -0
  133. data/lib/flapjack/gateways/web/views/check.html.erb +159 -121
  134. data/lib/flapjack/gateways/web/views/checks.html.erb +82 -41
  135. data/lib/flapjack/gateways/web/views/contact.html.erb +59 -71
  136. data/lib/flapjack/gateways/web/views/contacts.html.erb +32 -8
  137. data/lib/flapjack/gateways/web/views/index.html.erb +2 -2
  138. data/lib/flapjack/gateways/web/views/{layout.erb → layout.html.erb} +7 -23
  139. data/lib/flapjack/gateways/web/views/self_stats.html.erb +32 -33
  140. data/lib/flapjack/gateways/web/views/tag.html.erb +32 -0
  141. data/lib/flapjack/gateways/web/views/tags.html.erb +51 -0
  142. data/lib/flapjack/logger.rb +34 -3
  143. data/lib/flapjack/notifier.rb +180 -112
  144. data/lib/flapjack/patches.rb +8 -63
  145. data/lib/flapjack/pikelet.rb +185 -143
  146. data/lib/flapjack/processor.rb +323 -191
  147. data/lib/flapjack/record_queue.rb +33 -0
  148. data/lib/flapjack/redis_proxy.rb +66 -0
  149. data/lib/flapjack/utility.rb +21 -15
  150. data/lib/flapjack/version.rb +2 -1
  151. data/libexec/httpbroker.go +218 -14
  152. data/libexec/oneoff.go +13 -10
  153. data/spec/lib/flapjack/configuration_spec.rb +286 -0
  154. data/spec/lib/flapjack/coordinator_spec.rb +103 -157
  155. data/spec/lib/flapjack/data/check_spec.rb +175 -0
  156. data/spec/lib/flapjack/data/contact_spec.rb +26 -349
  157. data/spec/lib/flapjack/data/event_spec.rb +76 -291
  158. data/spec/lib/flapjack/data/medium_spec.rb +19 -0
  159. data/spec/lib/flapjack/data/rule_spec.rb +43 -0
  160. data/spec/lib/flapjack/data/scheduled_maintenance_spec.rb +976 -0
  161. data/spec/lib/flapjack/data/unscheduled_maintenance_spec.rb +34 -0
  162. data/spec/lib/flapjack/gateways/aws_sns_spec.rb +111 -60
  163. data/spec/lib/flapjack/gateways/email_spec.rb +194 -161
  164. data/spec/lib/flapjack/gateways/jabber_spec.rb +961 -162
  165. data/spec/lib/flapjack/gateways/jsonapi/methods/check_links_spec.rb +155 -0
  166. data/spec/lib/flapjack/gateways/jsonapi/methods/checks_spec.rb +426 -0
  167. data/spec/lib/flapjack/gateways/jsonapi/methods/contact_links_spec.rb +217 -0
  168. data/spec/lib/flapjack/gateways/jsonapi/methods/contacts_spec.rb +425 -0
  169. data/spec/lib/flapjack/gateways/jsonapi/methods/events_spec.rb +271 -0
  170. data/spec/lib/flapjack/gateways/jsonapi/methods/media_spec.rb +257 -0
  171. data/spec/lib/flapjack/gateways/jsonapi/methods/medium_links_spec.rb +163 -0
  172. data/spec/lib/flapjack/gateways/jsonapi/methods/metrics_spec.rb +8 -0
  173. data/spec/lib/flapjack/gateways/jsonapi/methods/rule_links_spec.rb +212 -0
  174. data/spec/lib/flapjack/gateways/jsonapi/methods/rules_spec.rb +289 -0
  175. data/spec/lib/flapjack/gateways/jsonapi/methods/scheduled_maintenance_links_spec.rb +49 -0
  176. data/spec/lib/flapjack/gateways/jsonapi/methods/scheduled_maintenances_spec.rb +242 -0
  177. data/spec/lib/flapjack/gateways/jsonapi/methods/tag_links_spec.rb +274 -0
  178. data/spec/lib/flapjack/gateways/jsonapi/methods/tags_spec.rb +302 -0
  179. data/spec/lib/flapjack/gateways/jsonapi/methods/unscheduled_maintenance_links_spec.rb +49 -0
  180. data/spec/lib/flapjack/gateways/jsonapi/methods/unscheduled_maintenances_spec.rb +339 -0
  181. data/spec/lib/flapjack/gateways/jsonapi_spec.rb +1 -1
  182. data/spec/lib/flapjack/gateways/oobetet_spec.rb +151 -79
  183. data/spec/lib/flapjack/gateways/pager_duty_spec.rb +353 -0
  184. data/spec/lib/flapjack/gateways/slack_spec.rb +53 -53
  185. data/spec/lib/flapjack/gateways/sms_aspsms_spec.rb +106 -0
  186. data/spec/lib/flapjack/gateways/sms_messagenet_spec.rb +111 -54
  187. data/spec/lib/flapjack/gateways/sms_nexmo_spec.rb +50 -51
  188. data/spec/lib/flapjack/gateways/sms_twilio_spec.rb +108 -48
  189. data/spec/lib/flapjack/gateways/web_spec.rb +144 -216
  190. data/spec/lib/flapjack/notifier_spec.rb +132 -1
  191. data/spec/lib/flapjack/pikelet_spec.rb +111 -50
  192. data/spec/lib/flapjack/processor_spec.rb +210 -40
  193. data/spec/lib/flapjack/redis_proxy_spec.rb +45 -0
  194. data/spec/lib/flapjack/utility_spec.rb +11 -15
  195. data/spec/service_consumers/fixture_data.rb +547 -0
  196. data/spec/service_consumers/pact_helper.rb +21 -32
  197. data/spec/service_consumers/pacts/flapjack-diner_v2.0.json +4652 -0
  198. data/spec/service_consumers/provider_states_for_flapjack-diner.rb +279 -322
  199. data/spec/service_consumers/provider_support.rb +8 -0
  200. data/spec/spec_helper.rb +34 -44
  201. data/spec/support/erb_view_helper.rb +1 -1
  202. data/spec/support/factories.rb +58 -0
  203. data/spec/support/jsonapi_helper.rb +15 -26
  204. data/spec/support/mock_logger.rb +43 -0
  205. data/spec/support/xmpp_comparable.rb +24 -0
  206. data/src/flapjack/transport_test.go +30 -1
  207. data/tasks/dump_keys.rake +82 -0
  208. data/tasks/events.rake +7 -7
  209. data/tasks/support/flapjack_config_benchmark.toml +28 -0
  210. data/tasks/support/flapjack_config_benchmark.yaml +0 -2
  211. metadata +175 -222
  212. data/Guardfile +0 -14
  213. data/etc/flapjack_config.yaml.example +0 -477
  214. data/features/cli_flapjack-populator.feature +0 -90
  215. data/features/support/silent_system.rb +0 -4
  216. data/lib/flapjack/cli/import.rb +0 -108
  217. data/lib/flapjack/data/entity.rb +0 -652
  218. data/lib/flapjack/data/entity_check.rb +0 -1044
  219. data/lib/flapjack/data/message.rb +0 -56
  220. data/lib/flapjack/data/migration.rb +0 -234
  221. data/lib/flapjack/data/notification_rule.rb +0 -425
  222. data/lib/flapjack/data/semaphore.rb +0 -44
  223. data/lib/flapjack/data/tagged.rb +0 -48
  224. data/lib/flapjack/gateways/jsonapi/check_methods.rb +0 -206
  225. data/lib/flapjack/gateways/jsonapi/check_presenter.rb +0 -221
  226. data/lib/flapjack/gateways/jsonapi/contact_methods.rb +0 -186
  227. data/lib/flapjack/gateways/jsonapi/entity_methods.rb +0 -223
  228. data/lib/flapjack/gateways/jsonapi/medium_methods.rb +0 -185
  229. data/lib/flapjack/gateways/jsonapi/metrics_methods.rb +0 -132
  230. data/lib/flapjack/gateways/jsonapi/notification_rule_methods.rb +0 -141
  231. data/lib/flapjack/gateways/jsonapi/pagerduty_credential_methods.rb +0 -139
  232. data/lib/flapjack/gateways/jsonapi/report_methods.rb +0 -146
  233. data/lib/flapjack/gateways/pagerduty.rb +0 -318
  234. data/lib/flapjack/gateways/pagerduty/alert.text.erb +0 -10
  235. data/lib/flapjack/gateways/web/public/css/select2-bootstrap.css +0 -87
  236. data/lib/flapjack/gateways/web/public/css/select2.css +0 -615
  237. data/lib/flapjack/gateways/web/public/css/tablesort.css +0 -67
  238. data/lib/flapjack/gateways/web/public/img/select2-spinner.gif +0 -0
  239. data/lib/flapjack/gateways/web/public/img/select2.png +0 -0
  240. data/lib/flapjack/gateways/web/public/img/select2x2.png +0 -0
  241. data/lib/flapjack/gateways/web/public/js/backbone.js +0 -1581
  242. data/lib/flapjack/gateways/web/public/js/backbone.jsonapi.js +0 -322
  243. data/lib/flapjack/gateways/web/public/js/flapjack.js +0 -82
  244. data/lib/flapjack/gateways/web/public/js/jquery.tablesorter.js +0 -1640
  245. data/lib/flapjack/gateways/web/public/js/jquery.tablesorter.widgets.js +0 -1390
  246. data/lib/flapjack/gateways/web/public/js/modules/contact.js +0 -520
  247. data/lib/flapjack/gateways/web/public/js/modules/entity.js +0 -28
  248. data/lib/flapjack/gateways/web/public/js/modules/medium.js +0 -40
  249. data/lib/flapjack/gateways/web/public/js/select2.js +0 -3397
  250. data/lib/flapjack/gateways/web/public/js/tablesort.js +0 -44
  251. data/lib/flapjack/gateways/web/public/js/underscore.js +0 -1276
  252. data/lib/flapjack/gateways/web/views/edit_contacts.html.erb +0 -173
  253. data/lib/flapjack/gateways/web/views/entities.html.erb +0 -30
  254. data/lib/flapjack/gateways/web/views/entity.html.erb +0 -51
  255. data/lib/flapjack/rack_logger.rb +0 -47
  256. data/lib/flapjack/redis_pool.rb +0 -42
  257. data/spec/lib/flapjack/data/entity_check_spec.rb +0 -1418
  258. data/spec/lib/flapjack/data/entity_spec.rb +0 -872
  259. data/spec/lib/flapjack/data/message_spec.rb +0 -30
  260. data/spec/lib/flapjack/data/migration_spec.rb +0 -104
  261. data/spec/lib/flapjack/data/notification_rule_spec.rb +0 -232
  262. data/spec/lib/flapjack/data/notification_spec.rb +0 -53
  263. data/spec/lib/flapjack/data/semaphore_spec.rb +0 -24
  264. data/spec/lib/flapjack/filters/acknowledgement_spec.rb +0 -6
  265. data/spec/lib/flapjack/filters/delays_spec.rb +0 -6
  266. data/spec/lib/flapjack/filters/ok_spec.rb +0 -6
  267. data/spec/lib/flapjack/filters/scheduled_maintenance_spec.rb +0 -6
  268. data/spec/lib/flapjack/filters/unscheduled_maintenance_spec.rb +0 -6
  269. data/spec/lib/flapjack/gateways/jsonapi/check_methods_spec.rb +0 -315
  270. data/spec/lib/flapjack/gateways/jsonapi/check_presenter_spec.rb +0 -223
  271. data/spec/lib/flapjack/gateways/jsonapi/contact_methods_spec.rb +0 -131
  272. data/spec/lib/flapjack/gateways/jsonapi/entity_methods_spec.rb +0 -389
  273. data/spec/lib/flapjack/gateways/jsonapi/medium_methods_spec.rb +0 -231
  274. data/spec/lib/flapjack/gateways/jsonapi/notification_rule_methods_spec.rb +0 -169
  275. data/spec/lib/flapjack/gateways/jsonapi/pagerduty_credential_methods_spec.rb +0 -114
  276. data/spec/lib/flapjack/gateways/jsonapi/report_methods_spec.rb +0 -590
  277. data/spec/lib/flapjack/gateways/pagerduty_spec.rb +0 -249
  278. data/spec/lib/flapjack/gateways/web/views/check.html.erb_spec.rb +0 -21
  279. data/spec/lib/flapjack/gateways/web/views/contact.html.erb_spec.rb +0 -24
  280. data/spec/lib/flapjack/gateways/web/views/index.html.erb_spec.rb +0 -16
  281. data/spec/lib/flapjack/redis_pool_spec.rb +0 -29
  282. data/spec/service_consumers/pacts/flapjack-diner_v1.0.json +0 -4702
  283. data/tasks/entities.rake +0 -151
  284. data/tasks/profile.rake +0 -282
  285. data/tmp/acknowledge.rb +0 -13
  286. data/tmp/create_config_yaml.rb +0 -16
  287. data/tmp/create_event_ok.rb +0 -30
  288. data/tmp/create_event_unknown.rb +0 -30
  289. data/tmp/create_events_failure.rb +0 -34
  290. data/tmp/create_events_ok.rb +0 -32
  291. data/tmp/create_events_ok_fail_ack_ok.rb +0 -53
  292. data/tmp/create_events_ok_failure.rb +0 -41
  293. data/tmp/create_events_ok_failure_ack.rb +0 -53
  294. data/tmp/dummy_contacts.json +0 -43
  295. data/tmp/dummy_entities.json +0 -37
  296. data/tmp/generate_nagios_test_hosts.rb +0 -16
  297. data/tmp/notification_rules.rb +0 -73
  298. data/tmp/parse_config_yaml.rb +0 -7
  299. data/tmp/redis_find_spurious_unknown_states.rb +0 -52
  300. data/tmp/test_json_post.rb +0 -19
  301. data/tmp/test_notification_rules_api.rb +0 -171
@@ -3,233 +3,1032 @@ require 'flapjack/gateways/jabber'
3
3
 
4
4
  describe Flapjack::Gateways::Jabber, :logger => true do
5
5
 
6
- let(:config) { {'queue' => 'jabber_notifications',
7
- 'server' => 'example.com',
8
- 'port' => '5222',
9
- 'jabberid' => 'flapjack@example.com',
10
- 'password' => 'password',
11
- 'alias' => 'flapjack',
6
+ let(:config) { {'queue' => 'jabber_notifications',
7
+ 'server' => 'example.com',
8
+ 'port' => '5222',
9
+ 'jabberid' => 'flapjack@example.com',
10
+ 'password' => 'password',
11
+ 'alias' => 'flapjack',
12
12
  'identifiers' => ['@flapjack'],
13
- 'rooms' => ['flapjacktest@conference.example.com']
13
+ 'rooms' => ['flapjacktest@conference.example.com'],
14
+ 'chatbot_announce' => true
14
15
  }
15
16
  }
16
17
 
18
+ let(:check) { double(Flapjack::Data::Check, :id => SecureRandom.uuid) }
19
+
20
+ let(:redis) { double(::Redis) }
17
21
  let(:stanza) { double('stanza') }
18
22
 
19
- it "hooks up event handlers to the appropriate methods" do
20
- expect(Socket).to receive(:gethostname).and_return('thismachine')
23
+ let(:now) { Time.now }
24
+
25
+ let(:lock) { double(Monitor) }
26
+ let(:stop_cond) { double(MonitorMixin::ConditionVariable) }
21
27
 
22
- expect(Flapjack::RedisPool).to receive(:new)
23
- fj = Flapjack::Gateways::Jabber.new(:config => config, :logger => @logger)
28
+ before(:each) do
29
+ allow(Flapjack).to receive(:redis).and_return(redis)
30
+ end
24
31
 
25
- expect(EventMachine::Synchrony).to receive(:next_tick).exactly(4).times.and_yield
32
+ context 'notifications' do
26
33
 
27
- expect(fj).to receive(:register_handler).with(:ready).and_yield(stanza)
28
- expect(fj).to receive(:on_ready).with(stanza)
34
+ let(:queue) { double(Flapjack::RecordQueue) }
29
35
 
30
- body_matchers = [{:body => /^@flapjack[:\s]/}, {:body => /^flapjack[:\s]/}]
31
- expect(fj).to receive(:register_handler).with(:message, :groupchat?, body_matchers).and_yield(stanza)
32
- expect(fj).to receive(:on_groupchat).with(stanza)
36
+ let(:alert) { double(Flapjack::Data::Alert) }
33
37
 
34
- expect(fj).to receive(:register_handler).with(:message, :chat?, :body).and_yield(stanza)
35
- expect(fj).to receive(:on_chat).with(stanza)
38
+ it "starts and is stopped by an exception" do
39
+ expect(Flapjack::RecordQueue).to receive(:new).with('jabber_notifications',
40
+ Flapjack::Data::Alert).and_return(queue)
36
41
 
37
- expect(fj).to receive(:register_handler).with(:disconnected).and_yield(stanza)
38
- expect(fj).to receive(:on_disconnect).with(stanza).and_return(true)
42
+ expect(lock).to receive(:synchronize).and_yield
43
+ expect(queue).to receive(:foreach).and_yield(alert)
44
+ expect(queue).to receive(:wait).and_raise(Flapjack::PikeletStop)
39
45
 
40
- fj.setup
41
- end
46
+ expect(redis).to receive(:quit)
47
+
48
+ fjn = Flapjack::Gateways::Jabber::Notifier.new(:lock => lock,
49
+ :config => config)
50
+ expect(fjn).to receive(:handle_alert).with(alert)
51
+
52
+ expect { fjn.start }.to raise_error(Flapjack::PikeletStop)
53
+ end
54
+
55
+ it "handles notifications received via Redis" do
56
+ bot = double(Flapjack::Gateways::Jabber::Bot)
57
+ expect(bot).to receive(:respond_to?).with(:announce).and_return(true)
58
+ expect(bot).to receive(:announce).with('johns@example.com', /Problem: /)
59
+ expect(bot).to receive(:alias).and_return('flapjack')
42
60
 
43
- it "joins a chat room after connecting" do
44
- expect(Flapjack::RedisPool).to receive(:new)
61
+ expect(check).to receive(:name).twice.and_return('app-02:ping')
45
62
 
46
- fj = Flapjack::Gateways::Jabber.new(:config => config, :logger => @logger)
47
- expect(fj).to receive(:connected?).and_return(true)
63
+ expect(alert).to receive(:address).and_return('johns@example.com')
64
+ expect(alert).to receive(:check).twice.and_return(check)
65
+ expect(alert).to receive(:state).and_return('critical')
66
+ expect(alert).to receive(:state_title_case).and_return('Critical')
67
+ expect(alert).to receive(:summary).twice.and_return('')
68
+ # expect(alert).to receive(:event_count).and_return(33)
69
+ expect(alert).to receive(:type).twice.and_return('problem')
70
+ expect(alert).to receive(:type_sentence_case).and_return('Problem')
71
+ expect(alert).to receive(:rollup).and_return(nil)
72
+ expect(alert).to receive(:event_hash).and_return('abcd1234')
48
73
 
49
- expect(EventMachine::Synchrony).to receive(:next_tick).and_yield
50
- expect(fj).to receive(:write).with(an_instance_of(Blather::Stanza::Presence))
51
- expect(fj).to receive(:write).with(an_instance_of(Blather::Stanza::Message))
74
+ fjn = Flapjack::Gateways::Jabber::Notifier.new(:config => config)
75
+ fjn.instance_variable_set('@siblings', [bot])
76
+ fjn.send(:handle_alert, alert)
77
+ end
52
78
 
53
- fj.on_ready(stanza)
54
79
  end
55
80
 
56
- it "receives an acknowledgement message" do
57
- expect(stanza).to receive(:body).twice.and_return('flapjack: ACKID 1f8ac10f fixing now duration: 90m')
58
- from = double('from')
59
- expect(from).to receive(:stripped).and_return('sender')
60
- expect(stanza).to receive(:from).and_return(from)
81
+ context 'commands' do
61
82
 
62
- redis = double('redis')
63
- expect(redis).to receive(:hget).with('checks_by_hash', '1f8ac10f').
64
- and_return('main-example.com:ping')
83
+ let(:bot) { double(Flapjack::Gateways::Jabber::Bot) }
65
84
 
66
- entity_check = double(Flapjack::Data::EntityCheck)
67
- expect(entity_check).to receive(:in_unscheduled_maintenance?)
85
+ let(:tag) { double(Flapjack::Data::Tag, :id => SecureRandom.uuid) }
68
86
 
69
- expect(Flapjack::Data::Event).to receive(:create_acknowledgement).
70
- with('main-example.com', 'ping', :summary => 'fixing now',
71
- :acknowledgement_id => '1f8ac10f',
72
- :duration => (90 * 60), :redis => redis)
87
+ let(:checks) { double('checks', :ids => [check.id]) }
88
+ let(:tags) { double('tags', :all => [tag]) }
89
+ let(:states) { double('states', :last => state) }
90
+ let(:sorted_checks) { double('sorted_checks', :ids => [check.id])}
73
91
 
74
- expect(Flapjack::Data::EntityCheck).to receive(:for_event_id).
75
- with('main-example.com:ping', :redis => redis).
76
- and_return(entity_check)
92
+ # TODO use separate threads in the test instead?
93
+ it "starts and is stopped by a signal" do
94
+ expect(lock).to receive(:synchronize).and_yield
77
95
 
78
- expect(Flapjack::RedisPool).to receive(:new).and_return(redis)
79
- fj = Flapjack::Gateways::Jabber.new(:config => config, :logger => @logger)
96
+ fji = Flapjack::Gateways::Jabber::Interpreter.new(:lock => lock, :stop_condition => stop_cond,
97
+ :config => config)
98
+ msg = {:room => 'room1', :nick => 'jim', :time => now.to_i, :message => 'help'}
99
+ fji.instance_variable_get('@messages').push(msg)
100
+ expect(stop_cond).to receive(:wait_while) {
101
+ fji.instance_variable_set('@should_quit', true)
102
+ }
80
103
 
81
- expect(EventMachine::Synchrony).to receive(:next_tick).and_yield
82
- expect(fj).to receive(:connected?).and_return(true)
83
- expect(fj).to receive(:write).with(an_instance_of(Blather::Stanza::Message))
104
+ expect(fji).to receive(:interpret).with('room1', 'jim', now.to_i, 'help')
84
105
 
85
- fj.on_groupchat(stanza)
86
- end
106
+ expect(redis).to receive(:quit)
107
+ fji.start
108
+ end
87
109
 
88
- it "strips XML tags from the received message" do
89
- expect(stanza).to receive(:body).twice.
90
- and_return('flapjack: tell me about <span style="text-decoration: underline;">' +
91
- '<a href="http://example.org/">example.org</a></span>')
110
+ it "receives a message and and signals a condition variable" do
111
+ expect(lock).to receive(:synchronize).and_yield
92
112
 
93
- from = double('from')
94
- expect(from).to receive(:stripped).and_return('sender')
95
- expect(stanza).to receive(:from).and_return(from)
113
+ fji = Flapjack::Gateways::Jabber::Interpreter.new(:lock => lock, :stop_condition => stop_cond,
114
+ :config => config)
115
+ expect(fji.instance_variable_get('@messages')).to be_empty
116
+ expect(stop_cond).to receive(:signal)
96
117
 
97
- redis = double('redis')
98
- entity = double(Flapjack::Data::Entity)
99
- expect(entity).to receive(:check_list).and_return(['ping'])
118
+ fji.receive_message('room1', 'jim', now.to_i, 'help')
119
+ expect(fji.instance_variable_get('@messages').size).to eq(1)
120
+ end
100
121
 
101
- expect(Flapjack::Data::Entity).to receive(:find_by_name).with('example.org',
102
- :redis => redis).and_return(entity)
122
+ it "interprets a received help command (from a room)" do
123
+ expect(bot).to receive(:announce).with('room1', /^commands:/)
103
124
 
104
- entity_check = double(Flapjack::Data::EntityCheck)
105
- expect(entity_check).to receive(:current_maintenance).with(:scheduled => true).and_return(nil)
106
- expect(entity_check).to receive(:current_maintenance).with(:unscheduled => true).and_return(nil)
125
+ fji = Flapjack::Gateways::Jabber::Interpreter.new(:config => config)
126
+ fji.instance_variable_set('@bot', bot)
127
+ fji.interpret('room1', 'jim', now.to_i, 'help')
128
+ end
107
129
 
108
- expect(Flapjack::Data::EntityCheck).to receive(:for_entity).with(entity, 'ping',
109
- :redis => redis).and_return(entity_check)
130
+ it "interprets a received help command (from a user)" do
131
+ expect(bot).to receive(:say).with('jim', /^commands:/)
110
132
 
111
- expect(Flapjack::RedisPool).to receive(:new).and_return(redis)
112
- fj = Flapjack::Gateways::Jabber.new(:config => config, :logger => @logger)
133
+ fji = Flapjack::Gateways::Jabber::Interpreter.new(:config => config)
134
+ fji.instance_variable_set('@bot', bot)
135
+ fji.interpret(nil, 'jim', now.to_i, 'help')
136
+ end
113
137
 
114
- expect(EventMachine::Synchrony).to receive(:next_tick).and_yield
115
- expect(fj).to receive(:connected?).and_return(true)
116
- expect(fj).to receive(:write).with(an_instance_of(Blather::Stanza::Message))
138
+ it "interprets a received identify command " do
139
+ expect(bot).to receive(:announce).with('room1', /System CPU Time/)
140
+ expect(bot).to receive(:identifiers).and_return(['@flapjack'])
117
141
 
118
- fj.on_groupchat(stanza)
119
- end
142
+ fji = Flapjack::Gateways::Jabber::Interpreter.new(:config => config,
143
+ :boot_time => now)
144
+ fji.instance_variable_set('@bot', bot)
145
+ fji.interpret('room1', 'jim', now.to_i + 60, 'identify')
146
+ end
120
147
 
121
- it "handles a message with a newline in it" do
122
- expect(stanza).to receive(:body).twice.
123
- and_return("flapjack: tell me about \nexample.com")
148
+ it "interprets a received find command (with regex)" do
149
+ expect(bot).to receive(:announce).with('room1', "Checks matching /example/:\nexample.com:ping is OK")
124
150
 
125
- from = double('from')
126
- expect(from).to receive(:stripped).and_return('sender')
127
- expect(stanza).to receive(:from).and_return(from)
151
+ expect(check).to receive(:name).and_return('example.com:ping')
128
152
 
129
- redis = double('redis')
130
- entity = double(Flapjack::Data::Entity)
131
- expect(entity).to receive(:check_list).and_return(['ping'])
153
+ expect(Flapjack::Data::Check).to receive(:lock).
154
+ with(no_args).
155
+ and_yield
132
156
 
133
- expect(Flapjack::Data::Entity).to receive(:find_by_name).with('example.com',
134
- :redis => redis).and_return(entity)
157
+ expect(check).to receive(:condition).and_return('ok')
135
158
 
136
- entity_check = double(Flapjack::Data::EntityCheck)
137
- expect(entity_check).to receive(:current_maintenance).with(:scheduled => true).and_return(nil)
138
- expect(entity_check).to receive(:current_maintenance).with(:unscheduled => true).and_return(nil)
159
+ expect(Flapjack::Data::Check).to receive(:find_by_ids).with(check.id).and_return([check])
160
+ expect(checks).to receive(:count).and_return(5)
161
+ expect(checks).to receive(:sort).with(:name, :limit => 30).and_return(sorted_checks)
162
+ expect(Flapjack::Data::Check).to receive(:intersect).
163
+ with(:name => Regexp.new("example")).and_return(checks)
139
164
 
140
- expect(Flapjack::Data::EntityCheck).to receive(:for_entity).with(entity, 'ping',
141
- :redis => redis).and_return(entity_check)
165
+ fji = Flapjack::Gateways::Jabber::Interpreter.new(:config => config)
166
+ fji.instance_variable_set('@bot', bot)
167
+ fji.interpret('room1', 'jim', now.to_i, 'find checks matching /example/')
168
+ end
142
169
 
143
- expect(Flapjack::RedisPool).to receive(:new).and_return(redis)
144
- fj = Flapjack::Gateways::Jabber.new(:config => config, :logger => @logger)
170
+ it "interprets a received find command (with limited results)" do
171
+ expect(bot).to receive(:announce).with('room1', "Checks matching /example/:\nShowing first 2 results of 5:\nexample.com:ping is OK")
145
172
 
146
- expect(EventMachine::Synchrony).to receive(:next_tick).and_yield
147
- expect(fj).to receive(:connected?).and_return(true)
148
- expect(fj).to receive(:write).with(an_instance_of(Blather::Stanza::Message))
173
+ expect(check).to receive(:name).and_return('example.com:ping')
149
174
 
150
- fj.on_groupchat(stanza)
151
- end
175
+ expect(Flapjack::Data::Check).to receive(:lock).
176
+ with(no_args).
177
+ and_yield
152
178
 
153
- it "receives a message it doesn't understand" do
154
- expect(stanza).to receive(:body).twice.and_return('flapjack: hello!')
155
- from = double('from')
156
- expect(from).to receive(:stripped).and_return('sender')
157
- expect(stanza).to receive(:from).and_return(from)
179
+ expect(check).to receive(:condition).and_return('ok')
158
180
 
159
- expect(Flapjack::RedisPool).to receive(:new)
160
- fj = Flapjack::Gateways::Jabber.new(:config => config, :logger => @logger)
181
+ expect(Flapjack::Data::Check).to receive(:find_by_ids).with(check.id).and_return([check])
182
+ expect(checks).to receive(:count).and_return(5)
183
+ expect(checks).to receive(:sort).with(:name, :limit => 2).and_return(sorted_checks)
184
+ expect(Flapjack::Data::Check).to receive(:intersect).
185
+ with(:name => Regexp.new("example")).and_return(checks)
161
186
 
162
- expect(EventMachine::Synchrony).to receive(:next_tick).and_yield
163
- expect(fj).to receive(:connected?).and_return(true)
164
- expect(fj).to receive(:write).with(an_instance_of(Blather::Stanza::Message))
187
+ fji = Flapjack::Gateways::Jabber::Interpreter.new(:config => config)
188
+ fji.instance_variable_set('@bot', bot)
189
+ fji.interpret('room1', 'jim', now.to_i, 'find 2 checks matching /example/')
190
+ end
165
191
 
166
- fj.on_groupchat(stanza)
167
- end
192
+ it "interprets a received find command (with an invalid regex)" do
193
+ expect(bot).to receive(:announce).with('room1', 'Error parsing /(example/')
168
194
 
169
- it "reconnects when disconnected (if not quitting)" do
170
- expect(Flapjack::RedisPool).to receive(:new)
171
- fj = Flapjack::Gateways::Jabber.new(:config => config, :logger => @logger)
195
+ expect(Flapjack::Data::Check).to receive(:lock).
196
+ with(no_args).
197
+ and_yield
172
198
 
173
- attempts = 0
199
+ expect(Flapjack::Data::Check).not_to receive(:intersect)
174
200
 
175
- expect(EventMachine::Synchrony).to receive(:sleep).with(5).exactly(1).times
176
- expect(EventMachine::Synchrony).to receive(:sleep).with(2).exactly(3).times
177
- expect(fj).to receive(:connect).exactly(4).times {
178
- attempts +=1
179
- raise StandardError.new unless attempts > 3
180
- }
201
+ fji = Flapjack::Gateways::Jabber::Interpreter.new(:config => config)
202
+ fji.instance_variable_set('@bot', bot)
203
+ fji.interpret('room1', 'jim', now.to_i, 'find checks matching /(example/')
204
+ end
181
205
 
182
- ret = fj.on_disconnect(stanza)
183
- expect(ret).to be true
184
- end
206
+ it "interprets a received find command (with tag)" do
207
+ expect(bot).to receive(:announce).with('room1', "Checks with tag 'example.com':\nexample.com:ping is OK")
208
+
209
+ expect(check).to receive(:name).and_return('example.com:ping')
210
+
211
+ expect(Flapjack::Data::Check).to receive(:lock).
212
+ with(Flapjack::Data::Tag).
213
+ and_yield
214
+
215
+ expect(check).to receive(:condition).and_return('ok')
216
+ expect(checks).to receive(:sort).with(:name, :limit => 30).and_return(sorted_checks)
217
+
218
+ expect(Flapjack::Data::Check).to receive(:find_by_ids).with(check.id).and_return([check])
219
+
220
+ expect(checks).to receive(:count).and_return(5)
221
+ expect(tag).to receive(:checks).and_return(checks)
222
+ expect(Flapjack::Data::Tag).to receive(:intersect).
223
+ with(:name => 'example.com').and_return(tags)
224
+
225
+ fji = Flapjack::Gateways::Jabber::Interpreter.new(:config => config)
226
+ fji.instance_variable_set('@bot', bot)
227
+ fji.interpret('room1', 'jim', now.to_i, 'find checks with tag example.com')
228
+ end
229
+
230
+ it "interprets a received state command (with name)" do
231
+ expect(bot).to receive(:announce).with('room1', /example.com:ping - OK/)
232
+
233
+ expect(Flapjack::Data::Check).to receive(:intersect).
234
+ with(:id => [check.id]).and_return([check])
235
+
236
+ expect(Flapjack::Data::Check).to receive(:lock).
237
+ with(no_args).
238
+ and_yield
239
+
240
+ expect(check).to receive(:name).and_return('example.com:ping')
241
+ expect(check).to receive(:condition).and_return('ok')
242
+
243
+ expect(checks).to receive(:empty?).and_return(false)
244
+ expect(checks).to receive(:count).and_return(5)
245
+ expect(Flapjack::Data::Check).to receive(:intersect).
246
+ with(:name => 'example.com:ping').and_return(checks)
247
+
248
+ fji = Flapjack::Gateways::Jabber::Interpreter.new(:config => config)
249
+ fji.instance_variable_set('@bot', bot)
250
+ fji.interpret('room1', 'jim', now.to_i, 'state of example.com:ping')
251
+ end
252
+
253
+ it "interprets a received state command (with tag)" do
254
+ expect(bot).to receive(:announce).with('room1', /example.com:ping - OK/)
255
+
256
+ expect(Flapjack::Data::Check).to receive(:intersect).
257
+ with(:id => [check.id]).and_return([check])
258
+
259
+ expect(Flapjack::Data::Check).to receive(:lock).
260
+ with(Flapjack::Data::Tag).
261
+ and_yield
262
+
263
+ expect(check).to receive(:name).and_return('example.com:ping')
264
+ expect(check).to receive(:condition).and_return('ok')
265
+
266
+ expect(checks).to receive(:count).and_return(5)
267
+ expect(tag).to receive(:checks).and_return(checks)
268
+
269
+ expect(Flapjack::Data::Tag).to receive(:intersect).
270
+ with(:name => 'example.com').and_return(tags)
271
+
272
+ fji = Flapjack::Gateways::Jabber::Interpreter.new(:config => config)
273
+ fji.instance_variable_set('@bot', bot)
274
+ fji.interpret('room1', 'jim', now.to_i, 'state of checks with tag example.com')
275
+ end
276
+
277
+ it "interprets a received state command (with regex)" do
278
+ expect(bot).to receive(:announce).with('room1', /example.com:ping - OK/)
279
+
280
+ expect(Flapjack::Data::Check).to receive(:intersect).
281
+ with(:id => [check.id]).and_return([check])
282
+
283
+ expect(Flapjack::Data::Check).to receive(:lock).
284
+ with(no_args).
285
+ and_yield
286
+
287
+ expect(check).to receive(:name).and_return('example.com:ping')
288
+ expect(check).to receive(:condition).and_return('ok')
289
+
290
+ expect(checks).to receive(:count).and_return(5)
291
+
292
+ expect(Flapjack::Data::Check).to receive(:intersect).
293
+ with(:name => Regexp.new("^example.com:p")).and_return(checks)
294
+
295
+ fji = Flapjack::Gateways::Jabber::Interpreter.new(:config => config)
296
+ fji.instance_variable_set('@bot', bot)
297
+ fji.interpret('room1', 'jim', now.to_i, 'state of checks matching /^example.com:p/')
298
+ end
299
+
300
+ let(:start_range) { double(Zermelo::Filters::IndexRange) }
301
+ let(:end_range) { double(Zermelo::Filters::IndexRange) }
302
+
303
+ let(:scheduled_maintenances) { double('scheduled_maintenances') }
304
+ let(:unscheduled_maintenances) { double('unscheduled_maintenances') }
305
+
306
+ let(:no_scheduled_maintenances) { double('no_scheduled_maintenances', :all => [])}
307
+ let(:no_unscheduled_maintenances) { double('no_unscheduled_maintenances', :all => [])}
308
+
309
+ it "interprets a received information command (with name)" do
310
+ expect(bot).to receive(:announce).with('room1', /Not in scheduled or unscheduled maintenance./)
311
+
312
+ expect(Flapjack::Data::Check).to receive(:lock).
313
+ with(Flapjack::Data::ScheduledMaintenance,
314
+ Flapjack::Data::UnscheduledMaintenance).
315
+ and_yield
316
+
317
+ expect(Flapjack::Data::Check).to receive(:intersect).
318
+ with(:id => [check.id]).and_return([check])
319
+
320
+ expect(Zermelo::Filters::IndexRange).to receive(:new).
321
+ with(nil, an_instance_of(Time), :by_score => true).
322
+ and_return(start_range)
323
+
324
+ expect(Zermelo::Filters::IndexRange).to receive(:new).
325
+ with(an_instance_of(Time), nil, :by_score => true).
326
+ and_return(end_range)
327
+
328
+ expect(unscheduled_maintenances).to receive(:intersect).
329
+ with(:start_time => start_range, :end_time => end_range).
330
+ and_return(no_unscheduled_maintenances)
331
+ expect(scheduled_maintenances).to receive(:intersect).
332
+ with(:start_time => start_range, :end_time => end_range).
333
+ and_return(no_scheduled_maintenances)
334
+ expect(check).to receive(:scheduled_maintenances).and_return(scheduled_maintenances)
335
+ expect(check).to receive(:unscheduled_maintenances).and_return(unscheduled_maintenances)
336
+
337
+ expect(checks).to receive(:empty?).and_return(false)
338
+ expect(checks).to receive(:count).and_return(5)
339
+ expect(checks).to receive(:sort).with(:name, :limit => 30).and_return(sorted_checks)
340
+ expect(Flapjack::Data::Check).to receive(:intersect).
341
+ with(:name => 'example.com:ping').and_return(checks)
342
+
343
+ fji = Flapjack::Gateways::Jabber::Interpreter.new(:config => config)
344
+ fji.instance_variable_set('@bot', bot)
345
+ fji.interpret('room1', 'jim', now.to_i, 'tell me about example.com:ping')
346
+ end
347
+
348
+ it "handles a received information command (with name, with newline in the command)" do
349
+ expect(bot).to receive(:announce).with('room1', /Not in scheduled or unscheduled maintenance./)
350
+
351
+ expect(Flapjack::Data::Check).to receive(:lock).
352
+ with(Flapjack::Data::ScheduledMaintenance,
353
+ Flapjack::Data::UnscheduledMaintenance).
354
+ and_yield
355
+
356
+ expect(Flapjack::Data::Check).to receive(:intersect).
357
+ with(:id => [check.id]).and_return([check])
358
+
359
+ expect(Zermelo::Filters::IndexRange).to receive(:new).
360
+ with(nil, an_instance_of(Time), :by_score => true).
361
+ and_return(start_range)
362
+
363
+ expect(Zermelo::Filters::IndexRange).to receive(:new).
364
+ with(an_instance_of(Time), nil, :by_score => true).
365
+ and_return(end_range)
366
+
367
+ expect(unscheduled_maintenances).to receive(:intersect).
368
+ with(:start_time => start_range, :end_time => end_range).
369
+ and_return(no_unscheduled_maintenances)
370
+ expect(scheduled_maintenances).to receive(:intersect).
371
+ with(:start_time => start_range, :end_time => end_range).
372
+ and_return(no_scheduled_maintenances)
373
+ expect(check).to receive(:scheduled_maintenances).and_return(scheduled_maintenances)
374
+ expect(check).to receive(:unscheduled_maintenances).and_return(unscheduled_maintenances)
375
+
376
+ expect(checks).to receive(:empty?).and_return(false)
377
+ expect(checks).to receive(:count).and_return(5)
378
+ expect(checks).to receive(:sort).with(:name, :limit => 30).and_return(sorted_checks)
379
+ expect(Flapjack::Data::Check).to receive(:intersect).
380
+ with(:name => 'example.com:ping').and_return(checks)
381
+
382
+ fji = Flapjack::Gateways::Jabber::Interpreter.new(:config => config)
383
+ fji.instance_variable_set('@bot', bot)
384
+ fji.interpret('room1', 'jim', now.to_i, "tell me \nabout example.com:ping")
385
+ end
386
+
387
+ it "interprets a received information command (with tag)" do
388
+ expect(bot).to receive(:announce).with('room1', /Not in scheduled or unscheduled maintenance./)
389
+
390
+ expect(Flapjack::Data::Check).to receive(:lock).
391
+ with(Flapjack::Data::Tag,
392
+ Flapjack::Data::ScheduledMaintenance,
393
+ Flapjack::Data::UnscheduledMaintenance).
394
+ and_yield
395
+
396
+ expect(Flapjack::Data::Check).to receive(:intersect).
397
+ with(:id => [check.id]).and_return([check])
398
+
399
+ expect(Zermelo::Filters::IndexRange).to receive(:new).
400
+ with(nil, an_instance_of(Time), :by_score => true).
401
+ and_return(start_range)
402
+
403
+ expect(Zermelo::Filters::IndexRange).to receive(:new).
404
+ with(an_instance_of(Time), nil, :by_score => true).
405
+ and_return(end_range)
406
+
407
+ expect(unscheduled_maintenances).to receive(:intersect).
408
+ with(:start_time => start_range, :end_time => end_range).
409
+ and_return(no_unscheduled_maintenances)
410
+ expect(scheduled_maintenances).to receive(:intersect).
411
+ with(:start_time => start_range, :end_time => end_range).
412
+ and_return(no_scheduled_maintenances)
413
+ expect(check).to receive(:scheduled_maintenances).and_return(scheduled_maintenances)
414
+ expect(check).to receive(:unscheduled_maintenances).and_return(unscheduled_maintenances)
415
+
416
+ expect(checks).to receive(:count).and_return(5)
417
+ expect(checks).to receive(:sort).with(:name, :limit => 30).and_return(sorted_checks)
418
+ expect(tag).to receive(:checks).and_return(checks)
419
+
420
+ expect(Flapjack::Data::Tag).to receive(:intersect).
421
+ with(:name => 'example.com').and_return(tags)
422
+
423
+ fji = Flapjack::Gateways::Jabber::Interpreter.new(:config => config)
424
+ fji.instance_variable_set('@bot', bot)
425
+ fji.interpret('room1', 'jim', now.to_i, 'tell me about checks with tag example.com')
426
+ end
427
+
428
+ it "interprets a received information command (with regex)" do
429
+ expect(bot).to receive(:announce).with('room1', /Not in scheduled or unscheduled maintenance./)
430
+
431
+ expect(Flapjack::Data::Check).to receive(:lock).
432
+ with(Flapjack::Data::ScheduledMaintenance,
433
+ Flapjack::Data::UnscheduledMaintenance).
434
+ and_yield
435
+
436
+ expect(Flapjack::Data::Check).to receive(:intersect).
437
+ with(:id => [check.id]).and_return([check])
438
+
439
+ expect(Zermelo::Filters::IndexRange).to receive(:new).
440
+ with(nil, an_instance_of(Time), :by_score => true).
441
+ and_return(start_range)
442
+
443
+ expect(Zermelo::Filters::IndexRange).to receive(:new).
444
+ with(an_instance_of(Time), nil, :by_score => true).
445
+ and_return(end_range)
446
+
447
+ expect(unscheduled_maintenances).to receive(:intersect).
448
+ with(:start_time => start_range, :end_time => end_range).
449
+ and_return(no_unscheduled_maintenances)
450
+ expect(scheduled_maintenances).to receive(:intersect).
451
+ with(:start_time => start_range, :end_time => end_range).
452
+ and_return(no_scheduled_maintenances)
453
+ expect(check).to receive(:scheduled_maintenances).and_return(scheduled_maintenances)
454
+ expect(check).to receive(:unscheduled_maintenances).and_return(unscheduled_maintenances)
455
+
456
+ expect(checks).to receive(:count).and_return(5)
457
+ expect(checks).to receive(:sort).with(:name, :limit => 30).and_return(sorted_checks)
458
+ expect(Flapjack::Data::Check).to receive(:intersect).
459
+ with(:name => Regexp.new("^example.com:p")).and_return(checks)
460
+
461
+ fji = Flapjack::Gateways::Jabber::Interpreter.new(:config => config)
462
+ fji.instance_variable_set('@bot', bot)
463
+ fji.interpret('room1', 'jim', now.to_i, 'tell me about checks matching /^example.com:p/')
464
+ end
465
+
466
+ it "interprets a received information command (with limited results)" do
467
+ expect(bot).to receive(:announce).with('room1', /Showing first 2 results of 5:\nNot in scheduled or unscheduled maintenance./)
468
+
469
+ expect(Flapjack::Data::Check).to receive(:lock).
470
+ with(Flapjack::Data::ScheduledMaintenance,
471
+ Flapjack::Data::UnscheduledMaintenance).
472
+ and_yield
473
+
474
+ expect(Flapjack::Data::Check).to receive(:intersect).
475
+ with(:id => [check.id]).and_return([check])
476
+
477
+ expect(Zermelo::Filters::IndexRange).to receive(:new).
478
+ with(nil, an_instance_of(Time), :by_score => true).
479
+ and_return(start_range)
480
+
481
+ expect(Zermelo::Filters::IndexRange).to receive(:new).
482
+ with(an_instance_of(Time), nil, :by_score => true).
483
+ and_return(end_range)
484
+
485
+ expect(unscheduled_maintenances).to receive(:intersect).
486
+ with(:start_time => start_range, :end_time => end_range).
487
+ and_return(no_unscheduled_maintenances)
488
+ expect(scheduled_maintenances).to receive(:intersect).
489
+ with(:start_time => start_range, :end_time => end_range).
490
+ and_return(no_scheduled_maintenances)
491
+ expect(check).to receive(:scheduled_maintenances).and_return(scheduled_maintenances)
492
+ expect(check).to receive(:unscheduled_maintenances).and_return(unscheduled_maintenances)
493
+
494
+ expect(checks).to receive(:count).and_return(5)
495
+ expect(checks).to receive(:sort).with(:name, :limit => 2).and_return(sorted_checks)
496
+ expect(Flapjack::Data::Check).to receive(:intersect).
497
+ with(:name => Regexp.new("^example.com:p")).and_return(checks)
498
+
499
+ fji = Flapjack::Gateways::Jabber::Interpreter.new(:config => config)
500
+ fji.instance_variable_set('@bot', bot)
501
+ fji.interpret('room1', 'jim', now.to_i, 'tell me about 2 checks matching /^example.com:p/')
502
+ end
503
+
504
+ it "interprets a received ACKID command" do
505
+ expect(bot).to receive(:announce).with('room1', "ACKing example.com:ping (abcd1234)")
506
+
507
+ expect(check).to receive(:name).and_return('example.com:ping')
508
+ expect(check).to receive(:in_unscheduled_maintenance?).and_return(false)
509
+
510
+ expect(checks).to receive(:all).and_return([check])
511
+ expect(Flapjack::Data::Check).to receive(:intersect).
512
+ with(:ack_hash => 'abcd1234').and_return(checks)
513
+
514
+ expect(Flapjack::Data::Event).to receive(:create_acknowledgements).
515
+ with('events', [check],
516
+ :summary => 'JJ looking', :acknowledgement_id => 'abcd1234',
517
+ :duration => (60 * 60))
518
+
519
+ fji = Flapjack::Gateways::Jabber::Interpreter.new(:config => config)
520
+ fji.instance_variable_set('@bot', bot)
521
+ fji.interpret('room1', 'jim', now.to_i, 'ACKID abcd1234 JJ looking duration: 1 hour')
522
+ end
523
+
524
+ it "interprets a received acknowledgement command (with check name)" do
525
+ expect(bot).to receive(:announce).with('room1', "Ack list:\nexample.com:ping")
526
+
527
+ expect(Flapjack::Data::Check).to receive(:lock).
528
+ with(Flapjack::Data::UnscheduledMaintenance).
529
+ and_yield
530
+
531
+ expect(checks).to receive(:empty?).and_return(false)
532
+
533
+ expect(checks).to receive(:count).and_return(5)
534
+ expect(Flapjack::Data::Check).to receive(:intersect).
535
+ with(:name => 'example.com:ping').and_return(checks)
536
+
537
+ failing_checks = double('failing_checks')
538
+ expect(Flapjack::Data::Check).to receive(:intersect).
539
+ with(:id => [check.id], :failing => true).and_return(failing_checks)
540
+
541
+ expect(failing_checks).to receive(:empty?).and_return(false)
542
+
543
+ expect(check).to receive(:name).and_return('example.com:ping')
544
+ expect(failing_checks).to receive(:map) {|&arg| [arg.call(check)] }
545
+
546
+ expect(Flapjack::Data::Event).to receive(:create_acknowledgements).
547
+ with('events', failing_checks,
548
+ :summary => 'jim: Set via chatbot', :duration => (60 * 60))
549
+
550
+ fji = Flapjack::Gateways::Jabber::Interpreter.new(:config => config)
551
+ fji.instance_variable_set('@bot', bot)
552
+ fji.interpret('room1', 'jim', now.to_i, 'ack example.com:ping')
553
+ end
554
+
555
+ it "interprets a received acknowledgement command (with tag)" do
556
+ expect(bot).to receive(:announce).with('room1', "Ack list:\nexample.com:ping")
557
+
558
+ expect(Flapjack::Data::Check).to receive(:lock).
559
+ with(Flapjack::Data::Tag,
560
+ Flapjack::Data::UnscheduledMaintenance).
561
+ and_yield
562
+
563
+ expect(tag).to receive(:checks).and_return(checks)
564
+
565
+ expect(checks).to receive(:count).and_return(5)
566
+
567
+ failing_checks = double('failing_checks')
568
+ expect(Flapjack::Data::Check).to receive(:intersect).
569
+ with(:id => [check.id], :failing => true).and_return(failing_checks)
570
+
571
+ expect(failing_checks).to receive(:empty?).and_return(false)
572
+
573
+ expect(check).to receive(:name).and_return('example.com:ping')
574
+ expect(failing_checks).to receive(:map) {|&arg| [arg.call(check)] }
575
+
576
+ expect(Flapjack::Data::Tag).to receive(:intersect).
577
+ with(:name => 'example.com').and_return(tags)
185
578
 
186
- it "prompts the blocking redis connection to quit" do
187
- shutdown_redis = double('shutdown_redis')
188
- expect(shutdown_redis).to receive(:rpush).with('jabber_notifications', %q{{"notification_type":"shutdown"}})
189
- expect(EM::Hiredis).to receive(:connect).and_return(shutdown_redis)
579
+ expect(Flapjack::Data::Event).to receive(:create_acknowledgements).
580
+ with('events', failing_checks,
581
+ :summary => 'jim: Set via chatbot', :duration => (60 * 60))
190
582
 
191
- redis = double('redis')
192
- expect(Flapjack::RedisPool).to receive(:new).and_return(redis)
193
- fj = Flapjack::Gateways::Jabber.new(:config => config, :logger => @logger)
583
+ fji = Flapjack::Gateways::Jabber::Interpreter.new(:config => config)
584
+ fji.instance_variable_set('@bot', bot)
585
+ fji.interpret('room1', 'jim', now.to_i, 'ack checks with tag example.com')
586
+ end
587
+
588
+ it "interprets a received acknowledgement command (with regex)" do
589
+ expect(bot).to receive(:announce).with('room1', "Ack list:\nexample.com:ping")
590
+
591
+ expect(Flapjack::Data::Check).to receive(:lock).
592
+ with(Flapjack::Data::UnscheduledMaintenance).
593
+ and_yield
594
+
595
+ failing_checks = double('failing_checks')
596
+ expect(Flapjack::Data::Check).to receive(:intersect).
597
+ with(:id => [check.id], :failing => true).and_return(failing_checks)
598
+
599
+ expect(failing_checks).to receive(:empty?).and_return(false)
600
+
601
+ expect(failing_checks).to receive(:map) {|&arg| [arg.call(check)] }
602
+ expect(check).to receive(:name).and_return('example.com:ping')
603
+
604
+ expect(checks).to receive(:count).and_return(5)
605
+
606
+ expect(Flapjack::Data::Check).to receive(:intersect).
607
+ with(:name => Regexp.new("^example.com:p")).and_return(checks)
608
+
609
+ expect(Flapjack::Data::Event).to receive(:create_acknowledgements).
610
+ with('events', failing_checks,
611
+ :summary => 'jim: Set via chatbot', :duration => (60 * 60))
612
+
613
+ fji = Flapjack::Gateways::Jabber::Interpreter.new(:config => config)
614
+ fji.instance_variable_set('@bot', bot)
615
+ fji.interpret('room1', 'jim', now.to_i, 'ack checks matching /^example.com:p/')
616
+ end
617
+
618
+ it "interprets a received maintenance command (with name)" do
619
+ now = Time.now
620
+ t = now.to_i + 60
621
+ expect(Time).to receive(:now).and_return(now)
622
+
623
+ expect(bot).to receive(:announce).with('room1',
624
+ "Scheduled maintenance for 180 minutes starting at #{Time.at(t)} on:\nexample.com:ping")
625
+
626
+ expect(Flapjack::Data::Check).to receive(:lock).
627
+ with(Flapjack::Data::ScheduledMaintenance).
628
+ and_yield
629
+
630
+ expect(Flapjack::Data::Check).to receive(:find_by_ids).with(check.id).and_return([check])
631
+
632
+ expect(checks).to receive(:empty?).and_return(false)
633
+ expect(checks).to receive(:count).and_return(5)
634
+ expect(Flapjack::Data::Check).to receive(:intersect).
635
+ with(:name => 'example.com:ping').and_return(checks)
636
+
637
+ sched_maint = double(Flapjack::Data::ScheduledMaintenance)
638
+ expect(Flapjack::Data::ScheduledMaintenance).to receive(:new).with(
639
+ :start_time => t,
640
+ :end_time => t + (3 * 60 * 60),
641
+ :summary => 'test'
642
+ ).and_return(sched_maint)
643
+ expect(sched_maint).to receive(:save).and_return(true)
644
+
645
+ sched_maints = double('sched_maints')
646
+ expect(sched_maints).to receive(:<<).with(sched_maint)
647
+ expect(check).to receive(:scheduled_maintenances).and_return(sched_maints)
648
+
649
+ expect(check).to receive(:name).and_return('example.com:ping')
650
+
651
+ fji = Flapjack::Gateways::Jabber::Interpreter.new(:config => config)
652
+ fji.instance_variable_set('@bot', bot)
653
+ fji.interpret('room1', 'jim', now.to_i, 'maint example.com:ping start-in: 1 minute duration: 3 hour comment: test')
654
+ end
655
+
656
+ it "interprets a received maintenance command (with tag)" do
657
+ now = Time.now
658
+ t = now.to_i + 60
659
+ expect(Time).to receive(:now).and_return(now)
660
+
661
+ expect(bot).to receive(:announce).with('room1',
662
+ "Scheduled maintenance for 180 minutes starting at #{Time.at(t)} on:\nexample.com:ping")
663
+
664
+ expect(Flapjack::Data::Check).to receive(:lock).
665
+ with(Flapjack::Data::Tag, Flapjack::Data::ScheduledMaintenance).
666
+ and_yield
667
+
668
+ expect(Flapjack::Data::Check).to receive(:find_by_ids).with(check.id).and_return([check])
669
+
670
+ expect(checks).to receive(:count).and_return(5)
671
+ expect(tag).to receive(:checks).and_return(checks)
672
+
673
+ expect(Flapjack::Data::Tag).to receive(:intersect).
674
+ with(:name => 'example.com').and_return(tags)
675
+
676
+ sched_maint = double(Flapjack::Data::ScheduledMaintenance)
677
+ expect(Flapjack::Data::ScheduledMaintenance).to receive(:new).with(
678
+ :start_time => t,
679
+ :end_time => t + (3 * 60 * 60),
680
+ :summary => 'test'
681
+ ).and_return(sched_maint)
682
+ expect(sched_maint).to receive(:save).and_return(true)
683
+
684
+ sched_maints = double('sched_maints')
685
+ expect(sched_maints).to receive(:<<).with(sched_maint)
686
+ expect(check).to receive(:scheduled_maintenances).and_return(sched_maints)
687
+
688
+ expect(check).to receive(:name).and_return('example.com:ping')
689
+
690
+ fji = Flapjack::Gateways::Jabber::Interpreter.new(:config => config)
691
+ fji.instance_variable_set('@bot', bot)
692
+ fji.interpret('room1', 'jim', now.to_i, 'maint checks with tag example.com start-in: 1 minute duration: 3 hour comment: test')
693
+ end
694
+
695
+ it "interprets a received maintenance command (with regex)" do
696
+ now = Time.now
697
+ t = now.to_i + 60
698
+ expect(Time).to receive(:now).and_return(now)
699
+
700
+ expect(bot).to receive(:announce).with('room1',
701
+ "Scheduled maintenance for 180 minutes starting at #{Time.at(t)} on:\nexample.com:ping")
702
+
703
+ expect(Flapjack::Data::Check).to receive(:lock).
704
+ with(Flapjack::Data::ScheduledMaintenance).
705
+ and_yield
706
+
707
+ expect(Flapjack::Data::Check).to receive(:find_by_ids).with(check.id).and_return([check])
708
+
709
+ expect(checks).to receive(:count).and_return(5)
710
+ expect(Flapjack::Data::Check).to receive(:intersect).
711
+ with(:name => Regexp.new("^example.com:p")).and_return(checks)
712
+
713
+ sched_maint = double(Flapjack::Data::ScheduledMaintenance)
714
+ expect(Flapjack::Data::ScheduledMaintenance).to receive(:new).with(
715
+ :start_time => t,
716
+ :end_time => t + (3 * 60 * 60),
717
+ :summary => 'test'
718
+ ).and_return(sched_maint)
719
+ expect(sched_maint).to receive(:save).and_return(true)
720
+
721
+ sched_maints = double('sched_maints')
722
+ expect(sched_maints).to receive(:<<).with(sched_maint)
723
+ expect(check).to receive(:scheduled_maintenances).and_return(sched_maints)
724
+
725
+ expect(check).to receive(:name).and_return('example.com:ping')
726
+
727
+ fji = Flapjack::Gateways::Jabber::Interpreter.new(:config => config)
728
+ fji.instance_variable_set('@bot', bot)
729
+ fji.interpret('room1', 'jim', now.to_i, 'maint checks matching /^example.com:p/ start-in: 1 minute duration: 3 hour comment: test')
730
+ end
731
+
732
+ it "interprets a received test notifications command (with name)" do
733
+ expect(bot).to receive(:announce).with('room1', /Testing notifications for check with name 'example.com:ping'/)
734
+
735
+ expect(Flapjack::Data::Check).to receive(:lock).
736
+ with(no_args).
737
+ and_yield
738
+
739
+ expect(Flapjack::Data::Check).to receive(:find_by_ids).with(check.id).and_return([check])
740
+
741
+ expect(checks).to receive(:empty?).and_return(false)
742
+ expect(checks).to receive(:count).and_return(5)
743
+ expect(Flapjack::Data::Check).to receive(:intersect).
744
+ with(:name => 'example.com:ping').and_return(checks)
745
+
746
+ expect(Flapjack::Data::Event).to receive(:test_notifications).
747
+ with('events', [check], an_instance_of(Hash))
748
+
749
+ fji = Flapjack::Gateways::Jabber::Interpreter.new(:config => config)
750
+ fji.instance_variable_set('@bot', bot)
751
+ fji.interpret('room1', 'jim', now.to_i, 'test notifications for example.com:ping')
752
+ end
753
+
754
+ it "interprets a received test notifications command (with tag)" do
755
+ expect(bot).to receive(:announce).with('room1', /Testing notifications for check with tag 'example.com'/)
756
+
757
+ expect(Flapjack::Data::Check).to receive(:lock).
758
+ with(Flapjack::Data::Tag).
759
+ and_yield
760
+
761
+ expect(Flapjack::Data::Check).to receive(:find_by_ids).with(check.id).and_return([check])
762
+
763
+ expect(checks).to receive(:count).and_return(5)
764
+ expect(tag).to receive(:checks).and_return(checks)
765
+
766
+ expect(Flapjack::Data::Tag).to receive(:intersect).
767
+ with(:name => 'example.com').and_return(tags)
768
+
769
+ expect(Flapjack::Data::Event).to receive(:test_notifications).
770
+ with('events', [check], an_instance_of(Hash))
771
+
772
+ fji = Flapjack::Gateways::Jabber::Interpreter.new(:config => config)
773
+ fji.instance_variable_set('@bot', bot)
774
+ fji.interpret('room1', 'jim', now.to_i, 'test notifications for checks with tag example.com')
775
+ end
776
+
777
+ it "interprets a received test notifications command (with regex)" do
778
+ expect(bot).to receive(:announce).with('room1', /Testing notifications for check matching \/\^example.com:p\//)
779
+
780
+ expect(Flapjack::Data::Check).to receive(:lock).
781
+ with(no_args).
782
+ and_yield
783
+
784
+ expect(Flapjack::Data::Check).to receive(:find_by_ids).with(check.id).and_return([check])
785
+
786
+ expect(checks).to receive(:count).and_return(5)
787
+ expect(Flapjack::Data::Check).to receive(:intersect).
788
+ with(:name => Regexp.new("^example.com:p")).and_return(checks)
789
+
790
+ expect(Flapjack::Data::Event).to receive(:test_notifications).
791
+ with('events', [check], an_instance_of(Hash))
792
+
793
+ fji = Flapjack::Gateways::Jabber::Interpreter.new(:config => config)
794
+ fji.instance_variable_set('@bot', bot)
795
+ fji.interpret('room1', 'jim', now.to_i, 'test notifications for checks matching /^example.com:p/')
796
+ end
797
+
798
+ it "doesn't interpret an unmatched command" do
799
+ expect(bot).to receive(:announce).with('room1', /^what do you mean/)
800
+
801
+ fji = Flapjack::Gateways::Jabber::Interpreter.new(:config => config)
802
+ fji.instance_variable_set('@bot', bot)
803
+ fji.interpret('room1', 'jim', now.to_i, 'hello!')
804
+ end
194
805
 
195
- fj.stop
196
806
  end
197
807
 
198
- it "runs a blocking loop listening for notifications" do
199
- timer = double('timer')
200
- expect(timer).to receive(:cancel)
201
- expect(EM::Synchrony).to receive(:add_periodic_timer).with(1).and_return(timer)
808
+ context 'XMPP' do
809
+
810
+ let(:client) { double(::Jabber::Client) }
811
+ let(:muc_client) { double(::Jabber::MUC::SimpleMUCClient) }
812
+ let(:muc_clients) { {config['rooms'].first => muc_client} }
813
+
814
+ # TODO use separate threads in the test instead?
815
+ it "starts and is stopped by a signal" do
816
+ interpreter = double(Flapjack::Gateways::Jabber::Interpreter)
817
+ expect(interpreter).to receive(:respond_to?).with(:interpret).and_return(true)
818
+ expect(interpreter).to receive(:receive_message).with(nil, 'jim', nil, 'hello!')
819
+ expect(interpreter).to receive(:receive_message).with(config['rooms'].first, 'jim', now.to_i, 'hello!')
820
+
821
+ expect(client).to receive(:on_exception)
822
+
823
+ msg_client = double('msg_client')
824
+ expect(msg_client).to receive(:type).and_return(:chat)
825
+ expect(msg_client).to receive(:body).exactly(3).times.and_return('hello!')
826
+ expect(msg_client).to receive(:from).and_return('jim')
827
+ expect(msg_client).to receive(:each_element).and_yield([]) # TODO improve
828
+
829
+ expect(client).to receive(:add_message_callback).and_yield(msg_client)
830
+
831
+ expect(muc_client).to receive(:on_message).and_yield(now.to_i, 'jim', 'flapjack: hello!')
832
+ expect(client).to receive(:is_connected?).times.and_return(true)
833
+
834
+ expect(::Jabber::Client).to receive(:new).and_return(client)
835
+ expect(::Jabber::MUC::SimpleMUCClient).to receive(:new).and_return(muc_client)
836
+
837
+ expect(lock).to receive(:synchronize).twice.and_yield
838
+ stop_cond = double(MonitorMixin::ConditionVariable)
839
+
840
+ fjb = Flapjack::Gateways::Jabber::Bot.new(:lock => lock,
841
+ :stop_condition => stop_cond, :config => config)
842
+ expect(stop_cond).to receive(:wait_until) {
843
+ fjb.instance_variable_set('@should_quit', true)
844
+ }
845
+ fjb.instance_variable_set('@siblings', [interpreter])
846
+
847
+ expect(fjb).to receive(:_join).with(client, muc_clients)
848
+ expect(fjb).to receive(:_leave).with(client, muc_clients)
849
+
850
+ fjb.start
851
+ end
852
+
853
+ it "should handle an exception and signal for leave and rejoin"
854
+
855
+ it "strips XML from a received string"
856
+
857
+ it "handles an announce state change" do
858
+ expect(client).to receive(:is_connected?).and_return(true)
859
+
860
+ fjb = Flapjack::Gateways::Jabber::Bot.new(:lock => lock,
861
+ :config => config)
862
+ expect(fjb).to receive(:_announce).with(muc_clients)
863
+ fjb.instance_variable_set('@state_buffer', ['announce'])
864
+ fjb.handle_state_change(client, muc_clients)
865
+ end
866
+
867
+ it "handles a say state change" do
868
+ expect(client).to receive(:is_connected?).and_return(true)
869
+
870
+ fjb = Flapjack::Gateways::Jabber::Bot.new(:lock => lock,
871
+ :config => config)
872
+ expect(fjb).to receive(:_say).with(client)
873
+ fjb.instance_variable_set('@state_buffer', ['say'])
874
+ fjb.handle_state_change(client, muc_clients)
875
+ end
876
+
877
+ it "handles a leave state change (when connected)" do
878
+ expect(client).to receive(:is_connected?).and_return(true)
879
+
880
+ fjb = Flapjack::Gateways::Jabber::Bot.new(:lock => lock,
881
+ :config => config)
882
+ expect(fjb).to receive(:_leave).with(client, muc_clients)
883
+ fjb.instance_variable_set('@state_buffer', ['leave'])
884
+ fjb.handle_state_change(client, muc_clients)
885
+ end
886
+
887
+ it "handles a leave state change (when not connected)" do
888
+ expect(client).to receive(:is_connected?).and_return(false)
889
+
890
+ fjb = Flapjack::Gateways::Jabber::Bot.new(:lock => lock,
891
+ :config => config)
892
+ expect(fjb).to receive(:_deactivate).with(muc_clients)
893
+ fjb.instance_variable_set('@state_buffer', ['leave'])
894
+ fjb.handle_state_change(client, muc_clients)
895
+ end
896
+
897
+ it "handles a rejoin state change" do
898
+ expect(client).to receive(:is_connected?).and_return(false)
899
+
900
+ fjb = Flapjack::Gateways::Jabber::Bot.new(:lock => lock,
901
+ :config => config)
902
+ expect(fjb).to receive(:_join).with(client, muc_clients, :rejoin => true)
903
+ fjb.instance_variable_set('@state_buffer', ['rejoin'])
904
+ fjb.handle_state_change(client, muc_clients)
905
+ end
906
+
907
+ it "joins the jabber client with chatbot_announce on" do
908
+ expect(client).to receive(:connect)
909
+ expect(client).to receive(:auth).with('password')
910
+ expect(client).to receive(:send) # .with(kind_of(::Jabber::Presence))
911
+
912
+ expect(lock).to receive(:synchronize).twice.and_yield.and_yield
913
+
914
+ expect(muc_client).to receive(:join).with('flapjacktest@conference.example.com/flapjack', nil, :history => false)
915
+ expect(muc_client).to receive(:say).with(/^flapjack jabber gateway started/)
916
+
917
+ fjb = Flapjack::Gateways::Jabber::Bot.new(:lock => lock,
918
+ :config => config)
919
+ fjb._join(client, muc_clients)
920
+ end
921
+
922
+ it "joins the jabber client with chatbot_announce off" do
923
+ config['chatbot_announce'] = false
924
+ expect(client).to receive(:connect)
925
+ expect(client).to receive(:auth).with('password')
926
+ expect(client).to receive(:send) # .with(kind_of(::Jabber::Presence))
927
+
928
+ expect(lock).to receive(:synchronize).twice.and_yield.and_yield
929
+
930
+ expect(muc_client).to receive(:join).with('flapjacktest@conference.example.com/flapjack', nil, :history => false)
931
+ expect(muc_client).to_not receive(:say).with(/^flapjack jabber gateway started/)
932
+
933
+ fjb = Flapjack::Gateways::Jabber::Bot.new(:lock => lock,
934
+ :config => config)
935
+ fjb._join(client, muc_clients)
936
+ end
937
+
938
+ it "rejoins the jabber client with chatbot_announce on" do
939
+ expect(client).to receive(:connect)
940
+ expect(client).to receive(:auth).with('password')
941
+ expect(client).to receive(:send).with(an_instance_of(::Jabber::Presence))
942
+
943
+ expect(lock).to receive(:synchronize).twice.and_yield.and_yield
944
+
945
+ expect(muc_client).to receive(:join).with('flapjacktest@conference.example.com/flapjack', nil, :history => false)
946
+ expect(muc_client).to receive(:say).with(/^flapjack jabber gateway rejoining/)
947
+
948
+ fjb = Flapjack::Gateways::Jabber::Bot.new(:lock => lock,
949
+ :config => config)
950
+ fjb._join(client, muc_clients, :rejoin => true)
951
+ end
952
+
953
+ it "rejoins the jabber client with chatbot_announce off" do
954
+ config['chatbot_announce'] = false
955
+ expect(client).to receive(:connect)
956
+ expect(client).to receive(:auth).with('password')
957
+ expect(client).to receive(:send).with(an_instance_of(::Jabber::Presence))
958
+
959
+ expect(lock).to receive(:synchronize).twice.and_yield.and_yield
960
+
961
+ expect(muc_client).to receive(:join).with('flapjacktest@conference.example.com/flapjack', nil, :history => false)
962
+ expect(muc_client).to_not receive(:say).with(/^flapjack jabber gateway rejoining/)
963
+
964
+ fjb = Flapjack::Gateways::Jabber::Bot.new(:lock => lock,
965
+ :config => config)
966
+ fjb._join(client, muc_clients, :rejoin => true)
967
+ end
968
+
969
+ it "leaves the jabber client (connected)" do
970
+ expect(muc_client).to receive(:active?).and_return(true)
971
+ expect(muc_client).to receive(:exit)
972
+ expect(client).to receive(:close)
973
+
974
+ fjb = Flapjack::Gateways::Jabber::Bot.new(:lock => lock,
975
+ :config => config)
976
+ fjb.instance_variable_set('@joined', true)
977
+ fjb._leave(client, muc_clients)
978
+ end
979
+
980
+ it "deactivates the jabber client (not connected)" do
981
+ expect(muc_client).to receive(:deactivate)
982
+
983
+ fjb = Flapjack::Gateways::Jabber::Bot.new(:lock => lock,
984
+ :config => config)
985
+ fjb._deactivate(muc_clients)
986
+ end
987
+
988
+ it "speaks its announce buffer" do
989
+ expect(muc_client).to receive(:active?).and_return(true)
990
+ expect(muc_client).to receive(:say).with('hello!')
991
+
992
+ fjb = Flapjack::Gateways::Jabber::Bot.new(:lock => lock, :config => config)
993
+ fjb.instance_variable_set('@announce_buffer', [{:room => 'room1', :msg => 'hello!'}])
994
+ fjb._announce('room1' => muc_client)
995
+ end
202
996
 
203
- redis = double('redis')
997
+ it "speaks its say buffer" do
998
+ message = double(::Jabber::Message)
999
+ expect(message).to receive(:type=).with(:chat)
1000
+ expect(::Jabber::Message).to receive(:new).
1001
+ with('jim', 'hello!').and_return(message)
204
1002
 
205
- expect(Flapjack::RedisPool).to receive(:new).and_return(redis)
206
- fj = Flapjack::Gateways::Jabber.new(:config => config, :logger => @logger)
207
- expect(fj).to receive(:register_handler).exactly(4).times
1003
+ expect(client).to receive(:send).with(message)
208
1004
 
209
- expect(fj).to receive(:connect)
210
- expect(fj).to receive(:connected?).exactly(3).times.and_return(true)
1005
+ fjb = Flapjack::Gateways::Jabber::Bot.new(:lock => lock, :config => config)
1006
+ fjb.instance_variable_set('@say_buffer', [{:nick => 'jim', :msg => 'hello!'}])
1007
+ fjb._say(client)
1008
+ end
211
1009
 
212
- blpop_count = 0
1010
+ it "buffers an announce message and sends a signal" do
1011
+ expect(lock).to receive(:synchronize).and_yield
1012
+ expect(stop_cond).to receive(:signal)
213
1013
 
214
- event_json = '{"notification_type":"problem","event_id":"main-example.com:ping",' +
215
- '"state":"critical","summary":"!!!","duration":43,"state_duration":76}'
216
- expect(redis).to receive(:blpop).twice {
217
- blpop_count += 1
218
- if blpop_count == 1
219
- ["jabber_notifications", event_json]
220
- else
221
- fj.instance_variable_set('@should_quit', true)
222
- ["jabber_notifications", %q{{"notification_type":"shutdown"}}]
223
- end
224
- }
1014
+ fjb = Flapjack::Gateways::Jabber::Bot.new(:lock => lock, :stop_condition => stop_cond,
1015
+ :config => config)
1016
+ fjb.announce('room1', 'hello!')
1017
+ expect(fjb.instance_variable_get('@state_buffer')).to eq(['announce'])
1018
+ expect(fjb.instance_variable_get('@announce_buffer')).to eq([{:room => 'room1', :msg => 'hello!'}])
1019
+ end
225
1020
 
226
- expect(EventMachine::Synchrony).to receive(:next_tick).twice.and_yield
227
- expect(fj).to receive(:write).with(an_instance_of(Blather::Stanza::Message))
228
- expect(fj).to receive(:close)
1021
+ it "buffers a say message and sends a signal" do
1022
+ expect(lock).to receive(:synchronize).and_yield
1023
+ expect(stop_cond).to receive(:signal)
229
1024
 
230
- fj.start
1025
+ fjb = Flapjack::Gateways::Jabber::Bot.new(:lock => lock, :stop_condition => stop_cond,
1026
+ :config => config)
1027
+ fjb.say('jim', 'hello!')
1028
+ expect(fjb.instance_variable_get('@state_buffer')).to eq(['say'])
1029
+ expect(fjb.instance_variable_get('@say_buffer')).to eq([{:nick => 'jim', :msg => 'hello!'}])
1030
+ end
231
1031
 
232
- expect(@logger.errors).to be_empty
233
1032
  end
234
1033
 
235
1034
  end