diff options
Diffstat (limited to 'spec/unit/file_serving')
-rwxr-xr-x | spec/unit/file_serving/file_base.rb | 119 | ||||
-rwxr-xr-x | spec/unit/file_serving/metadata.rb | 75 |
2 files changed, 125 insertions, 69 deletions
diff --git a/spec/unit/file_serving/file_base.rb b/spec/unit/file_serving/file_base.rb index 4c7724f7c..e1a61cd65 100755 --- a/spec/unit/file_serving/file_base.rb +++ b/spec/unit/file_serving/file_base.rb @@ -4,7 +4,7 @@ require File.dirname(__FILE__) + '/../../spec_helper' require 'puppet/file_serving/file_base' -describe Puppet::FileServing::FileBase, " when initializing" do +describe Puppet::FileServing::FileBase do 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 @@ -30,72 +30,91 @@ describe Puppet::FileServing::FileBase, " when initializing" 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, " when setting the base path" do - before do - @file = Puppet::FileServing::FileBase.new("puppet://host/module/dir/file") + it "should have a means of determining if the file exists" do + Puppet::FileServing::FileBase.new("blah").should respond_to(:exist?) 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) + it "should correctly indicate if the file is present" do + File.expects(:lstat).with("/my/file").returns(mock("stat")) + Puppet::FileServing::FileBase.new("blah", :path => "/my/file").exist?.should be_true 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) + it "should correctly indicate if the file is asbsent" do + File.expects(:lstat).with("/my/file").raises RuntimeError + Puppet::FileServing::FileBase.new("blah", :path => "/my/file").exist?.should be_false end -end -describe Puppet::FileServing::FileBase, " when determining the full file path" do - before do - @file = Puppet::FileServing::FileBase.new("mykey", :path => "/this/file") - end + describe "when setting the base path" do + before do + @file = Puppet::FileServing::FileBase.new("puppet://host/module/dir/file") + end - it "should return the path if there is no relative path" do - @file.full_path.should == "/this/file" + 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 - 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" + describe "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 - 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 -end + describe "when determining the full file path" do + before do + @file = Puppet::FileServing::FileBase.new("mykey", :path => "/this/file") + end -describe Puppet::FileServing::FileBase, " when stat'ing files" do - before do - @file = Puppet::FileServing::FileBase.new("mykey", :path => "/this/file") - end + it "should return the path if there is no relative path" do + @file.full_path.should == "/this/file" + end - 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 return the path if the relative_path is set to ''" do + @file.relative_path = "" + @file.full_path.should == "/this/file" + end - 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 return the path joined with the relative path if there is a relative path and it is not set to '/' or ''" do + @file.relative_path = "not/qualified" + @file.full_path.should == "/this/file/not/qualified" + end - it "should use :lstat if :links is set to :manage" do - File.expects(:lstat).with("/this/file").returns stub("stat", :ftype => "file") - @file.stat + 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 end - it "should use :stat if :links is set to :follow" do - File.expects(:stat).with("/this/file").returns stub("stat", :ftype => "file") - @file.links = :follow - @file.stat + describe "when stat'ing files" do + before do + @file = Puppet::FileServing::FileBase.new("mykey", :path => "/this/file") + end + + 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 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").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").returns stub("stat", :ftype => "file") + @file.links = :follow + @file.stat + end end end diff --git a/spec/unit/file_serving/metadata.rb b/spec/unit/file_serving/metadata.rb index d31dd21f0..9743370c1 100755 --- a/spec/unit/file_serving/metadata.rb +++ b/spec/unit/file_serving/metadata.rb @@ -26,8 +26,8 @@ describe Puppet::FileServing::Metadata, " when finding the file to use for setti @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" + # Use a link because it's easier to test -- no checksumming + @stat = stub "stat", :uid => 10, :gid => 20, :mode => 0755, :ftype => "link" end it "should accept a base path path to which the file should be relative" do @@ -55,17 +55,19 @@ end describe Puppet::FileServing::Metadata, " when collecting attributes" do before do @path = "/my/file" - @stat = stub 'stat', :uid => 10, :gid => 20, :mode => 0755, :ftype => "file" + # Use a real file mode, so we can validate the masking is done. + @stat = stub 'stat', :uid => 10, :gid => 20, :mode => 33261, :ftype => "file" File.stubs(:lstat).returns(@stat) - @filehandle = mock 'filehandle' - #@filehandle.expects(:read).with(512).returns("some content\n").then.returns(nil) - File.stubs(:open).with(@path, 'r').yields(@filehandle) @checksum = Digest::MD5.hexdigest("some content\n") @metadata = Puppet::FileServing::Metadata.new("file", :path => "/my/file") - @metadata.expects(:md5_file).returns(@checksum) + @metadata.stubs(:md5_file).returns(@checksum) @metadata.collect_attributes end + it "should be able to produce xmlrpc-style attribute information" do + @metadata.should respond_to(:attributes_with_tabs) + end + # LAK:FIXME This should actually change at some point it "should set the owner by id" do @metadata.owner.should be_instance_of(Fixnum) @@ -84,28 +86,63 @@ describe Puppet::FileServing::Metadata, " when collecting attributes" do @metadata.group.should == 20 end - it "should set the mode to a string version of the mode in octal" do - @metadata.mode.should == "755" - end - - it "should set the mode to the file's current mode" do - @metadata.mode.should == "755" + it "should set the mode to the file's masked mode" do + @metadata.mode.should == 0755 end it "should set the checksum to the file's current checksum" do @metadata.checksum.should == "{md5}" + @checksum end - it "should default to a checksum of type MD5" do - @metadata.checksum.should == "{md5}" + @checksum + describe "when managing files" do + it "should default to a checksum of type MD5" do + @metadata.checksum.should == "{md5}" + @checksum + end + + it "should produce tab-separated mode, type, owner, group, and checksum for xmlrpc" do + @metadata.attributes_with_tabs.should == "#{0755.to_s}\tfile\t10\t20\t{md5}#{@checksum}" + end + end + + describe "when managing directories" do + before do + @stat.stubs(:ftype).returns("directory") + @time = Time.now + @metadata.expects(:ctime_file).returns(@time) + @metadata.collect_attributes + end + + it "should only use checksums of type 'ctime' for directories" do + @metadata.checksum.should == "{ctime}" + @time.to_s + end + + it "should produce tab-separated mode, type, owner, group, and checksum for xmlrpc" do + @metadata.attributes_with_tabs.should == "#{0755.to_s}\tdirectory\t10\t20\t{ctime}#{@time.to_s}" + end + end + + describe "when managing links" do + before do + @stat.stubs(:ftype).returns("link") + File.expects(:readlink).with("/my/file").returns("/path/to/link") + @metadata.collect_attributes + end + + it "should read links instead of returning their checksums" do + @metadata.destination.should == "/path/to/link" + end + + it "should produce tab-separated mode, type, owner, group, and destination for xmlrpc" do + @metadata.attributes_with_tabs.should == "#{0755.to_s}\tlink\t10\t20\t/path/to/link" + end end 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 +describe Puppet::FileServing::Metadata, " when pointing to a link" do + it "should store the destination of the link in :destination if links are :manage" do 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(: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 @@ -115,7 +152,7 @@ describe Puppet::FileServing::Metadata, " when pointing to a symlink" do it "should not collect the checksum" do 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(: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 |