summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Smith <tsmith@chef.io>2017-12-11 10:05:01 -0800
committerGitHub <noreply@github.com>2017-12-11 10:05:01 -0800
commit19eb180aace2aedb9bbd60093bfb286c8d781128 (patch)
tree7ad8238430ed749a4ea0be92ef84bb3df0805aa3
parentb40007342ba57eaf49cba75b6f134429bdfa68b6 (diff)
parent8802455211fbdcd91ff51357303321c46f98322e (diff)
downloadchef-19eb180aace2aedb9bbd60093bfb286c8d781128.tar.gz
Merge pull request #6654 from MsysTechnologiesllc/vasundhara/chef_windows_task_resource_not_idempotent_for_any_command
windows_task: Fix resource isn't fully idempotent due to command property
-rw-r--r--lib/chef/provider/windows_task.rb11
-rw-r--r--spec/unit/provider/windows_task_spec.rb15
2 files changed, 19 insertions, 7 deletions
diff --git a/lib/chef/provider/windows_task.rb b/lib/chef/provider/windows_task.rb
index 5228254308..02e45cb9e0 100644
--- a/lib/chef/provider/windows_task.rb
+++ b/lib/chef/provider/windows_task.rb
@@ -205,8 +205,14 @@ class Chef
def run_schtasks(task_action, options = {})
cmd = "schtasks /#{task_action} /TN \"#{new_resource.task_name}\" "
options.keys.each do |option|
- cmd += "/#{option} "
- cmd += "\"#{options[option].to_s.gsub('"', "\\\"")}\" " unless options[option] == ""
+ unless option == "TR"
+ cmd += "/#{option} "
+ cmd += "\"#{options[option].to_s.gsub('"', "\\\"")}\" " unless options[option] == ""
+ end
+ end
+ # Appending Task Run [TR] option at the end since appending causing sometimes to append other options in option["TR"] value
+ if options["TR"]
+ cmd += "/TR \"#{options["TR"]} \" " unless task_action == "DELETE"
end
Chef::Log.debug("running: ")
Chef::Log.debug(" #{cmd}")
@@ -359,7 +365,6 @@ class Chef
options["RP"] = new_resource.password if new_resource.password
options["IT"] = "" if new_resource.interactive_enabled
options["XML"] = temp_task_file
-
run_schtasks("DELETE", "F" => "")
run_schtasks("CREATE", options)
ensure
diff --git a/spec/unit/provider/windows_task_spec.rb b/spec/unit/provider/windows_task_spec.rb
index 9a73f4f07f..ce0fe9e50e 100644
--- a/spec/unit/provider/windows_task_spec.rb
+++ b/spec/unit/provider/windows_task_spec.rb
@@ -40,7 +40,7 @@ describe Chef::Provider::WindowsTask do
:LastRunTime => "3/30/2017 2:27:00 PM",
:LastResult => "1",
:Author => "Administrator",
- :TaskToRun => "chef-client",
+ :TaskToRun => "chef-client -L C:\\tmp\\",
:StartIn => "N/A",
:Comment => "N/A",
:ScheduledTaskState => "Enabled",
@@ -91,7 +91,7 @@ describe Chef::Provider::WindowsTask do
allow(provider).to receive(:load_task_hash).and_return(task_hash)
current_resource = provider.load_current_resource
expect(current_resource.exists).to be(true)
- expect(current_resource.command).to eq("chef-client")
+ expect(current_resource.command).to eq("chef-client -L C:\\tmp\\")
expect(current_resource.user).to eq("SYSTEM")
expect(current_resource.run_level).to eq(:highest)
expect(current_resource.frequency).to eq(:minute)
@@ -328,7 +328,7 @@ describe Chef::Provider::WindowsTask do
before do
@task_action = "CREATE"
@options = { "F" => "", "SC" => :minute, "MO" => 15, "TR" => "chef-client", "RU" => "SYSTEM", "RL" => "HIGHEST" }
- @cmd = "schtasks /CREATE /TN \"sample_task\" /F /SC \"minute\" /MO \"15\" /TR \"chef-client\" /RU \"SYSTEM\" /RL \"HIGHEST\" "
+ @cmd = "schtasks /CREATE /TN \"sample_task\" /F /SC \"minute\" /MO \"15\" /RU \"SYSTEM\" /RL \"HIGHEST\" /TR \"chef-client \" "
end
it "forms the command properly from the given options" do
@@ -379,7 +379,7 @@ describe Chef::Provider::WindowsTask do
allow(provider).to receive(:get_system_short_date_format).and_return("MM/dd/yyyy")
provider.load_current_resource
- new_resource.command "chef-client"
+ new_resource.command "chef-client -L C:\\tmp\\"
new_resource.run_level :highest
new_resource.frequency :minute
new_resource.frequency_modifier 15
@@ -422,6 +422,13 @@ describe Chef::Provider::WindowsTask do
expect(provider.send(:task_need_update?)).to be(true)
end
end
+
+ context "when command updated" do
+ it "return true" do
+ new_resource.command "chef-client"
+ expect(provider.send(:task_need_update?)).to be(true)
+ end
+ end
end
end