All spec tests and unit tests pass. Some new spec tests are added as well, including moving the old test/ral/type/filebucket.rb tests over to RSpec.
Signed-off-by: Steven Jenkins <[email protected]> --- lib/puppet/type/file.rb | 2 +- lib/puppet/util/backups.rb | 36 +++++--------- spec/unit/util/backups.rb | 97 ++++++++++++++++++++++++++++++++++++-- test/ral/type/filebucket.rb | 110 ------------------------------------------- 4 files changed, 106 insertions(+), 139 deletions(-) delete mode 100755 test/ral/type/filebucket.rb diff --git a/lib/puppet/type/file.rb b/lib/puppet/type/file.rb index 657c7ab..ed5147e 100644 --- a/lib/puppet/type/file.rb +++ b/lib/puppet/type/file.rb @@ -578,7 +578,7 @@ module Puppet def remove_existing(should) return unless s = stat - self.fail "Could not back up; will not replace" unless handlebackup + self.fail "Could not back up; will not replace" unless perform_backup unless should.to_s == "link" return if s.ftype.to_s == should.to_s diff --git a/lib/puppet/util/backups.rb b/lib/puppet/util/backups.rb index f76a52b..a116e3e 100644 --- a/lib/puppet/util/backups.rb +++ b/lib/puppet/util/backups.rb @@ -1,50 +1,35 @@ module Puppet::Util::Backups # Deal with backups. - def handlebackup(file = nil) + def perform_backup(file = nil) # let the path be specified file ||= self[:path] return true unless FileTest.exists?(file) # if they specifically don't want a backup, then just say # we're good return true unless self[:backup] - unless self.bucket || self[:backup] - self.err "Need a bucket or a backup setting for backups" - return false - end - return handlebucket(file) if self.bucket - return handlebackuplocal(file, self[:backup]) + return perform_backup_with_bucket(file) if self.bucket + return perform_backup_with_backuplocal(file, self[:backup]) end private - def handlebucket(fileobj) + def perform_backup_with_bucket(fileobj) file = (fileobj.class == String) ? fileobj : fileobj.name case File.stat(file).ftype when "directory" # we don't need to backup directories when recurse is on return true if self[:recurse] info "Recursively backing up to filebucket" - require 'find' - Find.find(file) do |f| - if File.file?(f) - sum = self.bucket.backup(f) - self.info "Filebucketed %s to %s with sum %s" % - [f, self.bucket.name, sum] - end - end - return true - when "file" - sum = self.bucket.backup(file) - self.info "Filebucketed %s to %s with sum %s" % - [file, self.bucket.name, sum] - return true + Find.find(self[:path]) { |f| backup_file_with_filebucket(f) if + File.file?(f) } + when "file"; backup_file_with_filebucket(f) when "link"; return true end end - def handlebackuplocal(fileobj, backup) + def perform_backup_with_backuplocal(fileobj, backup) file = (fileobj.class == String) ? fileobj : fileobj.name newfile = file + backup if FileTest.exists?(newfile) @@ -91,4 +76,9 @@ module Puppet::Util::Backups return false end end + + def backup_file_with_filebucket(f) + sum = self.bucket.backup(f) + self.info "Filebucketed %s to %s with sum %s" % [f, self.bucket.name, sum] + end end diff --git a/spec/unit/util/backups.rb b/spec/unit/util/backups.rb index 2ec06e5..bbc3045 100755 --- a/spec/unit/util/backups.rb +++ b/spec/unit/util/backups.rb @@ -4,24 +4,111 @@ require File.dirname(__FILE__) + '/../../spec_helper' require 'puppet/type/file' require 'puppet/util/backups' +include PuppetTest +#require 'tempfile' describe Puppet::Util::Backups do + def mkfile(hash) + file = nil + file = Puppet::Type.type(:file).new(hash) + return file + end + + def mkbucket(name,path) + bucket = nil + bucket = Puppet::Type.type(:filebucket).new( + :name => name, + :path => path + ) + @@tmpfiles.push path + + return bucket + end + + def mktestfile + tmpfile = tempfile() + File.open(tmpfile, "w") { |f| f.puts rand(100) } + @@tmpfiles.push tmpfile + mkfile(:name => tmpfile) + end + + describe "when backing up a simple file" do + it "should successfully create a bucket" do + name = "yayness" + bucketpath = tempfile() + resource = mkbucket(name, bucketpath) + + #bucket = resource.bucket + + bucket = Puppet::Type.type(:filebucket).new( + :name => name, + :path => bucketpath + ).bucket + + bucket.should be_instance_of(Puppet::Network::Client.dipper) + + #it "should successfully back up the file" do + md5 = nil + newpath = tempfile() + @@tmpfiles << newpath + system("cp /etc/passwd %s" % newpath) + md5 = bucket.backup(newpath) + + md5.should_not be_nil + + dir, file, pathfile = Puppet::Network::Handler.filebucket.paths(bucketpath, md5) + + File.directory?(dir).should be_true + newmd5 = nil + + # Just in case the file isn't writable + File.chmod(0644, newpath) + File.open(newpath, "w") { |f| f.puts ";lkjasdf;lkjasdflkjwerlkj134lkj" } + + lambda { + newmd5 = bucket.backup(newpath) + }.should_not raise_error + + md5.should_not == newmd5 + + lambda { + bucket.restore(newpath, md5) + }.should_not raise_error + + File.open(newpath) { |f| newmd5 = Digest::MD5.hexdigest(f.read) } + + md5.should == newmd5 + end + end + describe "when backing up a file" do it "should succeed silently if the file does not exist" do - Puppet::Type::File.new(:name => "/no/such/file").handlebackup + Puppet::Type::File.new(:name => "/no/such/file").perform_backup end it "should succeed silently if self[:backup] is false" do - Puppet::Type::File.new(:name => "/my/file", :backup => false).handlebackup + Puppet::Type::File.new(:name => "/my/file", :backup => false).perform_backup end it "should error if no bucket or backup suffix available" do - #lambda { Puppet::Type::File.new(:name => "/my/file", :backup => false, :bucket => false) }. should raise_error(Puppet::Error) + lambda { Puppet::Type::File.new(:name => "/my/file", :backup => false, :bucket => false) }.should raise_error(Puppet::Error) end # For the cases where there is an actual bucket or backup, # cf spec/unit/type/file.rb section starting "when setting the backup" # as those are the higher-level tests and cover the cases here. - # Also see test/ral/type/filebucket.rb and test/ral/type/file.rb, - # Testing #303, 304 are good examples end + + after :all do + @@tmpfiles.each { |file| + unless file =~ /tmp/ + puts "Not deleting tmpfile %s" % file + next + end + if File.exists?(file) + system("chmod -R 755 %s" % file) + system("rm -rf %s" % file) + end + } + @@tmpfiles.clear + end end diff --git a/test/ral/type/filebucket.rb b/test/ral/type/filebucket.rb deleted file mode 100755 index b18a66f..0000000 --- a/test/ral/type/filebucket.rb +++ /dev/null @@ -1,110 +0,0 @@ -#!/usr/bin/env ruby - -require File.dirname(__FILE__) + '/../../lib/puppettest' - -require 'puppettest' -require 'puppettest/support/utils' -require 'fileutils' - -class TestFileBucket < Test::Unit::TestCase - include PuppetTest::Support::Utils - include PuppetTest::FileTesting - # hmmm - # this is complicated, because we store references to the created - # objects in a central store - def mkfile(hash) - file = nil - assert_nothing_raised { - file = Puppet::Type.type(:file).new(hash) - } - return file - end - - def mkbucket(name,path) - bucket = nil - assert_nothing_raised { - bucket = Puppet::Type.type(:filebucket).new( - :name => name, - :path => path - ) - } - - @@tmpfiles.push path - - return bucket - end - - def mktestfile - # because luke's home directory is on nfs, it can't be used for testing - # as root - tmpfile = tempfile() - File.open(tmpfile, "w") { |f| f.puts rand(100) } - @@tmpfiles.push tmpfile - mkfile(:name => tmpfile) - end - - def setup - super - begin - initstorage - rescue - system("rm -rf %s" % Puppet[:statefile]) - end - end - - def initstorage - Puppet::Util::Storage.init - Puppet::Util::Storage.load - end - - def clearstorage - Puppet::Util::Storage.store - Puppet::Util::Storage.clear - end - - def test_simplebucket - name = "yayness" - bucketpath = tempfile() - resource = mkbucket(name, bucketpath) - - bucket = resource.bucket - - assert_instance_of(Puppet::Network::Client.dipper, bucket) - - md5 = nil - newpath = tempfile() - @@tmpfiles << newpath - system("cp /etc/passwd %s" % newpath) - assert_nothing_raised { - md5 = bucket.backup(newpath) - } - - assert(md5) - - dir, file, pathfile = Puppet::Network::Handler.filebucket.paths(bucketpath, md5) - - assert(FileTest.directory?(dir), - "MD5 directory does not exist") - - newmd5 = nil - - # Just in case the file isn't writable - File.chmod(0644, newpath) - File.open(newpath, "w") { |f| f.puts ";lkjasdf;lkjasdflkjwerlkj134lkj" } - - assert_nothing_raised { - newmd5 = bucket.backup(newpath) - } - - assert(md5 != newmd5) - - assert_nothing_raised { - bucket.restore(newpath, md5) - } - - File.open(newpath) { |f| newmd5 = Digest::MD5.hexdigest(f.read) } - - assert_equal(md5, newmd5) - end -end - -- 1.6.3.3 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Puppet Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/puppet-dev?hl=en -~----------~----~----~----~------~----~------~--~---
