trigger_agent_spec.rb 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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 "does fine without dots in the path" do
  221. @event.payload = { 'hello' => "world" }
  222. @checker.options['rules'].first['type'] = "field==value"
  223. @checker.options['rules'].first['path'] = "hello"
  224. @checker.options['rules'].first['value'] = "world"
  225. expect {
  226. @checker.receive([@event])
  227. }.to change { Event.count }.by(1)
  228. @checker.options['rules'].first['path'] = "foo"
  229. expect {
  230. @checker.receive([@event])
  231. }.not_to change { Event.count }
  232. @checker.options['rules'].first['value'] = "hi"
  233. expect {
  234. @checker.receive([@event])
  235. }.not_to change { Event.count }
  236. end
  237. it "handles multiple events" do
  238. event2 = Event.new
  239. event2.agent = agents(:bob_weather_agent)
  240. event2.payload = { 'foo' => { 'bar' => { 'baz' => "a2b" }}}
  241. event3 = Event.new
  242. event3.agent = agents(:bob_weather_agent)
  243. event3.payload = { 'foo' => { 'bar' => { 'baz' => "a222b" }}}
  244. expect {
  245. @checker.receive([@event, event2, event3])
  246. }.to change { Event.count }.by(2)
  247. end
  248. describe "with multiple rules" do
  249. before do
  250. @checker.options['rules'] << {
  251. 'type' => "field>=value",
  252. 'value' => "4",
  253. 'path' => "foo.bing"
  254. }
  255. end
  256. it "handles ANDing rules together" do
  257. @event.payload['foo']["bing"] = "5"
  258. expect {
  259. @checker.receive([@event])
  260. }.to change { Event.count }.by(1)
  261. @event.payload['foo']["bing"] = "2"
  262. expect {
  263. @checker.receive([@event])
  264. }.not_to change { Event.count }
  265. end
  266. it "can accept a partial rule set match when 'must_match' is present and less than the total number of rules" do
  267. @checker.options['must_match'] = "1"
  268. @event.payload['foo']["bing"] = "5" # 5 > 4
  269. expect {
  270. @checker.receive([@event])
  271. }.to change { Event.count }.by(1)
  272. @event.payload['foo']["bing"] = "2" # 2 !> 4
  273. expect {
  274. @checker.receive([@event])
  275. }.to change { Event.count } # but the first one matches
  276. @checker.options['must_match'] = "2"
  277. @event.payload['foo']["bing"] = "5" # 5 > 4
  278. expect {
  279. @checker.receive([@event])
  280. }.to change { Event.count }.by(1)
  281. @event.payload['foo']["bing"] = "2" # 2 !> 4
  282. expect {
  283. @checker.receive([@event])
  284. }.not_to change { Event.count } # only 1 matches, we needed 2
  285. end
  286. end
  287. describe "when 'keep_event' is true" do
  288. before do
  289. @checker.options['keep_event'] = 'true'
  290. @event.payload['foo']['bar']['baz'] = "5"
  291. @checker.options['rules'].first['type'] = "field<value"
  292. end
  293. it "can re-emit the origin event" do
  294. @checker.options['rules'].first['value'] = 3
  295. @checker.options['message'] = ''
  296. @event.payload['message'] = 'hi there'
  297. expect {
  298. @checker.receive([@event])
  299. }.not_to change { Event.count }
  300. @checker.options['rules'].first['value'] = 6
  301. expect {
  302. @checker.receive([@event])
  303. }.to change { Event.count }.by(1)
  304. expect(@checker.most_recent_event.payload).to eq(@event.payload)
  305. end
  306. it "merges 'message' into the original event when present" do
  307. @checker.options['rules'].first['value'] = 6
  308. @checker.receive([@event])
  309. expect(@checker.most_recent_event.payload).to eq(@event.payload.merge(:message => "I saw '5' from Joe"))
  310. end
  311. end
  312. end
  313. end