summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam Edwards <adamed@opscode.com>2014-06-14 20:59:44 -0700
committerAdam Edwards <adamed@opscode.com>2014-10-21 20:58:03 -0700
commit42dce171576d5a7ebf0d0e7c39b30307e4553726 (patch)
tree7de1f7640a0ab7212677fd5c90617d3840d2a4b0
parentd2c94417f1cf617c47bd5eed3afb8db6b1208809 (diff)
downloadchef-42dce171576d5a7ebf0d0e7c39b30307e4553726.tar.gz
powershell_script and batch resource guard_interpeter default to parent resource
-rw-r--r--lib/chef/resource/powershell_script.rb2
-rw-r--r--lib/chef/resource/windows_script.rb3
-rw-r--r--spec/functional/resource/batch_spec.rb15
-rw-r--r--spec/functional/resource/powershell_spec.rb7
-rw-r--r--spec/support/shared/functional/windows_script.rb108
-rw-r--r--spec/support/shared/unit/script_resource.rb2
-rw-r--r--spec/support/shared/unit/windows_script_resource.rb20
7 files changed, 141 insertions, 16 deletions
diff --git a/lib/chef/resource/powershell_script.rb b/lib/chef/resource/powershell_script.rb
index 1b47e7411a..a88fb5701b 100644
--- a/lib/chef/resource/powershell_script.rb
+++ b/lib/chef/resource/powershell_script.rb
@@ -21,8 +21,6 @@ class Chef
class Resource
class PowershellScript < Chef::Resource::WindowsScript
- set_guard_inherited_attributes(:architecture)
-
def initialize(name, run_context=nil)
super(name, run_context, :powershell_script, "powershell.exe")
@convert_boolean_return = false
diff --git a/lib/chef/resource/windows_script.rb b/lib/chef/resource/windows_script.rb
index 108891e9ba..185424717b 100644
--- a/lib/chef/resource/windows_script.rb
+++ b/lib/chef/resource/windows_script.rb
@@ -23,12 +23,15 @@ class Chef
class Resource
class WindowsScript < Chef::Resource::Script
+ set_guard_inherited_attributes(:architecture)
+
protected
def initialize(name, run_context, resource_name, interpreter_command)
super(name, run_context)
@interpreter = interpreter_command
@resource_name = resource_name
+ guard_interpreter resource_name
end
include Chef::Mixin::WindowsArchitectureHelper
diff --git a/spec/functional/resource/batch_spec.rb b/spec/functional/resource/batch_spec.rb
index baa01ee5d9..39133fd40b 100644
--- a/spec/functional/resource/batch_spec.rb
+++ b/spec/functional/resource/batch_spec.rb
@@ -21,17 +21,10 @@ require 'spec_helper'
describe Chef::Resource::WindowsScript::Batch, :windows_only do
include_context Chef::Resource::WindowsScript
- let(:script_content) { "whoami" }
+ let(:output_command) { ' > ' }
- let!(:resource) do
- Chef::Resource::WindowsScript::Batch.new("Batch resource functional test", @run_context)
- end
+ let (:architecture_command) { '@echo %PROCESSOR_ARCHITECTURE%' }
+
+ it_behaves_like "a Windows script running on Windows"
- describe "when the run action is invoked on Windows" do
- it "executes the script code" do
- resource.code(script_content + " > #{script_output_path}")
- resource.returns(0)
- resource.run_action(:run)
- end
- end
end
diff --git a/spec/functional/resource/powershell_spec.rb b/spec/functional/resource/powershell_spec.rb
index 96a356f441..a72a33d1a5 100644
--- a/spec/functional/resource/powershell_spec.rb
+++ b/spec/functional/resource/powershell_spec.rb
@@ -22,6 +22,12 @@ describe Chef::Resource::WindowsScript::PowershellScript, :windows_only do
include_context Chef::Resource::WindowsScript
+ let (:architecture_command) { 'echo $env:PROCESSOR_ARCHITECTURE' }
+ let (:output_command) { ' | out-file -encoding ASCII ' }
+
+ 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" }
@@ -36,6 +42,7 @@ describe Chef::Resource::WindowsScript::PowershellScript, :windows_only do
let(:arbitrary_nonzero_process_exit_code_content) { "exit #{arbitrary_nonzero_process_exit_code}" }
let(:invalid_powershell_interpreter_flag) { "/thisflagisinvalid" }
let(:valid_powershell_interpreter_flag) { "-Sta" }
+
let!(:resource) do
r = Chef::Resource::WindowsScript::PowershellScript.new("Powershell resource functional test", @run_context)
r.code(successful_executable_script_content)
diff --git a/spec/support/shared/functional/windows_script.rb b/spec/support/shared/functional/windows_script.rb
index fc06fb55d0..57bc8432b9 100644
--- a/spec/support/shared/functional/windows_script.rb
+++ b/spec/support/shared/functional/windows_script.rb
@@ -44,4 +44,112 @@ shared_context Chef::Resource::WindowsScript do
after(:each) do
File.delete(script_output_path) if File.exists?(script_output_path)
end
+
+ let!(:resource) do
+ Chef::Resource::WindowsScript::Batch.new("Batch resource functional test", @run_context)
+ end
+
+ 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
+ else
+ expected_architecture = :i386
+ end
+ end
+ let(:expected_architecture_output) do
+ expected_architecture == :i386 ? 'X86' : 'AMD64'
+ end
+ let(:guard_script_suffix) do
+ "guard"
+ end
+ let(:guard_script_output_path) do
+ "#{script_output_path}#{guard_script_suffix}"
+ end
+ let(:resource_command) do
+ "#{architecture_command} #{output_command} #{script_output_path}"
+ end
+ let(:resource_guard_command) do
+ "#{architecture_command} #{output_command} #{guard_script_output_path}"
+ end
+
+ before(:each) do
+ resource.code resource_command
+ (resource.architecture architecture) if architecture
+ resource.returns(0)
+ end
+
+ it "should create a process with the expected architecture" do
+ resource.run_action(:run)
+ get_process_architecture.should == expected_architecture_output.downcase
+ end
+
+ it "should execute guards with the same architecture as the resource" do
+ resource.only_if resource_guard_command
+ resource.run_action(:run)
+ get_process_architecture.should == expected_architecture_output.downcase
+ get_guard_process_architecture.should == expected_architecture_output.downcase
+ get_guard_process_architecture.should == 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" do
+ resource.only_if resource_guard_command, :architecture => :x86_64
+ resource.run_action(:run)
+ get_guard_process_architecture.should == 'amd64'
+ 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)
+ get_guard_process_architecture.should == 'x86'
+ end
+ end
+ end
+
+ shared_examples_for "a Windows script running on Windows" do
+
+ describe "when the run action is invoked on Windows" do
+ it "executes the script code" do
+ resource.code("@whoami > #{script_output_path}")
+ resource.returns(0)
+ resource.run_action(:run)
+ end
+ end
+
+ context "when the architecture attribute is not set" do
+ let(:architecture) { nil }
+ it_behaves_like "a script resource with architecture attribute"
+ end
+
+ context "when the architecture attribute is :i386" do
+ let(: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 }
+ it_behaves_like "a script resource with architecture attribute"
+ end
+ end
+
+ def get_windows_script_output(suffix = '')
+ File.read("#{script_output_path}#{suffix}")
+ end
+
+ def source_contains_case_insensitive_content?( source, content )
+ source.downcase.include?(content.downcase)
+ end
+
+ def get_guard_process_architecture
+ get_process_architecture(guard_script_suffix)
+ end
+
+ def get_process_architecture(suffix = '')
+ get_windows_script_output(suffix).strip.downcase
+ end
+
end
diff --git a/spec/support/shared/unit/script_resource.rb b/spec/support/shared/unit/script_resource.rb
index 1137958420..a34f930fc8 100644
--- a/spec/support/shared/unit/script_resource.rb
+++ b/spec/support/shared/unit/script_resource.rb
@@ -72,8 +72,8 @@ shared_examples_for "a script resource" do
it "when guard_interpreter is set to the default value, the guard command string should be evaluated by command execution and not through a resource" do
Chef::Resource::Conditional.any_instance.should_not_receive(:evaluate_block)
- Chef::Resource::Conditional.any_instance.should_receive(:evaluate_command).and_return(true)
Chef::GuardInterpreter::ResourceGuardInterpreter.any_instance.should_not_receive(:evaluate_action)
+ Chef::GuardInterpreter::DefaultGuardInterpreter.any_instance.should_receive(:evaluate).and_return(true)
resource.only_if 'echo hi'
resource.should_skip?(:run).should == nil
end
diff --git a/spec/support/shared/unit/windows_script_resource.rb b/spec/support/shared/unit/windows_script_resource.rb
index 23dbfbe722..837ac1a430 100644
--- a/spec/support/shared/unit/windows_script_resource.rb
+++ b/spec/support/shared/unit/windows_script_resource.rb
@@ -39,8 +39,24 @@ shared_examples_for "a Windows script resource" do
@resource.should be_a_kind_of(Chef::Resource::WindowsScript)
end
- context "script" do
- let(:script_resource) { resource_instance }
+ 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
+ end
+
+ it "should use a resource to evaluate the guard when guard_interpreter is not specified" do
+ Chef::GuardInterpreter::ResourceGuardInterpreter.any_instance.should_receive(:evaluate_action).and_return(true)
+ Chef::GuardInterpreter::DefaultGuardInterpreter.any_instance.should_not_receive(:evaluate)
+ @resource.only_if 'echo hi'
+ @resource.should_skip?(:run).should == nil
+ end
+ end
+
+ context "script with a default guard interpreter" do
+ let(:script_resource) do
+ resource_instance.guard_interpreter :default
+ resource_instance
+ end
it_should_behave_like "a script resource"
end