diff options
Diffstat (limited to 'spec/unit/indirector')
| -rwxr-xr-x | spec/unit/indirector/direct_file_server.rb | 95 | ||||
| -rwxr-xr-x | spec/unit/indirector/file_content/file.rb | 56 | ||||
| -rwxr-xr-x | spec/unit/indirector/file_metadata/file.rb | 48 | ||||
| -rwxr-xr-x | spec/unit/indirector/file_metadata/modules.rb | 2 | ||||
| -rwxr-xr-x | spec/unit/indirector/file_server.rb | 30 | ||||
| -rwxr-xr-x | spec/unit/indirector/indirection.rb | 94 | ||||
| -rwxr-xr-x | spec/unit/indirector/module_files.rb | 32 |
7 files changed, 242 insertions, 115 deletions
diff --git a/spec/unit/indirector/direct_file_server.rb b/spec/unit/indirector/direct_file_server.rb new file mode 100755 index 000000000..2a8ec1a49 --- /dev/null +++ b/spec/unit/indirector/direct_file_server.rb @@ -0,0 +1,95 @@ +#!/usr/bin/env ruby +# +# Created by Luke Kanies on 2007-10-24. +# Copyright (c) 2007. All rights reserved. + +require File.dirname(__FILE__) + '/../../spec_helper' + +require 'puppet/indirector/direct_file_server' + +module DirectFileServerTerminusTesting + def setup + 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 + + @uri = "file:///my/local" + end +end + +describe Puppet::Indirector::DirectFileServer, "when finding a single file" do + include DirectFileServerTerminusTesting + + it "should return nil if the file does not exist" do + FileTest.expects(:exists?).with("/my/local").returns false + @server.find(@uri).should be_nil + end + + 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(@uri).should == :mycontent + end +end + +describe Puppet::Indirector::DirectFileServer, "when creating the instance for a single found file" do + include DirectFileServerTerminusTesting + + before do + @data = mock 'content' + @data.stubs(:collect_attributes) + FileTest.expects(:exists?).with("/my/local").returns true + end + + it "should create the Content instance with the original key as the key" do + @model.expects(:new).with { |key, options| key == @uri }.returns(@data) + @server.find(@uri) + end + + it "should pass the full path to the instance" do + @model.expects(:new).with { |key, options| options[:path] == "/my/local" }.returns(@data) + @server.find(@uri) + 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) + @server.find(@uri, :links => :manage) + end +end + +describe Puppet::Indirector::DirectFileServer, "when searching for multiple files" do + include DirectFileServerTerminusTesting + + it "should return nil if the file does not exist" do + FileTest.expects(:exists?).with("/my/local").returns false + @server.find(@uri).should be_nil + end + + it "should pass the original key to :path2instances" do + FileTest.expects(:exists?).with("/my/local").returns true + @server.expects(:path2instances).with { |uri, path, options| uri == @uri } + @server.search(@uri) + 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(@uri) + end + + it "should pass any options on to :path2instances" do + FileTest.expects(:exists?).with("/my/local").returns true + @server.expects(:path2instances).with { |uri, path, options| options == {:testing => :one, :other => :two}} + @server.search(@uri, :testing => :one, :other => :two) + end +end diff --git a/spec/unit/indirector/file_content/file.rb b/spec/unit/indirector/file_content/file.rb index da2c90770..04656e0ed 100755 --- a/spec/unit/indirector/file_content/file.rb +++ b/spec/unit/indirector/file_content/file.rb @@ -12,59 +12,7 @@ describe Puppet::Indirector::FileContent::File do Puppet::Indirector::Terminus.terminus_class(:file_content, :file).should equal(Puppet::Indirector::FileContent::File) end - it "should be a subclass of the File terminus" do - Puppet::Indirector::FileContent::File.superclass.should equal(Puppet::Indirector::File) - end -end - -describe Puppet::Indirector::FileContent::File, "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 - @content = Puppet::Indirector::FileContent::File.new - @uri = "file:///my/local" - - FileTest.expects(:exists?).with("/my/local").returns true - Puppet::FileServing::Content.expects(:new).with("/my/local", :links => nil).returns(:mycontent) - @content.find(@uri).should == :mycontent - end - - it "should pass the :links setting on to the created Content instance if the file exists" do - @content = Puppet::Indirector::FileContent::File.new - @uri = "file:///my/local" - - FileTest.expects(:exists?).with("/my/local").returns true - Puppet::FileServing::Content.expects(:new).with("/my/local", :links => :manage).returns(:mycontent) - @content.find(@uri, :links => :manage) - end - - it "should return nil if the file does not exist" do - @content = Puppet::Indirector::FileContent::File.new - @uri = "file:///my/local" - - FileTest.expects(:exists?).with("/my/local").returns false - @content.find(@uri).should be_nil - end -end - -describe Puppet::Indirector::FileContent::File, "when searching for multiple files" do - before do - @content = Puppet::Indirector::FileContent::File.new - @uri = "file:///my/local" - end - - it "should return nil if the file does not exist" do - FileTest.expects(:exists?).with("/my/local").returns false - @content.find(@uri).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 - @content.expects(:path2instances).with("/my/local", {}) - @content.search(@uri) - end - - it "should pass any options on to :path2instances" do - FileTest.expects(:exists?).with("/my/local").returns true - @content.expects(:path2instances).with("/my/local", :testing => :one, :other => :two) - @content.search(@uri, :testing => :one, :other => :two) + 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_metadata/file.rb b/spec/unit/indirector/file_metadata/file.rb index c88d559a7..0a37a6895 100755 --- a/spec/unit/indirector/file_metadata/file.rb +++ b/spec/unit/indirector/file_metadata/file.rb @@ -11,44 +11,27 @@ 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) end + + it "should be a subclass of the DirectFileServer terminus" do + Puppet::Indirector::FileMetadata::File.superclass.should equal(Puppet::Indirector::DirectFileServer) + end end -describe Puppet::Indirector::FileMetadata::File, "when finding a single file" do +describe Puppet::Indirector::FileMetadata::File, "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' - end - - it "should return a Metadata instance created with the full path to the file if the file exists" do - @data.stubs(:collect_attributes) - - FileTest.expects(:exists?).with("/my/local").returns true - Puppet::FileServing::Metadata.expects(:new).with("/my/local", :links => nil).returns(@data) - @metadata.find(@uri).should == @data - end - - it "should pass the :links setting on to the created Content instance if the file exists" do @data.stubs(:collect_attributes) - FileTest.expects(:exists?).with("/my/local").returns true - Puppet::FileServing::Metadata.expects(:new).with("/my/local", :links => :manage).returns(@data) - @metadata.find(@uri, :links => :manage) end it "should collect its attributes when a file is found" do @data.expects(:collect_attributes) - FileTest.expects(:exists?).with("/my/local").returns true - Puppet::FileServing::Metadata.expects(:new).with("/my/local", :links => nil).returns(@data) + Puppet::FileServing::Metadata.expects(:new).returns(@data) @metadata.find(@uri).should == @data end - - it "should return nil if the file does not exist" do - FileTest.expects(:exists?).with("/my/local").returns false - @metadata.find(@uri).should be_nil - end end describe Puppet::Indirector::FileMetadata::File, "when searching for multiple files" do @@ -57,26 +40,9 @@ describe Puppet::Indirector::FileMetadata::File, "when searching for multiple fi @uri = "file:///my/local" end - it "should return nil if the file does not exist" do - FileTest.expects(:exists?).with("/my/local").returns false - @metadata.find(@uri).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 - @metadata.expects(:path2instances).with("/my/local", {}).returns([]) - @metadata.search(@uri) - end - - it "should pass any options on to :path2instances" do - FileTest.expects(:exists?).with("/my/local").returns true - @metadata.expects(:path2instances).with("/my/local", :testing => :one, :other => :two).returns([]) - @metadata.search(@uri, :testing => :one, :other => :two) - end - it "should collect the attributes of the instances returned" do FileTest.expects(:exists?).with("/my/local").returns true - @metadata.expects(:path2instances).with("/my/local", {}).returns( [mock("one", :collect_attributes => nil), mock("two", :collect_attributes => nil)] ) + @metadata.expects(:path2instances).returns( [mock("one", :collect_attributes => nil), mock("two", :collect_attributes => nil)] ) @metadata.search(@uri) end end diff --git a/spec/unit/indirector/file_metadata/modules.rb b/spec/unit/indirector/file_metadata/modules.rb index 94e1bf0dc..62f01832c 100755 --- a/spec/unit/indirector/file_metadata/modules.rb +++ b/spec/unit/indirector/file_metadata/modules.rb @@ -34,7 +34,7 @@ describe Puppet::Indirector::FileMetadata::Modules, " when finding metadata" do FileTest.expects(:exists?).with("/path/to/files/my/file").returns true instance = mock 'metadta' Puppet::FileServing::Metadata.expects(:new).returns instance - instance.expects :get_attributes + instance.expects :collect_attributes @finder.find("puppetmounts://hostname/modules/mymod/my/file") end end diff --git a/spec/unit/indirector/file_server.rb b/spec/unit/indirector/file_server.rb index ed36e180e..fda60f1ec 100755 --- a/spec/unit/indirector/file_server.rb +++ b/spec/unit/indirector/file_server.rb @@ -54,7 +54,7 @@ describe Puppet::Indirector::FileServer, " when finding files" do it "should return an instance of the model created with the full path if a file is found" do @configuration.expects(:file_path).with("/my/local/file", :node => nil).returns("/some/file") - @model.expects(:new).with("/some/file", :links => nil).returns(:myinstance) + @model.expects(:new).returns(:myinstance) @file_server.find(@uri).should == :myinstance end end @@ -63,11 +63,31 @@ end describe Puppet::Indirector::FileServer, " when returning instances" do include FileServerTerminusTesting - it "should pass the provided :links setting on to the instance if one is provided" do + before do @configuration.expects(:file_path).with("/my/local/file", :node => nil).returns("/some/file") - @model.expects(:new).with("/some/file", :links => :mytest) + @instance = mock 'instance' + end + + it "should create the instance with the key used to find the instance" do + @model.expects(:new).with { |key, *options| key == @uri } + @file_server.find(@uri) + end + + it "should create the instance with the path at which the instance was found" do + @model.expects(:new).with { |key, options| options[:path] == "/some/file" } + @file_server.find(@uri) + end + + it "should set the provided :links setting on to the instance if one is provided" do + @model.expects(:new).returns(@instance) + @instance.expects(:links=).with(:mytest) @file_server.find(@uri, :links => :mytest) end + + it "should not set a :links value if no :links parameter is provided" do + @model.expects(:new).returns(@instance) + @file_server.find(@uri) + end end describe Puppet::Indirector::FileServer, " when checking authorization" do @@ -141,13 +161,13 @@ describe Puppet::Indirector::FileServer, " when searching for files" do it "should use :path2instances from the terminus_helper to return instances if a module is found and the file exists" do @configuration.expects(:file_path).with("/my/local/file", :node => nil).returns("/my/file") - @file_server.expects(:path2instances).with("/my/file", {}) + @file_server.expects(:path2instances).with(@uri, "/my/file", {}) @file_server.search(@uri) end it "should pass any options on to :path2instances" do @configuration.expects(:file_path).with("/my/local/file", :node => nil).returns("/my/file") - @file_server.expects(:path2instances).with("/my/file", :testing => :one, :other => :two) + @file_server.expects(:path2instances).with(@uri, "/my/file", :testing => :one, :other => :two) @file_server.search(@uri, :testing => :one, :other => :two) end end diff --git a/spec/unit/indirector/indirection.rb b/spec/unit/indirector/indirection.rb index 00153ff35..5d8453905 100755 --- a/spec/unit/indirector/indirection.rb +++ b/spec/unit/indirector/indirection.rb @@ -67,7 +67,15 @@ describe Puppet::Indirector::Indirection, " when looking for a model instance" d proc { @indirection.find(@name) }.should_not raise_error end - it "should call the terminus's authorization hook if there is one" + it "should pass the instance to the :post_find hook if there is one" do + class << @terminus + def post_find + end + end + @terminus.expects(:post_find).with(@instance) + @terminus.expects(:find).with(@name).returns(@instance) + @indirection.find(@name) + end end describe Puppet::Indirector::Indirection, " when removing a model instance" do @@ -77,8 +85,6 @@ describe Puppet::Indirector::Indirection, " when removing a model instance" do @terminus.expects(:destroy).with(@name).returns(@instance) @indirection.destroy(@name).should == @instance end - - it "should call the terminus's authorization hook if there is one" end describe Puppet::Indirector::Indirection, " when searching for multiple model instances" do @@ -89,7 +95,15 @@ describe Puppet::Indirector::Indirection, " when searching for multiple model in @indirection.search(@name).should == @instance end - it "should call the terminus's authorization hook if there is one" + it "should pass the instances to the :post_search hook if there is one" do + class << @terminus + def post_search + end + end + @terminus.expects(:post_search).with(@instance) + @terminus.expects(:search).with(@name).returns(@instance) + @indirection.search(@name) + end end describe Puppet::Indirector::Indirection, " when storing a model instance" do @@ -99,8 +113,6 @@ describe Puppet::Indirector::Indirection, " when storing a model instance" do @terminus.expects(:save).with(@instance).returns(@instance) @indirection.save(@instance).should == @instance end - - it "should call the terminus's authorization hook if there is one" end describe Puppet::Indirector::Indirection, " when handling instance versions" do @@ -111,8 +123,6 @@ describe Puppet::Indirector::Indirection, " when handling instance versions" do @indirection.version(@name).should == 5 end - it "should call the terminus's authorization hook if there is one" - it "should add versions to found instances that do not already have them" do @terminus.expects(:find).with(@name).returns(@instance) time = mock 'time' @@ -470,3 +480,71 @@ describe Puppet::Indirector::Indirection, " when finding and using a cache" do @indirection.find(name).should equal(real) end end + +describe Puppet::Indirector::Indirection, " when an authorization hook is present" do + include IndirectionTesting + + before do + # So the :respond_to? turns out right. + 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(:find) + @indirection.find("/my/key") + end + + it "should fail while finding instances if authorization returns false" do + @terminus.expects(:authorized?).with(:find, "/my/key", :node => "mynode").returns(false) + @terminus.stubs(:find) + proc { @indirection.find("/my/key", :node => "mynode") }.should raise_error(ArgumentError) + end + + it "should continue finding instances if authorization returns true" do + @terminus.expects(:authorized?).with(:find, "/my/key", :node => "mynode").returns(true) + @terminus.stubs(:find) + @indirection.find("/my/key", :node => "mynode") + end + + it "should fail while saving instances if authorization returns false" do + @terminus.expects(:authorized?).with(:save, :myinstance, :node => "mynode").returns(false) + @terminus.stubs(:save) + proc { @indirection.save(:myinstance, :node => "mynode") }.should raise_error(ArgumentError) + end + + it "should continue saving instances if authorization returns true" do + instance = stub 'instance', :version => 1.0, :name => "eh" + @terminus.expects(:authorized?).with(:save, instance, :node => "mynode").returns(true) + @terminus.stubs(:save) + @indirection.save(instance, :node => "mynode") + end + + it "should fail while destroying instances if authorization returns false" do + @terminus.expects(:authorized?).with(:destroy, "/my/key", :node => "mynode").returns(false) + @terminus.stubs(:destroy) + proc { @indirection.destroy("/my/key", :node => "mynode") }.should raise_error(ArgumentError) + end + + it "should continue destroying instances if authorization returns true" do + instance = stub 'instance', :version => 1.0, :name => "eh" + @terminus.expects(:authorized?).with(:destroy, instance, :node => "mynode").returns(true) + @terminus.stubs(:destroy) + @indirection.destroy(instance, :node => "mynode") + end + + it "should fail while searching for instances if authorization returns false" do + @terminus.expects(:authorized?).with(:search, "/my/key", :node => "mynode").returns(false) + @terminus.stubs(:search) + proc { @indirection.search("/my/key", :node => "mynode") }.should raise_error(ArgumentError) + end + + it "should continue searching for instances if authorization returns true" do + @terminus.expects(:authorized?).with(:search, "/my/key", :node => "mynode").returns(true) + @terminus.stubs(:search) + @indirection.search("/my/key", :node => "mynode") + end +end diff --git a/spec/unit/indirector/module_files.rb b/spec/unit/indirector/module_files.rb index f2e43f771..9cb683c3c 100755 --- a/spec/unit/indirector/module_files.rb +++ b/spec/unit/indirector/module_files.rb @@ -62,10 +62,10 @@ describe Puppet::Indirector::ModuleFiles, " when finding files" do @module_files.find(@uri).should be_nil end - it "should return an instance of the model created with the full path if a module is found and the file exists" do + it "should return an instance of the model if a module is found and the file exists" do Puppet::Module.expects(:find).with('my', nil).returns @module FileTest.expects(:exists?).with("/module/path/files/local/file").returns(true) - @model.expects(:new).with("/module/path/files/local/file", :links => nil).returns(:myinstance) + @model.expects(:new).returns(:myinstance) @module_files.find(@uri).should == :myinstance end @@ -92,12 +92,32 @@ end describe Puppet::Indirector::ModuleFiles, " when returning instances" do include ModuleFilesTerminusTesting - it "should pass the provided :links setting on to the instance if one is provided" do + before do Puppet::Module.expects(:find).with('my', nil).returns @module FileTest.expects(:exists?).with("/module/path/files/local/file").returns(true) - @model.expects(:new).with("/module/path/files/local/file", :links => :mytest) + @instance = mock 'instance' + end + + it "should create the instance with the key used to find the instance" do + @model.expects(:new).with { |key, *options| key == @uri } + @module_files.find(@uri) + end + + it "should create the instance with the path at which the instance was found" do + @model.expects(:new).with { |key, options| options[:path] == "/module/path/files/local/file" } + @module_files.find(@uri) + end + + it "should set the provided :links setting on to the instance if one is provided" do + @model.expects(:new).returns(@instance) + @instance.expects(:links=).with(:mytest) @module_files.find(@uri, :links => :mytest) end + + it "should not set a :links value if no :links parameter is provided" do + @model.expects(:new).returns(@instance) + @module_files.find(@uri) + end end describe Puppet::Indirector::ModuleFiles, " when authorizing" do @@ -213,14 +233,14 @@ describe Puppet::Indirector::ModuleFiles, " when searching for files" do it "should use :path2instances from the terminus_helper to return instances if a module is found and the file exists" do Puppet::Module.expects(:find).with('my', nil).returns @module FileTest.expects(:exists?).with("/module/path/files/local/file").returns(true) - @module_files.expects(:path2instances).with("/module/path/files/local/file", {}) + @module_files.expects(:path2instances).with(@uri, "/module/path/files/local/file", {}) @module_files.search(@uri) end it "should pass any options on to :path2instances" do Puppet::Module.expects(:find).with('my', nil).returns @module FileTest.expects(:exists?).with("/module/path/files/local/file").returns(true) - @module_files.expects(:path2instances).with("/module/path/files/local/file", :testing => :one, :other => :two) + @module_files.expects(:path2instances).with(@uri, "/module/path/files/local/file", :testing => :one, :other => :two) @module_files.search(@uri, :testing => :one, :other => :two) end end |
