summaryrefslogtreecommitdiffstats
path: root/spec/unit
diff options
context:
space:
mode:
Diffstat (limited to 'spec/unit')
-rwxr-xr-xspec/unit/indirector/indirection.rb122
-rwxr-xr-xspec/unit/indirector/indirector.rb133
2 files changed, 129 insertions, 126 deletions
diff --git a/spec/unit/indirector/indirection.rb b/spec/unit/indirector/indirection.rb
index e9b10c8c7..2aead4abf 100755
--- a/spec/unit/indirector/indirection.rb
+++ b/spec/unit/indirector/indirection.rb
@@ -10,44 +10,93 @@ describe Puppet::Indirector::Indirection, " when initializing" do
@indirection.name.should == :myind
end
- it "should set any passed options" do
- @indirection = Puppet::Indirector::Indirection.new(:myind, :to => :node_source)
- @indirection.to.should == :node_source
+ it "should require indirections to have unique names" do
+ @indirection = Puppet::Indirector::Indirection.new(:test)
+ proc { Puppet::Indirector::Indirection.new(:test) }.should raise_error(ArgumentError)
end
- it "should only allow valid configuration parameters to be specified as :to targets" do
- proc { Puppet::Indirector::Indirection.new(:myind, :to => :no_such_variable) }.should raise_error(ArgumentError)
+ after do
+ @indirection.delete if defined? @indirection
+ end
+end
+
+describe Puppet::Indirector::Indirection, " when choosing terminus types" do
+ before do
+ @indirection = Puppet::Indirector::Indirection.new(:test)
+ @terminus = mock 'terminus'
+ @terminus_class = stub 'terminus class', :new => @terminus
+ end
+
+ it "should follow a convention on using per-model configuration parameters to determine the terminus class" do
+ Puppet.config.expects(:valid?).with('test_terminus').returns(true)
+ Puppet.config.expects(:value).with('test_terminus').returns(:foo)
+ Puppet::Indirector.expects(:terminus).with(:test, :foo).returns(@terminus_class)
+ @indirection.terminus.should equal(@terminus)
+ end
+
+ it "should use a default system-wide configuration parameter parameter to determine the terminus class when no
+ per-model configuration parameter is available" do
+ Puppet.config.expects(:valid?).with('test_terminus').returns(false)
+ Puppet.config.expects(:value).with(:default_terminus).returns(:foo)
+ Puppet::Indirector.expects(:terminus).with(:test, :foo).returns(@terminus_class)
+ @indirection.terminus.should equal(@terminus)
+ end
+
+ it "should select the specified terminus class if a name is provided" do
+ Puppet::Indirector.expects(:terminus).with(:test, :foo).returns(@terminus_class)
+ @indirection.terminus(:foo).should equal(@terminus)
+ end
+
+ it "should fail when the terminus class name is an empty string" do
+ proc { @indirection.terminus("") }.should raise_error(ArgumentError)
+ end
+
+ it "should fail when the terminus class name is nil" do
+ proc { @indirection.terminus(nil) }.should raise_error(ArgumentError)
+ end
+
+ it "should fail when the specified terminus class cannot be found" do
+ Puppet::Indirector.expects(:terminus).with(:test, :foo).returns(nil)
+ proc { @indirection.terminus(:foo) }.should raise_error(ArgumentError)
end
after do
- if defined? @indirection
- @indirection.delete
- end
+ @indirection.delete if defined? @indirection
end
end
-describe Puppet::Indirector, " when managing termini" do
+describe Puppet::Indirector::Indirection, " when managing terminus instances" do
before do
- @indirection = Puppet::Indirector::Indirection.new(:node, :to => :node_source)
+ @indirection = Puppet::Indirector::Indirection.new(:test)
+ @terminus = mock 'terminus'
+ @terminus_class = mock 'terminus class'
+ Puppet::Indirector.stubs(:terminus).with(:test, :foo).returns(@terminus_class)
+ end
+
+ it "should create an instance of the chosen terminus class" do
+ @terminus_class.stubs(:new).returns(@terminus)
+ @indirection.terminus(:foo).should equal(@terminus)
end
- it "should allow the clearance of cached termini" do
+ it "should allow the clearance of cached terminus instances" do
terminus1 = mock 'terminus1'
terminus2 = mock 'terminus2'
- Puppet::Indirector.terminus(:node, Puppet[:node_source]).stubs(:new).returns(terminus1, terminus2, ArgumentError)
- @indirection.terminus.should equal(terminus1)
+ @terminus_class.stubs(:new).returns(terminus1, terminus2, ArgumentError)
+ @indirection.terminus(:foo).should equal(terminus1)
@indirection.class.clear_cache
- @indirection.terminus.should equal(terminus2)
+ @indirection.terminus(:foo).should equal(terminus2)
end
# Make sure it caches the terminus.
- it "should return the same terminus each time" do
- @indirection = Puppet::Indirector::Indirection.new(:node, :to => :node_source)
- @terminus = mock 'new'
- Puppet::Indirector.terminus(:node, Puppet[:node_source]).expects(:new).returns(@terminus)
+ it "should return the same terminus instance each time for a given name" do
+ @terminus_class.stubs(:new).returns(@terminus)
+ @indirection.terminus(:foo).should equal(@terminus)
+ @indirection.terminus(:foo).should equal(@terminus)
+ end
- @indirection.terminus.should equal(@terminus)
- @indirection.terminus.should equal(@terminus)
+ it "should not create a terminus instance until one is actually needed" do
+ Puppet::Indirector.expects(:terminus).never
+ indirection = Puppet::Indirector::Indirection.new(:lazytest)
end
after do
@@ -55,3 +104,36 @@ describe Puppet::Indirector, " when managing termini" do
Puppet::Indirector::Indirection.clear_cache
end
end
+
+describe Puppet::Indirector::Indirection do
+ before do
+ @indirection = Puppet::Indirector::Indirection.new(:test)
+ @terminus = mock 'terminus'
+ @indirection.stubs(:terminus).returns(@terminus)
+ end
+
+ it "should handle lookups of a model instance by letting the appropriate terminus perform the lookup" do
+ @terminus.expects(:find).with(:mything).returns(:whev)
+ @indirection.find(:mything).should == :whev
+ end
+
+ it "should handle removing model instances from a terminus letting the appropriate terminus remove the instance" do
+ @terminus.expects(:destroy).with(:mything).returns(:whev)
+ @indirection.destroy(:mything).should == :whev
+ end
+
+ it "should handle searching for model instances by letting the appropriate terminus find the matching instances" do
+ @terminus.expects(:search).with(:mything).returns(:whev)
+ @indirection.search(:mything).should == :whev
+ end
+
+ it "should handle storing a model instance by letting the appropriate terminus store the instance" do
+ @terminus.expects(:save).with(:mything).returns(:whev)
+ @indirection.save(:mything).should == :whev
+ end
+
+ after do
+ @indirection.delete
+ Puppet::Indirector::Indirection.clear_cache
+ end
+end
diff --git a/spec/unit/indirector/indirector.rb b/spec/unit/indirector/indirector.rb
index b5a70cd8f..1a9704e5e 100755
--- a/spec/unit/indirector/indirector.rb
+++ b/spec/unit/indirector/indirector.rb
@@ -2,26 +2,6 @@ require File.dirname(__FILE__) + '/../../spec_helper'
require 'puppet/defaults'
require 'puppet/indirector'
-describe Puppet::Indirector do
- it "should provide a way to clear all registrations" do
- Puppet::Indirector.should respond_to(:reset)
- end
-
- it "should provide a way to access a list of all registered models" do
- Puppet::Indirector.should respond_to(:indirections)
- end
-end
-
-describe Puppet::Indirector, "when no models are registered" do
- before do
- Puppet::Indirector.reset
- end
-
- it "should provide an empty list of registered models" do
- Puppet::Indirector.indirections.should == {}
- end
-end
-
describe Puppet::Indirector, " when available to a model" do
before do
@thingie = Class.new do
@@ -32,86 +12,8 @@ describe Puppet::Indirector, " when available to a model" do
it "should provide a way for the model to register an indirection under a name" do
@thingie.should respond_to(:indirects)
end
-end
-
-describe Puppet::Indirector, "when registering an indirection" do
- before do
- Puppet::Indirector.reset
- @thingie = Class.new do
- extend Puppet::Indirector
- end
- Puppet::Indirector.stubs(:terminus_for_indirection).returns(:ldap)
- @terminus = mock 'terminus'
- @terminus_class = stub 'terminus class', :new => @terminus
- Puppet::Indirector.stubs(:terminus).returns(@terminus_class)
- end
-
- it "should require a name when registering a model" do
- Proc.new {@thingie.send(:indirects) }.should raise_error(ArgumentError)
- end
-
- it "should require each model to be registered under a unique name" do
- @thingie.send(:indirects, :name)
- Proc.new {@thingie.send(:indirects, :name)}.should raise_error(ArgumentError)
- end
-
- it "should not allow a model to register under multiple names" do
- @thingie.send(:indirects, :first)
- Proc.new {@thingie.send(:indirects, :second)}.should raise_error(ArgumentError)
- end
-
- it "should allow finding an instance of a model in a collection" do
- @thingie.send(:indirects, :first)
- @thingie.should respond_to(:find)
- end
-
- it "should allow removing an instance of a model from a collection" do
- @thingie.send(:indirects, :first)
- @thingie.should respond_to(:destroy)
- end
- it "should allow finding all matching model instances in a collection" do
- @thingie.send(:indirects, :first)
- @thingie.should respond_to(:search)
- end
-
- it "should allow for storing a model instance in a collection" do
- @thing = Class.new do
- extend Puppet::Indirector
- indirects :thing
- end.new
- @thing.should respond_to(:save)
- end
-
- it "should provide a way to get a handle to the terminus for a model" do
- Puppet::Indirector.expects(:terminus_for_indirection).with(:node).returns(:ldap)
- terminus = mock 'terminus'
- terminus_class = stub 'terminus class', :new => terminus
- Puppet::Indirector.expects(:terminus).returns(terminus_class)
- @thingie.send(:indirects, :node)
- @thingie.indirection.should == terminus
- end
-
- it "should list the model in a list of known indirections" do
- @thingie.send(:indirects, :name)
- Puppet::Indirector.indirections[:name].should == @thingie
- end
-
- # when dealing with Terminus methods
- it "should consult a per-model configuration to determine what kind of collection a model is being stored in" do
- Puppet::Indirector.expects(:terminus_for_indirection).with(:node).returns(:ldap)
- @thingie.send(:indirects, :node)
- end
-
- it "should use the collection type described in the per-model configuration" do
- terminus = mock 'terminus'
- terminus_class = stub 'terminus class', :new => terminus
- Puppet::Indirector.expects(:terminus_for_indirection).with(:foo).returns(:bar)
- Puppet::Indirector.expects(:terminus).with(:foo, :bar).returns(terminus_class)
- @thingie.send(:indirects, :foo)
- end
-
- it "should handle lookups of a model instance by letting the terminus perform the lookup" do
+ it "should give model the ability to lookup a model instance by letting the indirection perform the lookup" do
@thingie.send(:indirects, :node)
mock_terminus = mock('Terminus')
mock_terminus.expects(:find)
@@ -119,7 +21,7 @@ describe Puppet::Indirector, "when registering an indirection" do
@thingie.find
end
- it "should handle removing model instances from a collection letting the terminus remove the instance" do
+ it "should give model the ability to remove model instances from a terminus by letting the indirection remove the instance" do
@thingie.send(:indirects, :node)
mock_terminus = mock('Terminus')
mock_terminus.expects(:destroy)
@@ -127,7 +29,7 @@ describe Puppet::Indirector, "when registering an indirection" do
@thingie.destroy
end
- it "should handle searching for model instances by letting the terminus find the matching instances" do
+ it "should give model the ability to search for model instances by letting the indirection find the matching instances" do
@thingie.send(:indirects, :node)
mock_terminus = mock('Terminus')
mock_terminus.expects(:search)
@@ -135,24 +37,43 @@ describe Puppet::Indirector, "when registering an indirection" do
@thingie.search
end
- it "should handle storing a model instance by letting the terminus store the instance" do
+ it "should give model the ability to store a model instance by letting the indirection store the instance" do
@thingie.send(:indirects, :node)
mock_terminus = mock('Terminus')
mock_terminus.expects(:save)
@thingie.expects(:indirection).returns(mock_terminus)
@thingie.new.save
end
-
- it "should provide the same terminus for a given registered model"
+end
- it "should not access the collection for a registered model until that collection is actually needed"
+describe Puppet::Indirector, "when registering an indirection" do
+ before do
+ Puppet::Indirector.reset
+ @thingie = Class.new do
+ extend Puppet::Indirector
+ end
+ Puppet::Indirector.stubs(:terminus_for_indirection).returns(:ldap)
+ @terminus = mock 'terminus'
+ @terminus_class = stub 'terminus class', :new => @terminus
+ Puppet::Indirector.stubs(:terminus).returns(@terminus_class)
+ end
+
+ it "should require a name when registering a model" do
+ Proc.new {@thingie.send(:indirects) }.should raise_error(ArgumentError)
+ end
+
+ it "should not allow a model to register under multiple names" do
+ @thingie.send(:indirects, :first)
+ Proc.new {@thingie.send(:indirects, :second)}.should raise_error(ArgumentError)
+ end
+
+ it "should create an indirection instance to manage each indirecting model"
# TODO: node lookup retries/searching
end
-
# describe Puppet::Indirector::Terminus do
# it "should register itself" # ???
#