summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam Edwards <adamed@opscode.com>2014-10-22 07:44:04 -0700
committerAdam Edwards <adamed@opscode.com>2014-10-22 07:44:04 -0700
commit89f7869eb031198302fba4611bb45ea380b5eae7 (patch)
tree1254c53f8ba4045b0bd5e05987f7cc64ecc309c4
parent04a3c18696f7ad2a857965ec7737ff85084290ed (diff)
downloadchef-adamedx/guard-interpreter-self.tar.gz
Add :self capability for guard_interpreter attribute and default for :batch, :powershell_script resourcesadamedx/guard-interpreter-self
-rw-r--r--lib/chef/guard_interpreter/resource_guard_interpreter.rb4
-rw-r--r--lib/chef/resource.rb17
-rw-r--r--lib/chef/resource/conditional.rb2
-rw-r--r--lib/chef/resource/windows_script.rb2
-rw-r--r--spec/functional/resource/powershell_spec.rb407
-rw-r--r--spec/support/shared/functional/windows_script.rb10
-rw-r--r--spec/support/shared/unit/windows_script_resource.rb2
7 files changed, 229 insertions, 215 deletions
diff --git a/lib/chef/guard_interpreter/resource_guard_interpreter.rb b/lib/chef/guard_interpreter/resource_guard_interpreter.rb
index 346b585d8c..f43c1739ca 100644
--- a/lib/chef/guard_interpreter/resource_guard_interpreter.rb
+++ b/lib/chef/guard_interpreter/resource_guard_interpreter.rb
@@ -82,7 +82,9 @@ class Chef
raise ArgumentError, "Node for guard resource parent must not be nil"
end
- resource_class = Chef::Resource.resource_for_node(parent_resource.guard_interpreter, parent_resource.node)
+ guard_resource = (parent_resource.guard_interpreter != :self) ? parent_resource.guard_interpreter : parent_resource.resource_name
+
+ resource_class = Chef::Resource.resource_for_node(guard_resource, parent_resource.node)
if resource_class.nil?
raise ArgumentError, "Specified guard_interpreter resource #{parent_resource.guard_interpreter.to_s} unknown for this platform"
diff --git a/lib/chef/resource.rb b/lib/chef/resource.rb
index e92ea28c69..ebb1f95d33 100644
--- a/lib/chef/resource.rb
+++ b/lib/chef/resource.rb
@@ -229,8 +229,6 @@ F
attr_reader :elapsed_time
- attr_reader :default_guard_interpreter
-
# Each notify entry is a resource/action pair, modeled as an
# Struct with a #resource and #action member
@@ -258,7 +256,6 @@ F
# interpreter. Therefore we store the default separately in a different
# attribute.
@guard_interpreter = nil
- @default_guard_interpreter = :default
@elapsed_time = 0
@sensitive = false
end
@@ -419,15 +416,11 @@ F
end
def guard_interpreter(arg=nil)
- if arg.nil?
- @guard_interpreter || @default_guard_interpreter
- else
- set_or_return(
- :guard_interpreter,
- arg,
- :kind_of => Symbol
- )
- end
+ set_or_return(
+ :guard_interpreter,
+ arg,
+ :kind_of => Symbol
+ )
end
# Sets up a notification from this resource to the resource specified by +resource_spec+.
diff --git a/lib/chef/resource/conditional.rb b/lib/chef/resource/conditional.rb
index 8960a4d57f..622e8b4ca5 100644
--- a/lib/chef/resource/conditional.rb
+++ b/lib/chef/resource/conditional.rb
@@ -62,7 +62,7 @@ class Chef
# We should have a block if we get here
# Check to see if the user set the guard_interpreter on the parent resource. Note that
# this error will not be raised when using the default_guard_interpreter
- if @parent_resource.guard_interpreter != @parent_resource.default_guard_interpreter
+ if @parent_resource.guard_interpreter != :default
msg = "#{@parent_resource.name} was given a guard_interpreter of #{@parent_resource.guard_interpreter}, "
msg << "but not given a command as a string. guard_interpreter does not support blocks (because they just contain ruby)."
raise ArgumentError, msg
diff --git a/lib/chef/resource/windows_script.rb b/lib/chef/resource/windows_script.rb
index 185424717b..7c50692466 100644
--- a/lib/chef/resource/windows_script.rb
+++ b/lib/chef/resource/windows_script.rb
@@ -31,7 +31,7 @@ class Chef
super(name, run_context)
@interpreter = interpreter_command
@resource_name = resource_name
- guard_interpreter resource_name
+ guard_interpreter :self
end
include Chef::Mixin::WindowsArchitectureHelper
diff --git a/spec/functional/resource/powershell_spec.rb b/spec/functional/resource/powershell_spec.rb
index a72a33d1a5..510fd045c8 100644
--- a/spec/functional/resource/powershell_spec.rb
+++ b/spec/functional/resource/powershell_spec.rb
@@ -221,227 +221,236 @@ describe Chef::Resource::WindowsScript::PowershellScript, :windows_only do
before(:each) do
resource.not_if.clear
resource.only_if.clear
- # resource.guard_interpreter should be :default by default
end
- it "evaluates a succeeding not_if block using cmd.exe as false by default" do
- 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.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.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.only_if "exit /b 2"
- resource.should_skip?(:run).should be_true
- end
-
- context "the only_if is specified before the guard" do
- before do
- # force the guard_interpreter to :default in case the default changes later
- resource.guard_interpreter :default
- end
-
- it "evaluates a powershell $true for a only_if block as true" do
- resource.only_if "$true"
- resource.guard_interpreter :powershell_script
- resource.should_skip?(:run).should be_false
- end
- end
-
- context "with powershell_script as the guard_interpreter" do
+ context "when the guard_interpreter's default value of :self is overridden to :default" do
before(:each) do
- resource.guard_interpreter :powershell_script
- 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 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
+ resource.guard_interpreter :default
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"
+ it "evaluates a succeeding not_if block using cmd.exe as false by default" do
+ resource.not_if "exit /b 0"
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"
+ it "evaluates a failing not_if block using cmd.exe as true by default" do
+ resource.not_if "exit /b 2"
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"
+ it "evaluates an succeeding only_if block using cmd.exe as true by default" do
+ resource.only_if "exit /b 0"
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"
+ it "evaluates a failing only_if block using cmd.exe as false by default" do
+ resource.only_if "exit /b 2"
resource.should_skip?(:run).should be_true
end
+ 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
+ context "the only_if is specified before the guard" do
+ before do
+ # force the guard_interpreter to :default in case the default changes later
+ resource.guard_interpreter :default
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'"
+ it "evaluates a powershell $true for a only_if block as true" do
+ resource.only_if "$true"
+ resource.guard_interpreter :powershell_script
resource.should_skip?(:run).should be_false
end
+ 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
+ context "the guard_interpeter is set to its default value" do
+
+ it "has a default value of :self" do
+ resource.guard_interpreter.should == :self
+ end
+
+ context "with powershell_script as the guard_interpreter" do
+
+ 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 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
end
end
diff --git a/spec/support/shared/functional/windows_script.rb b/spec/support/shared/functional/windows_script.rb
index 57bc8432b9..a3ee2d553d 100644
--- a/spec/support/shared/functional/windows_script.rb
+++ b/spec/support/shared/functional/windows_script.rb
@@ -120,6 +120,16 @@ shared_context Chef::Resource::WindowsScript do
end
end
+ context "when evaluating guards" do
+ it "has a default value of :self that results in a guard interpreter of the same class as the parent resource" do
+ resource.guard_interpreter.should == :self
+ resource.not_if "findstr.exe /thiscommandhasnonzeroexitstatus"
+ expect(Chef::Resource).to receive(:resource_for_node).and_call_original
+ expect(resource.class).to receive(:new).and_call_original
+ resource.should_skip?(:run).should be_false
+ end
+ end
+
context "when the architecture attribute is not set" do
let(:architecture) { nil }
it_behaves_like "a script resource with architecture attribute"
diff --git a/spec/support/shared/unit/windows_script_resource.rb b/spec/support/shared/unit/windows_script_resource.rb
index 837ac1a430..1463a55a6f 100644
--- a/spec/support/shared/unit/windows_script_resource.rb
+++ b/spec/support/shared/unit/windows_script_resource.rb
@@ -41,7 +41,7 @@ shared_examples_for "a Windows script resource" do
context "when evaluating guards" do
it "should default to using guard_interpreter attribute that is the same as the resource" do
- @resource.guard_interpreter.should == @resource.resource_name
+ @resource.guard_interpreter.should == :self
end
it "should use a resource to evaluate the guard when guard_interpreter is not specified" do