agent_controller_concern.rb 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. require 'rails_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. it "should ensure that 'configure_options' exists in options when the action is 'configure'" do
  39. agent.options['action'] = 'configure'
  40. expect(agent).not_to be_valid
  41. agent.options['configure_options'] = {}
  42. expect(agent).not_to be_valid
  43. agent.options['configure_options'] = { 'key' => 'value' }
  44. expect(agent).to be_valid
  45. end
  46. end
  47. end
  48. describe 'control_action' do
  49. it "returns options['action']" do
  50. expect(agent.control_action).to eq('run')
  51. ['run', 'enable', 'disable'].each { |action|
  52. agent.options['action'] = action
  53. expect(agent.control_action).to eq(action)
  54. }
  55. end
  56. it "returns the result of interpolation" do
  57. expect(agent.control_action).to eq('run')
  58. agent.options['action'] = '{{ "enable" }}'
  59. expect(agent.control_action).to eq('enable')
  60. end
  61. end
  62. describe "control!" do
  63. before do
  64. agent.control_targets = [agents(:bob_website_agent), agents(:bob_weather_agent)]
  65. agent.save!
  66. end
  67. it "should run 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!
  73. expect(control_target_ids).to be_empty
  74. end
  75. it "should not run disabled targets" do
  76. control_target_ids = agent.control_targets.map(&:id)
  77. stub(Agent).async_check(anything) { |id|
  78. control_target_ids.delete(id)
  79. }
  80. agent.control_targets.last.update!(disabled: true)
  81. agent.control!
  82. expect(control_target_ids).to eq [agent.control_targets.last.id]
  83. end
  84. it "should enable targets" do
  85. agent.options['action'] = 'disable'
  86. agent.save!
  87. agent.control_targets.first.update!(disabled: true)
  88. agent.control!
  89. expect(agent.control_targets.reload).to all(be_disabled)
  90. end
  91. it "should disable targets" do
  92. agent.options['action'] = 'enable'
  93. agent.save!
  94. agent.control_targets.first.update!(disabled: true)
  95. agent.control!
  96. expect(agent.control_targets.reload).to all(satisfy { |a| !a.disabled? })
  97. end
  98. it "should configure targets" do
  99. agent.options['action'] = 'configure'
  100. agent.options['configure_options'] = { 'url' => 'http://some-new-url.com/{{"something" | upcase}}' }
  101. agent.save!
  102. old_options = agents(:bob_website_agent).options
  103. agent.control!
  104. expect(agent.control_targets.reload).to all(satisfy { |a| a.options['url'] == 'http://some-new-url.com/SOMETHING' })
  105. expect(agents(:bob_website_agent).reload.options).to eq(old_options.merge('url' => 'http://some-new-url.com/SOMETHING'))
  106. end
  107. it "should configure targets with nested objects" do
  108. agent.control_targets = [
  109. agents(:bob_basecamp_agent), # does not support a `template` option, but anyway
  110. agents(:bob_data_output_agent)
  111. ]
  112. agent.options['action'] = 'configure'
  113. agent.options['configure_options'] = {
  114. template: {
  115. item: {
  116. title: "changed"
  117. }
  118. }
  119. }
  120. agent.save!
  121. old_options = agents(:bob_data_output_agent).options
  122. agent.control!
  123. expect(agent.control_targets.reload).to all(satisfy { |a| a.options['template'] && a.options['template']['item'] && (a.options['template']['item']['title'] == 'changed') })
  124. expect(agents(:bob_data_output_agent).reload.options).to eq(old_options.deep_merge(agent.options['configure_options']))
  125. end
  126. end
  127. end