scheduler_agent_spec.rb 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. require 'rails_helper'
  2. describe Agents::SchedulerAgent do
  3. let(:valid_params) {
  4. {
  5. name: 'Example',
  6. options: {
  7. 'action' => 'run',
  8. 'schedule' => '0 * * * *'
  9. },
  10. }
  11. }
  12. let(:agent) {
  13. described_class.create!(valid_params) { |agent|
  14. agent.user = users(:bob)
  15. }
  16. }
  17. it_behaves_like AgentControllerConcern
  18. describe "validation" do
  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. allow(agent).to receive(:second_precision_enabled) { true }
  36. agent.options['schedule'] = '*/15 * * * * *'
  37. expect(agent).to be_valid
  38. allow(agent).to receive(: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 "save" do
  52. it "should delete memory['scheduled_at'] if and only if options is changed" do
  53. time = Time.now.to_i
  54. agent.memory['scheduled_at'] = time
  55. agent.save
  56. expect(agent.memory['scheduled_at']).to eq(time)
  57. agent.memory['scheduled_at'] = time
  58. # Currently agent.options[]= is not detected
  59. agent.options = {
  60. 'action' => 'run',
  61. 'schedule' => '*/5 * * * *'
  62. }
  63. agent.save
  64. expect(agent.memory['scheduled_at']).to be_nil
  65. end
  66. end
  67. end