summaryrefslogtreecommitdiff
path: root/spec/unit
diff options
context:
space:
mode:
authorTyler Ball <tyleraball@gmail.com>2014-10-17 16:15:12 -0500
committerTyler Ball <tyleraball@gmail.com>2014-10-17 16:15:12 -0500
commit9b3e925188a41bbea954429ac81ffdf65e936eda (patch)
tree4080102aa81bd9f94da69fa4d1ae44472536dd99 /spec/unit
parent901e8eff95c953b91f597e4d83932d5b8803d31a (diff)
parented7a6d2dead738a99ebcb1782d29ca89924093cc (diff)
downloadchef-9b3e925188a41bbea954429ac81ffdf65e936eda.tar.gz
Merge pull request #2216 from opscode/tball/bsd_package_name
Notify a resource by the `resource[name]` key it was written as
Diffstat (limited to 'spec/unit')
-rw-r--r--spec/unit/resource_collection/resource_set_spec.rb199
-rw-r--r--spec/unit/resource_collection_spec.rb52
-rw-r--r--spec/unit/resource_spec.rb4
-rw-r--r--spec/unit/shell/shell_ext_spec.rb2
4 files changed, 216 insertions, 41 deletions
diff --git a/spec/unit/resource_collection/resource_set_spec.rb b/spec/unit/resource_collection/resource_set_spec.rb
new file mode 100644
index 0000000000..29b676f85a
--- /dev/null
+++ b/spec/unit/resource_collection/resource_set_spec.rb
@@ -0,0 +1,199 @@
+#
+# Author:: Tyler Ball (<tball@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::ResourceCollection::ResourceSet do
+ let(:collection) { Chef::ResourceCollection::ResourceSet.new }
+
+ let(:zen_master_name) { "Neo" }
+ let(:zen_master2_name) { "Morpheus" }
+ let(:zen_follower_name) { "Squid" }
+ let(:zen_master) { Chef::Resource::ZenMaster.new(zen_master_name) }
+ let(:zen_master2) { Chef::Resource::ZenMaster.new(zen_master2_name) }
+ let(:zen_follower) { Chef::Resource::ZenFollower.new(zen_follower_name) }
+
+ describe "initialize" do
+ it "should return a Chef::ResourceCollection" do
+ expect(collection).to be_instance_of(Chef::ResourceCollection::ResourceSet)
+ end
+ end
+
+ describe "keys" do
+ it "should return an empty list for an empty ResourceSet" do
+ expect(collection.keys).to eq([])
+ end
+
+ it "should return the keys for a non-empty ResourceSet" do
+ collection.instance_variable_get(:@resources_by_key)["key"] = nil
+ expect(collection.keys).to eq(["key"])
+ end
+ end
+
+ describe "insert_as, lookup and find" do
+ # To validate insert_as you need lookup, and vice-versa - putting all tests in 1 context to avoid duplication
+ it "should accept only Chef::Resources" do
+ expect { collection.insert_as(zen_master) }.to_not raise_error
+ expect { collection.insert_as("string") }.to raise_error(ArgumentError)
+ end
+
+ it "should allow you to lookup resources by a default .to_s" do
+ collection.insert_as(zen_master)
+ expect(collection.lookup(zen_master.to_s)).to equal(zen_master)
+ end
+
+ it "should use a custom type and name to insert" do
+ collection.insert_as(zen_master, "OtherResource", "other_resource")
+ expect(collection.lookup("OtherResource[other_resource]")).to equal(zen_master)
+ end
+
+ it "should raise an exception if you send something strange to lookup" do
+ expect { collection.lookup(:symbol) }.to raise_error(ArgumentError)
+ end
+
+ it "should raise an exception if it cannot find a resource with lookup" do
+ expect { collection.lookup(zen_master.to_s) }.to raise_error(Chef::Exceptions::ResourceNotFound)
+ end
+
+ it "should find a resource by type symbol and name" do
+ collection.insert_as(zen_master)
+ expect(collection.find(:zen_master => zen_master_name)).to equal(zen_master)
+ end
+
+ it "should find a resource by type symbol and array of names" do
+ collection.insert_as(zen_master)
+ collection.insert_as(zen_master2)
+ check_by_names(collection.find(:zen_master => [zen_master_name,zen_master2_name]), zen_master_name, zen_master2_name)
+ end
+
+ it "should find a resource by type symbol and array of names with custom names" do
+ collection.insert_as(zen_master, :zzz, "name1")
+ collection.insert_as(zen_master2, :zzz, "name2")
+ check_by_names(collection.find( :zzz => ["name1","name2"]), zen_master_name, zen_master2_name)
+ end
+
+ it "should find resources of multiple kinds (:zen_master => a, :zen_follower => b)" do
+ collection.insert_as(zen_master)
+ collection.insert_as(zen_follower)
+ check_by_names(collection.find(:zen_master => [zen_master_name], :zen_follower => [zen_follower_name]),
+ zen_master_name, zen_follower_name)
+ end
+
+ it "should find resources of multiple kinds (:zen_master => a, :zen_follower => b with custom names)" do
+ collection.insert_as(zen_master, :zzz, "name1")
+ collection.insert_as(zen_master2, :zzz, "name2")
+ collection.insert_as(zen_follower, :yyy, "name3")
+ check_by_names(collection.find(:zzz => ["name1","name2"], :yyy => ["name3"]),
+ zen_master_name, zen_follower_name, zen_master2_name)
+ end
+
+ it "should find a resource by string zen_master[a]" do
+ collection.insert_as(zen_master)
+ expect(collection.find("zen_master[#{zen_master_name}]")).to eq(zen_master)
+ end
+
+ it "should find a resource by string zen_master[a] with custom names" do
+ collection.insert_as(zen_master, :zzz, "name1")
+ expect(collection.find("zzz[name1]")).to eq(zen_master)
+ end
+
+ it "should find resources by strings of zen_master[a,b]" do
+ collection.insert_as(zen_master)
+ collection.insert_as(zen_master2)
+ check_by_names(collection.find("zen_master[#{zen_master_name},#{zen_master2_name}]"),
+ zen_master_name, zen_master2_name)
+ end
+
+ it "should find resources by strings of zen_master[a,b] with custom names" do
+ collection.insert_as(zen_master, :zzz, "name1")
+ collection.insert_as(zen_master2, :zzz, "name2")
+ check_by_names(collection.find("zzz[name1,name2]"),
+ zen_master_name, zen_master2_name)
+ end
+
+ it "should find resources of multiple types by strings of zen_master[a]" do
+ collection.insert_as(zen_master)
+ collection.insert_as(zen_follower)
+ check_by_names(collection.find("zen_master[#{zen_master_name}]", "zen_follower[#{zen_follower_name}]"),
+ zen_master_name, zen_follower_name)
+ end
+
+ it "should find resources of multiple types by strings of zen_master[a] with custom names" do
+ collection.insert_as(zen_master, :zzz, "name1")
+ collection.insert_as(zen_master2, :zzz, "name2")
+ collection.insert_as(zen_follower, :yyy, "name3")
+ check_by_names(collection.find("zzz[name1,name2]", "yyy[name3]"),
+ zen_master_name, zen_follower_name,zen_master2_name)
+ end
+
+ it "should only keep the last copy when multiple instances of a Resource are inserted" do
+ collection.insert_as(zen_master)
+ expect(collection.find("zen_master[#{zen_master_name}]")).to eq(zen_master)
+ new_zm =zen_master.dup
+ new_zm.retries = 10
+ expect(new_zm).to_not eq(zen_master)
+ collection.insert_as(new_zm)
+ expect(collection.find("zen_master[#{zen_master_name}]")).to eq(new_zm)
+ end
+
+ it "should raise an exception if you pass a bad name to resources" do
+ expect { collection.find("michael jackson") }.to raise_error(ArgumentError)
+ end
+
+ it "should raise an exception if you pass something other than a string or hash to resource" do
+ expect { collection.find([Array.new]) }.to raise_error(ArgumentError)
+ end
+
+ it "raises an error when attempting to find a resource that does not exist" do
+ expect { collection.find("script[nonesuch]") }.to raise_error(Chef::Exceptions::ResourceNotFound)
+ end
+ end
+
+ describe "validate_lookup_spec!" do
+ it "accepts a string of the form 'resource_type[resource_name]'" do
+ expect(collection.validate_lookup_spec!("resource_type[resource_name]")).to be_true
+ end
+
+ it "accepts a single-element :resource_type => 'resource_name' Hash" do
+ expect(collection.validate_lookup_spec!(:service => "apache2")).to be_true
+ end
+
+ it "accepts a chef resource object" do
+ expect(collection.validate_lookup_spec!(zen_master)).to be_true
+ end
+
+ it "rejects a malformed query string" do
+ expect { collection.validate_lookup_spec!("resource_type[missing-end-bracket") }.to \
+ raise_error(Chef::Exceptions::InvalidResourceSpecification)
+ end
+
+ it "rejects an argument that is not a String, Hash, or Chef::Resource" do
+ expect { collection.validate_lookup_spec!(Object.new) }.to \
+ raise_error(Chef::Exceptions::InvalidResourceSpecification)
+ end
+
+ end
+
+ def check_by_names(results, *names)
+ expect(results.size).to eq(names.size)
+ names.each do |name|
+ expect(results.detect{|r| r.name == name}).to_not eq(nil)
+ end
+ end
+
+end
diff --git a/spec/unit/resource_collection_spec.rb b/spec/unit/resource_collection_spec.rb
index cf119f1ab0..a575d2996c 100644
--- a/spec/unit/resource_collection_spec.rb
+++ b/spec/unit/resource_collection_spec.rb
@@ -26,6 +26,10 @@ describe Chef::ResourceCollection do
@resource = Chef::Resource::ZenMaster.new("makoto")
end
+ it "should throw an error when calling a non-delegated method" do
+ expect { @rc.not_a_method }.to raise_error(NoMethodError)
+ end
+
describe "initialize" do
it "should return a Chef::ResourceCollection" do
@rc.should be_kind_of(Chef::ResourceCollection)
@@ -35,7 +39,7 @@ describe Chef::ResourceCollection do
describe "[]" do
it "should accept Chef::Resources through [index]" do
lambda { @rc[0] = @resource }.should_not raise_error
- lambda { @rc[0] = "string" }.should raise_error
+ lambda { @rc[0] = "string" }.should raise_error(ArgumentError)
end
it "should allow you to fetch Chef::Resources by position" do
@@ -47,7 +51,7 @@ describe Chef::ResourceCollection do
describe "push" do
it "should accept Chef::Resources through pushing" do
lambda { @rc.push(@resource) }.should_not raise_error
- lambda { @rc.push("string") }.should raise_error
+ lambda { @rc.push("string") }.should raise_error(ArgumentError)
end
end
@@ -60,7 +64,12 @@ describe Chef::ResourceCollection do
describe "insert" do
it "should accept only Chef::Resources" do
lambda { @rc.insert(@resource) }.should_not raise_error
- lambda { @rc.insert("string") }.should raise_error
+ lambda { @rc.insert("string") }.should raise_error(ArgumentError)
+ end
+
+ it "should accept named arguments in any order" do
+ @rc.insert(@resource, :instance_name => 'foo', :resource_type =>'bar')
+ expect(@rc[0]).to eq(@resource)
end
it "should append resources to the end of the collection when not executing a run" do
@@ -88,39 +97,6 @@ describe Chef::ResourceCollection do
end
end
- describe "insert_at" do
- it "should accept only Chef::Resources" do
- lambda { @rc.insert_at(0, @resource, @resource) }.should_not raise_error
- lambda { @rc.insert_at(0, "string") }.should raise_error
- lambda { @rc.insert_at(0, @resource, "string") }.should raise_error
- end
-
- it "should toss an error if it receives a bad index" do
- @rc.insert_at(10, @resource)
- end
-
- it "should insert resources at the beginning when asked" do
- @rc.insert(Chef::Resource::ZenMaster.new('1'))
- @rc.insert(Chef::Resource::ZenMaster.new('2'))
- @rc.insert_at(0, Chef::Resource::ZenMaster.new('X'))
- @rc.all_resources.map { |r| r.name }.should == [ 'X', '1', '2' ]
- end
-
- it "should insert resources in the middle when asked" do
- @rc.insert(Chef::Resource::ZenMaster.new('1'))
- @rc.insert(Chef::Resource::ZenMaster.new('2'))
- @rc.insert_at(1, Chef::Resource::ZenMaster.new('X'))
- @rc.all_resources.map { |r| r.name }.should == [ '1', 'X', '2' ]
- end
-
- it "should insert resources at the end when asked" do
- @rc.insert(Chef::Resource::ZenMaster.new('1'))
- @rc.insert(Chef::Resource::ZenMaster.new('2'))
- @rc.insert_at(2, Chef::Resource::ZenMaster.new('X'))
- @rc.all_resources.map { |r| r.name }.should == [ '1', '2', 'X' ]
- end
- end
-
describe "each" do
it "should allow you to iterate over every resource in the collection" do
load_up_resources
@@ -300,13 +276,13 @@ describe Chef::ResourceCollection do
describe "provides access to the raw resources array" do
it "returns the resources via the all_resources method" do
- @rc.all_resources.should equal(@rc.instance_variable_get(:@resources))
+ @rc.all_resources.should equal(@rc.instance_variable_get(:@resource_list).instance_variable_get(:@resources))
end
end
describe "provides access to stepable iterator" do
it "returns the iterator object" do
- @rc.instance_variable_set(:@iterator, :fooboar)
+ @rc.instance_variable_get(:@resource_list).instance_variable_set(:@iterator, :fooboar)
@rc.iterator.should == :fooboar
end
end
diff --git a/spec/unit/resource_spec.rb b/spec/unit/resource_spec.rb
index 5dfc17f333..bcc91d52bc 100644
--- a/spec/unit/resource_spec.rb
+++ b/spec/unit/resource_spec.rb
@@ -174,12 +174,12 @@ describe Chef::Resource do
end
it "should load the attributes of a prior resource" do
- @resource.load_prior_resource
+ @resource.load_prior_resource(@resource.resource_name, @resource.name)
@resource.supports.should == { :funky => true }
end
it "should not inherit the action from the prior resource" do
- @resource.load_prior_resource
+ @resource.load_prior_resource(@resource.resource_name, @resource.name)
@resource.action.should_not == @prior_resource.action
end
end
diff --git a/spec/unit/shell/shell_ext_spec.rb b/spec/unit/shell/shell_ext_spec.rb
index c24acbca3e..8485b66d23 100644
--- a/spec/unit/shell/shell_ext_spec.rb
+++ b/spec/unit/shell/shell_ext_spec.rb
@@ -121,7 +121,7 @@ describe Shell::Extensions do
Shell.session.stub(:rebuild_context)
events = Chef::EventDispatch::Dispatcher.new
run_context = Chef::RunContext.new(Chef::Node.new, {}, events)
- run_context.resource_collection.instance_variable_set(:@iterator, :the_iterator)
+ run_context.resource_collection.instance_variable_get(:@resource_list).instance_variable_set(:@iterator, :the_iterator)
Shell.session.run_context = run_context
@root_context.chef_run.should == :the_iterator
end