diff options
-rw-r--r-- | CHANGELOG.md | 4 | ||||
-rw-r--r-- | lib/chef/config.rb | 28 | ||||
-rw-r--r-- | lib/chef/dsl/recipe.rb | 6 | ||||
-rw-r--r-- | lib/chef/guard_interpreter/resource_guard_interpreter.rb | 21 | ||||
-rw-r--r-- | lib/chef/mixin/shell_out.rb | 4 | ||||
-rw-r--r-- | lib/chef/recipe.rb | 2 | ||||
-rw-r--r-- | lib/chef/resource.rb | 24 | ||||
-rw-r--r-- | lib/chef/resource/conditional.rb | 6 | ||||
-rw-r--r-- | lib/chef/resource/execute.rb | 34 | ||||
-rw-r--r-- | lib/chef/resource/script.rb | 25 | ||||
-rw-r--r-- | spec/functional/resource/execute_spec.rb | 113 | ||||
-rw-r--r-- | spec/functional/resource/file_spec.rb | 2 | ||||
-rw-r--r-- | spec/support/shared/unit/execute_resource.rb | 7 | ||||
-rw-r--r-- | spec/unit/config_spec.rb | 89 | ||||
-rw-r--r-- | spec/unit/recipe_spec.rb | 19 | ||||
-rw-r--r-- | spec/unit/resource/execute_spec.rb | 5 | ||||
-rw-r--r-- | spec/unit/resource_definition_spec.rb | 78 | ||||
-rw-r--r-- | spec/unit/resource_spec.rb | 2 |
18 files changed, 357 insertions, 112 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index f321120901..2b6e280bea 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -127,6 +127,7 @@ ### Chef Contributions +* Recipe definition now returns the retval of the definition * Add support for Windows 10 to version helper. * `dsc_script` resource should honor configuration parameters when `configuration_data_script` is not set (Issue #2209) * Ruby has been updated to 2.1.3 along with rubygems update to 2.4.2 @@ -199,6 +200,9 @@ * Removed dependencies on the 'json' gem, replaced with ffi-yajl. Use Chef::JSONCompat library for parsing and printing. * Restore the deprecation logic of #valid_actions in LWRPs until Chef 13. * Now that we don't allow unforked chef-client interval runs, remove the reloading of previously defined LWRPs. +* Use shell_out to determine Chef::Config[:internal_locale], fix CentOS locale detection bug. +* `only_if` and `not_if` attributes of `execute` resource now inherits the parent resource's + attributes when set to a `String`. ## Last Release: 11.14.2 diff --git a/lib/chef/config.rb b/lib/chef/config.rb index 107b50ee85..9a8117d2c2 100644 --- a/lib/chef/config.rb +++ b/lib/chef/config.rb @@ -25,11 +25,13 @@ require 'mixlib/config' require 'chef/util/selinux' require 'chef/util/path_helper' require 'pathname' +require 'chef/mixin/shell_out' class Chef class Config extend Mixlib::Config + extend Chef::Mixin::ShellOut PathHelper = Chef::Util::PathHelper @@ -604,25 +606,37 @@ class Chef # available English UTF-8 locale. However, all modern POSIXen should support 'locale -a'. default :internal_locale do begin - locales = `locale -a`.split + # https://github.com/opscode/chef/issues/2181 + # Some systems have the `locale -a` command, but the result has + # invalid characters for the default encoding. + # + # For example, on CentOS 6 with ENV['LANG'] = "en_US.UTF-8", + # `locale -a`.split fails with ArgumentError invalid UTF-8 encoding. + locales = shell_out_with_systems_locale("locale -a").stdout.split case when locales.include?('C.UTF-8') 'C.UTF-8' - when locales.include?('en_US.UTF-8') + when locales.include?('en_US.UTF-8'), locales.include?('en_US.utf8') 'en_US.UTF-8' when locales.include?('en.UTF-8') 'en.UTF-8' - when guesses = locales.select { |l| l =~ /^en_.*UTF-8$'/ } - guesses.first else - Chef::Log.warn "Please install an English UTF-8 locale for Chef to use, falling back to C locale and disabling UTF-8 support." - 'C' + # Will match en_ZZ.UTF-8, en_ZZ.utf-8, en_ZZ.UTF8, en_ZZ.utf8 + guesses = locales.select { |l| l =~ /^en_.*UTF-?8$/i } + unless guesses.empty? + guessed_locale = guesses.first + # Transform into the form en_ZZ.UTF-8 + guessed_locale.gsub(/UTF-?8$/i, "UTF-8") + else + Chef::Log.warn "Please install an English UTF-8 locale for Chef to use, falling back to C locale and disabling UTF-8 support." + 'C' + end end rescue if Chef::Platform.windows? Chef::Log.debug "Defaulting to locale en_US.UTF-8 on Windows, until it matters that we do something else." else - Chef::Log.warn "No usable locale -a command found, assuming you have en_US.UTF-8 installed." + Chef::Log.debug "No usable locale -a command found, assuming you have en_US.UTF-8 installed." end 'en_US.UTF-8' end diff --git a/lib/chef/dsl/recipe.rb b/lib/chef/dsl/recipe.rb index 72de9e3662..94b0d2d18b 100644 --- a/lib/chef/dsl/recipe.rb +++ b/lib/chef/dsl/recipe.rb @@ -53,9 +53,7 @@ class Chef end def has_resource_definition?(name) - yes_or_no = run_context.definitions.has_key?(name) - - yes_or_no + run_context.definitions.has_key?(name) end # Processes the arguments and block as a resource definition. @@ -69,12 +67,10 @@ class Chef # This sets up the parameter overrides new_def.instance_eval(&block) if block - new_recipe = Chef::Recipe.new(cookbook_name, recipe_name, run_context) new_recipe.params = new_def.params new_recipe.params[:name] = args[0] new_recipe.instance_eval(&new_def.recipe) - new_recipe end # Instantiates a resource (via #build_resource), then adds it to the diff --git a/lib/chef/guard_interpreter/resource_guard_interpreter.rb b/lib/chef/guard_interpreter/resource_guard_interpreter.rb index 229a8502c7..346b585d8c 100644 --- a/lib/chef/guard_interpreter/resource_guard_interpreter.rb +++ b/lib/chef/guard_interpreter/resource_guard_interpreter.rb @@ -33,10 +33,19 @@ class Chef # to the resource merge_inherited_attributes - # Script resources have a code attribute, which is - # what is used to execute the command, so include - # that with attributes specified by caller in opts - block_attributes = @command_opts.merge({:code => @command}) + # Only execute and script resources and use guard attributes. + # The command to be executed on them are passed via different attributes. + # Script resources use code attribute and execute resources use + # command attribute. Moreover script resources are also execute + # resources. Here we make sure @command is assigned to the right + # 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 + block_attributes = @command_opts.merge({:code => @command}) + else + block_attributes = @command_opts.merge({:command => @command}) + end # Handles cases like powershell_script where default # attributes are different when used in a guard vs. not. For @@ -79,8 +88,8 @@ class Chef raise ArgumentError, "Specified guard_interpreter resource #{parent_resource.guard_interpreter.to_s} unknown for this platform" end - if ! resource_class.ancestors.include?(Chef::Resource::Script) - raise ArgumentError, "Specified guard interpreter class #{resource_class} must be a kind of Chef::Resource::Script resource" + if ! resource_class.ancestors.include?(Chef::Resource::Execute) + raise ArgumentError, "Specified guard interpreter class #{resource_class} must be a kind of Chef::Resource::Execute resource" end empty_events = Chef::EventDispatch::Dispatcher.new diff --git a/lib/chef/mixin/shell_out.rb b/lib/chef/mixin/shell_out.rb index 82772b584a..5b05e788db 100644 --- a/lib/chef/mixin/shell_out.rb +++ b/lib/chef/mixin/shell_out.rb @@ -15,10 +15,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -# chef/shell_out has been deprecated in favor of mixlib/shellout -# chef/shell_out is still required here to ensure backward compatibility -require 'chef/shell_out' - require 'mixlib/shellout' class Chef diff --git a/lib/chef/recipe.rb b/lib/chef/recipe.rb index 32578da5ab..de72a8d0c4 100644 --- a/lib/chef/recipe.rb +++ b/lib/chef/recipe.rb @@ -96,6 +96,8 @@ class Chef # true<TrueClass>:: If all the parameters are present # false<FalseClass>:: If any of the parameters are missing def tagged?(*tags) + return false if run_context.node[:tags].nil? + tags.each do |tag| return false unless run_context.node[:tags].include?(tag) end diff --git a/lib/chef/resource.rb b/lib/chef/resource.rb index cfa478c389..0b8fb2cb12 100644 --- a/lib/chef/resource.rb +++ b/lib/chef/resource.rb @@ -229,6 +229,8 @@ F attr_reader :elapsed_time + attr_reader :default_guard_interpreter + # Each notify entry is a resource/action pair, modeled as an # Struct with a #resource and #action member @@ -250,7 +252,13 @@ F @not_if = [] @only_if = [] @source_line = nil - @guard_interpreter = :default + # We would like to raise an error when the user gives us a guard + # interpreter and a ruby_block to the guard. In order to achieve this + # we need to understand when the user overrides the default guard + # interpreter. Therefore we store the default separately in a different + # attribute. + @guard_interpreter = nil + @default_guard_interpreter = :default @elapsed_time = 0 @sensitive = false end @@ -411,11 +419,15 @@ F end def guard_interpreter(arg=nil) - set_or_return( - :guard_interpreter, - arg, - :kind_of => Symbol - ) + if arg.nil? + @guard_interpreter || @default_guard_interpreter + else + set_or_return( + :guard_interpreter, + arg, + :kind_of => Symbol + ) + end end # Sets up a notification from this resource to the resource specified by +resource_spec+. diff --git a/lib/chef/resource/conditional.rb b/lib/chef/resource/conditional.rb index 324c5a4676..8960a4d57f 100644 --- a/lib/chef/resource/conditional.rb +++ b/lib/chef/resource/conditional.rb @@ -59,8 +59,10 @@ class Chef @guard_interpreter = new_guard_interpreter(@parent_resource, @command, @command_opts, &@block) @block = nil when nil - # we should have a block if we get here - if @parent_resource.guard_interpreter != :default + # We should have a block if we get here + # Check to see if the user set the guard_interpreter on the parent resource. Note that + # this error will not be raised when using the default_guard_interpreter + if @parent_resource.guard_interpreter != @parent_resource.default_guard_interpreter msg = "#{@parent_resource.name} was given a guard_interpreter of #{@parent_resource.guard_interpreter}, " msg << "but not given a command as a string. guard_interpreter does not support blocks (because they just contain ruby)." raise ArgumentError, msg diff --git a/lib/chef/resource/execute.rb b/lib/chef/resource/execute.rb index 7c4fa48c0a..ae118b1c9e 100644 --- a/lib/chef/resource/execute.rb +++ b/lib/chef/resource/execute.rb @@ -35,12 +35,12 @@ class Chef @cwd = nil @environment = nil @group = nil - @path = nil @returns = 0 @timeout = nil @user = nil @allowed_actions.push(:run) @umask = nil + @default_guard_interpreter = :execute end def umask(arg=nil) @@ -93,14 +93,6 @@ class Chef ) end - def path(arg=nil) - set_or_return( - :path, - arg, - :kind_of => [ Array ] - ) - end - def returns(arg=nil) set_or_return( :returns, @@ -125,6 +117,30 @@ class Chef ) end + def self.set_guard_inherited_attributes(*inherited_attributes) + @class_inherited_attributes = inherited_attributes + end + + def self.guard_inherited_attributes(*inherited_attributes) + # Similar to patterns elsewhere, return attributes from this + # class and superclasses as a form of inheritance + ancestor_attributes = [] + + if superclass.respond_to?(:guard_inherited_attributes) + ancestor_attributes = superclass.guard_inherited_attributes + end + + ancestor_attributes.concat(@class_inherited_attributes ? @class_inherited_attributes : []).uniq + end + + set_guard_inherited_attributes( + :cwd, + :environment, + :group, + :user, + :umask + ) + end end end diff --git a/lib/chef/resource/script.rb b/lib/chef/resource/script.rb index 6f66fb9094..8cc9c6f0c5 100644 --- a/lib/chef/resource/script.rb +++ b/lib/chef/resource/script.rb @@ -58,31 +58,6 @@ class Chef ) end - def self.set_guard_inherited_attributes(*inherited_attributes) - @class_inherited_attributes = inherited_attributes - end - - def self.guard_inherited_attributes(*inherited_attributes) - # Similar to patterns elsewhere, return attributes from this - # class and superclasses as a form of inheritance - ancestor_attributes = [] - - if superclass.respond_to?(:guard_inherited_attributes) - ancestor_attributes = superclass.guard_inherited_attributes - end - - ancestor_attributes.concat(@class_inherited_attributes ? @class_inherited_attributes : []).uniq - end - - set_guard_inherited_attributes( - :cwd, - :environment, - :group, - :path, - :user, - :umask - ) - end end end diff --git a/spec/functional/resource/execute_spec.rb b/spec/functional/resource/execute_spec.rb new file mode 100644 index 0000000000..ff358fe045 --- /dev/null +++ b/spec/functional/resource/execute_spec.rb @@ -0,0 +1,113 @@ +# +# Author:: Serdar Sutay (<serdar@opscode.com>) +# Copyright:: Copyright (c) 2014 Opscode, 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' +require 'functional/resource/base' + +describe Chef::Resource::Execute do + let(:execute_resource) { + exec_resource = Chef::Resource::Execute.new("foo_resource", run_context) + + exec_resource.environment(resource_environment) if resource_environment + exec_resource.cwd(resource_cwd) if resource_cwd + exec_resource.command("echo hello") + if guard + if guard_options + exec_resource.only_if(guard, guard_options) + else + exec_resource.only_if(guard) + end + end + exec_resource + } + + let(:resource_environment) { nil } + let(:resource_cwd) { nil } + let(:guard) { nil } + let(:guard_options) { nil } + + describe "when guard is ruby block" do + it "guard can still run" do + execute_resource.only_if do + true + end + execute_resource.run_action(:run) + execute_resource.should be_updated_by_last_action + end + end + + describe "when parent resource sets :cwd" do + let(:resource_cwd) { CHEF_SPEC_DATA } + + let(:guard) { %{ruby -e 'exit 1 unless File.exists?("./big_json_plus_one.json")'} } + + it "guard inherits :cwd from resource" do + execute_resource.run_action(:run) + execute_resource.should be_updated_by_last_action + end + end + + describe "when parent resource sets :environment" do + let(:resource_environment) do + { + "SAWS_SECRET" => "supersecret", + "SAWS_KEY" => "qwerty" + } + end + + # We use ruby command so that we don't need to deal with platform specific + # commands while testing execute resource. We set it so that the resource + # will be updated if the ENV variable is set to what we are intending + let(:guard) { %{ruby -e 'exit 1 if ENV["SAWS_SECRET"] != "supersecret"'} } + + it "guard inherits :environment value from resource" do + execute_resource.run_action(:run) + execute_resource.should be_updated_by_last_action + end + + describe "when guard sets additional values in the :environment" do + let(:guard) { %{ruby -e 'exit 1 if ENV["SGCE_SECRET"] != "regularsecret"'} } + + let(:guard_options) do + { + :environment => { 'SGCE_SECRET' => "regularsecret" } + } + end + + it "guard sees merged value for in its ENV" do + execute_resource.run_action(:run) + execute_resource.should be_updated_by_last_action + end + end + + describe "when guard sets same value in the :environment" do + let(:guard) { %{ruby -e 'exit 1 if ENV["SAWS_SECRET"] != "regularsecret"'} } + + let(:guard_options) do + { + :environment => { 'SAWS_SECRET' => "regularsecret" } + } + end + + it "guard sees value from guard options in its ENV" do + execute_resource.run_action(:run) + execute_resource.should be_updated_by_last_action + end + end + end +end diff --git a/spec/functional/resource/file_spec.rb b/spec/functional/resource/file_spec.rb index 99966f85c8..83f051ea06 100644 --- a/spec/functional/resource/file_spec.rb +++ b/spec/functional/resource/file_spec.rb @@ -17,6 +17,7 @@ # require 'spec_helper' +require 'tmpdir' describe Chef::Resource::File do include_context Chef::Resource::File @@ -30,6 +31,7 @@ describe Chef::Resource::File do run_context = Chef::RunContext.new(node, {}, events) use_path = if opts[:use_relative_path] + Dir.chdir(Dir.tmpdir) File.basename(path) else path diff --git a/spec/support/shared/unit/execute_resource.rb b/spec/support/shared/unit/execute_resource.rb index 609e77ad63..298e0c5baf 100644 --- a/spec/support/shared/unit/execute_resource.rb +++ b/spec/support/shared/unit/execute_resource.rb @@ -76,11 +76,6 @@ shared_examples_for "an execute resource" do @resource.group.should eql(1) end - it "should accept an array for the execution path" do - @resource.path ["woot"] - @resource.path.should eql(["woot"]) - end - it "should accept an integer for the return code" do @resource.returns 1 @resource.returns.should eql(1) @@ -112,7 +107,6 @@ shared_examples_for "an execute resource" do @resource.cwd("/tmp/") @resource.environment({ :one => :two }) @resource.group("legos") - @resource.path(["/var/local/"]) @resource.returns(1) @resource.user("root") end @@ -122,4 +116,3 @@ shared_examples_for "an execute resource" do end end end - diff --git a/spec/unit/config_spec.rb b/spec/unit/config_spec.rb index 41411669e6..cc83ca3c45 100644 --- a/spec/unit/config_spec.rb +++ b/spec/unit/config_spec.rb @@ -417,6 +417,95 @@ describe Chef::Config do end end end + + describe "Chef::Config[:internal_locale]" do + let(:shell_out) do + double("Chef::Mixin::ShellOut double", :exitstatus => 0, :stdout => locales) + end + + let(:locales) { locale_array.join("\n") } + + before do + allow(Chef::Config).to receive(:shell_out_with_systems_locale).with("locale -a").and_return(shell_out) + end + + shared_examples_for "a suitable locale" do + it "returns an English UTF-8 locale" do + expect(Chef::Log).to_not receive(:warn).with(/Please install an English UTF-8 locale for Chef to use/) + expect(Chef::Log).to_not receive(:debug).with(/Defaulting to locale en_US.UTF-8 on Windows/) + expect(Chef::Log).to_not receive(:debug).with(/No usable locale -a command found/) + expect(Chef::Config[:internal_locale]).to eq expected_locale + end + end + + context "when the result includes 'C.UTF-8'" do + include_examples "a suitable locale" do + let(:locale_array) { [expected_locale, "en_US.UTF-8"] } + let(:expected_locale) { "C.UTF-8" } + end + end + + context "when the result includes 'en_US.UTF-8'" do + include_examples "a suitable locale" do + let(:locale_array) { ["en_CA.UTF-8", expected_locale, "en_NZ.UTF-8"] } + let(:expected_locale) { "en_US.UTF-8" } + end + end + + context "when the result includes 'en_US.utf8'" do + include_examples "a suitable locale" do + let(:locale_array) { ["en_CA.utf8", "en_US.utf8", "en_NZ.utf8"] } + let(:expected_locale) { "en_US.UTF-8" } + end + end + + context "when the result includes 'en.UTF-8'" do + include_examples "a suitable locale" do + let(:locale_array) { ["en.ISO8859-1", expected_locale] } + let(:expected_locale) { "en.UTF-8" } + end + end + + context "when the result includes 'en_*.UTF-8'" do + include_examples "a suitable locale" do + let(:locale_array) { [expected_locale, "en_CA.UTF-8", "en_GB.UTF-8"] } + let(:expected_locale) { "en_AU.UTF-8" } + end + end + + context "when the result includes 'en_*.utf8'" do + include_examples "a suitable locale" do + let(:locale_array) { ["en_AU.utf8", "en_CA.utf8", "en_GB.utf8"] } + let(:expected_locale) { "en_AU.UTF-8" } + end + end + + context "when the result does not include 'en_*.UTF-8'" do + let(:locale_array) { ["af_ZA", "af_ZA.ISO8859-1", "af_ZA.ISO8859-15", "af_ZA.UTF-8"] } + + it "should fall back to C locale" do + expect(Chef::Log).to receive(:warn).with("Please install an English UTF-8 locale for Chef to use, falling back to C locale and disabling UTF-8 support.") + expect(Chef::Config[:internal_locale]).to eq 'C' + end + end + + context "on error" do + let(:locale_array) { [] } + + before do + allow(Chef::Config).to receive(:shell_out_with_systems_locale).and_raise("THIS IS AN ERROR") + end + + it "should default to 'en_US.UTF-8'" do + if is_windows + expect(Chef::Log).to receive(:debug).with("Defaulting to locale en_US.UTF-8 on Windows, until it matters that we do something else.") + else + expect(Chef::Log).to receive(:debug).with("No usable locale -a command found, assuming you have en_US.UTF-8 installed.") + end + expect(Chef::Config[:internal_locale]).to eq "en_US.UTF-8" + end + end + end end end end diff --git a/spec/unit/recipe_spec.rb b/spec/unit/recipe_spec.rb index 5b6f3ea55b..cb7581b5fb 100644 --- a/spec/unit/recipe_spec.rb +++ b/spec/unit/recipe_spec.rb @@ -345,6 +345,17 @@ describe Chef::Recipe do end recipe.resources(:zen_master => "lao tzu").something.should eql(false) end + + it "should return the last statement in the definition as the retval" do + crow_define = Chef::ResourceDefinition.new + crow_define.define :crow, :peace => false, :something => true do + "the return val" + end + run_context.definitions[:crow] = crow_define + recipe.crow "mine" do + peace true + end.should eql("the return val") + end end end @@ -423,6 +434,14 @@ describe Chef::Recipe do end describe "tags" do + describe "with the default node object" do + let(:node) { Chef::Node.new } + + it "should return false for any tags" do + recipe.tagged?("foo").should be(false) + end + end + it "should set tags via tag" do recipe.tag "foo" node[:tags].should include("foo") diff --git a/spec/unit/resource/execute_spec.rb b/spec/unit/resource/execute_spec.rb index 8c8dcfb6ca..e1728b7892 100644 --- a/spec/unit/resource/execute_spec.rb +++ b/spec/unit/resource/execute_spec.rb @@ -23,4 +23,9 @@ describe Chef::Resource::Execute do let(:resource_instance_name) { "some command" } let(:execute_resource) { Chef::Resource::Execute.new(resource_instance_name) } it_behaves_like "an execute resource" + + it "default guard interpreter should be :execute interpreter" do + execute_resource.guard_interpreter.should be(:execute) + end + end diff --git a/spec/unit/resource_definition_spec.rb b/spec/unit/resource_definition_spec.rb index f24254cfce..01e28bf091 100644 --- a/spec/unit/resource_definition_spec.rb +++ b/spec/unit/resource_definition_spec.rb @@ -19,24 +19,22 @@ require 'spec_helper' describe Chef::ResourceDefinition do - before(:each) do - @def = Chef::ResourceDefinition.new() - end + let(:defn) { Chef::ResourceDefinition.new() } describe "initialize" do it "should be a Chef::ResourceDefinition" do - @def.should be_a_kind_of(Chef::ResourceDefinition) + expect(defn).to be_a_kind_of(Chef::ResourceDefinition) end it "should not initialize a new node if one is not provided" do - @def.node.should eql(nil) + expect(defn.node).to eql(nil) end it "should accept a node as an argument" do node = Chef::Node.new node.name("bobo") - @def = Chef::ResourceDefinition.new(node) - @def.node.name.should == "bobo" + defn = Chef::ResourceDefinition.new(node) + expect(defn.node.name).to eq("bobo") end end @@ -44,76 +42,76 @@ describe Chef::ResourceDefinition do it "should set the node with node=" do node = Chef::Node.new node.name("bobo") - @def.node = node - @def.node.name.should == "bobo" + defn.node = node + expect(defn.node.name).to eq("bobo") end it "should return the node" do - @def.node = Chef::Node.new - @def.node.should be_a_kind_of(Chef::Node) + defn.node = Chef::Node.new + expect(defn.node).to be_a_kind_of(Chef::Node) end end it "should accept a new definition with a symbol for a name" do - lambda { - @def.define :smoke do + expect { + defn.define :smoke do end - }.should_not raise_error - lambda { - @def.define "george washington" do + }.not_to raise_error + expect { + defn.define "george washington" do end - }.should raise_error(ArgumentError) - @def.name.should eql(:smoke) + }.to raise_error(ArgumentError) + expect(defn.name).to eql(:smoke) end it "should accept a new definition with a hash" do - lambda { - @def.define :smoke, :cigar => "cuban", :cigarette => "marlboro" do + expect { + defn.define :smoke, :cigar => "cuban", :cigarette => "marlboro" do end - }.should_not raise_error + }.not_to raise_error end it "should expose the prototype hash params in the params hash" do - @def.define :smoke, :cigar => "cuban", :cigarette => "marlboro" do; end - @def.params[:cigar].should eql("cuban") - @def.params[:cigarette].should eql("marlboro") + defn.define :smoke, :cigar => "cuban", :cigarette => "marlboro" do; end + expect(defn.params[:cigar]).to eql("cuban") + expect(defn.params[:cigarette]).to eql("marlboro") end it "should store the block passed to define as a proc under recipe" do - @def.define :smoke do + defn.define :smoke do "I am what I am" end - @def.recipe.should be_a_kind_of(Proc) - @def.recipe.call.should eql("I am what I am") + expect(defn.recipe).to be_a_kind_of(Proc) + expect(defn.recipe.call).to eql("I am what I am") end it "should set paramaters based on method_missing" do - @def.mind "to fly" - @def.params[:mind].should eql("to fly") + defn.mind "to fly" + expect(defn.params[:mind]).to eql("to fly") end it "should raise an exception if prototype_params is not a hash" do - lambda { - @def.define :monkey, Array.new do + expect { + defn.define :monkey, Array.new do end - }.should raise_error(ArgumentError) + }.to raise_error(ArgumentError) end it "should raise an exception if define is called without a block" do - lambda { - @def.define :monkey - }.should raise_error(ArgumentError) + expect { + defn.define :monkey + }.to raise_error(ArgumentError) end it "should load a description from a file" do - @def.from_file(File.join(CHEF_SPEC_DATA, "definitions", "test.rb")) - @def.name.should eql(:rico_suave) - @def.params[:rich].should eql("smooth") + defn.from_file(File.join(CHEF_SPEC_DATA, "definitions", "test.rb")) + expect(defn.name).to eql(:rico_suave) + expect(defn.params[:rich]).to eql("smooth") end it "should turn itself into a string based on the name with to_s" do - @def.name = :woot - @def.to_s.should eql("woot") + defn.name = :woot + expect(defn.to_s).to eql("woot") end end diff --git a/spec/unit/resource_spec.rb b/spec/unit/resource_spec.rb index aa69861630..bcc91d52bc 100644 --- a/spec/unit/resource_spec.rb +++ b/spec/unit/resource_spec.rb @@ -349,7 +349,7 @@ describe Chef::Resource do :updated_by_last_action, :before, :supports, :noop, :ignore_failure, :name, :source_line, :action, :retries, :retry_delay, :elapsed_time, - :guard_interpreter, :sensitive ] + :default_guard_interpreter, :guard_interpreter, :sensitive ] (hash.keys - expected_keys).should == [] (expected_keys - hash.keys).should == [] hash[:name].should eql("funk") |