diff options
author | Claire McQuin <mcquin@users.noreply.github.com> | 2015-10-02 15:14:06 -0700 |
---|---|---|
committer | Claire McQuin <mcquin@users.noreply.github.com> | 2015-10-02 15:14:06 -0700 |
commit | 69cf21c8b35d7fb94775db377e810908e5fad54a (patch) | |
tree | c1b1df8585f8bf881c4ca4beaad0e118145298a6 | |
parent | 86a62b1d54c28bf5855c76b6404452fc6ebae43c (diff) | |
parent | e444aeb53a2e031b4c8e6300b380fe26c14e203c (diff) | |
download | chef-69cf21c8b35d7fb94775db377e810908e5fad54a.tar.gz |
Merge pull request #4016 from chef/mcquin/nano-ps-script
Use -Command flag on Nano
-rw-r--r-- | lib/chef/provider/powershell_script.rb | 8 | ||||
-rw-r--r-- | spec/functional/resource/powershell_script_spec.rb | 64 | ||||
-rw-r--r-- | spec/spec_helper.rb | 1 | ||||
-rw-r--r-- | spec/support/platform_helpers.rb | 5 | ||||
-rw-r--r-- | spec/support/shared/functional/windows_script.rb | 58 | ||||
-rw-r--r-- | spec/unit/provider/powershell_script_spec.rb | 68 |
6 files changed, 145 insertions, 59 deletions
diff --git a/lib/chef/provider/powershell_script.rb b/lib/chef/provider/powershell_script.rb index b876b6d8ee..91ce11c337 100644 --- a/lib/chef/provider/powershell_script.rb +++ b/lib/chef/provider/powershell_script.rb @@ -16,6 +16,7 @@ # limitations under the License. # +require 'chef/platform/query_helpers' require 'chef/provider/windows_script' class Chef @@ -49,7 +50,10 @@ class Chef # code -- otherwise, powershell.exe does not propagate the # error status of a failed Windows process that ran at the # end of the script, it gets changed to '1'. - interpreter_flags = [default_interpreter_flags, '-File'].join(' ') + # + # Nano only supports -Command + file_or_command = Chef::Platform.windows_nano_server? ? '-Command' : '-File' + interpreter_flags = [*default_interpreter_flags, file_or_command].join(' ') if ! (@new_resource.flags.nil?) interpreter_flags = [@new_resource.flags, interpreter_flags].join(' ') @@ -107,6 +111,8 @@ EOH end def default_interpreter_flags + return [] if Chef::Platform.windows_nano_server? + # Execution policy 'Bypass' is preferable since it doesn't require # user input confirmation for files such as PowerShell modules # downloaded from the Internet. However, 'Bypass' is not supported diff --git a/spec/functional/resource/powershell_script_spec.rb b/spec/functional/resource/powershell_script_spec.rb index be744e748b..ba0f867fcc 100644 --- a/spec/functional/resource/powershell_script_spec.rb +++ b/spec/functional/resource/powershell_script_spec.rb @@ -16,6 +16,7 @@ # limitations under the License. # +require 'chef/platform/query_helpers' require 'spec_helper' describe Chef::Resource::WindowsScript::PowershellScript, :windows_only do @@ -27,7 +28,6 @@ describe Chef::Resource::WindowsScript::PowershellScript, :windows_only do it_behaves_like "a Windows script running on Windows" - let(:successful_executable_script_content) { "#{ENV['SystemRoot']}\\system32\\attrib.exe $env:systemroot" } let(:failed_executable_script_content) { "#{ENV['SystemRoot']}\\system32\\attrib.exe /badargument" } let(:processor_architecture_script_content) { "echo $env:PROCESSOR_ARCHITECTURE" } @@ -57,6 +57,8 @@ describe Chef::Resource::WindowsScript::PowershellScript, :windows_only do end it "returns the exit status 27 for a powershell script that exits with 27" do + pending "powershell.exe always exits with 0 on nano" if Chef::Platform.windows_nano_server? + file = Tempfile.new(['foo', '.ps1']) begin file.write "exit 27" @@ -73,6 +75,8 @@ describe Chef::Resource::WindowsScript::PowershellScript, :windows_only do let (:negative_exit_status) { -27 } let (:unsigned_exit_status) { (-negative_exit_status ^ 65535) + 1 } it "returns the exit status -27 as a signed integer or an unsigned 16-bit 2's complement value of 65509 for a powershell script that exits with -27" do + pending "powershell.exe always exits with 0 on nano" if Chef::Platform.windows_nano_server? + # Versions of PowerShell prior to 4.0 return a 16-bit unsigned value -- # PowerShell 4.0 and later versions return a 32-bit signed value. file = Tempfile.new(['foo', '.ps1']) @@ -96,6 +100,8 @@ describe Chef::Resource::WindowsScript::PowershellScript, :windows_only do end it "returns the process exit code" do + pending "powershell.exe always exits with 0 on nano" if Chef::Platform.windows_nano_server? + resource.code(arbitrary_nonzero_process_exit_code_content) resource.returns(arbitrary_nonzero_process_exit_code) resource.run_action(:run) @@ -114,24 +120,34 @@ describe Chef::Resource::WindowsScript::PowershellScript, :windows_only do end it "returns 1 if the last command was a cmdlet that failed" do + pending "powershell.exe always exits with 0 on nano" if Chef::Platform.windows_nano_server? + resource.code(cmdlet_exit_code_not_found_content) resource.returns(1) resource.run_action(:run) end it "returns 1 if the last command was a cmdlet that failed and was preceded by a successfully executed non-cmdlet Windows binary" do + pending "powershell.exe always exits with 0 on nano" if Chef::Platform.windows_nano_server? + resource.code([windows_process_exit_code_success_content, cmdlet_exit_code_not_found_content].join(';')) resource.returns(1) expect { resource.run_action(:run) }.not_to raise_error end it "raises a Mixlib::ShellOut::ShellCommandFailed error if the script is not syntactically correct" do + pending "powershell.exe always exits with 0 on nano" if Chef::Platform.windows_nano_server? + resource.code('if({)') resource.returns(0) expect { resource.run_action(:run) }.to raise_error(Mixlib::ShellOut::ShellCommandFailed) end it "raises an error if the script is not syntactically correct even if returns is set to 1 which is what powershell.exe returns for syntactically invalid scripts" do + # This test fails because shell_out expects the exit status to be 1, but it is actually 0 + # The error is a false-positive. + skip "powershell.exe always exits with 0 on nano" if Chef::Platform.windows_nano_server? + resource.code('if({)') resource.returns(1) expect { resource.run_action(:run) }.to raise_error(Mixlib::ShellOut::ShellCommandFailed) @@ -146,24 +162,32 @@ describe Chef::Resource::WindowsScript::PowershellScript, :windows_only do # errors than 0 or 1, we return that instead, which is acceptable # since callers can test for nonzero rather than testing for 1. it "returns 1 if the last command was a cmdlet that failed and was preceded by an unsuccessfully executed non-cmdlet Windows binary" do + pending "powershell.exe always exits with 0 on nano" if Chef::Platform.windows_nano_server? + resource.code([arbitrary_nonzero_process_exit_code_content,cmdlet_exit_code_not_found_content].join(';')) resource.returns(arbitrary_nonzero_process_exit_code) resource.run_action(:run) end it "returns 0 if the last command was a non-cmdlet Windows binary that succeeded and was preceded by a failed cmdlet" do + pending "powershell.exe always exits with 0 on nano" if Chef::Platform.windows_nano_server? + resource.code([cmdlet_exit_code_success_content, arbitrary_nonzero_process_exit_code_content].join(';')) resource.returns(arbitrary_nonzero_process_exit_code) resource.run_action(:run) end it "returns a specific error code if the last command was a non-cmdlet Windows binary that failed and was preceded by cmdlet that succeeded" do + pending "powershell.exe always exits with 0 on nano" if Chef::Platform.windows_nano_server? + resource.code([cmdlet_exit_code_success_content, arbitrary_nonzero_process_exit_code_content].join(';')) resource.returns(arbitrary_nonzero_process_exit_code) resource.run_action(:run) end it "returns a specific error code if the last command was a non-cmdlet Windows binary that failed and was preceded by cmdlet that failed" do + pending "powershell.exe always exits with 0 on nano" if Chef::Platform.windows_nano_server? + resource.code([cmdlet_exit_code_not_found_content, arbitrary_nonzero_process_exit_code_content].join(';')) resource.returns(arbitrary_nonzero_process_exit_code) resource.run_action(:run) @@ -182,6 +206,8 @@ describe Chef::Resource::WindowsScript::PowershellScript, :windows_only do end it "returns 1 for $false as the last line of the script when convert_boolean_return is true" do + pending "powershell.exe always exits with 0 on nano" if Chef::Platform.windows_nano_server? + resource.convert_boolean_return true resource.code "$false" resource.returns(1) @@ -208,6 +234,8 @@ describe Chef::Resource::WindowsScript::PowershellScript, :windows_only do end it "returns 1 if an invalid flag is passed to the interpreter" do + pending "powershell.exe always exits with 0 on nano" if Chef::Platform.windows_nano_server? + resource.code(cmdlet_exit_code_success_content) resource.flags(invalid_powershell_interpreter_flag) resource.returns(1) @@ -287,6 +315,8 @@ configuration LCM end it "executes a script with a 32-bit process if :i386 arch is specified" do + pending "executing scripts with a 32-bit process should raise an error on nano" if Chef::Platform.windows_nano_server? + resource.code(processor_architecture_script_content + " | out-file -encoding ASCII #{script_output_path}") resource.architecture(:i386) resource.returns(0) @@ -347,6 +377,8 @@ configuration LCM end it "evaluates a powershell $false for a not_if block as true" do + pending "powershell.exe always exits with $true on nano" if Chef::Platform.windows_nano_server? + resource.not_if "$false" expect(resource.should_skip?(:run)).to be_falsey end @@ -357,6 +389,8 @@ configuration LCM end it "evaluates a powershell $false for an only_if block as false" do + pending "powershell.exe always exits with $true on nano" if Chef::Platform.windows_nano_server? + resource.only_if "$false" expect(resource.should_skip?(:run)).to be_truthy end @@ -377,6 +411,8 @@ configuration LCM end it "evaluates a non-zero powershell exit status for not_if as true" do + pending "powershell.exe always exits with 0 on nano" if Chef::Platform.windows_nano_server? + resource.not_if "exit 37" expect(resource.should_skip?(:run)).to be_falsey end @@ -387,6 +423,8 @@ configuration LCM end it "evaluates a failed executable exit status for not_if as false" do + pending "powershell.exe always exits with success on nano" if Chef::Platform.windows_nano_server? + resource.not_if windows_process_exit_code_not_found_content expect(resource.should_skip?(:run)).to be_falsey end @@ -397,6 +435,8 @@ configuration LCM end it "evaluates a failed executable exit status for only_if as false" do + pending "powershell.exe always exits with success on nano" if Chef::Platform.windows_nano_server? + resource.only_if windows_process_exit_code_not_found_content expect(resource.should_skip?(:run)).to be_truthy end @@ -407,6 +447,8 @@ configuration LCM end it "evaluates a failed cmdlet exit status for not_if as true" do + pending "powershell.exe always exits with success on nano" if Chef::Platform.windows_nano_server? + resource.not_if "throw 'up'" expect(resource.should_skip?(:run)).to be_falsey end @@ -417,6 +459,8 @@ configuration LCM end it "evaluates a failed cmdlet exit status for only_if as false" do + pending "powershell.exe always exits with success on nano" if Chef::Platform.windows_nano_server? + resource.only_if "throw 'up'" expect(resource.should_skip?(:run)).to be_truthy end @@ -459,30 +503,36 @@ configuration LCM end it "evaluates a 64-bit resource with a 64-bit guard and interprets boolean true as nonzero status code", :windows64_only do + pending "powershell.exe always exits with 0 on nano" if Chef::Platform.windows_nano_server? + resource.architecture :x86_64 resource.only_if "exit [int32]($env:PROCESSOR_ARCHITECTURE -eq 'AMD64')" expect(resource.should_skip?(:run)).to be_truthy end - it "evaluates a 32-bit resource with a 32-bit guard and interprets boolean false as zero status code" do + it "evaluates a 32-bit resource with a 32-bit guard and interprets boolean false as zero status code", :not_supported_on_nano do resource.architecture :i386 resource.only_if "exit [int32]($env:PROCESSOR_ARCHITECTURE -ne 'X86')" expect(resource.should_skip?(:run)).to be_falsey end - it "evaluates a 32-bit resource with a 32-bit guard and interprets boolean true as nonzero status code" do + it "evaluates a 32-bit resource with a 32-bit guard and interprets boolean true as nonzero status code", :not_supported_on_nano do resource.architecture :i386 resource.only_if "exit [int32]($env:PROCESSOR_ARCHITECTURE -eq 'X86')" expect(resource.should_skip?(:run)).to be_truthy end it "evaluates a simple boolean false as nonzero status code when convert_boolean_return is true for only_if" do + pending "powershell.exe always exits with 0 on nano" if Chef::Platform.windows_nano_server? + resource.convert_boolean_return true resource.only_if "$false" expect(resource.should_skip?(:run)).to be_truthy end it "evaluates a simple boolean false as nonzero status code when convert_boolean_return is true for not_if" do + pending "powershell.exe always exits with 0 on nano" if Chef::Platform.windows_nano_server? + resource.convert_boolean_return true resource.not_if "$false" expect(resource.should_skip?(:run)).to be_falsey @@ -500,28 +550,28 @@ configuration LCM expect(resource.should_skip?(:run)).to be_truthy end - it "evaluates a 32-bit resource with a 32-bit guard and interprets boolean false as zero status code using convert_boolean_return for only_if" do + it "evaluates a 32-bit resource with a 32-bit guard and interprets boolean false as zero status code using convert_boolean_return for only_if", :not_supported_on_nano do resource.convert_boolean_return true resource.architecture :i386 resource.only_if "$env:PROCESSOR_ARCHITECTURE -eq 'X86'" expect(resource.should_skip?(:run)).to be_falsey end - it "evaluates a 32-bit resource with a 32-bit guard and interprets boolean false as zero status code using convert_boolean_return for not_if" do + it "evaluates a 32-bit resource with a 32-bit guard and interprets boolean false as zero status code using convert_boolean_return for not_if", :not_supported_on_nano do resource.convert_boolean_return true resource.architecture :i386 resource.not_if "$env:PROCESSOR_ARCHITECTURE -ne 'X86'" expect(resource.should_skip?(:run)).to be_falsey end - it "evaluates a 32-bit resource with a 32-bit guard and interprets boolean true as nonzero status code using convert_boolean_return for only_if" do + it "evaluates a 32-bit resource with a 32-bit guard and interprets boolean true as nonzero status code using convert_boolean_return for only_if", :not_supported_on_nano do resource.convert_boolean_return true resource.architecture :i386 resource.only_if "$env:PROCESSOR_ARCHITECTURE -ne 'X86'" expect(resource.should_skip?(:run)).to be_truthy end - it "evaluates a 32-bit resource with a 32-bit guard and interprets boolean true as nonzero status code using convert_boolean_return for not_if" do + it "evaluates a 32-bit resource with a 32-bit guard and interprets boolean true as nonzero status code using convert_boolean_return for not_if", :not_supported_on_nano do resource.convert_boolean_return true resource.architecture :i386 resource.not_if "$env:PROCESSOR_ARCHITECTURE -eq 'X86'" diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 92a4daf6d5..3a1d116d79 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -128,6 +128,7 @@ RSpec.configure do |config| config.filter_run_excluding :mac_osx_only=> true if !mac_osx? config.filter_run_excluding :not_supported_on_win2k3 => true if windows_win2k3? config.filter_run_excluding :not_supported_on_solaris => true if solaris? + config.filter_run_excluding :not_supported_on_nano => true if windows_nano_server? config.filter_run_excluding :win2k3_only => true unless windows_win2k3? config.filter_run_excluding :windows_2008r2_or_later => true unless windows_2008r2_or_later? config.filter_run_excluding :windows64_only => true unless windows64? diff --git a/spec/support/platform_helpers.rb b/spec/support/platform_helpers.rb index 1cfad05172..9c6c3fdf72 100644 --- a/spec/support/platform_helpers.rb +++ b/spec/support/platform_helpers.rb @@ -83,6 +83,11 @@ def windows_powershell_dsc? supports_dsc end +def windows_nano_server? + require 'chef/platform/query_helpers' + Chef::Platform.windows_nano_server? +end + def mac_osx_106? if File.exists? "/usr/bin/sw_vers" result = ShellHelpers.shell_out("/usr/bin/sw_vers") diff --git a/spec/support/shared/functional/windows_script.rb b/spec/support/shared/functional/windows_script.rb index 3499cc98ec..b6fc2a98ad 100644 --- a/spec/support/shared/functional/windows_script.rb +++ b/spec/support/shared/functional/windows_script.rb @@ -19,14 +19,15 @@ # Shared context used by both Powershell and Batch script provider # tests. +require 'chef/platform/query_helpers' + shared_context Chef::Resource::WindowsScript do before(:all) do - - ohai_reader = Ohai::System.new - ohai_reader.all_plugins("platform") + @ohai_reader = Ohai::System.new + @ohai_reader.all_plugins(["platform", "kernel"]) new_node = Chef::Node.new - new_node.consume_external_attrs(ohai_reader.data,{}) + new_node.consume_external_attrs(@ohai_reader.data,{}) events = Chef::EventDispatch::Dispatcher.new @@ -51,12 +52,11 @@ shared_context Chef::Resource::WindowsScript do shared_examples_for "a script resource with architecture attribute" do context "with the given architecture attribute value" do - let(:resource_architecture) { architecture } let(:expected_architecture) do - if architecture - expected_architecture = architecture + if resource_architecture + expected_architecture = resource_architecture else - expected_architecture = :i386 + expected_architecture = @ohai_reader.data['kernel']['machine'].to_sym end end let(:expected_architecture_output) do @@ -77,16 +77,16 @@ shared_context Chef::Resource::WindowsScript do before(:each) do resource.code resource_command - (resource.architecture architecture) if architecture + (resource.architecture resource_architecture) if resource_architecture resource.returns(0) end - it "should create a process with the expected architecture" do + it "creates a process with the expected architecture" do resource.run_action(:run) expect(get_process_architecture).to eq(expected_architecture_output.downcase) end - it "should execute guards with the same architecture as the resource" do + it "executes guards with the same architecture as the resource" do resource.only_if resource_guard_command resource.run_action(:run) expect(get_process_architecture).to eq(expected_architecture_output.downcase) @@ -94,18 +94,24 @@ shared_context Chef::Resource::WindowsScript do expect(get_guard_process_architecture).to eq(get_process_architecture) end - let (:architecture) { :x86_64 } - it "should execute a 64-bit guard if the guard's architecture is specified as 64-bit", :windows64_only do - resource.only_if resource_guard_command, :architecture => :x86_64 - resource.run_action(:run) - expect(get_guard_process_architecture).to eq('amd64') + context "when the guard's architecture is specified as 64-bit" do + let (:guard_architecture) { :x86_64 } + it "executes a 64-bit guard", :windows64_only do + resource.only_if resource_guard_command, :architecture => guard_architecture + resource.run_action(:run) + expect(get_guard_process_architecture).to eq('amd64') + end end - let (:architecture) { :i386 } - it "should execute a 32-bit guard if the guard's architecture is specified as 32-bit" do - resource.only_if resource_guard_command, :architecture => :i386 - resource.run_action(:run) - expect(get_guard_process_architecture).to eq('x86') + context "when the guard's architecture is specified as 32-bit" do + let (:guard_architecture) { :i386 } + it "executes a 32-bit guard" do + pending "executing scripts with a 32-bit process should raise an error on nano" if Chef::Platform.windows_nano_server? + + resource.only_if resource_guard_command, :architecture => guard_architecture + resource.run_action(:run) + expect(get_guard_process_architecture).to eq('x86') + end end end end @@ -122,6 +128,8 @@ shared_context Chef::Resource::WindowsScript do context "when evaluating guards" do it "has a guard_interpreter attribute set to the short name of the resource" do + pending "powershell.exe always exits with 0 on nano" if Chef::Platform.windows_nano_server? + expect(resource.guard_interpreter).to eq(resource.resource_name) resource.not_if "findstr.exe /thiscommandhasnonzeroexitstatus" expect(Chef::Resource).to receive(:resource_for_node).and_call_original @@ -131,17 +139,17 @@ shared_context Chef::Resource::WindowsScript do end context "when the architecture attribute is not set" do - let(:architecture) { nil } + let(:resource_architecture) { nil } it_behaves_like "a script resource with architecture attribute" end - context "when the architecture attribute is :i386" do - let(:architecture) { :i386 } + context "when the architecture attribute is :i386", :not_supported_on_nano do + let(:resource_architecture) { :i386 } it_behaves_like "a script resource with architecture attribute" end context "when the architecture attribute is :x86_64" do - let(:architecture) { :x86_64 } + let(:resource_architecture) { :x86_64 } it_behaves_like "a script resource with architecture attribute" end end diff --git a/spec/unit/provider/powershell_script_spec.rb b/spec/unit/provider/powershell_script_spec.rb index 855c18af9b..d06d2762be 100644 --- a/spec/unit/provider/powershell_script_spec.rb +++ b/spec/unit/provider/powershell_script_spec.rb @@ -38,41 +38,57 @@ describe Chef::Provider::PowershellScript, "action_run" do } context 'when setting interpreter flags' do - it "should set the -File flag as the last flag" do - expect(provider.flags.split(' ').pop).to eq("-File") + context 'on nano' do + before(:each) do + allow(Chef::Platform).to receive(:windows_nano_server?).and_return(true) + end + + it "sets the -Command flag as the last flag" do + expect(provider.flags.split(' ').pop).to eq("-Command") + end end - let(:execution_policy_flag) do - execution_policy_index = 0 - provider_flags = provider.flags.split(' ') - execution_policy_specified = false + context 'not on nano' do + before(:each) do + allow(Chef::Platform).to receive(:windows_nano_server?).and_return(false) + end - provider_flags.find do | value | - execution_policy_index += 1 - execution_policy_specified = value.downcase == '-ExecutionPolicy'.downcase + it "sets the -File flag as the last flag" do + expect(provider.flags.split(' ').pop).to eq("-File") end - execution_policy = execution_policy_specified ? provider_flags[execution_policy_index] : nil - end + let(:execution_policy_flag) do + execution_policy_index = 0 + provider_flags = provider.flags.split(' ') + execution_policy_specified = false - context 'when running with an unspecified PowerShell version' do - let(:powershell_version) { nil } - it "should set the -ExecutionPolicy flag to 'Unrestricted' by default" do - expect(execution_policy_flag.downcase).to eq('unrestricted'.downcase) + provider_flags.find do | value | + execution_policy_index += 1 + execution_policy_specified = value.downcase == '-ExecutionPolicy'.downcase + end + + execution_policy = execution_policy_specified ? provider_flags[execution_policy_index] : nil + end + + context 'when running with an unspecified PowerShell version' do + let(:powershell_version) { nil } + it "sets the -ExecutionPolicy flag to 'Unrestricted' by default" do + expect(execution_policy_flag.downcase).to eq('unrestricted'.downcase) + end end - end - { '2.0' => 'Unrestricted', - '2.5' => 'Unrestricted', - '3.0' => 'Bypass', - '3.6' => 'Bypass', - '4.0' => 'Bypass', - '5.0' => 'Bypass' }.each do | version_policy | - let(:powershell_version) { version_policy[0].to_f } - context "when running PowerShell version #{version_policy[0]}" do + { '2.0' => 'Unrestricted', + '2.5' => 'Unrestricted', + '3.0' => 'Bypass', + '3.6' => 'Bypass', + '4.0' => 'Bypass', + '5.0' => 'Bypass' }.each do | version_policy | let(:powershell_version) { version_policy[0].to_f } - it "should set the -ExecutionPolicy flag to '#{version_policy[1]}'" do - expect(execution_policy_flag.downcase).to eq(version_policy[1].downcase) + context "when running PowerShell version #{version_policy[0]}" do + let(:powershell_version) { version_policy[0].to_f } + it "sets the -ExecutionPolicy flag to '#{version_policy[1]}'" do + expect(execution_policy_flag.downcase).to eq(version_policy[1].downcase) + end end end end |