trigger_agent_spec.rb 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 # no events have ever been 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 "puts can extract values into the message based on paths" do
  64. @checker.receive([@event])
  65. Event.last.payload[:message].should == "I saw 'a2b' from Joe"
  66. end
  67. it "handles numerical comparisons" do
  68. @event.payload[:foo]["bar"][:baz] = "5"
  69. @checker.options[:rules].first[:value] = 6
  70. @checker.options[:rules].first[:type] = "field<value"
  71. lambda {
  72. @checker.receive([@event])
  73. }.should change { Event.count }.by(1)
  74. @checker.options[:rules].first[:value] = 3
  75. lambda {
  76. @checker.receive([@event])
  77. }.should_not change { Event.count }
  78. end
  79. it "handles exact comparisons" do
  80. @event.payload[:foo]["bar"][:baz] = "hello world"
  81. @checker.options[:rules].first[:type] = "field==value"
  82. @checker.options[:rules].first[:value] = "hello there"
  83. lambda {
  84. @checker.receive([@event])
  85. }.should_not change { Event.count }
  86. @checker.options[:rules].first[:value] = "hello world"
  87. lambda {
  88. @checker.receive([@event])
  89. }.should change { Event.count }.by(1)
  90. end
  91. it "does fine without dots in the path" do
  92. @event.payload = { :hello => "world" }
  93. @checker.options[:rules].first[:type] = "field==value"
  94. @checker.options[:rules].first[:path] = "hello"
  95. @checker.options[:rules].first[:value] = "world"
  96. lambda {
  97. @checker.receive([@event])
  98. }.should change { Event.count }.by(1)
  99. @checker.options[:rules].first[:path] = "foo"
  100. lambda {
  101. @checker.receive([@event])
  102. }.should_not change { Event.count }
  103. @checker.options[:rules].first[:value] = "hi"
  104. lambda {
  105. @checker.receive([@event])
  106. }.should_not change { Event.count }
  107. end
  108. it "handles multiple events" do
  109. event2 = Event.new
  110. event2.agent = agents(:bob_weather_agent)
  111. event2.payload = { :foo => { "bar" => { :baz => "a2b" }}}
  112. event3 = Event.new
  113. event3.agent = agents(:bob_weather_agent)
  114. event3.payload = { :foo => { "bar" => { :baz => "a222b" }}}
  115. lambda {
  116. @checker.receive([@event, event2, event3])
  117. }.should change { Event.count }.by(2)
  118. end
  119. it "handles ANDing rules together" do
  120. @checker.options[:rules] << {
  121. :type => "field>=value",
  122. :value => "4",
  123. :path => "foo.bing"
  124. }
  125. @event.payload[:foo]["bing"] = "5"
  126. lambda {
  127. @checker.receive([@event])
  128. }.should change { Event.count }.by(1)
  129. @checker.options[:rules].last[:value] = 6
  130. lambda {
  131. @checker.receive([@event])
  132. }.should_not change { Event.count }
  133. end
  134. end
  135. end