trigger_agent_spec.rb 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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. it "should validate non-hash rules" do
  73. @checker.options['rules'] << "{% if status == 'ok' %}true{% endif %}"
  74. expect(@checker).to be_valid
  75. @checker.options['rules'] << []
  76. expect(@checker).not_to be_valid
  77. end
  78. end
  79. describe "#working?" do
  80. it "checks to see if the Agent has received any events in the last 'expected_receive_period_in_days' days" do
  81. @event.save!
  82. expect(@checker).not_to be_working # no events have ever been received
  83. Agents::TriggerAgent.async_receive(@checker.id, [@event.id])
  84. expect(@checker.reload).to be_working # Events received
  85. three_days_from_now = 3.days.from_now
  86. stub(Time).now { three_days_from_now }
  87. expect(@checker.reload).not_to be_working # too much time has passed
  88. end
  89. end
  90. describe "#receive" do
  91. it "handles regex" do
  92. @event.payload['foo']['bar']['baz'] = "a222b"
  93. expect {
  94. @checker.receive([@event])
  95. }.not_to change { Event.count }
  96. @event.payload['foo']['bar']['baz'] = "a2b"
  97. expect {
  98. @checker.receive([@event])
  99. }.to change { Event.count }.by(1)
  100. end
  101. it "handles array of regex" do
  102. @event.payload['foo']['bar']['baz'] = "a222b"
  103. @checker.options['rules'][0] = {
  104. 'type' => "regex",
  105. 'value' => ["a\\db", "a\\Wb"],
  106. 'path' => "foo.bar.baz",
  107. }
  108. expect {
  109. @checker.receive([@event])
  110. }.not_to change { Event.count }
  111. @event.payload['foo']['bar']['baz'] = "a2b"
  112. expect {
  113. @checker.receive([@event])
  114. }.to change { Event.count }.by(1)
  115. @event.payload['foo']['bar']['baz'] = "a b"
  116. expect {
  117. @checker.receive([@event])
  118. }.to change { Event.count }.by(1)
  119. end
  120. it "handles negated regex" do
  121. @event.payload['foo']['bar']['baz'] = "a2b"
  122. @checker.options['rules'][0] = {
  123. 'type' => "!regex",
  124. 'value' => "a\\db",
  125. 'path' => "foo.bar.baz",
  126. }
  127. expect {
  128. @checker.receive([@event])
  129. }.not_to change { Event.count }
  130. @event.payload['foo']['bar']['baz'] = "a22b"
  131. expect {
  132. @checker.receive([@event])
  133. }.to change { Event.count }.by(1)
  134. end
  135. it "handles array of negated regex" do
  136. @event.payload['foo']['bar']['baz'] = "a2b"
  137. @checker.options['rules'][0] = {
  138. 'type' => "!regex",
  139. 'value' => ["a\\db", "a2b"],
  140. 'path' => "foo.bar.baz",
  141. }
  142. expect {
  143. @checker.receive([@event])
  144. }.not_to change { Event.count }
  145. @event.payload['foo']['bar']['baz'] = "a3b"
  146. expect {
  147. @checker.receive([@event])
  148. }.to change { Event.count }.by(1)
  149. end
  150. it "puts can extract values into the message based on paths" do
  151. @checker.receive([@event])
  152. expect(Event.last.payload['message']).to eq("I saw 'a2b' from Joe")
  153. end
  154. it "handles numerical comparisons" do
  155. @event.payload['foo']['bar']['baz'] = "5"
  156. @checker.options['rules'].first['value'] = 6
  157. @checker.options['rules'].first['type'] = "field<value"
  158. expect {
  159. @checker.receive([@event])
  160. }.to change { Event.count }.by(1)
  161. @checker.options['rules'].first['value'] = 3
  162. expect {
  163. @checker.receive([@event])
  164. }.not_to change { Event.count }
  165. end
  166. it "handles array of numerical comparisons" do
  167. @event.payload['foo']['bar']['baz'] = "5"
  168. @checker.options['rules'].first['value'] = [6, 3]
  169. @checker.options['rules'].first['type'] = "field<value"
  170. expect {
  171. @checker.receive([@event])
  172. }.to change { Event.count }.by(1)
  173. @checker.options['rules'].first['value'] = [4, 3]
  174. expect {
  175. @checker.receive([@event])
  176. }.not_to change { Event.count }
  177. end
  178. it "handles exact comparisons" do
  179. @event.payload['foo']['bar']['baz'] = "hello world"
  180. @checker.options['rules'].first['type'] = "field==value"
  181. @checker.options['rules'].first['value'] = "hello there"
  182. expect {
  183. @checker.receive([@event])
  184. }.not_to change { Event.count }
  185. @checker.options['rules'].first['value'] = "hello world"
  186. expect {
  187. @checker.receive([@event])
  188. }.to change { Event.count }.by(1)
  189. end
  190. it "handles array of exact comparisons" do
  191. @event.payload['foo']['bar']['baz'] = "hello world"
  192. @checker.options['rules'].first['type'] = "field==value"
  193. @checker.options['rules'].first['value'] = ["hello there", "hello universe"]
  194. expect {
  195. @checker.receive([@event])
  196. }.not_to change { Event.count }
  197. @checker.options['rules'].first['value'] = ["hello world", "hello universe"]
  198. expect {
  199. @checker.receive([@event])
  200. }.to change { Event.count }.by(1)
  201. end
  202. it "handles negated comparisons" do
  203. @event.payload['foo']['bar']['baz'] = "hello world"
  204. @checker.options['rules'].first['type'] = "field!=value"
  205. @checker.options['rules'].first['value'] = "hello world"
  206. expect {
  207. @checker.receive([@event])
  208. }.not_to change { Event.count }
  209. @checker.options['rules'].first['value'] = "hello there"
  210. expect {
  211. @checker.receive([@event])
  212. }.to change { Event.count }.by(1)
  213. end
  214. it "handles array of negated comparisons" do
  215. @event.payload['foo']['bar']['baz'] = "hello world"
  216. @checker.options['rules'].first['type'] = "field!=value"
  217. @checker.options['rules'].first['value'] = ["hello world", "hello world"]
  218. expect {
  219. @checker.receive([@event])
  220. }.not_to change { Event.count }
  221. @checker.options['rules'].first['value'] = ["hello there", "hello world"]
  222. expect {
  223. @checker.receive([@event])
  224. }.to change { Event.count }.by(1)
  225. end
  226. it "handles array of `not in` comparisons" do
  227. @event.payload['foo']['bar']['baz'] = "hello world"
  228. @checker.options['rules'].first['type'] = "not in"
  229. @checker.options['rules'].first['value'] = ["hello world", "hello world"]
  230. expect {
  231. @checker.receive([@event])
  232. }.not_to change { Event.count }
  233. @checker.options['rules'].first['value'] = ["hello there", "hello world"]
  234. expect {
  235. @checker.receive([@event])
  236. }.not_to change { Event.count }
  237. @checker.options['rules'].first['value'] = ["hello there", "hello here"]
  238. expect {
  239. @checker.receive([@event])
  240. }.to change { Event.count }.by(1)
  241. end
  242. it "does fine without dots in the path" do
  243. @event.payload = { 'hello' => "world" }
  244. @checker.options['rules'].first['type'] = "field==value"
  245. @checker.options['rules'].first['path'] = "hello"
  246. @checker.options['rules'].first['value'] = "world"
  247. expect {
  248. @checker.receive([@event])
  249. }.to change { Event.count }.by(1)
  250. @checker.options['rules'].first['path'] = "foo"
  251. expect {
  252. @checker.receive([@event])
  253. }.not_to change { Event.count }
  254. @checker.options['rules'].first['value'] = "hi"
  255. expect {
  256. @checker.receive([@event])
  257. }.not_to change { Event.count }
  258. end
  259. it "handles multiple events" do
  260. event2 = Event.new
  261. event2.agent = agents(:bob_weather_agent)
  262. event2.payload = { 'foo' => { 'bar' => { 'baz' => "a2b" }}}
  263. event3 = Event.new
  264. event3.agent = agents(:bob_weather_agent)
  265. event3.payload = { 'foo' => { 'bar' => { 'baz' => "a222b" }}}
  266. expect {
  267. @checker.receive([@event, event2, event3])
  268. }.to change { Event.count }.by(2)
  269. end
  270. it "handles Liquid rules" do
  271. event1 = Event.create!(
  272. agent: agents(:bob_rain_notifier_agent),
  273. payload: { 'hello' => 'world1', 'created_at' => '2019-03-27T08:54:12+09:00' }
  274. )
  275. event2 = Event.create!(
  276. agent: agents(:bob_rain_notifier_agent),
  277. payload: { 'hello' => 'world2', 'created_at' => '2019-03-27T09:17:01+09:00' }
  278. )
  279. @checker.options['message'] = '{{hello}}'
  280. @checker.options['rules'] = [
  281. "{% assign value = created_at | date: '%s' | plus: 0 %}{% assign threshold = 'now' | date: '%s' | minus: 86400 %}{% if value > threshold %}true{% endif %}"
  282. ]
  283. expect {
  284. travel_to(Time.parse('2019-03-28T00:00:00+00:00')) {
  285. @checker.receive([event1, event2])
  286. }
  287. }.to change { Event.count }.by(1)
  288. expect(Event.last.payload['message']).to eq("world2")
  289. end
  290. describe "with multiple rules" do
  291. before do
  292. @checker.options['rules'] << {
  293. 'type' => "field>=value",
  294. 'value' => "4",
  295. 'path' => "foo.bing"
  296. }
  297. end
  298. it "handles ANDing rules together" do
  299. @event.payload['foo']["bing"] = "5"
  300. expect {
  301. @checker.receive([@event])
  302. }.to change { Event.count }.by(1)
  303. @event.payload['foo']["bing"] = "2"
  304. expect {
  305. @checker.receive([@event])
  306. }.not_to change { Event.count }
  307. end
  308. it "can accept a partial rule set match when 'must_match' is present and less than the total number of rules" do
  309. @checker.options['must_match'] = "1"
  310. @event.payload['foo']["bing"] = "5" # 5 > 4
  311. expect {
  312. @checker.receive([@event])
  313. }.to change { Event.count }.by(1)
  314. @event.payload['foo']["bing"] = "2" # 2 !> 4
  315. expect {
  316. @checker.receive([@event])
  317. }.to change { Event.count } # but the first one matches
  318. @checker.options['must_match'] = "2"
  319. @event.payload['foo']["bing"] = "5" # 5 > 4
  320. expect {
  321. @checker.receive([@event])
  322. }.to change { Event.count }.by(1)
  323. @event.payload['foo']["bing"] = "2" # 2 !> 4
  324. expect {
  325. @checker.receive([@event])
  326. }.not_to change { Event.count } # only 1 matches, we needed 2
  327. end
  328. end
  329. describe "when 'keep_event' is true" do
  330. before do
  331. @checker.options['keep_event'] = 'true'
  332. @event.payload['foo']['bar']['baz'] = "5"
  333. @checker.options['rules'].first['type'] = "field<value"
  334. end
  335. it "can re-emit the origin event" do
  336. @checker.options['rules'].first['value'] = 3
  337. @checker.options['message'] = ''
  338. @event.payload['message'] = 'hi there'
  339. expect {
  340. @checker.receive([@event])
  341. }.not_to change { Event.count }
  342. @checker.options['rules'].first['value'] = 6
  343. expect {
  344. @checker.receive([@event])
  345. }.to change { Event.count }.by(1)
  346. expect(@checker.most_recent_event.payload).to eq(@event.payload)
  347. end
  348. it "merges 'message' into the original event when present" do
  349. @checker.options['rules'].first['value'] = 6
  350. @checker.receive([@event])
  351. expect(@checker.most_recent_event.payload).to eq(@event.payload.merge(:message => "I saw '5' from Joe"))
  352. end
  353. end
  354. end
  355. end