scheduler_agent_spec.rb 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. require 'spec_helper'
  2. describe Agents::SchedulerAgent do
  3. before do
  4. @agent = Agents::SchedulerAgent.new(name: 'Example', options: { 'schedule' => '0 * * * *' })
  5. @agent.user = users(:bob)
  6. @agent.save
  7. end
  8. describe "validation" do
  9. it "should validate action" do
  10. ['run', 'enable', 'disable', '', nil].each { |action|
  11. @agent.options['action'] = action
  12. expect(@agent).to be_valid
  13. }
  14. ['delete', 1, true].each { |action|
  15. @agent.options['action'] = action
  16. expect(@agent).not_to be_valid
  17. }
  18. end
  19. it "should validate schedule" do
  20. expect(@agent).to be_valid
  21. @agent.options.delete('schedule')
  22. expect(@agent).not_to be_valid
  23. @agent.options['schedule'] = nil
  24. expect(@agent).not_to be_valid
  25. @agent.options['schedule'] = ''
  26. expect(@agent).not_to be_valid
  27. @agent.options['schedule'] = '0'
  28. expect(@agent).not_to be_valid
  29. @agent.options['schedule'] = '*/15 * * * * * *'
  30. expect(@agent).not_to be_valid
  31. @agent.options['schedule'] = '*/1 * * * *'
  32. expect(@agent).to be_valid
  33. @agent.options['schedule'] = '*/1 * * *'
  34. expect(@agent).not_to be_valid
  35. stub(@agent).second_precision_enabled { true }
  36. @agent.options['schedule'] = '*/15 * * * * *'
  37. expect(@agent).to be_valid
  38. stub(@agent).second_precision_enabled { false }
  39. @agent.options['schedule'] = '*/10 * * * * *'
  40. expect(@agent).not_to be_valid
  41. @agent.options['schedule'] = '5/30 * * * * *'
  42. expect(@agent).not_to be_valid
  43. @agent.options['schedule'] = '*/15 * * * * *'
  44. expect(@agent).to be_valid
  45. @agent.options['schedule'] = '15,45 * * * * *'
  46. expect(@agent).to be_valid
  47. @agent.options['schedule'] = '0 * * * * *'
  48. expect(@agent).to be_valid
  49. end
  50. end
  51. describe 'control_action' do
  52. it "should be one of the supported values" do
  53. ['run', '', nil].each { |action|
  54. @agent.options['action'] = action
  55. expect(@agent.control_action).to eq('run')
  56. }
  57. ['enable', 'disable'].each { |action|
  58. @agent.options['action'] = action
  59. expect(@agent.control_action).to eq(action)
  60. }
  61. end
  62. it "cannot be 'run' if any of the control targets cannot be scheduled" do
  63. expect(@agent.control_action).to eq('run')
  64. @agent.control_targets = [agents(:bob_rain_notifier_agent)]
  65. expect(@agent).not_to be_valid
  66. end
  67. it "can be 'enable' or 'disable' no matter if control targets can be scheduled or not" do
  68. ['enable', 'disable'].each { |action|
  69. @agent.options['action'] = action
  70. @agent.control_targets = [agents(:bob_rain_notifier_agent)]
  71. expect(@agent).to be_valid
  72. }
  73. end
  74. end
  75. describe "save" do
  76. it "should delete memory['scheduled_at'] if and only if options is changed" do
  77. time = Time.now.to_i
  78. @agent.memory['scheduled_at'] = time
  79. @agent.save
  80. expect(@agent.memory['scheduled_at']).to eq(time)
  81. @agent.memory['scheduled_at'] = time
  82. # Currently @agent.options[]= is not detected
  83. @agent.options = { 'schedule' => '*/5 * * * *' }
  84. @agent.save
  85. expect(@agent.memory['scheduled_at']).to be_nil
  86. end
  87. end
  88. describe "check!" do
  89. it "should control targets" do
  90. control_targets = [agents(:bob_website_agent), agents(:bob_weather_agent)]
  91. @agent.control_targets = control_targets
  92. @agent.save!
  93. control_target_ids = control_targets.map(&:id)
  94. stub(Agent).async_check(anything) { |id|
  95. control_target_ids.delete(id)
  96. }
  97. @agent.check!
  98. expect(control_target_ids).to be_empty
  99. @agent.options['action'] = 'disable'
  100. @agent.save!
  101. @agent.check!
  102. control_targets.all? { |control_target| control_target.disabled? }
  103. @agent.options['action'] = 'enable'
  104. @agent.save!
  105. @agent.check!
  106. control_targets.all? { |control_target| !control_target.disabled? }
  107. end
  108. end
  109. end