foreman-tasks 0.8.6 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (87) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +35 -0
  3. data/.rubocop_todo.yml +138 -0
  4. data/app/controllers/foreman_tasks/api/recurring_logics_controller.rb +3 -4
  5. data/app/controllers/foreman_tasks/api/tasks_controller.rb +56 -72
  6. data/app/controllers/foreman_tasks/concerns/hosts_controller_extension.rb +2 -4
  7. data/app/controllers/foreman_tasks/recurring_logics_controller.rb +2 -5
  8. data/app/controllers/foreman_tasks/tasks_controller.rb +7 -8
  9. data/app/helpers/foreman_tasks/foreman_tasks_helper.rb +44 -46
  10. data/app/helpers/foreman_tasks/tasks_helper.rb +1 -1
  11. data/app/lib/actions/action_with_sub_plans.rb +6 -8
  12. data/app/lib/actions/base.rb +6 -7
  13. data/app/lib/actions/bulk_action.rb +13 -9
  14. data/app/lib/actions/entry_action.rb +1 -3
  15. data/app/lib/actions/foreman/host/import_facts.rb +2 -5
  16. data/app/lib/actions/foreman/puppetclass/import.rb +1 -1
  17. data/app/lib/actions/helpers/args_serialization.rb +0 -1
  18. data/app/lib/actions/helpers/humanizer.rb +16 -21
  19. data/app/lib/actions/helpers/with_continuous_output.rb +0 -1
  20. data/app/lib/actions/helpers/with_delegated_action.rb +2 -2
  21. data/app/lib/actions/middleware/inherit_task_groups.rb +3 -5
  22. data/app/lib/actions/middleware/keep_current_user.rb +0 -3
  23. data/app/lib/actions/middleware/recurring_logic.rb +0 -1
  24. data/app/lib/actions/proxy_action.rb +8 -8
  25. data/app/lib/actions/serializers/active_record_serializer.rb +0 -3
  26. data/app/lib/proxy_api/foreman_dynflow/dynflow_proxy.rb +3 -3
  27. data/app/models/foreman_tasks/concerns/action_subject.rb +4 -6
  28. data/app/models/foreman_tasks/concerns/action_triggering.rb +20 -33
  29. data/app/models/foreman_tasks/concerns/host_action_subject.rb +5 -5
  30. data/app/models/foreman_tasks/lock.rb +29 -37
  31. data/app/models/foreman_tasks/recurring_logic.rb +23 -24
  32. data/app/models/foreman_tasks/task.rb +65 -39
  33. data/app/models/foreman_tasks/task/dynflow_task.rb +23 -24
  34. data/app/models/foreman_tasks/task/status_explicator.rb +3 -3
  35. data/app/models/foreman_tasks/task/summarizer.rb +3 -3
  36. data/app/models/foreman_tasks/task_group.rb +0 -2
  37. data/app/models/foreman_tasks/task_group_member.rb +0 -2
  38. data/app/models/foreman_tasks/task_groups/recurring_logic_task_group.rb +1 -4
  39. data/app/models/foreman_tasks/triggering.rb +19 -19
  40. data/app/models/setting/foreman_tasks.rb +8 -11
  41. data/app/services/foreman_tasks/proxy_selector.rb +4 -5
  42. data/app/views/foreman_tasks/tasks/_details.html.erb +1 -1
  43. data/bin/dynflow-executor +1 -1
  44. data/bin/foreman-tasks +1 -1
  45. data/config/routes.rb +1 -1
  46. data/db/migrate/20150814204140_add_task_type_value_index.rb +1 -1
  47. data/db/migrate/20160924213030_change_tasks_widget_names.rb +8 -8
  48. data/db/seeds.d/61-foreman_tasks_bookmarks.rb +3 -3
  49. data/deploy/foreman-tasks.sysconfig +6 -0
  50. data/extra/dynflow-debug.sh +12 -0
  51. data/foreman-tasks.gemspec +1 -1
  52. data/lib/foreman_tasks.rb +3 -3
  53. data/lib/foreman_tasks/authorizer_ext.rb +1 -1
  54. data/lib/foreman_tasks/cleaner.rb +14 -16
  55. data/lib/foreman_tasks/dynflow.rb +11 -9
  56. data/lib/foreman_tasks/dynflow/configuration.rb +8 -10
  57. data/lib/foreman_tasks/dynflow/console_authorizer.rb +4 -5
  58. data/lib/foreman_tasks/dynflow/daemon.rb +17 -19
  59. data/lib/foreman_tasks/dynflow/persistence.rb +5 -8
  60. data/lib/foreman_tasks/engine.rb +30 -31
  61. data/lib/foreman_tasks/task_error.rb +1 -3
  62. data/lib/foreman_tasks/tasks/cleanup.rake +7 -19
  63. data/lib/foreman_tasks/tasks/dynflow.rake +1 -1
  64. data/lib/foreman_tasks/tasks/export_tasks.rake +51 -59
  65. data/lib/foreman_tasks/test_extensions.rb +1 -1
  66. data/lib/foreman_tasks/version.rb +1 -1
  67. data/lib/tasks/gettext.rake +10 -7
  68. data/locale/action_names.rb +3 -6
  69. data/locale/en/foreman_tasks.po +189 -177
  70. data/locale/foreman_tasks.pot +177 -137
  71. data/test/controllers/api/recurring_logics_controller_test.rb +3 -5
  72. data/test/controllers/api/tasks_controller_test.rb +5 -7
  73. data/test/factories/task_factory.rb +8 -8
  74. data/test/factories/triggering_factory.rb +2 -3
  75. data/test/helpers/foreman_tasks/tasks_helper_test.rb +11 -11
  76. data/test/support/dummy_proxy_action.rb +3 -4
  77. data/test/unit/actions/action_with_sub_plans_test.rb +5 -6
  78. data/test/unit/actions/proxy_action_test.rb +5 -8
  79. data/test/unit/cleaner_test.rb +11 -12
  80. data/test/unit/dynflow_console_authorizer_test.rb +4 -4
  81. data/test/unit/proxy_selector_test.rb +3 -3
  82. data/test/unit/recurring_logic_test.rb +19 -17
  83. data/test/unit/task_groups_test.rb +3 -4
  84. data/test/unit/task_test.rb +72 -5
  85. data/test/unit/triggering_test.rb +0 -1
  86. metadata +7 -6
  87. data/app/controllers/foreman_tasks/concerns/environments_extension.rb +0 -24
@@ -1,4 +1,4 @@
1
- require "foreman_tasks_test_helper"
1
+ require 'foreman_tasks_test_helper'
2
2
 
3
3
  module ForemanRecurringLogic
4
4
  class Api::RecurringLogicControllerTest < ActionController::TestCase
@@ -22,7 +22,7 @@ module ForemanRecurringLogic
22
22
 
23
23
  describe 'GET /api/recurring_logics/:id' do
24
24
  it 'searches for recurring logic' do
25
- get(:show, :id => @recurring_logic.id)
25
+ get :show, :id => @recurring_logic.id
26
26
  assert_response :success
27
27
  assert_template 'api/recurring_logics/show'
28
28
  end
@@ -30,7 +30,7 @@ module ForemanRecurringLogic
30
30
 
31
31
  describe 'POST /api/recurring_logics/:id/cancel' do
32
32
  it 'cancels recurring logic' do
33
- post(:cancel, :id => @recurring_logic.id)
33
+ post :cancel, :id => @recurring_logic.id
34
34
  assert_response :success
35
35
  @recurring_logic.reload
36
36
  assert @recurring_logic.state == 'cancelled'
@@ -39,5 +39,3 @@ module ForemanRecurringLogic
39
39
  end
40
40
  end
41
41
  end
42
-
43
-
@@ -1,4 +1,4 @@
1
- require "foreman_tasks_test_helper"
1
+ require 'foreman_tasks_test_helper'
2
2
 
3
3
  module ForemanTasks
4
4
  class Api::TasksControllerTest < ActionController::TestCase
@@ -14,7 +14,7 @@ module ForemanTasks
14
14
  describe 'GET /api/tasks/show' do
15
15
  it 'searches for task' do
16
16
  task = FactoryGirl.create(:dynflow_task, :user_create_task)
17
- get(:show, :id => task.id)
17
+ get :show, :id => task.id
18
18
  assert_response :success
19
19
  assert_template 'api/tasks/show'
20
20
  end
@@ -37,18 +37,16 @@ module ForemanTasks
37
37
  task.result.must_equal 'pending'
38
38
 
39
39
  callback = Support::DummyProxyAction.proxy.log[:trigger_task].first[1][:callback]
40
- post(:callback, 'callback' => callback, 'data' => {'result' => 'success'})
40
+ post :callback, 'callback' => callback, 'data' => { 'result' => 'success' }
41
41
  triggered.finished.wait(5)
42
42
 
43
43
  task.reload
44
44
  task.state.must_equal 'stopped'
45
45
  task.result.must_equal 'success'
46
- task.main_action.output['proxy_task_id'].must_equal "123"
47
- task.main_action.output['proxy_output'].must_equal({ 'result' => 'success' })
46
+ task.main_action.output['proxy_task_id'].must_equal '123'
47
+ task.main_action.output['proxy_output'].must_equal('result' => 'success')
48
48
  end
49
49
  end
50
50
  end
51
51
  end
52
52
  end
53
-
54
-
@@ -14,12 +14,12 @@ FactoryGirl.define do
14
14
  end
15
15
 
16
16
  factory :dynflow_task, :class => ForemanTasks::Task::DynflowTask do
17
- label "Support::DummyDynflowAction"
18
- type "ForemanTasks::Task::DynflowTask"
19
- started_at "2014-10-01 11:15:55"
20
- ended_at "2014-10-01 11:15:57"
21
- state "stopped"
22
- result "success"
17
+ label 'Support::DummyDynflowAction'
18
+ type 'ForemanTasks::Task::DynflowTask'
19
+ started_at '2014-10-01 11:15:55'
20
+ ended_at '2014-10-01 11:15:57'
21
+ state 'stopped'
22
+ result 'success'
23
23
  parent_task_id nil
24
24
 
25
25
  transient do
@@ -37,11 +37,11 @@ FactoryGirl.define do
37
37
  end
38
38
 
39
39
  trait :user_create_task do
40
- label "Actions::User::Create"
40
+ label 'Actions::User::Create'
41
41
  end
42
42
 
43
43
  trait :product_create_task do
44
- label "Actions::Katello::Product::Create"
44
+ label 'Actions::Katello::Product::Create'
45
45
  end
46
46
 
47
47
  trait :inconsistent_dynflow_task do
@@ -3,7 +3,7 @@ FactoryGirl.define do
3
3
  mode :immediate
4
4
 
5
5
  trait :future do
6
- time = Time.now
6
+ time = Time.zone.now
7
7
  mode :future
8
8
  start_at time
9
9
  start_at_raw(time.strftime(ForemanTasks::Triggering::TIME_FORMAT))
@@ -18,8 +18,7 @@ FactoryGirl.define do
18
18
 
19
19
  trait :end_time_limited do
20
20
  end_time_limited true
21
- end_time(Time.now + 60)
21
+ end_time(Time.zone.now + 60)
22
22
  end
23
23
  end
24
-
25
24
  end
@@ -1,13 +1,13 @@
1
- require "foreman_tasks_test_helper"
1
+ require 'foreman_tasks_test_helper'
2
2
 
3
3
  module ForemanTasks
4
4
  class TasksHelperTest < ActionView::TestCase
5
5
  describe 'when formatting simple input' do
6
6
  before do
7
7
  @task = FactoryGirl.build(:dynflow_task, :user_create_task)
8
- humanized = { :humanized_name => "Create", :humanized_input => [[:user, {:text => "user 'Anonymous Admin'", :link => nil}]] }
8
+ humanized = { :humanized_name => 'Create', :humanized_input => [[:user, { :text => "user 'Anonymous Admin'", :link => nil }]] }
9
9
  @task.instance_variable_set('@humanized_cache', humanized)
10
- @task.stubs(:input).returns({"user"=>{"id"=>1, "name"=>"Anonymous Admin"}, "locale"=>"en"})
10
+ @task.stubs(:input).returns('user' => { 'id' => 1, 'name' => 'Anonymous Admin' }, 'locale' => 'en')
11
11
  end
12
12
 
13
13
  it 'formats the task input properly' do
@@ -16,21 +16,21 @@ module ForemanTasks
16
16
  end
17
17
 
18
18
  it 'displays the dash if task is nil' do
19
- format_task_input(nil, true).must_equal('-')
19
+ format_task_input(nil, true).must_equal('-')
20
20
  end
21
21
  end
22
22
 
23
23
  describe 'when formatting input' do
24
24
  before do
25
25
  @task = FactoryGirl.build(:dynflow_task, :product_create_task)
26
- humanized = { :humanized_name => "Create",
27
- :humanized_input => [[:product, { :text => "product 'product-2'", :link => "#/products/3/info"}], [:organization, { :text => "organization 'test-0'", :link => "/organizations/3/edit" }]]}
26
+ humanized = { :humanized_name => 'Create',
27
+ :humanized_input => [[:product, { :text => "product 'product-2'", :link => '#/products/3/info' }], [:organization, { :text => "organization 'test-0'", :link => '/organizations/3/edit' }]] }
28
28
  @task.instance_variable_set('@humanized_cache', humanized)
29
- input = {"product"=>{"id"=>3, "name"=>"product-2", "label"=>"product-2", "cp_id"=>nil},
30
- "provider"=>{"id"=>3, "name"=>"Anonymous"},
31
- "organization"=>{"id"=>3, "name"=>"test-0", "label"=>"test-0"},
32
- "cp_id"=>"1412251033866",
33
- "locale"=>"en"}
29
+ input = { 'product' => { 'id' => 3, 'name' => 'product-2', 'label' => 'product-2', 'cp_id' => nil },
30
+ 'provider' => { 'id' => 3, 'name' => 'Anonymous' },
31
+ 'organization' => { 'id' => 3, 'name' => 'test-0', 'label' => 'test-0' },
32
+ 'cp_id' => '1412251033866',
33
+ 'locale' => 'en' }
34
34
  @task.stubs(:input).returns(input)
35
35
  end
36
36
 
@@ -1,6 +1,5 @@
1
1
  module Support
2
2
  class DummyProxyAction < Actions::ProxyAction
3
-
4
3
  class DummyProxy
5
4
  attr_reader :log, :task_triggered
6
5
 
@@ -12,7 +11,7 @@ module Support
12
11
  def trigger_task(*args)
13
12
  @log[:trigger_task] << args
14
13
  @task_triggered.success(true)
15
- {"task_id" => "123"}
14
+ { 'task_id' => '123' }
16
15
  end
17
16
 
18
17
  def cancel_task(*args)
@@ -40,8 +39,8 @@ module Support
40
39
  ForemanTasks::Task::DynflowTask.new.tap { |task| task.id = '123' }
41
40
  end
42
41
 
43
- def self.proxy
44
- @proxy
42
+ class << self
43
+ attr_reader :proxy
45
44
  end
46
45
 
47
46
  def self.reset
@@ -1,7 +1,7 @@
1
- require "foreman_tasks_test_helper"
1
+ require 'foreman_tasks_test_helper'
2
2
 
3
3
  module ForemanTasks
4
- class ActionWithSubPlansTest < ActiveSupport::TestCase
4
+ class ActionWithSubPlansTest < ActiveSupport::TestCase
5
5
  self.use_transactional_fixtures = false
6
6
 
7
7
  before do
@@ -30,8 +30,8 @@ module ForemanTasks
30
30
  action_subject(user)
31
31
  plan_self(user_id: user.id)
32
32
  end
33
- def run
34
- end
33
+
34
+ def run; end
35
35
  end
36
36
 
37
37
  describe Actions::ActionWithSubPlans do
@@ -43,7 +43,7 @@ module ForemanTasks
43
43
  ForemanTasks::Task.where(:external_id => triggered.id).first
44
44
  end
45
45
 
46
- specify "the sub-plan stores the information about its parent" do
46
+ specify 'the sub-plan stores the information about its parent' do
47
47
  task.sub_tasks.size.must_equal 1
48
48
  task.sub_tasks.first.label.must_equal ChildAction.name
49
49
  end
@@ -53,6 +53,5 @@ module ForemanTasks
53
53
  assert(child_task.locks.any? { |lock| lock.name == 'write' }, "it's locks don't conflict with parent's")
54
54
  end
55
55
  end
56
-
57
56
  end
58
57
  end
@@ -1,8 +1,7 @@
1
- require "foreman_tasks_test_helper"
1
+ require 'foreman_tasks_test_helper'
2
2
 
3
3
  module ForemanTasks
4
4
  class ProxyActionTest < ActiveSupport::TestCase
5
-
6
5
  describe Actions::ProxyAction do
7
6
  include ::Dynflow::Testing
8
7
 
@@ -23,7 +22,7 @@ module ForemanTasks
23
22
  'connection_options' =>
24
23
  { 'retry_interval' => 15, 'retry_count' => 4, 'timeout' => 60 },
25
24
  'proxy_url' => 'proxy.example.com',
26
- 'proxy_action_name'=>'Proxy::DummyAction',
25
+ 'proxy_action_name' => 'Proxy::DummyAction',
27
26
  'callback' => { 'task_id' => '123', 'step_id' => @action.run_step_id } }]
28
27
  proxy_call.must_equal(expected_call)
29
28
  end
@@ -66,14 +65,14 @@ module ForemanTasks
66
65
 
67
66
  it 'saves the data comming from the proxy to the output and finishes' do
68
67
  action = run_action(@action, ::Actions::ProxyAction::CallbackData.new('result' => 'success'))
69
- action.output[:proxy_output].must_equal({'result' => 'success'})
68
+ action.output[:proxy_output].must_equal('result' => 'success')
70
69
  end
71
70
 
72
71
  it 'handles connection errors' do
73
72
  action = create_and_plan_action(Support::DummyProxyAction,
74
73
  Support::DummyProxyAction.proxy,
75
- 'Proxy::DummyAction',
76
- { :foo => 'bar' })
74
+ 'Proxy::DummyAction',
75
+ :foo => 'bar')
77
76
  run_stubbed_action = lambda do |lambda_action|
78
77
  run_action lambda_action do |block_action|
79
78
  block_action.expects(:trigger_proxy_task).raises(Errno::ECONNREFUSED.new('Connection refused'))
@@ -87,8 +86,6 @@ module ForemanTasks
87
86
  proc { action = run_stubbed_action.call action }.must_raise(Errno::ECONNREFUSED)
88
87
  action.state.must_equal :error
89
88
  end
90
-
91
89
  end
92
-
93
90
  end
94
91
  end
@@ -8,7 +8,7 @@ class TasksTest < ActiveSupport::TestCase
8
8
  tasks_to_delete = [FactoryGirl.create(:dynflow_task, :user_create_task),
9
9
  FactoryGirl.create(:dynflow_task, :user_create_task)]
10
10
  tasks_to_keep = [FactoryGirl.create(:dynflow_task, :user_create_task) do |task|
11
- task.started_at = task.ended_at = Time.now
11
+ task.started_at = task.ended_at = Time.zone.now
12
12
  task.save
13
13
  end,
14
14
  FactoryGirl.create(:dynflow_task, :product_create_task)]
@@ -16,18 +16,18 @@ class TasksTest < ActiveSupport::TestCase
16
16
  ForemanTasks::Task.where(id: tasks_to_delete).must_be_empty
17
17
  ForemanTasks::Task.where(id: tasks_to_keep).order(:id).map(&:id).must_equal tasks_to_keep.map(&:id).sort
18
18
 
19
- ForemanTasks.dynflow.world.persistence.
20
- find_execution_plans(filters: {'uuid' => tasks_to_delete.map(&:external_id)}).size.must_equal 0
19
+ ForemanTasks.dynflow.world.persistence
20
+ .find_execution_plans(filters: { 'uuid' => tasks_to_delete.map(&:external_id) }).size.must_equal 0
21
21
 
22
- ForemanTasks.dynflow.world.persistence.
23
- find_execution_plans(filters: {'uuid' => tasks_to_keep.map(&:external_id)}).size.must_equal tasks_to_keep.size
22
+ ForemanTasks.dynflow.world.persistence
23
+ .find_execution_plans(filters: { 'uuid' => tasks_to_keep.map(&:external_id) }).size.must_equal tasks_to_keep.size
24
24
  end
25
25
 
26
26
  it 'deletes all tasks matching the filter when the time limit is not specified' do
27
27
  cleaner = ForemanTasks::Cleaner.new(:filter => 'label = "Actions::User::Create"')
28
28
  tasks_to_delete = [FactoryGirl.create(:dynflow_task, :user_create_task),
29
29
  FactoryGirl.create(:dynflow_task, :user_create_task) do |task|
30
- task.started_at = task.ended_at = Time.now
30
+ task.started_at = task.ended_at = Time.zone.now
31
31
  task.save
32
32
  end]
33
33
 
@@ -43,7 +43,7 @@ class TasksTest < ActiveSupport::TestCase
43
43
  FactoryGirl.create(:dynflow_task, :product_create_task)]
44
44
 
45
45
  tasks_to_keep = [FactoryGirl.create(:dynflow_task, :user_create_task) do |task|
46
- task.started_at = task.ended_at = Time.now
46
+ task.started_at = task.ended_at = Time.zone.now
47
47
  task.save
48
48
  end]
49
49
  cleaner.delete
@@ -57,18 +57,17 @@ class TasksTest < ActiveSupport::TestCase
57
57
  end
58
58
  end
59
59
 
60
- describe "default behaviour" do
61
- it "searches for the actions that have the cleanup_after defined" do
60
+ describe 'default behaviour' do
61
+ it 'searches for the actions that have the cleanup_after defined' do
62
62
  ForemanTasks::Cleaner.stubs(:cleanup_settings => {})
63
63
  ForemanTasks::Cleaner.actions_with_default_cleanup[ActionWithCleanup].must_equal '15d'
64
64
  end
65
65
 
66
- it "searches for the actions that have the cleanup_after defined" do
66
+ it 'searches for the actions that have the cleanup_after defined' do
67
67
  ForemanTasks::Cleaner.stubs(:cleanup_settings =>
68
- { :actions => [{:name => ActionWithCleanup.name, :after => '5d'}]})
68
+ { :actions => [{ :name => ActionWithCleanup.name, :after => '5d' }] })
69
69
  ForemanTasks::Cleaner.actions_with_default_cleanup[ActionWithCleanup].must_equal '5d'
70
70
  end
71
-
72
71
  end
73
72
  end
74
73
  end
@@ -1,7 +1,7 @@
1
- require "foreman_tasks_test_helper"
1
+ require 'foreman_tasks_test_helper'
2
2
 
3
3
  module ForemanTasks
4
- class DynflowConsoleAuthorizerTest < ActiveSupport::TestCase
4
+ class DynflowConsoleAuthorizerTest < ActiveSupport::TestCase
5
5
  include Rack::Test::Methods
6
6
 
7
7
  before do
@@ -18,8 +18,8 @@ module ForemanTasks
18
18
  def dynflow_console_authorized?(task = nil)
19
19
  dynflow_path = '/'
20
20
  dynflow_path += task.external_id.to_s if task
21
- dynflow_rack_env = { "rack.session" => { "user" => user.id, "expires_at" => Time.now + 100 },
22
- "PATH_INFO" => dynflow_path}.with_indifferent_access
21
+ dynflow_rack_env = { 'rack.session' => { 'user' => user.id, 'expires_at' => Time.zone.now + 100 },
22
+ 'PATH_INFO' => dynflow_path }.with_indifferent_access
23
23
  ForemanTasks::Dynflow::ConsoleAuthorizer.new(dynflow_rack_env).allow?
24
24
  end
25
25
 
@@ -1,4 +1,4 @@
1
- require "foreman_tasks_test_helper"
1
+ require 'foreman_tasks_test_helper'
2
2
 
3
3
  describe ForemanTasks::ProxySelector do
4
4
  let(:proxy_selector) { ForemanTasks::ProxySelector.new }
@@ -10,8 +10,8 @@ describe ForemanTasks::ProxySelector do
10
10
  describe '#select_by_jobs_count' do
11
11
  it 'load balances' do
12
12
  count = 3
13
- ProxyAPI::ForemanDynflow::DynflowProxy.any_instance.expects(:tasks_count).raises.
14
- then.times(count - 1).returns(0)
13
+ ProxyAPI::ForemanDynflow::DynflowProxy.any_instance.expects(:tasks_count).raises
14
+ .then.times(count - 1).returns(0)
15
15
  proxies = FactoryGirl.create_list(:smart_proxy, count)
16
16
 
17
17
  available = proxies.reduce([]) do |found, _|
@@ -1,11 +1,9 @@
1
1
  require 'foreman_tasks_test_helper'
2
2
 
3
3
  class RecurringLogicsTest < ActiveSupport::TestCase
4
-
5
4
  describe 'generating times' do
6
-
7
5
  it 'assembles cronline' do
8
- hash = { }
6
+ hash = {}
9
7
  ForemanTasks::RecurringLogic.assemble_cronline(hash).must_equal '* * * * *'
10
8
  hash.update :minutes => '*'
11
9
  ForemanTasks::RecurringLogic.assemble_cronline(hash).must_equal '* * * * *'
@@ -13,27 +11,31 @@ class RecurringLogicsTest < ActiveSupport::TestCase
13
11
  ForemanTasks::RecurringLogic.assemble_cronline(hash).must_equal '* 0,12 * * *'
14
12
  hash.update :days => '*/2'
15
13
  ForemanTasks::RecurringLogic.assemble_cronline(hash).must_equal '* 0,12 */2 * *'
16
- hash.update :months => '12'
14
+ hash.update :months => '12'
17
15
  ForemanTasks::RecurringLogic.assemble_cronline(hash).must_equal '* 0,12 */2 12 *'
18
16
  hash.update :days_of_week => '1,2,3,4,5,6,7'
19
17
  ForemanTasks::RecurringLogic.assemble_cronline(hash).must_equal '* 0,12 */2 12 1,2,3,4,5,6,7'
20
18
  end
21
19
 
22
20
  it 'generates correct times' do
23
- year, month, day, hour, minute = [2015, 9, 29, 15, 0]
24
- reference_time = Time.new(year, month, day, hour, minute)
21
+ year = 2015
22
+ month = 9
23
+ day = 29
24
+ hour = 15
25
+ minute = 0
26
+ reference_time = Time.utc(year, month, day, hour, minute)
25
27
  parser = ForemanTasks::RecurringLogic.new_from_cronline('* * * * *')
26
- parser.next_occurrence_time(reference_time).must_equal Time.new(year, month, day, hour, minute + 1)
28
+ parser.next_occurrence_time(reference_time).must_equal Time.utc(year, month, day, hour, minute + 1)
27
29
  parser = ForemanTasks::RecurringLogic.new_from_cronline('*/2 * * * *')
28
- parser.next_occurrence_time(reference_time).must_equal Time.new(year, month, day, hour, minute + 2)
30
+ parser.next_occurrence_time(reference_time).must_equal Time.utc(year, month, day, hour, minute + 2)
29
31
  parser = ForemanTasks::RecurringLogic.new_from_cronline('*/2 18,19 * * *')
30
- parser.next_occurrence_time(reference_time).must_equal Time.new(year, month, day, 18)
32
+ parser.next_occurrence_time(reference_time).must_equal Time.utc(year, month, day, 18)
31
33
  parser = ForemanTasks::RecurringLogic.new_from_cronline('*/2 18,19 10 * *')
32
- parser.next_occurrence_time(reference_time).must_equal Time.new(year, month + 1, 10, 18, minute)
34
+ parser.next_occurrence_time(reference_time).must_equal Time.utc(year, month + 1, 10, 18, minute)
33
35
  parser = ForemanTasks::RecurringLogic.new_from_cronline('*/2 18,19 10 11,12 *')
34
- parser.next_occurrence_time(reference_time).must_equal Time.new(year, 11, 10, 18, 0)
36
+ parser.next_occurrence_time(reference_time).must_equal Time.utc(year, 11, 10, 18, 0)
35
37
  parser = ForemanTasks::RecurringLogic.new_from_cronline('* * * * 1')
36
- parser.next_occurrence_time(reference_time).must_equal Time.new(year, month + 1, 5)
38
+ parser.next_occurrence_time(reference_time).must_equal Time.utc(year, month + 1, 5)
37
39
  end
38
40
 
39
41
  it 'creates correct cronline hash' do
@@ -60,7 +62,7 @@ class RecurringLogicsTest < ActiveSupport::TestCase
60
62
  parser.expects(:iteration).twice.returns(5)
61
63
  parser.wont_be :can_continue?
62
64
  parser.max_iteration = nil
63
- time = Time.new(2015, 9, 29, 15, 0)
65
+ time = Time.utc(2015, 9, 29, 15, 0)
64
66
  parser.end_time = time
65
67
  parser.wont_be :can_continue?, time
66
68
  parser.end_time = time + 120
@@ -72,11 +74,11 @@ class RecurringLogicsTest < ActiveSupport::TestCase
72
74
  it 'generates delay options' do
73
75
  parser = ForemanTasks::RecurringLogic.new_from_cronline('* * * * *')
74
76
  parser.stubs(:id).returns(1)
75
- reference_time = Time.new(2015, 9, 29, 15)
77
+ reference_time = Time.utc(2015, 9, 29, 15)
76
78
  expected_hash = { :start_at => reference_time + 60, :start_before => nil, :recurring_logic_id => parser.id }
77
79
  parser.generate_delay_options(reference_time).must_equal expected_hash
78
80
  parser.generate_delay_options(reference_time, 'start_before' => reference_time + 3600)
79
- .must_equal expected_hash.merge(:start_before => reference_time + 3600)
81
+ .must_equal expected_hash.merge(:start_before => reference_time + 3600)
80
82
  end
81
83
 
82
84
  it 'can start' do
@@ -100,7 +102,7 @@ class RecurringLogicsTest < ActiveSupport::TestCase
100
102
  end
101
103
 
102
104
  it 'is invalid when end time in past' do
103
- logic.end_time = (Time.now - 120)
105
+ logic.end_time = (Time.zone.now - 120)
104
106
  logic.wont_be :valid?
105
107
  end
106
108
 
@@ -110,7 +112,7 @@ class RecurringLogicsTest < ActiveSupport::TestCase
110
112
  end
111
113
 
112
114
  it 'is valid when in active state' do
113
- logic.end_time = (Time.now - 120)
115
+ logic.end_time = (Time.zone.now - 120)
114
116
  logic.wont_be :valid?
115
117
  logic.state = 'active'
116
118
  logic.must_be :valid?