summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBryan McLellan <btm@loftninjas.org>2017-09-12 13:02:13 -0400
committerGitHub <noreply@github.com>2017-09-12 13:02:13 -0400
commitf7173b4ff9e33443c53ede3a657f6332908666e8 (patch)
tree7e1fed305a4f894bb4a785533f5d8b6f55a6ddce
parenta3649800c39893899988b5900ffda66d5a9fdd1b (diff)
parent97ca5e89b709f41307c0d48384fd0366357b0e03 (diff)
downloadchef-f7173b4ff9e33443c53ede3a657f6332908666e8.tar.gz
Merge pull request #6395 from chef/btm/12_dsc-script
backport #6383 to 12
-rw-r--r--lib/chef/util/dsc/local_configuration_manager.rb24
-rw-r--r--spec/unit/util/dsc/local_configuration_manager_spec.rb68
2 files changed, 77 insertions, 15 deletions
diff --git a/lib/chef/util/dsc/local_configuration_manager.rb b/lib/chef/util/dsc/local_configuration_manager.rb
index d837a16185..1f154b1c71 100644
--- a/lib/chef/util/dsc/local_configuration_manager.rb
+++ b/lib/chef/util/dsc/local_configuration_manager.rb
@@ -47,15 +47,13 @@ class Chef::Util::DSC
def run_configuration_cmdlet(configuration_document, apply_configuration, shellout_flags)
Chef::Log.debug("DSC: Calling DSC Local Config Manager to #{apply_configuration ? "set" : "test"} configuration document.")
- test_only_parameters = ! apply_configuration ? "-whatif; if (! $?) { exit 1 }" : ""
start_operation_timing
- command_code = lcm_command_code(@configuration_path, test_only_parameters)
status = nil
begin
save_configuration_document(configuration_document)
- cmdlet = ::Chef::Util::Powershell::Cmdlet.new(@node, "#{command_code}")
+ cmdlet = ::Chef::Util::Powershell::Cmdlet.new(@node, lcm_command(apply_configuration))
if apply_configuration
status = cmdlet.run!({}, shellout_flags)
else
@@ -72,10 +70,22 @@ class Chef::Util::DSC
status
end
- def lcm_command_code(configuration_path, test_only_parameters)
- <<-EOH
-$ProgressPreference = 'SilentlyContinue';start-dscconfiguration -path #{@configuration_path} -wait -erroraction 'stop' -force #{test_only_parameters}
-EOH
+ def lcm_command(apply_configuration)
+ common_command_prefix = "$ProgressPreference = 'SilentlyContinue';"
+ ps4_base_command = "#{common_command_prefix} Start-DscConfiguration -path #{@configuration_path} -wait -erroraction 'stop' -force"
+ if apply_configuration
+ ps4_base_command
+ else
+ if ps_version_gte_5?
+ "#{common_command_prefix} Test-DscConfiguration -path #{@configuration_path}"
+ else
+ ps4_base_command + " -whatif; if (! $?) { exit 1 }"
+ end
+ end
+ end
+
+ def ps_version_gte_5?
+ Chef::Platform.supported_powershell_version?(@node, 5)
end
def log_what_if_exception(what_if_exception_output)
diff --git a/spec/unit/util/dsc/local_configuration_manager_spec.rb b/spec/unit/util/dsc/local_configuration_manager_spec.rb
index 3f1210dbf1..c87b446286 100644
--- a/spec/unit/util/dsc/local_configuration_manager_spec.rb
+++ b/spec/unit/util/dsc/local_configuration_manager_spec.rb
@@ -63,7 +63,7 @@ EOH
let(:lcm_standard_error) { nil }
let(:lcm_cmdlet_success) { true }
- it "should successfully return resource information for normally formatted output when cmdlet the cmdlet succeeds" do
+ it "successfully returns resource information for normally formatted output when cmdlet the cmdlet succeeds" do
test_configuration_result = lcm.test_configuration("config", {})
expect(test_configuration_result.class).to be(Array)
expect(test_configuration_result.length).to be > 0
@@ -71,6 +71,58 @@ EOH
end
end
+ context "when running on PowerShell version 5" do
+ let(:lcm_standard_output) { normal_lcm_output }
+ let(:lcm_standard_error) { nil }
+ let(:lcm_cmdlet_success) { true }
+
+ it "successfully returns resource information for normally formatted output when cmdlet the cmdlet succeeds" do
+ allow(lcm).to receive(:ps_version_gte_5?).and_return(true)
+ test_configuration_result = lcm.test_configuration("config", {})
+ expect(test_configuration_result.class).to be(Array)
+ expect(test_configuration_result.length).to be > 0
+ expect(Chef::Log).not_to receive(:warn)
+ end
+ end
+
+ context "when running on PowerShell version less than 5" do
+ let(:lcm_standard_output) { normal_lcm_output }
+ let(:lcm_standard_error) { nil }
+ let(:lcm_cmdlet_success) { true }
+
+ it "successfully returns resource information for normally formatted output when cmdlet the cmdlet succeeds" do
+ allow(lcm).to receive(:ps_version_gte_5?).and_return(false)
+ test_configuration_result = lcm.test_configuration("config", {})
+ expect(test_configuration_result.class).to be(Array)
+ expect(test_configuration_result.length).to be > 0
+ expect(Chef::Log).not_to receive(:warn)
+ end
+ end
+
+ context "#lcm_command" do
+ let(:common_command_prefix) { "$ProgressPreference = 'SilentlyContinue';" }
+ let(:ps4_base_command) { "#{common_command_prefix} Start-DscConfiguration -path tmp -wait -erroraction 'stop' -force" }
+ let(:lcm_command_ps4) { ps4_base_command + " -whatif; if (! $?) { exit 1 }" }
+ let(:lcm_command_ps5) { "#{common_command_prefix} Test-DscConfiguration -path tmp" }
+ let(:lcm_standard_output) { normal_lcm_output }
+ let(:lcm_standard_error) { nil }
+ let(:lcm_cmdlet_success) { true }
+
+ it "successfully returns command when apply_configuration true" do
+ expect(lcm.send(:lcm_command, true)).to eq(ps4_base_command)
+ end
+
+ it "successfully returns command when PowerShell version 4" do
+ allow(lcm).to receive(:ps_version_gte_5?).and_return(false)
+ expect(lcm.send(:lcm_command, false)).to eq(lcm_command_ps4)
+ end
+
+ it "successfully returns command when PowerShell version 5" do
+ allow(lcm).to receive(:ps_version_gte_5?).and_return(true)
+ expect(lcm.send(:lcm_command, false)).to eq(lcm_command_ps5)
+ end
+ end
+
context "that fails due to missing what-if switch in DSC resource cmdlet implementation" do
let(:lcm_standard_output) { "" }
let(:lcm_standard_error) { no_whatif_lcm_output }
@@ -80,7 +132,7 @@ EOH
expect(lcm.send(:whatif_not_supported?, no_whatif_lcm_output)).to be_truthy
end
- it "should should return a (possibly empty) array of ResourceInfo instances" do
+ it "returns a (possibly empty) array of ResourceInfo instances" do
expect(Chef::Log).to receive(:warn).at_least(:once)
expect(lcm).to receive(:whatif_not_supported?).and_call_original
test_configuration_result = nil
@@ -94,14 +146,14 @@ EOH
let(:lcm_standard_error) { dsc_resource_import_failure_output }
let(:lcm_cmdlet_success) { false }
- it "should log a warning if the message is formatted as expected when a resource import failure occurs" do
+ it "logs a warning if the message is formatted as expected when a resource import failure occurs" do
expect(Chef::Log).to receive(:warn).at_least(:once)
expect(lcm).to receive(:dsc_module_import_failure?).and_call_original
test_configuration_result = nil
expect { test_configuration_result = lcm.test_configuration("config", {}) }.not_to raise_error
end
- it "should return a (possibly empty) array of ResourceInfo instances" do
+ it "returns a (possibly empty) array of ResourceInfo instances" do
expect(Chef::Log).to receive(:warn).at_least(:once)
test_configuration_result = nil
expect { test_configuration_result = lcm.test_configuration("config", {}) }.not_to raise_error
@@ -114,7 +166,7 @@ EOH
let(:lcm_standard_error) { "Abort, Retry, Fail?" }
let(:lcm_cmdlet_success) { false }
- it "should log a warning" do
+ it "logs a warning" do
expect(Chef::Log).to receive(:warn).at_least(:once)
expect(lcm).to receive(:dsc_module_import_failure?).and_call_original
expect { lcm.test_configuration("config", {}) }.not_to raise_error
@@ -122,15 +174,15 @@ EOH
end
end
- it "should identify a correctly formatted error message as a resource import failure" do
+ it "identify a correctly formatted error message as a resource import failure" do
expect(lcm.send(:dsc_module_import_failure?, dsc_resource_import_failure_output)).to be(true)
end
- it "should not identify an incorrectly formatted error message as a resource import failure" do
+ it "does not identify an incorrectly formatted error message as a resource import failure" do
expect(lcm.send(:dsc_module_import_failure?, dsc_resource_import_failure_output.gsub("module", "gibberish"))).to be(false)
end
- it "should not identify a message without a CimException reference as a resource import failure" do
+ it "does not identify a message without a CimException reference as a resource import failure" do
expect(lcm.send(:dsc_module_import_failure?, dsc_resource_import_failure_output.gsub("CimException", "ArgumentException"))).to be(false)
end
end