shell_command_agent_spec.rb 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. require 'rails_helper'
  2. describe Agents::ShellCommandAgent do
  3. before do
  4. @valid_path = Dir.pwd
  5. @valid_params = {
  6. path: @valid_path,
  7. command: 'pwd',
  8. expected_update_period_in_days: '1',
  9. }
  10. @valid_params2 = {
  11. path: @valid_path,
  12. command: [RbConfig.ruby, '-e', 'puts "hello, #{STDIN.eof? ? "world" : STDIN.read.strip}."; STDERR.puts "warning!"'],
  13. stdin: "{{name}}",
  14. expected_update_period_in_days: '1',
  15. }
  16. @checker = Agents::ShellCommandAgent.new(name: 'somename', options: @valid_params)
  17. @checker.user = users(:jane)
  18. @checker.save!
  19. @checker2 = Agents::ShellCommandAgent.new(name: 'somename2', options: @valid_params2)
  20. @checker2.user = users(:jane)
  21. @checker2.save!
  22. @event = Event.new
  23. @event.agent = agents(:jane_weather_agent)
  24. @event.payload = {
  25. 'name' => 'Huginn',
  26. 'cmd' => 'ls',
  27. }
  28. @event.save!
  29. stub(Agents::ShellCommandAgent).should_run? { true }
  30. end
  31. describe "validation" do
  32. before do
  33. expect(@checker).to be_valid
  34. expect(@checker2).to be_valid
  35. end
  36. it "should validate presence of necessary fields" do
  37. @checker.options[:command] = nil
  38. expect(@checker).not_to be_valid
  39. end
  40. it "should validate path" do
  41. @checker.options[:path] = 'notarealpath/itreallyisnt'
  42. expect(@checker).not_to be_valid
  43. end
  44. it "should validate path" do
  45. @checker.options[:path] = '/'
  46. expect(@checker).to be_valid
  47. end
  48. end
  49. describe "#working?" do
  50. it "generating events as scheduled" do
  51. stub(@checker).run_command(@valid_path, 'pwd', nil, {}) { ["fake pwd output", "", 0] }
  52. expect(@checker).not_to be_working
  53. @checker.check
  54. expect(@checker.reload).to be_working
  55. three_days_from_now = 3.days.from_now
  56. stub(Time).now { three_days_from_now }
  57. expect(@checker).not_to be_working
  58. end
  59. end
  60. describe "#check" do
  61. before do
  62. orig_run_command = @checker.method(:run_command)
  63. stub(@checker).run_command(@valid_path, 'pwd', nil, {}) { ["fake pwd output", "", 0] }
  64. stub(@checker).run_command(@valid_path, 'empty_output', nil, {}) { ["", "", 0] }
  65. stub(@checker).run_command(@valid_path, 'failure', nil, {}) { ["failed", "error message", 1] }
  66. stub(@checker).run_command(@valid_path, 'echo $BUNDLE_GEMFILE', nil, unbundle: true) { orig_run_command.(@valid_path, 'echo $BUNDLE_GEMFILE', nil, unbundle: true) }
  67. [[], [{}], [{ unbundle: false }]].each do |rest|
  68. stub(@checker).run_command(@valid_path, 'echo $BUNDLE_GEMFILE', nil, *rest) { [ENV['BUNDLE_GEMFILE'].to_s, "", 0] }
  69. end
  70. end
  71. it "should create an event when checking" do
  72. expect { @checker.check }.to change { Event.count }.by(1)
  73. expect(Event.last.payload[:path]).to eq(@valid_path)
  74. expect(Event.last.payload[:command]).to eq('pwd')
  75. expect(Event.last.payload[:output]).to eq("fake pwd output")
  76. end
  77. it "should create an event when checking (unstubbed)" do
  78. expect { @checker2.check }.to change { Event.count }.by(1)
  79. expect(Event.last.payload[:path]).to eq(@valid_path)
  80. expect(Event.last.payload[:command]).to eq([RbConfig.ruby, '-e', 'puts "hello, #{STDIN.eof? ? "world" : STDIN.read.strip}."; STDERR.puts "warning!"'])
  81. expect(Event.last.payload[:output]).to eq('hello, world.')
  82. expect(Event.last.payload[:errors]).to eq('warning!')
  83. end
  84. describe "with suppress_on_empty_output" do
  85. it "should suppress events on empty output" do
  86. @checker.options[:suppress_on_empty_output] = true
  87. @checker.options[:command] = 'empty_output'
  88. expect { @checker.check }.not_to change { Event.count }
  89. end
  90. it "should not suppress events on non-empty output" do
  91. @checker.options[:suppress_on_empty_output] = true
  92. @checker.options[:command] = 'failure'
  93. expect { @checker.check }.to change { Event.count }.by(1)
  94. end
  95. end
  96. describe "with suppress_on_failure" do
  97. it "should suppress events on failure" do
  98. @checker.options[:suppress_on_failure] = true
  99. @checker.options[:command] = 'failure'
  100. expect { @checker.check }.not_to change { Event.count }
  101. end
  102. it "should not suppress events on success" do
  103. @checker.options[:suppress_on_failure] = true
  104. @checker.options[:command] = 'empty_output'
  105. expect { @checker.check }.to change { Event.count }.by(1)
  106. end
  107. end
  108. it "does not run when should_run? is false" do
  109. stub(Agents::ShellCommandAgent).should_run? { false }
  110. expect { @checker.check }.not_to change { Event.count }
  111. end
  112. describe "with unbundle" do
  113. before do
  114. @checker.options[:command] = 'echo $BUNDLE_GEMFILE'
  115. if ENV['TRAVIS'] == 'true'
  116. stub.proxy(Bundler).original_env { |env| env.except('BUNDLE_GEMFILE') }
  117. end
  118. end
  119. context "unspecified" do
  120. it "should be run inside of our bundler context" do
  121. expect { @checker.check }.to change { Event.count }.by(1)
  122. expect(Event.last.payload[:output].strip).to eq(ENV['BUNDLE_GEMFILE'])
  123. end
  124. end
  125. context "explicitly set to false" do
  126. before do
  127. @checker.options[:unbundle] = false
  128. end
  129. it "should be run inside of our bundler context" do
  130. expect { @checker.check }.to change { Event.count }.by(1)
  131. expect(Event.last.payload[:output].strip).to eq(ENV['BUNDLE_GEMFILE'])
  132. end
  133. end
  134. context "set to true" do
  135. before do
  136. @checker.options[:unbundle] = true
  137. end
  138. it "should be run outside of our bundler context" do
  139. expect { @checker.check }.to change { Event.count }.by(1)
  140. expect(Event.last.payload[:output].strip).to eq('') # not_to eq(ENV['BUNDLE_GEMFILE']
  141. end
  142. end
  143. end
  144. end
  145. describe "#receive" do
  146. before do
  147. stub(@checker).run_command(@valid_path, @event.payload[:cmd], nil, {}) { ["fake ls output", "", 0] }
  148. end
  149. it "creates events" do
  150. @checker.options[:command] = "{{cmd}}"
  151. @checker.receive([@event])
  152. expect(Event.last.payload[:path]).to eq(@valid_path)
  153. expect(Event.last.payload[:command]).to eq(@event.payload[:cmd])
  154. expect(Event.last.payload[:output]).to eq("fake ls output")
  155. end
  156. it "creates events (unstubbed)" do
  157. @checker2.receive([@event])
  158. expect(Event.last.payload[:path]).to eq(@valid_path)
  159. expect(Event.last.payload[:output]).to eq('hello, Huginn.')
  160. expect(Event.last.payload[:errors]).to eq('warning!')
  161. end
  162. it "does not run when should_run? is false" do
  163. stub(Agents::ShellCommandAgent).should_run? { false }
  164. expect {
  165. @checker.receive([@event])
  166. }.not_to change { Event.count }
  167. end
  168. end
  169. end