diff options
-rw-r--r-- | lib/puppet/file_serving/content.rb | 1 | ||||
-rw-r--r-- | lib/puppet/file_serving/fileset.rb | 2 | ||||
-rw-r--r-- | lib/puppet/file_serving/metadata.rb | 2 | ||||
-rw-r--r-- | lib/puppet/file_serving/terminus_helper.rb | 1 | ||||
-rw-r--r-- | lib/puppet/indirector/file_metadata/file.rb | 4 | ||||
-rw-r--r-- | lib/puppet/indirector/file_metadata/modules.rb | 2 | ||||
-rw-r--r-- | lib/puppet/indirector/file_server.rb | 1 | ||||
-rwxr-xr-x | lib/puppet/network/handler/fileserver.rb | 2 | ||||
-rwxr-xr-x | spec/integration/indirector/direct_file_server.rb | 27 | ||||
-rwxr-xr-x | spec/integration/indirector/module_files.rb | 24 | ||||
-rw-r--r-- | spec/shared_behaviours/file_server_terminus.rb | 20 | ||||
-rwxr-xr-x | spec/unit/file_serving/content.rb | 13 | ||||
-rwxr-xr-x | spec/unit/file_serving/metadata.rb | 22 | ||||
-rwxr-xr-x | spec/unit/file_serving/terminus_helper.rb | 32 | ||||
-rwxr-xr-x | spec/unit/indirector/direct_file_server.rb | 2 | ||||
-rwxr-xr-x | spec/unit/indirector/file_metadata/file.rb | 6 | ||||
-rwxr-xr-x | spec/unit/indirector/file_metadata/modules.rb | 2 | ||||
-rwxr-xr-x | spec/unit/indirector/file_server.rb | 23 |
18 files changed, 112 insertions, 74 deletions
diff --git a/lib/puppet/file_serving/content.rb b/lib/puppet/file_serving/content.rb index 0f169c28b..c1ecff749 100644 --- a/lib/puppet/file_serving/content.rb +++ b/lib/puppet/file_serving/content.rb @@ -28,6 +28,7 @@ class Puppet::FileServing::Content < Puppet::FileServing::Base # Collect our data. def collect + return if stat.ftype == "directory" content end diff --git a/lib/puppet/file_serving/fileset.rb b/lib/puppet/file_serving/fileset.rb index fe54350b1..80a718c68 100644 --- a/lib/puppet/file_serving/fileset.rb +++ b/lib/puppet/file_serving/fileset.rb @@ -20,7 +20,7 @@ class Puppet::FileServing::Fileset # Now strip off the leading path, so each file becomes relative, and remove # any slashes that might end up at the beginning of the path. - result = files.collect { |file| file.sub(%r{^#{@path}/*}, '') } + result = files.collect { |file| file.sub(@path, '').sub(%r{^/},'') } # And add the path itself. result.unshift(".") diff --git a/lib/puppet/file_serving/metadata.rb b/lib/puppet/file_serving/metadata.rb index a1265dd8b..1cc3fa355 100644 --- a/lib/puppet/file_serving/metadata.rb +++ b/lib/puppet/file_serving/metadata.rb @@ -47,7 +47,7 @@ class Puppet::FileServing::Metadata < Puppet::FileServing::Base # 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 + def collect real_path = full_path() stat = stat() @owner = stat.uid diff --git a/lib/puppet/file_serving/terminus_helper.rb b/lib/puppet/file_serving/terminus_helper.rb index bde0bd389..598a5007a 100644 --- a/lib/puppet/file_serving/terminus_helper.rb +++ b/lib/puppet/file_serving/terminus_helper.rb @@ -13,6 +13,7 @@ module Puppet::FileServing::TerminusHelper Puppet::FileServing::Fileset.new(path, args).files.collect do |file| inst = model.new(path, :relative_path => file) inst.links = request.options[:links] if request.options[:links] + inst.collect inst end end diff --git a/lib/puppet/indirector/file_metadata/file.rb b/lib/puppet/indirector/file_metadata/file.rb index c46015c38..bb586489d 100644 --- a/lib/puppet/indirector/file_metadata/file.rb +++ b/lib/puppet/indirector/file_metadata/file.rb @@ -11,7 +11,7 @@ class Puppet::Indirector::FileMetadata::File < Puppet::Indirector::DirectFileSer def find(request) return unless data = super - data.collect_attributes + data.collect return data end @@ -19,7 +19,7 @@ class Puppet::Indirector::FileMetadata::File < Puppet::Indirector::DirectFileSer def search(request) return unless result = super - result.each { |instance| instance.collect_attributes } + result.each { |instance| instance.collect } return result end diff --git a/lib/puppet/indirector/file_metadata/modules.rb b/lib/puppet/indirector/file_metadata/modules.rb index 5ed7a8a45..4598c2175 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.collect_attributes + instance.collect instance end end diff --git a/lib/puppet/indirector/file_server.rb b/lib/puppet/indirector/file_server.rb index 476fc5b23..46a590f9c 100644 --- a/lib/puppet/indirector/file_server.rb +++ b/lib/puppet/indirector/file_server.rb @@ -27,6 +27,7 @@ class Puppet::Indirector::FileServer < Puppet::Indirector::Terminus return nil unless path = find_path(request) result = model.new(path) result.links = request.options[:links] if request.options[:links] + result.collect return result end diff --git a/lib/puppet/network/handler/fileserver.rb b/lib/puppet/network/handler/fileserver.rb index 183979429..14319ef96 100755 --- a/lib/puppet/network/handler/fileserver.rb +++ b/lib/puppet/network/handler/fileserver.rb @@ -75,7 +75,7 @@ class Puppet::Network::Handler return "" unless metadata.exist? begin - metadata.collect_attributes + metadata.collect rescue => detail puts detail.backtrace if Puppet[:trace] Puppet.err detail diff --git a/spec/integration/indirector/direct_file_server.rb b/spec/integration/indirector/direct_file_server.rb index 6f3da5169..417d661e8 100755 --- a/spec/integration/indirector/direct_file_server.rb +++ b/spec/integration/indirector/direct_file_server.rb @@ -37,21 +37,19 @@ describe Puppet::Indirector::DirectFileServer, " when interacting with FileServi before do @terminus = Puppet::Indirector::FileContent::File.new - @filepath = "/my/file" - FileTest.stubs(:exists?).with(@filepath).returns(true) + @path = Tempfile.new("direct_file_server_testing") + @path.close! + @path = @path.path - stat = stub 'stat', :directory? => true - File.stubs(:lstat).with(@filepath).returns(stat) + Dir.mkdir(@path) + File.open(File.join(@path, "one"), "w") { |f| f.print "one content" } + File.open(File.join(@path, "two"), "w") { |f| f.print "two content" } - @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 + @request = @terminus.indirection.request(:search, "file:///%s" % @path, :recurse => true) + end - @request = @terminus.indirection.request(:search, "file:///my/file", :recurse => true) + after do + system("rm -rf %s" % @path) end it "should return an instance for every file in the fileset" do @@ -62,11 +60,6 @@ describe Puppet::Indirector::DirectFileServer, " when interacting with FileServi 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(@request).each do |instance| case instance.full_path when /one/: instance.content.should == "one content" diff --git a/spec/integration/indirector/module_files.rb b/spec/integration/indirector/module_files.rb index 6cbbd3dbd..a54588ec5 100755 --- a/spec/integration/indirector/module_files.rb +++ b/spec/integration/indirector/module_files.rb @@ -30,22 +30,22 @@ describe Puppet::Indirector::ModuleFiles, " when interacting with FileServing::F it "should return an instance for every file in the fileset" do Puppet::Node::Environment.stubs(:new).returns(stub('env', :name => "myenv")) @terminus = Puppet::Indirector::FileContent::Modules.new - @module = Puppet::Module.new("mymod", "/some/path/mymod") - Puppet::Module.expects(:find).with("mymod", "myenv").returns(@module) - filepath = "/some/path/mymod/files/myfile" - FileTest.stubs(:exists?).with(filepath).returns(true) + @path = Tempfile.new("module_file_testing") + @path.close! + @path = @path.path - stat = stub 'stat', :directory? => true - File.stubs(:lstat).with(filepath).returns(stat) + Dir.mkdir(@path) + Dir.mkdir(File.join(@path, "files")) - subfiles = %w{one two} - subfiles.each do |f| - path = File.join(filepath, f) - FileTest.stubs(:exists?).with(path).returns(true) - end + basedir = File.join(@path, "files", "myfile") + Dir.mkdir(basedir) - Dir.expects(:entries).with(filepath).returns(%w{one two}) + File.open(File.join(basedir, "one"), "w") { |f| f.print "one content" } + File.open(File.join(basedir, "two"), "w") { |f| f.print "two content" } + + @module = Puppet::Module.new("mymod", @path) + Puppet::Module.expects(:find).with("mymod", "myenv").returns(@module) @request = Puppet::Indirector::Request.new(:content, :search, "puppet://host/modules/mymod/myfile", :recurse => true) diff --git a/spec/shared_behaviours/file_server_terminus.rb b/spec/shared_behaviours/file_server_terminus.rb index 91e1b2dca..fc5673a65 100644 --- a/spec/shared_behaviours/file_server_terminus.rb +++ b/spec/shared_behaviours/file_server_terminus.rb @@ -8,14 +8,19 @@ describe "Puppet::Indirector::FileServerTerminus", :shared => true do # the 'before' block in the including context. before do Puppet::Util::Cacher.invalidate + FileTest.stubs(:exists?).returns true FileTest.stubs(:exists?).with(Puppet[:fileserverconfig]).returns(true) - FileTest.stubs(:exists?).with("/my/mount/path").returns(true) - FileTest.stubs(:directory?).with("/my/mount/path").returns(true) - FileTest.stubs(:readable?).with("/my/mount/path").returns(true) + + @path = Tempfile.new("file_server_testing") + @path.close! + @path = @path.path + + Dir.mkdir(@path) + File.open(File.join(@path, "myfile"), "w") { |f| f.print "my content" } # Use a real mount, so the integration is a bit deeper. @mount1 = Puppet::FileServing::Configuration::Mount.new("one") - @mount1.path = "/my/mount/path" + @mount1.path = @path @parser = stub 'parser', :changed? => false @parser.stubs(:parse).returns("one" => @mount1) @@ -25,17 +30,14 @@ describe "Puppet::Indirector::FileServerTerminus", :shared => true do # Stub out the modules terminus @modules = mock 'modules terminus' - @request = Puppet::Indirector::Request.new(:indirection, :method, "puppet://myhost/one/my/file") + @request = Puppet::Indirector::Request.new(:indirection, :method, "puppet://myhost/one/myfile") end it "should use the file server configuration to find files" do @modules.stubs(:find).returns(nil) @terminus.indirection.stubs(:terminus).with(:modules).returns(@modules) - path = "/my/mount/path/my/file" - FileTest.stubs(:exists?).with(path).returns(true) - FileTest.stubs(:exists?).with("/my/mount/path").returns(true) - @mount1.expects(:file).with("my/file", :node => nil).returns(path) + path = File.join(@path, "myfile") @terminus.find(@request).should be_instance_of(@test_class) end diff --git a/spec/unit/file_serving/content.rb b/spec/unit/file_serving/content.rb index e471c3b29..eacaff89f 100755 --- a/spec/unit/file_serving/content.rb +++ b/spec/unit/file_serving/content.rb @@ -25,7 +25,7 @@ describe Puppet::FileServing::Content do Puppet::FileServing::Content.new("/path").should respond_to(:collect) end - it "should retrieve and store its contents when its attributes are collected" do + it "should retrieve and store its contents when its attributes are collected if the file is a normal file" do content = Puppet::FileServing::Content.new("/path") result = "foo" @@ -36,6 +36,17 @@ describe Puppet::FileServing::Content do content.instance_variable_get("@content").should_not be_nil end + it "should not attempt to retrieve its contents if the file is a directory" do + content = Puppet::FileServing::Content.new("/path") + + result = "foo" + File.stubs(:lstat).returns(stub("stat", :ftype => "directory")) + File.expects(:read).with("/path").never + content.collect + + content.instance_variable_get("@content").should be_nil + end + it "should have a method for setting its content" do content = Puppet::FileServing::Content.new("/path") content.should respond_to(:content=) diff --git a/spec/unit/file_serving/metadata.rb b/spec/unit/file_serving/metadata.rb index f0e15873f..768a7c56d 100755 --- a/spec/unit/file_serving/metadata.rb +++ b/spec/unit/file_serving/metadata.rb @@ -16,6 +16,10 @@ describe Puppet::FileServing::Metadata do it "should should include the IndirectionHooks module in its indirection" do Puppet::FileServing::Metadata.indirection.metaclass.included_modules.should include(Puppet::FileServing::IndirectionHooks) end + + it "should have a method that triggers attribute collection" do + Puppet::FileServing::Metadata.new("/foo/bar").should respond_to(:collect) + end end describe Puppet::FileServing::Metadata, " when finding the file to use for setting attributes" do @@ -30,22 +34,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(@path).returns @stat File.expects(:readlink).with(@path).returns "/what/ever" - @metadata.collect_attributes + @metadata.collect end it "should use the set base path if one is not provided" do File.expects(:lstat).with(@path).returns @stat File.expects(:readlink).with(@path).returns "/what/ever" - @metadata.collect_attributes() + @metadata.collect() end it "should fail if a base path is neither set nor provided" do - proc { @metadata.collect_attributes() }.should raise_error(Errno::ENOENT) + proc { @metadata.collect() }.should raise_error(Errno::ENOENT) end it "should raise an exception if the file does not exist" do File.expects(:lstat).with(@path).raises(Errno::ENOENT) - proc { @metadata.collect_attributes()}.should raise_error(Errno::ENOENT) + proc { @metadata.collect()}.should raise_error(Errno::ENOENT) end end @@ -58,7 +62,7 @@ describe Puppet::FileServing::Metadata, " when collecting attributes" do @checksum = Digest::MD5.hexdigest("some content\n") @metadata = Puppet::FileServing::Metadata.new("/my/file") @metadata.stubs(:md5_file).returns(@checksum) - @metadata.collect_attributes + @metadata.collect end it "should be able to produce xmlrpc-style attribute information" do @@ -106,7 +110,7 @@ describe Puppet::FileServing::Metadata, " when collecting attributes" do @stat.stubs(:ftype).returns("directory") @time = Time.now @metadata.expects(:ctime_file).returns(@time) - @metadata.collect_attributes + @metadata.collect end it "should only use checksums of type 'ctime' for directories" do @@ -122,7 +126,7 @@ describe Puppet::FileServing::Metadata, " when collecting attributes" do before do @stat.stubs(:ftype).returns("link") File.expects(:readlink).with("/my/file").returns("/path/to/link") - @metadata.collect_attributes + @metadata.collect end it "should read links instead of returning their checksums" do @@ -142,7 +146,7 @@ describe Puppet::FileServing::Metadata, " when pointing to a link" do File.expects(:lstat).with("/base/path/my/file").returns stub("stat", :uid => 1, :gid => 2, :ftype => "link", :mode => 0755) File.expects(:readlink).with("/base/path/my/file").returns "/some/other/path" - file.collect_attributes + file.collect file.destination.should == "/some/other/path" end @@ -152,7 +156,7 @@ describe Puppet::FileServing::Metadata, " when pointing to a link" do File.expects(:lstat).with("/base/path/my/file").returns stub("stat", :uid => 1, :gid => 2, :ftype => "link", :mode => 0755) File.expects(:readlink).with("/base/path/my/file").returns "/some/other/path" - file.collect_attributes + file.collect 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 aa9dca961..e504a51be 100755 --- a/spec/unit/file_serving/terminus_helper.rb +++ b/spec/unit/file_serving/terminus_helper.rb @@ -35,36 +35,48 @@ describe Puppet::FileServing::TerminusHelper do before do @request.stubs(:key).returns "puppet://host/mount/dir" + @one = stub 'one', :links= => nil, :collect => nil + @two = stub 'two', :links= => nil, :collect => nil + @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) + @model.expects(:new).returns(@one) + @model.expects(:new).returns(@two) @helper.path2instances(@request, "/my/file").length.should == 2 end it "should set each returned instance's path to the original path" do - @model.expects(:new).with { |key, options| key == "/my/file" }.returns(:one) - @model.expects(:new).with { |key, options| key == "/my/file" }.returns(:two) + @model.expects(:new).with { |key, options| key == "/my/file" }.returns(@one) + @model.expects(:new).with { |key, options| key == "/my/file" }.returns(@two) @helper.path2instances(@request, "/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) + @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(@request, "/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) + @one.expects(:links=).with :manage + @two.expects(:links=).with :manage + @model.expects(:new).returns(@one) + @model.expects(:new).returns(@two) @request.options[:links] = :manage @helper.path2instances(@request, "/my/file") end + + it "should collect the instance's attributes" do + @one.expects(:collect) + @two.expects(:collect) + @model.expects(:new).returns(@one) + @model.expects(:new).returns(@two) + + @helper.path2instances(@request, "/my/file") + end end end diff --git a/spec/unit/indirector/direct_file_server.rb b/spec/unit/indirector/direct_file_server.rb index b80519bbe..e32fe6678 100755 --- a/spec/unit/indirector/direct_file_server.rb +++ b/spec/unit/indirector/direct_file_server.rb @@ -45,7 +45,7 @@ describe Puppet::Indirector::DirectFileServer do before do @data = mock 'content' - @data.stubs(:collect_attributes) + @data.stubs(:collect) FileTest.expects(:exists?).with("/my/local").returns true end diff --git a/spec/unit/indirector/file_metadata/file.rb b/spec/unit/indirector/file_metadata/file.rb index b37c42c72..a096d469d 100755 --- a/spec/unit/indirector/file_metadata/file.rb +++ b/spec/unit/indirector/file_metadata/file.rb @@ -21,14 +21,14 @@ describe Puppet::Indirector::FileMetadata::File do @metadata = Puppet::Indirector::FileMetadata::File.new @uri = "file:///my/local" @data = mock 'metadata' - @data.stubs(:collect_attributes) + @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_attributes) + @data.expects(:collect) Puppet::FileServing::Metadata.expects(:new).returns(@data) @metadata.find(@request).should == @data @@ -45,7 +45,7 @@ describe Puppet::Indirector::FileMetadata::File do 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_attributes => nil), mock("two", :collect_attributes => nil)] ) + @metadata.expects(:path2instances).returns( [mock("one", :collect => nil), mock("two", :collect => nil)] ) @metadata.search(@request) end end diff --git a/spec/unit/indirector/file_metadata/modules.rb b/spec/unit/indirector/file_metadata/modules.rb index 838ac3b5f..7e5113df5 100755 --- a/spec/unit/indirector/file_metadata/modules.rb +++ b/spec/unit/indirector/file_metadata/modules.rb @@ -36,7 +36,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 :collect_attributes + instance.expects :collect @finder.find(@request) end end diff --git a/spec/unit/indirector/file_server.rb b/spec/unit/indirector/file_server.rb index ab8e32566..e17deecf9 100755 --- a/spec/unit/indirector/file_server.rb +++ b/spec/unit/indirector/file_server.rb @@ -56,19 +56,20 @@ describe Puppet::Indirector::FileServer 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).returns(:myinstance) - @file_server.find(@request).should == :myinstance + instance = stub("instance", :collect => nil) + @model.expects(:new).returns instance + @file_server.find(@request).should equal(instance) end end describe Puppet::Indirector::FileServer, " when returning instances" do before :each do @configuration.expects(:file_path).with("my/local/file", :node => nil).returns("/some/file") - @instance = mock 'instance' + @instance = stub 'instance', :collect => nil end it "should create the instance with the path at which the instance was found" do - @model.expects(:new).with { |key, options| key == "/some/file" } + @model.expects(:new).with { |key, options| key == "/some/file" }.returns @instance @file_server.find(@request) end @@ -83,6 +84,12 @@ describe Puppet::Indirector::FileServer do @model.expects(:new).returns(@instance) @file_server.find(@request) end + + it "should collect each instance's attributes before returning" do + @instance.expects(:collect) + @model.expects(:new).returns @instance + @file_server.find(@request) + end end describe Puppet::Indirector::FileServer, " when checking authorization" do @@ -171,8 +178,14 @@ describe Puppet::Indirector::FileServer do it "should pass the request on to :path2instances" do @configuration.expects(:file_path).with("my/local/file", :node => nil).returns("my/file") - @file_server.expects(:path2instances).with(@request, "my/file") + @file_server.expects(:path2instances).with(@request, "my/file").returns [] @file_server.search(@request) end + + it "should return the result of :path2instances" do + @configuration.expects(:file_path).with("my/local/file", :node => nil).returns("my/file") + @file_server.expects(:path2instances).with(@request, "my/file").returns :footest + @file_server.search(@request).should == :footest + end end end |