Aucune description http://j1x-huginn.herokuapp.com

trigger_agent_spec.rb 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. require 'spec_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. @checker.should be_valid
  27. end
  28. it "should validate presence of options" do
  29. @checker.options[:message] = nil
  30. @checker.should_not be_valid
  31. end
  32. it "should validate the three fields in each rule" do
  33. @checker.options[:rules] << { :path => "foo", :type => "fake", :value => "6" }
  34. @checker.should_not be_valid
  35. @checker.options[:rules].last[:type] = "field>=value"
  36. @checker.should be_valid
  37. @checker.options[:rules].last.delete(:value)
  38. @checker.should_not be_valid
  39. end
  40. end
  41. describe "#working?" do
  42. it "checks to see if the Agent has received any events in the last :expected_receive_period_in_days days" do
  43. @event.save!
  44. @checker.should_not be_working # no events have ever been received
  45. Agents::TriggerAgent.async_receive(@checker.id, [@event.id])
  46. @checker.reload.should be_working # Events received
  47. three_days_from_now = 3.days.from_now
  48. stub(Time).now { three_days_from_now }
  49. @checker.reload.should_not be_working # too much time has passed
  50. end
  51. end
  52. describe "#receive" do
  53. it "handles regex" do
  54. @event.payload[:foo]["bar"][:baz] = "a222b"
  55. lambda {
  56. @checker.receive([@event])
  57. }.should_not change { Event.count }
  58. @event.payload[:foo]["bar"][:baz] = "a2b"
  59. lambda {
  60. @checker.receive([@event])
  61. }.should change { Event.count }.by(1)
  62. end
  63. it "handles negated regex" do
  64. @event.payload[:foo]["bar"][:baz] = "a2b"
  65. @checker.options[:rules][0] = {
  66. :type => "!regex",
  67. :value => "a\\db",
  68. :path => "foo.bar.baz",
  69. }
  70. lambda {
  71. @checker.receive([@event])
  72. }.should_not change { Event.count }
  73. @event.payload[:foo]["bar"][:baz] = "a22b"
  74. lambda {
  75. @checker.receive([@event])
  76. }.should change { Event.count }.by(1)
  77. end
  78. it "puts can extract values into the message based on paths" do
  79. @checker.receive([@event])
  80. Event.last.payload[:message].should == "I saw 'a2b' from Joe"
  81. end
  82. it "handles numerical comparisons" do
  83. @event.payload[:foo]["bar"][:baz] = "5"
  84. @checker.options[:rules].first[:value] = 6
  85. @checker.options[:rules].first[:type] = "field<value"
  86. lambda {
  87. @checker.receive([@event])
  88. }.should change { Event.count }.by(1)
  89. @checker.options[:rules].first[:value] = 3
  90. lambda {
  91. @checker.receive([@event])
  92. }.should_not change { Event.count }
  93. end
  94. it "handles exact comparisons" do
  95. @event.payload[:foo]["bar"][:baz] = "hello world"
  96. @checker.options[:rules].first[:type] = "field==value"
  97. @checker.options[:rules].first[:value] = "hello there"
  98. lambda {
  99. @checker.receive([@event])
  100. }.should_not change { Event.count }
  101. @checker.options[:rules].first[:value] = "hello world"
  102. lambda {
  103. @checker.receive([@event])
  104. }.should change { Event.count }.by(1)
  105. end
  106. it "handles negated comparisons" do
  107. @event.payload[:foo]["bar"][:baz] = "hello world"
  108. @checker.options[:rules].first[:type] = "field!=value"
  109. @checker.options[:rules].first[:value] = "hello world"
  110. lambda {
  111. @checker.receive([@event])
  112. }.should_not change { Event.count }
  113. @checker.options[:rules].first[:value] = "hello there"
  114. lambda {
  115. @checker.receive([@event])
  116. }.should change { Event.count }.by(1)
  117. end
  118. it "does fine without dots in the path" do
  119. @event.payload = { :hello => "world" }
  120. @checker.options[:rules].first[:type] = "field==value"
  121. @checker.options[:rules].first[:path] = "hello"
  122. @checker.options[:rules].first[:value] = "world"
  123. lambda {
  124. @checker.receive([@event])
  125. }.should change { Event.count }.by(1)
  126. @checker.options[:rules].first[:path] = "foo"
  127. lambda {
  128. @checker.receive([@event])
  129. }.should_not change { Event.count }
  130. @checker.options[:rules].first[:value] = "hi"
  131. lambda {
  132. @checker.receive([@event])
  133. }.should_not change { Event.count }
  134. end
  135. it "handles multiple events" do
  136. event2 = Event.new
  137. event2.agent = agents(:bob_weather_agent)
  138. event2.payload = { :foo => { "bar" => { :baz => "a2b" }}}
  139. event3 = Event.new
  140. event3.agent = agents(:bob_weather_agent)
  141. event3.payload = { :foo => { "bar" => { :baz => "a222b" }}}
  142. lambda {
  143. @checker.receive([@event, event2, event3])
  144. }.should change { Event.count }.by(2)
  145. end
  146. it "handles ANDing rules together" do
  147. @checker.options[:rules] << {
  148. :type => "field>=value",
  149. :value => "4",
  150. :path => "foo.bing"
  151. }
  152. @event.payload[:foo]["bing"] = "5"
  153. lambda {
  154. @checker.receive([@event])
  155. }.should change { Event.count }.by(1)
  156. @checker.options[:rules].last[:value] = 6
  157. lambda {
  158. @checker.receive([@event])
  159. }.should_not change { Event.count }
  160. end
  161. end
  162. end