shell_command_agent_spec.rb 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. require 'spec_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. @checker = Agents::ShellCommandAgent.new(:name => "somename", :options => @valid_params)
  11. @checker.user = users(:jane)
  12. @checker.save!
  13. @event = Event.new
  14. @event.agent = agents(:jane_weather_agent)
  15. @event.payload = {
  16. :cmd => "ls"
  17. }
  18. @event.save!
  19. stub(Agents::ShellCommandAgent).should_run? { true }
  20. end
  21. describe "validation" do
  22. before do
  23. expect(@checker).to be_valid
  24. end
  25. it "should validate presence of necessary fields" do
  26. @checker.options[:command] = nil
  27. expect(@checker).not_to be_valid
  28. end
  29. it "should validate path" do
  30. @checker.options[:path] = 'notarealpath/itreallyisnt'
  31. expect(@checker).not_to be_valid
  32. end
  33. it "should validate path" do
  34. @checker.options[:path] = '/'
  35. expect(@checker).to be_valid
  36. end
  37. end
  38. describe "#working?" do
  39. it "generating events as scheduled" do
  40. stub(@checker).run_command(@valid_path, 'pwd') { ["fake pwd output", "", 0] }
  41. expect(@checker).not_to be_working
  42. @checker.check
  43. expect(@checker.reload).to be_working
  44. three_days_from_now = 3.days.from_now
  45. stub(Time).now { three_days_from_now }
  46. expect(@checker).not_to be_working
  47. end
  48. end
  49. describe "#check" do
  50. before do
  51. stub(@checker).run_command(@valid_path, 'pwd') { ["fake pwd output", "", 0] }
  52. end
  53. it "should create an event when checking" do
  54. expect { @checker.check }.to change { Event.count }.by(1)
  55. expect(Event.last.payload[:path]).to eq(@valid_path)
  56. expect(Event.last.payload[:command]).to eq('pwd')
  57. expect(Event.last.payload[:output]).to eq("fake pwd output")
  58. end
  59. it "does not run when should_run? is false" do
  60. stub(Agents::ShellCommandAgent).should_run? { false }
  61. expect { @checker.check }.not_to change { Event.count }
  62. end
  63. end
  64. describe "#receive" do
  65. before do
  66. stub(@checker).run_command(@valid_path, @event.payload[:cmd]) { ["fake ls output", "", 0] }
  67. end
  68. it "creates events" do
  69. @checker.options[:command] = "{{cmd}}"
  70. @checker.receive([@event])
  71. expect(Event.last.payload[:path]).to eq(@valid_path)
  72. expect(Event.last.payload[:command]).to eq(@event.payload[:cmd])
  73. expect(Event.last.payload[:output]).to eq("fake ls output")
  74. end
  75. it "does not run when should_run? is false" do
  76. stub(Agents::ShellCommandAgent).should_run? { false }
  77. expect {
  78. @checker.receive([@event])
  79. }.not_to change { Event.count }
  80. end
  81. end
  82. end