diff options
author | Thom May <thom@may.lt> | 2017-11-23 10:18:30 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-11-23 10:18:30 +0000 |
commit | 8382d655b5eb3df354d9399d6c1f07d5ce6cb644 (patch) | |
tree | 620af177e7ba73321754fae5b41d502e1981129d | |
parent | 3eeff76b7d6e5b323a71fcaeb91642fa6e35c40e (diff) | |
parent | 1cfafab4c9b2f65221dfc5615c7dd7d7b51102a4 (diff) | |
download | chef-8382d655b5eb3df354d9399d6c1f07d5ce6cb644.tar.gz |
Merge pull request #6544 from MsysTechnologiesllc/nim/invalid_date_err
[MSYS-688] Fixed invalid date and Invalid starttime error
-rw-r--r-- | lib/chef/provider/windows_task.rb | 19 | ||||
-rw-r--r-- | spec/unit/provider/windows_task_spec.rb | 27 |
2 files changed, 45 insertions, 1 deletions
diff --git a/lib/chef/provider/windows_task.rb b/lib/chef/provider/windows_task.rb index 588a731767..2aad02564c 100644 --- a/lib/chef/provider/windows_task.rb +++ b/lib/chef/provider/windows_task.rb @@ -79,10 +79,19 @@ class Chef Chef::Log.info "#{new_resource} task already exists - nothing to do" return end - # To merge current resource and new resource attributes + # Setting the attributes of new_resource as current_resource. + # This is required to maintain idempotency when the user is not passing start_day and start_time + # because SCHTASK.exe sets them by default as the current time and day. + # So the current_resource will always have start_time and start_days set for an existing task, + # even though the user didn't pass it. So we set the new_resource attributes as the current_resource attributes resource_attributes.each do |attribute| new_resource_attribute = new_resource.send(attribute) current_resource_attribute = current_resource.send(attribute) + # We accept start_day in mm/dd/yyyy format only. Hence while copying the start_day from system to new_resource.start_day, + # we are converting from system date format to mm/dd/yyyy + current_resource_attribute = convert_system_date_to_mm_dd_yyyy(current_resource_attribute) if attribute == "start_day" + # Convert start_time into 24hr time format + current_resource_attribute = DateTime.parse(current_resource_attribute).strftime("%H:%M") if attribute == "start_time" new_resource.send("#{attribute}=", current_resource_attribute ) if current_resource_attribute && new_resource_attribute.nil? end end @@ -274,6 +283,14 @@ class Chef @system_short_date_format end + def convert_system_date_to_mm_dd_yyyy(system_date) + system_date_format = convert_system_date_format_to_ruby_date_format + unless system_date_format == "%m/%d/%Y" + system_date = Date.strptime(system_date, system_date_format).strftime("%m/%d/%Y") + end + system_date + end + def update_task_xml(options = []) # random_delay xml element is different for different frequencies random_delay_xml_element = { diff --git a/spec/unit/provider/windows_task_spec.rb b/spec/unit/provider/windows_task_spec.rb index 55a1e77e4e..d45531f3dc 100644 --- a/spec/unit/provider/windows_task_spec.rb +++ b/spec/unit/provider/windows_task_spec.rb @@ -110,6 +110,33 @@ describe Chef::Provider::WindowsTask do expect(new_resource).not_to be_updated_by_last_action end + it "sets the start_time in 24hr format while updating an existing task" do + # task_hash has start_time = "1:12:00 PM" + allow(provider).to receive(:load_task_hash).and_return(task_hash) + provider.load_current_resource + allow(provider).to receive(:task_need_update?).and_return(true) + allow(provider).to receive(:convert_system_date_to_mm_dd_yyyy).and_return("03/30/2017") + allow(provider).to receive(:run_schtasks) + provider.run_action(:create) + # start_time gets set in 24hr format for new_resource + expect(new_resource.start_time).to eq("13:12") + expect(new_resource).to be_updated_by_last_action + end + + it "sets the start_day in mm/dd/yyyy format while updating an existing task" do + # start_day in yyyy-MM-dd format + task_hash[:StartDate] = "2017-03-30" + allow(provider).to receive(:load_task_hash).and_return(task_hash) + current_resource = provider.load_current_resource + allow(provider).to receive(:task_need_update?).and_return(true) + allow(provider).to receive(:convert_system_date_format_to_ruby_date_format).and_return("%Y-%m-%d") + allow(provider).to receive(:run_schtasks) + provider.run_action(:create) + # start_day gets set in mm/dd/yyyy format for new_resource + expect(new_resource.start_day).to eq("03/30/2017") + expect(new_resource).to be_updated_by_last_action + end + context "when task is not existing" do before do allow(provider).to receive(:load_task_hash) |