agent_controller_concern.rb 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. allow(Agent).to receive(:async_check).with(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. allow(Agent).to receive(:async_check).with(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 disable targets" do
  85. agent.options['action'] = 'disable'
  86. agent.save!
  87. agent.control_targets.first.update!(disabled: false)
  88. agent.control!
  89. expect(agent.control_targets.reload).to all(be_disabled)
  90. end
  91. it "should enable 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 enable targets and drop pending events" do
  99. agent.options['action'] = 'enable'
  100. agent.options['drop_pending_events'] = 'true'
  101. agent.save!
  102. agent.control_targets.first.update!(disabled: true)
  103. agent.control!
  104. # Find the maximum Event ID, which is what our control_targets should be set to when drop_pending_events is called.
  105. maximum_event_id = Event.maximum(:id)
  106. # Only test Agents which can_receive_events? because those are the only ones who will have had pending events dropped.
  107. agents_that_can_recieve_events = agent.control_targets.to_a.keep_if(&:can_receive_events?)
  108. expect(agents_that_can_recieve_events.collect(&:last_checked_event_id)).to all(eq(maximum_event_id))
  109. # Ensure that the control_targets are not disabled.
  110. expect(agent.control_targets.reload).to all(satisfy { |a| !a.disabled? })
  111. end
  112. it "should configure targets" do
  113. agent.options['action'] = 'configure'
  114. agent.options['configure_options'] = { 'url' => 'http://some-new-url.com/{{"something" | upcase}}' }
  115. agent.save!
  116. old_options = agents(:bob_website_agent).options
  117. agent.control!
  118. expect(agent.control_targets.reload).to all(satisfy { |a| a.options['url'] == 'http://some-new-url.com/SOMETHING' })
  119. expect(agents(:bob_website_agent).reload.options).to eq(old_options.merge('url' => 'http://some-new-url.com/SOMETHING'))
  120. end
  121. it "should configure targets with nested objects" do
  122. agent.control_targets = [
  123. agents(:bob_twitter_user_agent), # does not support a `template` option, but anyway
  124. agents(:bob_data_output_agent)
  125. ]
  126. agent.options['action'] = 'configure'
  127. agent.options['configure_options'] = {
  128. template: {
  129. item: {
  130. title: "changed"
  131. }
  132. }
  133. }
  134. agent.save!
  135. old_options = agents(:bob_data_output_agent).options
  136. agent.control!
  137. expect(agent.control_targets.reload).to all(satisfy { |a| a.options['template'] && a.options['template']['item'] && (a.options['template']['item']['title'] == 'changed') })
  138. expect(agents(:bob_data_output_agent).reload.options).to eq(old_options.deep_merge(agent.options['configure_options']))
  139. end
  140. end
  141. end