trigger_agent_spec.rb 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. require 'rails_helper'
  2. describe Agents::TriggerAgent do
  3. before do
  4. @valid_params = {
  5. 'name' => "my trigger agent",
  6. 'options' => {
  7. 'expected_receive_period_in_days' => 2,
  8. 'rules' => [{
  9. 'type' => "regex",
  10. 'value' => "a\\db",
  11. 'path' => "foo.bar.baz",
  12. }],
  13. 'message' => "I saw '{{foo.bar.baz}}' from {{name}}"
  14. }
  15. }
  16. @checker = Agents::TriggerAgent.new(@valid_params)
  17. @checker.user = users(:bob)
  18. @checker.save!
  19. @event = Event.new
  20. @event.agent = agents(:bob_rain_notifier_agent)
  21. @event.payload = { 'foo' => { "bar" => { 'baz' => "a2b" }},
  22. 'name' => "Joe" }
  23. end
  24. describe "validation" do
  25. before do
  26. expect(@checker).to be_valid
  27. end
  28. it "should validate presence of message" do
  29. @checker.options['message'] = nil
  30. expect(@checker).not_to be_valid
  31. @checker.options['message'] = ''
  32. expect(@checker).not_to be_valid
  33. end
  34. it "should be valid without a message when 'keep_event' is set" do
  35. @checker.options['keep_event'] = 'true'
  36. @checker.options['message'] = ''
  37. expect(@checker).to be_valid
  38. end
  39. it "if present, 'keep_event' must equal true or false" do
  40. @checker.options['keep_event'] = 'true'
  41. expect(@checker).to be_valid
  42. @checker.options['keep_event'] = 'false'
  43. expect(@checker).to be_valid
  44. @checker.options['keep_event'] = ''
  45. expect(@checker).to be_valid
  46. @checker.options['keep_event'] = 'tralse'
  47. expect(@checker).not_to be_valid
  48. end
  49. it "validates that 'must_match' is a positive integer, not greater than the number of rules, if provided" do
  50. @checker.options['must_match'] = '1'
  51. expect(@checker).to be_valid
  52. @checker.options['must_match'] = '0'
  53. expect(@checker).not_to be_valid
  54. @checker.options['must_match'] = 'wrong'
  55. expect(@checker).not_to be_valid
  56. @checker.options['must_match'] = ''
  57. expect(@checker).to be_valid
  58. @checker.options.delete('must_match')
  59. expect(@checker).to be_valid
  60. @checker.options['must_match'] = '2'
  61. expect(@checker).not_to be_valid
  62. expect(@checker.errors[:base].first).to match(/equal to or less than the number of rules/)
  63. end
  64. it "should validate the three fields in each rule" do
  65. @checker.options['rules'] << { 'path' => "foo", 'type' => "fake", 'value' => "6" }
  66. expect(@checker).not_to be_valid
  67. @checker.options['rules'].last['type'] = "field>=value"
  68. expect(@checker).to be_valid
  69. @checker.options['rules'].last.delete('value')
  70. expect(@checker).not_to be_valid
  71. end
  72. end
  73. describe "#working?" do
  74. it "checks to see if the Agent has received any events in the last 'expected_receive_period_in_days' days" do
  75. @event.save!
  76. expect(@checker).not_to be_working # no events have ever been received
  77. Agents::TriggerAgent.async_receive(@checker.id, [@event.id])
  78. expect(@checker.reload).to be_working # Events received
  79. three_days_from_now = 3.days.from_now
  80. stub(Time).now { three_days_from_now }
  81. expect(@checker.reload).not_to be_working # too much time has passed
  82. end
  83. end
  84. describe "#receive" do
  85. it "handles regex" do
  86. @event.payload['foo']['bar']['baz'] = "a222b"
  87. expect {
  88. @checker.receive([@event])
  89. }.not_to change { Event.count }
  90. @event.payload['foo']['bar']['baz'] = "a2b"
  91. expect {
  92. @checker.receive([@event])
  93. }.to change { Event.count }.by(1)
  94. end
  95. it "handles array of regex" do
  96. @event.payload['foo']['bar']['baz'] = "a222b"
  97. @checker.options['rules'][0] = {
  98. 'type' => "regex",
  99. 'value' => ["a\\db", "a\\Wb"],
  100. 'path' => "foo.bar.baz",
  101. }
  102. expect {
  103. @checker.receive([@event])
  104. }.not_to change { Event.count }
  105. @event.payload['foo']['bar']['baz'] = "a2b"
  106. expect {
  107. @checker.receive([@event])
  108. }.to change { Event.count }.by(1)
  109. @event.payload['foo']['bar']['baz'] = "a b"
  110. expect {
  111. @checker.receive([@event])
  112. }.to change { Event.count }.by(1)
  113. end
  114. it "handles negated regex" do
  115. @event.payload['foo']['bar']['baz'] = "a2b"
  116. @checker.options['rules'][0] = {
  117. 'type' => "!regex",
  118. 'value' => "a\\db",
  119. 'path' => "foo.bar.baz",
  120. }
  121. expect {
  122. @checker.receive([@event])
  123. }.not_to change { Event.count }
  124. @event.payload['foo']['bar']['baz'] = "a22b"
  125. expect {
  126. @checker.receive([@event])
  127. }.to change { Event.count }.by(1)
  128. end
  129. it "handles array of negated regex" do
  130. @event.payload['foo']['bar']['baz'] = "a2b"
  131. @checker.options['rules'][0] = {
  132. 'type' => "!regex",
  133. 'value' => ["a\\db", "a2b"],
  134. 'path' => "foo.bar.baz",
  135. }
  136. expect {
  137. @checker.receive([@event])
  138. }.not_to change { Event.count }
  139. @event.payload['foo']['bar']['baz'] = "a3b"
  140. expect {
  141. @checker.receive([@event])
  142. }.to change { Event.count }.by(1)
  143. end
  144. it "puts can extract values into the message based on paths" do
  145. @checker.receive([@event])
  146. expect(Event.last.payload['message']).to eq("I saw 'a2b' from Joe")
  147. end
  148. it "handles numerical comparisons" do
  149. @event.payload['foo']['bar']['baz'] = "5"
  150. @checker.options['rules'].first['value'] = 6
  151. @checker.options['rules'].first['type'] = "field<value"
  152. expect {
  153. @checker.receive([@event])
  154. }.to change { Event.count }.by(1)
  155. @checker.options['rules'].first['value'] = 3
  156. expect {
  157. @checker.receive([@event])
  158. }.not_to change { Event.count }
  159. end
  160. it "handles array of numerical comparisons" do
  161. @event.payload['foo']['bar']['baz'] = "5"
  162. @checker.options['rules'].first['value'] = [6, 3]
  163. @checker.options['rules'].first['type'] = "field<value"
  164. expect {
  165. @checker.receive([@event])
  166. }.to change { Event.count }.by(1)
  167. @checker.options['rules'].first['value'] = [4, 3]
  168. expect {
  169. @checker.receive([@event])
  170. }.not_to change { Event.count }
  171. end
  172. it "handles exact comparisons" do
  173. @event.payload['foo']['bar']['baz'] = "hello world"
  174. @checker.options['rules'].first['type'] = "field==value"
  175. @checker.options['rules'].first['value'] = "hello there"
  176. expect {
  177. @checker.receive([@event])
  178. }.not_to change { Event.count }
  179. @checker.options['rules'].first['value'] = "hello world"
  180. expect {
  181. @checker.receive([@event])
  182. }.to change { Event.count }.by(1)
  183. end
  184. it "handles array of exact comparisons" do
  185. @event.payload['foo']['bar']['baz'] = "hello world"
  186. @checker.options['rules'].first['type'] = "field==value"
  187. @checker.options['rules'].first['value'] = ["hello there", "hello universe"]
  188. expect {
  189. @checker.receive([@event])
  190. }.not_to change { Event.count }
  191. @checker.options['rules'].first['value'] = ["hello world", "hello universe"]
  192. expect {
  193. @checker.receive([@event])
  194. }.to change { Event.count }.by(1)
  195. end
  196. it "handles negated comparisons" do
  197. @event.payload['foo']['bar']['baz'] = "hello world"
  198. @checker.options['rules'].first['type'] = "field!=value"
  199. @checker.options['rules'].first['value'] = "hello world"
  200. expect {
  201. @checker.receive([@event])
  202. }.not_to change { Event.count }
  203. @checker.options['rules'].first['value'] = "hello there"
  204. expect {
  205. @checker.receive([@event])
  206. }.to change { Event.count }.by(1)
  207. end
  208. it "handles array of negated comparisons" do
  209. @event.payload['foo']['bar']['baz'] = "hello world"
  210. @checker.options['rules'].first['type'] = "field!=value"
  211. @checker.options['rules'].first['value'] = ["hello world", "hello world"]
  212. expect {
  213. @checker.receive([@event])
  214. }.not_to change { Event.count }
  215. @checker.options['rules'].first['value'] = ["hello there", "hello world"]
  216. expect {
  217. @checker.receive([@event])
  218. }.to change { Event.count }.by(1)
  219. end
  220. it "handles array of `not in` comparisons" do
  221. @event.payload['foo']['bar']['baz'] = "hello world"
  222. @checker.options['rules'].first['type'] = "not in"
  223. @checker.options['rules'].first['value'] = ["hello world", "hello world"]
  224. expect {
  225. @checker.receive([@event])
  226. }.not_to change { Event.count }
  227. @checker.options['rules'].first['value'] = ["hello there", "hello world"]
  228. expect {
  229. @checker.receive([@event])
  230. }.not_to change { Event.count }
  231. @checker.options['rules'].first['value'] = ["hello there", "hello here"]
  232. expect {
  233. @checker.receive([@event])
  234. }.to change { Event.count }.by(1)
  235. end
  236. it "does fine without dots in the path" do
  237. @event.payload = { 'hello' => "world" }
  238. @checker.options['rules'].first['type'] = "field==value"
  239. @checker.options['rules'].first['path'] = "hello"
  240. @checker.options['rules'].first['value'] = "world"
  241. expect {
  242. @checker.receive([@event])
  243. }.to change { Event.count }.by(1)
  244. @checker.options['rules'].first['path'] = "foo"
  245. expect {
  246. @checker.receive([@event])
  247. }.not_to change { Event.count }
  248. @checker.options['rules'].first['value'] = "hi"
  249. expect {
  250. @checker.receive([@event])
  251. }.not_to change { Event.count }
  252. end
  253. it "handles multiple events" do
  254. event2 = Event.new
  255. event2.agent = agents(:bob_weather_agent)
  256. event2.payload = { 'foo' => { 'bar' => { 'baz' => "a2b" }}}
  257. event3 = Event.new
  258. event3.agent = agents(:bob_weather_agent)
  259. event3.payload = { 'foo' => { 'bar' => { 'baz' => "a222b" }}}
  260. expect {
  261. @checker.receive([@event, event2, event3])
  262. }.to change { Event.count }.by(2)
  263. end
  264. describe "with multiple rules" do
  265. before do
  266. @checker.options['rules'] << {
  267. 'type' => "field>=value",
  268. 'value' => "4",
  269. 'path' => "foo.bing"
  270. }
  271. end
  272. it "handles ANDing rules together" do
  273. @event.payload['foo']["bing"] = "5"
  274. expect {
  275. @checker.receive([@event])
  276. }.to change { Event.count }.by(1)
  277. @event.payload['foo']["bing"] = "2"
  278. expect {
  279. @checker.receive([@event])
  280. }.not_to change { Event.count }
  281. end
  282. it "can accept a partial rule set match when 'must_match' is present and less than the total number of rules" do
  283. @checker.options['must_match'] = "1"
  284. @event.payload['foo']["bing"] = "5" # 5 > 4
  285. expect {
  286. @checker.receive([@event])
  287. }.to change { Event.count }.by(1)
  288. @event.payload['foo']["bing"] = "2" # 2 !> 4
  289. expect {
  290. @checker.receive([@event])
  291. }.to change { Event.count } # but the first one matches
  292. @checker.options['must_match'] = "2"
  293. @event.payload['foo']["bing"] = "5" # 5 > 4
  294. expect {
  295. @checker.receive([@event])
  296. }.to change { Event.count }.by(1)
  297. @event.payload['foo']["bing"] = "2" # 2 !> 4
  298. expect {
  299. @checker.receive([@event])
  300. }.not_to change { Event.count } # only 1 matches, we needed 2
  301. end
  302. end
  303. describe "when 'keep_event' is true" do
  304. before do
  305. @checker.options['keep_event'] = 'true'
  306. @event.payload['foo']['bar']['baz'] = "5"
  307. @checker.options['rules'].first['type'] = "field<value"
  308. end
  309. it "can re-emit the origin event" do
  310. @checker.options['rules'].first['value'] = 3
  311. @checker.options['message'] = ''
  312. @event.payload['message'] = 'hi there'
  313. expect {
  314. @checker.receive([@event])
  315. }.not_to change { Event.count }
  316. @checker.options['rules'].first['value'] = 6
  317. expect {
  318. @checker.receive([@event])
  319. }.to change { Event.count }.by(1)
  320. expect(@checker.most_recent_event.payload).to eq(@event.payload)
  321. end
  322. it "merges 'message' into the original event when present" do
  323. @checker.options['rules'].first['value'] = 6
  324. @checker.receive([@event])
  325. expect(@checker.most_recent_event.payload).to eq(@event.payload.merge(:message => "I saw '5' from Joe"))
  326. end
  327. end
  328. end
  329. end