diff options
| -rwxr-xr-x | spec/integration/type/file.rb | 106 | ||||
| -rwxr-xr-x | test/ral/type/file.rb | 119 |
2 files changed, 106 insertions, 119 deletions
diff --git a/spec/integration/type/file.rb b/spec/integration/type/file.rb index 3b03d53e3..40f9244f1 100755 --- a/spec/integration/type/file.rb +++ b/spec/integration/type/file.rb @@ -7,6 +7,112 @@ require 'puppet_spec/files' describe Puppet::Type.type(:file) do include PuppetSpec::Files + describe "when writing files" do + it "should backup files to a filebucket when one is configured" do + bucket = Puppet::Type.type(:filebucket).new :path => tmpfile("filebucket"), :name => "mybucket" + file = Puppet::Type.type(:file).new :path => tmpfile("bucket_backs"), :backup => "mybucket", :content => "foo" + catalog = Puppet::Resource::Catalog.new + catalog.add_resource file, bucket + + File.open(file[:path], "w") { |f| f.puts "bar" } + + md5 = Digest::MD5.hexdigest(File.read(file[:path])) + + catalog.apply + + bucket.bucket.getfile(md5).should == "bar\n" + end + + it "should backup files in the local directory when a backup string is provided" do + file = Puppet::Type.type(:file).new :path => tmpfile("bucket_backs"), :backup => ".bak", :content => "foo" + catalog = Puppet::Resource::Catalog.new + catalog.add_resource file + + File.open(file[:path], "w") { |f| f.puts "bar" } + + catalog.apply + + backup = file[:path] + ".bak" + FileTest.should be_exist(backup) + File.read(backup).should == "bar\n" + end + + it "should fail if no backup can be performed" do + dir = tmpfile("backups") + Dir.mkdir(dir) + path = File.join(dir, "testfile") + file = Puppet::Type.type(:file).new :path => path, :backup => ".bak", :content => "foo" + catalog = Puppet::Resource::Catalog.new + catalog.add_resource file + + File.open(file[:path], "w") { |f| f.puts "bar" } + + File.chmod(0111, dir) # make it non-writeable + + catalog.apply + + File.read(file[:path]).should == "bar\n" + end + + it "should not backup symlinks" do + link = tmpfile("link") + dest1 = tmpfile("dest1") + dest2 = tmpfile("dest2") + bucket = Puppet::Type.type(:filebucket).new :path => tmpfile("filebucket"), :name => "mybucket" + file = Puppet::Type.type(:file).new :path => link, :target => dest2, :ensure => :link, :backup => "mybucket" + catalog = Puppet::Resource::Catalog.new + catalog.add_resource file, bucket + + File.open(dest1, "w") { |f| f.puts "whatever" } + File.symlink(dest1, link) + + md5 = Digest::MD5.hexdigest(File.read(file[:path])) + + catalog.apply + + File.readlink(link).should == dest2 + Find.find(bucket[:path]) { |f| File.file?(f) }.should be_nil + end + + it "should backup directories to the local filesystem by copying the whole directory" do + file = Puppet::Type.type(:file).new :path => tmpfile("bucket_backs"), :backup => ".bak", :content => "foo" + catalog = Puppet::Resource::Catalog.new + catalog.add_resource file + + Dir.mkdir(file[:path]) + otherfile = File.join(file[:path], "foo") + File.open(otherfile, "w") { |f| f.print "yay" } + + catalog.apply + + backup = file[:path] + ".bak" + FileTest.should be_directory(backup) + File.read(File.join(backup, "foo")).should == "yay" + end + + it "should backup directories to filebuckets by backing up each file separately" do + bucket = Puppet::Type.type(:filebucket).new :path => tmpfile("filebucket"), :name => "mybucket" + file = Puppet::Type.type(:file).new :path => tmpfile("bucket_backs"), :backup => "mybucket", :content => "foo" + catalog = Puppet::Resource::Catalog.new + catalog.add_resource file, bucket + + Dir.mkdir(file[:path]) + foofile = File.join(file[:path], "foo") + barfile = File.join(file[:path], "bar") + File.open(foofile, "w") { |f| f.print "fooyay" } + File.open(barfile, "w") { |f| f.print "baryay" } + + + foomd5 = Digest::MD5.hexdigest(File.read(foofile)) + barmd5 = Digest::MD5.hexdigest(File.read(barfile)) + + catalog.apply + + bucket.bucket.getfile(foomd5).should == "fooyay" + bucket.bucket.getfile(barmd5).should == "baryay" + end + end + describe "when recursing" do def build_path(dir) Dir.mkdir(dir) diff --git a/test/ral/type/file.rb b/test/ral/type/file.rb index 7d6ec659f..69a893e31 100755 --- a/test/ral/type/file.rb +++ b/test/ral/type/file.rb @@ -817,125 +817,6 @@ class TestFile < Test::Unit::TestCase end end - # Testing #304 - def test_links_to_directories - link = tempfile() - file = tempfile() - dir = tempfile() - Dir.mkdir(dir) - - bucket = Puppet::Type.type(:filebucket).new :name => "main" - File.symlink(dir, link) - File.open(file, "w") { |f| f.puts "" } - assert_equal(dir, File.readlink(link)) - obj = Puppet::Type.newfile :path => link, :ensure => :link, :target => file, :recurse => false, :backup => "main" - - catalog = mk_catalog(bucket, obj) - - catalog.apply - - assert_equal(file, File.readlink(link)) - end - - # Testing #303 - def test_nobackups_with_links - link = tempfile() - new = tempfile() - - File.open(link, "w") { |f| f.puts "old" } - File.open(new, "w") { |f| f.puts "new" } - obj = Puppet::Type.newfile :path => link, :ensure => :link, - :target => new, :recurse => true, :backup => false - - assert_nothing_raised do - obj.handlebackup - end - - bfile = [link, "puppet-bak"].join(".") - - assert(! FileTest.exists?(bfile), "Backed up when told not to") - - assert_apply(obj) - - assert(! FileTest.exists?(bfile), "Backed up when told not to") - end - - # Make sure we consistently handle backups for all cases. - def test_ensure_with_backups - # We've got three file types, so make sure we can replace any type - # with the other type and that backups are done correctly. - types = [:file, :directory, :link] - - dir = tempfile() - path = File.join(dir, "test") - linkdest = tempfile() - creators = { - :file => proc { File.open(path, "w") { |f| f.puts "initial" } }, - :directory => proc { Dir.mkdir(path) }, - :link => proc { File.symlink(linkdest, path) } - } - - bucket = Puppet::Type.type(:filebucket).new :name => "main", :path => tempfile() - - obj = Puppet::Type.newfile :path => path, :force => true, - :links => :manage - - catalog = mk_catalog(obj, bucket) - - Puppet[:trace] = true - ["main", false].each do |backup| - obj[:backup] = backup - obj.finish - types.each do |should| - types.each do |is| - # It makes no sense to replace a directory with a directory - # next if should == :directory and is == :directory - - Dir.mkdir(dir) - - # Make the thing - creators[is].call - - obj[:ensure] = should - - if should == :link - obj[:target] = linkdest - else - if obj.property(:target) - obj.delete(:target) - end - end - - # First try just removing the initial data - assert_nothing_raised do - obj.remove_existing(should) - end - - unless is == should - # Make sure the original is gone - assert(! FileTest.exists?(obj[:path]), - "remove_existing did not work: " + - "did not remove %s with %s" % [is, should]) - end - FileUtils.rmtree(obj[:path]) - - # Now make it again - creators[is].call - - property = obj.property(:ensure) - - currentvalue = property.retrieve - unless property.insync?(currentvalue) - assert_nothing_raised do - property.sync - end - end - FileUtils.rmtree(dir) - end - end - end - end - if Process.uid == 0 # Testing #364. def test_writing_in_directories_with_no_write_access |
