diff options
26 files changed, 497 insertions, 304 deletions
diff --git a/lib/puppet/file_serving/content.rb b/lib/puppet/file_serving/content.rb index 063192d15..9398513e7 100644 --- a/lib/puppet/file_serving/content.rb +++ b/lib/puppet/file_serving/content.rb @@ -17,11 +17,11 @@ class Puppet::FileServing::Content < Puppet::FileServing::FileBase attr_reader :path # Read the content of our file in. - def content(base = nil) + def content # This stat can raise an exception, too. - raise(ArgumentError, "Cannot read the contents of links unless following links") if stat(base).ftype == "symlink" + raise(ArgumentError, "Cannot read the contents of links unless following links") if stat().ftype == "symlink" - ::File.read(full_path(base)) + ::File.read(full_path()) end # Just return the file contents as the yaml. This allows us to diff --git a/lib/puppet/file_serving/file_base.rb b/lib/puppet/file_serving/file_base.rb index b2e9a0656..7f169d1ea 100644 --- a/lib/puppet/file_serving/file_base.rb +++ b/lib/puppet/file_serving/file_base.rb @@ -7,18 +7,19 @@ require 'puppet/file_serving' # The base class for Content and Metadata; provides common # functionality like the behaviour around links. class Puppet::FileServing::FileBase - attr_accessor :path, :base_path + attr_accessor :key - def full_path(base = nil) - base ||= base_path || raise(ArgumentError, "You must set or provide a base path") + # Return the full path to our file. Fails if there's no path set. + def full_path + raise(ArgumentError, "You must set a path to get a file's path") unless self.path - full = File.join(base, self.path) + relative_path ? File.join(path, relative_path) : path end - def initialize(path, options = {}) + def initialize(key, options = {}) raise ArgumentError.new("Files must not be fully qualified") if path =~ /^#{::File::SEPARATOR}/ - @path = path + @key = key @links = :manage options.each do |param, value| @@ -30,17 +31,33 @@ class Puppet::FileServing::FileBase end end + # Determine how we deal with links. attr_reader :links def links=(value) raise(ArgumentError, ":links can only be set to :manage or :follow") unless [:manage, :follow].include?(value) @links = value end + # Set our base path. + attr_reader :path + def path=(path) + raise ArgumentError.new("Paths must be fully qualified") unless path =~ /^#{::File::SEPARATOR}/ + @path = path + end + + # Set a relative path; this is used for recursion, and sets + # the file's path relative to the initial recursion point. + attr_reader :relative_path + def relative_path=(path) + raise ArgumentError.new("Relative paths must not be fully qualified") if path =~ /^#{::File::SEPARATOR}/ + @relative_path = path + end + # Stat our file, using the appropriate link-sensitive method. - def stat(base = nil) + def stat unless defined?(@stat_method) @stat_method = self.links == :manage ? :lstat : :stat end - File.send(@stat_method, full_path(base)) + File.send(@stat_method, full_path()) end end diff --git a/lib/puppet/file_serving/metadata.rb b/lib/puppet/file_serving/metadata.rb index 410655731..e26e75844 100644 --- a/lib/puppet/file_serving/metadata.rb +++ b/lib/puppet/file_serving/metadata.rb @@ -11,6 +11,16 @@ require 'puppet/file_serving/indirection_hooks' # A class that handles retrieving file metadata. class Puppet::FileServing::Metadata < Puppet::FileServing::FileBase + module MetadataHelper + include Puppet::FileServing::IndirectionHooks + + def post_find(instance) + end + + def post_search(key, options = {}) + end + end + include Puppet::Util::Checksums extend Puppet::Indirector @@ -27,9 +37,9 @@ class Puppet::FileServing::Metadata < Puppet::FileServing::FileBase # Retrieve the attributes for this file, relative to a base directory. # Note that File.stat raises Errno::ENOENT if the file is absent and this # method does not catch that exception. - def collect_attributes(base = nil) - real_path = full_path(base) - stat = stat(base) + def collect_attributes + real_path = full_path() + stat = stat() @owner = stat.uid @group = stat.gid @ftype = stat.ftype diff --git a/lib/puppet/file_serving/terminus_helper.rb b/lib/puppet/file_serving/terminus_helper.rb index 9542cbf84..d465aa493 100644 --- a/lib/puppet/file_serving/terminus_helper.rb +++ b/lib/puppet/file_serving/terminus_helper.rb @@ -8,8 +8,12 @@ require 'puppet/file_serving/fileset' # Define some common methods for FileServing termini. module Puppet::FileServing::TerminusHelper # Create model instances for all files in a fileset. - def path2instances(path, options = {}) + def path2instances(key, path, options = {}) args = [:links, :ignore, :recurse].inject({}) { |hash, param| hash[param] = options[param] if options[param]; hash } - Puppet::FileServing::Fileset.new(path, args).files.collect { |file| model.new(file) } + Puppet::FileServing::Fileset.new(path, args).files.collect do |file| + inst = model.new(File.join(key, file), :path => path, :relative_path => file) + inst.links = options[:links] if options[:links] + inst + end end end diff --git a/lib/puppet/indirector/direct_file_server.rb b/lib/puppet/indirector/direct_file_server.rb new file mode 100644 index 000000000..31cc9aa16 --- /dev/null +++ b/lib/puppet/indirector/direct_file_server.rb @@ -0,0 +1,27 @@ +# +# Created by Luke Kanies on 2007-10-24. +# Copyright (c) 2007. All rights reserved. + +require 'puppet/file_serving/terminus_helper' +require 'puppet/util/uri_helper' +require 'puppet/indirector/terminus' + +class Puppet::Indirector::DirectFileServer < Puppet::Indirector::Terminus + + include Puppet::Util::URIHelper + include Puppet::FileServing::TerminusHelper + + def find(key, options = {}) + uri = key2uri(key) + return nil unless FileTest.exists?(uri.path) + instance = model.new(key, :path => uri.path) + instance.links = options[:links] if options[:links] + return instance + end + + def search(key, options = {}) + uri = key2uri(key) + return nil unless FileTest.exists?(uri.path) + path2instances(key, uri.path, options) + end +end diff --git a/lib/puppet/indirector/file_content/file.rb b/lib/puppet/indirector/file_content/file.rb index 4503a7919..30c79583c 100644 --- a/lib/puppet/indirector/file_content/file.rb +++ b/lib/puppet/indirector/file_content/file.rb @@ -3,26 +3,9 @@ # Copyright (c) 2007. All rights reserved. require 'puppet/file_serving/content' -require 'puppet/file_serving/terminus_helper' -require 'puppet/util/uri_helper' require 'puppet/indirector/file_content' -require 'puppet/indirector/file' +require 'puppet/indirector/direct_file_server' -class Puppet::Indirector::FileContent::File < Puppet::Indirector::File +class Puppet::Indirector::FileContent::File < Puppet::Indirector::DirectFileServer desc "Retrieve file contents from disk." - - include Puppet::Util::URIHelper - include Puppet::FileServing::TerminusHelper - - def find(key, options = {}) - uri = key2uri(key) - return nil unless FileTest.exists?(uri.path) - model.new(uri.path, :links => options[:links]) - end - - def search(key, options = {}) - uri = key2uri(key) - return nil unless FileTest.exists?(uri.path) - path2instances(uri.path, options) - end end diff --git a/lib/puppet/indirector/file_metadata/file.rb b/lib/puppet/indirector/file_metadata/file.rb index 823c26c36..b36846bbe 100644 --- a/lib/puppet/indirector/file_metadata/file.rb +++ b/lib/puppet/indirector/file_metadata/file.rb @@ -3,30 +3,24 @@ # Copyright (c) 2007. All rights reserved. require 'puppet/file_serving/metadata' -require 'puppet/file_serving/terminus_helper' require 'puppet/indirector/file_metadata' -require 'puppet/util/uri_helper' -require 'puppet/indirector/code' +require 'puppet/indirector/direct_file_server' -class Puppet::Indirector::FileMetadata::File < Puppet::Indirector::Code +class Puppet::Indirector::FileMetadata::File < Puppet::Indirector::DirectFileServer desc "Retrieve file metadata directly from the local filesystem." - include Puppet::Util::URIHelper - include Puppet::FileServing::TerminusHelper - def find(key, options = {}) - uri = key2uri(key) - - return nil unless FileTest.exists?(uri.path) - data = model.new(uri.path, :links => options[:links]) + return unless data = super data.collect_attributes return data end def search(key, options = {}) - uri = key2uri(key) - return nil unless FileTest.exists?(uri.path) - path2instances(uri.path, options).each { |instance| instance.collect_attributes } + return unless result = super + + result.each { |instance| instance.collect_attributes } + + return result end end diff --git a/lib/puppet/indirector/file_metadata/modules.rb b/lib/puppet/indirector/file_metadata/modules.rb index 739c40fca..5ed7a8a45 100644 --- a/lib/puppet/indirector/file_metadata/modules.rb +++ b/lib/puppet/indirector/file_metadata/modules.rb @@ -11,7 +11,7 @@ class Puppet::Indirector::FileMetadata::Modules < Puppet::Indirector::ModuleFile def find(*args) return unless instance = super - instance.get_attributes + instance.collect_attributes instance end end diff --git a/lib/puppet/indirector/file_server.rb b/lib/puppet/indirector/file_server.rb index de88bdc18..2eb323d46 100644 --- a/lib/puppet/indirector/file_server.rb +++ b/lib/puppet/indirector/file_server.rb @@ -25,7 +25,9 @@ class Puppet::Indirector::FileServer < Puppet::Indirector::Terminus # Find our key using the fileserver. def find(key, options = {}) return nil unless path = find_path(key, options) - return model.new(path, :links => options[:links]) + result = model.new(key, :path => path) + result.links = options[:links] if options[:links] + return result end # Search for files. This returns an array rather than a single @@ -33,7 +35,7 @@ class Puppet::Indirector::FileServer < Puppet::Indirector::Terminus def search(key, options = {}) return nil unless path = find_path(key, options) - path2instances(path, options) + path2instances(key, path, options) end private diff --git a/lib/puppet/indirector/indirection.rb b/lib/puppet/indirector/indirection.rb index 60278be98..9a9b1c0bf 100644 --- a/lib/puppet/indirector/indirection.rb +++ b/lib/puppet/indirector/indirection.rb @@ -168,6 +168,10 @@ class Puppet::Indirector::Indirection # Check authorization if there's a hook available; fail if there is one # and it returns false. def check_authorization(method, terminus_name, arguments) + # Don't check authorization if there's no node. + # LAK:FIXME This is a hack and is quite possibly not the design we want. + return unless arguments[-1].is_a?(Hash) and arguments[-1][:node] + if terminus(terminus_name).respond_to?(:authorized?) and ! terminus(terminus_name).authorized?(method, *arguments) raise ArgumentError, "Not authorized to call %s with %s" % [method, arguments[0]] end diff --git a/lib/puppet/indirector/module_files.rb b/lib/puppet/indirector/module_files.rb index 12794e4c7..c79fae57b 100644 --- a/lib/puppet/indirector/module_files.rb +++ b/lib/puppet/indirector/module_files.rb @@ -30,7 +30,9 @@ class Puppet::Indirector::ModuleFiles < Puppet::Indirector::Terminus def find(key, options = {}) return nil unless path = find_path(key, options) - return model.new(path, :links => options[:links]) + result = model.new(key, :path => path) + result.links = options[:links] if options[:links] + return result end # Try to find our module. @@ -41,7 +43,7 @@ class Puppet::Indirector::ModuleFiles < Puppet::Indirector::Terminus # Search for a list of files. def search(key, options = {}) return nil unless path = find_path(key, options) - path2instances(path, options) + path2instances(key, path, options) end private diff --git a/lib/puppet/type/pfile.rb b/lib/puppet/type/pfile.rb index 2b3df1ae7..f0a805525 100644 --- a/lib/puppet/type/pfile.rb +++ b/lib/puppet/type/pfile.rb @@ -618,7 +618,6 @@ module Puppet # than this last bit, so it doesn't really make sense. if child = configuration.resource(:file, path) unless child.parent.object_id == self.object_id - puts("Parent is %s, I am %s" % [child.parent.ref, self.ref]) if child.parent self.debug "Not managing more explicit file %s" % path return nil diff --git a/spec/integration/indirector/direct_file_server.rb b/spec/integration/indirector/direct_file_server.rb new file mode 100755 index 000000000..383486986 --- /dev/null +++ b/spec/integration/indirector/direct_file_server.rb @@ -0,0 +1,78 @@ +#!/usr/bin/env ruby +# +# Created by Luke Kanies on 2007-10-19. +# Copyright (c) 2007. All rights reserved. + +require File.dirname(__FILE__) + '/../../spec_helper' + +require 'puppet/indirector/file_content/file' +require 'puppet/indirector/module_files' + +describe Puppet::Indirector::DirectFileServer, " when interacting with the filesystem and the model" do + before do + # We just test a subclass, since it's close enough. + @terminus = Puppet::Indirector::FileContent::File.new + + @filepath = "/path/to/my/file" + end + + it "should return an instance of the model" do + FileTest.expects(:exists?).with(@filepath).returns(true) + + @terminus.find("file://host#{@filepath}").should be_instance_of(Puppet::FileServing::Content) + end + + it "should return an instance capable of returning its content" do + FileTest.expects(:exists?).with(@filepath).returns(true) + File.stubs(:lstat).with(@filepath).returns(stub("stat", :ftype => "file")) + File.expects(:read).with(@filepath).returns("my content") + + instance = @terminus.find("file://host#{@filepath}") + + instance.content.should == "my content" + end +end + +describe Puppet::Indirector::DirectFileServer, " when interacting with FileServing::Fileset and the model" do + before do + @terminus = Puppet::Indirector::FileContent::File.new + + @filepath = "/my/file" + FileTest.stubs(:exists?).with(@filepath).returns(true) + + stat = stub 'stat', :directory? => true + File.stubs(:lstat).with(@filepath).returns(stat) + + @subfiles = %w{one two} + @subfiles.each do |f| + path = File.join(@filepath, f) + FileTest.stubs(:exists?).with(@path).returns(true) + end + + Dir.expects(:entries).with(@filepath).returns @subfiles + end + + it "should return an instance for every file in the fileset" do + result = @terminus.search("file:///my/file", :recurse => true) + result.should be_instance_of(Array) + result.length.should == 3 + result.each { |r| r.should be_instance_of(Puppet::FileServing::Content) } + end + + it "should return instances capable of returning their content" do + @subfiles.each do |name| + File.stubs(:lstat).with(File.join(@filepath, name)).returns stub("#{name} stat", :ftype => "file", :directory? => false) + File.expects(:read).with(File.join(@filepath, name)).returns("#{name} content") + end + + @terminus.search("file:///my/file", :recurse => true).each do |instance| + case instance.key + when /one/: instance.content.should == "one content" + when /two/: instance.content.should == "two content" + when /\.$/: + else + raise "No valid key for %s" % instance.key.inspect + end + end + end +end diff --git a/spec/integration/indirector/module_files.rb b/spec/integration/indirector/module_files.rb index 3725a1286..3f49ec7fa 100755 --- a/spec/integration/indirector/module_files.rb +++ b/spec/integration/indirector/module_files.rb @@ -5,13 +5,13 @@ require File.dirname(__FILE__) + '/../../spec_helper' -require 'puppet/indirector/file_metadata/modules' +require 'puppet/indirector/file_content/modules' require 'puppet/indirector/module_files' -describe Puppet::Indirector::ModuleFiles, " when interacting with Puppet::Module" do +describe Puppet::Indirector::ModuleFiles, " when interacting with Puppet::Module and FileServing::Content" do it "should look for files in the module's 'files' directory" do # We just test a subclass, since it's close enough. - @terminus = Puppet::Indirector::FileMetadata::Modules.new + @terminus = Puppet::Indirector::FileContent::Modules.new @module = Puppet::Module.new("mymod", "/some/path/mymod") Puppet::Module.expects(:find).with("mymod", nil).returns(@module) @@ -19,8 +19,33 @@ describe Puppet::Indirector::ModuleFiles, " when interacting with Puppet::Module FileTest.expects(:exists?).with(filepath).returns(true) - @terminus.model.expects(:new).with(filepath, :links => nil) + @terminus.find("puppetmounts://host/modules/mymod/myfile").should be_instance_of(Puppet::FileServing::Content) + end +end + +describe Puppet::Indirector::ModuleFiles, " when interacting with FileServing::Fileset and FileServing::Content" do + it "should return an instance for every file in the fileset" do + @terminus = Puppet::Indirector::FileContent::Modules.new + @module = Puppet::Module.new("mymod", "/some/path/mymod") + Puppet::Module.expects(:find).with("mymod", nil).returns(@module) + + filepath = "/some/path/mymod/files/myfile" + FileTest.stubs(:exists?).with(filepath).returns(true) + + stat = stub 'stat', :directory? => true + File.stubs(:lstat).with(filepath).returns(stat) + + subfiles = %w{one two} + subfiles.each do |f| + path = File.join(filepath, f) + FileTest.stubs(:exists?).with(path).returns(true) + end + + Dir.expects(:entries).with(filepath).returns(%w{one two}) - @terminus.find("puppetmounts://host/modules/mymod/myfile") + result = @terminus.search("puppetmounts://host/modules/mymod/myfile", :recurse => true) + result.should be_instance_of(Array) + result.length.should == 3 + result.each { |r| r.should be_instance_of(Puppet::FileServing::Content) } end end diff --git a/spec/lib/shared_behaviours/file_server_terminus.rb b/spec/lib/shared_behaviours/file_server_terminus.rb index e1ec35251..de08f29fc 100644 --- a/spec/lib/shared_behaviours/file_server_terminus.rb +++ b/spec/lib/shared_behaviours/file_server_terminus.rb @@ -32,10 +32,9 @@ describe "Puppet::Indirector::FileServerTerminus", :shared => true do path = "/my/mount/path/my/file" FileTest.stubs(:exists?).with(path).returns(true) - @test_class.expects(:new).with(path, :links => nil).returns(:myinstance) FileTest.stubs(:exists?).with("/my/mount/path").returns(true) @mount1.expects(:file).with("my/file", :node => nil).returns(path) - @terminus.find("puppetmounts://myhost/one/my/file").should == :myinstance + @terminus.find("puppetmounts://myhost/one/my/file").should be_instance_of(@test_class) end end diff --git a/spec/unit/file_serving/content.rb b/spec/unit/file_serving/content.rb index d6b5fdbe7..89d786295 100755 --- a/spec/unit/file_serving/content.rb +++ b/spec/unit/file_serving/content.rb @@ -18,79 +18,43 @@ describe Puppet::FileServing::Content do end end -describe Puppet::FileServing::Content, " when initializing" do - it "should accept a file path" do - Puppet::FileServing::Content.new("not/qualified").path.should == "not/qualified" - end - - it "should not allow a fully qualified file path" do - proc { Puppet::FileServing::Content.new("/fully/qualified") }.should raise_error(ArgumentError) - end - - it "should allow specification of whether links should be managed" do - Puppet::FileServing::Content.new("not/qualified", :links => :manage) - end - - it "should fail if :links is set to anything other than :manage or :follow" do - Puppet::FileServing::Content.new("not/qualified", :links => :manage) - end - - it "should default to :manage for :links" do - Puppet::FileServing::Content.new("not/qualified", :links => :manage) - end -end - describe Puppet::FileServing::Content, " when returning the contents" do before do - @content = Puppet::FileServing::Content.new("sub/path", :links => :follow) - @base = "/my/base" - @full = "/my/base/sub/path" + @path = "/my/base" + @content = Puppet::FileServing::Content.new("sub/path", :links => :follow, :path => @path) end it "should fail if the file is a symlink and links are set to :manage" do @content.links = :manage - File.expects(:lstat).with(@full).returns stub("stat", :ftype => "symlink") - proc { @content.content(@base) }.should raise_error(ArgumentError) - end - - it "should accept a base path path to which the file should be relative" do - File.expects(:stat).with(@full).returns stub("stat", :ftype => "file") - File.expects(:read).with(@full).returns(:mycontent) - @content.content(@base).should == :mycontent + File.expects(:lstat).with(@path).returns stub("stat", :ftype => "symlink") + proc { @content.content }.should raise_error(ArgumentError) end - it "should use the set base path if one is not provided" do - @content.base_path = @base - File.expects(:stat).with(@full).returns stub("stat", :ftype => "file") - File.expects(:read).with(@full).returns(:mycontent) - @content.content() - end - - it "should fail if a base path is neither set nor provided" do - proc { @content.content() }.should raise_error(ArgumentError) + it "should fail if a path is not set" do + proc { @content.content() }.should raise_error(Errno::ENOENT) end it "should raise Errno::ENOENT if the file is absent" do - @content.base_path = "/there/is/absolutely/no/chance/that/this/path/exists" + @content.path = "/there/is/absolutely/no/chance/that/this/path/exists" proc { @content.content() }.should raise_error(Errno::ENOENT) end it "should return the contents of the path if the file exists" do - File.expects(:stat).with(@full).returns stub("stat", :ftype => "file") - File.expects(:read).with(@full).returns(:mycontent) - @content.content(@base).should == :mycontent + File.expects(:stat).with(@path).returns stub("stat", :ftype => "file") + File.expects(:read).with(@path).returns(:mycontent) + @content.content.should == :mycontent end end describe Puppet::FileServing::Content, " when converting to yaml" do - it "should fail if no base path has been set" do - @content = Puppet::FileServing::Content.new("some/path") + it "should fail if no path has been set" do + @content = Puppet::FileServing::Content.new("some/key") proc { @content.to_yaml }.should raise_error(ArgumentError) end it "should return the file contents" do @content = Puppet::FileServing::Content.new("some/path") - @content.base_path = "/base/path" + @content.path = "/base/path" @content.expects(:content).returns(:content) @content.to_yaml.should == :content end diff --git a/spec/unit/file_serving/file_base.rb b/spec/unit/file_serving/file_base.rb index 14be6d003..4c7724f7c 100755 --- a/spec/unit/file_serving/file_base.rb +++ b/spec/unit/file_serving/file_base.rb @@ -5,48 +5,67 @@ require File.dirname(__FILE__) + '/../../spec_helper' require 'puppet/file_serving/file_base' describe Puppet::FileServing::FileBase, " when initializing" do - it "should accept a file path" do - Puppet::FileServing::FileBase.new("not/qualified").path.should == "not/qualified" - end - - it "should not allow a fully qualified file path" do - proc { Puppet::FileServing::FileBase.new("/fully/qualified") }.should raise_error(ArgumentError) + it "should accept a key in the form of a URI" do + Puppet::FileServing::FileBase.new("puppet://host/module/dir/file").key.should == "puppet://host/module/dir/file" end it "should allow specification of whether links should be managed" do - Puppet::FileServing::FileBase.new("not/qualified", :links => :manage).links.should == :manage + Puppet::FileServing::FileBase.new("puppet://host/module/dir/file", :links => :manage).links.should == :manage end it "should fail if :links is set to anything other than :manage or :follow" do - proc { Puppet::FileServing::FileBase.new("not/qualified", :links => :else) }.should raise_error(ArgumentError) + proc { Puppet::FileServing::FileBase.new("puppet://host/module/dir/file", :links => :else) }.should raise_error(ArgumentError) end it "should default to :manage for :links" do - Puppet::FileServing::FileBase.new("not/qualified").links.should == :manage + Puppet::FileServing::FileBase.new("puppet://host/module/dir/file").links.should == :manage + end + + it "should allow specification of a path" do + FileTest.stubs(:exists?).returns(true) + Puppet::FileServing::FileBase.new("puppet://host/module/dir/file", :path => "/my/file").path.should == "/my/file" + end + + it "should allow specification of a relative path" do + FileTest.stubs(:exists?).returns(true) + Puppet::FileServing::FileBase.new("puppet://host/module/dir/file", :relative_path => "my/file").relative_path.should == "my/file" end end -describe Puppet::FileServing::FileBase do - it "should provide a method for setting the base path" do - @file = Puppet::FileServing::FileBase.new("not/qualified") - @file.base_path = "/something" - @file.base_path.should == "/something" +describe Puppet::FileServing::FileBase, " when setting the base path" do + before do + @file = Puppet::FileServing::FileBase.new("puppet://host/module/dir/file") + end + + it "should require that the base path be fully qualified" do + FileTest.stubs(:exists?).returns(true) + proc { @file.path = "unqualified/file" }.should raise_error(ArgumentError) + end +end + +describe Puppet::FileServing::FileBase, " when setting the relative path" do + it "should require that the relative path be unqualified" do + @file = Puppet::FileServing::FileBase.new("puppet://host/module/dir/file") + FileTest.stubs(:exists?).returns(true) + proc { @file.relative_path = "/qualified/file" }.should raise_error(ArgumentError) end end describe Puppet::FileServing::FileBase, " when determining the full file path" do - it "should return the provided path joined with the qualified path if a path is provided" do - @file = Puppet::FileServing::FileBase.new("not/qualified") - @file.full_path("/this/file").should == "/this/file/not/qualified" + before do + @file = Puppet::FileServing::FileBase.new("mykey", :path => "/this/file") end - it "should return the set base path joined with the qualified path if a base path is set" do - @file = Puppet::FileServing::FileBase.new("not/qualified") - @file.base_path = "/this/file" + it "should return the path if there is no relative path" do + @file.full_path.should == "/this/file" + end + + it "should return the path joined with the relative path if there is a relative path" do + @file.relative_path = "not/qualified" @file.full_path.should == "/this/file/not/qualified" end - it "should should fail if a base path is neither provided nor set" do + it "should should fail if there is no path set" do @file = Puppet::FileServing::FileBase.new("not/qualified") proc { @file.full_path }.should raise_error(ArgumentError) end @@ -54,32 +73,29 @@ end describe Puppet::FileServing::FileBase, " when stat'ing files" do before do - @file = Puppet::FileServing::FileBase.new("not/qualified") - end - - it "should join the provided path with the qualified path is a path is provided" do - File.expects(:lstat).with("/this/file/not/qualified").returns stub("stat", :ftype => "file") - @file.stat("/this/file") + @file = Puppet::FileServing::FileBase.new("mykey", :path => "/this/file") end - it "should use the set base path if no base is provided" do - @file.base_path = "/this/file" - File.expects(:lstat).with("/this/file/not/qualified").returns stub("stat", :ftype => "file") + it "should stat the file's full path" do + @file.stubs(:full_path).returns("/this/file") + File.expects(:lstat).with("/this/file").returns stub("stat", :ftype => "file") @file.stat end - it "should fail if a base path is neither set nor provided" do - proc { @file.stat }.should raise_error(ArgumentError) + it "should fail if the file does not exist" do + @file.stubs(:full_path).returns("/this/file") + File.expects(:lstat).with("/this/file").raises(Errno::ENOENT) + proc { @file.stat }.should raise_error(Errno::ENOENT) end it "should use :lstat if :links is set to :manage" do - File.expects(:lstat).with("/this/file/not/qualified").returns stub("stat", :ftype => "file") - @file.stat("/this/file") + File.expects(:lstat).with("/this/file").returns stub("stat", :ftype => "file") + @file.stat end it "should use :stat if :links is set to :follow" do - File.expects(:stat).with("/this/file/not/qualified").returns stub("stat", :ftype => "file") + File.expects(:stat).with("/this/file").returns stub("stat", :ftype => "file") @file.links = :follow - @file.stat("/this/file") + @file.stat end end diff --git a/spec/unit/file_serving/metadata.rb b/spec/unit/file_serving/metadata.rb index bdddd255b..f7ab0c8d6 100755 --- a/spec/unit/file_serving/metadata.rb +++ b/spec/unit/file_serving/metadata.rb @@ -5,6 +5,10 @@ require File.dirname(__FILE__) + '/../../spec_helper' require 'puppet/file_serving/metadata' describe Puppet::FileServing::Metadata do + it "should should be a subclass of FileBase" do + Puppet::FileServing::Metadata.superclass.should equal(Puppet::FileServing::FileBase) + end + it "should indirect file_metadata" do Puppet::FileServing::Metadata.indirection.name.should == :file_metadata end @@ -14,39 +18,14 @@ describe Puppet::FileServing::Metadata do end end -describe Puppet::FileServing::Metadata, " when initializing" do - it "should not allow initialization without a path" do - proc { Puppet::FileServing::Metadata.new() }.should raise_error(ArgumentError) - end - - it "should not allow the path to be fully qualified if it is provided" do - proc { Puppet::FileServing::Metadata.new("/fully/qualified") }.should raise_error(ArgumentError) - end - - it "should allow initialization with a relative path" do - Puppet::FileServing::Metadata.new("not/fully/qualified") - end - - it "should allow specification of whether links should be managed" do - Puppet::FileServing::Metadata.new("not/qualified", :links => :manage) - end - - it "should fail if :links is set to anything other than :manage or :follow" do - Puppet::FileServing::Metadata.new("not/qualified", :links => :manage) - end - - it "should default to :manage for :links" do - Puppet::FileServing::Metadata.new("not/qualified", :links => :manage) - end -end - describe Puppet::FileServing::Metadata, " when finding the file to use for setting attributes" do before do @metadata = Puppet::FileServing::Metadata.new("my/path") - @base = "/base/path" @full = "/base/path/my/path" + @metadata.path = @full + # Use a symlink because it's easier to test -- no checksumming @stat = stub "stat", :uid => 10, :gid => 20, :mode => 0755, :ftype => "symlink" end @@ -54,23 +33,22 @@ describe Puppet::FileServing::Metadata, " when finding the file to use for setti it "should accept a base path path to which the file should be relative" do File.expects(:lstat).with(@full).returns @stat File.expects(:readlink).with(@full).returns "/what/ever" - @metadata.collect_attributes(@base) + @metadata.collect_attributes end it "should use the set base path if one is not provided" do - @metadata.base_path = @base File.expects(:lstat).with(@full).returns @stat File.expects(:readlink).with(@full).returns "/what/ever" @metadata.collect_attributes() end it "should fail if a base path is neither set nor provided" do - proc { @metadata.collect_attributes() }.should raise_error(ArgumentError) + proc { @metadata.collect_attributes() }.should raise_error(Errno::ENOENT) end it "should raise an exception if the file does not exist" do - File.expects(:lstat).with("/base/dir/my/path").raises(Errno::ENOENT) - proc { @metadata.collect_attributes("/base/dir")}.should raise_error(Errno::ENOENT) + File.expects(:lstat).with(@full).raises(Errno::ENOENT) + proc { @metadata.collect_attributes()}.should raise_error(Errno::ENOENT) end end @@ -83,12 +61,8 @@ describe Puppet::FileServing::Metadata, " when collecting attributes" do @filehandle.expects(:each_line).yields("some content\n") File.stubs(:open).with(@path, 'r').yields(@filehandle) @checksum = Digest::MD5.hexdigest("some content\n") - @metadata = Puppet::FileServing::Metadata.new("file") - @metadata.collect_attributes("/my") - end - - it "should accept a file path" do - @metadata.path.should == "file" + @metadata = Puppet::FileServing::Metadata.new("file", :path => "/my/file") + @metadata.collect_attributes end # LAK:FIXME This should actually change at some point @@ -128,22 +102,22 @@ end describe Puppet::FileServing::Metadata, " when pointing to a symlink" do it "should store the destination of the symlink in :destination if links are :manage" do - file = Puppet::FileServing::Metadata.new("my/file", :links => :manage) + file = Puppet::FileServing::Metadata.new("mykey", :links => :manage, :path => "/base/path/my/file") File.expects(:lstat).with("/base/path/my/file").returns stub("stat", :uid => 1, :gid => 2, :ftype => "symlink", :mode => 0755) File.expects(:readlink).with("/base/path/my/file").returns "/some/other/path" - file.collect_attributes("/base/path") + file.collect_attributes file.destination.should == "/some/other/path" end it "should not collect the checksum" do - file = Puppet::FileServing::Metadata.new("my/file", :links => :manage) + file = Puppet::FileServing::Metadata.new("my/file", :links => :manage, :path => "/base/path/my/file") File.expects(:lstat).with("/base/path/my/file").returns stub("stat", :uid => 1, :gid => 2, :ftype => "symlink", :mode => 0755) File.expects(:readlink).with("/base/path/my/file").returns "/some/other/path" - file.collect_attributes("/base/path") + file.collect_attributes file.checksum.should be_nil end end diff --git a/spec/unit/file_serving/terminus_helper.rb b/spec/unit/file_serving/terminus_helper.rb index 3a5274b5a..b919469a2 100755 --- a/spec/unit/file_serving/terminus_helper.rb +++ b/spec/unit/file_serving/terminus_helper.rb @@ -19,20 +19,60 @@ describe Puppet::FileServing::TerminusHelper do it "should use a fileset to find paths" do fileset = mock 'fileset', :files => [] Puppet::FileServing::Fileset.expects(:new).with("/my/file", {}).returns(fileset) - @helper.path2instances("/my/file") + @helper.path2instances("url", "/my/file") end it "should pass :recurse, :ignore, and :links settings on to the fileset if present" do fileset = mock 'fileset', :files => [] Puppet::FileServing::Fileset.expects(:new).with("/my/file", :links => :a, :ignore => :b, :recurse => :c).returns(fileset) - @helper.path2instances("/my/file", :links => :a, :ignore => :b, :recurse => :c) + @helper.path2instances("url", "/my/file", :links => :a, :ignore => :b, :recurse => :c) end +end - it "should return an instance of the model for each path returned by the fileset" do - fileset = mock 'fileset', :files => %w{one two} - Puppet::FileServing::Fileset.expects(:new).with("/my/file", {}).returns(fileset) - @model.expects(:new).with("one").returns(:one) - @model.expects(:new).with("two").returns(:two) - @helper.path2instances("/my/file").should == [:one, :two] + +describe Puppet::FileServing::TerminusHelper, " when creating instances" do + before do + @helper = Object.new + @helper.extend(Puppet::FileServing::TerminusHelper) + + @model = mock 'model' + @helper.stubs(:model).returns(@model) + + @key = "puppet://host/mount/dir" + + @fileset = mock 'fileset', :files => %w{one two} + Puppet::FileServing::Fileset.expects(:new).returns(@fileset) + end + + it "should create an instance of the model for each path returned by the fileset" do + @model.expects(:new).returns(:one) + @model.expects(:new).returns(:two) + @helper.path2instances(@key, "/my/file").length.should == 2 + end + + it "should set each instance's key to be the original key plus the file-specific path" do + @model.expects(:new).with { |key, options| key == @key + "/one" }.returns(:one) + @model.expects(:new).with { |key, options| key == @key + "/two" }.returns(:two) + @helper.path2instances(@key, "/my/file") + end + + it "should set each returned instance's path to the original path" do + @model.expects(:new).with { |key, options| options[:path] == "/my/file" }.returns(:one) + @model.expects(:new).with { |key, options| options[:path] == "/my/file" }.returns(:two) + @helper.path2instances(@key, "/my/file") + end + + it "should set each returned instance's relative path to the file-specific path" do + @model.expects(:new).with { |key, options| options[:relative_path] == "one" }.returns(:one) + @model.expects(:new).with { |key, options| options[:relative_path] == "two" }.returns(:two) + @helper.path2instances(@key, "/my/file") + end + + it "should set the links value on each instance if one is provided" do + one = mock 'one', :links= => :manage + two = mock 'two', :links= => :manage + @model.expects(:new).returns(one) + @model.expects(:new).returns(two) + @helper.path2instances(@key, "/my/file", :links => :manage) end end 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 8987fef8a..0ac2356d6 100755 --- a/spec/unit/indirector/indirection.rb +++ b/spec/unit/indirector/indirection.rb @@ -482,6 +482,12 @@ describe Puppet::Indirector::Indirection, " when an authorization hook is presen 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) 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 |