From 970deabf029f8f35a973a5baf081031c73c89809 Mon Sep 17 00:00:00 2001 From: Neha Pansare Date: Fri, 11 Nov 2022 17:00:40 +0530 Subject: Update occurences for Chef::PowerShell to chef-powershell gem's ChefPowershell::PowerShell Signed-off-by: Neha Pansare --- lib/chef/mixin/powershell_exec.rb | 33 +++++----------------- lib/chef/platform/query_helpers.rb | 4 +-- spec/functional/resource/dsc_script_spec.rb | 2 +- spec/unit/mixin/powershell_exec_spec.rb | 18 ++++++------ spec/unit/platform/query_helpers_spec.rb | 10 +++---- .../util/dsc/local_configuration_manager_spec.rb | 4 +-- 6 files changed, 26 insertions(+), 45 deletions(-) diff --git a/lib/chef/mixin/powershell_exec.rb b/lib/chef/mixin/powershell_exec.rb index bbf8ae1a69..25b6a683db 100644 --- a/lib/chef/mixin/powershell_exec.rb +++ b/lib/chef/mixin/powershell_exec.rb @@ -15,8 +15,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -require_relative "../powershell" -require_relative "../pwsh" +# Powershell is being run using chef-powershell gem. The code resides at https://github.com/chef/chef-powershell-shim # The powershell_exec mixin provides in-process access to the PowerShell engine. # @@ -95,33 +94,15 @@ require_relative "../pwsh" # credentials of the user running Chef Client are used. # +if ChefUtils.windows? + require "chef-powershell" +end + class Chef module Mixin module PowershellExec - # Run a command under PowerShell via a managed (.NET) API. - # - # Requires: .NET Framework 4.0 or higher on the target machine. - # - # @param script [String] script to run - # @param interpreter [Symbol] the interpreter type, `:powershell` or `:pwsh` - # @return [Chef::PowerShell] output - def powershell_exec(script, interpreter = :powershell) - case interpreter - when :powershell - Chef::PowerShell.new(script) - when :pwsh - Chef::Pwsh.new(script) - else - raise ArgumentError, "Expected interpreter of :powershell or :pwsh" - end - end - - # The same as the #powershell_exec method except this will raise - # Chef::PowerShell::CommandFailed if the command fails - def powershell_exec!(script, interpreter = :powershell) - cmd = powershell_exec(script, interpreter) - cmd.error! - cmd + if ChefUtils.windows? + include ChefPowerShell::ChefPowerShellModule::PowerShellExec end end end diff --git a/lib/chef/platform/query_helpers.rb b/lib/chef/platform/query_helpers.rb index bd0703d72a..efee17e6b2 100644 --- a/lib/chef/platform/query_helpers.rb +++ b/lib/chef/platform/query_helpers.rb @@ -58,8 +58,8 @@ class Chef end def dsc_refresh_mode_disabled?(node) - require_relative "../powershell" - exec = Chef::PowerShell.new("Get-DscLocalConfigurationManager") + require "chef-powershell" + exec = ChefPowerShell::PowerShell.new("Get-DscLocalConfigurationManager") exec.error! exec.result["RefreshMode"] == "Disabled" end diff --git a/spec/functional/resource/dsc_script_spec.rb b/spec/functional/resource/dsc_script_spec.rb index b22599266b..a3299f041e 100644 --- a/spec/functional/resource/dsc_script_spec.rb +++ b/spec/functional/resource/dsc_script_spec.rb @@ -263,7 +263,7 @@ describe Chef::Resource::DscScript, :windows_powershell_dsc_only, :ruby64_only d dsc_test_resource.cwd(dsc_environment_fail_etc_directory) expect { dsc_test_resource.run_action(:run) - }.to raise_error(Chef::PowerShell::CommandFailed, /#{exception_message_signature}/) + }.to raise_error(ChefPowerShell::PowerShellExceptions::PowerShellCommandFailed, /#{exception_message_signature}/) end end end diff --git a/spec/unit/mixin/powershell_exec_spec.rb b/spec/unit/mixin/powershell_exec_spec.rb index 92e92dc2a1..a5a403f69a 100644 --- a/spec/unit/mixin/powershell_exec_spec.rb +++ b/spec/unit/mixin/powershell_exec_spec.rb @@ -25,8 +25,8 @@ describe Chef::Mixin::PowershellExec, :windows_only do describe "#powershell_exec" do context "not specifying an interpreter" do - it "runs a basic command and returns a Chef::PowerShell object" do - expect(object.powershell_exec("$PSVersionTable")).to be_kind_of(Chef::PowerShell) + it "runs a basic command and returns a ChefPowerShell::PowerShell object" do + expect(object.powershell_exec("$PSVersionTable")).to be_kind_of(ChefPowerShell::PowerShell) end it "uses less than version 6" do @@ -36,8 +36,8 @@ describe Chef::Mixin::PowershellExec, :windows_only do end context "using pwsh interpreter" do - it "runs a basic command and returns a Chef::PowerShell object" do - expect(object.powershell_exec("$PSVersionTable", :pwsh)).to be_kind_of(Chef::Pwsh) + it "runs a basic command and returns a ChefPowerShell::Pwsh object" do + expect(object.powershell_exec("$PSVersionTable", :pwsh)).to be_kind_of(ChefPowerShell::Pwsh) end it "uses greater than version 6" do @@ -47,8 +47,8 @@ describe Chef::Mixin::PowershellExec, :windows_only do end context "using powershell interpreter" do - it "runs a basic command and returns a Chef::PowerShell object" do - expect(object.powershell_exec("$PSVersionTable", :powershell)).to be_kind_of(Chef::PowerShell) + it "runs a basic command and returns a ChefPowerShell::PowerShell object" do + expect(object.powershell_exec("$PSVersionTable", :powershell)).to be_kind_of(ChefPowerShell::PowerShell) end it "uses less than version 6" do @@ -75,12 +75,12 @@ describe Chef::Mixin::PowershellExec, :windows_only do end describe "#powershell_exec!" do - it "runs a basic command and returns a Chef::PowerShell object" do - expect(object.powershell_exec!("$PSVersionTable")).to be_kind_of(Chef::PowerShell) + it "runs a basic command and returns a ChefPowerShell::PowerShell object" do + expect(object.powershell_exec!("$PSVersionTable")).to be_kind_of(ChefPowerShell::PowerShell) end it "raises an error if the command fails" do - expect { object.powershell_exec!("this-should-error") }.to raise_error(Chef::PowerShell::CommandFailed) + expect { object.powershell_exec!("this-should-error") }.to raise_error(ChefPowerShell::PowerShellExceptions::PowerShellCommandFailed) 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 0b4169810e..b476b45196 100644 --- a/spec/unit/platform/query_helpers_spec.rb +++ b/spec/unit/platform/query_helpers_spec.rb @@ -18,7 +18,7 @@ require "spec_helper" -describe "Chef::Platform#supports_dsc_invoke_resource?" do +describe "Chef::Platform#supports_dsc_invoke_resource?", :windows_only do it "returns false if powershell is not present" do node = Chef::Node.new expect(Chef::Platform.supports_dsc_invoke_resource?(node)).to be_falsey @@ -39,12 +39,12 @@ describe "Chef::Platform#supports_dsc_invoke_resource?" do end end -describe "Chef::Platform#dsc_refresh_mode_disabled?" do +describe "Chef::Platform#dsc_refresh_mode_disabled?", :windows_only do let(:node) { instance_double("Chef::Node") } - let(:powershell) { instance_double("Chef::PowerShell") } + let(:powershell) { instance_double("ChefPowerShell::PowerShell") } it "returns true when RefreshMode is Disabled" do - expect(Chef::PowerShell).to receive(:new) + expect(ChefPowerShell::PowerShell).to receive(:new) .with("Get-DscLocalConfigurationManager") .and_return(powershell) expect(powershell).to receive(:error!) @@ -53,7 +53,7 @@ describe "Chef::Platform#dsc_refresh_mode_disabled?" do end it "returns false when RefreshMode is not Disabled" do - expect(Chef::PowerShell).to receive(:new) + expect(ChefPowerShell::PowerShell).to receive(:new) .with("Get-DscLocalConfigurationManager") .and_return(powershell) expect(powershell).to receive(:error!) diff --git a/spec/unit/util/dsc/local_configuration_manager_spec.rb b/spec/unit/util/dsc/local_configuration_manager_spec.rb index 8adf778949..9edd923d99 100644 --- a/spec/unit/util/dsc/local_configuration_manager_spec.rb +++ b/spec/unit/util/dsc/local_configuration_manager_spec.rb @@ -50,7 +50,7 @@ describe Chef::Util::DSC::LocalConfigurationManager do end let(:powershell) do - double("Chef::PowerShell", errors: lcm_errors, error?: !lcm_errors.empty?, result: lcm_result) + double("ChefPowerShell::PowerShell", errors: lcm_errors, error?: !lcm_errors.empty?, result: lcm_result) end describe "test_configuration method invocation" do @@ -185,7 +185,7 @@ describe Chef::Util::DSC::LocalConfigurationManager do context "when invalid dsc script is given" do it "raises exception" do configuration_document = "invalid-config" - expect { lcm.send(:run_configuration_cmdlet, configuration_document, true) }.to raise_error(Chef::PowerShell::CommandFailed) + expect { lcm.send(:run_configuration_cmdlet, configuration_document, true) }.to raise_error(ChefPowerShell::PowerShellExceptions::PowerShellCommandFailed) end end end -- cgit v1.2.1