diff options
author | Adam Edwards <adamed@opscode.com> | 2014-03-27 05:28:38 -0700 |
---|---|---|
committer | Adam Edwards <adamed@opscode.com> | 2014-03-29 00:21:04 -0700 |
commit | 8fef6af58a397d79de6e46642e2480f83ae77628 (patch) | |
tree | 8a18b652e239115b186fb445b42aeceab7f7ac00 /spec | |
parent | fe1279f3bbabba5add37a490f3c9e098a76ed896 (diff) | |
download | chef-8fef6af58a397d79de6e46642e2480f83ae77628.tar.gz |
Guard resource new specs and spec fixes
Diffstat (limited to 'spec')
-rw-r--r-- | spec/support/shared/unit/script_resource.rb | 31 | ||||
-rw-r--r-- | spec/unit/resource/conditional/guard_interpreter_spec.rb | 60 | ||||
-rw-r--r-- | spec/unit/resource/powershell_spec.rb | 46 | ||||
-rw-r--r-- | spec/unit/resource_spec.rb | 22 |
4 files changed, 157 insertions, 2 deletions
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 |