summaryrefslogtreecommitdiffstats
path: root/spec
diff options
context:
space:
mode:
authorMichael V. O'Brien <michael@reductivelabs.com>2007-09-25 17:23:36 -0500
committerMichael V. O'Brien <michael@reductivelabs.com>2007-09-25 17:23:36 -0500
commit93f64885100eecb4c235d08e1f9cd266e6d789ad (patch)
treea5ab7d8236ffc84ef05124d03c1cb9db89baf4a9 /spec
parentdf1879b814c25cd3564abaa3064e0cdd6ef50eb4 (diff)
parentfa643e61c7451c2c46623d2c801a42c6c7640e1e (diff)
Merge branch 'master' of git://reductivelabs.com/puppet
Diffstat (limited to 'spec')
-rwxr-xr-xspec/unit/indirector/code/facts.rb70
-rwxr-xr-xspec/unit/indirector/indirection.rb145
-rwxr-xr-xspec/unit/indirector/terminus.rb30
-rwxr-xr-xspec/unit/indirector/yaml.rb6
-rwxr-xr-xspec/unit/indirector/yaml/configuration.rb25
-rwxr-xr-xspec/unit/indirector/yaml/node.rb25
6 files changed, 266 insertions, 35 deletions
diff --git a/spec/unit/indirector/code/facts.rb b/spec/unit/indirector/code/facts.rb
new file mode 100755
index 000000000..9b645617f
--- /dev/null
+++ b/spec/unit/indirector/code/facts.rb
@@ -0,0 +1,70 @@
+#!/usr/bin/env ruby
+#
+# Created by Luke Kanies on 2007-9-23.
+# Copyright (c) 2007. All rights reserved.
+
+require File.dirname(__FILE__) + '/../../../spec_helper'
+
+require 'puppet/indirector/code/facts'
+
+describe Puppet::Indirector::Code::Facts do
+ it "should be a subclass of the Code terminus" do
+ Puppet::Indirector::Code::Facts.superclass.should equal(Puppet::Indirector::Code)
+ end
+
+ it "should have documentation" do
+ Puppet::Indirector::Code::Facts.doc.should_not be_nil
+ end
+
+ it "should be registered with the configuration store indirection" do
+ indirection = Puppet::Indirector::Indirection.instance(:facts)
+ Puppet::Indirector::Code::Facts.indirection.should equal(indirection)
+ end
+
+ it "should have its name set to :facts" do
+ Puppet::Indirector::Code::Facts.name.should == :facts
+ end
+end
+
+module TestingCodeFacts
+ def setup
+ @facter = Puppet::Indirector::Code::Facts.new
+ Facter.stubs(:to_hash).returns({})
+ @name = "me"
+ @facts = @facter.find(@name)
+ end
+end
+
+describe Puppet::Indirector::Code::Facts, " when finding facts" do
+ include TestingCodeFacts
+
+ it "should return a Facts instance" do
+ @facts.should be_instance_of(Puppet::Node::Facts)
+ end
+
+ it "should return a Facts instance with the provided key as the name" do
+ @facts.name.should == @name
+ end
+
+ it "should return the Facter facts as the values in the Facts instance" do
+ Facter.expects(:to_hash).returns("one" => "two")
+ facts = @facter.find(@name)
+ facts.values["one"].should == "two"
+ end
+end
+
+describe Puppet::Indirector::Code::Facts, " when saving facts" do
+ include TestingCodeFacts
+
+ it "should fail" do
+ proc { @facter.save(@facts) }.should raise_error(Puppet::DevError)
+ end
+end
+
+describe Puppet::Indirector::Code::Facts, " when destroying facts" do
+ include TestingCodeFacts
+
+ it "should fail" do
+ proc { @facter.destroy(@facts) }.should raise_error(Puppet::DevError)
+ end
+end
diff --git a/spec/unit/indirector/indirection.rb b/spec/unit/indirector/indirection.rb
index 326b6b470..41f50522f 100755
--- a/spec/unit/indirector/indirection.rb
+++ b/spec/unit/indirector/indirection.rb
@@ -4,6 +4,39 @@ require File.dirname(__FILE__) + '/../../spec_helper'
require 'puppet/indirector'
+describe Puppet::Indirector::Indirection do
+ before do
+ @indirection = Puppet::Indirector::Indirection.new(mock('model'), :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
+
describe Puppet::Indirector::Indirection, " when initializing" do
it "should keep a reference to the indirecting model" do
model = mock 'model'
@@ -126,32 +159,98 @@ describe Puppet::Indirector::Indirection, " when managing terminus instances" do
end
end
-describe Puppet::Indirector::Indirection do
- before do
+describe Puppet::Indirector::Indirection, " when deciding whether to cache" do
+ before do
@indirection = Puppet::Indirector::Indirection.new(mock('model'), :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
+ end
+
+ it "should not use a cache if there no cache setting" do
+ Puppet.settings.expects(:valid?).with("test_cache").returns(false)
+ @indirection.expects(:cache).never
+ @terminus.stubs(:save)
+ @indirection.save(:whev)
+ end
+
+ it "should not use a cache if the cache setting is set to 'none'" do
+ Puppet.settings.expects(:valid?).with("test_cache").returns(true)
+ Puppet.settings.expects(:value).with("test_cache").returns("none")
+ @indirection.expects(:cache).never
+ @terminus.stubs(:save)
+ @indirection.save(:whev)
+ end
+
+ it "should use a cache if there is a related cache setting and it is not set to 'none'" do
+ Puppet.settings.expects(:valid?).with("test_cache").returns(true)
+ Puppet.settings.expects(:value).with("test_cache").returns("something")
+ cache = mock 'cache'
+ cache.expects(:save).with(:whev)
+ @indirection.expects(:cache).returns(cache)
+ @terminus.stubs(:save)
+ @indirection.save(:whev)
+ end
+
+ after do
+ @indirection.delete
+ Puppet::Indirector::Indirection.clear_cache
+ end
+end
+
+describe Puppet::Indirector::Indirection, " when using a cache" do
+ before do
+ Puppet.settings.stubs(:valid?).returns(true)
+ Puppet.settings.stubs(:value).with("test_terminus").returns("test_terminus")
+ @terminus_class = mock 'terminus_class'
+ @terminus = mock 'terminus'
+ @terminus_class.stubs(:new).returns(@terminus)
+ @cache = mock 'cache'
+ @cache_class = mock 'cache_class'
+ Puppet::Indirector::Terminus.stubs(:terminus_class).with(:cache_terminus, :test).returns(@cache_class)
+ Puppet::Indirector::Terminus.stubs(:terminus_class).with(:test_terminus, :test).returns(@terminus_class)
+ @indirection = Puppet::Indirector::Indirection.new(mock('model'), :test)
+ end
+
+ it "should copy all writing indirection calls to the cache terminus" do
+ @cache_class.expects(:new).returns(@cache)
+ Puppet.settings.stubs(:value).with("test_cache").returns("cache_terminus")
+ @cache.expects(:save).with(:whev)
+ @terminus.stubs(:save)
+ @indirection.save(:whev)
+ end
+
+ it "should not create a cache terminus at initialization" do
+ # This is weird, because all of the code is in the setup. If we got
+ # new() called on the cache class, we'd get an exception here.
+ end
+
+ it "should reuse the cache terminus" do
+ @cache_class.expects(:new).returns(@cache)
+ Puppet.settings.stubs(:value).with("test_cache").returns("cache_terminus")
+ @indirection.cache.should equal(@cache)
+ @indirection.cache.should equal(@cache)
+ end
+
+ it "should remove the cache terminus when all other terminus instances are cleared" do
+ cache2 = mock 'cache2'
+ @cache_class.stubs(:new).returns(@cache, cache2)
+ Puppet.settings.stubs(:value).with("test_cache").returns("cache_terminus")
+ @indirection.cache.should equal(@cache)
+ @indirection.clear_cache
+ @indirection.cache.should equal(cache2)
+ end
+
+ it "should look up the cache name when recreating the cache terminus after terminus instances have been cleared" do
+ cache_class2 = mock 'cache_class2'
+ cache2 = mock 'cache2'
+ cache_class2.expects(:new).returns(cache2)
+ @cache_class.expects(:new).returns(@cache)
+ Puppet::Indirector::Terminus.stubs(:terminus_class).with(:other_cache, :test).returns(cache_class2)
+ Puppet.settings.stubs(:value).with("test_cache").returns("cache_terminus", "other_cache")
+ @indirection.cache.should equal(@cache)
+ @indirection.clear_cache
+ @indirection.cache.should equal(cache2)
+ end
after do
@indirection.delete
diff --git a/spec/unit/indirector/terminus.rb b/spec/unit/indirector/terminus.rb
index dc86cf315..44180cf4b 100755
--- a/spec/unit/indirector/terminus.rb
+++ b/spec/unit/indirector/terminus.rb
@@ -6,8 +6,8 @@ describe Puppet::Indirector::Terminus do
before do
Puppet::Indirector::Terminus.stubs(:register_terminus_class)
- @indirection = stub 'indirection', :name => :mystuff, :register_terminus_type => nil
- Puppet::Indirector::Indirection.stubs(:instance).with(:mystuff).returns(@indirection)
+ @indirection = stub 'indirection', :name => :my_stuff, :register_terminus_type => nil
+ Puppet::Indirector::Indirection.stubs(:instance).with(:my_stuff).returns(@indirection)
@abstract_terminus = Class.new(Puppet::Indirector::Terminus) do
def self.to_s
"Abstract"
@@ -91,7 +91,7 @@ describe Puppet::Indirector::Terminus, " when managing terminus classes" do
end
it "should fail when no indirection can be found" do
- Puppet::Indirector::Indirection.expects(:instance).with(:myindirection).returns(nil)
+ Puppet::Indirector::Indirection.expects(:instance).with(:my_indirection).returns(nil)
@abstract_terminus = Class.new(Puppet::Indirector::Terminus) do
def self.to_s
@@ -109,10 +109,10 @@ describe Puppet::Indirector::Terminus, " when managing terminus classes" do
it "should register the terminus class with the terminus base class" do
Puppet::Indirector::Terminus.expects(:register_terminus_class).with do |type|
- type.terminus_type == :abstract and type.name == :myindirection
+ type.terminus_type == :abstract and type.name == :my_indirection
end
@indirection = stub 'indirection', :name => :myind, :register_terminus_type => nil
- Puppet::Indirector::Indirection.expects(:instance).with(:myindirection).returns(@indirection)
+ Puppet::Indirector::Indirection.expects(:instance).with(:my_indirection).returns(@indirection)
@abstract_terminus = Class.new(Puppet::Indirector::Terminus) do
def self.to_s
@@ -128,6 +128,18 @@ describe Puppet::Indirector::Terminus, " when managing terminus classes" do
end
end
+describe Puppet::Indirector::Terminus, " when converting class constants to indirection names" do
+ it "should convert camel case to lower case with underscores as word separators" do
+ subclass = mock 'subclass'
+ subclass.stubs(:to_s).returns("OneTwo")
+ subclass.stubs(:mark_as_abstract_terminus)
+
+ subclass.expects(:name=).with(:one_two)
+
+ Puppet::Indirector::Terminus.inherited(subclass)
+ end
+end
+
describe Puppet::Indirector::Terminus, " when creating terminus class types" do
before do
Puppet::Indirector::Terminus.stubs(:register_terminus_class)
@@ -139,7 +151,7 @@ describe Puppet::Indirector::Terminus, " when creating terminus class types" do
end
it "should set the name of the abstract subclass to be its class constant" do
- @subclass.name.should equal(:mytermtype)
+ @subclass.name.should equal(:my_term_type)
end
it "should mark abstract terminus types as such" do
@@ -156,7 +168,7 @@ describe Puppet::Indirector::Terminus, " when creating terminus classes" do
Puppet::Indirector::Terminus.stubs(:register_terminus_class)
@indirection = stub 'indirection', :name => :myind, :register_terminus_type => nil
- Puppet::Indirector::Indirection.expects(:instance).with(:myindirection).returns(@indirection)
+ Puppet::Indirector::Indirection.expects(:instance).with(:my_indirection).returns(@indirection)
@abstract_terminus = Class.new(Puppet::Indirector::Terminus) do
def self.to_s
@@ -179,7 +191,7 @@ describe Puppet::Indirector::Terminus, " when creating terminus classes" do
end
it "should set the subclass's name to the indirection name" do
- @terminus.name.should == :myindirection
+ @terminus.name.should == :my_indirection
end
it "should set the subclass's model to the indirection model" do
@@ -192,7 +204,7 @@ describe Puppet::Indirector::Terminus, " when a terminus instance" do
before do
Puppet::Indirector::Terminus.stubs(:register_terminus_class)
@indirection = stub 'indirection', :name => :myyaml, :register_terminus_type => nil
- Puppet::Indirector::Indirection.stubs(:instance).with(:mystuff).returns(@indirection)
+ Puppet::Indirector::Indirection.stubs(:instance).with(:my_stuff).returns(@indirection)
@abstract_terminus = Class.new(Puppet::Indirector::Terminus) do
def self.to_s
"Abstract"
diff --git a/spec/unit/indirector/yaml.rb b/spec/unit/indirector/yaml.rb
index 9e1d65e49..453b703a1 100755
--- a/spec/unit/indirector/yaml.rb
+++ b/spec/unit/indirector/yaml.rb
@@ -6,8 +6,8 @@ require 'puppet/indirector/yaml'
module YamlTesting
def setup
- @indirection = stub 'indirection', :name => :myyaml, :register_terminus_type => nil
- Puppet::Indirector::Indirection.stubs(:instance).with(:myyaml).returns(@indirection)
+ @indirection = stub 'indirection', :name => :my_yaml, :register_terminus_type => nil
+ Puppet::Indirector::Indirection.stubs(:instance).with(:my_yaml).returns(@indirection)
@store_class = Class.new(Puppet::Indirector::Yaml) do
def self.to_s
"MyYaml"
@@ -33,7 +33,7 @@ describe Puppet::Indirector::Yaml, " when choosing file location" do
end
it "should use the terminus name for choosing the subdirectory" do
- @store.send(:path, :me).should =~ %r{^#{@dir}/myyaml}
+ @store.send(:path, :me).should =~ %r{^#{@dir}/my_yaml}
end
it "should use the object's name to determine the file name" do
diff --git a/spec/unit/indirector/yaml/configuration.rb b/spec/unit/indirector/yaml/configuration.rb
new file mode 100755
index 000000000..3f0bc6f30
--- /dev/null
+++ b/spec/unit/indirector/yaml/configuration.rb
@@ -0,0 +1,25 @@
+#!/usr/bin/env ruby
+
+require File.dirname(__FILE__) + '/../../../spec_helper'
+
+require 'puppet/node/configuration'
+require 'puppet/indirector/yaml/configuration'
+
+describe Puppet::Indirector::Yaml::Configuration do
+ it "should be a subclass of the Yaml terminus" do
+ Puppet::Indirector::Yaml::Configuration.superclass.should equal(Puppet::Indirector::Yaml)
+ end
+
+ it "should have documentation" do
+ Puppet::Indirector::Yaml::Configuration.doc.should_not be_nil
+ end
+
+ it "should be registered with the configuration store indirection" do
+ indirection = Puppet::Indirector::Indirection.instance(:configuration)
+ Puppet::Indirector::Yaml::Configuration.indirection.should equal(indirection)
+ end
+
+ it "should have its name set to :configuration" do
+ Puppet::Indirector::Yaml::Configuration.name.should == :configuration
+ end
+end
diff --git a/spec/unit/indirector/yaml/node.rb b/spec/unit/indirector/yaml/node.rb
new file mode 100755
index 000000000..a14171b05
--- /dev/null
+++ b/spec/unit/indirector/yaml/node.rb
@@ -0,0 +1,25 @@
+#!/usr/bin/env ruby
+
+require File.dirname(__FILE__) + '/../../../spec_helper'
+
+require 'puppet/node'
+require 'puppet/indirector/yaml/node'
+
+describe Puppet::Indirector::Yaml::Node do
+ it "should be a subclass of the Yaml terminus" do
+ Puppet::Indirector::Yaml::Node.superclass.should equal(Puppet::Indirector::Yaml)
+ end
+
+ it "should have documentation" do
+ Puppet::Indirector::Yaml::Node.doc.should_not be_nil
+ end
+
+ it "should be registered with the configuration store indirection" do
+ indirection = Puppet::Indirector::Indirection.instance(:node)
+ Puppet::Indirector::Yaml::Node.indirection.should equal(indirection)
+ end
+
+ it "should have its name set to :node" do
+ Puppet::Indirector::Yaml::Node.name.should == :node
+ end
+end