summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClaire McQuin <claire@getchef.com>2015-10-01 11:53:17 -0700
committerClaire McQuin <claire@getchef.com>2015-10-01 15:42:36 -0700
commite03535f7e0e239e4f53bc72b057c2fa268970863 (patch)
tree9580117a8e200a713c73309373add86542c16873
parent8d32fdd4377476a9f7dc36a864ccdaa17c32b3a1 (diff)
downloadchef-e03535f7e0e239e4f53bc72b057c2fa268970863.tar.gz
Use -Command flag on Nano
-rw-r--r--lib/chef/provider/powershell_script.rb8
-rw-r--r--spec/functional/resource/powershell_script_spec.rb64
-rw-r--r--spec/support/shared/functional/windows_script.rb22
-rw-r--r--spec/unit/provider/powershell_script_spec.rb68
4 files changed, 130 insertions, 32 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..0d298a5632 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,40 @@ 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
+ pending "executing scripts with a 32-bit process should raise an error on nano" if Chef::Platform.windows_nano_server?
+
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
+ skip "executing scripts with a 32-bit process should raise an error on nano" if Chef::Platform.windows_nano_server?
+
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
@@ -501,6 +555,8 @@ configuration LCM
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
+ pending "executing scripts with a 32-bit process should raise an error on nano" if Chef::Platform.windows_nano_server?
+
resource.convert_boolean_return true
resource.architecture :i386
resource.only_if "$env:PROCESSOR_ARCHITECTURE -eq 'X86'"
@@ -508,6 +564,8 @@ configuration LCM
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
+ skip "executing scripts with a 32-bit process should raise an error on nano" if Chef::Platform.windows_nano_server?
+
resource.convert_boolean_return true
resource.architecture :i386
resource.not_if "$env:PROCESSOR_ARCHITECTURE -ne 'X86'"
@@ -515,6 +573,8 @@ configuration LCM
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
+ skip "executing scripts with a 32-bit process should raise an error on nano" if Chef::Platform.windows_nano_server?
+
resource.convert_boolean_return true
resource.architecture :i386
resource.only_if "$env:PROCESSOR_ARCHITECTURE -ne 'X86'"
@@ -522,6 +582,8 @@ configuration LCM
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
+ pending "executing scripts with a 32-bit process should raise an error on nano" if Chef::Platform.windows_nano_server?
+
resource.convert_boolean_return true
resource.architecture :i386
resource.not_if "$env:PROCESSOR_ARCHITECTURE -eq 'X86'"
diff --git a/spec/support/shared/functional/windows_script.rb b/spec/support/shared/functional/windows_script.rb
index 3499cc98ec..5f056a2139 100644
--- a/spec/support/shared/functional/windows_script.rb
+++ b/spec/support/shared/functional/windows_script.rb
@@ -19,6 +19,8 @@
# 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
@@ -52,6 +54,8 @@ 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 }
+ # TODO: Why does expected_architecture default to :i386 and not
+ # the machine architecture?
let(:expected_architecture) do
if architecture
expected_architecture = architecture
@@ -81,12 +85,16 @@ shared_context Chef::Resource::WindowsScript do
resource.returns(0)
end
- it "should create a process with the expected architecture" do
+ it "creates a process with the expected architecture" do
+ pending "executing scripts with a 32-bit process should raise an error on nano" if Chef::Platform.windows_nano_server? && expected_architecture == :i386
+
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
+ pending "executing scripts with a 32-bit process should raise an error on nano" if Chef::Platform.windows_nano_server? && expected_architecture == :i386
+
resource.only_if resource_guard_command
resource.run_action(:run)
expect(get_process_architecture).to eq(expected_architecture_output.downcase)
@@ -95,14 +103,18 @@ shared_context Chef::Resource::WindowsScript do
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
+ it "executes a 64-bit guard if the guard's architecture is specified as 64-bit", :windows64_only do
+ pending "executing scripts with a 32-bit process should raise an error on nano" if Chef::Platform.windows_nano_server? && expected_architecture == :i386
+
resource.only_if resource_guard_command, :architecture => :x86_64
resource.run_action(:run)
expect(get_guard_process_architecture).to eq('amd64')
end
let (:architecture) { :i386 }
- it "should execute a 32-bit guard if the guard's architecture is specified as 32-bit" do
+ it "executes a 32-bit guard if the guard's architecture is specified as 32-bit" do
+ pending "executing scripts with a 32-bit process should raise an error on nano" if Chef::Platform.windows_nano_server? && expected_architecture == :i386
+
resource.only_if resource_guard_command, :architecture => :i386
resource.run_action(:run)
expect(get_guard_process_architecture).to eq('x86')
@@ -122,6 +134,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
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