diff options
author | Tim Smith <tsmith@chef.io> | 2020-12-01 12:57:44 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-01 12:57:44 -0800 |
commit | 9a78eaafabce31808702e600d823609920e85798 (patch) | |
tree | a5455f0d3e19135cdbf661cc427bd4a639cf43b5 /spec | |
parent | bbd4b74336db3531cb010537337a9fa490ea9312 (diff) | |
parent | d0fbda0d7f394c52b76735cbac978de2d29487a5 (diff) | |
download | chef-9a78eaafabce31808702e600d823609920e85798.tar.gz |
Merge pull request #10683 from chef/cmdlet
Signed-off-by: Tim Smith <tsmith@chef.io>
Diffstat (limited to 'spec')
-rw-r--r-- | spec/functional/resource/dsc_script_spec.rb | 7 | ||||
-rw-r--r-- | spec/functional/util/powershell/cmdlet_spec.rb | 111 | ||||
-rw-r--r-- | spec/spec_helper.rb | 2 | ||||
-rw-r--r-- | spec/unit/mixin/powershell_exec_spec.rb | 2 | ||||
-rw-r--r-- | spec/unit/platform/query_helpers_spec.rb | 23 | ||||
-rw-r--r-- | spec/unit/provider/dsc_resource_spec.rb | 37 | ||||
-rw-r--r-- | spec/unit/provider/dsc_script_spec.rb | 2 | ||||
-rw-r--r-- | spec/unit/util/dsc/configuration_generator_spec.rb | 79 | ||||
-rw-r--r-- | spec/unit/util/dsc/local_configuration_manager_spec.rb | 62 | ||||
-rw-r--r-- | spec/unit/util/powershell/cmdlet_spec.rb | 106 |
10 files changed, 132 insertions, 299 deletions
diff --git a/spec/functional/resource/dsc_script_spec.rb b/spec/functional/resource/dsc_script_spec.rb index 9d18e2f85d..87c624e85f 100644 --- a/spec/functional/resource/dsc_script_spec.rb +++ b/spec/functional/resource/dsc_script_spec.rb @@ -261,12 +261,9 @@ describe Chef::Resource::DscScript, :windows_powershell_dsc_only do it "should raise an exception if the cwd is etc" do dsc_test_resource.cwd(dsc_environment_fail_etc_directory) - expect { dsc_test_resource.run_action(:run) }.to raise_error(Chef::Exceptions::PowershellCmdletException) - begin + expect { dsc_test_resource.run_action(:run) - rescue Chef::Exceptions::PowershellCmdletException => e - expect(e.message).to match(exception_message_signature) - end + }.to raise_error(Chef::PowerShell::CommandFailed, /#{exception_message_signature}/) end end end diff --git a/spec/functional/util/powershell/cmdlet_spec.rb b/spec/functional/util/powershell/cmdlet_spec.rb deleted file mode 100644 index 565456f687..0000000000 --- a/spec/functional/util/powershell/cmdlet_spec.rb +++ /dev/null @@ -1,111 +0,0 @@ -# -# Author:: Adam Edwards (<adamed@chef.io>) -# -# Copyright:: Copyright (c) Chef Software Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# - -require "chef/json_compat" -require "spec_helper" - -describe Chef::Util::Powershell::Cmdlet, :windows_powershell_dsc_only do - before(:all) do - @node = Chef::Node.new - @node.consume_external_attrs(OHAI_SYSTEM.data, {}) - end - let(:cmd_output_format) { :text } - let(:simple_cmdlet) { Chef::Util::Powershell::Cmdlet.new(@node, "get-childitem", cmd_output_format, { depth: 2 }) } - let(:invalid_cmdlet) { Chef::Util::Powershell::Cmdlet.new(@node, "get-idontexist", cmd_output_format) } - let(:cmdlet_get_item_requires_switch_or_argument) { Chef::Util::Powershell::Cmdlet.new(@node, "get-item", cmd_output_format, { depth: 2 }) } - let(:cmdlet_alias_requires_switch_or_argument) { Chef::Util::Powershell::Cmdlet.new(@node, "alias", cmd_output_format, { depth: 2 }) } - let(:etc_directory) { "#{ENV["systemroot"]}\\system32\\drivers\\etc" } - let(:architecture_cmdlet) { Chef::Util::Powershell::Cmdlet.new(@node, "$env:PROCESSOR_ARCHITECTURE") } - - it "executes a simple process" do - result = simple_cmdlet.run - expect(result.succeeded?).to eq(true) - end - - it "#run does not raise a PowershellCmdletException exception if the command cannot be executed" do - expect { invalid_cmdlet.run }.not_to raise_error - end - - it "#run! raises a PowershellCmdletException exception if the command cannot be executed" do - expect { invalid_cmdlet.run! }.to raise_error(Chef::Exceptions::PowershellCmdletException) - end - - it "executes a 64-bit command on a 64-bit OS, 32-bit otherwise" do - os_arch = ENV["PROCESSOR_ARCHITEW6432"] - if os_arch.nil? - os_arch = ENV["PROCESSOR_ARCHITECTURE"] - end - - result = architecture_cmdlet.run - execution_arch = result.return_value - execution_arch.strip! - expect(execution_arch).to eq(os_arch) - end - - it "passes command line switches to the command" do - result = cmdlet_alias_requires_switch_or_argument.run({ name: "ls" }) - expect(result.succeeded?).to eq(true) - end - - it "passes command line arguments to the command" do - result = cmdlet_alias_requires_switch_or_argument.run({}, {}, "ls") - expect(result.succeeded?).to eq(true) - end - - it "passes command line arguments and switches to the command" do - result = cmdlet_get_item_requires_switch_or_argument.run({ path: etc_directory }, {}, " | select-object -property fullname | format-table -hidetableheaders") - expect(result.succeeded?).to eq(true) - returned_directory = result.return_value - returned_directory.strip! - expect(returned_directory).to eq(etc_directory) - end - - it "passes execution options to the command" do - result = cmdlet_get_item_requires_switch_or_argument.run({}, { cwd: etc_directory }, ". | select-object -property fullname | format-table -hidetableheaders") - expect(result.succeeded?).to eq(true) - returned_directory = result.return_value - returned_directory.strip! - expect(returned_directory).to eq(etc_directory) - end - - context "when returning json" do - let(:cmd_output_format) { :json } - it "returns json format data" do - result = cmdlet_alias_requires_switch_or_argument.run({}, {}, "ls") - expect(result.succeeded?).to eq(true) - expect { Chef::JSONCompat.parse(result.return_value) }.not_to raise_error - end - end - - context "when returning Ruby objects" do - let(:cmd_output_format) { :object } - it "returns object format data" do - result = simple_cmdlet.run({}, { cwd: etc_directory }, "hosts") - expect(result.succeeded?).to eq(true) - data = result.return_value - expect(data["Name"]).to eq("hosts") - end - end - - context "when constructor is given invalid arguments" do - let(:cmd_output_format) { :invalid } - it "throws an exception if an invalid format is passed to the constructor" do - expect { simple_cmdlet }.to raise_error(ArgumentError) - end - end -end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 4c925bace3..17ce1ab5b7 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -31,7 +31,7 @@ $LOAD_PATH.unshift File.expand_path("../chef-utils/lib", __dir__) require "rubygems" require "rspec/mocks" - +require "rexml/document" require "webmock/rspec" require "chef" diff --git a/spec/unit/mixin/powershell_exec_spec.rb b/spec/unit/mixin/powershell_exec_spec.rb index 245f681688..92e92dc2a1 100644 --- a/spec/unit/mixin/powershell_exec_spec.rb +++ b/spec/unit/mixin/powershell_exec_spec.rb @@ -66,7 +66,7 @@ describe Chef::Mixin::PowershellExec, :windows_only do execution = object.powershell_exec("this-should-error") expect(execution.errors).to be_a_kind_of(Array) expect(execution.errors[0]).to be_a_kind_of(String) - expect(execution.errors[0]).to include("Runtime exception: this-should-error") + expect(execution.errors[0]).to include("The term 'this-should-error' is not recognized") end it "raises an error if the interpreter is invalid" do diff --git a/spec/unit/platform/query_helpers_spec.rb b/spec/unit/platform/query_helpers_spec.rb index 728348e9af..0b4169810e 100644 --- a/spec/unit/platform/query_helpers_spec.rb +++ b/spec/unit/platform/query_helpers_spec.rb @@ -41,24 +41,23 @@ end describe "Chef::Platform#dsc_refresh_mode_disabled?" do let(:node) { instance_double("Chef::Node") } - let(:cmdlet) { instance_double("Chef::Util::Powershell::Cmdlet") } - let(:cmdlet_result) { instance_double("Chef::Util::Powershell::CmdletResult") } + let(:powershell) { instance_double("Chef::PowerShell") } it "returns true when RefreshMode is Disabled" do - expect(Chef::Util::Powershell::Cmdlet).to receive(:new) - .with(node, "Get-DscLocalConfigurationManager", :object) - .and_return(cmdlet) - expect(cmdlet).to receive(:run!).and_return(cmdlet_result) - expect(cmdlet_result).to receive(:return_value).and_return({ "RefreshMode" => "Disabled" }) + expect(Chef::PowerShell).to receive(:new) + .with("Get-DscLocalConfigurationManager") + .and_return(powershell) + expect(powershell).to receive(:error!) + expect(powershell).to receive(:result).and_return({ "RefreshMode" => "Disabled" }) expect(Chef::Platform.dsc_refresh_mode_disabled?(node)).to be true end it "returns false when RefreshMode is not Disabled" do - expect(Chef::Util::Powershell::Cmdlet).to receive(:new) - .with(node, "Get-DscLocalConfigurationManager", :object) - .and_return(cmdlet) - expect(cmdlet).to receive(:run!).and_return(cmdlet_result) - expect(cmdlet_result).to receive(:return_value).and_return({ "RefreshMode" => "LaLaLa" }) + expect(Chef::PowerShell).to receive(:new) + .with("Get-DscLocalConfigurationManager") + .and_return(powershell) + expect(powershell).to receive(:error!) + expect(powershell).to receive(:result).and_return({ "RefreshMode" => "LaLaLa" }) expect(Chef::Platform.dsc_refresh_mode_disabled?(node)).to be false end end diff --git a/spec/unit/provider/dsc_resource_spec.rb b/spec/unit/provider/dsc_resource_spec.rb index 8613ce4af4..2540cb9df2 100644 --- a/spec/unit/provider/dsc_resource_spec.rb +++ b/spec/unit/provider/dsc_resource_spec.rb @@ -85,14 +85,13 @@ describe Chef::Provider::DscResource do node.automatic[:languages][:powershell][:version] = "5.0.10018.0" node end - let(:resource_result) { double("CmdletResult", return_value: { "InDesiredState" => true }, stream: "description") } - let(:invoke_dsc_resource) { double("cmdlet", run!: resource_result) } + let(:resource_result) { double("PowerShell", result: { "InDesiredState" => true }, verbose: ["description"]) } let(:store) { double("ResourceStore", find: resource_records) } let(:resource_records) { [] } before do allow(Chef::Util::DSC::ResourceStore).to receive(:instance).and_return(store) - allow(Chef::Util::Powershell::Cmdlet).to receive(:new).and_return(invoke_dsc_resource) + allow(provider).to receive(:powershell_exec!).and_return(resource_result) allow(provider).to receive(:dsc_refresh_mode_disabled?).and_return(true) end @@ -112,9 +111,8 @@ describe Chef::Provider::DscResource do it "flags the resource as reboot required when required" do expect(provider).to receive(:test_resource).and_return(false) expect(provider).to receive(:invoke_resource) - .and_return(double(stdout: "", return_value: nil)) + .and_return(double(result: { "RebootRequired" => true })) expect(provider).to receive(:add_dsc_verbose_log) - expect(provider).to receive(:return_dsc_resource_result).and_return(true) expect(provider).to receive(:create_reboot_resource) provider.run_action(:run) end @@ -122,9 +120,8 @@ describe Chef::Provider::DscResource do it "does not flag the resource as reboot required when not required" do expect(provider).to receive(:test_resource).and_return(false) expect(provider).to receive(:invoke_resource) - .and_return(double(stdout: "", return_value: nil)) + .and_return(double(stdout: "", result: {})) expect(provider).to receive(:add_dsc_verbose_log) - expect(provider).to receive(:return_dsc_resource_result).and_return(false) expect(provider).to_not receive(:create_reboot_resource) provider.run_action(:run) end @@ -142,9 +139,7 @@ describe Chef::Provider::DscResource do let(:resource_records) { [{}] } it "returns the default dsc resource module" do - expect(Chef::Util::Powershell::Cmdlet).to receive(:new) do |node, cmdlet, format| - expect(cmdlet).to match(/Module PSDesiredStateConfiguration /) - end.and_return(invoke_dsc_resource) + expect(provider).to receive(:powershell_exec!).with(/Module PSDesiredStateConfiguration /).and_return(resource_result) provider.run_action(:run) end end @@ -153,9 +148,7 @@ describe Chef::Provider::DscResource do let(:resource_records) { [{ "Module" => { "Name" => "ModuleName" } }] } it "returns the default dsc resource module" do - expect(Chef::Util::Powershell::Cmdlet).to receive(:new) do |node, cmdlet, format| - expect(cmdlet).to match(/Module ModuleName /) - end.and_return(invoke_dsc_resource) + expect(provider).to receive(:powershell_exec!).with(/Module ModuleName /).and_return(resource_result) provider.run_action(:run) end end @@ -286,8 +279,6 @@ describe Chef::Provider::DscResource do end describe "invoke_resource" do - let(:cmdlet) { double(run!: nil) } - before(:each) do allow(provider).to receive(:translate_type).and_return("my_properties") provider.instance_variable_set(:@new_resource, double( @@ -301,12 +292,8 @@ describe Chef::Provider::DscResource do end it "invokes Invoke-DscResource command with module name" do - expect(Chef::Util::Powershell::Cmdlet).to receive(:new).with( - node, - "Invoke-DscResource -Method my_method -Name my_resource -Property my_properties -Module my_module -Verbose", - "my_output_format" - ).and_return(cmdlet) - provider.send(:invoke_resource, "my_method", "my_output_format") + expect(provider).to receive(:powershell_exec!).with("Invoke-DscResource -Method my_method -Name my_resource -Property my_properties -Module my_module -Verbose").and_return(nil) + provider.send(:invoke_resource, "my_method") end end @@ -318,12 +305,8 @@ describe Chef::Provider::DscResource do end it "invokes Invoke-DscResource command with module info object" do - expect(Chef::Util::Powershell::Cmdlet).to receive(:new).with( - node, - "Invoke-DscResource -Method my_method -Name my_resource -Property my_properties -Module @{ModuleName='my_module';ModuleVersion='my_module_version'} -Verbose", - "my_output_format" - ).and_return(cmdlet) - provider.send(:invoke_resource, "my_method", "my_output_format") + expect(provider).to receive(:powershell_exec!).with("Invoke-DscResource -Method my_method -Name my_resource -Property my_properties -Module @{ModuleName='my_module';ModuleVersion='my_module_version'} -Verbose").and_return(nil) + provider.send(:invoke_resource, "my_method") end end end diff --git a/spec/unit/provider/dsc_script_spec.rb b/spec/unit/provider/dsc_script_spec.rb index f0a63e0a5b..d59b6f2480 100644 --- a/spec/unit/provider/dsc_script_spec.rb +++ b/spec/unit/provider/dsc_script_spec.rb @@ -99,7 +99,7 @@ describe Chef::Provider::DscScript do it "should noop if neither code or command are provided" do allow(provider).to receive(:load_current_resource) generator = double("Chef::Util::DSC::ConfigurationGenerator") - expect(generator).to receive(:configuration_document_from_script_code).with("", anything, anything, anything) + expect(generator).to receive(:configuration_document_from_script_code).with("", anything, anything) allow(Chef::Util::DSC::ConfigurationGenerator).to receive(:new).and_return(generator) provider.send(:generate_configuration_document, "tmp", nil) end diff --git a/spec/unit/util/dsc/configuration_generator_spec.rb b/spec/unit/util/dsc/configuration_generator_spec.rb index eee6adbd07..3f85f3189d 100644 --- a/spec/unit/util/dsc/configuration_generator_spec.rb +++ b/spec/unit/util/dsc/configuration_generator_spec.rb @@ -25,6 +25,85 @@ describe Chef::Util::DSC::ConfigurationGenerator do Chef::Util::DSC::ConfigurationGenerator.new(node, "tmp") end + describe "#validate_switch_name!" do + it "should not raise an error if a name contains all upper case letters" do + conf_man.send(:validate_switch_name!, "HELLO") + end + + it "should not raise an error if the name contains all lower case letters" do + conf_man.send(:validate_switch_name!, "hello") + end + + it "should not raise an error if no special characters are used except _" do + conf_man.send(:validate_switch_name!, "hello_world") + end + + %w{! @ # $ % ^ & * & * ( ) - = + \{ \} . ? < > \\ /}.each do |sym| + it "raises an ArgumentError if configuration name contains #{sym}" do + expect do + conf_man.send(:validate_switch_name!, "Hello#{sym}") + end.to raise_error(ArgumentError) + end + end + end + + describe "#escape_parameter_value" do + # Is this list really complete? + %w{` " # '}.each do |c| + it "escapes #{c}" do + expect(conf_man.send(:escape_parameter_value, "stuff #{c}")).to eql("stuff `#{c}") + end + end + + it "does not do anything to a string without special characters" do + expect(conf_man.send(:escape_parameter_value, "stuff")).to eql("stuff") + end + end + + describe "#escape_string_parameter_value" do + it "surrounds a string with ''" do + expect(conf_man.send(:escape_string_parameter_value, "stuff")).to eql("'stuff'") + end + end + + describe "#command_switches_string" do + it "raises an ArgumentError if the key is not a symbol" do + expect do + conf_man.send(:command_switches_string, { "foo" => "bar" }) + end.to raise_error(ArgumentError) + end + + it "does not allow invalid switch names" do + expect do + conf_man.send(:command_switches_string, { foo!: "bar" }) + end.to raise_error(ArgumentError) + end + + it "ignores switches with a false value" do + expect(conf_man.send(:command_switches_string, { foo: false })).to eql("") + end + + it "should correctly handle a value type of string" do + expect(conf_man.send(:command_switches_string, { foo: "bar" })).to eql("-foo 'bar'") + end + + it "should correctly handle a value type of string even when it is 0 length" do + expect(conf_man.send(:command_switches_string, { foo: "" })).to eql("-foo ''") + end + + it "should not quote integers" do + expect(conf_man.send(:command_switches_string, { foo: 1 })).to eql("-foo 1") + end + + it "should not quote floats" do + expect(conf_man.send(:command_switches_string, { foo: 1.0 })).to eql("-foo 1.0") + end + + it "has just the switch when the value is true" do + expect(conf_man.send(:command_switches_string, { foo: true })).to eql("-foo") + end + end + describe "#validate_configuration_name!" do it "should not raise an error if a name contains all upper case letters" do conf_man.send(:validate_configuration_name!, "HELLO") diff --git a/spec/unit/util/dsc/local_configuration_manager_spec.rb b/spec/unit/util/dsc/local_configuration_manager_spec.rb index e0f7c4ddaf..8adf778949 100644 --- a/spec/unit/util/dsc/local_configuration_manager_spec.rb +++ b/spec/unit/util/dsc/local_configuration_manager_spec.rb @@ -49,23 +49,22 @@ describe Chef::Util::DSC::LocalConfigurationManager do EOH end - let(:lcm_status) do - double("LCM cmdlet status", stderr: lcm_standard_error, return_value: lcm_standard_output, succeeded?: lcm_cmdlet_success) + let(:powershell) do + double("Chef::PowerShell", errors: lcm_errors, error?: !lcm_errors.empty?, result: lcm_result) end describe "test_configuration method invocation" do context "when interacting with the LCM using a PowerShell cmdlet" do before(:each) do - allow(lcm).to receive(:run_configuration_cmdlet).and_return(lcm_status) + allow(lcm).to receive(:run_configuration_cmdlet).and_return(powershell) allow(lcm).to receive(:ps_version_gte_5?).and_return(false) end context "that returns successfully" do - let(:lcm_standard_output) { normal_lcm_output } - let(:lcm_standard_error) { nil } - let(:lcm_cmdlet_success) { true } + let(:lcm_result) { normal_lcm_output } + let(:lcm_errors) { [] } it "successfully returns resource information for normally formatted output when cmdlet the cmdlet succeeds" do - test_configuration_result = lcm.test_configuration("config", {}) + 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) @@ -73,13 +72,12 @@ describe Chef::Util::DSC::LocalConfigurationManager do 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 } + let(:lcm_result) { normal_lcm_output } + let(:lcm_errors) { [] } 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", {}) + 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) @@ -87,13 +85,12 @@ describe Chef::Util::DSC::LocalConfigurationManager do 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 } + let(:lcm_result) { normal_lcm_output } + let(:lcm_errors) { [] } 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", {}) + 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) @@ -104,10 +101,9 @@ describe Chef::Util::DSC::LocalConfigurationManager 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 | format-list" } - let(:lcm_standard_output) { normal_lcm_output } - let(:lcm_standard_error) { nil } - let(:lcm_cmdlet_success) { true } + let(:lcm_command_ps5) { "#{common_command_prefix} Test-DscConfiguration -path tmp | format-list | Out-String" } + let(:lcm_result) { normal_lcm_output } + let(:lcm_errors) { [] } it "successfully returns command when apply_configuration true" do expect(lcm.send(:lcm_command, true)).to eq(ps4_base_command) @@ -125,9 +121,8 @@ describe Chef::Util::DSC::LocalConfigurationManager do 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 } - let(:lcm_cmdlet_success) { false } + let(:lcm_result) { "" } + let(:lcm_errors) { [no_whatif_lcm_output] } it "returns true when passed to #whatif_not_supported?" do expect(lcm.send(:whatif_not_supported?, no_whatif_lcm_output)).to be_truthy @@ -137,40 +132,38 @@ describe Chef::Util::DSC::LocalConfigurationManager do expect(Chef::Log).to receive(:warn).at_least(:once) expect(lcm).to receive(:whatif_not_supported?).and_call_original test_configuration_result = nil - expect { test_configuration_result = lcm.test_configuration("config", {}) }.not_to raise_error + expect { test_configuration_result = lcm.test_configuration("config") }.not_to raise_error expect(test_configuration_result.class).to be(Array) end end context "that fails due to a DSC resource not being imported before StartDSCConfiguration -whatif is executed" do - let(:lcm_standard_output) { "" } - let(:lcm_standard_error) { dsc_resource_import_failure_output } - let(:lcm_cmdlet_success) { false } + let(:lcm_result) { "" } + let(:lcm_errors) { [dsc_resource_import_failure_output] } 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 + expect { test_configuration_result = lcm.test_configuration("config") }.not_to raise_error end 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 + expect { test_configuration_result = lcm.test_configuration("config") }.not_to raise_error expect(test_configuration_result.class).to be(Array) end end context "that fails due to an unknown PowerShell cmdlet error" do - let(:lcm_standard_output) { "some output" } - let(:lcm_standard_error) { "Abort, Retry, Fail?" } - let(:lcm_cmdlet_success) { false } + let(:lcm_result) { "some output" } + let(:lcm_errors) { ["Abort, Retry, Fail?"] } 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 + expect { lcm.test_configuration("config") }.not_to raise_error end end end @@ -188,12 +181,11 @@ describe Chef::Util::DSC::LocalConfigurationManager do end end - describe "#run_configuration_cmdlet" do + describe "#run_configuration_cmdlet", :windows_powershell_dsc_only do context "when invalid dsc script is given" do it "raises exception" do configuration_document = "invalid-config" - shellout_flags = { cwd: nil, environment: nil, timeout: nil } - expect { lcm.send(:run_configuration_cmdlet, configuration_document, true, shellout_flags) }.to raise_error(Chef::Exceptions::PowershellCmdletException) + expect { lcm.send(:run_configuration_cmdlet, configuration_document, true) }.to raise_error(Chef::PowerShell::CommandFailed) end end end diff --git a/spec/unit/util/powershell/cmdlet_spec.rb b/spec/unit/util/powershell/cmdlet_spec.rb deleted file mode 100644 index 4dc6e2b85c..0000000000 --- a/spec/unit/util/powershell/cmdlet_spec.rb +++ /dev/null @@ -1,106 +0,0 @@ -# -# Author:: Jay Mundrawala <jdm@chef.io> -# Copyright:: Copyright (c) Chef Software Inc. -# License:: Apache License, Version 2.0 -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# - -require "chef" -require "chef/util/powershell/cmdlet" - -describe Chef::Util::Powershell::Cmdlet do - before (:all) do - @node = Chef::Node.new - @cmdlet = Chef::Util::Powershell::Cmdlet.new(@node, "Some-Commandlet") - end - - describe "#validate_switch_name!" do - it "should not raise an error if a name contains all upper case letters" do - @cmdlet.send(:validate_switch_name!, "HELLO") - end - - it "should not raise an error if the name contains all lower case letters" do - @cmdlet.send(:validate_switch_name!, "hello") - end - - it "should not raise an error if no special characters are used except _" do - @cmdlet.send(:validate_switch_name!, "hello_world") - end - - %w{! @ # $ % ^ & * & * ( ) - = + \{ \} . ? < > \\ /}.each do |sym| - it "raises an Argument error if it configuration name contains #{sym}" do - expect do - @cmdlet.send(:validate_switch_name!, "Hello#{sym}") - end.to raise_error(ArgumentError) - end - end - end - - describe "#escape_parameter_value" do - # Is this list really complete? - %w{` " # '}.each do |c| - it "escapse #{c}" do - expect(@cmdlet.send(:escape_parameter_value, "stuff #{c}")).to eql("stuff `#{c}") - end - end - - it "does not do anything to a string without special characters" do - expect(@cmdlet.send(:escape_parameter_value, "stuff")).to eql("stuff") - end - end - - describe "#escape_string_parameter_value" do - it "surrounds a string with ''" do - expect(@cmdlet.send(:escape_string_parameter_value, "stuff")).to eql("'stuff'") - end - end - - describe "#command_switches_string" do - it "raises an ArgumentError if the key is not a symbol" do - expect do - @cmdlet.send(:command_switches_string, { "foo" => "bar" }) - end.to raise_error(ArgumentError) - end - - it "does not allow invalid switch names" do - expect do - @cmdlet.send(:command_switches_string, { foo!: "bar" }) - end.to raise_error(ArgumentError) - end - - it "ignores switches with a false value" do - expect(@cmdlet.send(:command_switches_string, { foo: false })).to eql("") - end - - it "should correctly handle a value type of string" do - expect(@cmdlet.send(:command_switches_string, { foo: "bar" })).to eql("-foo 'bar'") - end - - it "should correctly handle a value type of string even when it is 0 length" do - expect(@cmdlet.send(:command_switches_string, { foo: "" })).to eql("-foo ''") - end - - it "should not quote integers" do - expect(@cmdlet.send(:command_switches_string, { foo: 1 })).to eql("-foo 1") - end - - it "should not quote floats" do - expect(@cmdlet.send(:command_switches_string, { foo: 1.0 })).to eql("-foo 1.0") - end - - it "has just the switch when the value is true" do - expect(@cmdlet.send(:command_switches_string, { foo: true })).to eql("-foo") - end - end -end |