agent_controller_concern.rb 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. require 'spec_helper'
  2. shared_examples_for AgentControllerConcern do
  3. describe "preconditions" do
  4. it "must be satisfied for these shared examples" do
  5. expect(agent.user).to eq(users(:bob))
  6. expect(agent.control_action).to eq('run')
  7. end
  8. end
  9. describe "validation" do
  10. describe "of action" do
  11. it "should allow certain values" do
  12. ['run', 'enable', 'disable', '{{ action }}'].each { |action|
  13. agent.options['action'] = action
  14. expect(agent).to be_valid
  15. }
  16. end
  17. it "should disallow obviously bad values" do
  18. ['delete', nil, 1, true].each { |action|
  19. agent.options['action'] = action
  20. expect(agent).not_to be_valid
  21. }
  22. end
  23. it "should accept 'run' if all target agents are schedulable" do
  24. agent.control_targets = [agents(:bob_website_agent)]
  25. expect(agent).to be_valid
  26. end
  27. it "should reject 'run' if targets include an unschedulable agent" do
  28. agent.control_targets = [agents(:bob_rain_notifier_agent)]
  29. expect(agent).not_to be_valid
  30. end
  31. it "should not reject 'enable' or 'disable' no matter if targets include an unschedulable agent" do
  32. ['enable', 'disable'].each { |action|
  33. agent.options['action'] = action
  34. agent.control_targets = [agents(:bob_rain_notifier_agent)]
  35. expect(agent).to be_valid
  36. }
  37. end
  38. end
  39. end
  40. describe 'control_action' do
  41. it "returns options['action']" do
  42. expect(agent.control_action).to eq('run')
  43. ['run', 'enable', 'disable'].each { |action|
  44. agent.options['action'] = action
  45. expect(agent.control_action).to eq(action)
  46. }
  47. end
  48. it "returns the result of interpolation" do
  49. expect(agent.control_action).to eq('run')
  50. agent.options['action'] = '{{ "enable" }}'
  51. expect(agent.control_action).to eq('enable')
  52. end
  53. end
  54. describe "control!" do
  55. before do
  56. agent.control_targets = [agents(:bob_website_agent), agents(:bob_weather_agent)]
  57. agent.save!
  58. end
  59. it "should run targets" do
  60. control_target_ids = agent.control_targets.map(&:id)
  61. stub(Agent).async_check(anything) { |id|
  62. control_target_ids.delete(id)
  63. }
  64. agent.control!
  65. expect(control_target_ids).to be_empty
  66. end
  67. it "should not run disabled targets" do
  68. control_target_ids = agent.control_targets.map(&:id)
  69. stub(Agent).async_check(anything) { |id|
  70. control_target_ids.delete(id)
  71. }
  72. agent.control_targets.last.update!(disabled: true)
  73. agent.control!
  74. expect(control_target_ids).to eq [agent.control_targets.last.id]
  75. end
  76. it "should enable targets" do
  77. agent.options['action'] = 'disable'
  78. agent.save!
  79. agent.control_targets.first.update!(disabled: true)
  80. agent.control!
  81. expect(agent.control_targets.reload).to all(be_disabled)
  82. end
  83. it "should disable targets" do
  84. agent.options['action'] = 'enable'
  85. agent.save!
  86. agent.control_targets.first.update!(disabled: true)
  87. agent.control!
  88. expect(agent.control_targets.reload).to all(satisfy { |a| !a.disabled? })
  89. end
  90. end
  91. end