shell_command_agent_spec.rb 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. stub(@checker).run_command(@valid_path, 'pwd', nil) { ["fake pwd output", "", 0] }
  63. stub(@checker).run_command(@valid_path, 'empty_output', nil) { ["", "", 0] }
  64. stub(@checker).run_command(@valid_path, 'failure', nil) { ["failed", "error message", 1] }
  65. end
  66. it "should create an event when checking" do
  67. expect { @checker.check }.to change { Event.count }.by(1)
  68. expect(Event.last.payload[:path]).to eq(@valid_path)
  69. expect(Event.last.payload[:command]).to eq('pwd')
  70. expect(Event.last.payload[:output]).to eq("fake pwd output")
  71. end
  72. it "should create an event when checking (unstubbed)" do
  73. expect { @checker2.check }.to change { Event.count }.by(1)
  74. expect(Event.last.payload[:path]).to eq(@valid_path)
  75. expect(Event.last.payload[:command]).to eq([RbConfig.ruby, '-e', 'puts "hello, #{STDIN.eof? ? "world" : STDIN.read.strip}."; STDERR.puts "warning!"'])
  76. expect(Event.last.payload[:output]).to eq('hello, world.')
  77. expect(Event.last.payload[:errors]).to eq('warning!')
  78. end
  79. describe "with suppress_on_empty_output" do
  80. it "should suppress events on empty output" do
  81. @checker.options[:suppress_on_empty_output] = true
  82. @checker.options[:command] = 'empty_output'
  83. expect { @checker.check }.not_to change { Event.count }
  84. end
  85. it "should not suppress events on non-empty output" do
  86. @checker.options[:suppress_on_empty_output] = true
  87. @checker.options[:command] = 'failure'
  88. expect { @checker.check }.to change { Event.count }.by(1)
  89. end
  90. end
  91. describe "with suppress_on_failure" do
  92. it "should suppress events on failure" do
  93. @checker.options[:suppress_on_failure] = true
  94. @checker.options[:command] = 'failure'
  95. expect { @checker.check }.not_to change { Event.count }
  96. end
  97. it "should not suppress events on success" do
  98. @checker.options[:suppress_on_failure] = true
  99. @checker.options[:command] = 'empty_output'
  100. expect { @checker.check }.to change { Event.count }.by(1)
  101. end
  102. end
  103. it "does not run when should_run? is false" do
  104. stub(Agents::ShellCommandAgent).should_run? { false }
  105. expect { @checker.check }.not_to change { Event.count }
  106. end
  107. end
  108. describe "#receive" do
  109. before do
  110. stub(@checker).run_command(@valid_path, @event.payload[:cmd], nil) { ["fake ls output", "", 0] }
  111. end
  112. it "creates events" do
  113. @checker.options[:command] = "{{cmd}}"
  114. @checker.receive([@event])
  115. expect(Event.last.payload[:path]).to eq(@valid_path)
  116. expect(Event.last.payload[:command]).to eq(@event.payload[:cmd])
  117. expect(Event.last.payload[:output]).to eq("fake ls output")
  118. end
  119. it "creates events (unstubbed)" do
  120. @checker2.receive([@event])
  121. expect(Event.last.payload[:path]).to eq(@valid_path)
  122. expect(Event.last.payload[:output]).to eq('hello, Huginn.')
  123. expect(Event.last.payload[:errors]).to eq('warning!')
  124. end
  125. it "does not run when should_run? is false" do
  126. stub(Agents::ShellCommandAgent).should_run? { false }
  127. expect {
  128. @checker.receive([@event])
  129. }.not_to change { Event.count }
  130. end
  131. end
  132. end