diff options
Diffstat (limited to 'spec/functional/resource/powershell_spec.rb')
-rw-r--r-- | spec/functional/resource/powershell_spec.rb | 263 |
1 files changed, 262 insertions, 1 deletions
diff --git a/spec/functional/resource/powershell_spec.rb b/spec/functional/resource/powershell_spec.rb index 6bd3b3c1e5..5001e870a9 100644 --- a/spec/functional/resource/powershell_spec.rb +++ b/spec/functional/resource/powershell_spec.rb @@ -84,7 +84,7 @@ describe Chef::Resource::WindowsScript::PowershellScript, :windows_only do # last line executed -- in this case, we return the status of the # second to last line. This happens because Powershell gives no # way for us to determine whether the last operation was a cmdlet - # or Windows process. Because the latter gives more specified + # or Windows process. Because the latter gives more specific # 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 @@ -111,6 +111,32 @@ describe Chef::Resource::WindowsScript::PowershellScript, :windows_only do resource.run_action(:run) end + it "returns 0 for $false as the last line of the script when convert_boolean_return is false" do + resource.code "$false" + resource.returns(0) + resource.run_action(:run) + end + + it "returns 0 for $true as the last line of the script when convert_boolean_return is false" do + resource.code "$true" + resource.returns(0) + resource.run_action(:run) + end + + it "returns 1 for $false as the last line of the script when convert_boolean_return is true" do + resource.convert_boolean_return true + resource.code "$false" + resource.returns(1) + resource.run_action(:run) + end + + it "returns 0 for $true as the last line of the script when convert_boolean_return is true" do + resource.convert_boolean_return true + resource.code "$true" + resource.returns(0) + resource.run_action(:run) + end + it "executes a script with a 64-bit process on a 64-bit OS, otherwise a 32-bit process" do resource.code(processor_architecture_script_content + " | out-file -encoding ASCII #{script_output_path}") resource.returns(0) @@ -177,6 +203,241 @@ describe Chef::Resource::WindowsScript::PowershellScript, :windows_only do end end + describe "when executing guards" do + + before(:each) do + resource.not_if.clear + resource.only_if.clear + resource.guard_interpreter :powershell_script + end + + it "evaluates a succeeding not_if block using cmd.exe as false by default" do + resource.guard_interpreter :default + resource.not_if "exit /b 0" + resource.should_skip?(:run).should be_true + end + + it "evaluates a failing not_if block using cmd.exe as true by default" do + resource.guard_interpreter :default + resource.not_if "exit /b 2" + resource.should_skip?(:run).should be_false + end + + it "evaluates an succeeding only_if block using cmd.exe as true by default" do + resource.guard_interpreter :default + resource.only_if "exit /b 0" + resource.should_skip?(:run).should be_false + end + + it "evaluates a failing only_if block using cmd.exe as false by default" do + resource.guard_interpreter :default + resource.only_if "exit /b 2" + resource.should_skip?(:run).should be_true + end + + it "evaluates a powershell $false for a not_if block as true" do + resource.not_if "$false" + resource.should_skip?(:run).should be_false + end + + it "evaluates a powershell $true for a not_if block as false" do + resource.not_if "$true" + resource.should_skip?(:run).should be_true + end + + it "evaluates a powershell $false for an only_if block as false" do + resource.only_if "$false" + resource.should_skip?(:run).should be_true + end + + it "evaluates a powershell $true for a only_if block as true" do + resource.only_if "$true" + resource.should_skip?(:run).should be_false + end + + it "evaluates a not_if block using powershell.exe" do + resource.not_if "exit([int32](![System.Environment]::CommandLine.Contains('powershell.exe')))" + resource.should_skip?(:run).should be_true + end + + it "evaluates an only_if block using powershell.exe" do + resource.only_if "exit([int32](![System.Environment]::CommandLine.Contains('powershell.exe')))" + resource.should_skip?(:run).should be_false + end + + it "evaluates a not_if block as false" do + resource.not_if { false } + resource.should_skip?(:run).should be_false + end + + it "evaluates a not_if block as true" do + resource.not_if { true } + resource.should_skip?(:run).should be_true + end + + it "evaluates an only_if block as false" do + resource.only_if { false } + resource.should_skip?(:run).should be_true + end + + it "evaluates an only_if block as true" do + resource.only_if { true } + resource.should_skip?(:run).should be_false + end + + it "evaluates a non-zero powershell exit status for not_if as true" do + resource.not_if "exit 37" + resource.should_skip?(:run).should be_false + end + + it "evaluates a zero powershell exit status for not_if as false" do + resource.not_if "exit 0" + resource.should_skip?(:run).should be_true + end + + it "evaluates a failed executable exit status for not_if as false" do + resource.not_if windows_process_exit_code_not_found_content + resource.should_skip?(:run).should be_false + end + + it "evaluates a successful executable exit status for not_if as true" do + resource.not_if windows_process_exit_code_success_content + resource.should_skip?(:run).should be_true + end + + it "evaluates a failed executable exit status for only_if as false" do + resource.only_if windows_process_exit_code_not_found_content + resource.should_skip?(:run).should be_true + end + + it "evaluates a successful executable exit status for only_if as true" do + resource.only_if windows_process_exit_code_success_content + resource.should_skip?(:run).should be_false + end + + it "evaluates a failed cmdlet exit status for not_if as true" do + resource.not_if "throw 'up'" + resource.should_skip?(:run).should be_false + end + + it "evaluates a successful cmdlet exit status for not_if as true" do + resource.not_if "cd ." + resource.should_skip?(:run).should be_true + end + + it "evaluates a failed cmdlet exit status for only_if as false" do + resource.only_if "throw 'up'" + resource.should_skip?(:run).should be_true + end + + it "evaluates a successful cmdlet exit status for only_if as true" do + resource.only_if "cd ." + resource.should_skip?(:run).should be_false + end + + it "evaluates a not_if block using the cwd guard parameter" do + custom_cwd = "#{ENV['SystemRoot']}\\system32\\drivers\\etc" + resource.not_if "exit ! [int32]($pwd.path -eq '#{custom_cwd}')", :cwd => custom_cwd + resource.should_skip?(:run).should be_true + end + + it "evaluates an only_if block using the cwd guard parameter" do + custom_cwd = "#{ENV['SystemRoot']}\\system32\\drivers\\etc" + resource.only_if "exit ! [int32]($pwd.path -eq '#{custom_cwd}')", :cwd => custom_cwd + resource.should_skip?(:run).should be_false + end + + it "inherits cwd from the parent resource for only_if" do + custom_cwd = "#{ENV['SystemRoot']}\\system32\\drivers\\etc" + resource.cwd custom_cwd + resource.only_if "exit ! [int32]($pwd.path -eq '#{custom_cwd}')" + resource.should_skip?(:run).should be_false + end + + it "inherits cwd from the parent resource for not_if" do + custom_cwd = "#{ENV['SystemRoot']}\\system32\\drivers\\etc" + resource.cwd custom_cwd + resource.not_if "exit ! [int32]($pwd.path -eq '#{custom_cwd}')" + resource.should_skip?(:run).should be_true + end + + it "evaluates a 64-bit resource with a 64-bit guard and interprets boolean false as zero status code", :windows64_only do + resource.architecture :x86_64 + resource.only_if "exit [int32]($env:PROCESSOR_ARCHITECTURE -ne 'AMD64')" + resource.should_skip?(:run).should be_false + end + + it "evaluates a 64-bit resource with a 64-bit guard and interprets boolean true as nonzero status code", :windows64_only do + resource.architecture :x86_64 + resource.only_if "exit [int32]($env:PROCESSOR_ARCHITECTURE -eq 'AMD64')" + resource.should_skip?(:run).should be_true + end + + it "evaluates a 32-bit resource with a 32-bit guard and interprets boolean false as zero status code" do + resource.architecture :i386 + resource.only_if "exit [int32]($env:PROCESSOR_ARCHITECTURE -ne 'X86')" + resource.should_skip?(:run).should be_false + end + + it "evaluates a 32-bit resource with a 32-bit guard and interprets boolean true as nonzero status code" do + resource.architecture :i386 + resource.only_if "exit [int32]($env:PROCESSOR_ARCHITECTURE -eq 'X86')" + resource.should_skip?(:run).should be_true + end + + it "evaluates a simple boolean false as nonzero status code when convert_boolean_return is true for only_if" do + resource.convert_boolean_return true + resource.only_if "$false" + resource.should_skip?(:run).should be_true + end + + it "evaluates a simple boolean false as nonzero status code when convert_boolean_return is true for not_if" do + resource.convert_boolean_return true + resource.not_if "$false" + resource.should_skip?(:run).should be_false + end + + it "evaluates a simple boolean true as 0 status code when convert_boolean_return is true for only_if" do + resource.convert_boolean_return true + resource.only_if "$true" + resource.should_skip?(:run).should be_false + end + + it "evaluates a simple boolean true as 0 status code when convert_boolean_return is true for not_if" do + resource.convert_boolean_return true + resource.not_if "$true" + resource.should_skip?(:run).should be_true + 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 + resource.convert_boolean_return true + resource.architecture :i386 + resource.only_if "$env:PROCESSOR_ARCHITECTURE -eq 'X86'" + resource.should_skip?(:run).should be_false + 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 + resource.convert_boolean_return true + resource.architecture :i386 + resource.not_if "$env:PROCESSOR_ARCHITECTURE -ne 'X86'" + resource.should_skip?(:run).should be_false + 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 + resource.convert_boolean_return true + resource.architecture :i386 + resource.only_if "$env:PROCESSOR_ARCHITECTURE -ne 'X86'" + resource.should_skip?(:run).should be_true + 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 + resource.convert_boolean_return true + resource.architecture :i386 + resource.not_if "$env:PROCESSOR_ARCHITECTURE -eq 'X86'" + resource.should_skip?(:run).should be_true + end + end + def get_script_output script_output = File.read(script_output_path) end |