diff options
author | John Keiser <john@johnkeiser.com> | 2015-05-27 16:04:47 -0700 |
---|---|---|
committer | John Keiser <john@johnkeiser.com> | 2015-06-01 08:02:03 -0700 |
commit | a3aba411ccdf1a0a5f236ed3ce2a678a1892e29b (patch) | |
tree | c6c5a4da0143b4928e076f57941d72a9c2baa1b0 /spec | |
parent | 9f8d3fbb943206c27364593b49b875f7254b77be (diff) | |
download | chef-a3aba411ccdf1a0a5f236ed3ce2a678a1892e29b.tar.gz |
Move resource_name up to Resource, and figure out its value automatically
Diffstat (limited to 'spec')
-rw-r--r-- | spec/integration/recipes/provider_choice.rb | 1 | ||||
-rw-r--r-- | spec/integration/recipes/recipe_dsl_spec.rb | 5 | ||||
-rw-r--r-- | spec/support/lib/chef/resource/cat.rb | 1 | ||||
-rw-r--r-- | spec/support/lib/chef/resource/one_two_three_four.rb | 5 | ||||
-rw-r--r-- | spec/support/lib/chef/resource/with_state.rb | 11 | ||||
-rw-r--r-- | spec/support/lib/chef/resource/zen_follower.rb | 5 | ||||
-rw-r--r-- | spec/support/lib/chef/resource/zen_master.rb | 1 | ||||
-rw-r--r-- | spec/unit/lwrp_spec.rb | 8 | ||||
-rw-r--r-- | spec/unit/resource/timestamped_deploy_spec.rb | 3 | ||||
-rw-r--r-- | spec/unit/resource_spec.rb | 103 |
10 files changed, 109 insertions, 34 deletions
diff --git a/spec/integration/recipes/provider_choice.rb b/spec/integration/recipes/provider_choice.rb index 999765a1de..ca8ca16e7a 100644 --- a/spec/integration/recipes/provider_choice.rb +++ b/spec/integration/recipes/provider_choice.rb @@ -10,7 +10,6 @@ describe "Recipe DSL methods" do super @action = :create @allowed_actions = [ :create ] - @resource_name = 'provider_thingy' end provides :provider_thingy def to_s diff --git a/spec/integration/recipes/recipe_dsl_spec.rb b/spec/integration/recipes/recipe_dsl_spec.rb index 079baaa162..b491f343c5 100644 --- a/spec/integration/recipes/recipe_dsl_spec.rb +++ b/spec/integration/recipes/recipe_dsl_spec.rb @@ -9,11 +9,12 @@ describe "Recipe DSL methods" do class BaseThingy < Chef::Resource def initialize(*args, &block) super - @resource_name = 'base_thingy' @allowed_actions = [ :create ] @action = :create end + resource_name 'base_thingy' + class<<self attr_accessor :created_resource attr_accessor :created_provider @@ -54,10 +55,10 @@ describe "Recipe DSL methods" do class Chef::Resource::BackcompatThingy < Chef::Resource def initialize(*args, &block) super - @resource_name = 'backcompat_thingy' @allowed_actions = [ :create ] @action = :create end + resource_name 'backcompat_thingy' end class Chef::Provider::BackcompatThingy < Chef::Provider def load_current_resource diff --git a/spec/support/lib/chef/resource/cat.rb b/spec/support/lib/chef/resource/cat.rb index 641ce28795..01dbe4a272 100644 --- a/spec/support/lib/chef/resource/cat.rb +++ b/spec/support/lib/chef/resource/cat.rb @@ -24,7 +24,6 @@ class Chef attr_accessor :action def initialize(name, run_context=nil) - @resource_name = :cat super @action = "sell" end diff --git a/spec/support/lib/chef/resource/one_two_three_four.rb b/spec/support/lib/chef/resource/one_two_three_four.rb index d7e5ea095e..9199b0e247 100644 --- a/spec/support/lib/chef/resource/one_two_three_four.rb +++ b/spec/support/lib/chef/resource/one_two_three_four.rb @@ -23,11 +23,6 @@ class Chef attr_reader :i_can_count - def initialize(name, run_context) - @resource_name = :one_two_three_four - super - end - def i_can_count(tf) @i_can_count = tf end diff --git a/spec/support/lib/chef/resource/with_state.rb b/spec/support/lib/chef/resource/with_state.rb index 226de0a6d2..91051cca58 100644 --- a/spec/support/lib/chef/resource/with_state.rb +++ b/spec/support/lib/chef/resource/with_state.rb @@ -22,16 +22,9 @@ require 'chef/json_compat' class Chef class Resource class WithState < Chef::Resource - attr_accessor :state - - def initialize(name, run_context=nil) - @resource_name = :with_state - super - end + resource_name :with_state - def state - @state - end + attr_accessor :state end end end diff --git a/spec/support/lib/chef/resource/zen_follower.rb b/spec/support/lib/chef/resource/zen_follower.rb index 590aa0827b..c4f3f44d88 100644 --- a/spec/support/lib/chef/resource/zen_follower.rb +++ b/spec/support/lib/chef/resource/zen_follower.rb @@ -25,11 +25,6 @@ class Chef provides :follower, platform: "zen" - def initialize(name, run_context=nil) - @resource_name = :zen_follower - super - end - def master(arg=nil) if !arg.nil? @master = arg diff --git a/spec/support/lib/chef/resource/zen_master.rb b/spec/support/lib/chef/resource/zen_master.rb index 145dd70e64..18661dc05c 100644 --- a/spec/support/lib/chef/resource/zen_master.rb +++ b/spec/support/lib/chef/resource/zen_master.rb @@ -27,7 +27,6 @@ class Chef attr_reader :peace def initialize(name, run_context=nil) - @resource_name = :zen_master super allowed_actions << :win << :score end diff --git a/spec/unit/lwrp_spec.rb b/spec/unit/lwrp_spec.rb index 659696fd28..8cac70e800 100644 --- a/spec/unit/lwrp_spec.rb +++ b/spec/unit/lwrp_spec.rb @@ -229,14 +229,6 @@ describe "LWRP" do expect(klass.resource_name).to eq(:foo) end - context "when creating a new instance" do - it "raises an exception if resource_name is nil" do - expect { - klass.new('blah') - }.to raise_error(Chef::Exceptions::InvalidResourceSpecification) - end - end - context "lazy default values" do let(:klass) do Class.new(Chef::Resource::LWRPBase) do diff --git a/spec/unit/resource/timestamped_deploy_spec.rb b/spec/unit/resource/timestamped_deploy_spec.rb index eca6c570d4..4ebfdaf059 100644 --- a/spec/unit/resource/timestamped_deploy_spec.rb +++ b/spec/unit/resource/timestamped_deploy_spec.rb @@ -23,11 +23,10 @@ describe Chef::Resource::TimestampedDeploy, "initialize" do static_provider_resolution( resource: Chef::Resource::TimestampedDeploy, provider: Chef::Provider::Deploy::Timestamped, - name: :deploy, + name: :timestamped_deploy, action: :deploy, os: 'linux', platform_family: 'rhel', ) end - diff --git a/spec/unit/resource_spec.rb b/spec/unit/resource_spec.rb index 3bfd63f5ab..accf52ba26 100644 --- a/spec/unit/resource_spec.rb +++ b/spec/unit/resource_spec.rb @@ -336,6 +336,109 @@ describe Chef::Resource do end end + describe "self.resource_name" do + context "When resource_name is not set" do + it "and there is a provides line, it is used for resource_name" do + c = Class.new(Chef::Resource) do + provides :self_resource_name_test_1 + end + + r = c.new('hi') + r.declared_type = :d + expect(c.resource_name).to eq :self_resource_name_test_1 + expect(r.resource_name).to eq :self_resource_name_test_1 + expect(r.declared_type).to eq :d + end + it "and multiple provides lines, the first line is used for resource_name" do + c = Class.new(Chef::Resource) do + provides :self_resource_name_test_2 + provides :self_resource_name_test_3 + end + + r = c.new('hi') + r.declared_type = :d + expect(c.resource_name).to eq :self_resource_name_test_2 + expect(r.resource_name).to eq :self_resource_name_test_2 + expect(r.declared_type).to eq :d + end + it "and there are no provides lines, resource_name is nil" do + c = Class.new(Chef::Resource) do + end + + r = c.new('hi') + r.declared_type = :d + expect(c.resource_name).to be_nil + expect(r.resource_name).to be_nil + expect(r.declared_type).to eq :d + end + it "and there are no provides lines, @resource_name is used" do + c = Class.new(Chef::Resource) do + def initialize(*args, &block) + @resource_name = :blah + super + end + end + + r = c.new('hi') + r.declared_type = :d + expect(c.resource_name).to be_nil + expect(r.resource_name).to eq :blah + expect(r.declared_type).to eq :d + end + end + + it "resource_name without provides is honored" do + c = Class.new(Chef::Resource) do + resource_name 'blah' + end + + r = c.new('hi') + r.declared_type = :d + expect(c.resource_name).to eq :blah + expect(r.resource_name).to eq :blah + expect(r.declared_type).to eq :d + end + it "setting class.resource_name with 'resource_name = blah' overrides and declared_type" do + c = Class.new(Chef::Resource) do + provides :self_resource_name_test_2 + end + c.resource_name = :blah + + r = c.new('hi') + r.declared_type = :d + expect(c.resource_name).to eq :blah + expect(r.resource_name).to eq :blah + expect(r.declared_type).to eq :d + end + it "setting class.resource_name with 'resource_name blah' overrides provides and declared_type" do + c = Class.new(Chef::Resource) do + resource_name :blah + provides :self_resource_name_test_3 + end + + r = c.new('hi') + r.declared_type = :d + expect(c.resource_name).to eq :blah + expect(r.resource_name).to eq :blah + expect(r.declared_type).to eq :d + end + it "setting @resource_name overrides provides and declared_type" do + c = Class.new(Chef::Resource) do + provides :self_resource_name_test_4 + def initialize(*args, &block) + @resource_name = :blah + super + end + end + + r = c.new('hi') + r.declared_type = :d + expect(c.resource_name).to eq :self_resource_name_test_4 + expect(r.resource_name).to eq :blah + expect(r.declared_type).to eq :d + end + end + describe "is" do it "should return the arguments passed with 'is'" do zm = Chef::Resource::ZenMaster.new("coffee") |