summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTyler Ball <tyleraball@gmail.com>2015-02-13 17:16:29 -0800
committerTyler Ball <tyleraball@gmail.com>2015-02-13 17:16:29 -0800
commitccf8a2791650fa8f7ecfb849a9c87d8bbc820269 (patch)
treeba76820fc7d21a8715c910e97f75738a60b935e6
parent36477867cac9eb1db37730ecfd4d0181d42fa660 (diff)
parenta5f29b1245cfeb5b726bed373331f34014c6199b (diff)
downloadchef-ccf8a2791650fa8f7ecfb849a9c87d8bbc820269.tar.gz
Merge pull request #2746 from chef/tball/finish-2688
Adding tests for https://github.com/opscode/chef/pull/2688
-rw-r--r--lib/chef/guard_interpreter/resource_guard_interpreter.rb2
-rw-r--r--spec/unit/guard_interpreter/resource_guard_interpreter_spec.rb116
2 files changed, 104 insertions, 14 deletions
diff --git a/lib/chef/guard_interpreter/resource_guard_interpreter.rb b/lib/chef/guard_interpreter/resource_guard_interpreter.rb
index 7d9bccb6ca..fb545c95eb 100644
--- a/lib/chef/guard_interpreter/resource_guard_interpreter.rb
+++ b/lib/chef/guard_interpreter/resource_guard_interpreter.rb
@@ -41,7 +41,7 @@ class Chef
# attribute by checking the type of the resources.
# We need to make sure we check for Script first because any resource
# that can get to here is an Execute resource.
- if @parent_resource.is_a? Chef::Resource::Script
+ if @resource.is_a? Chef::Resource::Script
block_attributes = @command_opts.merge({:code => @command})
else
block_attributes = @command_opts.merge({:command => @command})
diff --git a/spec/unit/guard_interpreter/resource_guard_interpreter_spec.rb b/spec/unit/guard_interpreter/resource_guard_interpreter_spec.rb
index 9089177e18..4cf3ba827a 100644
--- a/spec/unit/guard_interpreter/resource_guard_interpreter_spec.rb
+++ b/spec/unit/guard_interpreter/resource_guard_interpreter_spec.rb
@@ -29,28 +29,118 @@ describe Chef::GuardInterpreter::ResourceGuardInterpreter do
let(:run_context) { Chef::RunContext.new(node, nil, nil) }
- let(:resource) do
- resource = Chef::Resource.new("powershell_unit_test", run_context)
- allow(resource).to receive(:run_action)
- allow(resource).to receive(:updated).and_return(true)
- resource
+ let(:parent_resource) do
+ parent_resource = Chef::Resource.new("powershell_unit_test", run_context)
+ allow(parent_resource).to receive(:run_action)
+ allow(parent_resource).to receive(:updated).and_return(true)
+ parent_resource
end
+ let(:guard_interpreter) { Chef::GuardInterpreter::ResourceGuardInterpreter.new(parent_resource, "echo hi", nil) }
describe "get_interpreter_resource" do
it "allows the guard interpreter to be set to Chef::Resource::Script" do
- resource.guard_interpreter(:script)
- expect { Chef::GuardInterpreter::ResourceGuardInterpreter.new(resource, "echo hi", nil) }.not_to raise_error
+ parent_resource.guard_interpreter(:script)
+ expect { guard_interpreter }.not_to raise_error
end
-
+
it "allows the guard interpreter to be set to Chef::Resource::PowershellScript derived indirectly from Chef::Resource::Script" do
- resource.guard_interpreter(:powershell_script)
- expect { Chef::GuardInterpreter::ResourceGuardInterpreter.new(resource, "echo hi", nil) }.not_to raise_error
+ parent_resource.guard_interpreter(:powershell_script)
+ expect { guard_interpreter }.not_to raise_error
end
-
+
it "raises an exception if guard_interpreter is set to a resource not derived from Chef::Resource::Script" do
- resource.guard_interpreter(:file)
- expect { Chef::GuardInterpreter::ResourceGuardInterpreter.new(resource, "echo hi", nil) }.to raise_error(ArgumentError)
+ parent_resource.guard_interpreter(:file)
+ expect { guard_interpreter }.to raise_error(ArgumentError, 'Specified guard interpreter class Chef::Resource::File must be a kind of Chef::Resource::Execute resource')
+ end
+
+ context "when the resource cannot be found for the platform" do
+ before do
+ expect(Chef::Resource).to receive(:resource_for_node).with(:foobar, node).and_return(nil)
+ end
+
+ it "raises an exception" do
+ parent_resource.guard_interpreter(:foobar)
+ expect { guard_interpreter }.to raise_error(ArgumentError, 'Specified guard_interpreter resource foobar unknown for this platform')
+ end
+ end
+
+ it "fails when parent_resource is nil" do
+ expect { Chef::GuardInterpreter::ResourceGuardInterpreter.new(nil, "echo hi", nil) }.to raise_error(ArgumentError, /Node for guard resource parent must not be nil/)
+ end
+
+ end
+
+ describe "#evaluate" do
+ let(:guard_interpreter) { Chef::GuardInterpreter::ResourceGuardInterpreter.new(parent_resource, "echo hi", {}) }
+ let(:parent_resource) do
+ parent_resource = Chef::Resource.new("execute resource", run_context)
+ parent_resource.guard_interpreter(:execute)
+ parent_resource
+ end
+
+ it "successfully evaluates the resource" do
+ expect(guard_interpreter.evaluate).to eq(true)
+ end
+
+ describe "script command opts switch" do
+ let(:command_opts) { {} }
+ let(:guard_interpreter) { Chef::GuardInterpreter::ResourceGuardInterpreter.new(parent_resource, "exit 0", command_opts) }
+
+ context "resource is a Script" do
+ context "and guard_interpreter is a :script" do
+ let(:parent_resource) do
+ parent_resource = Chef::Resource::Script.new("resource", run_context)
+ # Ruby scripts are cross platform to both Linux and Windows
+ parent_resource.guard_interpreter(:ruby)
+ parent_resource
+ end
+
+ let(:shell_out) {
+ instance_double(Mixlib::ShellOut, :live_stream => true, :run_command => true, :error! => nil)
+ }
+
+ before do
+ # TODO for some reason Windows is failing on executing a ruby script
+ expect(Mixlib::ShellOut).to receive(:new) do |*args|
+ expect(args[0]).to match(/^"ruby"/)
+ shell_out
+ end
+ end
+
+ it "merges to :code" do
+ expect(command_opts).to receive(:merge).with({:code => "exit 0"}).and_call_original
+ expect(guard_interpreter.evaluate).to eq(true)
+ end
+ end
+
+ context "and guard_interpreter is :execute" do
+ let(:parent_resource) do
+ parent_resource = Chef::Resource::Script.new("resource", run_context)
+ parent_resource.guard_interpreter(:execute)
+ parent_resource
+ end
+
+ it "merges to :code" do
+ expect(command_opts).to receive(:merge).with({:command => "exit 0"}).and_call_original
+ expect(guard_interpreter.evaluate).to eq(true)
+ end
+ end
+ end
+
+ context "resource is not a Script" do
+ let(:parent_resource) do
+ parent_resource = Chef::Resource::Execute.new("resource", run_context)
+ parent_resource.guard_interpreter(:execute)
+ parent_resource
+ end
+
+ it "merges to :command" do
+ expect(command_opts).to receive(:merge).with({:command => "exit 0"}).and_call_original
+ expect(guard_interpreter.evaluate).to eq(true)
+ end
+ end
+
end
end
end