summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam Edwards <adamed@opscode.com>2014-03-27 05:28:38 -0700
committerAdam Edwards <adamed@opscode.com>2014-03-29 00:21:04 -0700
commit8fef6af58a397d79de6e46642e2480f83ae77628 (patch)
tree8a18b652e239115b186fb445b42aeceab7f7ac00
parentfe1279f3bbabba5add37a490f3c9e098a76ed896 (diff)
downloadchef-8fef6af58a397d79de6e46642e2480f83ae77628.tar.gz
Guard resource new specs and spec fixes
-rw-r--r--lib/chef/resource.rb1
-rw-r--r--lib/chef/resource/conditional/guard_interpreter.rb15
-rw-r--r--lib/chef/resource/execute.rb6
-rw-r--r--lib/chef/resource/powershell_script.rb2
-rw-r--r--lib/chef/resource/script.rb13
-rw-r--r--spec/support/shared/unit/script_resource.rb31
-rw-r--r--spec/unit/resource/conditional/guard_interpreter_spec.rb60
-rw-r--r--spec/unit/resource/powershell_spec.rb46
-rw-r--r--spec/unit/resource_spec.rb22
9 files changed, 182 insertions, 14 deletions
diff --git a/lib/chef/resource.rb b/lib/chef/resource.rb
index 5db03ccf02..4a182c28ab 100644
--- a/lib/chef/resource.rb
+++ b/lib/chef/resource.rb
@@ -226,7 +226,6 @@ F
attr_reader :resource_name
attr_reader :not_if_args
attr_reader :only_if_args
- attr_reader :guard_inherited_attributes
attr_reader :elapsed_time
diff --git a/lib/chef/resource/conditional/guard_interpreter.rb b/lib/chef/resource/conditional/guard_interpreter.rb
index a174037587..0165b51aae 100644
--- a/lib/chef/resource/conditional/guard_interpreter.rb
+++ b/lib/chef/resource/conditional/guard_interpreter.rb
@@ -30,7 +30,12 @@ class Chef
empty_events = Chef::EventDispatch::Dispatcher.new
anonymous_run_context = Chef::RunContext.new(parent_resource.node, {}, empty_events)
- @resource = resource_class.new('anonymous', anonymous_run_context)
+ @resource = resource_class.new('Guard resource', anonymous_run_context)
+
+ if ! @resource.kind_of?(Chef::Resource::Script)
+ raise ArgumentError, "Specified guard interpreter class #{resource_class} must be a kind of Chef::Resource::Script resource"
+ end
+
@handled_exceptions = handled_exceptions ? handled_exceptions : []
merge_inherited_attributes(parent_resource)
@source_line = source_line if source_line
@@ -62,7 +67,7 @@ class Chef
def get_resource_class(parent_resource, resource_symbol)
if parent_resource.nil? || parent_resource.node.nil?
- raise ArgumentError, "Node for anonymous resource must not be nil"
+ raise ArgumentError, "Node for guard resource parent must not be nil"
end
Chef::Resource.resource_for_node(resource_symbol, parent_resource.node)
end
@@ -76,7 +81,11 @@ class Chef
end
def merge_inherited_attributes(parent_resource)
- inherited_attributes = parent_resource.guard_inherited_attributes
+ inherited_attributes = []
+
+ if parent_resource.respond_to?(:guard_inherited_attributes)
+ inherited_attributes = parent_resource.send(:guard_inherited_attributes)
+ end
if inherited_attributes
inherited_attributes.each do |attribute|
diff --git a/lib/chef/resource/execute.rb b/lib/chef/resource/execute.rb
index c51e534dec..7c4fa48c0a 100644
--- a/lib/chef/resource/execute.rb
+++ b/lib/chef/resource/execute.rb
@@ -125,12 +125,6 @@ class Chef
)
end
- protected
-
- def append_guard_inherited_attributes(inherited_attributes)
- @guard_inherited_attributes.concat(inherited_attributes)
- end
-
end
end
end
diff --git a/lib/chef/resource/powershell_script.rb b/lib/chef/resource/powershell_script.rb
index c2bb28c3e2..87cb225f73 100644
--- a/lib/chef/resource/powershell_script.rb
+++ b/lib/chef/resource/powershell_script.rb
@@ -23,7 +23,7 @@ class Chef
def initialize(name, run_context=nil)
super(name, run_context, :powershell_script, "powershell.exe")
- append_guard_inherited_attributes([:architecture])
+ set_guard_inherited_attributes([:architecture])
@convert_boolean_return = nil
end
diff --git a/lib/chef/resource/script.rb b/lib/chef/resource/script.rb
index 7d56d1d0c6..5619d4e9aa 100644
--- a/lib/chef/resource/script.rb
+++ b/lib/chef/resource/script.rb
@@ -33,7 +33,8 @@ class Chef
@interpreter = nil
@flags = nil
@guard_inherited_attributes = []
- append_guard_inherited_attributes(
+
+ set_guard_inherited_attributes(
[
:cwd,
:environment,
@@ -68,6 +69,16 @@ class Chef
)
end
+ protected
+
+ def set_guard_inherited_attributes(inherited_attributes)
+ @guard_inherited_attributes.concat(inherited_attributes).uniq
+ end
+
+ def guard_inherited_attributes
+ @guard_inherited_attributes
+ end
+
end
end
end
diff --git a/spec/support/shared/unit/script_resource.rb b/spec/support/shared/unit/script_resource.rb
index 5f37506df6..51f6fc7215 100644
--- a/spec/support/shared/unit/script_resource.rb
+++ b/spec/support/shared/unit/script_resource.rb
@@ -48,5 +48,36 @@ shared_examples_for "a script resource" do
@resource.flags.should eql("-f")
end
+ describe "when executing guards" do
+ let(:resource) { @resource }
+
+ before(:each) do
+ node = Chef::Node.new
+
+ node.automatic[:platform] = "debian"
+ node.automatic[:platform_version] = "6.0"
+
+ events = Chef::EventDispatch::Dispatcher.new
+ run_context = Chef::RunContext.new(node, {}, events)
+ resource.run_context = run_context
+ resource.code 'echo hi'
+ end
+
+ 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::Resource::Conditional::GuardInterpreter.any_instance.should_not_receive(:evaluate_action)
+ resource.only_if 'echo hi'
+ resource.should_skip?(:run).should == nil
+ end
+
+ it "when a valid guard_interpreter resource is specified, a block should be used to evaluate the guard" do
+ Chef::Resource::Conditional.any_instance.should_not_receive(:evaluate_command)
+ Chef::Resource::Conditional::GuardInterpreter.any_instance.should_receive(:evaluate_action).and_return(true)
+ resource.guard_interpreter :script
+ resource.only_if 'echo hi'
+ resource.should_skip?(:run).should == nil
+ end
+ end
end
diff --git a/spec/unit/resource/conditional/guard_interpreter_spec.rb b/spec/unit/resource/conditional/guard_interpreter_spec.rb
new file mode 100644
index 0000000000..3cf64b98a0
--- /dev/null
+++ b/spec/unit/resource/conditional/guard_interpreter_spec.rb
@@ -0,0 +1,60 @@
+#
+# Author:: Adam Edwards (<adamed@getchef.com>)
+# Copyright:: Copyright (c) 2014 Chef Software, Inc.
+# License:: Apache License, Version 2.0
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+require 'spec_helper'
+
+describe Chef::Resource::Conditional::GuardInterpreter do
+ before(:each) do
+ node = Chef::Node.new
+
+ node.default["kernel"] = Hash.new
+ node.default["kernel"][:machine] = :x86_64.to_s
+
+ run_context = Chef::RunContext.new(node, nil, nil)
+
+ @resource = Chef::Resource.new("powershell_unit_test", run_context)
+ @resource.stub(:run_action)
+ @resource.stub(:updated).and_return(true)
+ end
+
+ describe "when evaluating a guard resource" do
+ let(:resource) { @resource }
+
+ it "should allow guard interpreter to be set to Chef::Resource::Script" do
+ resource.guard_interpreter(:script)
+ allow_any_instance_of(Chef::Resource::Conditional::GuardInterpreter).to receive(:evaluate_action).and_return(false)
+ resource.only_if("echo hi")
+ end
+
+ it "should allow guard interpreter to be set to Chef::Resource::PowershellScript derived indirectly from Chef::Resource::Script" do
+ resource.guard_interpreter(:powershell_script)
+ allow_any_instance_of(Chef::Resource::Conditional::GuardInterpreter).to receive(:evaluate_action).and_return(false)
+ resource.only_if("echo hi")
+ end
+
+ it "should raise an exception if guard_interpreter is set to a resource not derived from Chef::Resource::Script" do
+ resource.guard_interpreter(:file)
+
+ begin
+ resource.only_if("echo hi").should raise_error ArgumentError
+ rescue ArgumentError
+ end
+ end
+ end
+end
+
diff --git a/spec/unit/resource/powershell_spec.rb b/spec/unit/resource/powershell_spec.rb
index a35e37c696..dd00b33c12 100644
--- a/spec/unit/resource/powershell_spec.rb
+++ b/spec/unit/resource/powershell_spec.rb
@@ -36,7 +36,50 @@ describe Chef::Resource::PowershellScript do
@resource.should be_a_kind_of(Chef::Resource::PowershellScript)
end
- context "windowsscript" do
+ context "when using guards" do
+ let(:resource) { @resource }
+ before(:each) do
+ # allow_any_instance_of(Chef::Resource::Conditional::GuardInterpreter).to receive(:evaluate_action).and_return(false)
+ resource.stub(:run_action)
+ resource.stub(:updated).and_return(true)
+ end
+
+ it "should allow guard interpreter to be set to Chef::Resource::Script" do
+ resource.guard_interpreter(:script)
+ allow_any_instance_of(Chef::Resource::Conditional::GuardInterpreter).to receive(:evaluate_action).and_return(false)
+ resource.only_if("echo hi")
+ end
+
+ it "should allow guard interpreter to be set to Chef::Resource::Bash derived from Chef::Resource::Script" do
+ resource.guard_interpreter(:bash)
+ allow_any_instance_of(Chef::Resource::Conditional::GuardInterpreter).to receive(:evaluate_action).and_return(false)
+ resource.only_if("echo hi")
+ end
+
+ it "should allow guard interpreter to be set to Chef::Resource::PowershellScript derived indirectly from Chef::Resource::Script" do
+ resource.guard_interpreter(:powershell_script)
+ allow_any_instance_of(Chef::Resource::Conditional::GuardInterpreter).to receive(:evaluate_action).and_return(false)
+ resource.only_if("echo hi")
+ end
+
+ it "should raise an exception on an attempt to set the guard_interpreter attribute to something other than a Symbol" do
+ begin
+ resource.guard_interpreter('script')
+ rescue Chef::Exceptions::ValidationFailed
+ end
+ end
+
+ it "should raise an exception if guard_interpreter is set to a resource not derived from Chef::Resource::Script" do
+ resource.guard_interpreter(:file)
+
+ begin
+ resource.only_if("echo hi").should raise_error ArgumentError
+ rescue ArgumentError
+ end
+ end
+ end
+
+ context "as a script running in Windows-based scripting language" do
let(:resource_instance) { @resource }
let(:resource_instance_name ) { @resource.command }
let(:resource_name) { :powershell_script }
@@ -44,5 +87,4 @@ describe Chef::Resource::PowershellScript do
it_should_behave_like "a Windows script resource"
end
-
end
diff --git a/spec/unit/resource_spec.rb b/spec/unit/resource_spec.rb
index 3871d88058..911d234d1d 100644
--- a/spec/unit/resource_spec.rb
+++ b/spec/unit/resource_spec.rb
@@ -526,6 +526,28 @@ describe Chef::Resource do
snitch_var2.should be_false
end
+ describe "guard_interpreter attribute" do
+ let(:resource) { @resource }
+
+ it "should be set to :default by default" do
+ resource.guard_interpreter.should == :default
+ end
+
+ it "should raise Chef::Exceptions::ValidationFailed on an attempt to set the guard_interpreter attribute to something other than a Symbol" do
+ begin
+ resource.guard_interpreter('command_dot_com')
+ rescue Chef::Exceptions::ValidationFailed
+ end
+ end
+
+ it "should not raise an exception when setting the guard interpreter attribute to a Symbol" do
+ begin
+ resource.guard_interpreter(:command_dot_com)
+ rescue Chef::Exceptions::ValidationFailed
+ end
+ end
+ end
+
end
describe "should_skip?" do