summaryrefslogtreecommitdiff
path: root/spec/unit/resource
diff options
context:
space:
mode:
Diffstat (limited to 'spec/unit/resource')
-rw-r--r--spec/unit/resource/conditional_spec.rb25
-rw-r--r--spec/unit/resource/cron_spec.rb9
-rw-r--r--spec/unit/resource/powershell_spec.rb87
-rw-r--r--spec/unit/resource/subversion_spec.rb5
-rw-r--r--spec/unit/resource/windows_package_spec.rb74
5 files changed, 184 insertions, 16 deletions
diff --git a/spec/unit/resource/conditional_spec.rb b/spec/unit/resource/conditional_spec.rb
index 1be7bcea71..4df185bcd6 100644
--- a/spec/unit/resource/conditional_spec.rb
+++ b/spec/unit/resource/conditional_spec.rb
@@ -24,12 +24,13 @@ describe Chef::Resource::Conditional do
Mixlib::ShellOut.any_instance.stub(:run_command).and_return(nil)
@status = OpenStruct.new(:success? => true)
Mixlib::ShellOut.any_instance.stub(:status).and_return(@status)
+ @parent_resource = Chef::Resource.new(nil, Chef::Node.new)
end
describe "when created as an `only_if`" do
describe "after running a successful command" do
before do
- @conditional = Chef::Resource::Conditional.only_if("true")
+ @conditional = Chef::Resource::Conditional.only_if(@parent_resource, "true")
end
it "indicates that resource convergence should continue" do
@@ -40,7 +41,7 @@ describe Chef::Resource::Conditional do
describe "after running a negative/false command" do
before do
@status.send("success?=", false)
- @conditional = Chef::Resource::Conditional.only_if("false")
+ @conditional = Chef::Resource::Conditional.only_if(@parent_resource, "false")
end
it "indicates that resource convergence should not continue" do
@@ -50,8 +51,8 @@ describe Chef::Resource::Conditional do
describe 'after running a command which timed out' do
before do
- @conditional = Chef::Resource::Conditional.only_if("false")
- @conditional.stub(:shell_out).and_raise(Chef::Exceptions::CommandTimeout)
+ @conditional = Chef::Resource::Conditional.only_if(@parent_resource, "false")
+ Chef::GuardInterpreter::DefaultGuardInterpreter.any_instance.stub(:shell_out).and_raise(Chef::Exceptions::CommandTimeout)
end
it 'indicates that resource convergence should not continue' do
@@ -66,7 +67,7 @@ describe Chef::Resource::Conditional do
describe "after running a block that returns a truthy value" do
before do
- @conditional = Chef::Resource::Conditional.only_if { Object.new }
+ @conditional = Chef::Resource::Conditional.only_if(@parent_resource) { Object.new }
end
it "indicates that resource convergence should continue" do
@@ -76,7 +77,7 @@ describe Chef::Resource::Conditional do
describe "after running a block that returns a falsey value" do
before do
- @conditional = Chef::Resource::Conditional.only_if { nil }
+ @conditional = Chef::Resource::Conditional.only_if(@parent_resource) { nil }
end
it "indicates that resource convergence should not continue" do
@@ -88,7 +89,7 @@ describe Chef::Resource::Conditional do
describe "when created as a `not_if`" do
describe "after running a successful/true command" do
before do
- @conditional = Chef::Resource::Conditional.not_if("true")
+ @conditional = Chef::Resource::Conditional.not_if(@parent_resource, "true")
end
it "indicates that resource convergence should not continue" do
@@ -99,7 +100,7 @@ describe Chef::Resource::Conditional do
describe "after running a failed/false command" do
before do
@status.send("success?=", false)
- @conditional = Chef::Resource::Conditional.not_if("false")
+ @conditional = Chef::Resource::Conditional.not_if(@parent_resource, "false")
end
it "indicates that resource convergence should continue" do
@@ -109,8 +110,8 @@ describe Chef::Resource::Conditional do
describe 'after running a command which timed out' do
before do
- @conditional = Chef::Resource::Conditional.not_if("false")
- @conditional.stub(:shell_out).and_raise(Chef::Exceptions::CommandTimeout)
+ @conditional = Chef::Resource::Conditional.not_if(@parent_resource, "false")
+ Chef::GuardInterpreter::DefaultGuardInterpreter.any_instance.stub(:shell_out).and_raise(Chef::Exceptions::CommandTimeout)
end
it 'indicates that resource convergence should continue' do
@@ -125,7 +126,7 @@ describe Chef::Resource::Conditional do
describe "after running a block that returns a truthy value" do
before do
- @conditional = Chef::Resource::Conditional.not_if { Object.new }
+ @conditional = Chef::Resource::Conditional.not_if(@parent_resource) { Object.new }
end
it "indicates that resource convergence should not continue" do
@@ -135,7 +136,7 @@ describe Chef::Resource::Conditional do
describe "after running a block that returns a falsey value" do
before do
- @conditional = Chef::Resource::Conditional.not_if { nil }
+ @conditional = Chef::Resource::Conditional.not_if(@parent_resource) { nil }
end
it "indicates that resource convergence should continue" do
diff --git a/spec/unit/resource/cron_spec.rb b/spec/unit/resource/cron_spec.rb
index 355a7f09ba..cf821e3d32 100644
--- a/spec/unit/resource/cron_spec.rb
+++ b/spec/unit/resource/cron_spec.rb
@@ -143,8 +143,13 @@ describe Chef::Resource::Cron do
lambda { @resource.month "13" }.should raise_error(RangeError)
end
- it "should reject any weekday over 7" do
- lambda { @resource.weekday "8" }.should raise_error(RangeError)
+ describe "weekday" do
+ it "should reject any weekday over 7" do
+ lambda { @resource.weekday "8" }.should raise_error(RangeError)
+ end
+ it "should reject any symbols which don't represent day of week" do
+ lambda { @resource.weekday :foo }.should raise_error(RangeError)
+ end
end
it "should convert integer schedule values to a string" do
diff --git a/spec/unit/resource/powershell_spec.rb b/spec/unit/resource/powershell_spec.rb
index a35e37c696..da20c4f0bf 100644
--- a/spec/unit/resource/powershell_spec.rb
+++ b/spec/unit/resource/powershell_spec.rb
@@ -36,7 +36,91 @@ describe Chef::Resource::PowershellScript do
@resource.should be_a_kind_of(Chef::Resource::PowershellScript)
end
- context "windowsscript" do
+ it "should set convert_boolean_return to false by default" do
+ @resource.convert_boolean_return.should == false
+ end
+
+ it "should return the value for convert_boolean_return that was set" do
+ @resource.convert_boolean_return true
+ @resource.convert_boolean_return.should == true
+ @resource.convert_boolean_return false
+ @resource.convert_boolean_return.should == false
+ end
+
+ context "when using guards" do
+ let(:resource) { @resource }
+ before(:each) do
+ resource.stub(:run_action)
+ resource.stub(:updated).and_return(true)
+ end
+
+ it "inherits exactly the :cwd, :environment, :group, :path, :user, :umask, and :architecture attributes from a parent resource class" do
+ inherited_difference = Chef::Resource::PowershellScript.guard_inherited_attributes -
+ [:cwd, :environment, :group, :path, :user, :umask, :architecture ]
+
+ inherited_difference.should == []
+ end
+
+ it "should allow guard interpreter to be set to Chef::Resource::Script" do
+ resource.guard_interpreter(:script)
+ allow_any_instance_of(Chef::GuardInterpreter::ResourceGuardInterpreter).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::GuardInterpreter::ResourceGuardInterpreter).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::GuardInterpreter::ResourceGuardInterpreter).to receive(:evaluate_action).and_return(false)
+ resource.only_if("echo hi")
+ end
+
+ it "should enable convert_boolean_return by default for guards in the context of powershell_script when no guard params are specified" do
+ allow_any_instance_of(Chef::GuardInterpreter::ResourceGuardInterpreter).to receive(:evaluate_action).and_return(true)
+ allow_any_instance_of(Chef::GuardInterpreter::ResourceGuardInterpreter).to receive(:block_from_attributes).with(
+ {:convert_boolean_return => true, :code => "$true"}).and_return(Proc.new {})
+ resource.only_if("$true")
+ end
+
+ it "should enable convert_boolean_return by default for guards in non-Chef::Resource::Script derived resources when no guard params are specified" do
+ node = Chef::Node.new
+ run_context = Chef::RunContext.new(node, nil, nil)
+ file_resource = Chef::Resource::File.new('idontexist', run_context)
+ file_resource.guard_interpreter :powershell_script
+
+ allow_any_instance_of(Chef::GuardInterpreter::ResourceGuardInterpreter).to receive(:block_from_attributes).with(
+ {:convert_boolean_return => true, :code => "$true"}).and_return(Proc.new {})
+ resource.only_if("$true")
+ end
+
+ it "should enable convert_boolean_return by default for guards in the context of powershell_script when guard params are specified" do
+ guard_parameters = {:cwd => '/etc/chef', :architecture => :x86_64}
+ allow_any_instance_of(Chef::GuardInterpreter::ResourceGuardInterpreter).to receive(:block_from_attributes).with(
+ {:convert_boolean_return => true, :code => "$true"}.merge(guard_parameters)).and_return(Proc.new {})
+ resource.only_if("$true", guard_parameters)
+ end
+
+ it "should pass convert_boolean_return as true if it was specified as true in a guard parameter" do
+ guard_parameters = {:cwd => '/etc/chef', :convert_boolean_return => true, :architecture => :x86_64}
+ allow_any_instance_of(Chef::GuardInterpreter::ResourceGuardInterpreter).to receive(:block_from_attributes).with(
+ {:convert_boolean_return => true, :code => "$true"}.merge(guard_parameters)).and_return(Proc.new {})
+ resource.only_if("$true", guard_parameters)
+ end
+
+ it "should pass convert_boolean_return as false if it was specified as true in a guard parameter" do
+ other_guard_parameters = {:cwd => '/etc/chef', :architecture => :x86_64}
+ parameters_with_boolean_disabled = other_guard_parameters.merge({:convert_boolean_return => false, :code => "$true"})
+ allow_any_instance_of(Chef::GuardInterpreter::ResourceGuardInterpreter).to receive(:block_from_attributes).with(
+ parameters_with_boolean_disabled).and_return(Proc.new {})
+ resource.only_if("$true", parameters_with_boolean_disabled)
+ 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 +128,4 @@ describe Chef::Resource::PowershellScript do
it_should_behave_like "a Windows script resource"
end
-
end
diff --git a/spec/unit/resource/subversion_spec.rb b/spec/unit/resource/subversion_spec.rb
index 67593c5a7c..ae06ce665a 100644
--- a/spec/unit/resource/subversion_spec.rb
+++ b/spec/unit/resource/subversion_spec.rb
@@ -55,4 +55,9 @@ describe Chef::Resource::Subversion do
@svn.svn_arguments.should be_nil
end
+ it "hides password from custom exception message" do
+ @svn.svn_password "l33th4x0rpa$$w0rd"
+ e = @svn.customize_exception(Chef::Exceptions::Exec.new "Exception with password #{@svn.svn_password}")
+ e.message.include?(@svn.svn_password).should be_false
+ end
end
diff --git a/spec/unit/resource/windows_package_spec.rb b/spec/unit/resource/windows_package_spec.rb
new file mode 100644
index 0000000000..c9ef8d910c
--- /dev/null
+++ b/spec/unit/resource/windows_package_spec.rb
@@ -0,0 +1,74 @@
+#
+# Author:: Bryan McLellan <btm@loftninjas.org>
+# 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::WindowsPackage, "initialize", :windows_only do
+
+ let(:resource) { Chef::Resource::WindowsPackage.new("solitaire.msi") }
+
+ it "returns a Chef::Resource::WindowsPackage" do
+ expect(resource).to be_a_kind_of(Chef::Resource::WindowsPackage)
+ end
+
+ it "sets the resource_name to :windows_package" do
+ expect(resource.resource_name).to eql(:windows_package)
+ end
+
+ it "sets the provider to Chef::Provider::Package::Windows" do
+ expect(resource.provider).to eql(Chef::Provider::Package::Windows)
+ end
+
+ it "supports setting installer_type" do
+ resource.installer_type("msi")
+ expect(resource.installer_type).to eql("msi")
+ end
+
+ # String, Integer
+ [ "600", 600 ].each do |val|
+ it "supports setting a timeout as a #{val.class}" do
+ resource.timeout(val)
+ expect(resource.timeout).to eql(val)
+ end
+ end
+
+ # String, Integer, Array
+ [ "42", 42, [47, 48, 49] ].each do |val|
+ it "supports setting an alternate return value as a #{val.class}" do
+ resource.returns(val)
+ expect(resource.returns).to eql(val)
+ end
+ end
+
+ it "coverts a source to an absolute path" do
+ ::File.stub(:absolute_path).and_return("c:\\Files\\frost.msi")
+ resource.source("frost.msi")
+ expect(resource.source).to eql "c:\\Files\\frost.msi"
+ end
+
+ it "converts slashes to backslashes in the source path" do
+ ::File.stub(:absolute_path).and_return("c:\\frost.msi")
+ resource.source("c:/frost.msi")
+ expect(resource.source).to eql "c:\\frost.msi"
+ end
+
+ it "defaults source to the resource name" do
+ # it's a little late to stub out File.absolute_path
+ expect(resource.source).to include("solitaire.msi")
+ end
+end