diff options
-rw-r--r-- | CHANGELOG | 4 | ||||
-rw-r--r-- | lib/puppet/network/client/dipper.rb | 2 | ||||
-rwxr-xr-x | lib/puppet/provider/parsedfile.rb | 1 | ||||
-rwxr-xr-x | lib/puppet/type/pfilebucket.rb | 5 | ||||
-rwxr-xr-x | lib/puppet/util/filetype.rb | 11 | ||||
-rwxr-xr-x | test/util/filetype.rb | 50 |
6 files changed, 67 insertions, 6 deletions
@@ -1,3 +1,7 @@ + Files modified using a FileType instance, as ParsedFile + does, will now automatically get backed up to the filebucket + named "puppet". + Added a 'maillist' type for managing mailing lists. Added a 'mailalias' type for managing mail aliases. diff --git a/lib/puppet/network/client/dipper.rb b/lib/puppet/network/client/dipper.rb index 5ddb67db1..c16e37725 100644 --- a/lib/puppet/network/client/dipper.rb +++ b/lib/puppet/network/client/dipper.rb @@ -19,7 +19,7 @@ class Puppet::Network::Client::Dipper < Puppet::Network::Client # Back up a file to our bucket def backup(file) unless FileTest.exists?(file) - raise(BucketError, "File %s does not exist" % file) + raise(ArgumentError, "File %s does not exist" % file) end contents = ::File.read(file) unless local? diff --git a/lib/puppet/provider/parsedfile.rb b/lib/puppet/provider/parsedfile.rb index 4e3dac8f8..cdb770b8b 100755 --- a/lib/puppet/provider/parsedfile.rb +++ b/lib/puppet/provider/parsedfile.rb @@ -316,7 +316,6 @@ class Puppet::Provider::ParsedFile < Puppet::Provider end @property_hash[:name] ||= @resource.name - warning "Flushing" self.class.flush(@property_hash) end diff --git a/lib/puppet/type/pfilebucket.rb b/lib/puppet/type/pfilebucket.rb index fd0f131a1..df30c1872 100755 --- a/lib/puppet/type/pfilebucket.rb +++ b/lib/puppet/type/pfilebucket.rb @@ -65,9 +65,10 @@ module Puppet # Create a default filebucket. def self.mkdefaultbucket - unless self["puppet"] - self.create :name => "puppet", :path => Puppet[:clientbucketdir] + unless default = self["puppet"] + default = self.create :name => "puppet", :path => Puppet[:clientbucketdir] end + default end def self.instances diff --git a/lib/puppet/util/filetype.rb b/lib/puppet/util/filetype.rb index 8abe0cc00..7ce732b63 100755 --- a/lib/puppet/util/filetype.rb +++ b/lib/puppet/util/filetype.rb @@ -67,6 +67,16 @@ class Puppet::Util::FileType @filetypes[type] end + # Back the file up before replacing it. + def backup + bucket.backup(@path) if FileTest.exists?(@path) + end + + # Pick or create a filebucket to use. + def bucket + Puppet::Type.type(:filebucket).mkdefaultbucket.bucket + end + def initialize(path) @path = path end @@ -91,6 +101,7 @@ class Puppet::Util::FileType # Overwrite the file. def write(text) + backup() File.open(@path, "w") { |f| f.print text; f.flush } end end diff --git a/test/util/filetype.rb b/test/util/filetype.rb index 6f026f9f1..2c4a16fc2 100755 --- a/test/util/filetype.rb +++ b/test/util/filetype.rb @@ -2,9 +2,9 @@ $:.unshift("../lib").unshift("../../lib") if __FILE__ =~ /\.rb$/ -require 'puppet' -require 'puppet/util/filetype' require 'puppettest' +require 'puppet/util/filetype' +require 'mocha' class TestFileType < Test::Unit::TestCase include PuppetTest @@ -53,6 +53,52 @@ class TestFileType < Test::Unit::TestCase assert_equal(text, newtext, "Text was changed somehow") end + # Make sure that modified files are backed up before they're changed. + def test_backup_is_called + path = tempfile + File.open(path, "w") { |f| f.print 'yay' } + + obj = Puppet::Util::FileType.filetype(:flat).new(path) + + obj.expects(:backup) + + obj.write("something") + + assert_equal("something", File.read(path), "File did not get changed") + end + + def test_backup + path = tempfile + type = Puppet::Type.type(:filebucket) + + obj = Puppet::Util::FileType.filetype(:flat).new(path) + + # First try it when the file does not yet exist. + assert_nothing_raised("Could not call backup when file does not exist") do + obj.backup + end + + # Then create the file + File.open(path, "w") { |f| f.print 'one' } + + # Then try it with no filebucket objects + assert_nothing_raised("Could not call backup with no buckets") do + obj.backup + end + puppet = type["puppet"] + assert(puppet, "Did not create default filebucket") + + assert_equal("one", puppet.bucket.getfile(Digest::MD5.hexdigest(File.read(path))), "Could not get file from backup") + + # Try it again when the default already exists + File.open(path, "w") { |f| f.print 'two' } + assert_nothing_raised("Could not call backup with no buckets") do + obj.backup + end + + assert_equal("two", puppet.bucket.getfile(Digest::MD5.hexdigest(File.read(path))), "Could not get file from backup") + end + if Facter["operatingsystem"].value == "Darwin" def test_ninfotoarray obj = nil |