shell_command_agent_spec.rb 6.9 KB

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