Fixing #1541 - ParsedFile only backs up files once per transaction
This moves responsibility for backups from the filetype to the consumer of the filetype, but only ParsedFile actually uses filetypes. Signed-off-by: Luke Kanies <luke@madstop.com>
This commit is contained in:
Родитель
53f15b9208
Коммит
d5a193a594
|
@ -78,8 +78,22 @@ class Puppet::Provider::ParsedFile < Puppet::Provider
|
||||||
@modified.reject! { |t| flushed.include?(t) }
|
@modified.reject! { |t| flushed.include?(t) }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Make sure our file is backed up, but only back it up once per transaction.
|
||||||
|
# We cheat and rely on the fact that @records is created on each prefetch.
|
||||||
|
def self.backup_target(target)
|
||||||
|
unless defined?(@backup_stats)
|
||||||
|
@backup_stats = {}
|
||||||
|
end
|
||||||
|
return nil if @backup_stats[target] == @records.object_id
|
||||||
|
|
||||||
|
target_object(target).backup
|
||||||
|
@backup_stats[target] = @records.object_id
|
||||||
|
end
|
||||||
|
|
||||||
# Flush all of the records relating to a specific target.
|
# Flush all of the records relating to a specific target.
|
||||||
def self.flush_target(target)
|
def self.flush_target(target)
|
||||||
|
backup_target(target)
|
||||||
|
|
||||||
records = target_records(target).reject { |r|
|
records = target_records(target).reject { |r|
|
||||||
r[:ensure] == :absent
|
r[:ensure] == :absent
|
||||||
}
|
}
|
||||||
|
|
|
@ -108,7 +108,6 @@ class Puppet::Util::FileType
|
||||||
|
|
||||||
# Overwrite the file.
|
# Overwrite the file.
|
||||||
def write(text)
|
def write(text)
|
||||||
backup()
|
|
||||||
require "tempfile"
|
require "tempfile"
|
||||||
tf = Tempfile.new("puppet")
|
tf = Tempfile.new("puppet")
|
||||||
tf.print text; tf.flush
|
tf.print text; tf.flush
|
||||||
|
|
|
@ -47,4 +47,40 @@ describe Puppet::Provider::ParsedFile do
|
||||||
@class.instances
|
@class.instances
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "when flushing a file's records to disk" do
|
||||||
|
before do
|
||||||
|
# This way we start with some @records, like we would in real life.
|
||||||
|
@class.stubs(:retrieve).returns []
|
||||||
|
@class.default_target = "/foo/bar"
|
||||||
|
@class.initvars
|
||||||
|
@class.prefetch
|
||||||
|
|
||||||
|
@filetype = mock 'filetype'
|
||||||
|
Puppet::Util::FileType.filetype(:flat).expects(:new).with("/my/file").returns @filetype
|
||||||
|
|
||||||
|
@filetype.stubs(:write)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should back up the file being written" do
|
||||||
|
@filetype.expects(:backup)
|
||||||
|
|
||||||
|
@class.flush_target("/my/file")
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should not back up the file more than once between calls to 'prefetch'" do
|
||||||
|
@filetype.expects(:backup).once
|
||||||
|
|
||||||
|
@class.flush_target("/my/file")
|
||||||
|
@class.flush_target("/my/file")
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should back the file up again once the file has been reread" do
|
||||||
|
@filetype.expects(:backup).times(2)
|
||||||
|
|
||||||
|
@class.flush_target("/my/file")
|
||||||
|
@class.prefetch
|
||||||
|
@class.flush_target("/my/file")
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -91,12 +91,6 @@ describe Puppet::Util::FileType do
|
||||||
Tempfile.stubs(:new).returns @tempfile
|
Tempfile.stubs(:new).returns @tempfile
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should back up the file" do
|
|
||||||
@file.expects(:backup)
|
|
||||||
|
|
||||||
@file.write("foo")
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should first create a temp file and copy its contents over to the file location" do
|
it "should first create a temp file and copy its contents over to the file location" do
|
||||||
Tempfile.expects(:new).with("puppet").returns @tempfile
|
Tempfile.expects(:new).with("puppet").returns @tempfile
|
||||||
@tempfile.expects(:print).with("my text")
|
@tempfile.expects(:print).with("my text")
|
||||||
|
|
Загрузка…
Ссылка в новой задаче