diff options
author | Markus Roberts <Markus@reality.com> | 2010-07-09 18:12:17 -0700 |
---|---|---|
committer | Markus Roberts <Markus@reality.com> | 2010-07-09 18:12:17 -0700 |
commit | 3180b9d9b2c844dade1d361326600f7001ec66dd (patch) | |
tree | 98fe7c5ac7eb942aac9c39f019a17b0b3f5a57f4 /spec/unit/indirector | |
parent | 543225970225de5697734bfaf0a6eee996802c04 (diff) | |
download | puppet-3180b9d9b2c844dade1d361326600f7001ec66dd.tar.gz puppet-3180b9d9b2c844dade1d361326600f7001ec66dd.tar.xz puppet-3180b9d9b2c844dade1d361326600f7001ec66dd.zip |
Code smell: Two space indentation
Replaced 106806 occurances of ^( +)(.*$) with
The ruby community almost universally (i.e. everyone but Luke, Markus, and the other eleven people
who learned ruby in the 1900s) uses two-space indentation.
3 Examples:
The code:
end
# Tell getopt which arguments are valid
def test_get_getopt_args
element = Setting.new :name => "foo", :desc => "anything", :settings => Puppet::Util::Settings.new
assert_equal([["--foo", GetoptLong::REQUIRED_ARGUMENT]], element.getopt_args, "Did not produce appropriate getopt args")
becomes:
end
# Tell getopt which arguments are valid
def test_get_getopt_args
element = Setting.new :name => "foo", :desc => "anything", :settings => Puppet::Util::Settings.new
assert_equal([["--foo", GetoptLong::REQUIRED_ARGUMENT]], element.getopt_args, "Did not produce appropriate getopt args")
The code:
assert_equal(str, val)
assert_instance_of(Float, result)
end
# Now test it with a passed object
becomes:
assert_equal(str, val)
assert_instance_of(Float, result)
end
# Now test it with a passed object
The code:
end
assert_nothing_raised do
klass[:Yay] = "boo"
klass["Cool"] = :yayness
end
becomes:
end
assert_nothing_raised do
klass[:Yay] = "boo"
klass["Cool"] = :yayness
end
Diffstat (limited to 'spec/unit/indirector')
62 files changed, 4240 insertions, 4240 deletions
diff --git a/spec/unit/indirector/active_record_spec.rb b/spec/unit/indirector/active_record_spec.rb index f0bf07f07..258c4e793 100755 --- a/spec/unit/indirector/active_record_spec.rb +++ b/spec/unit/indirector/active_record_spec.rb @@ -5,72 +5,72 @@ require File.dirname(__FILE__) + '/../../spec_helper' require 'puppet/indirector/active_record' describe Puppet::Indirector::ActiveRecord do - before do - Puppet::Rails.stubs(:init) + before do + Puppet::Rails.stubs(:init) + + Puppet::Indirector::Terminus.stubs(:register_terminus_class) + @model = mock 'model' + @indirection = stub 'indirection', :name => :mystuff, :register_terminus_type => nil, :model => @model + Puppet::Indirector::Indirection.stubs(:instance).returns(@indirection) + + @active_record_class = Class.new(Puppet::Indirector::ActiveRecord) do + def self.to_s + "Mystuff::Testing" + end + end - Puppet::Indirector::Terminus.stubs(:register_terminus_class) - @model = mock 'model' - @indirection = stub 'indirection', :name => :mystuff, :register_terminus_type => nil, :model => @model - Puppet::Indirector::Indirection.stubs(:instance).returns(@indirection) + @ar_model = mock 'ar_model' - @active_record_class = Class.new(Puppet::Indirector::ActiveRecord) do - def self.to_s - "Mystuff::Testing" - end - end + @active_record_class.use_ar_model @ar_model + @terminus = @active_record_class.new - @ar_model = mock 'ar_model' + @name = "me" + @instance = stub 'instance', :name => @name - @active_record_class.use_ar_model @ar_model - @terminus = @active_record_class.new + @request = stub 'request', :key => @name, :instance => @instance + end - @name = "me" - @instance = stub 'instance', :name => @name + it "should allow declaration of an ActiveRecord model to use" do + @active_record_class.use_ar_model "foo" + @active_record_class.ar_model.should == "foo" + end - @request = stub 'request', :key => @name, :instance => @instance + describe "when initializing" do + it "should init Rails" do + Puppet::Rails.expects(:init) + @active_record_class.new end + end - it "should allow declaration of an ActiveRecord model to use" do - @active_record_class.use_ar_model "foo" - @active_record_class.ar_model.should == "foo" - end + describe "when finding an instance" do + it "should use the ActiveRecord model to find the instance" do + @ar_model.expects(:find_by_name).with(@name) - describe "when initializing" do - it "should init Rails" do - Puppet::Rails.expects(:init) - @active_record_class.new - end + @terminus.find(@request) end - describe "when finding an instance" do - it "should use the ActiveRecord model to find the instance" do - @ar_model.expects(:find_by_name).with(@name) - - @terminus.find(@request) - end - - it "should return nil if no instance is found" do - @ar_model.expects(:find_by_name).with(@name).returns nil - @terminus.find(@request).should be_nil - end + it "should return nil if no instance is found" do + @ar_model.expects(:find_by_name).with(@name).returns nil + @terminus.find(@request).should be_nil + end - it "should convert the instance to a Puppet object if it is found" do - instance = mock 'rails_instance' - instance.expects(:to_puppet).returns "mypuppet" + it "should convert the instance to a Puppet object if it is found" do + instance = mock 'rails_instance' + instance.expects(:to_puppet).returns "mypuppet" - @ar_model.expects(:find_by_name).with(@name).returns instance - @terminus.find(@request).should == "mypuppet" - end + @ar_model.expects(:find_by_name).with(@name).returns instance + @terminus.find(@request).should == "mypuppet" end + end - describe "when saving an instance" do - it "should use the ActiveRecord model to convert the instance into a Rails object and then save that rails object" do - rails_object = mock 'rails_object' - @ar_model.expects(:from_puppet).with(@instance).returns rails_object + describe "when saving an instance" do + it "should use the ActiveRecord model to convert the instance into a Rails object and then save that rails object" do + rails_object = mock 'rails_object' + @ar_model.expects(:from_puppet).with(@instance).returns rails_object - rails_object.expects(:save) + rails_object.expects(:save) - @terminus.save(@request) - end + @terminus.save(@request) end + end end diff --git a/spec/unit/indirector/catalog/active_record_spec.rb b/spec/unit/indirector/catalog/active_record_spec.rb index 8678dd6b3..4e9d049a1 100755 --- a/spec/unit/indirector/catalog/active_record_spec.rb +++ b/spec/unit/indirector/catalog/active_record_spec.rb @@ -4,138 +4,138 @@ require File.dirname(__FILE__) + '/../../../spec_helper' describe "Puppet::Resource::Catalog::ActiveRecord" do - confine "Missing Rails" => Puppet.features.rails? + confine "Missing Rails" => Puppet.features.rails? + before do + require 'puppet/indirector/catalog/active_record' + Puppet.features.stubs(:rails?).returns true + Puppet::Rails.stubs(:init) + @terminus = Puppet::Resource::Catalog::ActiveRecord.new + end + + it "should be a subclass of the ActiveRecord terminus class" do + Puppet::Resource::Catalog::ActiveRecord.ancestors.should be_include(Puppet::Indirector::ActiveRecord) + end + + it "should use Puppet::Rails::Host as its ActiveRecord model" do + Puppet::Resource::Catalog::ActiveRecord.ar_model.should equal(Puppet::Rails::Host) + end + + describe "when finding an instance" do before do - require 'puppet/indirector/catalog/active_record' - Puppet.features.stubs(:rails?).returns true - Puppet::Rails.stubs(:init) - @terminus = Puppet::Resource::Catalog::ActiveRecord.new + @request = stub 'request', :key => "foo", :options => {:cache_integration_hack => true} end - it "should be a subclass of the ActiveRecord terminus class" do - Puppet::Resource::Catalog::ActiveRecord.ancestors.should be_include(Puppet::Indirector::ActiveRecord) + # This hack is here because we don't want to look in the db unless we actually want + # to look in the db, but our indirection architecture in 0.24.x isn't flexible + # enough to tune that via configuration. + it "should return nil unless ':cache_integration_hack' is set to true" do + @request.options[:cache_integration_hack] = false + Puppet::Rails::Host.expects(:find_by_name).never + @terminus.find(@request).should be_nil end - it "should use Puppet::Rails::Host as its ActiveRecord model" do - Puppet::Resource::Catalog::ActiveRecord.ar_model.should equal(Puppet::Rails::Host) + it "should use the Hosts ActiveRecord class to find the host" do + Puppet::Rails::Host.expects(:find_by_name).with { |key, args| key == "foo" } + @terminus.find(@request) end - describe "when finding an instance" do - before do - @request = stub 'request', :key => "foo", :options => {:cache_integration_hack => true} - end - - # This hack is here because we don't want to look in the db unless we actually want - # to look in the db, but our indirection architecture in 0.24.x isn't flexible - # enough to tune that via configuration. - it "should return nil unless ':cache_integration_hack' is set to true" do - @request.options[:cache_integration_hack] = false - Puppet::Rails::Host.expects(:find_by_name).never - @terminus.find(@request).should be_nil - end + it "should return nil if no host instance can be found" do + Puppet::Rails::Host.expects(:find_by_name).returns nil - it "should use the Hosts ActiveRecord class to find the host" do - Puppet::Rails::Host.expects(:find_by_name).with { |key, args| key == "foo" } - @terminus.find(@request) - end - - it "should return nil if no host instance can be found" do - Puppet::Rails::Host.expects(:find_by_name).returns nil - - @terminus.find(@request).should be_nil - end + @terminus.find(@request).should be_nil + end - it "should return a catalog with the same name as the host if the host can be found" do - host = stub 'host', :name => "foo", :resources => [] - Puppet::Rails::Host.expects(:find_by_name).returns host + it "should return a catalog with the same name as the host if the host can be found" do + host = stub 'host', :name => "foo", :resources => [] + Puppet::Rails::Host.expects(:find_by_name).returns host - result = @terminus.find(@request) - result.should be_instance_of(Puppet::Resource::Catalog) - result.name.should == "foo" - end + result = @terminus.find(@request) + result.should be_instance_of(Puppet::Resource::Catalog) + result.name.should == "foo" + end - it "should set each of the host's resources as a transportable resource within the catalog" do - host = stub 'host', :name => "foo" - Puppet::Rails::Host.expects(:find_by_name).returns host + it "should set each of the host's resources as a transportable resource within the catalog" do + host = stub 'host', :name => "foo" + Puppet::Rails::Host.expects(:find_by_name).returns host - res1 = mock 'res1', :to_transportable => "trans_res1" - res2 = mock 'res2', :to_transportable => "trans_res2" + res1 = mock 'res1', :to_transportable => "trans_res1" + res2 = mock 'res2', :to_transportable => "trans_res2" - host.expects(:resources).returns [res1, res2] + host.expects(:resources).returns [res1, res2] - catalog = stub 'catalog' - Puppet::Resource::Catalog.expects(:new).returns catalog + catalog = stub 'catalog' + Puppet::Resource::Catalog.expects(:new).returns catalog - catalog.expects(:add_resource).with "trans_res1" - catalog.expects(:add_resource).with "trans_res2" + catalog.expects(:add_resource).with "trans_res1" + catalog.expects(:add_resource).with "trans_res2" - @terminus.find(@request) - end + @terminus.find(@request) end + end - describe "when saving an instance" do - before do - @host = stub 'host', :name => "foo", :save => nil, :merge_resources => nil, :last_compile= => nil, :ip= => nil, :environment= => nil - @host.stubs(:railsmark).yields + describe "when saving an instance" do + before do + @host = stub 'host', :name => "foo", :save => nil, :merge_resources => nil, :last_compile= => nil, :ip= => nil, :environment= => nil + @host.stubs(:railsmark).yields - @node = stub_everything 'node', :parameters => {} - Puppet::Node.stubs(:find).returns(@node) + @node = stub_everything 'node', :parameters => {} + Puppet::Node.stubs(:find).returns(@node) - Puppet::Rails::Host.stubs(:find_by_name).returns @host - @catalog = Puppet::Resource::Catalog.new("foo") - @request = stub 'request', :key => "foo", :instance => @catalog - end + Puppet::Rails::Host.stubs(:find_by_name).returns @host + @catalog = Puppet::Resource::Catalog.new("foo") + @request = stub 'request', :key => "foo", :instance => @catalog + end - it "should find the Rails host with the same name" do - Puppet::Rails::Host.expects(:find_by_name).with("foo").returns @host + it "should find the Rails host with the same name" do + Puppet::Rails::Host.expects(:find_by_name).with("foo").returns @host - @terminus.save(@request) - end + @terminus.save(@request) + end - it "should create a new Rails host if none can be found" do - Puppet::Rails::Host.expects(:find_by_name).with("foo").returns nil + it "should create a new Rails host if none can be found" do + Puppet::Rails::Host.expects(:find_by_name).with("foo").returns nil - Puppet::Rails::Host.expects(:create).with(:name => "foo").returns @host + Puppet::Rails::Host.expects(:create).with(:name => "foo").returns @host - @terminus.save(@request) - end + @terminus.save(@request) + end - it "should set the catalog vertices as resources on the Rails host instance" do - @catalog.expects(:vertices).returns "foo" - @host.expects(:merge_resources).with("foo") + it "should set the catalog vertices as resources on the Rails host instance" do + @catalog.expects(:vertices).returns "foo" + @host.expects(:merge_resources).with("foo") - @terminus.save(@request) - end + @terminus.save(@request) + end - it "should set host ip if we could find a matching node" do - @node.stubs(:parameters).returns({"ipaddress" => "192.168.0.1"}) + it "should set host ip if we could find a matching node" do + @node.stubs(:parameters).returns({"ipaddress" => "192.168.0.1"}) - @host.expects(:ip=).with '192.168.0.1' + @host.expects(:ip=).with '192.168.0.1' - @terminus.save(@request) - end + @terminus.save(@request) + end - it "should set host environment if we could find a matching node" do - @node.stubs(:environment).returns("myenv") + it "should set host environment if we could find a matching node" do + @node.stubs(:environment).returns("myenv") - @host.expects(:environment=).with 'myenv' + @host.expects(:environment=).with 'myenv' - @terminus.save(@request) - end + @terminus.save(@request) + end - it "should set the last compile time on the host" do - now = Time.now - Time.expects(:now).returns now - @host.expects(:last_compile=).with now + it "should set the last compile time on the host" do + now = Time.now + Time.expects(:now).returns now + @host.expects(:last_compile=).with now - @terminus.save(@request) - end + @terminus.save(@request) + end - it "should save the Rails host instance" do - @host.expects(:save) + it "should save the Rails host instance" do + @host.expects(:save) - @terminus.save(@request) - end + @terminus.save(@request) end + end end diff --git a/spec/unit/indirector/catalog/compiler_spec.rb b/spec/unit/indirector/catalog/compiler_spec.rb index 8339d1861..755509f53 100755 --- a/spec/unit/indirector/catalog/compiler_spec.rb +++ b/spec/unit/indirector/catalog/compiler_spec.rb @@ -8,258 +8,258 @@ require File.dirname(__FILE__) + '/../../../spec_helper' require 'puppet/indirector/catalog/compiler' describe Puppet::Resource::Catalog::Compiler do + before do + Puppet::Rails.stubs(:init) + Facter.stubs(:to_hash).returns({}) + Facter.stubs(:value).returns(Facter::Util::Fact.new("something")) + end + + describe "when initializing" do before do - Puppet::Rails.stubs(:init) - Facter.stubs(:to_hash).returns({}) - Facter.stubs(:value).returns(Facter::Util::Fact.new("something")) + Puppet.expects(:version).returns(1) + Facter.expects(:value).with('fqdn').returns("my.server.com") + Facter.expects(:value).with('ipaddress').returns("my.ip.address") + end + + it "should gather data about itself" do + Puppet::Resource::Catalog::Compiler.new + end + + it "should cache the server metadata and reuse it" do + compiler = Puppet::Resource::Catalog::Compiler.new + node1 = stub 'node1', :merge => nil + node2 = stub 'node2', :merge => nil + compiler.stubs(:compile) + Puppet::Node.stubs(:find).with('node1').returns(node1) + Puppet::Node.stubs(:find).with('node2').returns(node2) + + compiler.find(stub('request', :node => 'node1', :options => {})) + compiler.find(stub('node2request', :node => 'node2', :options => {})) end - describe "when initializing" do - before do - Puppet.expects(:version).returns(1) - Facter.expects(:value).with('fqdn').returns("my.server.com") - Facter.expects(:value).with('ipaddress').returns("my.ip.address") - end - - it "should gather data about itself" do - Puppet::Resource::Catalog::Compiler.new - end - - it "should cache the server metadata and reuse it" do - compiler = Puppet::Resource::Catalog::Compiler.new - node1 = stub 'node1', :merge => nil - node2 = stub 'node2', :merge => nil - compiler.stubs(:compile) - Puppet::Node.stubs(:find).with('node1').returns(node1) - Puppet::Node.stubs(:find).with('node2').returns(node2) - - compiler.find(stub('request', :node => 'node1', :options => {})) - compiler.find(stub('node2request', :node => 'node2', :options => {})) - end - - it "should provide a method for determining if the catalog is networked" do - compiler = Puppet::Resource::Catalog::Compiler.new - compiler.should respond_to(:networked?) - end - - describe "and storeconfigs is enabled" do - before do - Puppet.settings.expects(:value).with(:storeconfigs).returns true - end - - it "should initialize Rails if it is available" do - Puppet.features.expects(:rails?).returns true - Puppet::Rails.expects(:init) - Puppet::Resource::Catalog::Compiler.new - end - - it "should fail if Rails is unavailable" do - Puppet.features.expects(:rails?).returns false - Puppet::Rails.expects(:init).never - lambda { Puppet::Resource::Catalog::Compiler.new }.should raise_error(Puppet::Error) - end - end + it "should provide a method for determining if the catalog is networked" do + compiler = Puppet::Resource::Catalog::Compiler.new + compiler.should respond_to(:networked?) end - describe "when finding catalogs" do - before do - Facter.stubs(:value).returns("whatever") - - @compiler = Puppet::Resource::Catalog::Compiler.new - @name = "me" - @node = Puppet::Node.new @name - @node.stubs(:merge) - Puppet::Node.stubs(:find).returns @node - @request = stub 'request', :key => "does not matter", :node => @name, :options => {} - end - - it "should directly use provided nodes" do - Puppet::Node.expects(:find).never - @compiler.expects(:compile).with(@node) - @request.stubs(:options).returns(:use_node => @node) - @compiler.find(@request) - end - - it "should use the request's node name if no explicit node is provided" do - Puppet::Node.expects(:find).with(@name).returns(@node) - @compiler.expects(:compile).with(@node) - @compiler.find(@request) - end - - it "should use the provided node name if no explicit node is provided and no authenticated node information is available" do - @request.expects(:node).returns nil - @request.expects(:key).returns "my_node" - - Puppet::Node.expects(:find).with("my_node").returns @node - @compiler.expects(:compile).with(@node) - @compiler.find(@request) - end - - it "should fail if no node is passed and none can be found" do - Puppet::Node.stubs(:find).with(@name).returns(nil) - proc { @compiler.find(@request) }.should raise_error(ArgumentError) - end - - it "should fail intelligently when searching for a node raises an exception" do - Puppet::Node.stubs(:find).with(@name).raises "eh" - proc { @compiler.find(@request) }.should raise_error(Puppet::Error) - end - - it "should pass the found node to the compiler for compiling" do - Puppet::Node.expects(:find).with(@name).returns(@node) - config = mock 'config' - Puppet::Parser::Compiler.expects(:compile).with(@node) - @compiler.find(@request) - end - - it "should extract and save any facts from the request" do - Puppet::Node.expects(:find).with(@name).returns @node - @compiler.expects(:extract_facts_from_request).with(@request) - Puppet::Parser::Compiler.stubs(:compile) - @compiler.find(@request) - end - - it "should return the results of compiling as the catalog" do - Puppet::Node.stubs(:find).returns(@node) - config = mock 'config' - result = mock 'result' - - Puppet::Parser::Compiler.expects(:compile).returns result - @compiler.find(@request).should equal(result) - end - - it "should benchmark the compile process" do - Puppet::Node.stubs(:find).returns(@node) - @compiler.stubs(:networked?).returns(true) - @compiler.expects(:benchmark).with do |level, message| - level == :notice and message =~ /^Compiled catalog/ - end - Puppet::Parser::Compiler.stubs(:compile) - @compiler.find(@request) - end + describe "and storeconfigs is enabled" do + before do + Puppet.settings.expects(:value).with(:storeconfigs).returns true + end + + it "should initialize Rails if it is available" do + Puppet.features.expects(:rails?).returns true + Puppet::Rails.expects(:init) + Puppet::Resource::Catalog::Compiler.new + end + + it "should fail if Rails is unavailable" do + Puppet.features.expects(:rails?).returns false + Puppet::Rails.expects(:init).never + lambda { Puppet::Resource::Catalog::Compiler.new }.should raise_error(Puppet::Error) + end end + end - describe "when extracting facts from the request" do - before do - Facter.stubs(:value).returns "something" - @compiler = Puppet::Resource::Catalog::Compiler.new - @request = stub 'request', :options => {} + describe "when finding catalogs" do + before do + Facter.stubs(:value).returns("whatever") + + @compiler = Puppet::Resource::Catalog::Compiler.new + @name = "me" + @node = Puppet::Node.new @name + @node.stubs(:merge) + Puppet::Node.stubs(:find).returns @node + @request = stub 'request', :key => "does not matter", :node => @name, :options => {} + end - @facts = stub 'facts', :save => nil - end + it "should directly use provided nodes" do + Puppet::Node.expects(:find).never + @compiler.expects(:compile).with(@node) + @request.stubs(:options).returns(:use_node => @node) + @compiler.find(@request) + end - it "should do nothing if no facts are provided" do - Puppet::Node::Facts.expects(:convert_from).never - @request.options[:facts] = nil + it "should use the request's node name if no explicit node is provided" do + Puppet::Node.expects(:find).with(@name).returns(@node) + @compiler.expects(:compile).with(@node) + @compiler.find(@request) + end - @compiler.extract_facts_from_request(@request) - end + it "should use the provided node name if no explicit node is provided and no authenticated node information is available" do + @request.expects(:node).returns nil + @request.expects(:key).returns "my_node" - it "should use the Facts class to deserialize the provided facts" do - @request.options[:facts_format] = "foo" - @request.options[:facts] = "bar" - Puppet::Node::Facts.expects(:convert_from).returns @facts + Puppet::Node.expects(:find).with("my_node").returns @node + @compiler.expects(:compile).with(@node) + @compiler.find(@request) + end - @compiler.extract_facts_from_request(@request) - end + it "should fail if no node is passed and none can be found" do + Puppet::Node.stubs(:find).with(@name).returns(nil) + proc { @compiler.find(@request) }.should raise_error(ArgumentError) + end - it "should use the provided fact format" do - @request.options[:facts_format] = "foo" - @request.options[:facts] = "bar" - Puppet::Node::Facts.expects(:convert_from).with { |format, text| format == "foo" }.returns @facts + it "should fail intelligently when searching for a node raises an exception" do + Puppet::Node.stubs(:find).with(@name).raises "eh" + proc { @compiler.find(@request) }.should raise_error(Puppet::Error) + end - @compiler.extract_facts_from_request(@request) - end + it "should pass the found node to the compiler for compiling" do + Puppet::Node.expects(:find).with(@name).returns(@node) + config = mock 'config' + Puppet::Parser::Compiler.expects(:compile).with(@node) + @compiler.find(@request) + end - it "should convert the facts into a fact instance and save it" do - @request.options[:facts_format] = "foo" - @request.options[:facts] = "bar" - Puppet::Node::Facts.expects(:convert_from).returns @facts + it "should extract and save any facts from the request" do + Puppet::Node.expects(:find).with(@name).returns @node + @compiler.expects(:extract_facts_from_request).with(@request) + Puppet::Parser::Compiler.stubs(:compile) + @compiler.find(@request) + end - @facts.expects(:save) + it "should return the results of compiling as the catalog" do + Puppet::Node.stubs(:find).returns(@node) + config = mock 'config' + result = mock 'result' - @compiler.extract_facts_from_request(@request) - end + Puppet::Parser::Compiler.expects(:compile).returns result + @compiler.find(@request).should equal(result) end - describe "when finding nodes" do - before do - Facter.stubs(:value).returns("whatever") - @compiler = Puppet::Resource::Catalog::Compiler.new - @name = "me" - @node = mock 'node' - @request = stub 'request', :node => @name, :options => {} - @compiler.stubs(:compile) - end - - it "should look node information up via the Node class with the provided key" do - @node.stubs :merge - Puppet::Node.expects(:find).with(@name).returns(@node) - @compiler.find(@request) - end + it "should benchmark the compile process" do + Puppet::Node.stubs(:find).returns(@node) + @compiler.stubs(:networked?).returns(true) + @compiler.expects(:benchmark).with do |level, message| + level == :notice and message =~ /^Compiled catalog/ + end + Puppet::Parser::Compiler.stubs(:compile) + @compiler.find(@request) end + end - describe "after finding nodes" do - before do - Puppet.expects(:version).returns(1) - Facter.expects(:value).with('fqdn').returns("my.server.com") - Facter.expects(:value).with('ipaddress').returns("my.ip.address") - @compiler = Puppet::Resource::Catalog::Compiler.new - @name = "me" - @node = mock 'node' - @request = stub 'request', :node => @name, :options => {} - @compiler.stubs(:compile) - Puppet::Node.stubs(:find).with(@name).returns(@node) - end - - it "should add the server's Puppet version to the node's parameters as 'serverversion'" do - @node.expects(:merge).with { |args| args["serverversion"] == "1" } - @compiler.find(@request) - end - - it "should add the server's fqdn to the node's parameters as 'servername'" do - @node.expects(:merge).with { |args| args["servername"] == "my.server.com" } - @compiler.find(@request) - end - - it "should add the server's IP address to the node's parameters as 'serverip'" do - @node.expects(:merge).with { |args| args["serverip"] == "my.ip.address" } - @compiler.find(@request) - end + describe "when extracting facts from the request" do + before do + Facter.stubs(:value).returns "something" + @compiler = Puppet::Resource::Catalog::Compiler.new + @request = stub 'request', :options => {} + + @facts = stub 'facts', :save => nil end - describe "when filtering resources" do - before :each do - Facter.stubs(:value) - @compiler = Puppet::Resource::Catalog::Compiler.new - @catalog = stub_everything 'catalog' - @catalog.stubs(:respond_to?).with(:filter).returns(true) - end + it "should do nothing if no facts are provided" do + Puppet::Node::Facts.expects(:convert_from).never + @request.options[:facts] = nil + + @compiler.extract_facts_from_request(@request) + end - it "should delegate to the catalog instance filtering" do - @catalog.expects(:filter) - @compiler.filter(@catalog) - end + it "should use the Facts class to deserialize the provided facts" do + @request.options[:facts_format] = "foo" + @request.options[:facts] = "bar" + Puppet::Node::Facts.expects(:convert_from).returns @facts - it "should filter out virtual resources" do - resource = mock 'resource', :virtual? => true - @catalog.stubs(:filter).yields(resource) + @compiler.extract_facts_from_request(@request) + end - @compiler.filter(@catalog) - end + it "should use the provided fact format" do + @request.options[:facts_format] = "foo" + @request.options[:facts] = "bar" + Puppet::Node::Facts.expects(:convert_from).with { |format, text| format == "foo" }.returns @facts - it "should return the same catalog if it doesn't support filtering" do - @catalog.stubs(:respond_to?).with(:filter).returns(false) + @compiler.extract_facts_from_request(@request) + end - @compiler.filter(@catalog).should == @catalog - end + it "should convert the facts into a fact instance and save it" do + @request.options[:facts_format] = "foo" + @request.options[:facts] = "bar" + Puppet::Node::Facts.expects(:convert_from).returns @facts - it "should return the filtered catalog" do - catalog = stub 'filtered catalog' - @catalog.stubs(:filter).returns(catalog) + @facts.expects(:save) - @compiler.filter(@catalog).should == catalog - end + @compiler.extract_facts_from_request(@request) + end + end + describe "when finding nodes" do + before do + Facter.stubs(:value).returns("whatever") + @compiler = Puppet::Resource::Catalog::Compiler.new + @name = "me" + @node = mock 'node' + @request = stub 'request', :node => @name, :options => {} + @compiler.stubs(:compile) end + + it "should look node information up via the Node class with the provided key" do + @node.stubs :merge + Puppet::Node.expects(:find).with(@name).returns(@node) + @compiler.find(@request) + end + end + + describe "after finding nodes" do + before do + Puppet.expects(:version).returns(1) + Facter.expects(:value).with('fqdn').returns("my.server.com") + Facter.expects(:value).with('ipaddress').returns("my.ip.address") + @compiler = Puppet::Resource::Catalog::Compiler.new + @name = "me" + @node = mock 'node' + @request = stub 'request', :node => @name, :options => {} + @compiler.stubs(:compile) + Puppet::Node.stubs(:find).with(@name).returns(@node) + end + + it "should add the server's Puppet version to the node's parameters as 'serverversion'" do + @node.expects(:merge).with { |args| args["serverversion"] == "1" } + @compiler.find(@request) + end + + it "should add the server's fqdn to the node's parameters as 'servername'" do + @node.expects(:merge).with { |args| args["servername"] == "my.server.com" } + @compiler.find(@request) + end + + it "should add the server's IP address to the node's parameters as 'serverip'" do + @node.expects(:merge).with { |args| args["serverip"] == "my.ip.address" } + @compiler.find(@request) + end + end + + describe "when filtering resources" do + before :each do + Facter.stubs(:value) + @compiler = Puppet::Resource::Catalog::Compiler.new + @catalog = stub_everything 'catalog' + @catalog.stubs(:respond_to?).with(:filter).returns(true) + end + + it "should delegate to the catalog instance filtering" do + @catalog.expects(:filter) + @compiler.filter(@catalog) + end + + it "should filter out virtual resources" do + resource = mock 'resource', :virtual? => true + @catalog.stubs(:filter).yields(resource) + + @compiler.filter(@catalog) + end + + it "should return the same catalog if it doesn't support filtering" do + @catalog.stubs(:respond_to?).with(:filter).returns(false) + + @compiler.filter(@catalog).should == @catalog + end + + it "should return the filtered catalog" do + catalog = stub 'filtered catalog' + @catalog.stubs(:filter).returns(catalog) + + @compiler.filter(@catalog).should == catalog + end + + end end diff --git a/spec/unit/indirector/catalog/queue_spec.rb b/spec/unit/indirector/catalog/queue_spec.rb index 66a30c0d6..38d40c94a 100755 --- a/spec/unit/indirector/catalog/queue_spec.rb +++ b/spec/unit/indirector/catalog/queue_spec.rb @@ -5,16 +5,16 @@ require File.dirname(__FILE__) + '/../../../spec_helper' require 'puppet/indirector/catalog/queue' describe Puppet::Resource::Catalog::Queue do - it 'should be a subclass of the Queue terminus' do - Puppet::Resource::Catalog::Queue.superclass.should equal(Puppet::Indirector::Queue) - end + it 'should be a subclass of the Queue terminus' do + Puppet::Resource::Catalog::Queue.superclass.should equal(Puppet::Indirector::Queue) + end - it 'should be registered with the catalog store indirection' do - indirection = Puppet::Indirector::Indirection.instance(:catalog) - Puppet::Resource::Catalog::Queue.indirection.should equal(indirection) - end + it 'should be registered with the catalog store indirection' do + indirection = Puppet::Indirector::Indirection.instance(:catalog) + Puppet::Resource::Catalog::Queue.indirection.should equal(indirection) + end - it 'shall be dubbed ":queue"' do - Puppet::Resource::Catalog::Queue.name.should == :queue - end + it 'shall be dubbed ":queue"' do + Puppet::Resource::Catalog::Queue.name.should == :queue + end end diff --git a/spec/unit/indirector/catalog/rest_spec.rb b/spec/unit/indirector/catalog/rest_spec.rb index 909e0a89e..d61054e16 100755 --- a/spec/unit/indirector/catalog/rest_spec.rb +++ b/spec/unit/indirector/catalog/rest_spec.rb @@ -5,7 +5,7 @@ require File.dirname(__FILE__) + '/../../../spec_helper' require 'puppet/indirector/catalog/rest' describe Puppet::Resource::Catalog::Rest do - it "should be a sublcass of Puppet::Indirector::REST" do - Puppet::Resource::Catalog::Rest.superclass.should equal(Puppet::Indirector::REST) - end + it "should be a sublcass of Puppet::Indirector::REST" do + Puppet::Resource::Catalog::Rest.superclass.should equal(Puppet::Indirector::REST) + end end diff --git a/spec/unit/indirector/catalog/yaml_spec.rb b/spec/unit/indirector/catalog/yaml_spec.rb index 5f233c91c..5a30b8268 100755 --- a/spec/unit/indirector/catalog/yaml_spec.rb +++ b/spec/unit/indirector/catalog/yaml_spec.rb @@ -6,20 +6,20 @@ require 'puppet/resource/catalog' require 'puppet/indirector/catalog/yaml' describe Puppet::Resource::Catalog::Yaml do - it "should be a subclass of the Yaml terminus" do - Puppet::Resource::Catalog::Yaml.superclass.should equal(Puppet::Indirector::Yaml) - end + it "should be a subclass of the Yaml terminus" do + Puppet::Resource::Catalog::Yaml.superclass.should equal(Puppet::Indirector::Yaml) + end - it "should have documentation" do - Puppet::Resource::Catalog::Yaml.doc.should_not be_nil - end + it "should have documentation" do + Puppet::Resource::Catalog::Yaml.doc.should_not be_nil + end - it "should be registered with the catalog store indirection" do - indirection = Puppet::Indirector::Indirection.instance(:catalog) - Puppet::Resource::Catalog::Yaml.indirection.should equal(indirection) - end + it "should be registered with the catalog store indirection" do + indirection = Puppet::Indirector::Indirection.instance(:catalog) + Puppet::Resource::Catalog::Yaml.indirection.should equal(indirection) + end - it "should have its name set to :yaml" do - Puppet::Resource::Catalog::Yaml.name.should == :yaml - end + it "should have its name set to :yaml" do + Puppet::Resource::Catalog::Yaml.name.should == :yaml + end end diff --git a/spec/unit/indirector/certificate/ca_spec.rb b/spec/unit/indirector/certificate/ca_spec.rb index 0158a8d90..6bf3e26ee 100755 --- a/spec/unit/indirector/certificate/ca_spec.rb +++ b/spec/unit/indirector/certificate/ca_spec.rb @@ -8,21 +8,21 @@ require File.dirname(__FILE__) + '/../../../spec_helper' require 'puppet/indirector/certificate/ca' describe Puppet::SSL::Certificate::Ca do - it "should have documentation" do - Puppet::SSL::Certificate::Ca.doc.should be_instance_of(String) - end + it "should have documentation" do + Puppet::SSL::Certificate::Ca.doc.should be_instance_of(String) + end - it "should use the :signeddir as the collection directory" do - Puppet.settings.expects(:value).with(:signeddir).returns "/cert/dir" - Puppet::SSL::Certificate::Ca.collection_directory.should == "/cert/dir" - end + it "should use the :signeddir as the collection directory" do + Puppet.settings.expects(:value).with(:signeddir).returns "/cert/dir" + Puppet::SSL::Certificate::Ca.collection_directory.should == "/cert/dir" + end - it "should store the ca certificate at the :cacert location" do - Puppet.settings.stubs(:use) - Puppet.settings.stubs(:value).returns "whatever" - Puppet.settings.stubs(:value).with(:cacert).returns "/ca/cert" - file = Puppet::SSL::Certificate::Ca.new - file.stubs(:ca?).returns true - file.path("whatever").should == "/ca/cert" - end + it "should store the ca certificate at the :cacert location" do + Puppet.settings.stubs(:use) + Puppet.settings.stubs(:value).returns "whatever" + Puppet.settings.stubs(:value).with(:cacert).returns "/ca/cert" + file = Puppet::SSL::Certificate::Ca.new + file.stubs(:ca?).returns true + file.path("whatever").should == "/ca/cert" + end end diff --git a/spec/unit/indirector/certificate/file_spec.rb b/spec/unit/indirector/certificate/file_spec.rb index 6fb00f1ca..6a8e7d8f4 100755 --- a/spec/unit/indirector/certificate/file_spec.rb +++ b/spec/unit/indirector/certificate/file_spec.rb @@ -8,21 +8,21 @@ require File.dirname(__FILE__) + '/../../../spec_helper' require 'puppet/indirector/certificate/file' describe Puppet::SSL::Certificate::File do - it "should have documentation" do - Puppet::SSL::Certificate::File.doc.should be_instance_of(String) - end + it "should have documentation" do + Puppet::SSL::Certificate::File.doc.should be_instance_of(String) + end - it "should use the :certdir as the collection directory" do - Puppet.settings.expects(:value).with(:certdir).returns "/cert/dir" - Puppet::SSL::Certificate::File.collection_directory.should == "/cert/dir" - end + it "should use the :certdir as the collection directory" do + Puppet.settings.expects(:value).with(:certdir).returns "/cert/dir" + Puppet::SSL::Certificate::File.collection_directory.should == "/cert/dir" + end - it "should store the ca certificate at the :localcacert location" do - Puppet.settings.stubs(:use) - Puppet.settings.stubs(:value).returns "whatever" - Puppet.settings.stubs(:value).with(:localcacert).returns "/ca/cert" - file = Puppet::SSL::Certificate::File.new - file.stubs(:ca?).returns true - file.path("whatever").should == "/ca/cert" - end + it "should store the ca certificate at the :localcacert location" do + Puppet.settings.stubs(:use) + Puppet.settings.stubs(:value).returns "whatever" + Puppet.settings.stubs(:value).with(:localcacert).returns "/ca/cert" + file = Puppet::SSL::Certificate::File.new + file.stubs(:ca?).returns true + file.path("whatever").should == "/ca/cert" + end end diff --git a/spec/unit/indirector/certificate/rest_spec.rb b/spec/unit/indirector/certificate/rest_spec.rb index 9f272fbb2..77b2a67bd 100755 --- a/spec/unit/indirector/certificate/rest_spec.rb +++ b/spec/unit/indirector/certificate/rest_spec.rb @@ -5,27 +5,27 @@ require File.dirname(__FILE__) + '/../../../spec_helper' require 'puppet/indirector/certificate/rest' describe Puppet::SSL::Certificate::Rest do - before do - @searcher = Puppet::SSL::Certificate::Rest.new - end + before do + @searcher = Puppet::SSL::Certificate::Rest.new + end - it "should be a sublcass of Puppet::Indirector::REST" do - Puppet::SSL::Certificate::Rest.superclass.should equal(Puppet::Indirector::REST) - end + it "should be a sublcass of Puppet::Indirector::REST" do + Puppet::SSL::Certificate::Rest.superclass.should equal(Puppet::Indirector::REST) + end - it "should set server_setting to :ca_server" do - Puppet::SSL::Certificate::Rest.server_setting.should == :ca_server - end + it "should set server_setting to :ca_server" do + Puppet::SSL::Certificate::Rest.server_setting.should == :ca_server + end - it "should set port_setting to :ca_port" do - Puppet::SSL::Certificate::Rest.port_setting.should == :ca_port - end + it "should set port_setting to :ca_port" do + Puppet::SSL::Certificate::Rest.port_setting.should == :ca_port + end - it "should make sure found certificates have their names set to the search string" do - terminus = Puppet::SSL::Certificate::Rest.new + it "should make sure found certificates have their names set to the search string" do + terminus = Puppet::SSL::Certificate::Rest.new - # This has 'boo.com' in the CN - cert_string = "-----BEGIN CERTIFICATE----- + # This has 'boo.com' in the CN + cert_string = "-----BEGIN CERTIFICATE----- MIICPzCCAaigAwIBAgIBBDANBgkqhkiG9w0BAQUFADAWMRQwEgYDVQQDDAtidWNr eS5sb2NhbDAeFw0wOTA5MTcxNzI1MzJaFw0xNDA5MTYxNzI1MzJaMBIxEDAOBgNV BAMMB2Jvby5jb20wgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAKG9B+DkTCNh @@ -42,17 +42,17 @@ rn/G -----END CERTIFICATE----- " - network = stub 'network' - terminus.stubs(:network).returns network + network = stub 'network' + terminus.stubs(:network).returns network - response = stub 'response', :code => "200", :body => cert_string - response.stubs(:[]).with('content-type').returns "text/plain" - response.stubs(:[]).with('content-encoding') - network.expects(:get).returns response + response = stub 'response', :code => "200", :body => cert_string + response.stubs(:[]).with('content-type').returns "text/plain" + response.stubs(:[]).with('content-encoding') + network.expects(:get).returns response - request = Puppet::Indirector::Request.new(:certificate, :find, "foo.com") - result = terminus.find(request) - result.should_not be_nil - result.name.should == "foo.com" - end + request = Puppet::Indirector::Request.new(:certificate, :find, "foo.com") + result = terminus.find(request) + result.should_not be_nil + result.name.should == "foo.com" + end end diff --git a/spec/unit/indirector/certificate_request/ca_spec.rb b/spec/unit/indirector/certificate_request/ca_spec.rb index b35d8b474..1bcbc46df 100755 --- a/spec/unit/indirector/certificate_request/ca_spec.rb +++ b/spec/unit/indirector/certificate_request/ca_spec.rb @@ -8,12 +8,12 @@ require File.dirname(__FILE__) + '/../../../spec_helper' require 'puppet/indirector/certificate_request/ca' describe Puppet::SSL::CertificateRequest::Ca do - it "should have documentation" do - Puppet::SSL::CertificateRequest::Ca.doc.should be_instance_of(String) - end + it "should have documentation" do + Puppet::SSL::CertificateRequest::Ca.doc.should be_instance_of(String) + end - it "should use the :csrdir as the collection directory" do - Puppet.settings.expects(:value).with(:csrdir).returns "/request/dir" - Puppet::SSL::CertificateRequest::Ca.collection_directory.should == "/request/dir" - end + it "should use the :csrdir as the collection directory" do + Puppet.settings.expects(:value).with(:csrdir).returns "/request/dir" + Puppet::SSL::CertificateRequest::Ca.collection_directory.should == "/request/dir" + end end diff --git a/spec/unit/indirector/certificate_request/file_spec.rb b/spec/unit/indirector/certificate_request/file_spec.rb index e1f442e2a..f18ba2ca8 100755 --- a/spec/unit/indirector/certificate_request/file_spec.rb +++ b/spec/unit/indirector/certificate_request/file_spec.rb @@ -8,12 +8,12 @@ require File.dirname(__FILE__) + '/../../../spec_helper' require 'puppet/indirector/certificate_request/file' describe Puppet::SSL::CertificateRequest::File do - it "should have documentation" do - Puppet::SSL::CertificateRequest::File.doc.should be_instance_of(String) - end + it "should have documentation" do + Puppet::SSL::CertificateRequest::File.doc.should be_instance_of(String) + end - it "should use the :requestdir as the collection directory" do - Puppet.settings.expects(:value).with(:requestdir).returns "/request/dir" - Puppet::SSL::CertificateRequest::File.collection_directory.should == "/request/dir" - end + it "should use the :requestdir as the collection directory" do + Puppet.settings.expects(:value).with(:requestdir).returns "/request/dir" + Puppet::SSL::CertificateRequest::File.collection_directory.should == "/request/dir" + end end diff --git a/spec/unit/indirector/certificate_request/rest_spec.rb b/spec/unit/indirector/certificate_request/rest_spec.rb index a1492acab..8b90b46a9 100755 --- a/spec/unit/indirector/certificate_request/rest_spec.rb +++ b/spec/unit/indirector/certificate_request/rest_spec.rb @@ -5,19 +5,19 @@ require File.dirname(__FILE__) + '/../../../spec_helper' require 'puppet/indirector/certificate_request/rest' describe Puppet::SSL::CertificateRequest::Rest do - before do - @searcher = Puppet::SSL::CertificateRequest::Rest.new - end + before do + @searcher = Puppet::SSL::CertificateRequest::Rest.new + end - it "should be a sublcass of Puppet::Indirector::REST" do - Puppet::SSL::CertificateRequest::Rest.superclass.should equal(Puppet::Indirector::REST) - end + it "should be a sublcass of Puppet::Indirector::REST" do + Puppet::SSL::CertificateRequest::Rest.superclass.should equal(Puppet::Indirector::REST) + end - it "should set server_setting to :ca_server" do - Puppet::SSL::CertificateRequest::Rest.server_setting.should == :ca_server - end + it "should set server_setting to :ca_server" do + Puppet::SSL::CertificateRequest::Rest.server_setting.should == :ca_server + end - it "should set port_setting to :ca_port" do - Puppet::SSL::CertificateRequest::Rest.port_setting.should == :ca_port - end + it "should set port_setting to :ca_port" do + Puppet::SSL::CertificateRequest::Rest.port_setting.should == :ca_port + end end diff --git a/spec/unit/indirector/certificate_revocation_list/ca_spec.rb b/spec/unit/indirector/certificate_revocation_list/ca_spec.rb index f6da86861..756185da9 100755 --- a/spec/unit/indirector/certificate_revocation_list/ca_spec.rb +++ b/spec/unit/indirector/certificate_revocation_list/ca_spec.rb @@ -8,14 +8,14 @@ require File.dirname(__FILE__) + '/../../../spec_helper' require 'puppet/indirector/certificate_revocation_list/ca' describe Puppet::SSL::CertificateRevocationList::Ca do - it "should have documentation" do - Puppet::SSL::CertificateRevocationList::Ca.doc.should be_instance_of(String) - end + it "should have documentation" do + Puppet::SSL::CertificateRevocationList::Ca.doc.should be_instance_of(String) + end - it "should use the :cacrl setting as the crl location" do - Puppet.settings.stubs(:value).returns "whatever" - Puppet.settings.stubs(:use) - Puppet.settings.stubs(:value).with(:cacrl).returns "/request/dir" - Puppet::SSL::CertificateRevocationList::Ca.new.path("whatever").should == "/request/dir" - end + it "should use the :cacrl setting as the crl location" do + Puppet.settings.stubs(:value).returns "whatever" + Puppet.settings.stubs(:use) + Puppet.settings.stubs(:value).with(:cacrl).returns "/request/dir" + Puppet::SSL::CertificateRevocationList::Ca.new.path("whatever").should == "/request/dir" + end end diff --git a/spec/unit/indirector/certificate_revocation_list/file_spec.rb b/spec/unit/indirector/certificate_revocation_list/file_spec.rb index 5db4f8c06..507942a6a 100755 --- a/spec/unit/indirector/certificate_revocation_list/file_spec.rb +++ b/spec/unit/indirector/certificate_revocation_list/file_spec.rb @@ -8,13 +8,13 @@ require File.dirname(__FILE__) + '/../../../spec_helper' require 'puppet/indirector/certificate_revocation_list/file' describe Puppet::SSL::CertificateRevocationList::File do - it "should have documentation" do - Puppet::SSL::CertificateRevocationList::File.doc.should be_instance_of(String) - end + it "should have documentation" do + Puppet::SSL::CertificateRevocationList::File.doc.should be_instance_of(String) + end - it "should always store the file to :hostcrl location" do - Puppet.settings.expects(:value).with(:hostcrl).returns "/host/crl" - Puppet.settings.stubs(:use) - Puppet::SSL::CertificateRevocationList::File.file_location.should == "/host/crl" - end + it "should always store the file to :hostcrl location" do + Puppet.settings.expects(:value).with(:hostcrl).returns "/host/crl" + Puppet.settings.stubs(:use) + Puppet::SSL::CertificateRevocationList::File.file_location.should == "/host/crl" + end end diff --git a/spec/unit/indirector/certificate_revocation_list/rest_spec.rb b/spec/unit/indirector/certificate_revocation_list/rest_spec.rb index 4c7cdc3d5..8cb32632e 100755 --- a/spec/unit/indirector/certificate_revocation_list/rest_spec.rb +++ b/spec/unit/indirector/certificate_revocation_list/rest_spec.rb @@ -5,19 +5,19 @@ require File.dirname(__FILE__) + '/../../../spec_helper' require 'puppet/indirector/certificate_revocation_list/rest' describe Puppet::SSL::CertificateRevocationList::Rest do - before do - @searcher = Puppet::SSL::CertificateRevocationList::Rest.new - end + before do + @searcher = Puppet::SSL::CertificateRevocationList::Rest.new + end - it "should be a sublcass of Puppet::Indirector::REST" do - Puppet::SSL::CertificateRevocationList::Rest.superclass.should equal(Puppet::Indirector::REST) - end + it "should be a sublcass of Puppet::Indirector::REST" do + Puppet::SSL::CertificateRevocationList::Rest.superclass.should equal(Puppet::Indirector::REST) + end - it "should set server_setting to :ca_server" do - Puppet::SSL::CertificateRevocationList::Rest.server_setting.should == :ca_server - end + it "should set server_setting to :ca_server" do + Puppet::SSL::CertificateRevocationList::Rest.server_setting.should == :ca_server + end - it "should set port_setting to :ca_port" do - Puppet::SSL::CertificateRevocationList::Rest.port_setting.should == :ca_port - end + it "should set port_setting to :ca_port" do + Puppet::SSL::CertificateRevocationList::Rest.port_setting.should == :ca_port + end end diff --git a/spec/unit/indirector/code_spec.rb b/spec/unit/indirector/code_spec.rb index 3eb7540ef..916469a14 100755 --- a/spec/unit/indirector/code_spec.rb +++ b/spec/unit/indirector/code_spec.rb @@ -4,30 +4,30 @@ require File.dirname(__FILE__) + '/../../spec_helper' require 'puppet/indirector/code' describe Puppet::Indirector::Code do - before do - Puppet::Indirector::Terminus.stubs(:register_terminus_class) - @model = mock 'model' - @indirection = stub 'indirection', :name => :mystuff, :register_terminus_type => nil, :model => @model - Puppet::Indirector::Indirection.stubs(:instance).returns(@indirection) + before do + Puppet::Indirector::Terminus.stubs(:register_terminus_class) + @model = mock 'model' + @indirection = stub 'indirection', :name => :mystuff, :register_terminus_type => nil, :model => @model + Puppet::Indirector::Indirection.stubs(:instance).returns(@indirection) - @code_class = Class.new(Puppet::Indirector::Code) do - def self.to_s - "Mystuff::Testing" - end - end - - @searcher = @code_class.new + @code_class = Class.new(Puppet::Indirector::Code) do + def self.to_s + "Mystuff::Testing" + end end - it "should not have a find() method defined" do - @searcher.should_not respond_to(:find) - end + @searcher = @code_class.new + end - it "should not have a save() method defined" do - @searcher.should_not respond_to(:save) - end + it "should not have a find() method defined" do + @searcher.should_not respond_to(:find) + end - it "should not have a destroy() method defined" do - @searcher.should_not respond_to(:destroy) - end + it "should not have a save() method defined" do + @searcher.should_not respond_to(:save) + end + + it "should not have a destroy() method defined" do + @searcher.should_not respond_to(:destroy) + end end diff --git a/spec/unit/indirector/direct_file_server_spec.rb b/spec/unit/indirector/direct_file_server_spec.rb index e32fe6678..0111b5445 100755 --- a/spec/unit/indirector/direct_file_server_spec.rb +++ b/spec/unit/indirector/direct_file_server_spec.rb @@ -8,77 +8,77 @@ require File.dirname(__FILE__) + '/../../spec_helper' require 'puppet/indirector/direct_file_server' describe Puppet::Indirector::DirectFileServer do - before :each do - Puppet::Indirector::Terminus.stubs(:register_terminus_class) - @model = mock 'model' - @indirection = stub 'indirection', :name => :mystuff, :register_terminus_type => nil, :model => @model - Puppet::Indirector::Indirection.stubs(:instance).returns(@indirection) + before :each do + Puppet::Indirector::Terminus.stubs(:register_terminus_class) + @model = mock 'model' + @indirection = stub 'indirection', :name => :mystuff, :register_terminus_type => nil, :model => @model + Puppet::Indirector::Indirection.stubs(:instance).returns(@indirection) + + @direct_file_class = Class.new(Puppet::Indirector::DirectFileServer) do + def self.to_s + "Testing::Mytype" + end + end + + @server = @direct_file_class.new - @direct_file_class = Class.new(Puppet::Indirector::DirectFileServer) do - def self.to_s - "Testing::Mytype" - end - end + @uri = "file:///my/local" - @server = @direct_file_class.new + @request = Puppet::Indirector::Request.new(:mytype, :find, @uri) + end - @uri = "file:///my/local" + describe Puppet::Indirector::DirectFileServer, "when finding a single file" do - @request = Puppet::Indirector::Request.new(:mytype, :find, @uri) + it "should return nil if the file does not exist" do + FileTest.expects(:exists?).with("/my/local").returns false + @server.find(@request).should be_nil end - describe Puppet::Indirector::DirectFileServer, "when finding a single file" do + it "should return a Content instance created with the full path to the file if the file exists" do + FileTest.expects(:exists?).with("/my/local").returns true + @model.expects(:new).returns(:mycontent) + @server.find(@request).should == :mycontent + end + end - it "should return nil if the file does not exist" do - FileTest.expects(:exists?).with("/my/local").returns false - @server.find(@request).should be_nil - end + describe Puppet::Indirector::DirectFileServer, "when creating the instance for a single found file" do - it "should return a Content instance created with the full path to the file if the file exists" do - FileTest.expects(:exists?).with("/my/local").returns true - @model.expects(:new).returns(:mycontent) - @server.find(@request).should == :mycontent - end + before do + @data = mock 'content' + @data.stubs(:collect) + FileTest.expects(:exists?).with("/my/local").returns true end - describe Puppet::Indirector::DirectFileServer, "when creating the instance for a single found file" do + it "should pass the full path to the instance" do + @model.expects(:new).with { |key, options| key == "/my/local" }.returns(@data) + @server.find(@request) + end - before do - @data = mock 'content' - @data.stubs(:collect) - FileTest.expects(:exists?).with("/my/local").returns true - end + it "should pass the :links setting on to the created Content instance if the file exists and there is a value for :links" do + @model.expects(:new).returns(@data) + @data.expects(:links=).with(:manage) - it "should pass the full path to the instance" do - @model.expects(:new).with { |key, options| key == "/my/local" }.returns(@data) - @server.find(@request) - end + @request.stubs(:options).returns(:links => :manage) + @server.find(@request) + end + end - it "should pass the :links setting on to the created Content instance if the file exists and there is a value for :links" do - @model.expects(:new).returns(@data) - @data.expects(:links=).with(:manage) + describe Puppet::Indirector::DirectFileServer, "when searching for multiple files" do + it "should return nil if the file does not exist" do + FileTest.expects(:exists?).with("/my/local").returns false + @server.find(@request).should be_nil + end - @request.stubs(:options).returns(:links => :manage) - @server.find(@request) - end + it "should use :path2instances from the terminus_helper to return instances if the file exists" do + FileTest.expects(:exists?).with("/my/local").returns true + @server.expects(:path2instances) + @server.search(@request) end - describe Puppet::Indirector::DirectFileServer, "when searching for multiple files" do - it "should return nil if the file does not exist" do - FileTest.expects(:exists?).with("/my/local").returns false - @server.find(@request).should be_nil - end - - it "should use :path2instances from the terminus_helper to return instances if the file exists" do - FileTest.expects(:exists?).with("/my/local").returns true - @server.expects(:path2instances) - @server.search(@request) - end - - it "should pass the original request to :path2instances" do - FileTest.expects(:exists?).with("/my/local").returns true - @server.expects(:path2instances).with(@request, "/my/local") - @server.search(@request) - end + it "should pass the original request to :path2instances" do + FileTest.expects(:exists?).with("/my/local").returns true + @server.expects(:path2instances).with(@request, "/my/local") + @server.search(@request) end + end end diff --git a/spec/unit/indirector/envelope_spec.rb b/spec/unit/indirector/envelope_spec.rb index 17c62023a..fb80824c9 100755 --- a/spec/unit/indirector/envelope_spec.rb +++ b/spec/unit/indirector/envelope_spec.rb @@ -4,44 +4,44 @@ require File.dirname(__FILE__) + '/../../spec_helper' require 'puppet/indirector/envelope' describe Puppet::Indirector::Envelope do - before do - @instance = Object.new - @instance.extend(Puppet::Indirector::Envelope) + before do + @instance = Object.new + @instance.extend(Puppet::Indirector::Envelope) + end + + it "should have an expiration accessor" do + @instance.expiration = "testing" + @instance.expiration.should == "testing" + end + + it "should have an expiration setter" do + @instance.should respond_to(:expiration=) + end + + it "should have a means of testing whether it is expired" do + @instance.should respond_to(:expired?) + end + + describe "when testing if it is expired" do + it "should return false if there is no expiration set" do + @instance.should_not be_expired end - it "should have an expiration accessor" do - @instance.expiration = "testing" - @instance.expiration.should == "testing" + it "should return true if the current date is after the expiration date" do + @instance.expiration = Time.now - 10 + @instance.should be_expired end - it "should have an expiration setter" do - @instance.should respond_to(:expiration=) + it "should return false if the current date is prior to the expiration date" do + @instance.expiration = Time.now + 10 + @instance.should_not be_expired end - it "should have a means of testing whether it is expired" do - @instance.should respond_to(:expired?) - end - - describe "when testing if it is expired" do - it "should return false if there is no expiration set" do - @instance.should_not be_expired - end - - it "should return true if the current date is after the expiration date" do - @instance.expiration = Time.now - 10 - @instance.should be_expired - end - - it "should return false if the current date is prior to the expiration date" do - @instance.expiration = Time.now + 10 - @instance.should_not be_expired - end - - it "should return false if the current date is equal to the expiration date" do - now = Time.now - Time.stubs(:now).returns(now) - @instance.expiration = now - @instance.should_not be_expired - end + it "should return false if the current date is equal to the expiration date" do + now = Time.now + Time.stubs(:now).returns(now) + @instance.expiration = now + @instance.should_not be_expired end + end end diff --git a/spec/unit/indirector/exec_spec.rb b/spec/unit/indirector/exec_spec.rb index 4a7e2b50e..acad1ea93 100755 --- a/spec/unit/indirector/exec_spec.rb +++ b/spec/unit/indirector/exec_spec.rb @@ -5,52 +5,52 @@ require File.dirname(__FILE__) + '/../../spec_helper' require 'puppet/indirector/exec' describe Puppet::Indirector::Exec do - before do - @indirection = stub 'indirection', :name => :testing - Puppet::Indirector::Indirection.expects(:instance).with(:testing).returns(@indirection) - @exec_class = Class.new(Puppet::Indirector::Exec) do - def self.to_s - "Testing::Mytype" - end + before do + @indirection = stub 'indirection', :name => :testing + Puppet::Indirector::Indirection.expects(:instance).with(:testing).returns(@indirection) + @exec_class = Class.new(Puppet::Indirector::Exec) do + def self.to_s + "Testing::Mytype" + end + + attr_accessor :command + end - attr_accessor :command - end + @searcher = @exec_class.new + @searcher.command = ["/echo"] - @searcher = @exec_class.new - @searcher.command = ["/echo"] + @request = stub 'request', :key => "foo" + end - @request = stub 'request', :key => "foo" - end + it "should throw an exception if the command is not an array" do + @searcher.command = "/usr/bin/echo" + proc { @searcher.find(@request) }.should raise_error(Puppet::DevError) + end - it "should throw an exception if the command is not an array" do - @searcher.command = "/usr/bin/echo" - proc { @searcher.find(@request) }.should raise_error(Puppet::DevError) - end + it "should throw an exception if the command is not fully qualified" do + @searcher.command = ["mycommand"] + proc { @searcher.find(@request) }.should raise_error(ArgumentError) + end - it "should throw an exception if the command is not fully qualified" do - @searcher.command = ["mycommand"] - proc { @searcher.find(@request) }.should raise_error(ArgumentError) - end + it "should execute the command with the object name as the only argument" do + @searcher.expects(:execute).with(%w{/echo foo}) + @searcher.find(@request) + end - it "should execute the command with the object name as the only argument" do - @searcher.expects(:execute).with(%w{/echo foo}) - @searcher.find(@request) - end + it "should return the output of the script" do + @searcher.expects(:execute).with(%w{/echo foo}).returns("whatever") + @searcher.find(@request).should == "whatever" + end - it "should return the output of the script" do - @searcher.expects(:execute).with(%w{/echo foo}).returns("whatever") - @searcher.find(@request).should == "whatever" - end - - it "should return nil when the command produces no output" do - @searcher.expects(:execute).with(%w{/echo foo}).returns(nil) - @searcher.find(@request).should be_nil - end + it "should return nil when the command produces no output" do + @searcher.expects(:execute).with(%w{/echo foo}).returns(nil) + @searcher.find(@request).should be_nil + end - it "should return nil and log an error if there's an execution failure" do - @searcher.expects(:execute).with(%w{/echo foo}).raises(Puppet::ExecutionFailure.new("message")) + it "should return nil and log an error if there's an execution failure" do + @searcher.expects(:execute).with(%w{/echo foo}).raises(Puppet::ExecutionFailure.new("message")) - Puppet.expects(:err) - @searcher.find(@request).should be_nil - end + Puppet.expects(:err) + @searcher.find(@request).should be_nil + end end diff --git a/spec/unit/indirector/facts/active_record_spec.rb b/spec/unit/indirector/facts/active_record_spec.rb index 1718311c5..0cdb70e01 100755 --- a/spec/unit/indirector/facts/active_record_spec.rb +++ b/spec/unit/indirector/facts/active_record_spec.rb @@ -5,100 +5,100 @@ require File.dirname(__FILE__) + '/../../../spec_helper' require 'puppet/node/facts' describe "Puppet::Node::Facts::ActiveRecord" do - confine "Missing Rails" => Puppet.features.rails? + confine "Missing Rails" => Puppet.features.rails? + before do + require 'puppet/indirector/facts/active_record' + Puppet.features.stubs(:rails?).returns true + Puppet::Rails.stubs(:init) + @terminus = Puppet::Node::Facts::ActiveRecord.new + end + + it "should be a subclass of the ActiveRecord terminus class" do + Puppet::Node::Facts::ActiveRecord.ancestors.should be_include(Puppet::Indirector::ActiveRecord) + end + + it "should use Puppet::Rails::Host as its ActiveRecord model" do + Puppet::Node::Facts::ActiveRecord.ar_model.should equal(Puppet::Rails::Host) + end + + describe "when finding an instance" do before do - require 'puppet/indirector/facts/active_record' - Puppet.features.stubs(:rails?).returns true - Puppet::Rails.stubs(:init) - @terminus = Puppet::Node::Facts::ActiveRecord.new + @request = stub 'request', :key => "foo" end - it "should be a subclass of the ActiveRecord terminus class" do - Puppet::Node::Facts::ActiveRecord.ancestors.should be_include(Puppet::Indirector::ActiveRecord) + it "should use the Hosts ActiveRecord class to find the host" do + Puppet::Rails::Host.expects(:find_by_name).with { |key, args| key == "foo" } + @terminus.find(@request) end - it "should use Puppet::Rails::Host as its ActiveRecord model" do - Puppet::Node::Facts::ActiveRecord.ar_model.should equal(Puppet::Rails::Host) + it "should include the fact names and values when finding the host" do + Puppet::Rails::Host.expects(:find_by_name).with { |key, args| args[:include] == {:fact_values => :fact_name} } + @terminus.find(@request) end - describe "when finding an instance" do - before do - @request = stub 'request', :key => "foo" - end - - it "should use the Hosts ActiveRecord class to find the host" do - Puppet::Rails::Host.expects(:find_by_name).with { |key, args| key == "foo" } - @terminus.find(@request) - end + it "should return nil if no host instance can be found" do + Puppet::Rails::Host.expects(:find_by_name).returns nil - it "should include the fact names and values when finding the host" do - Puppet::Rails::Host.expects(:find_by_name).with { |key, args| args[:include] == {:fact_values => :fact_name} } - @terminus.find(@request) - end - - it "should return nil if no host instance can be found" do - Puppet::Rails::Host.expects(:find_by_name).returns nil - - @terminus.find(@request).should be_nil - end + @terminus.find(@request).should be_nil + end - it "should convert the node's parameters into a Facts instance if a host instance is found" do - host = stub 'host', :name => "foo" - host.expects(:get_facts_hash).returns("one" => [mock("two_value", :value => "two")], "three" => [mock("three_value", :value => "four")]) + it "should convert the node's parameters into a Facts instance if a host instance is found" do + host = stub 'host', :name => "foo" + host.expects(:get_facts_hash).returns("one" => [mock("two_value", :value => "two")], "three" => [mock("three_value", :value => "four")]) - Puppet::Rails::Host.expects(:find_by_name).returns host + Puppet::Rails::Host.expects(:find_by_name).returns host - result = @terminus.find(@request) + result = @terminus.find(@request) - result.should be_instance_of(Puppet::Node::Facts) - result.name.should == "foo" - result.values.should == {"one" => "two", "three" => "four"} - end + result.should be_instance_of(Puppet::Node::Facts) + result.name.should == "foo" + result.values.should == {"one" => "two", "three" => "four"} + end - it "should convert all single-member arrays into non-arrays" do - host = stub 'host', :name => "foo" - host.expects(:get_facts_hash).returns("one" => [mock("two_value", :value => "two")]) + it "should convert all single-member arrays into non-arrays" do + host = stub 'host', :name => "foo" + host.expects(:get_facts_hash).returns("one" => [mock("two_value", :value => "two")]) - Puppet::Rails::Host.expects(:find_by_name).returns host + Puppet::Rails::Host.expects(:find_by_name).returns host - @terminus.find(@request).values["one"].should == "two" - end + @terminus.find(@request).values["one"].should == "two" end + end - describe "when saving an instance" do - before do - @host = stub 'host', :name => "foo", :save => nil, :merge_facts => nil - Puppet::Rails::Host.stubs(:find_by_name).returns @host - @facts = Puppet::Node::Facts.new("foo", "one" => "two", "three" => "four") - @request = stub 'request', :key => "foo", :instance => @facts - end + describe "when saving an instance" do + before do + @host = stub 'host', :name => "foo", :save => nil, :merge_facts => nil + Puppet::Rails::Host.stubs(:find_by_name).returns @host + @facts = Puppet::Node::Facts.new("foo", "one" => "two", "three" => "four") + @request = stub 'request', :key => "foo", :instance => @facts + end - it "should find the Rails host with the same name" do - Puppet::Rails::Host.expects(:find_by_name).with("foo").returns @host + it "should find the Rails host with the same name" do + Puppet::Rails::Host.expects(:find_by_name).with("foo").returns @host - @terminus.save(@request) - end + @terminus.save(@request) + end - it "should create a new Rails host if none can be found" do - Puppet::Rails::Host.expects(:find_by_name).with("foo").returns nil + it "should create a new Rails host if none can be found" do + Puppet::Rails::Host.expects(:find_by_name).with("foo").returns nil - Puppet::Rails::Host.expects(:create).with(:name => "foo").returns @host + Puppet::Rails::Host.expects(:create).with(:name => "foo").returns @host - @terminus.save(@request) - end + @terminus.save(@request) + end - it "should set the facts as facts on the Rails host instance" do - # There is other stuff added to the hash. - @host.expects(:merge_facts).with { |args| args["one"] == "two" and args["three"] == "four" } + it "should set the facts as facts on the Rails host instance" do + # There is other stuff added to the hash. + @host.expects(:merge_facts).with { |args| args["one"] == "two" and args["three"] == "four" } - @terminus.save(@request) - end + @terminus.save(@request) + end - it "should save the Rails host instance" do - @host.expects(:save) + it "should save the Rails host instance" do + @host.expects(:save) - @terminus.save(@request) - end + @terminus.save(@request) end + end end diff --git a/spec/unit/indirector/facts/couch_spec.rb b/spec/unit/indirector/facts/couch_spec.rb index 088401280..c0dd54b8a 100644 --- a/spec/unit/indirector/facts/couch_spec.rb +++ b/spec/unit/indirector/facts/couch_spec.rb @@ -5,94 +5,94 @@ require File.dirname(__FILE__) + '/../../../spec_helper' require 'puppet/node/facts' describe "Puppet::Node::Facts::Couch" do - confine "couchrest gem is missing; cannot test couch terminus" => Puppet.features.couchdb? - require 'puppet/indirector/facts/couch' if Puppet.features.couchdb? - - before do - @mock_db = mock('couch db') - mock_document = CouchRest::Document.new(:_id => fake_request.key, :facts => fake_request.values) - mock_document.stubs(:database).returns(@mock_db) - @mock_db.stubs(:get).with(fake_request.key).returns(mock_document) - Puppet::Node::Facts::Couch.stubs(:db).returns(@mock_db) + confine "couchrest gem is missing; cannot test couch terminus" => Puppet.features.couchdb? + require 'puppet/indirector/facts/couch' if Puppet.features.couchdb? + + before do + @mock_db = mock('couch db') + mock_document = CouchRest::Document.new(:_id => fake_request.key, :facts => fake_request.values) + mock_document.stubs(:database).returns(@mock_db) + @mock_db.stubs(:get).with(fake_request.key).returns(mock_document) + Puppet::Node::Facts::Couch.stubs(:db).returns(@mock_db) + end + + subject { Puppet::Node::Facts::Couch } + + describe "#find" do + describe "when the node document exists" do + it "should find the request by key" do + @mock_db.expects(:get).with(fake_request.key).returns({'_id' => fake_request.key, 'facts' => fake_request.instance.values}) + subject.new.find(fake_request).should == fake_request.instance + end end - subject { Puppet::Node::Facts::Couch } - - describe "#find" do - describe "when the node document exists" do - it "should find the request by key" do - @mock_db.expects(:get).with(fake_request.key).returns({'_id' => fake_request.key, 'facts' => fake_request.instance.values}) - subject.new.find(fake_request).should == fake_request.instance - end - end - - describe "when the node document does not exist" do - before do - @mock_db.expects(:get). - with(fake_request.key). - raises(RestClient::ResourceNotFound) - end - - it "should return nil" do - subject.new.find(fake_request).should be_nil - end - - it "should send Puppet a debug message" do - Puppet.expects(:debug).with("No couchdb document with id: test.local") - subject.new.find(fake_request).should be_nil - end - - end - end - - describe "#save" do - describe "with options" do - subject do - lambda { Puppet::Node::Facts::Couch.new.save(fake_request([1])) } - end + describe "when the node document does not exist" do + before do + @mock_db.expects(:get). + with(fake_request.key). + raises(RestClient::ResourceNotFound) + end - it { should raise_error(ArgumentError, "PUT does not accept options") } - end + it "should return nil" do + subject.new.find(fake_request).should be_nil + end - it "should save the json to the CouchDB database" do - @mock_db.expects(:save_doc).at_least_once.returns({'ok' => true }) - subject.new.save(fake_request) - end + it "should send Puppet a debug message" do + Puppet.expects(:debug).with("No couchdb document with id: test.local") + subject.new.find(fake_request).should be_nil + end - describe "when the document exists" do - before do - @doc = CouchRest::Document.new(:_id => fake_request.key, :facts => fake_request.instance.values) - @mock_db.expects(:get).with(fake_request.key).returns(@doc) - end + end + end - it "saves the document" do - @doc.expects(:save) - subject.new.save(fake_request) - end + describe "#save" do + describe "with options" do + subject do + lambda { Puppet::Node::Facts::Couch.new.save(fake_request([1])) } + end - end + it { should raise_error(ArgumentError, "PUT does not accept options") } + end - describe "when the document does not exist" do - before do - @mock_db.expects(:get). - with(fake_request.key). - raises(RestClient::ResourceNotFound) - end + it "should save the json to the CouchDB database" do + @mock_db.expects(:save_doc).at_least_once.returns({'ok' => true }) + subject.new.save(fake_request) + end - it "saves the document" do - @mock_db.expects(:save_doc) - subject.new.save(fake_request) - end + describe "when the document exists" do + before do + @doc = CouchRest::Document.new(:_id => fake_request.key, :facts => fake_request.instance.values) + @mock_db.expects(:get).with(fake_request.key).returns(@doc) + end - end + it "saves the document" do + @doc.expects(:save) + subject.new.save(fake_request) + end end - def fake_request(options={}) - facts = YAML.load_file(File.join(PuppetSpec::FIXTURE_DIR, 'yaml', 'test.local.yaml')) - Struct.new(:instance, :key, :options).new(facts, facts.name, options) + describe "when the document does not exist" do + before do + @mock_db.expects(:get). + with(fake_request.key). + raises(RestClient::ResourceNotFound) + end + + it "saves the document" do + @mock_db.expects(:save_doc) + subject.new.save(fake_request) + end + end - private :fake_request + + end + + def fake_request(options={}) + facts = YAML.load_file(File.join(PuppetSpec::FIXTURE_DIR, 'yaml', 'test.local.yaml')) + Struct.new(:instance, :key, :options).new(facts, facts.name, options) + end + private :fake_request end diff --git a/spec/unit/indirector/facts/facter_spec.rb b/spec/unit/indirector/facts/facter_spec.rb index 2b127ad34..169440915 100755 --- a/spec/unit/indirector/facts/facter_spec.rb +++ b/spec/unit/indirector/facts/facter_spec.rb @@ -8,136 +8,136 @@ require File.dirname(__FILE__) + '/../../../spec_helper' require 'puppet/indirector/facts/facter' describe Puppet::Node::Facts::Facter do - it "should be a subclass of the Code terminus" do - Puppet::Node::Facts::Facter.superclass.should equal(Puppet::Indirector::Code) - end + it "should be a subclass of the Code terminus" do + Puppet::Node::Facts::Facter.superclass.should equal(Puppet::Indirector::Code) + end + + it "should have documentation" do + Puppet::Node::Facts::Facter.doc.should_not be_nil + end + + it "should be registered with the configuration store indirection" do + indirection = Puppet::Indirector::Indirection.instance(:facts) + Puppet::Node::Facts::Facter.indirection.should equal(indirection) + end + + it "should have its name set to :facter" do + Puppet::Node::Facts::Facter.name.should == :facter + end + + it "should load facts on initialization" do + Puppet::Node::Facts::Facter.expects(:load_fact_plugins) + Puppet::Node::Facts::Facter.new + end +end - it "should have documentation" do - Puppet::Node::Facts::Facter.doc.should_not be_nil +describe Puppet::Node::Facts::Facter do + before :each do + @facter = Puppet::Node::Facts::Facter.new + Facter.stubs(:to_hash).returns({}) + @name = "me" + @request = stub 'request', :key => @name + end + + describe Puppet::Node::Facts::Facter, " when finding facts" do + it "should return a Facts instance" do + @facter.find(@request).should be_instance_of(Puppet::Node::Facts) end - it "should be registered with the configuration store indirection" do - indirection = Puppet::Indirector::Indirection.instance(:facts) - Puppet::Node::Facts::Facter.indirection.should equal(indirection) + it "should return a Facts instance with the provided key as the name" do + @facter.find(@request).name.should == @name end - it "should have its name set to :facter" do - Puppet::Node::Facts::Facter.name.should == :facter + it "should return the Facter facts as the values in the Facts instance" do + Facter.expects(:to_hash).returns("one" => "two") + facts = @facter.find(@request) + facts.values["one"].should == "two" end - it "should load facts on initialization" do - Puppet::Node::Facts::Facter.expects(:load_fact_plugins) - Puppet::Node::Facts::Facter.new - end -end + it "should add local facts" do + facts = Puppet::Node::Facts.new("foo") + Puppet::Node::Facts.expects(:new).returns facts + facts.expects(:add_local_facts) -describe Puppet::Node::Facts::Facter do - before :each do - @facter = Puppet::Node::Facts::Facter.new - Facter.stubs(:to_hash).returns({}) - @name = "me" - @request = stub 'request', :key => @name + @facter.find(@request) end - describe Puppet::Node::Facts::Facter, " when finding facts" do - it "should return a Facts instance" do - @facter.find(@request).should be_instance_of(Puppet::Node::Facts) - end - - it "should return a Facts instance with the provided key as the name" do - @facter.find(@request).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(@request) - facts.values["one"].should == "two" - end + it "should convert all facts into strings" do + facts = Puppet::Node::Facts.new("foo") + Puppet::Node::Facts.expects(:new).returns facts + facts.expects(:stringify) - it "should add local facts" do - facts = Puppet::Node::Facts.new("foo") - Puppet::Node::Facts.expects(:new).returns facts - facts.expects(:add_local_facts) - - @facter.find(@request) - end - - it "should convert all facts into strings" do - facts = Puppet::Node::Facts.new("foo") - Puppet::Node::Facts.expects(:new).returns facts - facts.expects(:stringify) - - @facter.find(@request) - end + @facter.find(@request) + end - it "should call the downcase hook" do - facts = Puppet::Node::Facts.new("foo") - Puppet::Node::Facts.expects(:new).returns facts - facts.expects(:downcase_if_necessary) + it "should call the downcase hook" do + facts = Puppet::Node::Facts.new("foo") + Puppet::Node::Facts.expects(:new).returns facts + facts.expects(:downcase_if_necessary) - @facter.find(@request) - end + @facter.find(@request) end + end - describe Puppet::Node::Facts::Facter, " when saving facts" do + describe Puppet::Node::Facts::Facter, " when saving facts" do - it "should fail" do - proc { @facter.save(@facts) }.should raise_error(Puppet::DevError) - end + it "should fail" do + proc { @facter.save(@facts) }.should raise_error(Puppet::DevError) end + end - describe Puppet::Node::Facts::Facter, " when destroying facts" do + describe Puppet::Node::Facts::Facter, " when destroying facts" do - it "should fail" do - proc { @facter.destroy(@facts) }.should raise_error(Puppet::DevError) - end + it "should fail" do + proc { @facter.destroy(@facts) }.should raise_error(Puppet::DevError) end + end - it "should skip files when asked to load a directory" do - FileTest.expects(:directory?).with("myfile").returns false + it "should skip files when asked to load a directory" do + FileTest.expects(:directory?).with("myfile").returns false - Puppet::Node::Facts::Facter.load_facts_in_dir("myfile") - end + Puppet::Node::Facts::Facter.load_facts_in_dir("myfile") + end - it "should load each ruby file when asked to load a directory" do - FileTest.expects(:directory?).with("mydir").returns true - Dir.expects(:chdir).with("mydir").yields + it "should load each ruby file when asked to load a directory" do + FileTest.expects(:directory?).with("mydir").returns true + Dir.expects(:chdir).with("mydir").yields - Dir.expects(:glob).with("*.rb").returns %w{a.rb b.rb} + Dir.expects(:glob).with("*.rb").returns %w{a.rb b.rb} - Puppet::Node::Facts::Facter.expects(:load).with("a.rb") - Puppet::Node::Facts::Facter.expects(:load).with("b.rb") + Puppet::Node::Facts::Facter.expects(:load).with("a.rb") + Puppet::Node::Facts::Facter.expects(:load).with("b.rb") - Puppet::Node::Facts::Facter.load_facts_in_dir("mydir") - end + Puppet::Node::Facts::Facter.load_facts_in_dir("mydir") + end - describe Puppet::Node::Facts::Facter, "when loading fact plugins from disk" do - it "should load each directory in the Fact path" do - Puppet.settings.stubs(:value).returns "foo" - Puppet.settings.expects(:value).with(:factpath).returns("one#{File::PATH_SEPARATOR}two") + describe Puppet::Node::Facts::Facter, "when loading fact plugins from disk" do + it "should load each directory in the Fact path" do + Puppet.settings.stubs(:value).returns "foo" + Puppet.settings.expects(:value).with(:factpath).returns("one#{File::PATH_SEPARATOR}two") - Puppet::Node::Facts::Facter.expects(:load_facts_in_dir).with("one") - Puppet::Node::Facts::Facter.expects(:load_facts_in_dir).with("two") + Puppet::Node::Facts::Facter.expects(:load_facts_in_dir).with("one") + Puppet::Node::Facts::Facter.expects(:load_facts_in_dir).with("two") - Puppet::Node::Facts::Facter.load_fact_plugins - end + Puppet::Node::Facts::Facter.load_fact_plugins + end - it "should load all facts from the modules" do - Puppet.settings.stubs(:value).returns "foo" - Puppet::Node::Facts::Facter.stubs(:load_facts_in_dir) + it "should load all facts from the modules" do + Puppet.settings.stubs(:value).returns "foo" + Puppet::Node::Facts::Facter.stubs(:load_facts_in_dir) - Puppet.settings.expects(:value).with(:modulepath).returns("one#{File::PATH_SEPARATOR}two") + Puppet.settings.expects(:value).with(:modulepath).returns("one#{File::PATH_SEPARATOR}two") - Dir.stubs(:glob).returns [] - Dir.expects(:glob).with("one/*/lib/facter").returns %w{oneA oneB} - Dir.expects(:glob).with("two/*/lib/facter").returns %w{twoA twoB} + Dir.stubs(:glob).returns [] + Dir.expects(:glob).with("one/*/lib/facter").returns %w{oneA oneB} + Dir.expects(:glob).with("two/*/lib/facter").returns %w{twoA twoB} - Puppet::Node::Facts::Facter.expects(:load_facts_in_dir).with("oneA") - Puppet::Node::Facts::Facter.expects(:load_facts_in_dir).with("oneB") - Puppet::Node::Facts::Facter.expects(:load_facts_in_dir).with("twoA") - Puppet::Node::Facts::Facter.expects(:load_facts_in_dir).with("twoB") + Puppet::Node::Facts::Facter.expects(:load_facts_in_dir).with("oneA") + Puppet::Node::Facts::Facter.expects(:load_facts_in_dir).with("oneB") + Puppet::Node::Facts::Facter.expects(:load_facts_in_dir).with("twoA") + Puppet::Node::Facts::Facter.expects(:load_facts_in_dir).with("twoB") - Puppet::Node::Facts::Facter.load_fact_plugins - end + Puppet::Node::Facts::Facter.load_fact_plugins end + end end diff --git a/spec/unit/indirector/facts/rest_spec.rb b/spec/unit/indirector/facts/rest_spec.rb index db46db02c..19a56921f 100755 --- a/spec/unit/indirector/facts/rest_spec.rb +++ b/spec/unit/indirector/facts/rest_spec.rb @@ -5,7 +5,7 @@ require File.dirname(__FILE__) + '/../../../spec_helper' require 'puppet/indirector/facts/rest' describe Puppet::Node::Facts::Rest do - it "should be a sublcass of Puppet::Indirector::REST" do - Puppet::Node::Facts::Rest.superclass.should equal(Puppet::Indirector::REST) - end + it "should be a sublcass of Puppet::Indirector::REST" do + Puppet::Node::Facts::Rest.superclass.should equal(Puppet::Indirector::REST) + end end diff --git a/spec/unit/indirector/facts/yaml_spec.rb b/spec/unit/indirector/facts/yaml_spec.rb index 8b49fa3b5..e7bac3471 100755 --- a/spec/unit/indirector/facts/yaml_spec.rb +++ b/spec/unit/indirector/facts/yaml_spec.rb @@ -6,21 +6,21 @@ require 'puppet/node/facts' require 'puppet/indirector/facts/yaml' describe Puppet::Node::Facts::Yaml do - it "should be a subclass of the Yaml terminus" do - Puppet::Node::Facts::Yaml.superclass.should equal(Puppet::Indirector::Yaml) - end + it "should be a subclass of the Yaml terminus" do + Puppet::Node::Facts::Yaml.superclass.should equal(Puppet::Indirector::Yaml) + end - it "should have documentation" do - Puppet::Node::Facts::Yaml.doc.should_not be_nil - end + it "should have documentation" do + Puppet::Node::Facts::Yaml.doc.should_not be_nil + end - it "should be registered with the facts indirection" do - indirection = Puppet::Indirector::Indirection.instance(:facts) - Puppet::Node::Facts::Yaml.indirection.should equal(indirection) - end + it "should be registered with the facts indirection" do + indirection = Puppet::Indirector::Indirection.instance(:facts) + Puppet::Node::Facts::Yaml.indirection.should equal(indirection) + end - it "should have its name set to :facts" do - Puppet::Node::Facts::Yaml.name.should == :yaml - end + it "should have its name set to :facts" do + Puppet::Node::Facts::Yaml.name.should == :yaml + end end diff --git a/spec/unit/indirector/file_bucket_file/file_spec.rb b/spec/unit/indirector/file_bucket_file/file_spec.rb index 652da3df0..aa3ade6b6 100755 --- a/spec/unit/indirector/file_bucket_file/file_spec.rb +++ b/spec/unit/indirector/file_bucket_file/file_spec.rb @@ -5,286 +5,286 @@ require ::File.dirname(__FILE__) + '/../../../spec_helper' require 'puppet/indirector/file_bucket_file/file' describe Puppet::FileBucketFile::File do - it "should be a subclass of the Code terminus class" do - Puppet::FileBucketFile::File.superclass.should equal(Puppet::Indirector::Code) + it "should be a subclass of the Code terminus class" do + Puppet::FileBucketFile::File.superclass.should equal(Puppet::Indirector::Code) + end + + it "should have documentation" do + Puppet::FileBucketFile::File.doc.should be_instance_of(String) + end + + describe "when initializing" do + it "should use the filebucket settings section" do + Puppet.settings.expects(:use).with(:filebucket) + Puppet::FileBucketFile::File.new end + end - it "should have documentation" do - Puppet::FileBucketFile::File.doc.should be_instance_of(String) + + describe "the find_by_checksum method" do + before do + # this is the default from spec_helper, but it keeps getting reset at odd times + Puppet[:bucketdir] = "/dev/null/bucket" + + @digest = "4a8ec4fa5f01b4ab1a0ab8cbccb709f0" + @checksum = "{md5}4a8ec4fa5f01b4ab1a0ab8cbccb709f0" + @dir = '/dev/null/bucket/4/a/8/e/c/4/f/a/4a8ec4fa5f01b4ab1a0ab8cbccb709f0' + + @contents = "file contents" end - describe "when initializing" do - it "should use the filebucket settings section" do - Puppet.settings.expects(:use).with(:filebucket) - Puppet::FileBucketFile::File.new - end + it "should return nil if a file doesn't exist" do + ::File.expects(:exist?).with("#{@dir}/contents").returns false + + bucketfile = Puppet::FileBucketFile::File.new.send(:find_by_checksum, "{md5}#{@digest}", {}) + bucketfile.should == nil end + it "should find a filebucket if the file exists" do + ::File.expects(:exist?).with("#{@dir}/contents").returns true + ::File.expects(:exist?).with("#{@dir}/paths").returns false + ::File.expects(:read).with("#{@dir}/contents").returns @contents - describe "the find_by_checksum method" do - before do - # this is the default from spec_helper, but it keeps getting reset at odd times - Puppet[:bucketdir] = "/dev/null/bucket" + bucketfile = Puppet::FileBucketFile::File.new.send(:find_by_checksum, "{md5}#{@digest}", {}) + bucketfile.should_not == nil + end - @digest = "4a8ec4fa5f01b4ab1a0ab8cbccb709f0" - @checksum = "{md5}4a8ec4fa5f01b4ab1a0ab8cbccb709f0" - @dir = '/dev/null/bucket/4/a/8/e/c/4/f/a/4a8ec4fa5f01b4ab1a0ab8cbccb709f0' + it "should load the paths" do + paths = ["path1", "path2"] + ::File.expects(:exist?).with("#{@dir}/contents").returns true + ::File.expects(:exist?).with("#{@dir}/paths").returns true + ::File.expects(:read).with("#{@dir}/contents").returns @contents - @contents = "file contents" - end + mockfile = mock "file" + mockfile.expects(:readlines).returns( paths ) + ::File.expects(:open).with("#{@dir}/paths").yields mockfile - it "should return nil if a file doesn't exist" do - ::File.expects(:exist?).with("#{@dir}/contents").returns false + Puppet::FileBucketFile::File.new.send(:find_by_checksum, "{md5}#{@digest}", {}).paths.should == paths + end - bucketfile = Puppet::FileBucketFile::File.new.send(:find_by_checksum, "{md5}#{@digest}", {}) - bucketfile.should == nil - end + end - it "should find a filebucket if the file exists" do - ::File.expects(:exist?).with("#{@dir}/contents").returns true - ::File.expects(:exist?).with("#{@dir}/paths").returns false - ::File.expects(:read).with("#{@dir}/contents").returns @contents + describe "when retrieving files" do + before :each do + Puppet.settings.stubs(:use) + @store = Puppet::FileBucketFile::File.new - bucketfile = Puppet::FileBucketFile::File.new.send(:find_by_checksum, "{md5}#{@digest}", {}) - bucketfile.should_not == nil - end + @digest = "70924d6fa4b2d745185fa4660703a5c0" + @sum = stub 'sum', :name => @digest - it "should load the paths" do - paths = ["path1", "path2"] - ::File.expects(:exist?).with("#{@dir}/contents").returns true - ::File.expects(:exist?).with("#{@dir}/paths").returns true - ::File.expects(:read).with("#{@dir}/contents").returns @contents + @dir = "/what/ever" - mockfile = mock "file" - mockfile.expects(:readlines).returns( paths ) - ::File.expects(:open).with("#{@dir}/paths").yields mockfile + Puppet.stubs(:[]).with(:bucketdir).returns(@dir) - Puppet::FileBucketFile::File.new.send(:find_by_checksum, "{md5}#{@digest}", {}).paths.should == paths - end + @contents_path = '/what/ever/7/0/9/2/4/d/6/f/70924d6fa4b2d745185fa4660703a5c0/contents' + @paths_path = '/what/ever/7/0/9/2/4/d/6/f/70924d6fa4b2d745185fa4660703a5c0/paths' + @request = stub 'request', :key => "md5/#{@digest}/remote/path", :options => {} end - describe "when retrieving files" do - before :each do - Puppet.settings.stubs(:use) - @store = Puppet::FileBucketFile::File.new + it "should call find_by_checksum" do + @store.expects(:find_by_checksum).with{|x,opts| x == "{md5}#{@digest}"}.returns(false) + @store.find(@request) + end - @digest = "70924d6fa4b2d745185fa4660703a5c0" - @sum = stub 'sum', :name => @digest + it "should look for the calculated path" do + ::File.expects(:exist?).with(@contents_path).returns(false) + @store.find(@request) + end - @dir = "/what/ever" + it "should return an instance of Puppet::FileBucket::File created with the content if the file exists" do + content = "my content" + bucketfile = stub 'bucketfile' + bucketfile.stubs(:bucket_path) + bucketfile.stubs(:bucket_path=) + bucketfile.stubs(:checksum_data).returns(@digest) + bucketfile.stubs(:checksum).returns(@checksum) - Puppet.stubs(:[]).with(:bucketdir).returns(@dir) + bucketfile.expects(:contents=).with(content) + Puppet::FileBucket::File.expects(:new).with(nil, {:checksum => "{md5}#{@digest}"}).yields(bucketfile).returns(bucketfile) - @contents_path = '/what/ever/7/0/9/2/4/d/6/f/70924d6fa4b2d745185fa4660703a5c0/contents' - @paths_path = '/what/ever/7/0/9/2/4/d/6/f/70924d6fa4b2d745185fa4660703a5c0/paths' + ::File.expects(:exist?).with(@contents_path).returns(true) + ::File.expects(:exist?).with(@paths_path).returns(false) + ::File.expects(:read).with(@contents_path).returns(content) - @request = stub 'request', :key => "md5/#{@digest}/remote/path", :options => {} - end + @store.find(@request).should equal(bucketfile) + end - it "should call find_by_checksum" do - @store.expects(:find_by_checksum).with{|x,opts| x == "{md5}#{@digest}"}.returns(false) - @store.find(@request) - end + it "should return nil if no file is found" do + ::File.expects(:exist?).with(@contents_path).returns(false) + @store.find(@request).should be_nil + end - it "should look for the calculated path" do - ::File.expects(:exist?).with(@contents_path).returns(false) - @store.find(@request) - end + it "should fail intelligently if a found file cannot be read" do + ::File.expects(:exist?).with(@contents_path).returns(true) + ::File.expects(:read).with(@contents_path).raises(RuntimeError) + proc { @store.find(@request) }.should raise_error(Puppet::Error) + end - it "should return an instance of Puppet::FileBucket::File created with the content if the file exists" do - content = "my content" - bucketfile = stub 'bucketfile' - bucketfile.stubs(:bucket_path) - bucketfile.stubs(:bucket_path=) - bucketfile.stubs(:checksum_data).returns(@digest) - bucketfile.stubs(:checksum).returns(@checksum) + end - bucketfile.expects(:contents=).with(content) - Puppet::FileBucket::File.expects(:new).with(nil, {:checksum => "{md5}#{@digest}"}).yields(bucketfile).returns(bucketfile) + describe "when determining file paths" do + before do + Puppet[:bucketdir] = '/dev/null/bucketdir' + @digest = 'DEADBEEFC0FFEE' + @bucket = stub_everything "bucket" + @bucket.expects(:checksum_data).returns(@digest) + end - ::File.expects(:exist?).with(@contents_path).returns(true) - ::File.expects(:exist?).with(@paths_path).returns(false) - ::File.expects(:read).with(@contents_path).returns(content) + it "should use the value of the :bucketdir setting as the root directory" do + path = Puppet::FileBucketFile::File.new.send(:contents_path_for, @bucket) + path.should =~ %r{^/dev/null/bucketdir} + end - @store.find(@request).should equal(bucketfile) - end + it "should choose a path 8 directories deep with each directory name being the respective character in the filebucket" do + path = Puppet::FileBucketFile::File.new.send(:contents_path_for, @bucket) + dirs = @digest[0..7].split("").join(File::SEPARATOR) + path.should be_include(dirs) + end - it "should return nil if no file is found" do - ::File.expects(:exist?).with(@contents_path).returns(false) - @store.find(@request).should be_nil - end + it "should use the full filebucket as the final directory name" do + path = Puppet::FileBucketFile::File.new.send(:contents_path_for, @bucket) + ::File.basename(::File.dirname(path)).should == @digest + end - it "should fail intelligently if a found file cannot be read" do - ::File.expects(:exist?).with(@contents_path).returns(true) - ::File.expects(:read).with(@contents_path).raises(RuntimeError) - proc { @store.find(@request) }.should raise_error(Puppet::Error) - end + it "should use 'contents' as the actual file name" do + path = Puppet::FileBucketFile::File.new.send(:contents_path_for, @bucket) + ::File.basename(path).should == "contents" + end + it "should use the bucketdir, the 8 sum character directories, the full filebucket, and 'contents' as the full file name" do + path = Puppet::FileBucketFile::File.new.send(:contents_path_for, @bucket) + path.should == ['/dev/null/bucketdir', @digest[0..7].split(""), @digest, "contents"].flatten.join(::File::SEPARATOR) end + end + + describe "when saving files" do + before do + # this is the default from spec_helper, but it keeps getting reset at odd times + Puppet[:bucketdir] = "/dev/null/bucket" + + @digest = "4a8ec4fa5f01b4ab1a0ab8cbccb709f0" + @checksum = "{md5}4a8ec4fa5f01b4ab1a0ab8cbccb709f0" + @dir = '/dev/null/bucket/4/a/8/e/c/4/f/a/4a8ec4fa5f01b4ab1a0ab8cbccb709f0' + + @contents = "file contents" - describe "when determining file paths" do - before do - Puppet[:bucketdir] = '/dev/null/bucketdir' - @digest = 'DEADBEEFC0FFEE' - @bucket = stub_everything "bucket" - @bucket.expects(:checksum_data).returns(@digest) - end - - it "should use the value of the :bucketdir setting as the root directory" do - path = Puppet::FileBucketFile::File.new.send(:contents_path_for, @bucket) - path.should =~ %r{^/dev/null/bucketdir} - end - - it "should choose a path 8 directories deep with each directory name being the respective character in the filebucket" do - path = Puppet::FileBucketFile::File.new.send(:contents_path_for, @bucket) - dirs = @digest[0..7].split("").join(File::SEPARATOR) - path.should be_include(dirs) - end - - it "should use the full filebucket as the final directory name" do - path = Puppet::FileBucketFile::File.new.send(:contents_path_for, @bucket) - ::File.basename(::File.dirname(path)).should == @digest - end - - it "should use 'contents' as the actual file name" do - path = Puppet::FileBucketFile::File.new.send(:contents_path_for, @bucket) - ::File.basename(path).should == "contents" - end - - it "should use the bucketdir, the 8 sum character directories, the full filebucket, and 'contents' as the full file name" do - path = Puppet::FileBucketFile::File.new.send(:contents_path_for, @bucket) - path.should == ['/dev/null/bucketdir', @digest[0..7].split(""), @digest, "contents"].flatten.join(::File::SEPARATOR) - end + @bucket = stub "bucket file" + @bucket.stubs(:bucket_path) + @bucket.stubs(:checksum_data).returns(@digest) + @bucket.stubs(:path).returns(nil) + @bucket.stubs(:checksum).returns(nil) + @bucket.stubs(:contents).returns("file contents") end - describe "when saving files" do - before do - # this is the default from spec_helper, but it keeps getting reset at odd times - Puppet[:bucketdir] = "/dev/null/bucket" - - @digest = "4a8ec4fa5f01b4ab1a0ab8cbccb709f0" - @checksum = "{md5}4a8ec4fa5f01b4ab1a0ab8cbccb709f0" - @dir = '/dev/null/bucket/4/a/8/e/c/4/f/a/4a8ec4fa5f01b4ab1a0ab8cbccb709f0' - - @contents = "file contents" - - @bucket = stub "bucket file" - @bucket.stubs(:bucket_path) - @bucket.stubs(:checksum_data).returns(@digest) - @bucket.stubs(:path).returns(nil) - @bucket.stubs(:checksum).returns(nil) - @bucket.stubs(:contents).returns("file contents") - end - - it "should save the contents to the calculated path" do - ::File.stubs(:directory?).with(@dir).returns(true) - ::File.expects(:exist?).with("#{@dir}/contents").returns false - - mockfile = mock "file" - mockfile.expects(:print).with(@contents) - ::File.expects(:open).with("#{@dir}/contents", ::File::WRONLY|::File::CREAT, 0440).yields(mockfile) - - Puppet::FileBucketFile::File.new.send(:save_to_disk, @bucket) - end - - it "should make any directories necessary for storage" do - FileUtils.expects(:mkdir_p).with do |arg| - ::File.umask == 0007 and arg == @dir - end - ::File.expects(:directory?).with(@dir).returns(false) - ::File.expects(:open).with("#{@dir}/contents", ::File::WRONLY|::File::CREAT, 0440) - ::File.expects(:exist?).with("#{@dir}/contents").returns false - - Puppet::FileBucketFile::File.new.send(:save_to_disk, @bucket) - end + it "should save the contents to the calculated path" do + ::File.stubs(:directory?).with(@dir).returns(true) + ::File.expects(:exist?).with("#{@dir}/contents").returns false + + mockfile = mock "file" + mockfile.expects(:print).with(@contents) + ::File.expects(:open).with("#{@dir}/contents", ::File::WRONLY|::File::CREAT, 0440).yields(mockfile) + + Puppet::FileBucketFile::File.new.send(:save_to_disk, @bucket) end + it "should make any directories necessary for storage" do + FileUtils.expects(:mkdir_p).with do |arg| + ::File.umask == 0007 and arg == @dir + end + ::File.expects(:directory?).with(@dir).returns(false) + ::File.expects(:open).with("#{@dir}/contents", ::File::WRONLY|::File::CREAT, 0440) + ::File.expects(:exist?).with("#{@dir}/contents").returns false - describe "when verifying identical files" do - before do - # this is the default from spec_helper, but it keeps getting reset at odd times - Puppet[:bucketdir] = "/dev/null/bucket" + Puppet::FileBucketFile::File.new.send(:save_to_disk, @bucket) + end + end - @digest = "4a8ec4fa5f01b4ab1a0ab8cbccb709f0" - @checksum = "{md5}4a8ec4fa5f01b4ab1a0ab8cbccb709f0" - @dir = '/dev/null/bucket/4/a/8/e/c/4/f/a/4a8ec4fa5f01b4ab1a0ab8cbccb709f0' - @contents = "file contents" + describe "when verifying identical files" do + before do + # this is the default from spec_helper, but it keeps getting reset at odd times + Puppet[:bucketdir] = "/dev/null/bucket" - @bucket = stub "bucket file" - @bucket.stubs(:bucket_path) - @bucket.stubs(:checksum).returns(@checksum) - @bucket.stubs(:checksum_data).returns(@digest) - @bucket.stubs(:path).returns(nil) - @bucket.stubs(:contents).returns("file contents") - end + @digest = "4a8ec4fa5f01b4ab1a0ab8cbccb709f0" + @checksum = "{md5}4a8ec4fa5f01b4ab1a0ab8cbccb709f0" + @dir = '/dev/null/bucket/4/a/8/e/c/4/f/a/4a8ec4fa5f01b4ab1a0ab8cbccb709f0' - it "should raise an error if the files don't match" do - File.expects(:read).with("#{@dir}/contents").returns("corrupt contents") - lambda{ Puppet::FileBucketFile::File.new.send(:verify_identical_file!, @bucket) }.should raise_error(Puppet::FileBucket::BucketError) - end + @contents = "file contents" - it "should do nothing if the files match" do - File.expects(:read).with("#{@dir}/contents").returns("file contents") - Puppet::FileBucketFile::File.new.send(:verify_identical_file!, @bucket) - end + @bucket = stub "bucket file" + @bucket.stubs(:bucket_path) + @bucket.stubs(:checksum).returns(@checksum) + @bucket.stubs(:checksum_data).returns(@digest) + @bucket.stubs(:path).returns(nil) + @bucket.stubs(:contents).returns("file contents") + end + it "should raise an error if the files don't match" do + File.expects(:read).with("#{@dir}/contents").returns("corrupt contents") + lambda{ Puppet::FileBucketFile::File.new.send(:verify_identical_file!, @bucket) }.should raise_error(Puppet::FileBucket::BucketError) end + it "should do nothing if the files match" do + File.expects(:read).with("#{@dir}/contents").returns("file contents") + Puppet::FileBucketFile::File.new.send(:verify_identical_file!, @bucket) + end - describe "when writing to the paths file" do - before do - Puppet[:bucketdir] = '/dev/null/bucketdir' - @digest = '70924d6fa4b2d745185fa4660703a5c0' - @bucket = stub_everything "bucket" + end - @paths_path = '/dev/null/bucketdir/7/0/9/2/4/d/6/f/70924d6fa4b2d745185fa4660703a5c0/paths' - @paths = [] - @bucket.stubs(:paths).returns(@paths) - @bucket.stubs(:checksum_data).returns(@digest) - end + describe "when writing to the paths file" do + before do + Puppet[:bucketdir] = '/dev/null/bucketdir' + @digest = '70924d6fa4b2d745185fa4660703a5c0' + @bucket = stub_everything "bucket" - it "should create a file if it doesn't exist" do - @bucket.expects(:path).returns('path/to/save').at_least_once - File.expects(:exist?).with(@paths_path).returns(false) - file = stub "file" - file.expects(:puts).with('path/to/save') - File.expects(:open).with(@paths_path, ::File::WRONLY|::File::CREAT|::File::APPEND).yields(file) + @paths_path = '/dev/null/bucketdir/7/0/9/2/4/d/6/f/70924d6fa4b2d745185fa4660703a5c0/paths' - Puppet::FileBucketFile::File.new.send(:save_path_to_paths_file, @bucket) - end + @paths = [] + @bucket.stubs(:paths).returns(@paths) + @bucket.stubs(:checksum_data).returns(@digest) + end - it "should append to a file if it exists" do - @bucket.expects(:path).returns('path/to/save').at_least_once - File.expects(:exist?).with(@paths_path).returns(true) - old_file = stub "file" - old_file.stubs(:readlines).returns [] - File.expects(:open).with(@paths_path).yields(old_file) + it "should create a file if it doesn't exist" do + @bucket.expects(:path).returns('path/to/save').at_least_once + File.expects(:exist?).with(@paths_path).returns(false) + file = stub "file" + file.expects(:puts).with('path/to/save') + File.expects(:open).with(@paths_path, ::File::WRONLY|::File::CREAT|::File::APPEND).yields(file) - file = stub "file" - file.expects(:puts).with('path/to/save') - File.expects(:open).with(@paths_path, ::File::WRONLY|::File::CREAT|::File::APPEND).yields(file) + Puppet::FileBucketFile::File.new.send(:save_path_to_paths_file, @bucket) + end - Puppet::FileBucketFile::File.new.send(:save_path_to_paths_file, @bucket) - end + it "should append to a file if it exists" do + @bucket.expects(:path).returns('path/to/save').at_least_once + File.expects(:exist?).with(@paths_path).returns(true) + old_file = stub "file" + old_file.stubs(:readlines).returns [] + File.expects(:open).with(@paths_path).yields(old_file) - it "should not alter a file if it already contains the path" do - @bucket.expects(:path).returns('path/to/save').at_least_once - File.expects(:exist?).with(@paths_path).returns(true) - old_file = stub "file" - old_file.stubs(:readlines).returns ["path/to/save\n"] - File.expects(:open).with(@paths_path).yields(old_file) + file = stub "file" + file.expects(:puts).with('path/to/save') + File.expects(:open).with(@paths_path, ::File::WRONLY|::File::CREAT|::File::APPEND).yields(file) - Puppet::FileBucketFile::File.new.send(:save_path_to_paths_file, @bucket) - end + Puppet::FileBucketFile::File.new.send(:save_path_to_paths_file, @bucket) + end + + it "should not alter a file if it already contains the path" do + @bucket.expects(:path).returns('path/to/save').at_least_once + File.expects(:exist?).with(@paths_path).returns(true) + old_file = stub "file" + old_file.stubs(:readlines).returns ["path/to/save\n"] + File.expects(:open).with(@paths_path).yields(old_file) + + Puppet::FileBucketFile::File.new.send(:save_path_to_paths_file, @bucket) + end - it "should do nothing if there is no path" do - @bucket.expects(:path).returns(nil).at_least_once + it "should do nothing if there is no path" do + @bucket.expects(:path).returns(nil).at_least_once - Puppet::FileBucketFile::File.new.send(:save_path_to_paths_file, @bucket) - end + Puppet::FileBucketFile::File.new.send(:save_path_to_paths_file, @bucket) end + end end diff --git a/spec/unit/indirector/file_bucket_file/rest_spec.rb b/spec/unit/indirector/file_bucket_file/rest_spec.rb index 3aacd3ca4..960f60ae7 100755 --- a/spec/unit/indirector/file_bucket_file/rest_spec.rb +++ b/spec/unit/indirector/file_bucket_file/rest_spec.rb @@ -5,7 +5,7 @@ Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f require 'puppet/indirector/file_bucket_file/rest' describe Puppet::FileBucketFile::Rest do - it "should be a sublcass of Puppet::Indirector::REST" do - Puppet::FileBucketFile::Rest.superclass.should equal(Puppet::Indirector::REST) - end + it "should be a sublcass of Puppet::Indirector::REST" do + Puppet::FileBucketFile::Rest.superclass.should equal(Puppet::Indirector::REST) + end end diff --git a/spec/unit/indirector/file_content/file_server_spec.rb b/spec/unit/indirector/file_content/file_server_spec.rb index a09c7e2f7..326a8c863 100755 --- a/spec/unit/indirector/file_content/file_server_spec.rb +++ b/spec/unit/indirector/file_content/file_server_spec.rb @@ -8,11 +8,11 @@ require File.dirname(__FILE__) + '/../../../spec_helper' require 'puppet/indirector/file_content/file_server' describe Puppet::Indirector::FileContent::FileServer do - it "should be registered with the file_content indirection" do - Puppet::Indirector::Terminus.terminus_class(:file_content, :file_server).should equal(Puppet::Indirector::FileContent::FileServer) - end + it "should be registered with the file_content indirection" do + Puppet::Indirector::Terminus.terminus_class(:file_content, :file_server).should equal(Puppet::Indirector::FileContent::FileServer) + end - it "should be a subclass of the FileServer terminus" do - Puppet::Indirector::FileContent::FileServer.superclass.should equal(Puppet::Indirector::FileServer) - end + it "should be a subclass of the FileServer terminus" do + Puppet::Indirector::FileContent::FileServer.superclass.should equal(Puppet::Indirector::FileServer) + end end diff --git a/spec/unit/indirector/file_content/file_spec.rb b/spec/unit/indirector/file_content/file_spec.rb index 04656e0ed..62c28632e 100755 --- a/spec/unit/indirector/file_content/file_spec.rb +++ b/spec/unit/indirector/file_content/file_spec.rb @@ -8,11 +8,11 @@ require File.dirname(__FILE__) + '/../../../spec_helper' require 'puppet/indirector/file_content/file' describe Puppet::Indirector::FileContent::File do - it "should be registered with the file_content indirection" do - Puppet::Indirector::Terminus.terminus_class(:file_content, :file).should equal(Puppet::Indirector::FileContent::File) - end + it "should be registered with the file_content indirection" do + Puppet::Indirector::Terminus.terminus_class(:file_content, :file).should equal(Puppet::Indirector::FileContent::File) + end - it "should be a subclass of the DirectFileServer terminus" do - Puppet::Indirector::FileContent::File.superclass.should equal(Puppet::Indirector::DirectFileServer) - end + it "should be a subclass of the DirectFileServer terminus" do + Puppet::Indirector::FileContent::File.superclass.should equal(Puppet::Indirector::DirectFileServer) + end end diff --git a/spec/unit/indirector/file_content/rest_spec.rb b/spec/unit/indirector/file_content/rest_spec.rb index afb674e0a..3b2f43f13 100755 --- a/spec/unit/indirector/file_content/rest_spec.rb +++ b/spec/unit/indirector/file_content/rest_spec.rb @@ -5,7 +5,7 @@ require File.dirname(__FILE__) + '/../../../spec_helper' require 'puppet/indirector/file_content' describe "Puppet::Indirector::Content::Rest" do - it "should add the node's cert name to the arguments" + it "should add the node's cert name to the arguments" - it "should set the content type to text/plain" + it "should set the content type to text/plain" end diff --git a/spec/unit/indirector/file_metadata/file_server_spec.rb b/spec/unit/indirector/file_metadata/file_server_spec.rb index 40232a108..98f89c57d 100755 --- a/spec/unit/indirector/file_metadata/file_server_spec.rb +++ b/spec/unit/indirector/file_metadata/file_server_spec.rb @@ -8,11 +8,11 @@ require File.dirname(__FILE__) + '/../../../spec_helper' require 'puppet/indirector/file_metadata/file_server' describe Puppet::Indirector::FileMetadata::FileServer do - it "should be registered with the file_metadata indirection" do - Puppet::Indirector::Terminus.terminus_class(:file_metadata, :file_server).should equal(Puppet::Indirector::FileMetadata::FileServer) - end + it "should be registered with the file_metadata indirection" do + Puppet::Indirector::Terminus.terminus_class(:file_metadata, :file_server).should equal(Puppet::Indirector::FileMetadata::FileServer) + end - it "should be a subclass of the FileServer terminus" do - Puppet::Indirector::FileMetadata::FileServer.superclass.should equal(Puppet::Indirector::FileServer) - end + it "should be a subclass of the FileServer terminus" do + Puppet::Indirector::FileMetadata::FileServer.superclass.should equal(Puppet::Indirector::FileServer) + end end diff --git a/spec/unit/indirector/file_metadata/file_spec.rb b/spec/unit/indirector/file_metadata/file_spec.rb index a096d469d..906b24b3e 100755 --- a/spec/unit/indirector/file_metadata/file_spec.rb +++ b/spec/unit/indirector/file_metadata/file_spec.rb @@ -8,45 +8,45 @@ require File.dirname(__FILE__) + '/../../../spec_helper' require 'puppet/indirector/file_metadata/file' describe Puppet::Indirector::FileMetadata::File do - it "should be registered with the file_metadata indirection" do - Puppet::Indirector::Terminus.terminus_class(:file_metadata, :file).should equal(Puppet::Indirector::FileMetadata::File) + it "should be registered with the file_metadata indirection" do + Puppet::Indirector::Terminus.terminus_class(:file_metadata, :file).should equal(Puppet::Indirector::FileMetadata::File) + end + + it "should be a subclass of the DirectFileServer terminus" do + Puppet::Indirector::FileMetadata::File.superclass.should equal(Puppet::Indirector::DirectFileServer) + end + + describe "when creating the instance for a single found file" do + before do + @metadata = Puppet::Indirector::FileMetadata::File.new + @uri = "file:///my/local" + @data = mock 'metadata' + @data.stubs(:collect) + FileTest.expects(:exists?).with("/my/local").returns true + + @request = Puppet::Indirector::Request.new(:file_metadata, :find, @uri) end - it "should be a subclass of the DirectFileServer terminus" do - Puppet::Indirector::FileMetadata::File.superclass.should equal(Puppet::Indirector::DirectFileServer) - end - - describe "when creating the instance for a single found file" do - before do - @metadata = Puppet::Indirector::FileMetadata::File.new - @uri = "file:///my/local" - @data = mock 'metadata' - @data.stubs(:collect) - FileTest.expects(:exists?).with("/my/local").returns true - - @request = Puppet::Indirector::Request.new(:file_metadata, :find, @uri) - end + it "should collect its attributes when a file is found" do + @data.expects(:collect) - it "should collect its attributes when a file is found" do - @data.expects(:collect) - - Puppet::FileServing::Metadata.expects(:new).returns(@data) - @metadata.find(@request).should == @data - end + Puppet::FileServing::Metadata.expects(:new).returns(@data) + @metadata.find(@request).should == @data end + end - describe "when searching for multiple files" do - before do - @metadata = Puppet::Indirector::FileMetadata::File.new - @uri = "file:///my/local" + describe "when searching for multiple files" do + before do + @metadata = Puppet::Indirector::FileMetadata::File.new + @uri = "file:///my/local" - @request = Puppet::Indirector::Request.new(:file_metadata, :find, @uri) - end + @request = Puppet::Indirector::Request.new(:file_metadata, :find, @uri) + end - it "should collect the attributes of the instances returned" do - FileTest.expects(:exists?).with("/my/local").returns true - @metadata.expects(:path2instances).returns( [mock("one", :collect => nil), mock("two", :collect => nil)] ) - @metadata.search(@request) - end + it "should collect the attributes of the instances returned" do + FileTest.expects(:exists?).with("/my/local").returns true + @metadata.expects(:path2instances).returns( [mock("one", :collect => nil), mock("two", :collect => nil)] ) + @metadata.search(@request) end + end end diff --git a/spec/unit/indirector/file_metadata/rest_spec.rb b/spec/unit/indirector/file_metadata/rest_spec.rb index e93406f88..f6e998300 100755 --- a/spec/unit/indirector/file_metadata/rest_spec.rb +++ b/spec/unit/indirector/file_metadata/rest_spec.rb @@ -5,5 +5,5 @@ require File.dirname(__FILE__) + '/../../../spec_helper' require 'puppet/indirector/file_metadata' describe "Puppet::Indirector::Metadata::Rest" do - it "should add the node's cert name to the arguments" + it "should add the node's cert name to the arguments" end diff --git a/spec/unit/indirector/file_server_spec.rb b/spec/unit/indirector/file_server_spec.rb index 912695e27..686f79a24 100755 --- a/spec/unit/indirector/file_server_spec.rb +++ b/spec/unit/indirector/file_server_spec.rb @@ -10,258 +10,258 @@ require 'puppet/file_serving/configuration' describe Puppet::Indirector::FileServer do - before :each do - Puppet::Indirector::Terminus.stubs(:register_terminus_class) - @model = mock 'model' - @indirection = stub 'indirection', :name => :mystuff, :register_terminus_type => nil, :model => @model - Puppet::Indirector::Indirection.stubs(:instance).returns(@indirection) + before :each do + Puppet::Indirector::Terminus.stubs(:register_terminus_class) + @model = mock 'model' + @indirection = stub 'indirection', :name => :mystuff, :register_terminus_type => nil, :model => @model + Puppet::Indirector::Indirection.stubs(:instance).returns(@indirection) + + @file_server_class = Class.new(Puppet::Indirector::FileServer) do + def self.to_s + "Testing::Mytype" + end + end - @file_server_class = Class.new(Puppet::Indirector::FileServer) do - def self.to_s - "Testing::Mytype" - end - end + @file_server = @file_server_class.new - @file_server = @file_server_class.new + @uri = "puppet://host/my/local/file" + @configuration = mock 'configuration' + Puppet::FileServing::Configuration.stubs(:create).returns(@configuration) - @uri = "puppet://host/my/local/file" - @configuration = mock 'configuration' - Puppet::FileServing::Configuration.stubs(:create).returns(@configuration) + @request = Puppet::Indirector::Request.new(:myind, :mymethod, @uri, :environment => "myenv") + end - @request = Puppet::Indirector::Request.new(:myind, :mymethod, @uri, :environment => "myenv") + describe "when finding files" do + before do + @mount = stub 'mount', :find => nil + @instance = stub('instance', :links= => nil, :collect => nil) end - describe "when finding files" do - before do - @mount = stub 'mount', :find => nil - @instance = stub('instance', :links= => nil, :collect => nil) - end - - it "should use the configuration to find the mount and relative path" do - @configuration.expects(:split_path).with(@request) + it "should use the configuration to find the mount and relative path" do + @configuration.expects(:split_path).with(@request) - @file_server.find(@request) - end + @file_server.find(@request) + end - it "should return nil if it cannot find the mount" do - @configuration.expects(:split_path).with(@request).returns(nil, nil) + it "should return nil if it cannot find the mount" do + @configuration.expects(:split_path).with(@request).returns(nil, nil) - @file_server.find(@request).should be_nil - end + @file_server.find(@request).should be_nil + end - it "should use the mount to find the full path" do - @configuration.expects(:split_path).with(@request).returns([@mount, "rel/path"]) + it "should use the mount to find the full path" do + @configuration.expects(:split_path).with(@request).returns([@mount, "rel/path"]) - @mount.expects(:find).with { |key, request| key == "rel/path" } + @mount.expects(:find).with { |key, request| key == "rel/path" } - @file_server.find(@request) - end + @file_server.find(@request) + end - it "should pass the request when finding a file" do - @configuration.expects(:split_path).with(@request).returns([@mount, "rel/path"]) + it "should pass the request when finding a file" do + @configuration.expects(:split_path).with(@request).returns([@mount, "rel/path"]) - @mount.expects(:find).with { |key, request| request == @request } + @mount.expects(:find).with { |key, request| request == @request } - @file_server.find(@request) - end + @file_server.find(@request) + end - it "should return nil if it cannot find a full path" do - @configuration.expects(:split_path).with(@request).returns([@mount, "rel/path"]) + it "should return nil if it cannot find a full path" do + @configuration.expects(:split_path).with(@request).returns([@mount, "rel/path"]) - @mount.expects(:find).with { |key, request| key == "rel/path" }.returns nil + @mount.expects(:find).with { |key, request| key == "rel/path" }.returns nil - @file_server.find(@request).should be_nil - end + @file_server.find(@request).should be_nil + end - it "should create an instance with the found path" do - @configuration.expects(:split_path).with(@request).returns([@mount, "rel/path"]) + it "should create an instance with the found path" do + @configuration.expects(:split_path).with(@request).returns([@mount, "rel/path"]) - @mount.expects(:find).with { |key, request| key == "rel/path" }.returns "/my/file" + @mount.expects(:find).with { |key, request| key == "rel/path" }.returns "/my/file" - @model.expects(:new).with("/my/file").returns @instance + @model.expects(:new).with("/my/file").returns @instance - @file_server.find(@request).should equal(@instance) - end + @file_server.find(@request).should equal(@instance) + end - it "should set 'links' on the instance if it is set in the request options" do - @request.options[:links] = true - @configuration.expects(:split_path).with(@request).returns([@mount, "rel/path"]) + it "should set 'links' on the instance if it is set in the request options" do + @request.options[:links] = true + @configuration.expects(:split_path).with(@request).returns([@mount, "rel/path"]) - @mount.expects(:find).with { |key, request| key == "rel/path" }.returns "/my/file" + @mount.expects(:find).with { |key, request| key == "rel/path" }.returns "/my/file" - @model.expects(:new).with("/my/file").returns @instance + @model.expects(:new).with("/my/file").returns @instance - @instance.expects(:links=).with(true) + @instance.expects(:links=).with(true) - @file_server.find(@request).should equal(@instance) - end + @file_server.find(@request).should equal(@instance) + end - it "should collect the instance" do - @request.options[:links] = true - @configuration.expects(:split_path).with(@request).returns([@mount, "rel/path"]) + it "should collect the instance" do + @request.options[:links] = true + @configuration.expects(:split_path).with(@request).returns([@mount, "rel/path"]) - @mount.expects(:find).with { |key, request| key == "rel/path" }.returns "/my/file" + @mount.expects(:find).with { |key, request| key == "rel/path" }.returns "/my/file" - @model.expects(:new).with("/my/file").returns @instance + @model.expects(:new).with("/my/file").returns @instance - @instance.expects(:collect) + @instance.expects(:collect) - @file_server.find(@request).should equal(@instance) - end + @file_server.find(@request).should equal(@instance) end + end - describe "when searching for instances" do - before do - @mount = stub 'mount', :search => nil - @instance = stub('instance', :links= => nil, :collect => nil) - end + describe "when searching for instances" do + before do + @mount = stub 'mount', :search => nil + @instance = stub('instance', :links= => nil, :collect => nil) + end - it "should use the configuration to search the mount and relative path" do - @configuration.expects(:split_path).with(@request) + it "should use the configuration to search the mount and relative path" do + @configuration.expects(:split_path).with(@request) - @file_server.search(@request) - end + @file_server.search(@request) + end - it "should return nil if it cannot search the mount" do - @configuration.expects(:split_path).with(@request).returns(nil, nil) + it "should return nil if it cannot search the mount" do + @configuration.expects(:split_path).with(@request).returns(nil, nil) - @file_server.search(@request).should be_nil - end + @file_server.search(@request).should be_nil + end - it "should use the mount to search for the full paths" do - @configuration.expects(:split_path).with(@request).returns([@mount, "rel/path"]) + it "should use the mount to search for the full paths" do + @configuration.expects(:split_path).with(@request).returns([@mount, "rel/path"]) - @mount.expects(:search).with { |key, request| key == "rel/path" } + @mount.expects(:search).with { |key, request| key == "rel/path" } - @file_server.search(@request) - end + @file_server.search(@request) + end - it "should pass the request" do - @configuration.stubs(:split_path).returns([@mount, "rel/path"]) + it "should pass the request" do + @configuration.stubs(:split_path).returns([@mount, "rel/path"]) - @mount.expects(:search).with { |key, request| request == @request } + @mount.expects(:search).with { |key, request| request == @request } - @file_server.search(@request) - end + @file_server.search(@request) + end - it "should return nil if searching does not find any full paths" do - @configuration.expects(:split_path).with(@request).returns([@mount, "rel/path"]) + it "should return nil if searching does not find any full paths" do + @configuration.expects(:split_path).with(@request).returns([@mount, "rel/path"]) - @mount.expects(:search).with { |key, request| key == "rel/path" }.returns nil + @mount.expects(:search).with { |key, request| key == "rel/path" }.returns nil - @file_server.search(@request).should be_nil - end + @file_server.search(@request).should be_nil + end - it "should create a fileset with each returned path and merge them" do - @configuration.expects(:split_path).with(@request).returns([@mount, "rel/path"]) + it "should create a fileset with each returned path and merge them" do + @configuration.expects(:split_path).with(@request).returns([@mount, "rel/path"]) - @mount.expects(:search).with { |key, request| key == "rel/path" }.returns %w{/one /two} + @mount.expects(:search).with { |key, request| key == "rel/path" }.returns %w{/one /two} - FileTest.stubs(:exist?).returns true + FileTest.stubs(:exist?).returns true - one = mock 'fileset_one' - Puppet::FileServing::Fileset.expects(:new).with("/one", @request).returns(one) - two = mock 'fileset_two' - Puppet::FileServing::Fileset.expects(:new).with("/two", @request).returns(two) + one = mock 'fileset_one' + Puppet::FileServing::Fileset.expects(:new).with("/one", @request).returns(one) + two = mock 'fileset_two' + Puppet::FileServing::Fileset.expects(:new).with("/two", @request).returns(two) - Puppet::FileServing::Fileset.expects(:merge).with(one, two).returns [] + Puppet::FileServing::Fileset.expects(:merge).with(one, two).returns [] - @file_server.search(@request) - end + @file_server.search(@request) + end - it "should create an instance with each path resulting from the merger of the filesets" do - @configuration.expects(:split_path).with(@request).returns([@mount, "rel/path"]) + it "should create an instance with each path resulting from the merger of the filesets" do + @configuration.expects(:split_path).with(@request).returns([@mount, "rel/path"]) - @mount.expects(:search).with { |key, request| key == "rel/path" }.returns [] + @mount.expects(:search).with { |key, request| key == "rel/path" }.returns [] - FileTest.stubs(:exist?).returns true + FileTest.stubs(:exist?).returns true - Puppet::FileServing::Fileset.expects(:merge).returns("one" => "/one", "two" => "/two") + Puppet::FileServing::Fileset.expects(:merge).returns("one" => "/one", "two" => "/two") - one = stub 'one', :collect => nil - @model.expects(:new).with("/one", :relative_path => "one").returns one + one = stub 'one', :collect => nil + @model.expects(:new).with("/one", :relative_path => "one").returns one - two = stub 'two', :collect => nil - @model.expects(:new).with("/two", :relative_path => "two").returns two + two = stub 'two', :collect => nil + @model.expects(:new).with("/two", :relative_path => "two").returns two - # order can't be guaranteed - result = @file_server.search(@request) - result.should be_include(one) - result.should be_include(two) - result.length.should == 2 - end + # order can't be guaranteed + result = @file_server.search(@request) + result.should be_include(one) + result.should be_include(two) + result.length.should == 2 + end - it "should set 'links' on the instances if it is set in the request options" do - @configuration.expects(:split_path).with(@request).returns([@mount, "rel/path"]) + it "should set 'links' on the instances if it is set in the request options" do + @configuration.expects(:split_path).with(@request).returns([@mount, "rel/path"]) - @mount.expects(:search).with { |key, request| key == "rel/path" }.returns [] + @mount.expects(:search).with { |key, request| key == "rel/path" }.returns [] - FileTest.stubs(:exist?).returns true + FileTest.stubs(:exist?).returns true - Puppet::FileServing::Fileset.expects(:merge).returns("one" => "/one") + Puppet::FileServing::Fileset.expects(:merge).returns("one" => "/one") - one = stub 'one', :collect => nil - @model.expects(:new).with("/one", :relative_path => "one").returns one - one.expects(:links=).with true + one = stub 'one', :collect => nil + @model.expects(:new).with("/one", :relative_path => "one").returns one + one.expects(:links=).with true - @request.options[:links] = true + @request.options[:links] = true - @file_server.search(@request) - end + @file_server.search(@request) + end - it "should collect the instances" do - @configuration.expects(:split_path).with(@request).returns([@mount, "rel/path"]) + it "should collect the instances" do + @configuration.expects(:split_path).with(@request).returns([@mount, "rel/path"]) - @mount.expects(:search).with { |key, options| key == "rel/path" }.returns [] + @mount.expects(:search).with { |key, options| key == "rel/path" }.returns [] - FileTest.stubs(:exist?).returns true + FileTest.stubs(:exist?).returns true - Puppet::FileServing::Fileset.expects(:merge).returns("one" => "/one") + Puppet::FileServing::Fileset.expects(:merge).returns("one" => "/one") - one = mock 'one' - @model.expects(:new).with("/one", :relative_path => "one").returns one - one.expects(:collect) + one = mock 'one' + @model.expects(:new).with("/one", :relative_path => "one").returns one + one.expects(:collect) - @file_server.search(@request) - end + @file_server.search(@request) end + end - describe "when checking authorization" do - before do - @request.method = :find - end + describe "when checking authorization" do + before do + @request.method = :find + end - it "should return false when destroying" do - @request.method = :destroy - @file_server.should_not be_authorized(@request) - end + it "should return false when destroying" do + @request.method = :destroy + @file_server.should_not be_authorized(@request) + end - it "should return false when saving" do - @request.method = :save - @file_server.should_not be_authorized(@request) - end + it "should return false when saving" do + @request.method = :save + @file_server.should_not be_authorized(@request) + end - it "should use the configuration to find the mount and relative path" do - @configuration.expects(:split_path).with(@request) + it "should use the configuration to find the mount and relative path" do + @configuration.expects(:split_path).with(@request) - @file_server.authorized?(@request) - end + @file_server.authorized?(@request) + end - it "should return false if it cannot find the mount" do - @configuration.expects(:split_path).with(@request).returns(nil, nil) + it "should return false if it cannot find the mount" do + @configuration.expects(:split_path).with(@request).returns(nil, nil) - @file_server.should_not be_authorized(@request) - end + @file_server.should_not be_authorized(@request) + end - it "should return the results of asking the mount whether the node and IP are authorized" do - @mount = stub 'mount' - @configuration.expects(:split_path).with(@request).returns([@mount, "rel/path"]) + it "should return the results of asking the mount whether the node and IP are authorized" do + @mount = stub 'mount' + @configuration.expects(:split_path).with(@request).returns([@mount, "rel/path"]) - @request.stubs(:node).returns("mynode") - @request.stubs(:ip).returns("myip") - @mount.expects(:allowed?).with("mynode", "myip").returns "something" + @request.stubs(:node).returns("mynode") + @request.stubs(:ip).returns("myip") + @mount.expects(:allowed?).with("mynode", "myip").returns "something" - @file_server.authorized?(@request).should == "something" - end + @file_server.authorized?(@request).should == "something" end + end end diff --git a/spec/unit/indirector/file_spec.rb b/spec/unit/indirector/file_spec.rb index 2505a0cd5..86673f0e2 100755 --- a/spec/unit/indirector/file_spec.rb +++ b/spec/unit/indirector/file_spec.rb @@ -5,177 +5,177 @@ require 'puppet/indirector/file' describe Puppet::Indirector::File do - before :each do - Puppet::Indirector::Terminus.stubs(:register_terminus_class) - @model = mock 'model' - @indirection = stub 'indirection', :name => :mystuff, :register_terminus_type => nil, :model => @model - Puppet::Indirector::Indirection.stubs(:instance).returns(@indirection) + before :each do + Puppet::Indirector::Terminus.stubs(:register_terminus_class) + @model = mock 'model' + @indirection = stub 'indirection', :name => :mystuff, :register_terminus_type => nil, :model => @model + Puppet::Indirector::Indirection.stubs(:instance).returns(@indirection) + + @file_class = Class.new(Puppet::Indirector::File) do + def self.to_s + "Testing::Mytype" + end + end - @file_class = Class.new(Puppet::Indirector::File) do - def self.to_s - "Testing::Mytype" - end - end + @searcher = @file_class.new - @searcher = @file_class.new + @path = "/my/file" + @dir = "/my" - @path = "/my/file" - @dir = "/my" + @request = stub 'request', :key => @path + end - @request = stub 'request', :key => @path + describe "when finding files" do + it "should provide a method to return file contents at a specified path" do + @searcher.should respond_to(:find) end - describe "when finding files" do - it "should provide a method to return file contents at a specified path" do - @searcher.should respond_to(:find) - end - - it "should use the server data directory plus the indirection name if the run_mode is master" do - Puppet.run_mode.expects(:master?).returns true - Puppet.settings.expects(:value).with(:server_datadir).returns "/my/dir" + it "should use the server data directory plus the indirection name if the run_mode is master" do + Puppet.run_mode.expects(:master?).returns true + Puppet.settings.expects(:value).with(:server_datadir).returns "/my/dir" - @searcher.data_directory.should == File.join("/my/dir", "mystuff") - end + @searcher.data_directory.should == File.join("/my/dir", "mystuff") + end - it "should use the client data directory plus the indirection name if the run_mode is not master" do - Puppet.run_mode.expects(:master?).returns false - Puppet.settings.expects(:value).with(:client_datadir).returns "/my/dir" + it "should use the client data directory plus the indirection name if the run_mode is not master" do + Puppet.run_mode.expects(:master?).returns false + Puppet.settings.expects(:value).with(:client_datadir).returns "/my/dir" - @searcher.data_directory.should == File.join("/my/dir", "mystuff") - end + @searcher.data_directory.should == File.join("/my/dir", "mystuff") + end - it "should use the newest file in the data directory matching the indirection key without extension" do - @searcher.expects(:data_directory).returns "/data/dir" - @request.stubs(:key).returns "foo" - Dir.expects(:glob).with("/data/dir/foo.*").returns %w{/data1.stuff /data2.stuff} + it "should use the newest file in the data directory matching the indirection key without extension" do + @searcher.expects(:data_directory).returns "/data/dir" + @request.stubs(:key).returns "foo" + Dir.expects(:glob).with("/data/dir/foo.*").returns %w{/data1.stuff /data2.stuff} - stat1 = stub 'data1', :mtime => (Time.now - 5) - stat2 = stub 'data2', :mtime => Time.now - File.expects(:stat).with("/data1.stuff").returns stat1 - File.expects(:stat).with("/data2.stuff").returns stat2 + stat1 = stub 'data1', :mtime => (Time.now - 5) + stat2 = stub 'data2', :mtime => Time.now + File.expects(:stat).with("/data1.stuff").returns stat1 + File.expects(:stat).with("/data2.stuff").returns stat2 - @searcher.latest_path(@request).should == "/data2.stuff" - end + @searcher.latest_path(@request).should == "/data2.stuff" + end - it "should return nil when no files are found" do - @searcher.stubs(:latest_path).returns nil + it "should return nil when no files are found" do + @searcher.stubs(:latest_path).returns nil - @searcher.find(@request).should be_nil - end + @searcher.find(@request).should be_nil + end - it "should determine the file format from the file extension" do - @searcher.file_format("/data2.pson").should == "pson" - end + it "should determine the file format from the file extension" do + @searcher.file_format("/data2.pson").should == "pson" + end - it "should fail if the model does not support the file format" do - @searcher.stubs(:latest_path).returns "/my/file.pson" + it "should fail if the model does not support the file format" do + @searcher.stubs(:latest_path).returns "/my/file.pson" - @model.expects(:support_format?).with("pson").returns false + @model.expects(:support_format?).with("pson").returns false - lambda { @searcher.find(@request) }.should raise_error(ArgumentError) - end + lambda { @searcher.find(@request) }.should raise_error(ArgumentError) end + end - describe "when saving files" do - before do - @content = "my content" - @file = stub 'file', :content => @content, :path => @path, :name => @path, :render => "mydata" - @request.stubs(:instance).returns @file - end + describe "when saving files" do + before do + @content = "my content" + @file = stub 'file', :content => @content, :path => @path, :name => @path, :render => "mydata" + @request.stubs(:instance).returns @file + end - it "should provide a method to save file contents at a specified path" do - @searcher.should respond_to(:save) - end + it "should provide a method to save file contents at a specified path" do + @searcher.should respond_to(:save) + end - it "should choose the file extension based on the default format of the model" do - @model.expects(:default_format).returns "pson" + it "should choose the file extension based on the default format of the model" do + @model.expects(:default_format).returns "pson" - @searcher.serialization_format.should == "pson" - end + @searcher.serialization_format.should == "pson" + end - it "should place the file in the data directory, named after the indirection, key, and format" do - @searcher.stubs(:data_directory).returns "/my/dir" - @searcher.stubs(:serialization_format).returns "pson" + it "should place the file in the data directory, named after the indirection, key, and format" do + @searcher.stubs(:data_directory).returns "/my/dir" + @searcher.stubs(:serialization_format).returns "pson" - @request.stubs(:key).returns "foo" - @searcher.file_path(@request).should == File.join("/my/dir", "foo.pson") - end + @request.stubs(:key).returns "foo" + @searcher.file_path(@request).should == File.join("/my/dir", "foo.pson") + end - it "should fail intelligently if the file's parent directory does not exist" do - @searcher.stubs(:file_path).returns "/my/dir/file.pson" - @searcher.stubs(:serialization_format).returns "pson" + it "should fail intelligently if the file's parent directory does not exist" do + @searcher.stubs(:file_path).returns "/my/dir/file.pson" + @searcher.stubs(:serialization_format).returns "pson" - @request.stubs(:key).returns "foo" - File.expects(:directory?).with(File.join("/my/dir")).returns(false) + @request.stubs(:key).returns "foo" + File.expects(:directory?).with(File.join("/my/dir")).returns(false) - proc { @searcher.save(@request) }.should raise_error(Puppet::Error) - end + proc { @searcher.save(@request) }.should raise_error(Puppet::Error) + end - it "should render the instance using the file format and print it to the file path" do - @searcher.stubs(:file_path).returns "/my/file.pson" - @searcher.stubs(:serialization_format).returns "pson" + it "should render the instance using the file format and print it to the file path" do + @searcher.stubs(:file_path).returns "/my/file.pson" + @searcher.stubs(:serialization_format).returns "pson" - File.stubs(:directory?).returns true + File.stubs(:directory?).returns true - @request.instance.expects(:render).with("pson").returns "data" + @request.instance.expects(:render).with("pson").returns "data" - fh = mock 'filehandle' - File.expects(:open).with("/my/file.pson", "w").yields fh - fh.expects(:print).with("data") + fh = mock 'filehandle' + File.expects(:open).with("/my/file.pson", "w").yields fh + fh.expects(:print).with("data") - @searcher.save(@request) - end + @searcher.save(@request) + end - it "should fail intelligently if a file cannot be written" do - filehandle = mock 'file' - File.stubs(:directory?).returns(true) - File.stubs(:open).yields(filehandle) - filehandle.expects(:print).raises(ArgumentError) + it "should fail intelligently if a file cannot be written" do + filehandle = mock 'file' + File.stubs(:directory?).returns(true) + File.stubs(:open).yields(filehandle) + filehandle.expects(:print).raises(ArgumentError) - @searcher.stubs(:file_path).returns "/my/file.pson" - @model.stubs(:default_format).returns "pson" + @searcher.stubs(:file_path).returns "/my/file.pson" + @model.stubs(:default_format).returns "pson" - @instance.stubs(:render).returns "stuff" + @instance.stubs(:render).returns "stuff" - proc { @searcher.save(@request) }.should raise_error(Puppet::Error) - end + proc { @searcher.save(@request) }.should raise_error(Puppet::Error) end + end - describe "when removing files" do - it "should provide a method to remove files" do - @searcher.should respond_to(:destroy) - end + describe "when removing files" do + it "should provide a method to remove files" do + @searcher.should respond_to(:destroy) + end - it "should remove files in all formats found in the data directory that match the request key" do - @searcher.stubs(:data_directory).returns "/my/dir" - @request.stubs(:key).returns "me" + it "should remove files in all formats found in the data directory that match the request key" do + @searcher.stubs(:data_directory).returns "/my/dir" + @request.stubs(:key).returns "me" - Dir.expects(:glob).with(File.join("/my/dir", "me.*")).returns %w{/one /two} + Dir.expects(:glob).with(File.join("/my/dir", "me.*")).returns %w{/one /two} - File.expects(:unlink).with("/one") - File.expects(:unlink).with("/two") + File.expects(:unlink).with("/one") + File.expects(:unlink).with("/two") - @searcher.destroy(@request) - end + @searcher.destroy(@request) + end - it "should throw an exception if no file is found" do - @searcher.stubs(:data_directory).returns "/my/dir" - @request.stubs(:key).returns "me" + it "should throw an exception if no file is found" do + @searcher.stubs(:data_directory).returns "/my/dir" + @request.stubs(:key).returns "me" - Dir.expects(:glob).with(File.join("/my/dir", "me.*")).returns [] + Dir.expects(:glob).with(File.join("/my/dir", "me.*")).returns [] - proc { @searcher.destroy(@request) }.should raise_error(Puppet::Error) - end + proc { @searcher.destroy(@request) }.should raise_error(Puppet::Error) + end - it "should fail intelligently if a file cannot be removed" do - @searcher.stubs(:data_directory).returns "/my/dir" - @request.stubs(:key).returns "me" + it "should fail intelligently if a file cannot be removed" do + @searcher.stubs(:data_directory).returns "/my/dir" + @request.stubs(:key).returns "me" - Dir.expects(:glob).with(File.join("/my/dir", "me.*")).returns %w{/one} + Dir.expects(:glob).with(File.join("/my/dir", "me.*")).returns %w{/one} - File.expects(:unlink).with("/one").raises ArgumentError + File.expects(:unlink).with("/one").raises ArgumentError - proc { @searcher.destroy(@request) }.should raise_error(Puppet::Error) - end + proc { @searcher.destroy(@request) }.should raise_error(Puppet::Error) end + end end diff --git a/spec/unit/indirector/indirection_spec.rb b/spec/unit/indirector/indirection_spec.rb index 8bcd9cd54..b0e0f019c 100755 --- a/spec/unit/indirector/indirection_spec.rb +++ b/spec/unit/indirector/indirection_spec.rb @@ -5,791 +5,791 @@ require File.dirname(__FILE__) + '/../../spec_helper' require 'puppet/indirector/indirection' describe "Indirection Delegator", :shared => true do - it "should create a request object with the appropriate method name and all of the passed arguments" do - request = Puppet::Indirector::Request.new(:indirection, :find, "me") + it "should create a request object with the appropriate method name and all of the passed arguments" do + request = Puppet::Indirector::Request.new(:indirection, :find, "me") - @indirection.expects(:request).with(@method, "mystuff", :one => :two).returns request + @indirection.expects(:request).with(@method, "mystuff", :one => :two).returns request - @terminus.stubs(@method) + @terminus.stubs(@method) - @indirection.send(@method, "mystuff", :one => :two) + @indirection.send(@method, "mystuff", :one => :two) + end + + it "should let the :select_terminus method choose the terminus using the created request if the :select_terminus method is available" do + # Define the method, so our respond_to? hook matches. + class << @indirection + def select_terminus(request) + end end - it "should let the :select_terminus method choose the terminus using the created request if the :select_terminus method is available" do - # Define the method, so our respond_to? hook matches. - class << @indirection - def select_terminus(request) - end - end + request = Puppet::Indirector::Request.new(:indirection, :find, "me") - request = Puppet::Indirector::Request.new(:indirection, :find, "me") + @indirection.stubs(:request).returns request - @indirection.stubs(:request).returns request + @indirection.expects(:select_terminus).with(request).returns :test_terminus - @indirection.expects(:select_terminus).with(request).returns :test_terminus + @indirection.stubs(:check_authorization) + @terminus.expects(@method) - @indirection.stubs(:check_authorization) - @terminus.expects(@method) + @indirection.send(@method, "me") + end - @indirection.send(@method, "me") + it "should fail if the :select_terminus hook does not return a terminus name" do + # Define the method, so our respond_to? hook matches. + class << @indirection + def select_terminus(request) + end end - it "should fail if the :select_terminus hook does not return a terminus name" do - # Define the method, so our respond_to? hook matches. - class << @indirection - def select_terminus(request) - end - end - - request = stub 'request', :key => "me", :options => {} + request = stub 'request', :key => "me", :options => {} - @indirection.stubs(:request).returns request + @indirection.stubs(:request).returns request - @indirection.expects(:select_terminus).with(request).returns nil + @indirection.expects(:select_terminus).with(request).returns nil - lambda { @indirection.send(@method, "me") }.should raise_error(ArgumentError) - end + lambda { @indirection.send(@method, "me") }.should raise_error(ArgumentError) + end - it "should choose the terminus returned by the :terminus_class method if no :select_terminus method is available" do - @indirection.expects(:terminus_class).returns :test_terminus + it "should choose the terminus returned by the :terminus_class method if no :select_terminus method is available" do + @indirection.expects(:terminus_class).returns :test_terminus - @terminus.expects(@method) + @terminus.expects(@method) - @indirection.send(@method, "me") - end + @indirection.send(@method, "me") + end - it "should let the appropriate terminus perform the lookup" do - @terminus.expects(@method).with { |r| r.is_a?(Puppet::Indirector::Request) } - @indirection.send(@method, "me") - end + it "should let the appropriate terminus perform the lookup" do + @terminus.expects(@method).with { |r| r.is_a?(Puppet::Indirector::Request) } + @indirection.send(@method, "me") + end end describe "Delegation Authorizer", :shared => true do - before do - # So the :respond_to? turns out correctly. - class << @terminus - def authorized? - end - end + before do + # So the :respond_to? turns out correctly. + class << @terminus + def authorized? + end end + end + + it "should not check authorization if a node name is not provided" do + @terminus.expects(:authorized?).never + @terminus.stubs(@method) + + # The quotes are necessary here, else it looks like a block. + @request.stubs(:options).returns({}) + @indirection.send(@method, "/my/key") + end + + it "should pass the request to the terminus's authorization method" do + @terminus.expects(:authorized?).with { |r| r.is_a?(Puppet::Indirector::Request) }.returns(true) + @terminus.stubs(@method) + + @indirection.send(@method, "/my/key", :node => "mynode") + end + + it "should fail if authorization returns false" do + @terminus.expects(:authorized?).returns(false) + @terminus.stubs(@method) + proc { @indirection.send(@method, "/my/key", :node => "mynode") }.should raise_error(ArgumentError) + end + + it "should continue if authorization returns true" do + @terminus.expects(:authorized?).returns(true) + @terminus.stubs(@method) + @indirection.send(@method, "/my/key", :node => "mynode") + end +end - it "should not check authorization if a node name is not provided" do - @terminus.expects(:authorized?).never - @terminus.stubs(@method) - - # The quotes are necessary here, else it looks like a block. - @request.stubs(:options).returns({}) - @indirection.send(@method, "/my/key") +describe Puppet::Indirector::Indirection do + after do + Puppet::Util::Cacher.expire + end + describe "when initializing" do + # (LAK) I've no idea how to test this, really. + it "should store a reference to itself before it consumes its options" do + proc { @indirection = Puppet::Indirector::Indirection.new(Object.new, :testingness, :not_valid_option) }.should raise_error + Puppet::Indirector::Indirection.instance(:testingness).should be_instance_of(Puppet::Indirector::Indirection) + Puppet::Indirector::Indirection.instance(:testingness).delete end - it "should pass the request to the terminus's authorization method" do - @terminus.expects(:authorized?).with { |r| r.is_a?(Puppet::Indirector::Request) }.returns(true) - @terminus.stubs(@method) + it "should keep a reference to the indirecting model" do + model = mock 'model' + @indirection = Puppet::Indirector::Indirection.new(model, :myind) + @indirection.model.should equal(model) + end - @indirection.send(@method, "/my/key", :node => "mynode") + it "should set the name" do + @indirection = Puppet::Indirector::Indirection.new(mock('model'), :myind) + @indirection.name.should == :myind end - it "should fail if authorization returns false" do - @terminus.expects(:authorized?).returns(false) - @terminus.stubs(@method) - proc { @indirection.send(@method, "/my/key", :node => "mynode") }.should raise_error(ArgumentError) + it "should require indirections to have unique names" do + @indirection = Puppet::Indirector::Indirection.new(mock('model'), :test) + proc { Puppet::Indirector::Indirection.new(:test) }.should raise_error(ArgumentError) end - it "should continue if authorization returns true" do - @terminus.expects(:authorized?).returns(true) - @terminus.stubs(@method) - @indirection.send(@method, "/my/key", :node => "mynode") + it "should extend itself with any specified module" do + mod = Module.new + @indirection = Puppet::Indirector::Indirection.new(mock('model'), :test, :extend => mod) + @indirection.singleton_class.included_modules.should include(mod) end -end -describe Puppet::Indirector::Indirection do after do - Puppet::Util::Cacher.expire - end - describe "when initializing" do - # (LAK) I've no idea how to test this, really. - it "should store a reference to itself before it consumes its options" do - proc { @indirection = Puppet::Indirector::Indirection.new(Object.new, :testingness, :not_valid_option) }.should raise_error - Puppet::Indirector::Indirection.instance(:testingness).should be_instance_of(Puppet::Indirector::Indirection) - Puppet::Indirector::Indirection.instance(:testingness).delete - end - - it "should keep a reference to the indirecting model" do - model = mock 'model' - @indirection = Puppet::Indirector::Indirection.new(model, :myind) - @indirection.model.should equal(model) - end - - it "should set the name" do - @indirection = Puppet::Indirector::Indirection.new(mock('model'), :myind) - @indirection.name.should == :myind - end - - it "should require indirections to have unique names" do - @indirection = Puppet::Indirector::Indirection.new(mock('model'), :test) - proc { Puppet::Indirector::Indirection.new(:test) }.should raise_error(ArgumentError) - end - - it "should extend itself with any specified module" do - mod = Module.new - @indirection = Puppet::Indirector::Indirection.new(mock('model'), :test, :extend => mod) - @indirection.singleton_class.included_modules.should include(mod) - end - - after do - @indirection.delete if defined?(@indirection) - end + @indirection.delete if defined?(@indirection) + end + end + + describe "when an instance" do + before :each do + @terminus_class = mock 'terminus_class' + @terminus = mock 'terminus' + @terminus_class.stubs(:new).returns(@terminus) + @cache = stub 'cache', :name => "mycache" + @cache_class = mock 'cache_class' + Puppet::Indirector::Terminus.stubs(:terminus_class).with(:test, :cache_terminus).returns(@cache_class) + Puppet::Indirector::Terminus.stubs(:terminus_class).with(:test, :test_terminus).returns(@terminus_class) + + @indirection = Puppet::Indirector::Indirection.new(mock('model'), :test) + @indirection.terminus_class = :test_terminus + + @instance = stub 'instance', :expiration => nil, :expiration= => nil, :name => "whatever" + @name = :mything + + #@request = stub 'instance', :key => "/my/key", :instance => @instance, :options => {} + @request = mock 'instance' end - describe "when an instance" do - before :each do - @terminus_class = mock 'terminus_class' - @terminus = mock 'terminus' - @terminus_class.stubs(:new).returns(@terminus) - @cache = stub 'cache', :name => "mycache" - @cache_class = mock 'cache_class' - Puppet::Indirector::Terminus.stubs(:terminus_class).with(:test, :cache_terminus).returns(@cache_class) - Puppet::Indirector::Terminus.stubs(:terminus_class).with(:test, :test_terminus).returns(@terminus_class) - - @indirection = Puppet::Indirector::Indirection.new(mock('model'), :test) - @indirection.terminus_class = :test_terminus - - @instance = stub 'instance', :expiration => nil, :expiration= => nil, :name => "whatever" - @name = :mything - - #@request = stub 'instance', :key => "/my/key", :instance => @instance, :options => {} - @request = mock 'instance' - end - - it "should allow setting the ttl" do - @indirection.ttl = 300 - @indirection.ttl.should == 300 - end + it "should allow setting the ttl" do + @indirection.ttl = 300 + @indirection.ttl.should == 300 + end - it "should default to the :runinterval setting, converted to an integer, for its ttl" do - Puppet.settings.expects(:value).returns "1800" - @indirection.ttl.should == 1800 - end + it "should default to the :runinterval setting, converted to an integer, for its ttl" do + Puppet.settings.expects(:value).returns "1800" + @indirection.ttl.should == 1800 + end - it "should calculate the current expiration by adding the TTL to the current time" do - @indirection.stubs(:ttl).returns(100) - now = Time.now - Time.stubs(:now).returns now - @indirection.expiration.should == (Time.now + 100) - end + it "should calculate the current expiration by adding the TTL to the current time" do + @indirection.stubs(:ttl).returns(100) + now = Time.now + Time.stubs(:now).returns now + @indirection.expiration.should == (Time.now + 100) + end - it "should have a method for creating an indirection request instance" do - @indirection.should respond_to(:request) - end + it "should have a method for creating an indirection request instance" do + @indirection.should respond_to(:request) + end - describe "creates a request" do - it "should create it with its name as the request's indirection name" do - Puppet::Indirector::Request.expects(:new).with { |name, *other| @indirection.name == name } - @indirection.request(:funtest, "yayness") - end - - it "should require a method and key" do - Puppet::Indirector::Request.expects(:new).with { |name, method, key, *other| method == :funtest and key == "yayness" } - @indirection.request(:funtest, "yayness") - end - - it "should support optional arguments" do - Puppet::Indirector::Request.expects(:new).with { |name, method, key, other| other == {:one => :two} } - @indirection.request(:funtest, "yayness", :one => :two) - end - - it "should not pass options if none are supplied" do - Puppet::Indirector::Request.expects(:new).with { |*args| args.length < 4 } - @indirection.request(:funtest, "yayness") - end - - it "should return the request" do - request = mock 'request' - Puppet::Indirector::Request.expects(:new).returns request - @indirection.request(:funtest, "yayness").should equal(request) - end - end + describe "creates a request" do + it "should create it with its name as the request's indirection name" do + Puppet::Indirector::Request.expects(:new).with { |name, *other| @indirection.name == name } + @indirection.request(:funtest, "yayness") + end + + it "should require a method and key" do + Puppet::Indirector::Request.expects(:new).with { |name, method, key, *other| method == :funtest and key == "yayness" } + @indirection.request(:funtest, "yayness") + end + + it "should support optional arguments" do + Puppet::Indirector::Request.expects(:new).with { |name, method, key, other| other == {:one => :two} } + @indirection.request(:funtest, "yayness", :one => :two) + end + + it "should not pass options if none are supplied" do + Puppet::Indirector::Request.expects(:new).with { |*args| args.length < 4 } + @indirection.request(:funtest, "yayness") + end + + it "should return the request" do + request = mock 'request' + Puppet::Indirector::Request.expects(:new).returns request + @indirection.request(:funtest, "yayness").should equal(request) + end + end - describe "and looking for a model instance" do - before { @method = :find } + describe "and looking for a model instance" do + before { @method = :find } - it_should_behave_like "Indirection Delegator" - it_should_behave_like "Delegation Authorizer" + it_should_behave_like "Indirection Delegator" + it_should_behave_like "Delegation Authorizer" - it "should return the results of the delegation" do - @terminus.expects(:find).returns(@instance) - @indirection.find("me").should equal(@instance) - end + it "should return the results of the delegation" do + @terminus.expects(:find).returns(@instance) + @indirection.find("me").should equal(@instance) + end - it "should set the expiration date on any instances without one set" do - @terminus.stubs(:find).returns(@instance) + it "should set the expiration date on any instances without one set" do + @terminus.stubs(:find).returns(@instance) - @indirection.expects(:expiration).returns :yay + @indirection.expects(:expiration).returns :yay - @instance.expects(:expiration).returns(nil) - @instance.expects(:expiration=).with(:yay) + @instance.expects(:expiration).returns(nil) + @instance.expects(:expiration=).with(:yay) - @indirection.find("/my/key") - end + @indirection.find("/my/key") + end - it "should not override an already-set expiration date on returned instances" do - @terminus.stubs(:find).returns(@instance) + it "should not override an already-set expiration date on returned instances" do + @terminus.stubs(:find).returns(@instance) - @indirection.expects(:expiration).never + @indirection.expects(:expiration).never - @instance.expects(:expiration).returns(:yay) - @instance.expects(:expiration=).never + @instance.expects(:expiration).returns(:yay) + @instance.expects(:expiration=).never - @indirection.find("/my/key") - end + @indirection.find("/my/key") + end - it "should filter the result instance if the terminus supports it" do - @terminus.stubs(:find).returns(@instance) - @terminus.stubs(:respond_to?).with(:filter).returns(true) + it "should filter the result instance if the terminus supports it" do + @terminus.stubs(:find).returns(@instance) + @terminus.stubs(:respond_to?).with(:filter).returns(true) - @terminus.expects(:filter).with(@instance) + @terminus.expects(:filter).with(@instance) - @indirection.find("/my/key") - end - describe "when caching is enabled" do - before do - @indirection.cache_class = :cache_terminus - @cache_class.stubs(:new).returns(@cache) + @indirection.find("/my/key") + end + describe "when caching is enabled" do + before do + @indirection.cache_class = :cache_terminus + @cache_class.stubs(:new).returns(@cache) - @instance.stubs(:expired?).returns false - end + @instance.stubs(:expired?).returns false + end - it "should first look in the cache for an instance" do - @terminus.stubs(:find).never - @cache.expects(:find).returns @instance + it "should first look in the cache for an instance" do + @terminus.stubs(:find).never + @cache.expects(:find).returns @instance - @indirection.find("/my/key") - end + @indirection.find("/my/key") + end - it "should not look in the cache if the request specifies not to use the cache" do - @terminus.expects(:find).returns @instance - @cache.expects(:find).never - @cache.stubs(:save) + it "should not look in the cache if the request specifies not to use the cache" do + @terminus.expects(:find).returns @instance + @cache.expects(:find).never + @cache.stubs(:save) - @indirection.find("/my/key", :ignore_cache => true) - end + @indirection.find("/my/key", :ignore_cache => true) + end - it "should still save to the cache even if the cache is being ignored during readin" do - @terminus.expects(:find).returns @instance - @cache.expects(:save) + it "should still save to the cache even if the cache is being ignored during readin" do + @terminus.expects(:find).returns @instance + @cache.expects(:save) - @indirection.find("/my/key", :ignore_cache => true) - end + @indirection.find("/my/key", :ignore_cache => true) + end - it "should only look in the cache if the request specifies not to use the terminus" do - @terminus.expects(:find).never - @cache.expects(:find) + it "should only look in the cache if the request specifies not to use the terminus" do + @terminus.expects(:find).never + @cache.expects(:find) - @indirection.find("/my/key", :ignore_terminus => true) - end + @indirection.find("/my/key", :ignore_terminus => true) + end - it "should use a request to look in the cache for cached objects" do - @cache.expects(:find).with { |r| r.method == :find and r.key == "/my/key" }.returns @instance + it "should use a request to look in the cache for cached objects" do + @cache.expects(:find).with { |r| r.method == :find and r.key == "/my/key" }.returns @instance - @cache.stubs(:save) + @cache.stubs(:save) - @indirection.find("/my/key") - end + @indirection.find("/my/key") + end - it "should return the cached object if it is not expired" do - @instance.stubs(:expired?).returns false + it "should return the cached object if it is not expired" do + @instance.stubs(:expired?).returns false - @cache.stubs(:find).returns @instance - @indirection.find("/my/key").should equal(@instance) - end + @cache.stubs(:find).returns @instance + @indirection.find("/my/key").should equal(@instance) + end - it "should not fail if the cache fails" do - @terminus.stubs(:find).returns @instance + it "should not fail if the cache fails" do + @terminus.stubs(:find).returns @instance - @cache.expects(:find).raises ArgumentError - @cache.stubs(:save) - lambda { @indirection.find("/my/key") }.should_not raise_error - end + @cache.expects(:find).raises ArgumentError + @cache.stubs(:save) + lambda { @indirection.find("/my/key") }.should_not raise_error + end - it "should look in the main terminus if the cache fails" do - @terminus.expects(:find).returns @instance - @cache.expects(:find).raises ArgumentError - @cache.stubs(:save) - @indirection.find("/my/key").should equal(@instance) - end + it "should look in the main terminus if the cache fails" do + @terminus.expects(:find).returns @instance + @cache.expects(:find).raises ArgumentError + @cache.stubs(:save) + @indirection.find("/my/key").should equal(@instance) + end - it "should send a debug log if it is using the cached object" do - Puppet.expects(:debug) - @cache.stubs(:find).returns @instance + it "should send a debug log if it is using the cached object" do + Puppet.expects(:debug) + @cache.stubs(:find).returns @instance - @indirection.find("/my/key") - end + @indirection.find("/my/key") + end - it "should not return the cached object if it is expired" do - @instance.stubs(:expired?).returns true + it "should not return the cached object if it is expired" do + @instance.stubs(:expired?).returns true - @cache.stubs(:find).returns @instance - @terminus.stubs(:find).returns nil - @indirection.find("/my/key").should be_nil - end + @cache.stubs(:find).returns @instance + @terminus.stubs(:find).returns nil + @indirection.find("/my/key").should be_nil + end - it "should send an info log if it is using the cached object" do - Puppet.expects(:info) - @instance.stubs(:expired?).returns true + it "should send an info log if it is using the cached object" do + Puppet.expects(:info) + @instance.stubs(:expired?).returns true - @cache.stubs(:find).returns @instance - @terminus.stubs(:find).returns nil - @indirection.find("/my/key") - end + @cache.stubs(:find).returns @instance + @terminus.stubs(:find).returns nil + @indirection.find("/my/key") + end - it "should cache any objects not retrieved from the cache" do - @cache.expects(:find).returns nil + it "should cache any objects not retrieved from the cache" do + @cache.expects(:find).returns nil - @terminus.expects(:find).returns(@instance) - @cache.expects(:save) + @terminus.expects(:find).returns(@instance) + @cache.expects(:save) - @indirection.find("/my/key") - end + @indirection.find("/my/key") + end - it "should use a request to look in the cache for cached objects" do - @cache.expects(:find).with { |r| r.method == :find and r.key == "/my/key" }.returns nil + it "should use a request to look in the cache for cached objects" do + @cache.expects(:find).with { |r| r.method == :find and r.key == "/my/key" }.returns nil - @terminus.stubs(:find).returns(@instance) - @cache.stubs(:save) + @terminus.stubs(:find).returns(@instance) + @cache.stubs(:save) - @indirection.find("/my/key") - end + @indirection.find("/my/key") + end - it "should cache the instance using a request with the instance set to the cached object" do - @cache.stubs(:find).returns nil + it "should cache the instance using a request with the instance set to the cached object" do + @cache.stubs(:find).returns nil - @terminus.stubs(:find).returns(@instance) + @terminus.stubs(:find).returns(@instance) - @cache.expects(:save).with { |r| r.method == :save and r.instance == @instance } + @cache.expects(:save).with { |r| r.method == :save and r.instance == @instance } - @indirection.find("/my/key") - end + @indirection.find("/my/key") + end - it "should send an info log that the object is being cached" do - @cache.stubs(:find).returns nil + it "should send an info log that the object is being cached" do + @cache.stubs(:find).returns nil - @terminus.stubs(:find).returns(@instance) - @cache.stubs(:save) + @terminus.stubs(:find).returns(@instance) + @cache.stubs(:save) - Puppet.expects(:info) + Puppet.expects(:info) - @indirection.find("/my/key") - end - end + @indirection.find("/my/key") end + end + end - describe "and storing a model instance" do - before { @method = :save } + describe "and storing a model instance" do + before { @method = :save } - it_should_behave_like "Indirection Delegator" - it_should_behave_like "Delegation Authorizer" + it_should_behave_like "Indirection Delegator" + it_should_behave_like "Delegation Authorizer" - it "should return the result of the save" do - @terminus.stubs(:save).returns "foo" - @indirection.save(@instance).should == "foo" - end + it "should return the result of the save" do + @terminus.stubs(:save).returns "foo" + @indirection.save(@instance).should == "foo" + end - describe "when caching is enabled" do - before do - @indirection.cache_class = :cache_terminus - @cache_class.stubs(:new).returns(@cache) + describe "when caching is enabled" do + before do + @indirection.cache_class = :cache_terminus + @cache_class.stubs(:new).returns(@cache) - @instance.stubs(:expired?).returns false - end + @instance.stubs(:expired?).returns false + end - it "should return the result of saving to the terminus" do - request = stub 'request', :instance => @instance, :node => nil + it "should return the result of saving to the terminus" do + request = stub 'request', :instance => @instance, :node => nil - @indirection.expects(:request).returns request + @indirection.expects(:request).returns request - @cache.stubs(:save) - @terminus.stubs(:save).returns @instance - @indirection.save(@instance).should equal(@instance) - end + @cache.stubs(:save) + @terminus.stubs(:save).returns @instance + @indirection.save(@instance).should equal(@instance) + end - it "should use a request to save the object to the cache" do - request = stub 'request', :instance => @instance, :node => nil + it "should use a request to save the object to the cache" do + request = stub 'request', :instance => @instance, :node => nil - @indirection.expects(:request).returns request + @indirection.expects(:request).returns request - @cache.expects(:save).with(request) - @terminus.stubs(:save) - @indirection.save(@instance) - end + @cache.expects(:save).with(request) + @terminus.stubs(:save) + @indirection.save(@instance) + end - it "should not save to the cache if the normal save fails" do - request = stub 'request', :instance => @instance, :node => nil + it "should not save to the cache if the normal save fails" do + request = stub 'request', :instance => @instance, :node => nil - @indirection.expects(:request).returns request + @indirection.expects(:request).returns request - @cache.expects(:save).never - @terminus.expects(:save).raises "eh" - lambda { @indirection.save(@instance) }.should raise_error - end - end + @cache.expects(:save).never + @terminus.expects(:save).raises "eh" + lambda { @indirection.save(@instance) }.should raise_error end + end + end - describe "and removing a model instance" do - before { @method = :destroy } + describe "and removing a model instance" do + before { @method = :destroy } - it_should_behave_like "Indirection Delegator" - it_should_behave_like "Delegation Authorizer" + it_should_behave_like "Indirection Delegator" + it_should_behave_like "Delegation Authorizer" - it "should return the result of removing the instance" do - @terminus.stubs(:destroy).returns "yayness" - @indirection.destroy("/my/key").should == "yayness" - end + it "should return the result of removing the instance" do + @terminus.stubs(:destroy).returns "yayness" + @indirection.destroy("/my/key").should == "yayness" + end - describe "when caching is enabled" do - before do - @indirection.cache_class = :cache_terminus - @cache_class.expects(:new).returns(@cache) + describe "when caching is enabled" do + before do + @indirection.cache_class = :cache_terminus + @cache_class.expects(:new).returns(@cache) - @instance.stubs(:expired?).returns false - end + @instance.stubs(:expired?).returns false + end - it "should use a request instance to search in and remove objects from the cache" do - destroy = stub 'destroy_request', :key => "/my/key", :node => nil - find = stub 'destroy_request', :key => "/my/key", :node => nil + it "should use a request instance to search in and remove objects from the cache" do + destroy = stub 'destroy_request', :key => "/my/key", :node => nil + find = stub 'destroy_request', :key => "/my/key", :node => nil - @indirection.expects(:request).with(:destroy, "/my/key").returns destroy - @indirection.expects(:request).with(:find, "/my/key").returns find + @indirection.expects(:request).with(:destroy, "/my/key").returns destroy + @indirection.expects(:request).with(:find, "/my/key").returns find - cached = mock 'cache' + cached = mock 'cache' - @cache.expects(:find).with(find).returns cached - @cache.expects(:destroy).with(destroy) + @cache.expects(:find).with(find).returns cached + @cache.expects(:destroy).with(destroy) - @terminus.stubs(:destroy) + @terminus.stubs(:destroy) - @indirection.destroy("/my/key") - end - end + @indirection.destroy("/my/key") end + end + end - describe "and searching for multiple model instances" do - before { @method = :search } - - it_should_behave_like "Indirection Delegator" - it_should_behave_like "Delegation Authorizer" + describe "and searching for multiple model instances" do + before { @method = :search } - it "should set the expiration date on any instances without one set" do - @terminus.stubs(:search).returns([@instance]) + it_should_behave_like "Indirection Delegator" + it_should_behave_like "Delegation Authorizer" - @indirection.expects(:expiration).returns :yay + it "should set the expiration date on any instances without one set" do + @terminus.stubs(:search).returns([@instance]) - @instance.expects(:expiration).returns(nil) - @instance.expects(:expiration=).with(:yay) + @indirection.expects(:expiration).returns :yay - @indirection.search("/my/key") - end + @instance.expects(:expiration).returns(nil) + @instance.expects(:expiration=).with(:yay) - it "should not override an already-set expiration date on returned instances" do - @terminus.stubs(:search).returns([@instance]) + @indirection.search("/my/key") + end - @indirection.expects(:expiration).never + it "should not override an already-set expiration date on returned instances" do + @terminus.stubs(:search).returns([@instance]) - @instance.expects(:expiration).returns(:yay) - @instance.expects(:expiration=).never + @indirection.expects(:expiration).never - @indirection.search("/my/key") - end + @instance.expects(:expiration).returns(:yay) + @instance.expects(:expiration=).never - it "should return the results of searching in the terminus" do - @terminus.expects(:search).returns([@instance]) - @indirection.search("/my/key").should == [@instance] - end - end + @indirection.search("/my/key") + end - describe "and expiring a model instance" do - describe "when caching is not enabled" do - it "should do nothing" do - @cache_class.expects(:new).never + it "should return the results of searching in the terminus" do + @terminus.expects(:search).returns([@instance]) + @indirection.search("/my/key").should == [@instance] + end + end - @indirection.expire("/my/key") - end - end + describe "and expiring a model instance" do + describe "when caching is not enabled" do + it "should do nothing" do + @cache_class.expects(:new).never - describe "when caching is enabled" do - before do - @indirection.cache_class = :cache_terminus - @cache_class.expects(:new).returns(@cache) + @indirection.expire("/my/key") + end + end - @instance.stubs(:expired?).returns false + describe "when caching is enabled" do + before do + @indirection.cache_class = :cache_terminus + @cache_class.expects(:new).returns(@cache) - @cached = stub 'cached', :expiration= => nil, :name => "/my/key" - end + @instance.stubs(:expired?).returns false - it "should use a request to find within the cache" do - @cache.expects(:find).with { |r| r.is_a?(Puppet::Indirector::Request) and r.method == :find } - @indirection.expire("/my/key") - end + @cached = stub 'cached', :expiration= => nil, :name => "/my/key" + end - it "should do nothing if no such instance is cached" do - @cache.expects(:find).returns nil + it "should use a request to find within the cache" do + @cache.expects(:find).with { |r| r.is_a?(Puppet::Indirector::Request) and r.method == :find } + @indirection.expire("/my/key") + end - @indirection.expire("/my/key") - end + it "should do nothing if no such instance is cached" do + @cache.expects(:find).returns nil - it "should log when expiring a found instance" do - @cache.expects(:find).returns @cached - @cache.stubs(:save) + @indirection.expire("/my/key") + end - Puppet.expects(:info) + it "should log when expiring a found instance" do + @cache.expects(:find).returns @cached + @cache.stubs(:save) - @indirection.expire("/my/key") - end + Puppet.expects(:info) - it "should set the cached instance's expiration to a time in the past" do - @cache.expects(:find).returns @cached - @cache.stubs(:save) + @indirection.expire("/my/key") + end - @cached.expects(:expiration=).with { |t| t < Time.now } + it "should set the cached instance's expiration to a time in the past" do + @cache.expects(:find).returns @cached + @cache.stubs(:save) - @indirection.expire("/my/key") - end + @cached.expects(:expiration=).with { |t| t < Time.now } - it "should save the now expired instance back into the cache" do - @cache.expects(:find).returns @cached + @indirection.expire("/my/key") + end - @cached.expects(:expiration=).with { |t| t < Time.now } + it "should save the now expired instance back into the cache" do + @cache.expects(:find).returns @cached - @cache.expects(:save) + @cached.expects(:expiration=).with { |t| t < Time.now } - @indirection.expire("/my/key") - end + @cache.expects(:save) - it "should use a request to save the expired resource to the cache" do - @cache.expects(:find).returns @cached + @indirection.expire("/my/key") + end - @cached.expects(:expiration=).with { |t| t < Time.now } + it "should use a request to save the expired resource to the cache" do + @cache.expects(:find).returns @cached - @cache.expects(:save).with { |r| r.is_a?(Puppet::Indirector::Request) and r.instance == @cached and r.method == :save }.returns(@cached) + @cached.expects(:expiration=).with { |t| t < Time.now } - @indirection.expire("/my/key") - end - end - end + @cache.expects(:save).with { |r| r.is_a?(Puppet::Indirector::Request) and r.instance == @cached and r.method == :save }.returns(@cached) - after :each do - @indirection.delete - Puppet::Util::Cacher.expire + @indirection.expire("/my/key") end + end end + after :each do + @indirection.delete + Puppet::Util::Cacher.expire + end + end - describe "when managing indirection instances" do - it "should allow an indirection to be retrieved by name" do - @indirection = Puppet::Indirector::Indirection.new(mock('model'), :test) - Puppet::Indirector::Indirection.instance(:test).should equal(@indirection) - end - it "should return nil when the named indirection has not been created" do - Puppet::Indirector::Indirection.instance(:test).should be_nil - end + describe "when managing indirection instances" do + it "should allow an indirection to be retrieved by name" do + @indirection = Puppet::Indirector::Indirection.new(mock('model'), :test) + Puppet::Indirector::Indirection.instance(:test).should equal(@indirection) + end - it "should allow an indirection's model to be retrieved by name" do - mock_model = mock('model') - @indirection = Puppet::Indirector::Indirection.new(mock_model, :test) - Puppet::Indirector::Indirection.model(:test).should equal(mock_model) - end + it "should return nil when the named indirection has not been created" do + Puppet::Indirector::Indirection.instance(:test).should be_nil + end - it "should return nil when no model matches the requested name" do - Puppet::Indirector::Indirection.model(:test).should be_nil - end + it "should allow an indirection's model to be retrieved by name" do + mock_model = mock('model') + @indirection = Puppet::Indirector::Indirection.new(mock_model, :test) + Puppet::Indirector::Indirection.model(:test).should equal(mock_model) + end - after do - @indirection.delete if defined?(@indirection) - end + it "should return nil when no model matches the requested name" do + Puppet::Indirector::Indirection.model(:test).should be_nil end - describe "when routing to the correct the terminus class" do - before do - @indirection = Puppet::Indirector::Indirection.new(mock('model'), :test) - @terminus = mock 'terminus' - @terminus_class = stub 'terminus class', :new => @terminus - Puppet::Indirector::Terminus.stubs(:terminus_class).with(:test, :default).returns(@terminus_class) - end + after do + @indirection.delete if defined?(@indirection) + end + end - it "should fail if no terminus class can be picked" do - proc { @indirection.terminus_class }.should raise_error(Puppet::DevError) - end + describe "when routing to the correct the terminus class" do + before do + @indirection = Puppet::Indirector::Indirection.new(mock('model'), :test) + @terminus = mock 'terminus' + @terminus_class = stub 'terminus class', :new => @terminus + Puppet::Indirector::Terminus.stubs(:terminus_class).with(:test, :default).returns(@terminus_class) + end - it "should choose the default terminus class if one is specified" do - @indirection.terminus_class = :default - @indirection.terminus_class.should equal(:default) - end + it "should fail if no terminus class can be picked" do + proc { @indirection.terminus_class }.should raise_error(Puppet::DevError) + end - it "should use the provided Puppet setting if told to do so" do - Puppet::Indirector::Terminus.stubs(:terminus_class).with(:test, :my_terminus).returns(mock("terminus_class2")) - Puppet.settings.expects(:value).with(:my_setting).returns("my_terminus") - @indirection.terminus_setting = :my_setting - @indirection.terminus_class.should equal(:my_terminus) - end + it "should choose the default terminus class if one is specified" do + @indirection.terminus_class = :default + @indirection.terminus_class.should equal(:default) + end - it "should fail if the provided terminus class is not valid" do - Puppet::Indirector::Terminus.stubs(:terminus_class).with(:test, :nosuchclass).returns(nil) - proc { @indirection.terminus_class = :nosuchclass }.should raise_error(ArgumentError) - end + it "should use the provided Puppet setting if told to do so" do + Puppet::Indirector::Terminus.stubs(:terminus_class).with(:test, :my_terminus).returns(mock("terminus_class2")) + Puppet.settings.expects(:value).with(:my_setting).returns("my_terminus") + @indirection.terminus_setting = :my_setting + @indirection.terminus_class.should equal(:my_terminus) + end - after do - @indirection.delete if defined?(@indirection) - end + it "should fail if the provided terminus class is not valid" do + Puppet::Indirector::Terminus.stubs(:terminus_class).with(:test, :nosuchclass).returns(nil) + proc { @indirection.terminus_class = :nosuchclass }.should raise_error(ArgumentError) end - describe "when specifying the terminus class to use" do - before do - @indirection = Puppet::Indirector::Indirection.new(mock('model'), :test) - @terminus = mock 'terminus' - @terminus_class = stub 'terminus class', :new => @terminus - end + after do + @indirection.delete if defined?(@indirection) + end + end - it "should allow specification of a terminus type" do - @indirection.should respond_to(:terminus_class=) - end + describe "when specifying the terminus class to use" do + before do + @indirection = Puppet::Indirector::Indirection.new(mock('model'), :test) + @terminus = mock 'terminus' + @terminus_class = stub 'terminus class', :new => @terminus + end - it "should fail to redirect if no terminus type has been specified" do - proc { @indirection.find("blah") }.should raise_error(Puppet::DevError) - end + it "should allow specification of a terminus type" do + @indirection.should respond_to(:terminus_class=) + end - it "should fail when the terminus class name is an empty string" do - proc { @indirection.terminus_class = "" }.should raise_error(ArgumentError) - end + it "should fail to redirect if no terminus type has been specified" do + proc { @indirection.find("blah") }.should raise_error(Puppet::DevError) + end - it "should fail when the terminus class name is nil" do - proc { @indirection.terminus_class = nil }.should raise_error(ArgumentError) - end + it "should fail when the terminus class name is an empty string" do + proc { @indirection.terminus_class = "" }.should raise_error(ArgumentError) + end - it "should fail when the specified terminus class cannot be found" do - Puppet::Indirector::Terminus.expects(:terminus_class).with(:test, :foo).returns(nil) - proc { @indirection.terminus_class = :foo }.should raise_error(ArgumentError) - end + it "should fail when the terminus class name is nil" do + proc { @indirection.terminus_class = nil }.should raise_error(ArgumentError) + end - it "should select the specified terminus class if a terminus class name is provided" do - Puppet::Indirector::Terminus.expects(:terminus_class).with(:test, :foo).returns(@terminus_class) - @indirection.terminus(:foo).should equal(@terminus) - end + it "should fail when the specified terminus class cannot be found" do + Puppet::Indirector::Terminus.expects(:terminus_class).with(:test, :foo).returns(nil) + proc { @indirection.terminus_class = :foo }.should raise_error(ArgumentError) + end - it "should use the configured terminus class if no terminus name is specified" do - Puppet::Indirector::Terminus.stubs(:terminus_class).with(:test, :foo).returns(@terminus_class) - @indirection.terminus_class = :foo - @indirection.terminus.should equal(@terminus) - end + it "should select the specified terminus class if a terminus class name is provided" do + Puppet::Indirector::Terminus.expects(:terminus_class).with(:test, :foo).returns(@terminus_class) + @indirection.terminus(:foo).should equal(@terminus) + end - after do - @indirection.delete if defined?(@indirection) - end + it "should use the configured terminus class if no terminus name is specified" do + Puppet::Indirector::Terminus.stubs(:terminus_class).with(:test, :foo).returns(@terminus_class) + @indirection.terminus_class = :foo + @indirection.terminus.should equal(@terminus) end - describe "when managing terminus instances" do - before do - @indirection = Puppet::Indirector::Indirection.new(mock('model'), :test) - @terminus = mock 'terminus' - @terminus_class = mock 'terminus class' - Puppet::Indirector::Terminus.stubs(:terminus_class).with(:test, :foo).returns(@terminus_class) - end + after do + @indirection.delete if defined?(@indirection) + end + 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 + describe "when managing terminus instances" do + before do + @indirection = Puppet::Indirector::Indirection.new(mock('model'), :test) + @terminus = mock 'terminus' + @terminus_class = mock 'terminus class' + Puppet::Indirector::Terminus.stubs(:terminus_class).with(:test, :foo).returns(@terminus_class) + end - # Make sure it caches the 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 + 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 not create a terminus instance until one is actually needed" do - Puppet::Indirector.expects(:terminus).never - indirection = Puppet::Indirector::Indirection.new(mock('model'), :lazytest) - end + # Make sure it caches the 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 - after do - @indirection.delete - end + it "should not create a terminus instance until one is actually needed" do + Puppet::Indirector.expects(:terminus).never + indirection = Puppet::Indirector::Indirection.new(mock('model'), :lazytest) end - describe "when deciding whether to cache" do - before do - @indirection = Puppet::Indirector::Indirection.new(mock('model'), :test) - @terminus = mock 'terminus' - @terminus_class = mock 'terminus class' - @terminus_class.stubs(:new).returns(@terminus) - Puppet::Indirector::Terminus.stubs(:terminus_class).with(:test, :foo).returns(@terminus_class) - @indirection.terminus_class = :foo - end + after do + @indirection.delete + end + end - it "should provide a method for setting the cache terminus class" do - @indirection.should respond_to(:cache_class=) - end + describe "when deciding whether to cache" do + before do + @indirection = Puppet::Indirector::Indirection.new(mock('model'), :test) + @terminus = mock 'terminus' + @terminus_class = mock 'terminus class' + @terminus_class.stubs(:new).returns(@terminus) + Puppet::Indirector::Terminus.stubs(:terminus_class).with(:test, :foo).returns(@terminus_class) + @indirection.terminus_class = :foo + end - it "should fail to cache if no cache type has been specified" do - proc { @indirection.cache }.should raise_error(Puppet::DevError) - end + it "should provide a method for setting the cache terminus class" do + @indirection.should respond_to(:cache_class=) + end - it "should fail to set the cache class when the cache class name is an empty string" do - proc { @indirection.cache_class = "" }.should raise_error(ArgumentError) - end + it "should fail to cache if no cache type has been specified" do + proc { @indirection.cache }.should raise_error(Puppet::DevError) + end - it "should allow resetting the cache_class to nil" do - @indirection.cache_class = nil - @indirection.cache_class.should be_nil - end + it "should fail to set the cache class when the cache class name is an empty string" do + proc { @indirection.cache_class = "" }.should raise_error(ArgumentError) + end - it "should fail to set the cache class when the specified cache class cannot be found" do - Puppet::Indirector::Terminus.expects(:terminus_class).with(:test, :foo).returns(nil) - proc { @indirection.cache_class = :foo }.should raise_error(ArgumentError) - end + it "should allow resetting the cache_class to nil" do + @indirection.cache_class = nil + @indirection.cache_class.should be_nil + end - after do - @indirection.delete - end + it "should fail to set the cache class when the specified cache class cannot be found" do + Puppet::Indirector::Terminus.expects(:terminus_class).with(:test, :foo).returns(nil) + proc { @indirection.cache_class = :foo }.should raise_error(ArgumentError) end - describe "when using a cache" do - before :each do - 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(:test, :cache_terminus).returns(@cache_class) - Puppet::Indirector::Terminus.stubs(:terminus_class).with(:test, :test_terminus).returns(@terminus_class) - @indirection = Puppet::Indirector::Indirection.new(mock('model'), :test) - @indirection.terminus_class = :test_terminus - end + after do + @indirection.delete + end + end + + describe "when using a cache" do + before :each do + 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(:test, :cache_terminus).returns(@cache_class) + Puppet::Indirector::Terminus.stubs(:terminus_class).with(:test, :test_terminus).returns(@terminus_class) + @indirection = Puppet::Indirector::Indirection.new(mock('model'), :test) + @indirection.terminus_class = :test_terminus + end - describe "and managing the cache terminus" do - 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_class = :cache_terminus - @indirection.cache.should equal(@cache) - @indirection.cache.should equal(@cache) - end - end + describe "and managing the cache terminus" do + 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_class = :cache_terminus + @indirection.cache.should equal(@cache) + @indirection.cache.should equal(@cache) + end + end - describe "and saving" do - end + describe "and saving" do + end - describe "and finding" do - end + describe "and finding" do + end - after :each do - @indirection.delete - end + after :each do + @indirection.delete end + end end diff --git a/spec/unit/indirector/key/ca_spec.rb b/spec/unit/indirector/key/ca_spec.rb index 964b0b0ed..9ed5df67b 100755 --- a/spec/unit/indirector/key/ca_spec.rb +++ b/spec/unit/indirector/key/ca_spec.rb @@ -8,21 +8,21 @@ require File.dirname(__FILE__) + '/../../../spec_helper' require 'puppet/indirector/key/ca' describe Puppet::SSL::Key::Ca do - it "should have documentation" do - Puppet::SSL::Key::Ca.doc.should be_instance_of(String) - end + it "should have documentation" do + Puppet::SSL::Key::Ca.doc.should be_instance_of(String) + end - it "should use the :privatekeydir as the collection directory" do - Puppet.settings.expects(:value).with(:privatekeydir).returns "/key/dir" - Puppet::SSL::Key::Ca.collection_directory.should == "/key/dir" - end + it "should use the :privatekeydir as the collection directory" do + Puppet.settings.expects(:value).with(:privatekeydir).returns "/key/dir" + Puppet::SSL::Key::Ca.collection_directory.should == "/key/dir" + end - it "should store the ca key at the :cakey location" do - Puppet.settings.stubs(:use) - Puppet.settings.stubs(:value).returns "whatever" - Puppet.settings.stubs(:value).with(:cakey).returns "/ca/key" - file = Puppet::SSL::Key::Ca.new - file.stubs(:ca?).returns true - file.path("whatever").should == "/ca/key" - end + it "should store the ca key at the :cakey location" do + Puppet.settings.stubs(:use) + Puppet.settings.stubs(:value).returns "whatever" + Puppet.settings.stubs(:value).with(:cakey).returns "/ca/key" + file = Puppet::SSL::Key::Ca.new + file.stubs(:ca?).returns true + file.path("whatever").should == "/ca/key" + end end diff --git a/spec/unit/indirector/key/file_spec.rb b/spec/unit/indirector/key/file_spec.rb index f365bfd60..5037c7f0e 100755 --- a/spec/unit/indirector/key/file_spec.rb +++ b/spec/unit/indirector/key/file_spec.rb @@ -8,97 +8,97 @@ require File.dirname(__FILE__) + '/../../../spec_helper' require 'puppet/indirector/key/file' describe Puppet::SSL::Key::File do - it "should have documentation" do - Puppet::SSL::Key::File.doc.should be_instance_of(String) + it "should have documentation" do + Puppet::SSL::Key::File.doc.should be_instance_of(String) + end + + it "should use the :privatekeydir as the collection directory" do + Puppet.settings.expects(:value).with(:privatekeydir).returns "/key/dir" + Puppet::SSL::Key::File.collection_directory.should == "/key/dir" + end + + it "should store the ca key at the :cakey location" do + Puppet.settings.stubs(:use) + Puppet.settings.stubs(:value).returns "whatever" + Puppet.settings.stubs(:value).with(:cakey).returns "/ca/key" + file = Puppet::SSL::Key::File.new + file.stubs(:ca?).returns true + file.path("whatever").should == "/ca/key" + end + + describe "when choosing the path for the public key" do + it "should use the :capub setting location if the key is for the certificate authority" do + Puppet.settings.stubs(:value).returns "/fake/dir" + Puppet.settings.stubs(:value).with(:capub).returns "/ca/pubkey" + Puppet.settings.stubs(:use) + + @searcher = Puppet::SSL::Key::File.new + @searcher.stubs(:ca?).returns true + @searcher.public_key_path("whatever").should == "/ca/pubkey" end - it "should use the :privatekeydir as the collection directory" do - Puppet.settings.expects(:value).with(:privatekeydir).returns "/key/dir" - Puppet::SSL::Key::File.collection_directory.should == "/key/dir" - end - - it "should store the ca key at the :cakey location" do - Puppet.settings.stubs(:use) - Puppet.settings.stubs(:value).returns "whatever" - Puppet.settings.stubs(:value).with(:cakey).returns "/ca/key" - file = Puppet::SSL::Key::File.new - file.stubs(:ca?).returns true - file.path("whatever").should == "/ca/key" - end + it "should use the host name plus '.pem' in :publickeydir for normal hosts" do + Puppet.settings.stubs(:value).with(:privatekeydir).returns "/private/key/dir" + Puppet.settings.stubs(:value).with(:publickeydir).returns "/public/key/dir" + Puppet.settings.stubs(:use) - describe "when choosing the path for the public key" do - it "should use the :capub setting location if the key is for the certificate authority" do - Puppet.settings.stubs(:value).returns "/fake/dir" - Puppet.settings.stubs(:value).with(:capub).returns "/ca/pubkey" - Puppet.settings.stubs(:use) - - @searcher = Puppet::SSL::Key::File.new - @searcher.stubs(:ca?).returns true - @searcher.public_key_path("whatever").should == "/ca/pubkey" - end - - it "should use the host name plus '.pem' in :publickeydir for normal hosts" do - Puppet.settings.stubs(:value).with(:privatekeydir).returns "/private/key/dir" - Puppet.settings.stubs(:value).with(:publickeydir).returns "/public/key/dir" - Puppet.settings.stubs(:use) - - @searcher = Puppet::SSL::Key::File.new - @searcher.stubs(:ca?).returns false - @searcher.public_key_path("whatever").should == "/public/key/dir/whatever.pem" - end + @searcher = Puppet::SSL::Key::File.new + @searcher.stubs(:ca?).returns false + @searcher.public_key_path("whatever").should == "/public/key/dir/whatever.pem" end + end - describe "when managing private keys" do - before do - @searcher = Puppet::SSL::Key::File.new + describe "when managing private keys" do + before do + @searcher = Puppet::SSL::Key::File.new - @private_key_path = File.join("/fake/key/path") - @public_key_path = File.join("/other/fake/key/path") + @private_key_path = File.join("/fake/key/path") + @public_key_path = File.join("/other/fake/key/path") - @searcher.stubs(:public_key_path).returns @public_key_path - @searcher.stubs(:path).returns @private_key_path + @searcher.stubs(:public_key_path).returns @public_key_path + @searcher.stubs(:path).returns @private_key_path - FileTest.stubs(:directory?).returns true - FileTest.stubs(:writable?).returns true + FileTest.stubs(:directory?).returns true + FileTest.stubs(:writable?).returns true - @public_key = stub 'public_key' - @real_key = stub 'sslkey', :public_key => @public_key + @public_key = stub 'public_key' + @real_key = stub 'sslkey', :public_key => @public_key - @key = stub 'key', :name => "myname", :content => @real_key + @key = stub 'key', :name => "myname", :content => @real_key - @request = stub 'request', :key => "myname", :instance => @key - end + @request = stub 'request', :key => "myname", :instance => @key + end - it "should save the public key when saving the private key" do - Puppet.settings.stubs(:writesub) + it "should save the public key when saving the private key" do + Puppet.settings.stubs(:writesub) - fh = mock 'filehandle' + fh = mock 'filehandle' - Puppet.settings.expects(:writesub).with(:publickeydir, @public_key_path).yields fh - @public_key.expects(:to_pem).returns "my pem" + Puppet.settings.expects(:writesub).with(:publickeydir, @public_key_path).yields fh + @public_key.expects(:to_pem).returns "my pem" - fh.expects(:print).with "my pem" + fh.expects(:print).with "my pem" - @searcher.save(@request) - end + @searcher.save(@request) + end - it "should destroy the public key when destroying the private key" do - File.stubs(:unlink).with(@private_key_path) - FileTest.stubs(:exist?).with(@private_key_path).returns true - FileTest.expects(:exist?).with(@public_key_path).returns true - File.expects(:unlink).with(@public_key_path) + it "should destroy the public key when destroying the private key" do + File.stubs(:unlink).with(@private_key_path) + FileTest.stubs(:exist?).with(@private_key_path).returns true + FileTest.expects(:exist?).with(@public_key_path).returns true + File.expects(:unlink).with(@public_key_path) - @searcher.destroy(@request) - end + @searcher.destroy(@request) + end - it "should not fail if the public key does not exist when deleting the private key" do - File.stubs(:unlink).with(@private_key_path) + it "should not fail if the public key does not exist when deleting the private key" do + File.stubs(:unlink).with(@private_key_path) - FileTest.stubs(:exist?).with(@private_key_path).returns true - FileTest.expects(:exist?).with(@public_key_path).returns false - File.expects(:unlink).with(@public_key_path).never + FileTest.stubs(:exist?).with(@private_key_path).returns true + FileTest.expects(:exist?).with(@public_key_path).returns false + File.expects(:unlink).with(@public_key_path).never - @searcher.destroy(@request) - end + @searcher.destroy(@request) end + end end diff --git a/spec/unit/indirector/ldap_spec.rb b/spec/unit/indirector/ldap_spec.rb index 44df2b5bb..31a3406e9 100755 --- a/spec/unit/indirector/ldap_spec.rb +++ b/spec/unit/indirector/ldap_spec.rb @@ -5,139 +5,139 @@ require File.dirname(__FILE__) + '/../../spec_helper' require 'puppet/indirector/ldap' describe Puppet::Indirector::Ldap do - before do - @indirection = stub 'indirection', :name => :testing - Puppet::Indirector::Indirection.stubs(:instance).returns(@indirection) - @ldap_class = Class.new(Puppet::Indirector::Ldap) do - def self.to_s - "Testing::Mytype" - end - end - - @connection = mock 'ldap' - - @searcher = @ldap_class.new + before do + @indirection = stub 'indirection', :name => :testing + Puppet::Indirector::Indirection.stubs(:instance).returns(@indirection) + @ldap_class = Class.new(Puppet::Indirector::Ldap) do + def self.to_s + "Testing::Mytype" + end end - describe "when searching ldap" do - before do - # Stub everything, and we can selectively replace with an expect as - # we need to for testing. - @searcher.stubs(:connection).returns(@connection) - @searcher.stubs(:search_filter).returns(:filter) - @searcher.stubs(:search_base).returns(:base) - @searcher.stubs(:process) + @connection = mock 'ldap' - @request = stub 'request', :key => "yay" - end + @searcher = @ldap_class.new + end - it "should call the ldapsearch method with the search filter" do - @searcher.expects(:search_filter).with("yay").returns("yay's filter") - @searcher.expects(:ldapsearch).with("yay's filter") - @searcher.find @request - end + describe "when searching ldap" do + before do + # Stub everything, and we can selectively replace with an expect as + # we need to for testing. + @searcher.stubs(:connection).returns(@connection) + @searcher.stubs(:search_filter).returns(:filter) + @searcher.stubs(:search_base).returns(:base) + @searcher.stubs(:process) + + @request = stub 'request', :key => "yay" + end - it "should fail if no block is passed to the ldapsearch method" do - proc { @searcher.ldapsearch("blah") }.should raise_error(ArgumentError) - end + it "should call the ldapsearch method with the search filter" do + @searcher.expects(:search_filter).with("yay").returns("yay's filter") + @searcher.expects(:ldapsearch).with("yay's filter") + @searcher.find @request + end - it "should use the results of the ldapbase method as the ldap search base" do - @searcher.stubs(:search_base).returns("mybase") - @connection.expects(:search).with do |*args| - args[0].should == "mybase" - true - end - @searcher.find @request - end + it "should fail if no block is passed to the ldapsearch method" do + proc { @searcher.ldapsearch("blah") }.should raise_error(ArgumentError) + end - it "should default to the value of the :search_base setting as the result of the ldapbase method" do - Puppet.expects(:[]).with(:ldapbase).returns("myldapbase") - searcher = @ldap_class.new - searcher.search_base.should == "myldapbase" - end + it "should use the results of the ldapbase method as the ldap search base" do + @searcher.stubs(:search_base).returns("mybase") + @connection.expects(:search).with do |*args| + args[0].should == "mybase" + true + end + @searcher.find @request + end - it "should use the results of the :search_attributes method as the list of attributes to return" do - @searcher.stubs(:search_attributes).returns(:myattrs) - @connection.expects(:search).with do |*args| - args[3].should == :myattrs - true - end - @searcher.find @request - end + it "should default to the value of the :search_base setting as the result of the ldapbase method" do + Puppet.expects(:[]).with(:ldapbase).returns("myldapbase") + searcher = @ldap_class.new + searcher.search_base.should == "myldapbase" + end - it "should use depth 2 when searching" do - @connection.expects(:search).with do |*args| - args[1].should == 2 - true - end - @searcher.find @request - end + it "should use the results of the :search_attributes method as the list of attributes to return" do + @searcher.stubs(:search_attributes).returns(:myattrs) + @connection.expects(:search).with do |*args| + args[3].should == :myattrs + true + end + @searcher.find @request + end - it "should call process() on the first found entry" do - @connection.expects(:search).yields("myresult") - @searcher.expects(:process).with("myresult") - @searcher.find @request - end + it "should use depth 2 when searching" do + @connection.expects(:search).with do |*args| + args[1].should == 2 + true + end + @searcher.find @request + end - it "should reconnect and retry the search if there is a failure" do - run = false - @connection.stubs(:search).with do |*args| - if run - true - else - run = true - raise "failed" - end - end.yields("myresult") - @searcher.expects(:process).with("myresult") - - @searcher.find @request - end + it "should call process() on the first found entry" do + @connection.expects(:search).yields("myresult") + @searcher.expects(:process).with("myresult") + @searcher.find @request + end - it "should not reconnect on failure more than once" do - count = 0 - @connection.stubs(:search).with do |*args| - count += 1 - raise ArgumentError, "yay" - end - proc { @searcher.find(@request) }.should raise_error(Puppet::Error) - count.should == 2 + it "should reconnect and retry the search if there is a failure" do + run = false + @connection.stubs(:search).with do |*args| + if run + true + else + run = true + raise "failed" end + end.yields("myresult") + @searcher.expects(:process).with("myresult") - it "should return true if an entry is found" do - @connection.expects(:search).yields("result") - @searcher.ldapsearch("whatever") { |r| }.should be_true - end + @searcher.find @request end - describe "when connecting to ldap" do - confine "LDAP is not available" => Puppet.features.ldap? + it "should not reconnect on failure more than once" do + count = 0 + @connection.stubs(:search).with do |*args| + count += 1 + raise ArgumentError, "yay" + end + proc { @searcher.find(@request) }.should raise_error(Puppet::Error) + count.should == 2 + end - it "should create and start a Util::Ldap::Connection instance" do - conn = mock 'connection', :connection => "myconn", :start => nil - Puppet::Util::Ldap::Connection.expects(:instance).returns conn + it "should return true if an entry is found" do + @connection.expects(:search).yields("result") + @searcher.ldapsearch("whatever") { |r| }.should be_true + end + end - @searcher.connection.should == "myconn" - end + describe "when connecting to ldap" do + confine "LDAP is not available" => Puppet.features.ldap? - it "should only create the ldap connection when asked for it the first time" do - conn = mock 'connection', :connection => "myconn", :start => nil - Puppet::Util::Ldap::Connection.expects(:instance).returns conn + it "should create and start a Util::Ldap::Connection instance" do + conn = mock 'connection', :connection => "myconn", :start => nil + Puppet::Util::Ldap::Connection.expects(:instance).returns conn - @searcher.connection - end + @searcher.connection.should == "myconn" + end - it "should cache the connection" do - conn = mock 'connection', :connection => "myconn", :start => nil - Puppet::Util::Ldap::Connection.expects(:instance).returns conn + it "should only create the ldap connection when asked for it the first time" do + conn = mock 'connection', :connection => "myconn", :start => nil + Puppet::Util::Ldap::Connection.expects(:instance).returns conn - @searcher.connection.should equal(@searcher.connection) - end + @searcher.connection end - describe "when reconnecting to ldap" do - confine "Not running on culain as root" => (Puppet.features.root? and Facter.value("hostname") == "culain") + it "should cache the connection" do + conn = mock 'connection', :connection => "myconn", :start => nil + Puppet::Util::Ldap::Connection.expects(:instance).returns conn - it "should reconnect to ldap when connections are lost" + @searcher.connection.should equal(@searcher.connection) end + end + + describe "when reconnecting to ldap" do + confine "Not running on culain as root" => (Puppet.features.root? and Facter.value("hostname") == "culain") + + it "should reconnect to ldap when connections are lost" + end end diff --git a/spec/unit/indirector/memory_spec.rb b/spec/unit/indirector/memory_spec.rb index 3b754a1eb..160b4f91d 100755 --- a/spec/unit/indirector/memory_spec.rb +++ b/spec/unit/indirector/memory_spec.rb @@ -6,24 +6,24 @@ require 'puppet/indirector/memory' require 'shared_behaviours/memory_terminus' describe Puppet::Indirector::Memory do - it_should_behave_like "A Memory Terminus" + it_should_behave_like "A Memory Terminus" - before do - Puppet::Indirector::Terminus.stubs(:register_terminus_class) - @model = mock 'model' - @indirection = stub 'indirection', :name => :mystuff, :register_terminus_type => nil, :model => @model - Puppet::Indirector::Indirection.stubs(:instance).returns(@indirection) + before do + Puppet::Indirector::Terminus.stubs(:register_terminus_class) + @model = mock 'model' + @indirection = stub 'indirection', :name => :mystuff, :register_terminus_type => nil, :model => @model + Puppet::Indirector::Indirection.stubs(:instance).returns(@indirection) - @memory_class = Class.new(Puppet::Indirector::Memory) do - def self.to_s - "Mystuff::Testing" - end - end + @memory_class = Class.new(Puppet::Indirector::Memory) do + def self.to_s + "Mystuff::Testing" + end + end - @searcher = @memory_class.new - @name = "me" - @instance = stub 'instance', :name => @name + @searcher = @memory_class.new + @name = "me" + @instance = stub 'instance', :name => @name - @request = stub 'request', :key => @name, :instance => @instance - end + @request = stub 'request', :key => @name, :instance => @instance + end end diff --git a/spec/unit/indirector/node/active_record_spec.rb b/spec/unit/indirector/node/active_record_spec.rb index 8e7d1d6bd..6cc3f5132 100755 --- a/spec/unit/indirector/node/active_record_spec.rb +++ b/spec/unit/indirector/node/active_record_spec.rb @@ -6,36 +6,36 @@ require 'puppet/node' require 'spec/lib/puppet_spec/files.rb' describe "Puppet::Node::ActiveRecord" do - include PuppetSpec::Files + include PuppetSpec::Files - confine "Missing Rails" => Puppet.features.rails? - confine "Missing sqlite" => Puppet.features.sqlite? - before do - require 'puppet/indirector/node/active_record' - end + confine "Missing Rails" => Puppet.features.rails? + confine "Missing sqlite" => Puppet.features.sqlite? + before do + require 'puppet/indirector/node/active_record' + end - it "should be a subclass of the ActiveRecord terminus class" do - Puppet::Node::ActiveRecord.ancestors.should be_include(Puppet::Indirector::ActiveRecord) - end + it "should be a subclass of the ActiveRecord terminus class" do + Puppet::Node::ActiveRecord.ancestors.should be_include(Puppet::Indirector::ActiveRecord) + end - it "should use Puppet::Rails::Host as its ActiveRecord model" do - Puppet::Node::ActiveRecord.ar_model.should equal(Puppet::Rails::Host) - end + it "should use Puppet::Rails::Host as its ActiveRecord model" do + Puppet::Node::ActiveRecord.ar_model.should equal(Puppet::Rails::Host) + end - it "should call fact_merge when a node is found" do - db_instance = stub 'db_instance' - Puppet::Node::ActiveRecord.ar_model.expects(:find_by_name).returns db_instance + it "should call fact_merge when a node is found" do + db_instance = stub 'db_instance' + Puppet::Node::ActiveRecord.ar_model.expects(:find_by_name).returns db_instance - node = Puppet::Node.new("foo") - db_instance.expects(:to_puppet).returns node + node = Puppet::Node.new("foo") + db_instance.expects(:to_puppet).returns node - Puppet[:statedir] = tmpdir('active_record_tmp') - Puppet[:railslog] = '$statedir/rails.log' - ar = Puppet::Node::ActiveRecord.new + Puppet[:statedir] = tmpdir('active_record_tmp') + Puppet[:railslog] = '$statedir/rails.log' + ar = Puppet::Node::ActiveRecord.new - node.expects(:fact_merge) + node.expects(:fact_merge) - request = Puppet::Indirector::Request.new(:node, :find, "what.ever") - ar.find(request) - end + request = Puppet::Indirector::Request.new(:node, :find, "what.ever") + ar.find(request) + end end diff --git a/spec/unit/indirector/node/exec_spec.rb b/spec/unit/indirector/node/exec_spec.rb index c4b3417a3..d5299ad16 100755 --- a/spec/unit/indirector/node/exec_spec.rb +++ b/spec/unit/indirector/node/exec_spec.rb @@ -5,65 +5,65 @@ require File.dirname(__FILE__) + '/../../../spec_helper' require 'puppet/indirector/node/exec' describe Puppet::Node::Exec do - before do - @indirection = mock 'indirection' - Puppet.settings.stubs(:value).with(:external_nodes).returns("/echo") - @searcher = Puppet::Node::Exec.new - end + before do + @indirection = mock 'indirection' + Puppet.settings.stubs(:value).with(:external_nodes).returns("/echo") + @searcher = Puppet::Node::Exec.new + end - describe "when constructing the command to run" do - it "should use the external_node script as the command" do - Puppet.expects(:[]).with(:external_nodes).returns("/bin/echo") - @searcher.command.should == %w{/bin/echo} - end + describe "when constructing the command to run" do + it "should use the external_node script as the command" do + Puppet.expects(:[]).with(:external_nodes).returns("/bin/echo") + @searcher.command.should == %w{/bin/echo} + end - it "should throw an exception if no external node command is set" do - Puppet.expects(:[]).with(:external_nodes).returns("none") - proc { @searcher.find(stub('request', :key => "foo")) }.should raise_error(ArgumentError) - end + it "should throw an exception if no external node command is set" do + Puppet.expects(:[]).with(:external_nodes).returns("none") + proc { @searcher.find(stub('request', :key => "foo")) }.should raise_error(ArgumentError) end + end - describe "when handling the results of the command" do - before do - @node = stub 'node', :fact_merge => nil - @name = "yay" - Puppet::Node.expects(:new).with(@name).returns(@node) - @result = {} - # Use a local variable so the reference is usable in the execute definition. - result = @result - @searcher.meta_def(:execute) do |command| - return YAML.dump(result) - end + describe "when handling the results of the command" do + before do + @node = stub 'node', :fact_merge => nil + @name = "yay" + Puppet::Node.expects(:new).with(@name).returns(@node) + @result = {} + # Use a local variable so the reference is usable in the execute definition. + result = @result + @searcher.meta_def(:execute) do |command| + return YAML.dump(result) + end - @request = stub 'request', :key => @name - end + @request = stub 'request', :key => @name + end - it "should translate the YAML into a Node instance" do - # Use an empty hash - @searcher.find(@request).should equal(@node) - end + it "should translate the YAML into a Node instance" do + # Use an empty hash + @searcher.find(@request).should equal(@node) + end - it "should set the resulting parameters as the node parameters" do - @result[:parameters] = {"a" => "b", "c" => "d"} - @node.expects(:parameters=).with "a" => "b", "c" => "d" - @searcher.find(@request) - end + it "should set the resulting parameters as the node parameters" do + @result[:parameters] = {"a" => "b", "c" => "d"} + @node.expects(:parameters=).with "a" => "b", "c" => "d" + @searcher.find(@request) + end - it "should set the resulting classes as the node classes" do - @result[:classes] = %w{one two} - @node.expects(:classes=).with %w{one two} - @searcher.find(@request) - end + it "should set the resulting classes as the node classes" do + @result[:classes] = %w{one two} + @node.expects(:classes=).with %w{one two} + @searcher.find(@request) + end - it "should merge the node's facts with its parameters" do - @node.expects(:fact_merge) - @searcher.find(@request) - end + it "should merge the node's facts with its parameters" do + @node.expects(:fact_merge) + @searcher.find(@request) + end - it "should set the node's environment if one is provided" do - @result[:environment] = "yay" - @node.expects(:environment=).with "yay" - @searcher.find(@request) - end + it "should set the node's environment if one is provided" do + @result[:environment] = "yay" + @node.expects(:environment=).with "yay" + @searcher.find(@request) end + end end diff --git a/spec/unit/indirector/node/ldap_spec.rb b/spec/unit/indirector/node/ldap_spec.rb index aef623f86..f9c5efa07 100755 --- a/spec/unit/indirector/node/ldap_spec.rb +++ b/spec/unit/indirector/node/ldap_spec.rb @@ -5,452 +5,452 @@ require File.dirname(__FILE__) + '/../../../spec_helper' require 'puppet/indirector/node/ldap' describe Puppet::Node::Ldap do - before do - Puppet::Node::Facts.stubs(:terminus_class).returns :yaml - end - - describe "when searching for a single node" do - before :each do - @searcher = Puppet::Node::Ldap.new - - @name = "mynode.domain.com" - @node = stub 'node', :name => @name, :name= => nil - @node.stub_everything - - Puppet::Node.stubs(:new).returns(@node) - - @request = stub 'request', :key => @name - end - - it "should convert the hostname into a search filter" do - entry = stub 'entry', :dn => 'cn=mynode.domain.com,ou=hosts,dc=madstop,dc=com', :vals => %w{}, :to_hash => {"environment" => 'production'} - @searcher.expects(:ldapsearch).with("(&(objectclass=puppetClient)(cn=#{@name}))").yields entry - @searcher.name2hash(@name, 'production', 'parent') - end - - it "should convert any found entry into a hash" do - entry = stub 'entry', :dn => 'cn=mynode.domain.com,ou=hosts,dc=madstop,dc=com', :vals => %w{}, :to_hash => {"environment" => 'production'} - @searcher.expects(:ldapsearch).with("(&(objectclass=puppetClient)(cn=#{@name}))").yields entry - myhash = {"myhash" => true, :environment => 'production'} - @searcher.expects(:entry2hash).with(entry).returns myhash - @searcher.name2hash(@name, 'production', 'parent').should == myhash - end - - # This heavily tests our entry2hash method, so we don't have to stub out the stupid entry information any more. - describe "when an ldap entry is found" do - before do - @entry = stub 'entry', :dn => 'cn=mynode,ou=hosts,dc=madstop,dc=com', :vals => %w{}, :to_hash => {} - @searcher.stubs(:ldapsearch).yields @entry - end - - it "should convert the entry to a hash" do - @searcher.entry2hash(@entry).should be_instance_of(Hash) - end - - it "should add the entry's common name to the hash if fqdn if false" do - @searcher.entry2hash(@entry,fqdn = false)[:name].should == "mynode" - end - - it "should add the entry's fqdn name to the hash if fqdn if true" do - @searcher.entry2hash(@entry,fqdn = true)[:name].should == "mynode.madstop.com" - end - - it "should add all of the entry's classes to the hash" do - @entry.stubs(:vals).with("puppetclass").returns %w{one two} - @searcher.entry2hash(@entry)[:classes].should == %w{one two} - end - - it "should deduplicate class values" do - @entry.stubs(:to_hash).returns({}) - @searcher.stubs(:class_attributes).returns(%w{one two}) - @entry.stubs(:vals).with("one").returns(%w{a b}) - @entry.stubs(:vals).with("two").returns(%w{b c}) - @searcher.entry2hash(@entry)[:classes].should == %w{a b c} - end - - it "should add the entry's environment to the hash" do - @entry.stubs(:to_hash).returns("environment" => %w{production}) - @searcher.entry2hash(@entry)[:environment].should == "production" - end - - it "should add all stacked parameters as parameters in the hash" do - @entry.stubs(:vals).with("puppetvar").returns(%w{one=two three=four}) - result = @searcher.entry2hash(@entry) - result[:parameters]["one"].should == "two" - result[:parameters]["three"].should == "four" - end - - it "should not add the stacked parameter as a normal parameter" do - @entry.stubs(:vals).with("puppetvar").returns(%w{one=two three=four}) - @entry.stubs(:to_hash).returns("puppetvar" => %w{one=two three=four}) - @searcher.entry2hash(@entry)[:parameters]["puppetvar"].should be_nil - end - - it "should add all other attributes as parameters in the hash" do - @entry.stubs(:to_hash).returns("foo" => %w{one two}) - @searcher.entry2hash(@entry)[:parameters]["foo"].should == %w{one two} - end - - it "should return single-value parameters as strings, not arrays" do - @entry.stubs(:to_hash).returns("foo" => %w{one}) - @searcher.entry2hash(@entry)[:parameters]["foo"].should == "one" - end - - it "should convert 'true' values to the boolean 'true'" do - @entry.stubs(:to_hash).returns({"one" => ["true"]}) - @searcher.entry2hash(@entry)[:parameters]["one"].should == true - end - - it "should convert 'false' values to the boolean 'false'" do - @entry.stubs(:to_hash).returns({"one" => ["false"]}) - @searcher.entry2hash(@entry)[:parameters]["one"].should == false - end - - it "should convert 'true' values to the boolean 'true' inside an array" do - @entry.stubs(:to_hash).returns({"one" => ["true", "other"]}) - @searcher.entry2hash(@entry)[:parameters]["one"].should == [true, "other"] - end - - it "should convert 'false' values to the boolean 'false' inside an array" do - @entry.stubs(:to_hash).returns({"one" => ["false", "other"]}) - @searcher.entry2hash(@entry)[:parameters]["one"].should == [false, "other"] - end - - it "should add the parent's name if present" do - @entry.stubs(:vals).with("parentnode").returns(%w{foo}) - @searcher.entry2hash(@entry)[:parent].should == "foo" - end - - it "should fail if more than one parent is specified" do - @entry.stubs(:vals).with("parentnode").returns(%w{foo}) - @searcher.entry2hash(@entry)[:parent].should == "foo" - end - end - - it "should search first for the provided key" do - @searcher.expects(:name2hash).with("mynode.domain.com", 'production', 'child').returns({}) - @searcher.find(@request) - end - - it "should search for the short version of the provided key if the key looks like a hostname and no results are found for the key itself" do - @searcher.expects(:name2hash).with("mynode.domain.com", 'production', 'child').returns(nil) - @searcher.expects(:name2hash).with("mynode", 'production', 'child').returns({}) - @searcher.find(@request) - end - - it "should search for default information if no information can be found for the key" do - @searcher.expects(:name2hash).with("mynode.domain.com", 'production', 'child').returns(nil) - @searcher.expects(:name2hash).with("mynode", 'production', 'child').returns(nil) - @searcher.expects(:name2hash).with("default", 'production', 'child').returns({}) - @searcher.find(@request) - end - - it "should return nil if no results are found in ldap" do - @searcher.stubs(:name2hash).returns nil - @searcher.find(@request).should be_nil - end - - it "should return a node object if results are found in ldap" do - @searcher.stubs(:name2hash).returns({}) - @searcher.find(@request).should equal(@node) - end - - describe "and node information is found in LDAP" do - before do - @result = {} - @searcher.stubs(:name2hash).returns @result - end - - it "should create the node with the correct name, even if it was found by a different name" do - @searcher.expects(:name2hash).with("mynode.domain.com", 'production', 'child').returns nil - @searcher.expects(:name2hash).with("mynode", 'production', 'child').returns @result - - Puppet::Node.expects(:new).with("mynode.domain.com").returns @node - @searcher.find(@request) - end - - it "should add any classes from ldap" do - @result[:classes] = %w{a b c d} - @node.expects(:classes=).with(%w{a b c d}) - @searcher.find(@request) - end + before do + Puppet::Node::Facts.stubs(:terminus_class).returns :yaml + end - it "should add all entry attributes as node parameters" do - @result[:parameters] = {"one" => "two", "three" => "four"} - @node.expects(:parameters=).with("one" => "two", "three" => "four") - @searcher.find(@request) - end + describe "when searching for a single node" do + before :each do + @searcher = Puppet::Node::Ldap.new - it "should set the node's environment to the environment of the results" do - @result[:environment] = "test" - @node.expects(:environment=).with("test") - @searcher.find(@request) - end + @name = "mynode.domain.com" + @node = stub 'node', :name => @name, :name= => nil + @node.stub_everything - it "should retain false parameter values" do - @result[:parameters] = {} - @result[:parameters]["one"] = false - @node.expects(:parameters=).with("one" => false) - @searcher.find(@request) - end + Puppet::Node.stubs(:new).returns(@node) - it "should merge the node's facts after the parameters from ldap are assigned" do - # Make sure we've got data to start with, so the parameters are actually set. - @result[:parameters] = {} - @result[:parameters]["one"] = "yay" - - # A hackish way to enforce order. - set = false - @node.expects(:parameters=).with { |*args| set = true } - @node.expects(:fact_merge).with { |*args| raise "Facts were merged before parameters were set" unless set; true } - - @searcher.find(@request) - end - - describe "and a parent node is specified" do - before do - @entry = {:classes => [], :parameters => {}} - @parent = {:classes => [], :parameters => {}} - @parent_parent = {:classes => [], :parameters => {}} - - @searcher.stubs(:name2hash).with{|name, env, mode| name == @name}.returns(@entry) - @searcher.stubs(:name2hash).with{|name, env, mode| name == 'parent'}.returns(@parent) - @searcher.stubs(:name2hash).with{|name, env, mode| name == 'parent_parent'}.returns(@parent_parent) - - @searcher.stubs(:parent_attribute).returns(:parent) - end - - it "should search for the parent node" do - @entry[:parent] = "parent" - - @searcher.find(@request) - end + @request = stub 'request', :key => @name + end - it "should fail if the parent cannot be found" do - @entry[:parent] = "parent" + it "should convert the hostname into a search filter" do + entry = stub 'entry', :dn => 'cn=mynode.domain.com,ou=hosts,dc=madstop,dc=com', :vals => %w{}, :to_hash => {"environment" => 'production'} + @searcher.expects(:ldapsearch).with("(&(objectclass=puppetClient)(cn=#{@name}))").yields entry + @searcher.name2hash(@name, 'production', 'parent') + end - @searcher.expects(:name2hash).with('parent', 'production', 'parent').returns nil + it "should convert any found entry into a hash" do + entry = stub 'entry', :dn => 'cn=mynode.domain.com,ou=hosts,dc=madstop,dc=com', :vals => %w{}, :to_hash => {"environment" => 'production'} + @searcher.expects(:ldapsearch).with("(&(objectclass=puppetClient)(cn=#{@name}))").yields entry + myhash = {"myhash" => true, :environment => 'production'} + @searcher.expects(:entry2hash).with(entry).returns myhash + @searcher.name2hash(@name, 'production', 'parent').should == myhash + end - proc { @searcher.find(@request) }.should raise_error(Puppet::Error) - end + # This heavily tests our entry2hash method, so we don't have to stub out the stupid entry information any more. + describe "when an ldap entry is found" do + before do + @entry = stub 'entry', :dn => 'cn=mynode,ou=hosts,dc=madstop,dc=com', :vals => %w{}, :to_hash => {} + @searcher.stubs(:ldapsearch).yields @entry + end + + it "should convert the entry to a hash" do + @searcher.entry2hash(@entry).should be_instance_of(Hash) + end + + it "should add the entry's common name to the hash if fqdn if false" do + @searcher.entry2hash(@entry,fqdn = false)[:name].should == "mynode" + end + + it "should add the entry's fqdn name to the hash if fqdn if true" do + @searcher.entry2hash(@entry,fqdn = true)[:name].should == "mynode.madstop.com" + end + + it "should add all of the entry's classes to the hash" do + @entry.stubs(:vals).with("puppetclass").returns %w{one two} + @searcher.entry2hash(@entry)[:classes].should == %w{one two} + end + + it "should deduplicate class values" do + @entry.stubs(:to_hash).returns({}) + @searcher.stubs(:class_attributes).returns(%w{one two}) + @entry.stubs(:vals).with("one").returns(%w{a b}) + @entry.stubs(:vals).with("two").returns(%w{b c}) + @searcher.entry2hash(@entry)[:classes].should == %w{a b c} + end + + it "should add the entry's environment to the hash" do + @entry.stubs(:to_hash).returns("environment" => %w{production}) + @searcher.entry2hash(@entry)[:environment].should == "production" + end + + it "should add all stacked parameters as parameters in the hash" do + @entry.stubs(:vals).with("puppetvar").returns(%w{one=two three=four}) + result = @searcher.entry2hash(@entry) + result[:parameters]["one"].should == "two" + result[:parameters]["three"].should == "four" + end + + it "should not add the stacked parameter as a normal parameter" do + @entry.stubs(:vals).with("puppetvar").returns(%w{one=two three=four}) + @entry.stubs(:to_hash).returns("puppetvar" => %w{one=two three=four}) + @searcher.entry2hash(@entry)[:parameters]["puppetvar"].should be_nil + end + + it "should add all other attributes as parameters in the hash" do + @entry.stubs(:to_hash).returns("foo" => %w{one two}) + @searcher.entry2hash(@entry)[:parameters]["foo"].should == %w{one two} + end + + it "should return single-value parameters as strings, not arrays" do + @entry.stubs(:to_hash).returns("foo" => %w{one}) + @searcher.entry2hash(@entry)[:parameters]["foo"].should == "one" + end + + it "should convert 'true' values to the boolean 'true'" do + @entry.stubs(:to_hash).returns({"one" => ["true"]}) + @searcher.entry2hash(@entry)[:parameters]["one"].should == true + end + + it "should convert 'false' values to the boolean 'false'" do + @entry.stubs(:to_hash).returns({"one" => ["false"]}) + @searcher.entry2hash(@entry)[:parameters]["one"].should == false + end + + it "should convert 'true' values to the boolean 'true' inside an array" do + @entry.stubs(:to_hash).returns({"one" => ["true", "other"]}) + @searcher.entry2hash(@entry)[:parameters]["one"].should == [true, "other"] + end + + it "should convert 'false' values to the boolean 'false' inside an array" do + @entry.stubs(:to_hash).returns({"one" => ["false", "other"]}) + @searcher.entry2hash(@entry)[:parameters]["one"].should == [false, "other"] + end + + it "should add the parent's name if present" do + @entry.stubs(:vals).with("parentnode").returns(%w{foo}) + @searcher.entry2hash(@entry)[:parent].should == "foo" + end + + it "should fail if more than one parent is specified" do + @entry.stubs(:vals).with("parentnode").returns(%w{foo}) + @searcher.entry2hash(@entry)[:parent].should == "foo" + end + end - it "should add any parent classes to the node's classes" do - @entry[:parent] = "parent" - @entry[:classes] = %w{a b} + it "should search first for the provided key" do + @searcher.expects(:name2hash).with("mynode.domain.com", 'production', 'child').returns({}) + @searcher.find(@request) + end - @parent[:classes] = %w{c d} + it "should search for the short version of the provided key if the key looks like a hostname and no results are found for the key itself" do + @searcher.expects(:name2hash).with("mynode.domain.com", 'production', 'child').returns(nil) + @searcher.expects(:name2hash).with("mynode", 'production', 'child').returns({}) + @searcher.find(@request) + end - @node.expects(:classes=).with(%w{a b c d}) - @searcher.find(@request) - end + it "should search for default information if no information can be found for the key" do + @searcher.expects(:name2hash).with("mynode.domain.com", 'production', 'child').returns(nil) + @searcher.expects(:name2hash).with("mynode", 'production', 'child').returns(nil) + @searcher.expects(:name2hash).with("default", 'production', 'child').returns({}) + @searcher.find(@request) + end - it "should add any parent parameters to the node's parameters" do - @entry[:parent] = "parent" - @entry[:parameters]["one"] = "two" + it "should return nil if no results are found in ldap" do + @searcher.stubs(:name2hash).returns nil + @searcher.find(@request).should be_nil + end - @parent[:parameters]["three"] = "four" + it "should return a node object if results are found in ldap" do + @searcher.stubs(:name2hash).returns({}) + @searcher.find(@request).should equal(@node) + end - @node.expects(:parameters=).with("one" => "two", "three" => "four") - @searcher.find(@request) - end + describe "and node information is found in LDAP" do + before do + @result = {} + @searcher.stubs(:name2hash).returns @result + end + + it "should create the node with the correct name, even if it was found by a different name" do + @searcher.expects(:name2hash).with("mynode.domain.com", 'production', 'child').returns nil + @searcher.expects(:name2hash).with("mynode", 'production', 'child').returns @result + + Puppet::Node.expects(:new).with("mynode.domain.com").returns @node + @searcher.find(@request) + end + + it "should add any classes from ldap" do + @result[:classes] = %w{a b c d} + @node.expects(:classes=).with(%w{a b c d}) + @searcher.find(@request) + end + + it "should add all entry attributes as node parameters" do + @result[:parameters] = {"one" => "two", "three" => "four"} + @node.expects(:parameters=).with("one" => "two", "three" => "four") + @searcher.find(@request) + end + + it "should set the node's environment to the environment of the results" do + @result[:environment] = "test" + @node.expects(:environment=).with("test") + @searcher.find(@request) + end + + it "should retain false parameter values" do + @result[:parameters] = {} + @result[:parameters]["one"] = false + @node.expects(:parameters=).with("one" => false) + @searcher.find(@request) + end + + it "should merge the node's facts after the parameters from ldap are assigned" do + # Make sure we've got data to start with, so the parameters are actually set. + @result[:parameters] = {} + @result[:parameters]["one"] = "yay" + + # A hackish way to enforce order. + set = false + @node.expects(:parameters=).with { |*args| set = true } + @node.expects(:fact_merge).with { |*args| raise "Facts were merged before parameters were set" unless set; true } + + @searcher.find(@request) + end + + describe "and a parent node is specified" do + before do + @entry = {:classes => [], :parameters => {}} + @parent = {:classes => [], :parameters => {}} + @parent_parent = {:classes => [], :parameters => {}} + + @searcher.stubs(:name2hash).with{|name, env, mode| name == @name}.returns(@entry) + @searcher.stubs(:name2hash).with{|name, env, mode| name == 'parent'}.returns(@parent) + @searcher.stubs(:name2hash).with{|name, env, mode| name == 'parent_parent'}.returns(@parent_parent) + + @searcher.stubs(:parent_attribute).returns(:parent) + end - it "should prefer node parameters over parent parameters" do - @entry[:parent] = "parent" - @entry[:parameters]["one"] = "two" + it "should search for the parent node" do + @entry[:parent] = "parent" - @parent[:parameters]["one"] = "three" + @searcher.find(@request) + end - @node.expects(:parameters=).with("one" => "two") - @searcher.find(@request) - end + it "should fail if the parent cannot be found" do + @entry[:parent] = "parent" - it "should use the parent's environment if the node has none" do - @entry[:parent] = "parent" + @searcher.expects(:name2hash).with('parent', 'production', 'parent').returns nil - @parent[:environment] = "parent" + proc { @searcher.find(@request) }.should raise_error(Puppet::Error) + end - @node.stubs(:parameters=) - @node.expects(:environment=).with("parent") - @searcher.find(@request) - end + it "should add any parent classes to the node's classes" do + @entry[:parent] = "parent" + @entry[:classes] = %w{a b} - it "should prefer the node's environment to the parent's" do - @entry[:parent] = "parent" - @entry[:environment] = "child" + @parent[:classes] = %w{c d} - @parent[:environment] = "parent" + @node.expects(:classes=).with(%w{a b c d}) + @searcher.find(@request) + end - @node.stubs(:parameters=) - @node.expects(:environment=).with("child") - @searcher.find(@request) - end + it "should add any parent parameters to the node's parameters" do + @entry[:parent] = "parent" + @entry[:parameters]["one"] = "two" - it "should recursively look up parent information" do - @entry[:parent] = "parent" - @entry[:parameters]["one"] = "two" + @parent[:parameters]["three"] = "four" - @parent[:parent] = "parent_parent" - @parent[:parameters]["three"] = "four" + @node.expects(:parameters=).with("one" => "two", "three" => "four") + @searcher.find(@request) + end - @parent_parent[:parameters]["five"] = "six" + it "should prefer node parameters over parent parameters" do + @entry[:parent] = "parent" + @entry[:parameters]["one"] = "two" - @node.expects(:parameters=).with("one" => "two", "three" => "four", "five" => "six") - @searcher.find(@request) - end + @parent[:parameters]["one"] = "three" - it "should not allow loops in parent declarations" do - @entry[:parent] = "parent" - @parent[:parent] = @name - proc { @searcher.find(@request) }.should raise_error(ArgumentError) - end - end + @node.expects(:parameters=).with("one" => "two") + @searcher.find(@request) end - end - describe "when searching for multiple nodes" do - before :each do - @searcher = Puppet::Node::Ldap.new - @options = {} - @request = stub 'request', :key => "foo", :options => @options + it "should use the parent's environment if the node has none" do + @entry[:parent] = "parent" - Puppet::Node::Facts.stubs(:terminus_class).returns :yaml - end + @parent[:environment] = "parent" - it "should find all nodes if no arguments are provided" do - @searcher.expects(:ldapsearch).with("(objectclass=puppetClient)") - # LAK:NOTE The search method requires an essentially bogus key. It's - # an API problem that I don't really know how to fix. - @searcher.search @request + @node.stubs(:parameters=) + @node.expects(:environment=).with("parent") + @searcher.find(@request) end - describe "and a class is specified" do - it "should find all nodes that are members of that class" do - @searcher.expects(:ldapsearch).with("(&(objectclass=puppetClient)(puppetclass=one))") + it "should prefer the node's environment to the parent's" do + @entry[:parent] = "parent" + @entry[:environment] = "child" - @options[:class] = "one" - @searcher.search @request - end - end + @parent[:environment] = "parent" - describe "multiple classes are specified" do - it "should find all nodes that are members of all classes" do - @searcher.expects(:ldapsearch).with("(&(objectclass=puppetClient)(puppetclass=one)(puppetclass=two))") - @options[:class] = %w{one two} - @searcher.search @request - end + @node.stubs(:parameters=) + @node.expects(:environment=).with("child") + @searcher.find(@request) end - it "should process each found entry" do - # .yields can't be used to yield multiple values :/ - @searcher.expects(:ldapsearch).yields("one") - @searcher.expects(:entry2hash).with("one",nil).returns(:name => "foo") - @searcher.search @request - end + it "should recursively look up parent information" do + @entry[:parent] = "parent" + @entry[:parameters]["one"] = "two" - it "should return a node for each processed entry with the name from the entry" do - @searcher.expects(:ldapsearch).yields("whatever") - @searcher.expects(:entry2hash).with("whatever",nil).returns(:name => "foo") - result = @searcher.search(@request) - result[0].should be_instance_of(Puppet::Node) - result[0].name.should == "foo" - end + @parent[:parent] = "parent_parent" + @parent[:parameters]["three"] = "four" - it "should merge each node's facts" do - node = mock 'node' - Puppet::Node.expects(:new).with("foo").returns node - node.expects(:fact_merge) - @searcher.stubs(:ldapsearch).yields("one") - @searcher.stubs(:entry2hash).with("one",nil).returns(:name => "foo") - @searcher.search(@request) - end + @parent_parent[:parameters]["five"] = "six" - it "should pass the request's fqdn option to entry2hash" do - node = mock 'node' - @options[:fqdn] = :hello - Puppet::Node.stubs(:new).with("foo").returns node - node.stubs(:fact_merge) - @searcher.stubs(:ldapsearch).yields("one") - @searcher.expects(:entry2hash).with("one",:hello).returns(:name => "foo") - @searcher.search(@request) + @node.expects(:parameters=).with("one" => "two", "three" => "four", "five" => "six") + @searcher.find(@request) end - end -end -describe Puppet::Node::Ldap, " when developing the search query" do - before do - @searcher = Puppet::Node::Ldap.new + it "should not allow loops in parent declarations" do + @entry[:parent] = "parent" + @parent[:parent] = @name + proc { @searcher.find(@request) }.should raise_error(ArgumentError) + end + end end + end - it "should return the value of the :ldapclassattrs split on commas as the class attributes" do - Puppet.stubs(:[]).with(:ldapclassattrs).returns("one,two") - @searcher.class_attributes.should == %w{one two} - end + describe "when searching for multiple nodes" do + before :each do + @searcher = Puppet::Node::Ldap.new + @options = {} + @request = stub 'request', :key => "foo", :options => @options - it "should return nil as the parent attribute if the :ldapparentattr is set to an empty string" do - Puppet.stubs(:[]).with(:ldapparentattr).returns("") - @searcher.parent_attribute.should be_nil + Puppet::Node::Facts.stubs(:terminus_class).returns :yaml end - it "should return the value of the :ldapparentattr as the parent attribute" do - Puppet.stubs(:[]).with(:ldapparentattr).returns("pere") - @searcher.parent_attribute.should == "pere" + it "should find all nodes if no arguments are provided" do + @searcher.expects(:ldapsearch).with("(objectclass=puppetClient)") + # LAK:NOTE The search method requires an essentially bogus key. It's + # an API problem that I don't really know how to fix. + @searcher.search @request end - it "should use the value of the :ldapstring as the search filter" do - Puppet.stubs(:[]).with(:ldapstring).returns("mystring") - @searcher.search_filter("testing").should == "mystring" - end + describe "and a class is specified" do + it "should find all nodes that are members of that class" do + @searcher.expects(:ldapsearch).with("(&(objectclass=puppetClient)(puppetclass=one))") - it "should replace '%s' with the node name in the search filter if it is present" do - Puppet.stubs(:[]).with(:ldapstring).returns("my%sstring") - @searcher.search_filter("testing").should == "mytestingstring" + @options[:class] = "one" + @searcher.search @request + end end - it "should not modify the global :ldapstring when replacing '%s' in the search filter" do - filter = mock 'filter' - filter.expects(:include?).with("%s").returns(true) - filter.expects(:gsub).with("%s", "testing").returns("mynewstring") - Puppet.stubs(:[]).with(:ldapstring).returns(filter) - @searcher.search_filter("testing").should == "mynewstring" + describe "multiple classes are specified" do + it "should find all nodes that are members of all classes" do + @searcher.expects(:ldapsearch).with("(&(objectclass=puppetClient)(puppetclass=one)(puppetclass=two))") + @options[:class] = %w{one two} + @searcher.search @request + end end -end -describe Puppet::Node::Ldap, " when deciding attributes to search for" do - before do - @searcher = Puppet::Node::Ldap.new + it "should process each found entry" do + # .yields can't be used to yield multiple values :/ + @searcher.expects(:ldapsearch).yields("one") + @searcher.expects(:entry2hash).with("one",nil).returns(:name => "foo") + @searcher.search @request end - it "should use 'nil' if the :ldapattrs setting is 'all'" do - Puppet.stubs(:[]).with(:ldapattrs).returns("all") - @searcher.search_attributes.should be_nil + it "should return a node for each processed entry with the name from the entry" do + @searcher.expects(:ldapsearch).yields("whatever") + @searcher.expects(:entry2hash).with("whatever",nil).returns(:name => "foo") + result = @searcher.search(@request) + result[0].should be_instance_of(Puppet::Node) + result[0].name.should == "foo" end - it "should split the value of :ldapattrs on commas and use the result as the attribute list" do - Puppet.stubs(:[]).with(:ldapattrs).returns("one,two") - @searcher.stubs(:class_attributes).returns([]) - @searcher.stubs(:parent_attribute).returns(nil) - @searcher.search_attributes.should == %w{one two} + it "should merge each node's facts" do + node = mock 'node' + Puppet::Node.expects(:new).with("foo").returns node + node.expects(:fact_merge) + @searcher.stubs(:ldapsearch).yields("one") + @searcher.stubs(:entry2hash).with("one",nil).returns(:name => "foo") + @searcher.search(@request) end - it "should add the class attributes to the search attributes if not returning all attributes" do - Puppet.stubs(:[]).with(:ldapattrs).returns("one,two") - @searcher.stubs(:class_attributes).returns(%w{three four}) - @searcher.stubs(:parent_attribute).returns(nil) - # Sort them so i don't have to care about return order - @searcher.search_attributes.sort.should == %w{one two three four}.sort + it "should pass the request's fqdn option to entry2hash" do + node = mock 'node' + @options[:fqdn] = :hello + Puppet::Node.stubs(:new).with("foo").returns node + node.stubs(:fact_merge) + @searcher.stubs(:ldapsearch).yields("one") + @searcher.expects(:entry2hash).with("one",:hello).returns(:name => "foo") + @searcher.search(@request) end + end +end - it "should add the parent attribute to the search attributes if not returning all attributes" do - Puppet.stubs(:[]).with(:ldapattrs).returns("one,two") - @searcher.stubs(:class_attributes).returns([]) - @searcher.stubs(:parent_attribute).returns("parent") - @searcher.search_attributes.sort.should == %w{one two parent}.sort - end +describe Puppet::Node::Ldap, " when developing the search query" do + before do + @searcher = Puppet::Node::Ldap.new + end + + it "should return the value of the :ldapclassattrs split on commas as the class attributes" do + Puppet.stubs(:[]).with(:ldapclassattrs).returns("one,two") + @searcher.class_attributes.should == %w{one two} + end + + it "should return nil as the parent attribute if the :ldapparentattr is set to an empty string" do + Puppet.stubs(:[]).with(:ldapparentattr).returns("") + @searcher.parent_attribute.should be_nil + end + + it "should return the value of the :ldapparentattr as the parent attribute" do + Puppet.stubs(:[]).with(:ldapparentattr).returns("pere") + @searcher.parent_attribute.should == "pere" + end + + it "should use the value of the :ldapstring as the search filter" do + Puppet.stubs(:[]).with(:ldapstring).returns("mystring") + @searcher.search_filter("testing").should == "mystring" + end + + it "should replace '%s' with the node name in the search filter if it is present" do + Puppet.stubs(:[]).with(:ldapstring).returns("my%sstring") + @searcher.search_filter("testing").should == "mytestingstring" + end + + it "should not modify the global :ldapstring when replacing '%s' in the search filter" do + filter = mock 'filter' + filter.expects(:include?).with("%s").returns(true) + filter.expects(:gsub).with("%s", "testing").returns("mynewstring") + Puppet.stubs(:[]).with(:ldapstring).returns(filter) + @searcher.search_filter("testing").should == "mynewstring" + end +end - it "should not add nil parent attributes to the search attributes" do - Puppet.stubs(:[]).with(:ldapattrs).returns("one,two") - @searcher.stubs(:class_attributes).returns([]) - @searcher.stubs(:parent_attribute).returns(nil) - @searcher.search_attributes.should == %w{one two} - end +describe Puppet::Node::Ldap, " when deciding attributes to search for" do + before do + @searcher = Puppet::Node::Ldap.new + end + + it "should use 'nil' if the :ldapattrs setting is 'all'" do + Puppet.stubs(:[]).with(:ldapattrs).returns("all") + @searcher.search_attributes.should be_nil + end + + it "should split the value of :ldapattrs on commas and use the result as the attribute list" do + Puppet.stubs(:[]).with(:ldapattrs).returns("one,two") + @searcher.stubs(:class_attributes).returns([]) + @searcher.stubs(:parent_attribute).returns(nil) + @searcher.search_attributes.should == %w{one two} + end + + it "should add the class attributes to the search attributes if not returning all attributes" do + Puppet.stubs(:[]).with(:ldapattrs).returns("one,two") + @searcher.stubs(:class_attributes).returns(%w{three four}) + @searcher.stubs(:parent_attribute).returns(nil) + # Sort them so i don't have to care about return order + @searcher.search_attributes.sort.should == %w{one two three four}.sort + end + + it "should add the parent attribute to the search attributes if not returning all attributes" do + Puppet.stubs(:[]).with(:ldapattrs).returns("one,two") + @searcher.stubs(:class_attributes).returns([]) + @searcher.stubs(:parent_attribute).returns("parent") + @searcher.search_attributes.sort.should == %w{one two parent}.sort + end + + it "should not add nil parent attributes to the search attributes" do + Puppet.stubs(:[]).with(:ldapattrs).returns("one,two") + @searcher.stubs(:class_attributes).returns([]) + @searcher.stubs(:parent_attribute).returns(nil) + @searcher.search_attributes.should == %w{one two} + end end diff --git a/spec/unit/indirector/node/memory_spec.rb b/spec/unit/indirector/node/memory_spec.rb index 71e01d4f3..3f1fdb9f0 100755 --- a/spec/unit/indirector/node/memory_spec.rb +++ b/spec/unit/indirector/node/memory_spec.rb @@ -7,13 +7,13 @@ require 'puppet/indirector/node/memory' require 'shared_behaviours/memory_terminus' describe Puppet::Node::Memory do - before do - @name = "me" - @searcher = Puppet::Node::Memory.new - @instance = stub 'instance', :name => @name + before do + @name = "me" + @searcher = Puppet::Node::Memory.new + @instance = stub 'instance', :name => @name - @request = stub 'request', :key => @name, :instance => @instance - end + @request = stub 'request', :key => @name, :instance => @instance + end - it_should_behave_like "A Memory Terminus" + it_should_behave_like "A Memory Terminus" end diff --git a/spec/unit/indirector/node/plain_spec.rb b/spec/unit/indirector/node/plain_spec.rb index a595998a1..730086296 100755 --- a/spec/unit/indirector/node/plain_spec.rb +++ b/spec/unit/indirector/node/plain_spec.rb @@ -5,15 +5,15 @@ require File.dirname(__FILE__) + '/../../../spec_helper' require 'puppet/indirector/node/plain' describe Puppet::Node::Plain do - before do - @searcher = Puppet::Node::Plain.new - end + before do + @searcher = Puppet::Node::Plain.new + end - it "should call node_merge() on the returned node" do - node = mock 'node' - Puppet::Node.expects(:new).with("mynode").returns(node) - node.expects(:fact_merge) - request = stub 'request', :key => "mynode" - @searcher.find(request) - end + it "should call node_merge() on the returned node" do + node = mock 'node' + Puppet::Node.expects(:new).with("mynode").returns(node) + node.expects(:fact_merge) + request = stub 'request', :key => "mynode" + @searcher.find(request) + end end diff --git a/spec/unit/indirector/node/rest_spec.rb b/spec/unit/indirector/node/rest_spec.rb index c00bb98d0..0b20752e5 100755 --- a/spec/unit/indirector/node/rest_spec.rb +++ b/spec/unit/indirector/node/rest_spec.rb @@ -5,9 +5,9 @@ require File.dirname(__FILE__) + '/../../../spec_helper' require 'puppet/indirector/node/rest' describe Puppet::Node::Rest do - before do - @searcher = Puppet::Node::Rest.new - end + before do + @searcher = Puppet::Node::Rest.new + end end diff --git a/spec/unit/indirector/node/yaml_spec.rb b/spec/unit/indirector/node/yaml_spec.rb index 2e6c14c71..8e19a1230 100755 --- a/spec/unit/indirector/node/yaml_spec.rb +++ b/spec/unit/indirector/node/yaml_spec.rb @@ -6,20 +6,20 @@ require 'puppet/node' require 'puppet/indirector/node/yaml' describe Puppet::Node::Yaml do - it "should be a subclass of the Yaml terminus" do - Puppet::Node::Yaml.superclass.should equal(Puppet::Indirector::Yaml) - end + it "should be a subclass of the Yaml terminus" do + Puppet::Node::Yaml.superclass.should equal(Puppet::Indirector::Yaml) + end - it "should have documentation" do - Puppet::Node::Yaml.doc.should_not be_nil - end + it "should have documentation" do + Puppet::Node::Yaml.doc.should_not be_nil + end - it "should be registered with the configuration store indirection" do - indirection = Puppet::Indirector::Indirection.instance(:node) - Puppet::Node::Yaml.indirection.should equal(indirection) - end + it "should be registered with the configuration store indirection" do + indirection = Puppet::Indirector::Indirection.instance(:node) + Puppet::Node::Yaml.indirection.should equal(indirection) + end - it "should have its name set to :node" do - Puppet::Node::Yaml.name.should == :yaml - end + it "should have its name set to :node" do + Puppet::Node::Yaml.name.should == :yaml + end end diff --git a/spec/unit/indirector/plain_spec.rb b/spec/unit/indirector/plain_spec.rb index aca2816f2..2fb9e4dd3 100755 --- a/spec/unit/indirector/plain_spec.rb +++ b/spec/unit/indirector/plain_spec.rb @@ -4,26 +4,26 @@ require File.dirname(__FILE__) + '/../../spec_helper' require 'puppet/indirector/plain' describe Puppet::Indirector::Plain do - before do - Puppet::Indirector::Terminus.stubs(:register_terminus_class) - @model = mock 'model' - @indirection = stub 'indirection', :name => :mystuff, :register_terminus_type => nil, :model => @model - Puppet::Indirector::Indirection.stubs(:instance).returns(@indirection) + before do + Puppet::Indirector::Terminus.stubs(:register_terminus_class) + @model = mock 'model' + @indirection = stub 'indirection', :name => :mystuff, :register_terminus_type => nil, :model => @model + Puppet::Indirector::Indirection.stubs(:instance).returns(@indirection) - @plain_class = Class.new(Puppet::Indirector::Plain) do - def self.to_s - "Mystuff::Testing" - end - end + @plain_class = Class.new(Puppet::Indirector::Plain) do + def self.to_s + "Mystuff::Testing" + end + end - @searcher = @plain_class.new + @searcher = @plain_class.new - @request = stub 'request', :key => "yay" - end + @request = stub 'request', :key => "yay" + end - it "should return return an instance of the indirected model" do - object = mock 'object' - @model.expects(:new).with(@request.key).returns object - @searcher.find(@request).should equal(object) - end + it "should return return an instance of the indirected model" do + object = mock 'object' + @model.expects(:new).with(@request.key).returns object + @searcher.find(@request).should equal(object) + end end diff --git a/spec/unit/indirector/queue_spec.rb b/spec/unit/indirector/queue_spec.rb index 0748b5817..83e9c771d 100755 --- a/spec/unit/indirector/queue_spec.rb +++ b/spec/unit/indirector/queue_spec.rb @@ -7,117 +7,117 @@ class Puppet::Indirector::Queue::TestClient end class FooExampleData - attr_accessor :name + attr_accessor :name - def self.pson_create(pson) - new(pson['data'].to_sym) - end + def self.pson_create(pson) + new(pson['data'].to_sym) + end - def initialize(name = nil) - @name = name if name - end + def initialize(name = nil) + @name = name if name + end - def render(format = :pson) - to_pson - end + def render(format = :pson) + to_pson + end - def to_pson(*args) - {:type => self.class.to_s, :data => name}.to_pson(*args) - end + def to_pson(*args) + {:type => self.class.to_s, :data => name}.to_pson(*args) + end end describe Puppet::Indirector::Queue do - confine "PSON library is missing; cannot test queueing" => Puppet.features.pson? - - before :each do - @model = mock 'model' - @indirection = stub 'indirection', :name => :my_queue, :register_terminus_type => nil, :model => @model - Puppet::Indirector::Indirection.stubs(:instance).with(:my_queue).returns(@indirection) - @store_class = Class.new(Puppet::Indirector::Queue) do - def self.to_s - 'MyQueue::MyType' - end - end - @store = @store_class.new - - @subject_class = FooExampleData - @subject = @subject_class.new - @subject.name = :me - - Puppet.settings.stubs(:value).returns("bogus setting data") - Puppet.settings.stubs(:value).with(:queue_type).returns(:test_client) - Puppet::Util::Queue.stubs(:queue_type_to_class).with(:test_client).returns(Puppet::Indirector::Queue::TestClient) - - @request = stub 'request', :key => :me, :instance => @subject + confine "PSON library is missing; cannot test queueing" => Puppet.features.pson? + + before :each do + @model = mock 'model' + @indirection = stub 'indirection', :name => :my_queue, :register_terminus_type => nil, :model => @model + Puppet::Indirector::Indirection.stubs(:instance).with(:my_queue).returns(@indirection) + @store_class = Class.new(Puppet::Indirector::Queue) do + def self.to_s + 'MyQueue::MyType' + end end + @store = @store_class.new - it "should require PSON" do - Puppet.features.expects(:pson?).returns false + @subject_class = FooExampleData + @subject = @subject_class.new + @subject.name = :me - lambda { @store_class.new }.should raise_error(ArgumentError) - end + Puppet.settings.stubs(:value).returns("bogus setting data") + Puppet.settings.stubs(:value).with(:queue_type).returns(:test_client) + Puppet::Util::Queue.stubs(:queue_type_to_class).with(:test_client).returns(Puppet::Indirector::Queue::TestClient) - it 'should use the correct client type and queue' do - @store.queue.should == :my_queue - @store.client.should be_an_instance_of(Puppet::Indirector::Queue::TestClient) - end + @request = stub 'request', :key => :me, :instance => @subject + end + + it "should require PSON" do + Puppet.features.expects(:pson?).returns false - describe "when saving" do - it 'should render the instance using pson' do - @subject.expects(:render).with(:pson) - @store.client.stubs(:send_message) - @store.save(@request) - end + lambda { @store_class.new }.should raise_error(ArgumentError) + end - it "should send the rendered message to the appropriate queue on the client" do - @subject.expects(:render).returns "mypson" + it 'should use the correct client type and queue' do + @store.queue.should == :my_queue + @store.client.should be_an_instance_of(Puppet::Indirector::Queue::TestClient) + end - @store.client.expects(:send_message).with(:my_queue, "mypson") + describe "when saving" do + it 'should render the instance using pson' do + @subject.expects(:render).with(:pson) + @store.client.stubs(:send_message) + @store.save(@request) + end - @store.save(@request) - end + it "should send the rendered message to the appropriate queue on the client" do + @subject.expects(:render).returns "mypson" - it "should catch any exceptions raised" do - @store.client.expects(:send_message).raises ArgumentError + @store.client.expects(:send_message).with(:my_queue, "mypson") - lambda { @store.save(@request) }.should raise_error(Puppet::Error) - end + @store.save(@request) end - describe "when subscribing to the queue" do - before do - @store_class.stubs(:model).returns @model - end + it "should catch any exceptions raised" do + @store.client.expects(:send_message).raises ArgumentError - it "should use the model's Format support to intern the message from pson" do - @model.expects(:convert_from).with(:pson, "mymessage") + lambda { @store.save(@request) }.should raise_error(Puppet::Error) + end + end - @store_class.client.expects(:subscribe).yields("mymessage") - @store_class.subscribe {|o| o } - end + describe "when subscribing to the queue" do + before do + @store_class.stubs(:model).returns @model + end - it "should yield each interned received message" do - @model.stubs(:convert_from).returns "something" + it "should use the model's Format support to intern the message from pson" do + @model.expects(:convert_from).with(:pson, "mymessage") - @subject_two = @subject_class.new - @subject_two.name = :too + @store_class.client.expects(:subscribe).yields("mymessage") + @store_class.subscribe {|o| o } + end - @store_class.client.expects(:subscribe).with(:my_queue).multiple_yields(@subject, @subject_two) + it "should yield each interned received message" do + @model.stubs(:convert_from).returns "something" - received = [] - @store_class.subscribe do |obj| - received.push(obj) - end + @subject_two = @subject_class.new + @subject_two.name = :too - received.should == %w{something something} - end + @store_class.client.expects(:subscribe).with(:my_queue).multiple_yields(@subject, @subject_two) + + received = [] + @store_class.subscribe do |obj| + received.push(obj) + end + + received.should == %w{something something} + end - it "should log but not propagate errors" do - @store_class.client.expects(:subscribe).yields("foo") - @store_class.expects(:intern).raises ArgumentError - Puppet.expects(:err) - @store_class.subscribe {|o| o } - end + it "should log but not propagate errors" do + @store_class.client.expects(:subscribe).yields("foo") + @store_class.expects(:intern).raises ArgumentError + Puppet.expects(:err) + @store_class.subscribe {|o| o } end + end end diff --git a/spec/unit/indirector/report/processor_spec.rb b/spec/unit/indirector/report/processor_spec.rb index 3ee9b1b5e..5602a271f 100755 --- a/spec/unit/indirector/report/processor_spec.rb +++ b/spec/unit/indirector/report/processor_spec.rb @@ -8,74 +8,74 @@ require File.dirname(__FILE__) + '/../../../spec_helper' require 'puppet/indirector/report/processor' describe Puppet::Transaction::Report::Processor do - before do - Puppet.settings.stubs(:use).returns(true) - end + before do + Puppet.settings.stubs(:use).returns(true) + end - it "should provide a method for saving reports" do - Puppet::Transaction::Report::Processor.new.should respond_to(:save) - end + it "should provide a method for saving reports" do + Puppet::Transaction::Report::Processor.new.should respond_to(:save) + end end describe Puppet::Transaction::Report::Processor, " when saving a report" do - before do - Puppet.settings.stubs(:use) - @reporter = Puppet::Transaction::Report::Processor.new - end + before do + Puppet.settings.stubs(:use) + @reporter = Puppet::Transaction::Report::Processor.new + end - it "should not process the report if reports are set to 'none'" do - Puppet::Reports.expects(:report).never - Puppet.settings.expects(:value).with(:reports).returns("none") + it "should not process the report if reports are set to 'none'" do + Puppet::Reports.expects(:report).never + Puppet.settings.expects(:value).with(:reports).returns("none") - request = stub 'request', :instance => mock("report") + request = stub 'request', :instance => mock("report") - @reporter.save(request) - end + @reporter.save(request) + end - it "should process the report with each configured report type" do - Puppet.settings.stubs(:value).with(:reports).returns("one,two") - @reporter.send(:reports).should == %w{one two} - end + it "should process the report with each configured report type" do + Puppet.settings.stubs(:value).with(:reports).returns("one,two") + @reporter.send(:reports).should == %w{one two} + end end describe Puppet::Transaction::Report::Processor, " when processing a report" do - before do - Puppet.settings.stubs(:value).with(:reports).returns("one") - Puppet.settings.stubs(:use) - @reporter = Puppet::Transaction::Report::Processor.new - - @report_type = mock 'one' - @dup_report = mock 'dupe report' - @dup_report.stubs(:process) - @report = mock 'report' - @report.expects(:dup).returns(@dup_report) - - @request = stub 'request', :instance => @report - - Puppet::Reports.expects(:report).with("one").returns(@report_type) - - @dup_report.expects(:extend).with(@report_type) - end - - # LAK:NOTE This is stupid, because the code is so short it doesn't - # make sense to split it out, which means I just do the same test - # three times so the spec looks right. - it "should process a duplicate of the report, not the original" do - @reporter.save(@request) - end - - it "should extend the report with the report type's module" do - @reporter.save(@request) - end - - it "should call the report type's :process method" do - @dup_report.expects(:process) - @reporter.save(@request) - end - - it "should not raise exceptions" do - Puppet.settings.stubs(:value).with(:trace).returns(false) - @dup_report.expects(:process).raises(ArgumentError) - proc { @reporter.save(@request) }.should_not raise_error - end + before do + Puppet.settings.stubs(:value).with(:reports).returns("one") + Puppet.settings.stubs(:use) + @reporter = Puppet::Transaction::Report::Processor.new + + @report_type = mock 'one' + @dup_report = mock 'dupe report' + @dup_report.stubs(:process) + @report = mock 'report' + @report.expects(:dup).returns(@dup_report) + + @request = stub 'request', :instance => @report + + Puppet::Reports.expects(:report).with("one").returns(@report_type) + + @dup_report.expects(:extend).with(@report_type) + end + + # LAK:NOTE This is stupid, because the code is so short it doesn't + # make sense to split it out, which means I just do the same test + # three times so the spec looks right. + it "should process a duplicate of the report, not the original" do + @reporter.save(@request) + end + + it "should extend the report with the report type's module" do + @reporter.save(@request) + end + + it "should call the report type's :process method" do + @dup_report.expects(:process) + @reporter.save(@request) + end + + it "should not raise exceptions" do + Puppet.settings.stubs(:value).with(:trace).returns(false) + @dup_report.expects(:process).raises(ArgumentError) + proc { @reporter.save(@request) }.should_not raise_error + end end diff --git a/spec/unit/indirector/report/rest_spec.rb b/spec/unit/indirector/report/rest_spec.rb index ce6188334..d0d29f862 100755 --- a/spec/unit/indirector/report/rest_spec.rb +++ b/spec/unit/indirector/report/rest_spec.rb @@ -5,24 +5,24 @@ require File.dirname(__FILE__) + '/../../../spec_helper' require 'puppet/indirector/report/rest' describe Puppet::Transaction::Report::Rest do - it "should be a subclass of Puppet::Indirector::REST" do - Puppet::Transaction::Report::Rest.superclass.should equal(Puppet::Indirector::REST) - end + it "should be a subclass of Puppet::Indirector::REST" do + Puppet::Transaction::Report::Rest.superclass.should equal(Puppet::Indirector::REST) + end - it "should use the :report_server setting in preference to :reportserver" do - Puppet.settings[:reportserver] = "reportserver" - Puppet.settings[:report_server] = "report_server" - Puppet::Transaction::Report::Rest.server.should == "report_server" - end + it "should use the :report_server setting in preference to :reportserver" do + Puppet.settings[:reportserver] = "reportserver" + Puppet.settings[:report_server] = "report_server" + Puppet::Transaction::Report::Rest.server.should == "report_server" + end - it "should use the :report_server setting in preference to :server" do - Puppet.settings[:server] = "server" - Puppet.settings[:report_server] = "report_server" - Puppet::Transaction::Report::Rest.server.should == "report_server" - end + it "should use the :report_server setting in preference to :server" do + Puppet.settings[:server] = "server" + Puppet.settings[:report_server] = "report_server" + Puppet::Transaction::Report::Rest.server.should == "report_server" + end - it "should have a value for report_server and report_port" do - Puppet::Transaction::Report::Rest.server.should_not be_nil - Puppet::Transaction::Report::Rest.port.should_not be_nil - end + it "should have a value for report_server and report_port" do + Puppet::Transaction::Report::Rest.server.should_not be_nil + Puppet::Transaction::Report::Rest.port.should_not be_nil + end end diff --git a/spec/unit/indirector/request_spec.rb b/spec/unit/indirector/request_spec.rb index 58226397e..0b3c2c266 100755 --- a/spec/unit/indirector/request_spec.rb +++ b/spec/unit/indirector/request_spec.rb @@ -4,301 +4,301 @@ require File.dirname(__FILE__) + '/../../spec_helper' require 'puppet/indirector/request' describe Puppet::Indirector::Request do - describe "when initializing" do - it "should require an indirection name, a key, and a method" do - lambda { Puppet::Indirector::Request.new }.should raise_error(ArgumentError) - end + describe "when initializing" do + it "should require an indirection name, a key, and a method" do + lambda { Puppet::Indirector::Request.new }.should raise_error(ArgumentError) + end - it "should always convert the indirection name to a symbol" do - Puppet::Indirector::Request.new("ind", :method, "mykey").indirection_name.should == :ind - end + it "should always convert the indirection name to a symbol" do + Puppet::Indirector::Request.new("ind", :method, "mykey").indirection_name.should == :ind + end - it "should use provided value as the key if it is a string" do - Puppet::Indirector::Request.new(:ind, :method, "mykey").key.should == "mykey" - end + it "should use provided value as the key if it is a string" do + Puppet::Indirector::Request.new(:ind, :method, "mykey").key.should == "mykey" + end - it "should use provided value as the key if it is a symbol" do - Puppet::Indirector::Request.new(:ind, :method, :mykey).key.should == :mykey - end + it "should use provided value as the key if it is a symbol" do + Puppet::Indirector::Request.new(:ind, :method, :mykey).key.should == :mykey + end - it "should use the name of the provided instance as its key if an instance is provided as the key instead of a string" do - instance = mock 'instance', :name => "mykey" - request = Puppet::Indirector::Request.new(:ind, :method, instance) - request.key.should == "mykey" - request.instance.should equal(instance) - end + it "should use the name of the provided instance as its key if an instance is provided as the key instead of a string" do + instance = mock 'instance', :name => "mykey" + request = Puppet::Indirector::Request.new(:ind, :method, instance) + request.key.should == "mykey" + request.instance.should equal(instance) + end - it "should support options specified as a hash" do - lambda { Puppet::Indirector::Request.new(:ind, :method, :key, :one => :two) }.should_not raise_error(ArgumentError) - end + it "should support options specified as a hash" do + lambda { Puppet::Indirector::Request.new(:ind, :method, :key, :one => :two) }.should_not raise_error(ArgumentError) + end - it "should support nil options" do - lambda { Puppet::Indirector::Request.new(:ind, :method, :key, nil) }.should_not raise_error(ArgumentError) - end + it "should support nil options" do + lambda { Puppet::Indirector::Request.new(:ind, :method, :key, nil) }.should_not raise_error(ArgumentError) + end - it "should support unspecified options" do - lambda { Puppet::Indirector::Request.new(:ind, :method, :key) }.should_not raise_error(ArgumentError) - end + it "should support unspecified options" do + lambda { Puppet::Indirector::Request.new(:ind, :method, :key) }.should_not raise_error(ArgumentError) + end - it "should use an empty options hash if nil was provided" do - Puppet::Indirector::Request.new(:ind, :method, :key, nil).options.should == {} - end + it "should use an empty options hash if nil was provided" do + Puppet::Indirector::Request.new(:ind, :method, :key, nil).options.should == {} + end - it "should default to a nil node" do - Puppet::Indirector::Request.new(:ind, :method, :key, nil).node.should be_nil - end + it "should default to a nil node" do + Puppet::Indirector::Request.new(:ind, :method, :key, nil).node.should be_nil + end - it "should set its node attribute if provided in the options" do - Puppet::Indirector::Request.new(:ind, :method, :key, :node => "foo.com").node.should == "foo.com" - end + it "should set its node attribute if provided in the options" do + Puppet::Indirector::Request.new(:ind, :method, :key, :node => "foo.com").node.should == "foo.com" + end - it "should default to a nil ip" do - Puppet::Indirector::Request.new(:ind, :method, :key, nil).ip.should be_nil - end + it "should default to a nil ip" do + Puppet::Indirector::Request.new(:ind, :method, :key, nil).ip.should be_nil + end - it "should set its ip attribute if provided in the options" do - Puppet::Indirector::Request.new(:ind, :method, :key, :ip => "192.168.0.1").ip.should == "192.168.0.1" - end + it "should set its ip attribute if provided in the options" do + Puppet::Indirector::Request.new(:ind, :method, :key, :ip => "192.168.0.1").ip.should == "192.168.0.1" + end - it "should default to being unauthenticated" do - Puppet::Indirector::Request.new(:ind, :method, :key, nil).should_not be_authenticated - end + it "should default to being unauthenticated" do + Puppet::Indirector::Request.new(:ind, :method, :key, nil).should_not be_authenticated + end - it "should set be marked authenticated if configured in the options" do - Puppet::Indirector::Request.new(:ind, :method, :key, :authenticated => "eh").should be_authenticated - end + it "should set be marked authenticated if configured in the options" do + Puppet::Indirector::Request.new(:ind, :method, :key, :authenticated => "eh").should be_authenticated + end - it "should keep its options as a hash even if a node is specified" do - Puppet::Indirector::Request.new(:ind, :method, :key, :node => "eh").options.should be_instance_of(Hash) - end + it "should keep its options as a hash even if a node is specified" do + Puppet::Indirector::Request.new(:ind, :method, :key, :node => "eh").options.should be_instance_of(Hash) + end - it "should keep its options as a hash even if another option is specified" do - Puppet::Indirector::Request.new(:ind, :method, :key, :foo => "bar").options.should be_instance_of(Hash) - end + it "should keep its options as a hash even if another option is specified" do + Puppet::Indirector::Request.new(:ind, :method, :key, :foo => "bar").options.should be_instance_of(Hash) + end - it "should treat options other than :ip, :node, and :authenticated as options rather than attributes" do - Puppet::Indirector::Request.new(:ind, :method, :key, :server => "bar").options[:server].should == "bar" - end + it "should treat options other than :ip, :node, and :authenticated as options rather than attributes" do + Puppet::Indirector::Request.new(:ind, :method, :key, :server => "bar").options[:server].should == "bar" + end - it "should normalize options to use symbols as keys" do - Puppet::Indirector::Request.new(:ind, :method, :key, "foo" => "bar").options[:foo].should == "bar" - end + it "should normalize options to use symbols as keys" do + Puppet::Indirector::Request.new(:ind, :method, :key, "foo" => "bar").options[:foo].should == "bar" + end - describe "and the request key is a URI" do - describe "and the URI is a 'file' URI" do - before do - @request = Puppet::Indirector::Request.new(:ind, :method, "file:///my/file with spaces") - end - - it "should set the request key to the unescaped full file path" do - @request.key.should == "/my/file with spaces" - end - - it "should not set the protocol" do - @request.protocol.should be_nil - end - - it "should not set the port" do - @request.port.should be_nil - end - - it "should not set the server" do - @request.server.should be_nil - end - end - - it "should set the protocol to the URI scheme" do - Puppet::Indirector::Request.new(:ind, :method, "http://host/stuff").protocol.should == "http" - end - - it "should set the server if a server is provided" do - Puppet::Indirector::Request.new(:ind, :method, "http://host/stuff").server.should == "host" - end - - it "should set the server and port if both are provided" do - Puppet::Indirector::Request.new(:ind, :method, "http://host:543/stuff").port.should == 543 - end - - it "should default to the masterport if the URI scheme is 'puppet'" do - Puppet.settings.expects(:value).with(:masterport).returns "321" - Puppet::Indirector::Request.new(:ind, :method, "puppet://host/stuff").port.should == 321 - end - - it "should use the provided port if the URI scheme is not 'puppet'" do - Puppet::Indirector::Request.new(:ind, :method, "http://host/stuff").port.should == 80 - end - - it "should set the request key to the unescaped key part path from the URI" do - Puppet::Indirector::Request.new(:ind, :method, "http://host/environment/terminus/stuff with spaces").key.should == "stuff with spaces" - end - - it "should set the :uri attribute to the full URI" do - Puppet::Indirector::Request.new(:ind, :method, "http:///stuff").uri.should == "http:///stuff" - end + describe "and the request key is a URI" do + describe "and the URI is a 'file' URI" do + before do + @request = Puppet::Indirector::Request.new(:ind, :method, "file:///my/file with spaces") end - it "should allow indication that it should not read a cached instance" do - Puppet::Indirector::Request.new(:ind, :method, :key, :ignore_cache => true).should be_ignore_cache + it "should set the request key to the unescaped full file path" do + @request.key.should == "/my/file with spaces" end - it "should default to not ignoring the cache" do - Puppet::Indirector::Request.new(:ind, :method, :key).should_not be_ignore_cache + it "should not set the protocol" do + @request.protocol.should be_nil end - it "should allow indication that it should not not read an instance from the terminus" do - Puppet::Indirector::Request.new(:ind, :method, :key, :ignore_terminus => true).should be_ignore_terminus + it "should not set the port" do + @request.port.should be_nil end - it "should default to not ignoring the terminus" do - Puppet::Indirector::Request.new(:ind, :method, :key).should_not be_ignore_terminus + it "should not set the server" do + @request.server.should be_nil end - end + end - it "should look use the Indirection class to return the appropriate indirection" do - ind = mock 'indirection' - Puppet::Indirector::Indirection.expects(:instance).with(:myind).returns ind - request = Puppet::Indirector::Request.new(:myind, :method, :key) + it "should set the protocol to the URI scheme" do + Puppet::Indirector::Request.new(:ind, :method, "http://host/stuff").protocol.should == "http" + end - request.indirection.should equal(ind) - end + it "should set the server if a server is provided" do + Puppet::Indirector::Request.new(:ind, :method, "http://host/stuff").server.should == "host" + end - it "should use its indirection to look up the appropriate model" do - ind = mock 'indirection' - Puppet::Indirector::Indirection.expects(:instance).with(:myind).returns ind - request = Puppet::Indirector::Request.new(:myind, :method, :key) + it "should set the server and port if both are provided" do + Puppet::Indirector::Request.new(:ind, :method, "http://host:543/stuff").port.should == 543 + end - ind.expects(:model).returns "mymodel" + it "should default to the masterport if the URI scheme is 'puppet'" do + Puppet.settings.expects(:value).with(:masterport).returns "321" + Puppet::Indirector::Request.new(:ind, :method, "puppet://host/stuff").port.should == 321 + end - request.model.should == "mymodel" - end + it "should use the provided port if the URI scheme is not 'puppet'" do + Puppet::Indirector::Request.new(:ind, :method, "http://host/stuff").port.should == 80 + end - it "should fail intelligently when asked to find a model but the indirection cannot be found" do - Puppet::Indirector::Indirection.expects(:instance).with(:myind).returns nil - request = Puppet::Indirector::Request.new(:myind, :method, :key) + it "should set the request key to the unescaped key part path from the URI" do + Puppet::Indirector::Request.new(:ind, :method, "http://host/environment/terminus/stuff with spaces").key.should == "stuff with spaces" + end - lambda { request.model }.should raise_error(ArgumentError) + it "should set the :uri attribute to the full URI" do + Puppet::Indirector::Request.new(:ind, :method, "http:///stuff").uri.should == "http:///stuff" + end end - it "should have a method for determining if the request is plural or singular" do - Puppet::Indirector::Request.new(:myind, :method, :key).should respond_to(:plural?) + it "should allow indication that it should not read a cached instance" do + Puppet::Indirector::Request.new(:ind, :method, :key, :ignore_cache => true).should be_ignore_cache end - it "should be considered plural if the method is 'search'" do - Puppet::Indirector::Request.new(:myind, :search, :key).should be_plural + it "should default to not ignoring the cache" do + Puppet::Indirector::Request.new(:ind, :method, :key).should_not be_ignore_cache end - it "should not be considered plural if the method is not 'search'" do - Puppet::Indirector::Request.new(:myind, :find, :key).should_not be_plural + it "should allow indication that it should not not read an instance from the terminus" do + Puppet::Indirector::Request.new(:ind, :method, :key, :ignore_terminus => true).should be_ignore_terminus end - it "should use its uri, if it has one, as its string representation" do - Puppet::Indirector::Request.new(:myind, :find, "foo://bar/baz").to_s.should == "foo://bar/baz" + it "should default to not ignoring the terminus" do + Puppet::Indirector::Request.new(:ind, :method, :key).should_not be_ignore_terminus end + end - it "should use its indirection name and key, if it has no uri, as its string representation" do - Puppet::Indirector::Request.new(:myind, :find, "key") == "/myind/key" - end + it "should look use the Indirection class to return the appropriate indirection" do + ind = mock 'indirection' + Puppet::Indirector::Indirection.expects(:instance).with(:myind).returns ind + request = Puppet::Indirector::Request.new(:myind, :method, :key) - it "should be able to return the URI-escaped key" do - Puppet::Indirector::Request.new(:myind, :find, "my key").escaped_key.should == URI.escape("my key") - end + request.indirection.should equal(ind) + end - it "should have an environment accessor" do - Puppet::Indirector::Request.new(:myind, :find, "my key", :environment => "foo").should respond_to(:environment) - end + it "should use its indirection to look up the appropriate model" do + ind = mock 'indirection' + Puppet::Indirector::Indirection.expects(:instance).with(:myind).returns ind + request = Puppet::Indirector::Request.new(:myind, :method, :key) - it "should set its environment to an environment instance when a string is specified as its environment" do - Puppet::Indirector::Request.new(:myind, :find, "my key", :environment => "foo").environment.should == Puppet::Node::Environment.new("foo") - end + ind.expects(:model).returns "mymodel" - it "should use any passed in environment instances as its environment" do - env = Puppet::Node::Environment.new("foo") - Puppet::Indirector::Request.new(:myind, :find, "my key", :environment => env).environment.should equal(env) - end + request.model.should == "mymodel" + end - it "should use the default environment when none is provided" do - Puppet::Indirector::Request.new(:myind, :find, "my key" ).environment.should equal(Puppet::Node::Environment.new) - end + it "should fail intelligently when asked to find a model but the indirection cannot be found" do + Puppet::Indirector::Indirection.expects(:instance).with(:myind).returns nil + request = Puppet::Indirector::Request.new(:myind, :method, :key) - it "should support converting its options to a hash" do - Puppet::Indirector::Request.new(:myind, :find, "my key" ).should respond_to(:to_hash) - end + lambda { request.model }.should raise_error(ArgumentError) + end - it "should include all of its attributes when its options are converted to a hash" do - Puppet::Indirector::Request.new(:myind, :find, "my key", :node => 'foo').to_hash[:node].should == 'foo' - end + it "should have a method for determining if the request is plural or singular" do + Puppet::Indirector::Request.new(:myind, :method, :key).should respond_to(:plural?) + end - describe "when building a query string from its options" do - before do - @request = Puppet::Indirector::Request.new(:myind, :find, "my key") - end + it "should be considered plural if the method is 'search'" do + Puppet::Indirector::Request.new(:myind, :search, :key).should be_plural + end - it "should return an empty query string if there are no options" do - @request.stubs(:options).returns nil - @request.query_string.should == "" - end + it "should not be considered plural if the method is not 'search'" do + Puppet::Indirector::Request.new(:myind, :find, :key).should_not be_plural + end - it "should return an empty query string if the options are empty" do - @request.stubs(:options).returns({}) - @request.query_string.should == "" - end + it "should use its uri, if it has one, as its string representation" do + Puppet::Indirector::Request.new(:myind, :find, "foo://bar/baz").to_s.should == "foo://bar/baz" + end - it "should prefix the query string with '?'" do - @request.stubs(:options).returns(:one => "two") - @request.query_string.should =~ /^\?/ - end + it "should use its indirection name and key, if it has no uri, as its string representation" do + Puppet::Indirector::Request.new(:myind, :find, "key") == "/myind/key" + end - it "should include all options in the query string, separated by '&'" do - @request.stubs(:options).returns(:one => "two", :three => "four") - @request.query_string.sub(/^\?/, '').split("&").sort.should == %w{one=two three=four}.sort - end + it "should be able to return the URI-escaped key" do + Puppet::Indirector::Request.new(:myind, :find, "my key").escaped_key.should == URI.escape("my key") + end - it "should ignore nil options" do - @request.stubs(:options).returns(:one => "two", :three => nil) - @request.query_string.should_not be_include("three") - end + it "should have an environment accessor" do + Puppet::Indirector::Request.new(:myind, :find, "my key", :environment => "foo").should respond_to(:environment) + end - it "should convert 'true' option values into strings" do - @request.stubs(:options).returns(:one => true) - @request.query_string.should == "?one=true" - end + it "should set its environment to an environment instance when a string is specified as its environment" do + Puppet::Indirector::Request.new(:myind, :find, "my key", :environment => "foo").environment.should == Puppet::Node::Environment.new("foo") + end - it "should convert 'false' option values into strings" do - @request.stubs(:options).returns(:one => false) - @request.query_string.should == "?one=false" - end + it "should use any passed in environment instances as its environment" do + env = Puppet::Node::Environment.new("foo") + Puppet::Indirector::Request.new(:myind, :find, "my key", :environment => env).environment.should equal(env) + end - it "should convert to a string all option values that are integers" do - @request.stubs(:options).returns(:one => 50) - @request.query_string.should == "?one=50" - end + it "should use the default environment when none is provided" do + Puppet::Indirector::Request.new(:myind, :find, "my key" ).environment.should equal(Puppet::Node::Environment.new) + end - it "should convert to a string all option values that are floating point numbers" do - @request.stubs(:options).returns(:one => 1.2) - @request.query_string.should == "?one=1.2" - end + it "should support converting its options to a hash" do + Puppet::Indirector::Request.new(:myind, :find, "my key" ).should respond_to(:to_hash) + end - it "should CGI-escape all option values that are strings" do - escaping = CGI.escape("one two") - @request.stubs(:options).returns(:one => "one two") - @request.query_string.should == "?one=#{escaping}" - end + it "should include all of its attributes when its options are converted to a hash" do + Puppet::Indirector::Request.new(:myind, :find, "my key", :node => 'foo').to_hash[:node].should == 'foo' + end - it "should YAML-dump and CGI-escape arrays" do - escaping = CGI.escape(YAML.dump(%w{one two})) - @request.stubs(:options).returns(:one => %w{one two}) - @request.query_string.should == "?one=#{escaping}" - end + describe "when building a query string from its options" do + before do + @request = Puppet::Indirector::Request.new(:myind, :find, "my key") + end - it "should convert to a string and CGI-escape all option values that are symbols" do - escaping = CGI.escape("sym bol") - @request.stubs(:options).returns(:one => :"sym bol") - @request.query_string.should == "?one=#{escaping}" - end + it "should return an empty query string if there are no options" do + @request.stubs(:options).returns nil + @request.query_string.should == "" + end - it "should fail if options other than booleans or strings are provided" do - @request.stubs(:options).returns(:one => {:one => :two}) - lambda { @request.query_string }.should raise_error(ArgumentError) - end + it "should return an empty query string if the options are empty" do + @request.stubs(:options).returns({}) + @request.query_string.should == "" + end + + it "should prefix the query string with '?'" do + @request.stubs(:options).returns(:one => "two") + @request.query_string.should =~ /^\?/ + end + + it "should include all options in the query string, separated by '&'" do + @request.stubs(:options).returns(:one => "two", :three => "four") + @request.query_string.sub(/^\?/, '').split("&").sort.should == %w{one=two three=four}.sort + end + + it "should ignore nil options" do + @request.stubs(:options).returns(:one => "two", :three => nil) + @request.query_string.should_not be_include("three") + end + + it "should convert 'true' option values into strings" do + @request.stubs(:options).returns(:one => true) + @request.query_string.should == "?one=true" + end + + it "should convert 'false' option values into strings" do + @request.stubs(:options).returns(:one => false) + @request.query_string.should == "?one=false" + end + + it "should convert to a string all option values that are integers" do + @request.stubs(:options).returns(:one => 50) + @request.query_string.should == "?one=50" + end + + it "should convert to a string all option values that are floating point numbers" do + @request.stubs(:options).returns(:one => 1.2) + @request.query_string.should == "?one=1.2" + end + + it "should CGI-escape all option values that are strings" do + escaping = CGI.escape("one two") + @request.stubs(:options).returns(:one => "one two") + @request.query_string.should == "?one=#{escaping}" + end + + it "should YAML-dump and CGI-escape arrays" do + escaping = CGI.escape(YAML.dump(%w{one two})) + @request.stubs(:options).returns(:one => %w{one two}) + @request.query_string.should == "?one=#{escaping}" + end + + it "should convert to a string and CGI-escape all option values that are symbols" do + escaping = CGI.escape("sym bol") + @request.stubs(:options).returns(:one => :"sym bol") + @request.query_string.should == "?one=#{escaping}" + end + + it "should fail if options other than booleans or strings are provided" do + @request.stubs(:options).returns(:one => {:one => :two}) + lambda { @request.query_string }.should raise_error(ArgumentError) end + end end diff --git a/spec/unit/indirector/resource/ral_spec.rb b/spec/unit/indirector/resource/ral_spec.rb index c3dc8d8cf..a5baef367 100644 --- a/spec/unit/indirector/resource/ral_spec.rb +++ b/spec/unit/indirector/resource/ral_spec.rb @@ -3,127 +3,127 @@ require File.dirname(__FILE__) + '/../../../spec_helper' describe "Puppet::Resource::Ral" do - describe "find" do - before do - @request = stub 'request', :key => "user/root" - end - - it "should find an existing instance" do - my_resource = stub "my user resource" - - wrong_instance = stub "wrong user", :name => "bob" - my_instance = stub "my user", :name => "root", :to_resource => my_resource - - require 'puppet/type/user' - Puppet::Type::User.expects(:instances).returns([ wrong_instance, my_instance, wrong_instance ]) - Puppet::Resource::Ral.new.find(@request).should == my_resource - end - - it "if there is no instance, it should create one" do - wrong_instance = stub "wrong user", :name => "bob" - - require 'puppet/type/user' - Puppet::Type::User.expects(:instances).returns([ wrong_instance, wrong_instance ]) - result = Puppet::Resource::Ral.new.find(@request) - result.should be_is_a(Puppet::Resource) - result.title.should == "root" - end + describe "find" do + before do + @request = stub 'request', :key => "user/root" end - describe "search" do - before do - @request = stub 'request', :key => "user/", :options => {} - end + it "should find an existing instance" do + my_resource = stub "my user resource" - it "should convert ral resources into regular resources" do - my_resource = stub "my user resource" - my_instance = stub "my user", :name => "root", :to_resource => my_resource + wrong_instance = stub "wrong user", :name => "bob" + my_instance = stub "my user", :name => "root", :to_resource => my_resource - require 'puppet/type/user' - Puppet::Type::User.expects(:instances).returns([ my_instance ]) - Puppet::Resource::Ral.new.search(@request).should == [my_resource] - end + require 'puppet/type/user' + Puppet::Type::User.expects(:instances).returns([ wrong_instance, my_instance, wrong_instance ]) + Puppet::Resource::Ral.new.find(@request).should == my_resource + end + + it "if there is no instance, it should create one" do + wrong_instance = stub "wrong user", :name => "bob" + + require 'puppet/type/user' + Puppet::Type::User.expects(:instances).returns([ wrong_instance, wrong_instance ]) + result = Puppet::Resource::Ral.new.find(@request) + result.should be_is_a(Puppet::Resource) + result.title.should == "root" + end + end + + describe "search" do + before do + @request = stub 'request', :key => "user/", :options => {} + end + + it "should convert ral resources into regular resources" do + my_resource = stub "my user resource" + my_instance = stub "my user", :name => "root", :to_resource => my_resource - it "should filter results by name if there's a name in the key" do - my_resource = stub "my user resource" - my_resource.stubs(:to_resource).returns(my_resource) - my_resource.stubs(:[]).with(:name).returns("root") + require 'puppet/type/user' + Puppet::Type::User.expects(:instances).returns([ my_instance ]) + Puppet::Resource::Ral.new.search(@request).should == [my_resource] + end + + it "should filter results by name if there's a name in the key" do + my_resource = stub "my user resource" + my_resource.stubs(:to_resource).returns(my_resource) + my_resource.stubs(:[]).with(:name).returns("root") - wrong_resource = stub "wrong resource" - wrong_resource.stubs(:to_resource).returns(wrong_resource) - wrong_resource.stubs(:[]).with(:name).returns("bad") + wrong_resource = stub "wrong resource" + wrong_resource.stubs(:to_resource).returns(wrong_resource) + wrong_resource.stubs(:[]).with(:name).returns("bad") - my_instance = stub "my user", :to_resource => my_resource - wrong_instance = stub "wrong user", :to_resource => wrong_resource + my_instance = stub "my user", :to_resource => my_resource + wrong_instance = stub "wrong user", :to_resource => wrong_resource - @request = stub 'request', :key => "user/root", :options => {} + @request = stub 'request', :key => "user/root", :options => {} - require 'puppet/type/user' - Puppet::Type::User.expects(:instances).returns([ my_instance, wrong_instance ]) - Puppet::Resource::Ral.new.search(@request).should == [my_resource] - end + require 'puppet/type/user' + Puppet::Type::User.expects(:instances).returns([ my_instance, wrong_instance ]) + Puppet::Resource::Ral.new.search(@request).should == [my_resource] + end - it "should filter results by query parameters" do - wrong_resource = stub "my user resource" - wrong_resource.stubs(:to_resource).returns(wrong_resource) - wrong_resource.stubs(:[]).with(:name).returns("root") + it "should filter results by query parameters" do + wrong_resource = stub "my user resource" + wrong_resource.stubs(:to_resource).returns(wrong_resource) + wrong_resource.stubs(:[]).with(:name).returns("root") - my_resource = stub "wrong resource" - my_resource.stubs(:to_resource).returns(my_resource) - my_resource.stubs(:[]).with(:name).returns("bob") + my_resource = stub "wrong resource" + my_resource.stubs(:to_resource).returns(my_resource) + my_resource.stubs(:[]).with(:name).returns("bob") - my_instance = stub "my user", :to_resource => my_resource - wrong_instance = stub "wrong user", :to_resource => wrong_resource + my_instance = stub "my user", :to_resource => my_resource + wrong_instance = stub "wrong user", :to_resource => wrong_resource - @request = stub 'request', :key => "user/", :options => {:name => "bob"} + @request = stub 'request', :key => "user/", :options => {:name => "bob"} - require 'puppet/type/user' - Puppet::Type::User.expects(:instances).returns([ my_instance, wrong_instance ]) - Puppet::Resource::Ral.new.search(@request).should == [my_resource] - end + require 'puppet/type/user' + Puppet::Type::User.expects(:instances).returns([ my_instance, wrong_instance ]) + Puppet::Resource::Ral.new.search(@request).should == [my_resource] + end - it "should return sorted results" do - a_resource = stub "alice resource" - a_resource.stubs(:to_resource).returns(a_resource) - a_resource.stubs(:title).returns("alice") + it "should return sorted results" do + a_resource = stub "alice resource" + a_resource.stubs(:to_resource).returns(a_resource) + a_resource.stubs(:title).returns("alice") - b_resource = stub "bob resource" - b_resource.stubs(:to_resource).returns(b_resource) - b_resource.stubs(:title).returns("bob") + b_resource = stub "bob resource" + b_resource.stubs(:to_resource).returns(b_resource) + b_resource.stubs(:title).returns("bob") - a_instance = stub "alice user", :to_resource => a_resource - b_instance = stub "bob user", :to_resource => b_resource + a_instance = stub "alice user", :to_resource => a_resource + b_instance = stub "bob user", :to_resource => b_resource - @request = stub 'request', :key => "user/", :options => {} + @request = stub 'request', :key => "user/", :options => {} + + require 'puppet/type/user' + Puppet::Type::User.expects(:instances).returns([ b_instance, a_instance ]) + Puppet::Resource::Ral.new.search(@request).should == [a_resource, b_resource] + end + end + + describe "save" do + before do + @rebuilt_res = stub 'rebuilt instance' + @ral_res = stub 'ral resource', :to_resource => @rebuilt_res + @instance = stub 'instance', :to_ral => @ral_res + @request = stub 'request', :key => "user/", :instance => @instance + @catalog = stub 'catalog' + + Puppet::Resource::Catalog.stubs(:new).returns(@catalog) + @catalog.stubs(:apply) + @catalog.stubs(:add_resource) + end - require 'puppet/type/user' - Puppet::Type::User.expects(:instances).returns([ b_instance, a_instance ]) - Puppet::Resource::Ral.new.search(@request).should == [a_resource, b_resource] - end + it "should apply a new catalog with a ral object in it" do + Puppet::Resource::Catalog.expects(:new).returns(@catalog) + @catalog.expects(:add_resource).with(@ral_res) + @catalog.expects(:apply) + Puppet::Resource::Ral.new.save(@request) end - describe "save" do - before do - @rebuilt_res = stub 'rebuilt instance' - @ral_res = stub 'ral resource', :to_resource => @rebuilt_res - @instance = stub 'instance', :to_ral => @ral_res - @request = stub 'request', :key => "user/", :instance => @instance - @catalog = stub 'catalog' - - Puppet::Resource::Catalog.stubs(:new).returns(@catalog) - @catalog.stubs(:apply) - @catalog.stubs(:add_resource) - end - - it "should apply a new catalog with a ral object in it" do - Puppet::Resource::Catalog.expects(:new).returns(@catalog) - @catalog.expects(:add_resource).with(@ral_res) - @catalog.expects(:apply) - Puppet::Resource::Ral.new.save(@request) - end - - it "should return a regular resource that used to be the ral resource" do - Puppet::Resource::Ral.new.save(@request).should == @rebuilt_res - end + it "should return a regular resource that used to be the ral resource" do + Puppet::Resource::Ral.new.save(@request).should == @rebuilt_res end + end end diff --git a/spec/unit/indirector/resource/rest_spec.rb b/spec/unit/indirector/resource/rest_spec.rb index d5f2a9d46..351aee33b 100755 --- a/spec/unit/indirector/resource/rest_spec.rb +++ b/spec/unit/indirector/resource/rest_spec.rb @@ -5,7 +5,7 @@ Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f require 'puppet/indirector/resource/rest' describe Puppet::Resource::Rest do - it "should be a sublcass of Puppet::Indirector::REST" do - Puppet::Resource::Rest.superclass.should equal(Puppet::Indirector::REST) - end + it "should be a sublcass of Puppet::Indirector::REST" do + Puppet::Resource::Rest.superclass.should equal(Puppet::Indirector::REST) + end end diff --git a/spec/unit/indirector/resource_type/parser_spec.rb b/spec/unit/indirector/resource_type/parser_spec.rb index 331ca1c9b..5c0d9e04c 100755 --- a/spec/unit/indirector/resource_type/parser_spec.rb +++ b/spec/unit/indirector/resource_type/parser_spec.rb @@ -5,71 +5,71 @@ require File.dirname(__FILE__) + '/../../../spec_helper' require 'puppet/indirector/resource_type/parser' describe Puppet::Indirector::ResourceType::Parser do - before do - @terminus = Puppet::Indirector::ResourceType::Parser.new - @request = Puppet::Indirector::Request.new(:resource_type, :find, "foo") - @krt = Puppet::Resource::TypeCollection.new(@request.environment) - @request.environment.stubs(:known_resource_types).returns @krt - end - - it "should be registered with the resource_type indirection" do - Puppet::Indirector::Terminus.terminus_class(:resource_type, :parser).should equal(Puppet::Indirector::ResourceType::Parser) + before do + @terminus = Puppet::Indirector::ResourceType::Parser.new + @request = Puppet::Indirector::Request.new(:resource_type, :find, "foo") + @krt = Puppet::Resource::TypeCollection.new(@request.environment) + @request.environment.stubs(:known_resource_types).returns @krt + end + + it "should be registered with the resource_type indirection" do + Puppet::Indirector::Terminus.terminus_class(:resource_type, :parser).should equal(Puppet::Indirector::ResourceType::Parser) + end + + describe "when finding" do + it "should use the request's environment's list of known resource types" do + @request.environment.known_resource_types.expects(:hostclass).returns nil + + @terminus.find(@request) end - describe "when finding" do - it "should use the request's environment's list of known resource types" do - @request.environment.known_resource_types.expects(:hostclass).returns nil + it "should return any found type" do + type = @krt.add(Puppet::Resource::Type.new(:hostclass, "foo")) - @terminus.find(@request) - end - - it "should return any found type" do - type = @krt.add(Puppet::Resource::Type.new(:hostclass, "foo")) - - @terminus.find(@request).should == type - end + @terminus.find(@request).should == type + end - it "should return nil if no type can be found" do - @terminus.find(@request).should be_nil - end + it "should return nil if no type can be found" do + @terminus.find(@request).should be_nil + end - it "should prefer definitions to nodes" do - type = @krt.add(Puppet::Resource::Type.new(:hostclass, "foo")) - node = @krt.add(Puppet::Resource::Type.new(:node, "foo")) + it "should prefer definitions to nodes" do + type = @krt.add(Puppet::Resource::Type.new(:hostclass, "foo")) + node = @krt.add(Puppet::Resource::Type.new(:node, "foo")) - @terminus.find(@request).should == type - end + @terminus.find(@request).should == type end + end - describe "when searching" do - before do - @request.key = "*" - end + describe "when searching" do + before do + @request.key = "*" + end - it "should use the request's environment's list of known resource types" do - @request.environment.known_resource_types.expects(:hostclasses).returns({}) + it "should use the request's environment's list of known resource types" do + @request.environment.known_resource_types.expects(:hostclasses).returns({}) - @terminus.search(@request) - end + @terminus.search(@request) + end - it "should fail if anyther other than '*' was provided as the search key" do - @request.key = "foo*" - lambda { @terminus.search(@request) }.should raise_error(ArgumentError) - end + it "should fail if anyther other than '*' was provided as the search key" do + @request.key = "foo*" + lambda { @terminus.search(@request) }.should raise_error(ArgumentError) + end - it "should return all known types" do - type = @krt.add(Puppet::Resource::Type.new(:hostclass, "foo")) - node = @krt.add(Puppet::Resource::Type.new(:node, "bar")) - define = @krt.add(Puppet::Resource::Type.new(:definition, "baz")) + it "should return all known types" do + type = @krt.add(Puppet::Resource::Type.new(:hostclass, "foo")) + node = @krt.add(Puppet::Resource::Type.new(:node, "bar")) + define = @krt.add(Puppet::Resource::Type.new(:definition, "baz")) - result = @terminus.search(@request) - result.should be_include(type) - result.should be_include(node) - result.should be_include(define) - end + result = @terminus.search(@request) + result.should be_include(type) + result.should be_include(node) + result.should be_include(define) + end - it "should return nil if no types can be found" do - @terminus.search(@request).should be_nil - end + it "should return nil if no types can be found" do + @terminus.search(@request).should be_nil end + end end diff --git a/spec/unit/indirector/resource_type/rest_spec.rb b/spec/unit/indirector/resource_type/rest_spec.rb index 28196d53a..2486502bc 100755 --- a/spec/unit/indirector/resource_type/rest_spec.rb +++ b/spec/unit/indirector/resource_type/rest_spec.rb @@ -5,11 +5,11 @@ require File.dirname(__FILE__) + '/../../../spec_helper' require 'puppet/indirector/resource_type/rest' describe Puppet::Indirector::ResourceType::Rest do - it "should be registered with the resource_type indirection" do - Puppet::Indirector::Terminus.terminus_class(:resource_type, :rest).should equal(Puppet::Indirector::ResourceType::Rest) - end + it "should be registered with the resource_type indirection" do + Puppet::Indirector::Terminus.terminus_class(:resource_type, :rest).should equal(Puppet::Indirector::ResourceType::Rest) + end - it "should be a subclass of Puppet::Indirector::Rest" do - Puppet::Indirector::ResourceType::Rest.superclass.should == Puppet::Indirector::REST - end + it "should be a subclass of Puppet::Indirector::Rest" do + Puppet::Indirector::ResourceType::Rest.superclass.should == Puppet::Indirector::REST + end end diff --git a/spec/unit/indirector/rest_spec.rb b/spec/unit/indirector/rest_spec.rb index 668158c53..3efbfce97 100755 --- a/spec/unit/indirector/rest_spec.rb +++ b/spec/unit/indirector/rest_spec.rb @@ -4,452 +4,452 @@ require File.dirname(__FILE__) + '/../../spec_helper' require 'puppet/indirector/rest' describe "a REST http call", :shared => true do - it "should accept a path" do - lambda { @search.send(@method, *@arguments) }.should_not raise_error(ArgumentError) - end + it "should accept a path" do + lambda { @search.send(@method, *@arguments) }.should_not raise_error(ArgumentError) + end - it "should require a path" do - lambda { @searcher.send(@method) }.should raise_error(ArgumentError) - end + it "should require a path" do + lambda { @searcher.send(@method) }.should raise_error(ArgumentError) + end - it "should return the results of deserializing the response to the request" do - conn = mock 'connection' - conn.stubs(:put).returns @response - conn.stubs(:delete).returns @response - conn.stubs(:get).returns @response - Puppet::Network::HttpPool.stubs(:http_instance).returns conn + it "should return the results of deserializing the response to the request" do + conn = mock 'connection' + conn.stubs(:put).returns @response + conn.stubs(:delete).returns @response + conn.stubs(:get).returns @response + Puppet::Network::HttpPool.stubs(:http_instance).returns conn - @searcher.expects(:deserialize).with(@response).returns "myobject" + @searcher.expects(:deserialize).with(@response).returns "myobject" - @searcher.send(@method, *@arguments).should == 'myobject' - end + @searcher.send(@method, *@arguments).should == 'myobject' + end end describe Puppet::Indirector::REST do - before do - Puppet::Indirector::Terminus.stubs(:register_terminus_class) - @model = stub('model', :supported_formats => %w{}, :convert_from => nil) - @instance = stub('model instance', :name= => nil) - @indirection = stub('indirection', :name => :mystuff, :register_terminus_type => nil, :model => @model) - Puppet::Indirector::Indirection.stubs(:instance).returns(@indirection) - - @rest_class = Class.new(Puppet::Indirector::REST) do - def self.to_s - "This::Is::A::Test::Class" - end - end - - @response = stub('mock response', :body => 'result', :code => "200") - @response.stubs(:[]).with('content-type').returns "text/plain" - @response.stubs(:[]).with('content-encoding').returns nil - - @searcher = @rest_class.new - @searcher.stubs(:model).returns @model + before do + Puppet::Indirector::Terminus.stubs(:register_terminus_class) + @model = stub('model', :supported_formats => %w{}, :convert_from => nil) + @instance = stub('model instance', :name= => nil) + @indirection = stub('indirection', :name => :mystuff, :register_terminus_type => nil, :model => @model) + Puppet::Indirector::Indirection.stubs(:instance).returns(@indirection) + + @rest_class = Class.new(Puppet::Indirector::REST) do + def self.to_s + "This::Is::A::Test::Class" + end end - it "should include the v1 REST API module" do - Puppet::Indirector::REST.ancestors.should be_include(Puppet::Network::HTTP::API::V1) + @response = stub('mock response', :body => 'result', :code => "200") + @response.stubs(:[]).with('content-type').returns "text/plain" + @response.stubs(:[]).with('content-encoding').returns nil + + @searcher = @rest_class.new + @searcher.stubs(:model).returns @model + end + + it "should include the v1 REST API module" do + Puppet::Indirector::REST.ancestors.should be_include(Puppet::Network::HTTP::API::V1) + end + + it "should have a method for specifying what setting a subclass should use to retrieve its server" do + @rest_class.should respond_to(:use_server_setting) + end + + it "should use any specified setting to pick the server" do + @rest_class.expects(:server_setting).returns :servset + Puppet.settings.expects(:value).with(:servset).returns "myserver" + @rest_class.server.should == "myserver" + end + + it "should default to :server for the server setting" do + @rest_class.expects(:server_setting).returns nil + Puppet.settings.expects(:value).with(:server).returns "myserver" + @rest_class.server.should == "myserver" + end + + it "should have a method for specifying what setting a subclass should use to retrieve its port" do + @rest_class.should respond_to(:use_port_setting) + end + + it "should use any specified setting to pick the port" do + @rest_class.expects(:port_setting).returns :servset + Puppet.settings.expects(:value).with(:servset).returns "321" + @rest_class.port.should == 321 + end + + it "should default to :port for the port setting" do + @rest_class.expects(:port_setting).returns nil + Puppet.settings.expects(:value).with(:masterport).returns "543" + @rest_class.port.should == 543 + end + + describe "when deserializing responses" do + it "should return nil if the response code is 404" do + response = mock 'response' + response.expects(:code).returns "404" + + @searcher.deserialize(response).should be_nil end - it "should have a method for specifying what setting a subclass should use to retrieve its server" do - @rest_class.should respond_to(:use_server_setting) - end - - it "should use any specified setting to pick the server" do - @rest_class.expects(:server_setting).returns :servset - Puppet.settings.expects(:value).with(:servset).returns "myserver" - @rest_class.server.should == "myserver" - end - - it "should default to :server for the server setting" do - @rest_class.expects(:server_setting).returns nil - Puppet.settings.expects(:value).with(:server).returns "myserver" - @rest_class.server.should == "myserver" - end + [300,400,403,405,500,501,502,503,504].each { |rc| + describe "when the response code is #{rc}" do + before :each do + @model.expects(:convert_from).never - it "should have a method for specifying what setting a subclass should use to retrieve its port" do - @rest_class.should respond_to(:use_port_setting) - end + @response = mock 'response' + @response.stubs(:code).returns rc.to_s + @response.stubs(:[]).with('content-encoding').returns nil + @response.stubs(:message).returns "There was a problem (header)" + end - it "should use any specified setting to pick the port" do - @rest_class.expects(:port_setting).returns :servset - Puppet.settings.expects(:value).with(:servset).returns "321" - @rest_class.port.should == 321 - end + it "should fail" do + @response.stubs(:body).returns nil + lambda { @searcher.deserialize(@response) }.should raise_error(Net::HTTPError) + end - it "should default to :port for the port setting" do - @rest_class.expects(:port_setting).returns nil - Puppet.settings.expects(:value).with(:masterport).returns "543" - @rest_class.port.should == 543 - end + it "should take the error message from the body, if present" do + @response.stubs(:body).returns "There was a problem (body)" + lambda { @searcher.deserialize(@response) }.should raise_error(Net::HTTPError,"Error #{rc} on SERVER: There was a problem (body)") + end - describe "when deserializing responses" do - it "should return nil if the response code is 404" do - response = mock 'response' - response.expects(:code).returns "404" + it "should take the error message from the response header if the body is empty" do + @response.stubs(:body).returns "" + lambda { @searcher.deserialize(@response) }.should raise_error(Net::HTTPError,"Error #{rc} on SERVER: There was a problem (header)") + end - @searcher.deserialize(response).should be_nil + it "should take the error message from the response header if the body is absent" do + @response.stubs(:body).returns nil + lambda { @searcher.deserialize(@response) }.should raise_error(Net::HTTPError,"Error #{rc} on SERVER: There was a problem (header)") end - [300,400,403,405,500,501,502,503,504].each { |rc| - describe "when the response code is #{rc}" do - before :each do - @model.expects(:convert_from).never - - @response = mock 'response' - @response.stubs(:code).returns rc.to_s - @response.stubs(:[]).with('content-encoding').returns nil - @response.stubs(:message).returns "There was a problem (header)" - end - - it "should fail" do - @response.stubs(:body).returns nil - lambda { @searcher.deserialize(@response) }.should raise_error(Net::HTTPError) - end - - it "should take the error message from the body, if present" do - @response.stubs(:body).returns "There was a problem (body)" - lambda { @searcher.deserialize(@response) }.should raise_error(Net::HTTPError,"Error #{rc} on SERVER: There was a problem (body)") - end - - it "should take the error message from the response header if the body is empty" do - @response.stubs(:body).returns "" - lambda { @searcher.deserialize(@response) }.should raise_error(Net::HTTPError,"Error #{rc} on SERVER: There was a problem (header)") - end - - it "should take the error message from the response header if the body is absent" do - @response.stubs(:body).returns nil - lambda { @searcher.deserialize(@response) }.should raise_error(Net::HTTPError,"Error #{rc} on SERVER: There was a problem (header)") - end - - describe "and with http compression" do - it "should uncompress the body" do - @response.stubs(:body).returns("compressed body") - @searcher.expects(:uncompress_body).with(@response).returns("uncompressed") - lambda { @searcher.deserialize(@response) }.should raise_error { |e| e.message =~ /uncompressed/ } - end - end - end - } - - it "should return the results of converting from the format specified by the content-type header if the response code is in the 200s" do - @model.expects(:convert_from).with("myformat", "mydata").returns "myobject" - - response = mock 'response' - response.stubs(:[]).with("content-type").returns "myformat" - response.stubs(:[]).with("content-encoding").returns nil - response.stubs(:body).returns "mydata" - response.stubs(:code).returns "200" - - @searcher.deserialize(response).should == "myobject" + describe "and with http compression" do + it "should uncompress the body" do + @response.stubs(:body).returns("compressed body") + @searcher.expects(:uncompress_body).with(@response).returns("uncompressed") + lambda { @searcher.deserialize(@response) }.should raise_error { |e| e.message =~ /uncompressed/ } + end end + end + } - it "should convert and return multiple instances if the return code is in the 200s and 'multiple' is specified" do - @model.expects(:convert_from_multiple).with("myformat", "mydata").returns "myobjects" + it "should return the results of converting from the format specified by the content-type header if the response code is in the 200s" do + @model.expects(:convert_from).with("myformat", "mydata").returns "myobject" - response = mock 'response' - response.stubs(:[]).with("content-type").returns "myformat" - response.stubs(:[]).with("content-encoding").returns nil - response.stubs(:body).returns "mydata" - response.stubs(:code).returns "200" + response = mock 'response' + response.stubs(:[]).with("content-type").returns "myformat" + response.stubs(:[]).with("content-encoding").returns nil + response.stubs(:body).returns "mydata" + response.stubs(:code).returns "200" - @searcher.deserialize(response, true).should == "myobjects" - end + @searcher.deserialize(response).should == "myobject" + end - it "should strip the content-type header to keep only the mime-type" do - @model.expects(:convert_from).with("text/plain", "mydata").returns "myobject" + it "should convert and return multiple instances if the return code is in the 200s and 'multiple' is specified" do + @model.expects(:convert_from_multiple).with("myformat", "mydata").returns "myobjects" - response = mock 'response' - response.stubs(:[]).with("content-type").returns "text/plain; charset=utf-8" - response.stubs(:[]).with("content-encoding").returns nil - response.stubs(:body).returns "mydata" - response.stubs(:code).returns "200" + response = mock 'response' + response.stubs(:[]).with("content-type").returns "myformat" + response.stubs(:[]).with("content-encoding").returns nil + response.stubs(:body).returns "mydata" + response.stubs(:code).returns "200" - @searcher.deserialize(response) - end + @searcher.deserialize(response, true).should == "myobjects" + end - it "should uncompress the body" do - @model.expects(:convert_from).with("myformat", "uncompressed mydata").returns "myobject" + it "should strip the content-type header to keep only the mime-type" do + @model.expects(:convert_from).with("text/plain", "mydata").returns "myobject" - response = mock 'response' - response.stubs(:[]).with("content-type").returns "myformat" - response.stubs(:body).returns "compressed mydata" - response.stubs(:code).returns "200" + response = mock 'response' + response.stubs(:[]).with("content-type").returns "text/plain; charset=utf-8" + response.stubs(:[]).with("content-encoding").returns nil + response.stubs(:body).returns "mydata" + response.stubs(:code).returns "200" - @searcher.expects(:uncompress_body).with(response).returns("uncompressed mydata") + @searcher.deserialize(response) + end - @searcher.deserialize(response).should == "myobject" - end + it "should uncompress the body" do + @model.expects(:convert_from).with("myformat", "uncompressed mydata").returns "myobject" + + response = mock 'response' + response.stubs(:[]).with("content-type").returns "myformat" + response.stubs(:body).returns "compressed mydata" + response.stubs(:code).returns "200" + + @searcher.expects(:uncompress_body).with(response).returns("uncompressed mydata") + + @searcher.deserialize(response).should == "myobject" end + end - describe "when creating an HTTP client" do - before do - Puppet.settings.stubs(:value).returns("rest_testing") - end + describe "when creating an HTTP client" do + before do + Puppet.settings.stubs(:value).returns("rest_testing") + end - it "should use the class's server and port if the indirection request provides neither" do - @request = stub 'request', :key => "foo", :server => nil, :port => nil - @searcher.class.expects(:port).returns 321 - @searcher.class.expects(:server).returns "myserver" - Puppet::Network::HttpPool.expects(:http_instance).with("myserver", 321).returns "myconn" - @searcher.network(@request).should == "myconn" - end + it "should use the class's server and port if the indirection request provides neither" do + @request = stub 'request', :key => "foo", :server => nil, :port => nil + @searcher.class.expects(:port).returns 321 + @searcher.class.expects(:server).returns "myserver" + Puppet::Network::HttpPool.expects(:http_instance).with("myserver", 321).returns "myconn" + @searcher.network(@request).should == "myconn" + end - it "should use the server from the indirection request if one is present" do - @request = stub 'request', :key => "foo", :server => "myserver", :port => nil - @searcher.class.stubs(:port).returns 321 - Puppet::Network::HttpPool.expects(:http_instance).with("myserver", 321).returns "myconn" - @searcher.network(@request).should == "myconn" - end + it "should use the server from the indirection request if one is present" do + @request = stub 'request', :key => "foo", :server => "myserver", :port => nil + @searcher.class.stubs(:port).returns 321 + Puppet::Network::HttpPool.expects(:http_instance).with("myserver", 321).returns "myconn" + @searcher.network(@request).should == "myconn" + end - it "should use the port from the indirection request if one is present" do - @request = stub 'request', :key => "foo", :server => nil, :port => 321 - @searcher.class.stubs(:server).returns "myserver" - Puppet::Network::HttpPool.expects(:http_instance).with("myserver", 321).returns "myconn" - @searcher.network(@request).should == "myconn" - end + it "should use the port from the indirection request if one is present" do + @request = stub 'request', :key => "foo", :server => nil, :port => 321 + @searcher.class.stubs(:server).returns "myserver" + Puppet::Network::HttpPool.expects(:http_instance).with("myserver", 321).returns "myconn" + @searcher.network(@request).should == "myconn" end + end - describe "when doing a find" do - before :each do - @connection = stub('mock http connection', :get => @response) - @searcher.stubs(:network).returns(@connection) # neuter the network connection + describe "when doing a find" do + before :each do + @connection = stub('mock http connection', :get => @response) + @searcher.stubs(:network).returns(@connection) # neuter the network connection - # Use a key with spaces, so we can test escaping - @request = Puppet::Indirector::Request.new(:foo, :find, "foo bar") - end + # Use a key with spaces, so we can test escaping + @request = Puppet::Indirector::Request.new(:foo, :find, "foo bar") + end - it "should call the GET http method on a network connection" do - @searcher.expects(:network).returns @connection - @connection.expects(:get).returns @response - @searcher.find(@request) - end + it "should call the GET http method on a network connection" do + @searcher.expects(:network).returns @connection + @connection.expects(:get).returns @response + @searcher.find(@request) + end - it "should deserialize and return the http response, setting name" do - @connection.expects(:get).returns @response + it "should deserialize and return the http response, setting name" do + @connection.expects(:get).returns @response - instance = stub 'object' - instance.expects(:name=) - @searcher.expects(:deserialize).with(@response).returns instance + instance = stub 'object' + instance.expects(:name=) + @searcher.expects(:deserialize).with(@response).returns instance - @searcher.find(@request).should == instance - end + @searcher.find(@request).should == instance + end - it "should deserialize and return the http response, and not require name=" do - @connection.expects(:get).returns @response + it "should deserialize and return the http response, and not require name=" do + @connection.expects(:get).returns @response - instance = stub 'object' - @searcher.expects(:deserialize).with(@response).returns instance + instance = stub 'object' + @searcher.expects(:deserialize).with(@response).returns instance - @searcher.find(@request).should == instance - end + @searcher.find(@request).should == instance + end - it "should use the URI generated by the Handler module" do - @searcher.expects(:indirection2uri).with(@request).returns "/my/uri" - @connection.expects(:get).with { |path, args| path == "/my/uri" }.returns(@response) - @searcher.find(@request) - end + it "should use the URI generated by the Handler module" do + @searcher.expects(:indirection2uri).with(@request).returns "/my/uri" + @connection.expects(:get).with { |path, args| path == "/my/uri" }.returns(@response) + @searcher.find(@request) + end - it "should provide an Accept header containing the list of supported formats joined with commas" do - @connection.expects(:get).with { |path, args| args["Accept"] == "supported, formats" }.returns(@response) + it "should provide an Accept header containing the list of supported formats joined with commas" do + @connection.expects(:get).with { |path, args| args["Accept"] == "supported, formats" }.returns(@response) - @searcher.model.expects(:supported_formats).returns %w{supported formats} - @searcher.find(@request) - end + @searcher.model.expects(:supported_formats).returns %w{supported formats} + @searcher.find(@request) + end - it "should add Accept-Encoding header" do - @searcher.expects(:add_accept_encoding).returns({"accept-encoding" => "gzip"}) + it "should add Accept-Encoding header" do + @searcher.expects(:add_accept_encoding).returns({"accept-encoding" => "gzip"}) - @connection.expects(:get).with { |path, args| args["accept-encoding"] == "gzip" }.returns(@response) - @searcher.find(@request) - end + @connection.expects(:get).with { |path, args| args["accept-encoding"] == "gzip" }.returns(@response) + @searcher.find(@request) + end - it "should deserialize and return the network response" do - @searcher.expects(:deserialize).with(@response).returns @instance - @searcher.find(@request).should equal(@instance) - end + it "should deserialize and return the network response" do + @searcher.expects(:deserialize).with(@response).returns @instance + @searcher.find(@request).should equal(@instance) + end - it "should set the name of the resulting instance to the asked-for name" do - @searcher.expects(:deserialize).with(@response).returns @instance - @instance.expects(:name=).with "foo bar" - @searcher.find(@request) - end + it "should set the name of the resulting instance to the asked-for name" do + @searcher.expects(:deserialize).with(@response).returns @instance + @instance.expects(:name=).with "foo bar" + @searcher.find(@request) + end - it "should generate an error when result data deserializes fails" do - @searcher.expects(:deserialize).raises(ArgumentError) - lambda { @searcher.find(@request) }.should raise_error(ArgumentError) - end + it "should generate an error when result data deserializes fails" do + @searcher.expects(:deserialize).raises(ArgumentError) + lambda { @searcher.find(@request) }.should raise_error(ArgumentError) end + end - describe "when doing a search" do - before :each do - @connection = stub('mock http connection', :get => @response) - @searcher.stubs(:network).returns(@connection) # neuter the network connection + describe "when doing a search" do + before :each do + @connection = stub('mock http connection', :get => @response) + @searcher.stubs(:network).returns(@connection) # neuter the network connection - @model.stubs(:convert_from_multiple) + @model.stubs(:convert_from_multiple) - @request = Puppet::Indirector::Request.new(:foo, :search, "foo bar") - end + @request = Puppet::Indirector::Request.new(:foo, :search, "foo bar") + end - it "should call the GET http method on a network connection" do - @searcher.expects(:network).returns @connection - @connection.expects(:get).returns @response - @searcher.search(@request) - end + it "should call the GET http method on a network connection" do + @searcher.expects(:network).returns @connection + @connection.expects(:get).returns @response + @searcher.search(@request) + end - it "should deserialize as multiple instances and return the http response" do - @connection.expects(:get).returns @response - @searcher.expects(:deserialize).with(@response, true).returns "myobject" + it "should deserialize as multiple instances and return the http response" do + @connection.expects(:get).returns @response + @searcher.expects(:deserialize).with(@response, true).returns "myobject" - @searcher.search(@request).should == 'myobject' - end + @searcher.search(@request).should == 'myobject' + end - it "should use the URI generated by the Handler module" do - @searcher.expects(:indirection2uri).with(@request).returns "/mys/uri" - @connection.expects(:get).with { |path, args| path == "/mys/uri" }.returns(@response) - @searcher.search(@request) - end + it "should use the URI generated by the Handler module" do + @searcher.expects(:indirection2uri).with(@request).returns "/mys/uri" + @connection.expects(:get).with { |path, args| path == "/mys/uri" }.returns(@response) + @searcher.search(@request) + end - it "should provide an Accept header containing the list of supported formats joined with commas" do - @connection.expects(:get).with { |path, args| args["Accept"] == "supported, formats" }.returns(@response) + it "should provide an Accept header containing the list of supported formats joined with commas" do + @connection.expects(:get).with { |path, args| args["Accept"] == "supported, formats" }.returns(@response) - @searcher.model.expects(:supported_formats).returns %w{supported formats} - @searcher.search(@request) - end + @searcher.model.expects(:supported_formats).returns %w{supported formats} + @searcher.search(@request) + end - it "should return an empty array if serialization returns nil" do - @model.stubs(:convert_from_multiple).returns nil + it "should return an empty array if serialization returns nil" do + @model.stubs(:convert_from_multiple).returns nil - @searcher.search(@request).should == [] - end + @searcher.search(@request).should == [] + end - it "should generate an error when result data deserializes fails" do - @searcher.expects(:deserialize).raises(ArgumentError) - lambda { @searcher.search(@request) }.should raise_error(ArgumentError) - end + it "should generate an error when result data deserializes fails" do + @searcher.expects(:deserialize).raises(ArgumentError) + lambda { @searcher.search(@request) }.should raise_error(ArgumentError) end + end - describe "when doing a destroy" do - before :each do - @connection = stub('mock http connection', :delete => @response) - @searcher.stubs(:network).returns(@connection) # neuter the network connection + describe "when doing a destroy" do + before :each do + @connection = stub('mock http connection', :delete => @response) + @searcher.stubs(:network).returns(@connection) # neuter the network connection - @request = Puppet::Indirector::Request.new(:foo, :destroy, "foo bar") - end + @request = Puppet::Indirector::Request.new(:foo, :destroy, "foo bar") + end - it "should call the DELETE http method on a network connection" do - @searcher.expects(:network).returns @connection - @connection.expects(:delete).returns @response - @searcher.destroy(@request) - end + it "should call the DELETE http method on a network connection" do + @searcher.expects(:network).returns @connection + @connection.expects(:delete).returns @response + @searcher.destroy(@request) + end - it "should fail if any options are provided, since DELETE apparently does not support query options" do - @request.stubs(:options).returns(:one => "two", :three => "four") + it "should fail if any options are provided, since DELETE apparently does not support query options" do + @request.stubs(:options).returns(:one => "two", :three => "four") - lambda { @searcher.destroy(@request) }.should raise_error(ArgumentError) - end + lambda { @searcher.destroy(@request) }.should raise_error(ArgumentError) + end - it "should deserialize and return the http response" do - @connection.expects(:delete).returns @response - @searcher.expects(:deserialize).with(@response).returns "myobject" + it "should deserialize and return the http response" do + @connection.expects(:delete).returns @response + @searcher.expects(:deserialize).with(@response).returns "myobject" - @searcher.destroy(@request).should == 'myobject' - end + @searcher.destroy(@request).should == 'myobject' + end - it "should use the URI generated by the Handler module" do - @searcher.expects(:indirection2uri).with(@request).returns "/my/uri" - @connection.expects(:delete).with { |path, args| path == "/my/uri" }.returns(@response) - @searcher.destroy(@request) - end + it "should use the URI generated by the Handler module" do + @searcher.expects(:indirection2uri).with(@request).returns "/my/uri" + @connection.expects(:delete).with { |path, args| path == "/my/uri" }.returns(@response) + @searcher.destroy(@request) + end - it "should not include the query string" do - @connection.stubs(:delete).returns @response - @searcher.destroy(@request) - end + it "should not include the query string" do + @connection.stubs(:delete).returns @response + @searcher.destroy(@request) + end - it "should provide an Accept header containing the list of supported formats joined with commas" do - @connection.expects(:delete).with { |path, args| args["Accept"] == "supported, formats" }.returns(@response) + it "should provide an Accept header containing the list of supported formats joined with commas" do + @connection.expects(:delete).with { |path, args| args["Accept"] == "supported, formats" }.returns(@response) - @searcher.model.expects(:supported_formats).returns %w{supported formats} - @searcher.destroy(@request) - end + @searcher.model.expects(:supported_formats).returns %w{supported formats} + @searcher.destroy(@request) + end - it "should deserialize and return the network response" do - @searcher.expects(:deserialize).with(@response).returns @instance - @searcher.destroy(@request).should equal(@instance) - end + it "should deserialize and return the network response" do + @searcher.expects(:deserialize).with(@response).returns @instance + @searcher.destroy(@request).should equal(@instance) + end - it "should generate an error when result data deserializes fails" do - @searcher.expects(:deserialize).raises(ArgumentError) - lambda { @searcher.destroy(@request) }.should raise_error(ArgumentError) - end + it "should generate an error when result data deserializes fails" do + @searcher.expects(:deserialize).raises(ArgumentError) + lambda { @searcher.destroy(@request) }.should raise_error(ArgumentError) end + end - describe "when doing a save" do - before :each do - @connection = stub('mock http connection', :put => @response) - @searcher.stubs(:network).returns(@connection) # neuter the network connection + describe "when doing a save" do + before :each do + @connection = stub('mock http connection', :put => @response) + @searcher.stubs(:network).returns(@connection) # neuter the network connection - @instance = stub 'instance', :render => "mydata", :mime => "mime" - @request = Puppet::Indirector::Request.new(:foo, :save, "foo bar") - @request.instance = @instance - end + @instance = stub 'instance', :render => "mydata", :mime => "mime" + @request = Puppet::Indirector::Request.new(:foo, :save, "foo bar") + @request.instance = @instance + end - it "should call the PUT http method on a network connection" do - @searcher.expects(:network).returns @connection - @connection.expects(:put).returns @response - @searcher.save(@request) - end + it "should call the PUT http method on a network connection" do + @searcher.expects(:network).returns @connection + @connection.expects(:put).returns @response + @searcher.save(@request) + end - it "should fail if any options are provided, since DELETE apparently does not support query options" do - @request.stubs(:options).returns(:one => "two", :three => "four") + it "should fail if any options are provided, since DELETE apparently does not support query options" do + @request.stubs(:options).returns(:one => "two", :three => "four") - lambda { @searcher.save(@request) }.should raise_error(ArgumentError) - end + lambda { @searcher.save(@request) }.should raise_error(ArgumentError) + end - it "should use the URI generated by the Handler module" do - @searcher.expects(:indirection2uri).with(@request).returns "/my/uri" - @connection.expects(:put).with { |path, args| path == "/my/uri" }.returns(@response) - @searcher.save(@request) - end + it "should use the URI generated by the Handler module" do + @searcher.expects(:indirection2uri).with(@request).returns "/my/uri" + @connection.expects(:put).with { |path, args| path == "/my/uri" }.returns(@response) + @searcher.save(@request) + end - it "should serialize the instance using the default format and pass the result as the body of the request" do - @instance.expects(:render).returns "serial_instance" - @connection.expects(:put).with { |path, data, args| data == "serial_instance" }.returns @response + it "should serialize the instance using the default format and pass the result as the body of the request" do + @instance.expects(:render).returns "serial_instance" + @connection.expects(:put).with { |path, data, args| data == "serial_instance" }.returns @response - @searcher.save(@request) - end + @searcher.save(@request) + end - it "should deserialize and return the http response" do - @connection.expects(:put).returns @response - @searcher.expects(:deserialize).with(@response).returns "myobject" + it "should deserialize and return the http response" do + @connection.expects(:put).returns @response + @searcher.expects(:deserialize).with(@response).returns "myobject" - @searcher.save(@request).should == 'myobject' - end + @searcher.save(@request).should == 'myobject' + end - it "should provide an Accept header containing the list of supported formats joined with commas" do - @connection.expects(:put).with { |path, data, args| args["Accept"] == "supported, formats" }.returns(@response) + it "should provide an Accept header containing the list of supported formats joined with commas" do + @connection.expects(:put).with { |path, data, args| args["Accept"] == "supported, formats" }.returns(@response) - @searcher.model.expects(:supported_formats).returns %w{supported formats} - @searcher.save(@request) - end + @searcher.model.expects(:supported_formats).returns %w{supported formats} + @searcher.save(@request) + end - it "should provide a Content-Type header containing the mime-type of the sent object" do - @connection.expects(:put).with { |path, data, args| args['Content-Type'] == "mime" }.returns(@response) + it "should provide a Content-Type header containing the mime-type of the sent object" do + @connection.expects(:put).with { |path, data, args| args['Content-Type'] == "mime" }.returns(@response) - @instance.expects(:mime).returns "mime" - @searcher.save(@request) - end + @instance.expects(:mime).returns "mime" + @searcher.save(@request) + end - it "should deserialize and return the network response" do - @searcher.expects(:deserialize).with(@response).returns @instance - @searcher.save(@request).should equal(@instance) - end + it "should deserialize and return the network response" do + @searcher.expects(:deserialize).with(@response).returns @instance + @searcher.save(@request).should equal(@instance) + end - it "should generate an error when result data deserializes fails" do - @searcher.expects(:deserialize).raises(ArgumentError) - lambda { @searcher.save(@request) }.should raise_error(ArgumentError) - end + it "should generate an error when result data deserializes fails" do + @searcher.expects(:deserialize).raises(ArgumentError) + lambda { @searcher.save(@request) }.should raise_error(ArgumentError) end + end end diff --git a/spec/unit/indirector/run/local_spec.rb b/spec/unit/indirector/run/local_spec.rb index face61d5c..2684096d5 100644 --- a/spec/unit/indirector/run/local_spec.rb +++ b/spec/unit/indirector/run/local_spec.rb @@ -5,16 +5,16 @@ require File.dirname(__FILE__) + '/../../../spec_helper' require 'puppet/indirector/run/local' describe Puppet::Run::Local do - it "should be a sublcass of Puppet::Indirector::Code" do - Puppet::Run::Local.superclass.should equal(Puppet::Indirector::Code) - end + it "should be a sublcass of Puppet::Indirector::Code" do + Puppet::Run::Local.superclass.should equal(Puppet::Indirector::Code) + end - it "should call runner.run on save and return the runner" do - runner = Puppet::Run.new - runner.stubs(:run).returns(runner) + it "should call runner.run on save and return the runner" do + runner = Puppet::Run.new + runner.stubs(:run).returns(runner) - request = Puppet::Indirector::Request.new(:indirection, :save, "anything") - request.instance = runner = Puppet::Run.new - Puppet::Run::Local.new.save(request).should == runner - end + request = Puppet::Indirector::Request.new(:indirection, :save, "anything") + request.instance = runner = Puppet::Run.new + Puppet::Run::Local.new.save(request).should == runner + end end diff --git a/spec/unit/indirector/run/rest_spec.rb b/spec/unit/indirector/run/rest_spec.rb index ee976ed9f..2b2415ab6 100755 --- a/spec/unit/indirector/run/rest_spec.rb +++ b/spec/unit/indirector/run/rest_spec.rb @@ -5,7 +5,7 @@ require File.dirname(__FILE__) + '/../../../spec_helper' require 'puppet/indirector/run/rest' describe Puppet::Run::Rest do - it "should be a sublcass of Puppet::Indirector::REST" do - Puppet::Run::Rest.superclass.should equal(Puppet::Indirector::REST) - end + it "should be a sublcass of Puppet::Indirector::REST" do + Puppet::Run::Rest.superclass.should equal(Puppet::Indirector::REST) + end end diff --git a/spec/unit/indirector/ssl_file_spec.rb b/spec/unit/indirector/ssl_file_spec.rb index 02c7fcf0c..83145cffc 100755 --- a/spec/unit/indirector/ssl_file_spec.rb +++ b/spec/unit/indirector/ssl_file_spec.rb @@ -8,274 +8,274 @@ require File.dirname(__FILE__) + '/../../spec_helper' require 'puppet/indirector/ssl_file' describe Puppet::Indirector::SslFile do + before do + @model = mock 'model' + @indirection = stub 'indirection', :name => :testing, :model => @model + Puppet::Indirector::Indirection.expects(:instance).with(:testing).returns(@indirection) + @file_class = Class.new(Puppet::Indirector::SslFile) do + def self.to_s + "Testing::Mytype" + end + end + + @setting = :mydir + @file_class.store_in @setting + @path = "/my/directory" + Puppet.settings.stubs(:value).with(:noop).returns(false) + Puppet.settings.stubs(:value).with(@setting).returns(@path) + Puppet.settings.stubs(:value).with(:trace).returns(false) + end + + it "should use :main and :ssl upon initialization" do + Puppet.settings.expects(:use).with(:main, :ssl) + @file_class.new + end + + it "should return a nil collection directory if no directory setting has been provided" do + @file_class.store_in nil + @file_class.collection_directory.should be_nil + end + + it "should return a nil file location if no location has been provided" do + @file_class.store_at nil + @file_class.file_location.should be_nil + end + + it "should fail if no store directory or file location has been set" do + @file_class.store_in nil + @file_class.store_at nil + lambda { @file_class.new }.should raise_error(Puppet::DevError) + end + + describe "when managing ssl files" do before do - @model = mock 'model' - @indirection = stub 'indirection', :name => :testing, :model => @model - Puppet::Indirector::Indirection.expects(:instance).with(:testing).returns(@indirection) - @file_class = Class.new(Puppet::Indirector::SslFile) do - def self.to_s - "Testing::Mytype" - end - end + Puppet.settings.stubs(:use) + @searcher = @file_class.new - @setting = :mydir - @file_class.store_in @setting - @path = "/my/directory" - Puppet.settings.stubs(:value).with(:noop).returns(false) - Puppet.settings.stubs(:value).with(@setting).returns(@path) - Puppet.settings.stubs(:value).with(:trace).returns(false) + @cert = stub 'certificate', :name => "myname" + @certpath = File.join(@path, "myname.pem") + + @request = stub 'request', :key => @cert.name, :instance => @cert end - it "should use :main and :ssl upon initialization" do - Puppet.settings.expects(:use).with(:main, :ssl) - @file_class.new + it "should consider the file a ca file if the name is equal to what the SSL::Host class says is the CA name" do + Puppet::SSL::Host.expects(:ca_name).returns "amaca" + @searcher.should be_ca("amaca") end - it "should return a nil collection directory if no directory setting has been provided" do + describe "when choosing the location for certificates" do + it "should set them at the ca setting's path if a ca setting is available and the name resolves to the CA name" do @file_class.store_in nil - @file_class.collection_directory.should be_nil - end + @file_class.store_at :mysetting + @file_class.store_ca_at :casetting - it "should return a nil file location if no location has been provided" do - @file_class.store_at nil - @file_class.file_location.should be_nil - end + Puppet.settings.stubs(:value).with(:casetting).returns "/ca/file" + + @searcher.expects(:ca?).with(@cert.name).returns true + @searcher.path(@cert.name).should == "/ca/file" + end - it "should fail if no store directory or file location has been set" do + it "should set them at the file location if a file setting is available" do @file_class.store_in nil - @file_class.store_at nil - lambda { @file_class.new }.should raise_error(Puppet::DevError) + @file_class.store_at :mysetting + + Puppet.settings.stubs(:value).with(:mysetting).returns "/some/file" + + @searcher.path(@cert.name).should == "/some/file" + end + + it "should set them in the setting directory, with the certificate name plus '.pem', if a directory setting is available" do + @searcher.path(@cert.name).should == @certpath + end end - describe "when managing ssl files" do + describe "when finding certificates on disk" do + describe "and no certificate is present" do + before do + # Stub things so the case management bits work. + FileTest.stubs(:exist?).with(File.dirname(@certpath)).returns false + FileTest.expects(:exist?).with(@certpath).returns false + end + + it "should return nil" do + @searcher.find(@request).should be_nil + end + end + + describe "and a certificate is present" do before do - Puppet.settings.stubs(:use) - @searcher = @file_class.new + FileTest.expects(:exist?).with(@certpath).returns true + end - @cert = stub 'certificate', :name => "myname" - @certpath = File.join(@path, "myname.pem") + it "should return an instance of the model, which it should use to read the certificate" do + cert = mock 'cert' + model = mock 'model' + @file_class.stubs(:model).returns model - @request = stub 'request', :key => @cert.name, :instance => @cert + model.expects(:new).with("myname").returns cert + cert.expects(:read).with(@certpath) + @searcher.find(@request).should equal(cert) end + end - it "should consider the file a ca file if the name is equal to what the SSL::Host class says is the CA name" do - Puppet::SSL::Host.expects(:ca_name).returns "amaca" - @searcher.should be_ca("amaca") + describe "and a certificate is present but has uppercase letters" do + before do + @request = stub 'request', :key => "myhost" end - describe "when choosing the location for certificates" do - it "should set them at the ca setting's path if a ca setting is available and the name resolves to the CA name" do - @file_class.store_in nil - @file_class.store_at :mysetting - @file_class.store_ca_at :casetting + # This is kind of more an integration test; it's for #1382, until + # the support for upper-case certs can be removed around mid-2009. + it "should rename the existing file to the lower-case path" do + @path = @searcher.path("myhost") + FileTest.expects(:exist?).with(@path).returns(false) + dir, file = File.split(@path) + FileTest.expects(:exist?).with(dir).returns true + Dir.expects(:entries).with(dir).returns [".", "..", "something.pem", file.upcase] + + File.expects(:rename).with(File.join(dir, file.upcase), @path) + + cert = mock 'cert' + model = mock 'model' + @searcher.stubs(:model).returns model + @searcher.model.expects(:new).with("myhost").returns cert + cert.expects(:read).with(@path) - Puppet.settings.stubs(:value).with(:casetting).returns "/ca/file" + @searcher.find(@request) + end + end + end - @searcher.expects(:ca?).with(@cert.name).returns true - @searcher.path(@cert.name).should == "/ca/file" - end + describe "when saving certificates to disk" do + before do + FileTest.stubs(:directory?).returns true + FileTest.stubs(:writable?).returns true + end + + it "should fail if the directory is absent" do + FileTest.expects(:directory?).with(File.dirname(@certpath)).returns false + lambda { @searcher.save(@request) }.should raise_error(Puppet::Error) + end + + it "should fail if the directory is not writeable" do + FileTest.stubs(:directory?).returns true + FileTest.expects(:writable?).with(File.dirname(@certpath)).returns false + lambda { @searcher.save(@request) }.should raise_error(Puppet::Error) + end + + it "should save to the path the output of converting the certificate to a string" do + fh = mock 'filehandle' + fh.expects(:print).with("mycert") + + @searcher.stubs(:write).yields fh + @cert.expects(:to_s).returns "mycert" + + @searcher.save(@request) + end + + describe "and a directory setting is set" do + it "should use the Settings class to write the file" do + @searcher.class.store_in @setting + fh = mock 'filehandle' + fh.stubs :print + Puppet.settings.expects(:writesub).with(@setting, @certpath).yields fh + + @searcher.save(@request) + end + end - it "should set them at the file location if a file setting is available" do - @file_class.store_in nil - @file_class.store_at :mysetting + describe "and a file location is set" do + it "should use the filehandle provided by the Settings" do + @searcher.class.store_at @setting - Puppet.settings.stubs(:value).with(:mysetting).returns "/some/file" + fh = mock 'filehandle' + fh.stubs :print + Puppet.settings.expects(:write).with(@setting).yields fh + @searcher.save(@request) + end + end + + describe "and the name is the CA name and a ca setting is set" do + it "should use the filehandle provided by the Settings" do + @searcher.class.store_at @setting + @searcher.class.store_ca_at :castuff + Puppet.settings.stubs(:value).with(:castuff).returns "castuff stub" + + fh = mock 'filehandle' + fh.stubs :print + Puppet.settings.expects(:write).with(:castuff).yields fh + @searcher.stubs(:ca?).returns true + @searcher.save(@request) + end + end + end - @searcher.path(@cert.name).should == "/some/file" - end + describe "when destroying certificates" do + describe "that do not exist" do + before do + FileTest.expects(:exist?).with(@certpath).returns false + end - it "should set them in the setting directory, with the certificate name plus '.pem', if a directory setting is available" do - @searcher.path(@cert.name).should == @certpath - end + it "should return false" do + @searcher.destroy(@request).should be_false end + end - describe "when finding certificates on disk" do - describe "and no certificate is present" do - before do - # Stub things so the case management bits work. - FileTest.stubs(:exist?).with(File.dirname(@certpath)).returns false - FileTest.expects(:exist?).with(@certpath).returns false - end - - it "should return nil" do - @searcher.find(@request).should be_nil - end - end - - describe "and a certificate is present" do - before do - FileTest.expects(:exist?).with(@certpath).returns true - end - - it "should return an instance of the model, which it should use to read the certificate" do - cert = mock 'cert' - model = mock 'model' - @file_class.stubs(:model).returns model - - model.expects(:new).with("myname").returns cert - cert.expects(:read).with(@certpath) - @searcher.find(@request).should equal(cert) - end - end - - describe "and a certificate is present but has uppercase letters" do - before do - @request = stub 'request', :key => "myhost" - end - - # This is kind of more an integration test; it's for #1382, until - # the support for upper-case certs can be removed around mid-2009. - it "should rename the existing file to the lower-case path" do - @path = @searcher.path("myhost") - FileTest.expects(:exist?).with(@path).returns(false) - dir, file = File.split(@path) - FileTest.expects(:exist?).with(dir).returns true - Dir.expects(:entries).with(dir).returns [".", "..", "something.pem", file.upcase] - - File.expects(:rename).with(File.join(dir, file.upcase), @path) - - cert = mock 'cert' - model = mock 'model' - @searcher.stubs(:model).returns model - @searcher.model.expects(:new).with("myhost").returns cert - cert.expects(:read).with(@path) - - @searcher.find(@request) - end - end + describe "that exist" do + before do + FileTest.expects(:exist?).with(@certpath).returns true end - describe "when saving certificates to disk" do - before do - FileTest.stubs(:directory?).returns true - FileTest.stubs(:writable?).returns true - end - - it "should fail if the directory is absent" do - FileTest.expects(:directory?).with(File.dirname(@certpath)).returns false - lambda { @searcher.save(@request) }.should raise_error(Puppet::Error) - end - - it "should fail if the directory is not writeable" do - FileTest.stubs(:directory?).returns true - FileTest.expects(:writable?).with(File.dirname(@certpath)).returns false - lambda { @searcher.save(@request) }.should raise_error(Puppet::Error) - end - - it "should save to the path the output of converting the certificate to a string" do - fh = mock 'filehandle' - fh.expects(:print).with("mycert") - - @searcher.stubs(:write).yields fh - @cert.expects(:to_s).returns "mycert" - - @searcher.save(@request) - end - - describe "and a directory setting is set" do - it "should use the Settings class to write the file" do - @searcher.class.store_in @setting - fh = mock 'filehandle' - fh.stubs :print - Puppet.settings.expects(:writesub).with(@setting, @certpath).yields fh - - @searcher.save(@request) - end - end - - describe "and a file location is set" do - it "should use the filehandle provided by the Settings" do - @searcher.class.store_at @setting - - fh = mock 'filehandle' - fh.stubs :print - Puppet.settings.expects(:write).with(@setting).yields fh - @searcher.save(@request) - end - end - - describe "and the name is the CA name and a ca setting is set" do - it "should use the filehandle provided by the Settings" do - @searcher.class.store_at @setting - @searcher.class.store_ca_at :castuff - Puppet.settings.stubs(:value).with(:castuff).returns "castuff stub" - - fh = mock 'filehandle' - fh.stubs :print - Puppet.settings.expects(:write).with(:castuff).yields fh - @searcher.stubs(:ca?).returns true - @searcher.save(@request) - end - end + it "should unlink the certificate file" do + File.expects(:unlink).with(@certpath) + @searcher.destroy(@request) end - describe "when destroying certificates" do - describe "that do not exist" do - before do - FileTest.expects(:exist?).with(@certpath).returns false - end - - it "should return false" do - @searcher.destroy(@request).should be_false - end - end - - describe "that exist" do - before do - FileTest.expects(:exist?).with(@certpath).returns true - end - - it "should unlink the certificate file" do - File.expects(:unlink).with(@certpath) - @searcher.destroy(@request) - end - - it "should log that is removing the file" do - File.stubs(:exist?).returns true - File.stubs(:unlink) - Puppet.expects(:notice) - @searcher.destroy(@request) - end - end + it "should log that is removing the file" do + File.stubs(:exist?).returns true + File.stubs(:unlink) + Puppet.expects(:notice) + @searcher.destroy(@request) end + end + end - describe "when searching for certificates" do - before do - @model = mock 'model' - @file_class.stubs(:model).returns @model - end - it "should return a certificate instance for all files that exist" do - Dir.expects(:entries).with(@path).returns %w{one.pem two.pem} + describe "when searching for certificates" do + before do + @model = mock 'model' + @file_class.stubs(:model).returns @model + end + it "should return a certificate instance for all files that exist" do + Dir.expects(:entries).with(@path).returns %w{one.pem two.pem} - one = stub 'one', :read => nil - two = stub 'two', :read => nil + one = stub 'one', :read => nil + two = stub 'two', :read => nil - @model.expects(:new).with("one").returns one - @model.expects(:new).with("two").returns two + @model.expects(:new).with("one").returns one + @model.expects(:new).with("two").returns two - @searcher.search(@request).should == [one, two] - end + @searcher.search(@request).should == [one, two] + end - it "should read each certificate in using the model's :read method" do - Dir.expects(:entries).with(@path).returns %w{one.pem} + it "should read each certificate in using the model's :read method" do + Dir.expects(:entries).with(@path).returns %w{one.pem} - one = stub 'one' - one.expects(:read).with(File.join(@path, "one.pem")) + one = stub 'one' + one.expects(:read).with(File.join(@path, "one.pem")) - @model.expects(:new).with("one").returns one + @model.expects(:new).with("one").returns one - @searcher.search(@request) - end + @searcher.search(@request) + end - it "should skip any files that do not match /\.pem$/" do - Dir.expects(:entries).with(@path).returns %w{. .. one.pem} + it "should skip any files that do not match /\.pem$/" do + Dir.expects(:entries).with(@path).returns %w{. .. one.pem} - one = stub 'one', :read => nil + one = stub 'one', :read => nil - @model.expects(:new).with("one").returns one + @model.expects(:new).with("one").returns one - @searcher.search(@request) - end - end + @searcher.search(@request) + end end + end end diff --git a/spec/unit/indirector/status/rest_spec.rb b/spec/unit/indirector/status/rest_spec.rb index 8f803a253..5eed5fc93 100755 --- a/spec/unit/indirector/status/rest_spec.rb +++ b/spec/unit/indirector/status/rest_spec.rb @@ -5,7 +5,7 @@ Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f require 'puppet/indirector/status/rest' describe Puppet::Indirector::Status::Rest do - it "should be a sublcass of Puppet::Indirector::REST" do - Puppet::Indirector::Status::Rest.superclass.should equal(Puppet::Indirector::REST) - end + it "should be a sublcass of Puppet::Indirector::REST" do + Puppet::Indirector::Status::Rest.superclass.should equal(Puppet::Indirector::REST) + end end diff --git a/spec/unit/indirector/terminus_spec.rb b/spec/unit/indirector/terminus_spec.rb index 3fcbf9d0c..826b9347a 100755 --- a/spec/unit/indirector/terminus_spec.rb +++ b/spec/unit/indirector/terminus_spec.rb @@ -6,106 +6,106 @@ require 'puppet/indirector' require 'puppet/indirector/file' describe Puppet::Indirector::Terminus do - before :each do - Puppet::Indirector::Terminus.stubs(:register_terminus_class) - @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 - "Testing::Abstract" - end - end - @terminus_class = Class.new(@abstract_terminus) do - def self.to_s - "MyStuff::TermType" - end - end - @terminus = @terminus_class.new + before :each do + Puppet::Indirector::Terminus.stubs(:register_terminus_class) + @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 + "Testing::Abstract" + end end + @terminus_class = Class.new(@abstract_terminus) do + def self.to_s + "MyStuff::TermType" + end + end + @terminus = @terminus_class.new + end - describe Puppet::Indirector::Terminus do + describe Puppet::Indirector::Terminus do - it "should provide a method for setting terminus class documentation" do - @terminus_class.should respond_to(:desc) - end + it "should provide a method for setting terminus class documentation" do + @terminus_class.should respond_to(:desc) + end - it "should support a class-level name attribute" do - @terminus_class.should respond_to(:name) - end + it "should support a class-level name attribute" do + @terminus_class.should respond_to(:name) + end - it "should support a class-level indirection attribute" do - @terminus_class.should respond_to(:indirection) - end + it "should support a class-level indirection attribute" do + @terminus_class.should respond_to(:indirection) + end - it "should support a class-level terminus-type attribute" do - @terminus_class.should respond_to(:terminus_type) - end + it "should support a class-level terminus-type attribute" do + @terminus_class.should respond_to(:terminus_type) + end - it "should support a class-level model attribute" do - @terminus_class.should respond_to(:model) - end + it "should support a class-level model attribute" do + @terminus_class.should respond_to(:model) + end - it "should accept indirection instances as its indirection" do - indirection = stub 'indirection', :is_a? => true, :register_terminus_type => nil - proc { @terminus_class.indirection = indirection }.should_not raise_error - @terminus_class.indirection.should equal(indirection) - end + it "should accept indirection instances as its indirection" do + indirection = stub 'indirection', :is_a? => true, :register_terminus_type => nil + proc { @terminus_class.indirection = indirection }.should_not raise_error + @terminus_class.indirection.should equal(indirection) + end - it "should look up indirection instances when only a name has been provided" do - indirection = mock 'indirection' - Puppet::Indirector::Indirection.expects(:instance).with(:myind).returns(indirection) - @terminus_class.indirection = :myind - @terminus_class.indirection.should equal(indirection) - end + it "should look up indirection instances when only a name has been provided" do + indirection = mock 'indirection' + Puppet::Indirector::Indirection.expects(:instance).with(:myind).returns(indirection) + @terminus_class.indirection = :myind + @terminus_class.indirection.should equal(indirection) + end - it "should fail when provided a name that does not resolve to an indirection" do - Puppet::Indirector::Indirection.expects(:instance).with(:myind).returns(nil) - proc { @terminus_class.indirection = :myind }.should raise_error(ArgumentError) + it "should fail when provided a name that does not resolve to an indirection" do + Puppet::Indirector::Indirection.expects(:instance).with(:myind).returns(nil) + proc { @terminus_class.indirection = :myind }.should raise_error(ArgumentError) - # It shouldn't overwrite our existing one (or, more normally, it shouldn't set - # anything). - @terminus_class.indirection.should equal(@indirection) - end + # It shouldn't overwrite our existing one (or, more normally, it shouldn't set + # anything). + @terminus_class.indirection.should equal(@indirection) end + end - describe Puppet::Indirector::Terminus, " when creating terminus classes" do - it "should associate the subclass with an indirection based on the subclass constant" do - @terminus.indirection.should equal(@indirection) - end + describe Puppet::Indirector::Terminus, " when creating terminus classes" do + it "should associate the subclass with an indirection based on the subclass constant" do + @terminus.indirection.should equal(@indirection) + end - it "should set the subclass's type to the abstract terminus name" do - @terminus.terminus_type.should == :abstract - end + it "should set the subclass's type to the abstract terminus name" do + @terminus.terminus_type.should == :abstract + end - it "should set the subclass's name to the indirection name" do - @terminus.name.should == :term_type - end + it "should set the subclass's name to the indirection name" do + @terminus.name.should == :term_type + end - it "should set the subclass's model to the indirection model" do - @indirection.expects(:model).returns :yay - @terminus.model.should == :yay - end + it "should set the subclass's model to the indirection model" do + @indirection.expects(:model).returns :yay + @terminus.model.should == :yay end + end - describe Puppet::Indirector::Terminus, " when a terminus instance" do + describe Puppet::Indirector::Terminus, " when a terminus instance" do - it "should return the class's name as its name" do - @terminus.name.should == :term_type - end + it "should return the class's name as its name" do + @terminus.name.should == :term_type + end - it "should return the class's indirection as its indirection" do - @terminus.indirection.should equal(@indirection) - end + it "should return the class's indirection as its indirection" do + @terminus.indirection.should equal(@indirection) + end - it "should set the instances's type to the abstract terminus type's name" do - @terminus.terminus_type.should == :abstract - end + it "should set the instances's type to the abstract terminus type's name" do + @terminus.terminus_type.should == :abstract + end - it "should set the instances's model to the indirection's model" do - @indirection.expects(:model).returns :yay - @terminus.model.should == :yay - end + it "should set the instances's model to the indirection's model" do + @indirection.expects(:model).returns :yay + @terminus.model.should == :yay end + end end # LAK: This could reasonably be in the Indirection instances, too. It doesn't make @@ -113,133 +113,133 @@ end # the Terminus base class, we have to have a check to see if we're already # instance-loading a given terminus class type. describe Puppet::Indirector::Terminus, " when managing terminus classes" do - it "should provide a method for registering terminus classes" do - Puppet::Indirector::Terminus.should respond_to(:register_terminus_class) - end - - it "should provide a method for returning terminus classes by name and type" do - terminus = stub 'terminus_type', :name => :abstract, :indirection_name => :whatever - Puppet::Indirector::Terminus.register_terminus_class(terminus) - Puppet::Indirector::Terminus.terminus_class(:whatever, :abstract).should equal(terminus) - end - - it "should set up autoloading for any terminus class types requested" do - Puppet::Indirector::Terminus.expects(:instance_load).with(:test2, "puppet/indirector/test2") - Puppet::Indirector::Terminus.terminus_class(:test2, :whatever) - end - - it "should load terminus classes that are not found" do - # Set up instance loading; it would normally happen automatically - Puppet::Indirector::Terminus.instance_load :test1, "puppet/indirector/test1" - - Puppet::Indirector::Terminus.instance_loader(:test1).expects(:load).with(:yay) - Puppet::Indirector::Terminus.terminus_class(:test1, :yay) - end + it "should provide a method for registering terminus classes" do + Puppet::Indirector::Terminus.should respond_to(:register_terminus_class) + end - it "should fail when no indirection can be found" do - Puppet::Indirector::Indirection.expects(:instance).with(:my_indirection).returns(nil) + it "should provide a method for returning terminus classes by name and type" do + terminus = stub 'terminus_type', :name => :abstract, :indirection_name => :whatever + Puppet::Indirector::Terminus.register_terminus_class(terminus) + Puppet::Indirector::Terminus.terminus_class(:whatever, :abstract).should equal(terminus) + end - @abstract_terminus = Class.new(Puppet::Indirector::Terminus) do - def self.to_s - "Abstract" - end - end - proc { - @terminus = Class.new(@abstract_terminus) do - def self.to_s - "MyIndirection::TestType" - end - end - }.should raise_error(ArgumentError) - end - - it "should register the terminus class with the terminus base class" do - Puppet::Indirector::Terminus.expects(:register_terminus_class).with do |type| - type.indirection_name == :my_indirection and type.name == :test_terminus - end - @indirection = stub 'indirection', :name => :my_indirection, :register_terminus_type => nil - Puppet::Indirector::Indirection.expects(:instance).with(:my_indirection).returns(@indirection) + it "should set up autoloading for any terminus class types requested" do + Puppet::Indirector::Terminus.expects(:instance_load).with(:test2, "puppet/indirector/test2") + Puppet::Indirector::Terminus.terminus_class(:test2, :whatever) + end - @abstract_terminus = Class.new(Puppet::Indirector::Terminus) do - def self.to_s - "Abstract" - end - end + it "should load terminus classes that are not found" do + # Set up instance loading; it would normally happen automatically + Puppet::Indirector::Terminus.instance_load :test1, "puppet/indirector/test1" - @terminus = Class.new(@abstract_terminus) do - def self.to_s - "MyIndirection::TestTerminus" - end - end - end -end + Puppet::Indirector::Terminus.instance_loader(:test1).expects(:load).with(:yay) + Puppet::Indirector::Terminus.terminus_class(:test1, :yay) + end -describe Puppet::Indirector::Terminus, " when parsing class constants for indirection and terminus names" do - before do - @subclass = mock 'subclass' - @subclass.stubs(:to_s).returns("TestInd::OneTwo") - @subclass.stubs(:mark_as_abstract_terminus) - Puppet::Indirector::Terminus.stubs(:register_terminus_class) - end + it "should fail when no indirection can be found" do + Puppet::Indirector::Indirection.expects(:instance).with(:my_indirection).returns(nil) - it "should fail when anonymous classes are used" do - proc { Puppet::Indirector::Terminus.inherited(Class.new) }.should raise_error(Puppet::DevError) - end - - it "should use the last term in the constant for the terminus class name" do - @subclass.expects(:name=).with(:one_two) - @subclass.stubs(:indirection=) - Puppet::Indirector::Terminus.inherited(@subclass) + @abstract_terminus = Class.new(Puppet::Indirector::Terminus) do + def self.to_s + "Abstract" + end end + proc { + @terminus = Class.new(@abstract_terminus) do + def self.to_s + "MyIndirection::TestType" + end + end + }.should raise_error(ArgumentError) + end - it "should convert the terminus name to a downcased symbol" do - @subclass.expects(:name=).with(:one_two) - @subclass.stubs(:indirection=) - Puppet::Indirector::Terminus.inherited(@subclass) + it "should register the terminus class with the terminus base class" do + Puppet::Indirector::Terminus.expects(:register_terminus_class).with do |type| + type.indirection_name == :my_indirection and type.name == :test_terminus end + @indirection = stub 'indirection', :name => :my_indirection, :register_terminus_type => nil + Puppet::Indirector::Indirection.expects(:instance).with(:my_indirection).returns(@indirection) - it "should use the second to last term in the constant for the indirection name" do - @subclass.expects(:indirection=).with(:test_ind) - @subclass.stubs(:name=) - @subclass.stubs(:terminus_type=) - Puppet::Indirector::File.inherited(@subclass) + @abstract_terminus = Class.new(Puppet::Indirector::Terminus) do + def self.to_s + "Abstract" + end end - it "should convert the indirection name to a downcased symbol" do - @subclass.expects(:indirection=).with(:test_ind) - @subclass.stubs(:name=) - @subclass.stubs(:terminus_type=) - Puppet::Indirector::File.inherited(@subclass) + @terminus = Class.new(@abstract_terminus) do + def self.to_s + "MyIndirection::TestTerminus" + end end + end +end - it "should convert camel case to lower case with underscores as word separators" do - @subclass.expects(:name=).with(:one_two) - @subclass.stubs(:indirection=) - - Puppet::Indirector::Terminus.inherited(@subclass) - end +describe Puppet::Indirector::Terminus, " when parsing class constants for indirection and terminus names" do + before do + @subclass = mock 'subclass' + @subclass.stubs(:to_s).returns("TestInd::OneTwo") + @subclass.stubs(:mark_as_abstract_terminus) + Puppet::Indirector::Terminus.stubs(:register_terminus_class) + end + + it "should fail when anonymous classes are used" do + proc { Puppet::Indirector::Terminus.inherited(Class.new) }.should raise_error(Puppet::DevError) + end + + it "should use the last term in the constant for the terminus class name" do + @subclass.expects(:name=).with(:one_two) + @subclass.stubs(:indirection=) + Puppet::Indirector::Terminus.inherited(@subclass) + end + + it "should convert the terminus name to a downcased symbol" do + @subclass.expects(:name=).with(:one_two) + @subclass.stubs(:indirection=) + Puppet::Indirector::Terminus.inherited(@subclass) + end + + it "should use the second to last term in the constant for the indirection name" do + @subclass.expects(:indirection=).with(:test_ind) + @subclass.stubs(:name=) + @subclass.stubs(:terminus_type=) + Puppet::Indirector::File.inherited(@subclass) + end + + it "should convert the indirection name to a downcased symbol" do + @subclass.expects(:indirection=).with(:test_ind) + @subclass.stubs(:name=) + @subclass.stubs(:terminus_type=) + Puppet::Indirector::File.inherited(@subclass) + end + + it "should convert camel case to lower case with underscores as word separators" do + @subclass.expects(:name=).with(:one_two) + @subclass.stubs(:indirection=) + + 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) - @subclass = Class.new(Puppet::Indirector::Terminus) do - def self.to_s - "Puppet::Indirector::Terminus::MyTermType" - end - end - end - - it "should set the name of the abstract subclass to be its class constant" do - @subclass.name.should equal(:my_term_type) - end - - it "should mark abstract terminus types as such" do - @subclass.should be_abstract_terminus - end - - it "should not allow instances of abstract subclasses to be created" do - proc { @subclass.new }.should raise_error(Puppet::DevError) - end + before do + Puppet::Indirector::Terminus.stubs(:register_terminus_class) + @subclass = Class.new(Puppet::Indirector::Terminus) do + def self.to_s + "Puppet::Indirector::Terminus::MyTermType" + end + end + end + + it "should set the name of the abstract subclass to be its class constant" do + @subclass.name.should equal(:my_term_type) + end + + it "should mark abstract terminus types as such" do + @subclass.should be_abstract_terminus + end + + it "should not allow instances of abstract subclasses to be created" do + proc { @subclass.new }.should raise_error(Puppet::DevError) + end end diff --git a/spec/unit/indirector/yaml_spec.rb b/spec/unit/indirector/yaml_spec.rb index bd207a878..134d476ba 100755 --- a/spec/unit/indirector/yaml_spec.rb +++ b/spec/unit/indirector/yaml_spec.rb @@ -5,142 +5,142 @@ require File.dirname(__FILE__) + '/../../spec_helper' require 'puppet/indirector/yaml' describe Puppet::Indirector::Yaml, " when choosing file location" do - before :each do - @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::MyType" - end - end - @store = @store_class.new - - @subject = Object.new - @subject.singleton_class.send(:attr_accessor, :name) - @subject.name = :me - - @dir = "/what/ever" - Puppet.settings.stubs(:value).returns("fakesettingdata") - Puppet.settings.stubs(:value).with(:clientyamldir).returns(@dir) - Puppet.run_mode.stubs(:master?).returns false - - @request = stub 'request', :key => :me, :instance => @subject + before :each do + @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::MyType" + end end + @store = @store_class.new - describe Puppet::Indirector::Yaml, " when choosing file location" do - it "should use the server_datadir if the run_mode is master" do - Puppet.run_mode.expects(:master?).returns true - Puppet.settings.expects(:value).with(:yamldir).returns "/server/yaml/dir" - @store.path(:me).should =~ %r{^/server/yaml/dir} - end - - it "should use the client yamldir if the run_mode is not master" do - Puppet.run_mode.expects(:master?).returns false - Puppet.settings.expects(:value).with(:clientyamldir).returns "/client/yaml/dir" - @store.path(:me).should =~ %r{^/client/yaml/dir} - end - - it "should store all files in a single file root set in the Puppet defaults" do - @store.path(:me).should =~ %r{^#{@dir}} - end - - it "should use the terminus name for choosing the subdirectory" do - @store.path(:me).should =~ %r{^#{@dir}/my_yaml} - end - - it "should use the object's name to determine the file name" do - @store.path(:me).should =~ %r{me.yaml$} - end + @subject = Object.new + @subject.singleton_class.send(:attr_accessor, :name) + @subject.name = :me + + @dir = "/what/ever" + Puppet.settings.stubs(:value).returns("fakesettingdata") + Puppet.settings.stubs(:value).with(:clientyamldir).returns(@dir) + Puppet.run_mode.stubs(:master?).returns false + + @request = stub 'request', :key => :me, :instance => @subject + end + + describe Puppet::Indirector::Yaml, " when choosing file location" do + it "should use the server_datadir if the run_mode is master" do + Puppet.run_mode.expects(:master?).returns true + Puppet.settings.expects(:value).with(:yamldir).returns "/server/yaml/dir" + @store.path(:me).should =~ %r{^/server/yaml/dir} + end + + it "should use the client yamldir if the run_mode is not master" do + Puppet.run_mode.expects(:master?).returns false + Puppet.settings.expects(:value).with(:clientyamldir).returns "/client/yaml/dir" + @store.path(:me).should =~ %r{^/client/yaml/dir} + end + + it "should store all files in a single file root set in the Puppet defaults" do + @store.path(:me).should =~ %r{^#{@dir}} + end + + it "should use the terminus name for choosing the subdirectory" do + @store.path(:me).should =~ %r{^#{@dir}/my_yaml} end - describe Puppet::Indirector::Yaml, " when storing objects as YAML" do - it "should only store objects that respond to :name" do - @request.stubs(:instance).returns Object.new - proc { @store.save(@request) }.should raise_error(ArgumentError) - end - - it "should convert Ruby objects to YAML and write them to disk using a write lock" do - yaml = @subject.to_yaml - file = mock 'file' - path = @store.send(:path, @subject.name) - FileTest.expects(:exist?).with(File.dirname(path)).returns(true) - @store.expects(:writelock).with(path, 0660).yields(file) - file.expects(:print).with(yaml) - - @store.save(@request) - end - - it "should create the indirection subdirectory if it does not exist" do - yaml = @subject.to_yaml - file = mock 'file' - path = @store.send(:path, @subject.name) - dir = File.dirname(path) - - FileTest.expects(:exist?).with(dir).returns(false) - Dir.expects(:mkdir).with(dir) - - @store.expects(:writelock).yields(file) - file.expects(:print).with(yaml) - - @store.save(@request) - end + it "should use the object's name to determine the file name" do + @store.path(:me).should =~ %r{me.yaml$} end + end - describe Puppet::Indirector::Yaml, " when retrieving YAML" do - it "should read YAML in from disk using a read lock and convert it to Ruby objects" do - path = @store.send(:path, @subject.name) + describe Puppet::Indirector::Yaml, " when storing objects as YAML" do + it "should only store objects that respond to :name" do + @request.stubs(:instance).returns Object.new + proc { @store.save(@request) }.should raise_error(ArgumentError) + end - yaml = @subject.to_yaml - FileTest.expects(:exist?).with(path).returns(true) + it "should convert Ruby objects to YAML and write them to disk using a write lock" do + yaml = @subject.to_yaml + file = mock 'file' + path = @store.send(:path, @subject.name) + FileTest.expects(:exist?).with(File.dirname(path)).returns(true) + @store.expects(:writelock).with(path, 0660).yields(file) + file.expects(:print).with(yaml) - fh = mock 'filehandle' - @store.expects(:readlock).with(path).yields fh - fh.expects(:read).returns yaml + @store.save(@request) + end - @store.find(@request).instance_variable_get("@name").should == :me - end + it "should create the indirection subdirectory if it does not exist" do + yaml = @subject.to_yaml + file = mock 'file' + path = @store.send(:path, @subject.name) + dir = File.dirname(path) - it "should fail coherently when the stored YAML is invalid" do - path = @store.send(:path, @subject.name) - FileTest.expects(:exist?).with(path).returns(true) + FileTest.expects(:exist?).with(dir).returns(false) + Dir.expects(:mkdir).with(dir) - # Something that will fail in yaml - yaml = "--- !ruby/object:Hash" + @store.expects(:writelock).yields(file) + file.expects(:print).with(yaml) - fh = mock 'filehandle' - @store.expects(:readlock).yields fh - fh.expects(:read).returns yaml + @store.save(@request) + end + end + + describe Puppet::Indirector::Yaml, " when retrieving YAML" do + it "should read YAML in from disk using a read lock and convert it to Ruby objects" do + path = @store.send(:path, @subject.name) + + yaml = @subject.to_yaml + FileTest.expects(:exist?).with(path).returns(true) + + fh = mock 'filehandle' + @store.expects(:readlock).with(path).yields fh + fh.expects(:read).returns yaml + + @store.find(@request).instance_variable_get("@name").should == :me + end + + it "should fail coherently when the stored YAML is invalid" do + path = @store.send(:path, @subject.name) + FileTest.expects(:exist?).with(path).returns(true) + + # Something that will fail in yaml + yaml = "--- !ruby/object:Hash" + + fh = mock 'filehandle' + @store.expects(:readlock).yields fh + fh.expects(:read).returns yaml + + proc { @store.find(@request) }.should raise_error(Puppet::Error) + end + end + + describe Puppet::Indirector::Yaml, " when searching" do + it "should return an array of fact instances with one instance for each file when globbing *" do + @request = stub 'request', :key => "*", :instance => @subject + @one = mock 'one' + @two = mock 'two' + @store.expects(:base).returns "/my/yaml/dir" + Dir.expects(:glob).with(File.join("/my/yaml/dir", @store.class.indirection_name.to_s, @request.key)).returns(%w{one.yaml two.yaml}) + YAML.expects(:load_file).with("one.yaml").returns @one; + YAML.expects(:load_file).with("two.yaml").returns @two; + @store.search(@request).should == [@one, @two] + end - proc { @store.find(@request) }.should raise_error(Puppet::Error) - end + it "should return an array containing a single instance of fact when globbing 'one*'" do + @request = stub 'request', :key => "one*", :instance => @subject + @one = mock 'one' + @store.expects(:base).returns "/my/yaml/dir" + Dir.expects(:glob).with(File.join("/my/yaml/dir", @store.class.indirection_name.to_s, @request.key)).returns(%w{one.yaml}) + YAML.expects(:load_file).with("one.yaml").returns @one; + @store.search(@request).should == [@one] end - describe Puppet::Indirector::Yaml, " when searching" do - it "should return an array of fact instances with one instance for each file when globbing *" do - @request = stub 'request', :key => "*", :instance => @subject - @one = mock 'one' - @two = mock 'two' - @store.expects(:base).returns "/my/yaml/dir" - Dir.expects(:glob).with(File.join("/my/yaml/dir", @store.class.indirection_name.to_s, @request.key)).returns(%w{one.yaml two.yaml}) - YAML.expects(:load_file).with("one.yaml").returns @one; - YAML.expects(:load_file).with("two.yaml").returns @two; - @store.search(@request).should == [@one, @two] - end - - it "should return an array containing a single instance of fact when globbing 'one*'" do - @request = stub 'request', :key => "one*", :instance => @subject - @one = mock 'one' - @store.expects(:base).returns "/my/yaml/dir" - Dir.expects(:glob).with(File.join("/my/yaml/dir", @store.class.indirection_name.to_s, @request.key)).returns(%w{one.yaml}) - YAML.expects(:load_file).with("one.yaml").returns @one; - @store.search(@request).should == [@one] - end - - it "should return an empty array when the glob doesn't match anything" do - @request = stub 'request', :key => "f*ilglobcanfail*", :instance => @subject - @store.expects(:base).returns "/my/yaml/dir" - Dir.expects(:glob).with(File.join("/my/yaml/dir", @store.class.indirection_name.to_s, @request.key)).returns([]) - @store.search(@request).should == [] - end + it "should return an empty array when the glob doesn't match anything" do + @request = stub 'request', :key => "f*ilglobcanfail*", :instance => @subject + @store.expects(:base).returns "/my/yaml/dir" + Dir.expects(:glob).with(File.join("/my/yaml/dir", @store.class.indirection_name.to_s, @request.key)).returns([]) + @store.search(@request).should == [] end + end end |