diff options
-rwxr-xr-x | lib/puppet/util/filetype.rb | 6 | ||||
-rw-r--r-- | spec/unit/util/filetype.rb | 116 | ||||
-rwxr-xr-x | test/util/filetype.rb | 91 |
3 files changed, 119 insertions, 94 deletions
diff --git a/lib/puppet/util/filetype.rb b/lib/puppet/util/filetype.rb index 60cbc77e7..60380d5f1 100755 --- a/lib/puppet/util/filetype.rb +++ b/lib/puppet/util/filetype.rb @@ -74,7 +74,7 @@ class Puppet::Util::FileType # Back the file up before replacing it. def backup - bucket.backup(@path) if FileTest.exists?(@path) + bucket.backup(@path) if File.exists?(@path) end # Pick or create a filebucket to use. @@ -92,7 +92,7 @@ class Puppet::Util::FileType newfiletype(:flat) do # Read the file. def read - if File.exists?(@path) + if File.exist?(@path) File.read(@path) else return nil @@ -101,7 +101,7 @@ class Puppet::Util::FileType # Remove the file. def remove - if File.exists?(@path) + if File.exist?(@path) File.unlink(@path) end end diff --git a/spec/unit/util/filetype.rb b/spec/unit/util/filetype.rb new file mode 100644 index 000000000..74dae3356 --- /dev/null +++ b/spec/unit/util/filetype.rb @@ -0,0 +1,116 @@ +#!/usr/bin/env ruby + +Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") } + +require 'puppet/util/filetype' + +# XXX Import all of the tests into this file. +describe Puppet::Util::FileType do + describe "when backing up a file" do + before do + @file = Puppet::Util::FileType.filetype(:flat).new("/my/file") + end + + it "should do nothing if the file does not exist" do + File.expects(:exists?).with("/my/file").returns false + @file.expects(:bucket).never + @file.backup + end + + it "should use its filebucket to backup the file if it exists" do + File.expects(:exists?).with("/my/file").returns true + + bucket = mock 'bucket' + bucket.expects(:backup).with("/my/file") + + @file.expects(:bucket).returns bucket + @file.backup + end + + it "should use the filebucket named 'puppet' if it finds one" do + bucket = mock 'bucket' + bucket.expects(:bucket).returns "mybucket" + + Puppet::Type.type(:filebucket).expects(:[]).with("puppet").returns bucket + + @file.bucket.should == "mybucket" + end + + it "should use the default filebucket if none named 'puppet' is found" do + bucket = mock 'bucket' + bucket.expects(:bucket).returns "mybucket" + + Puppet::Type.type(:filebucket).expects(:[]).with("puppet").returns nil + Puppet::Type.type(:filebucket).expects(:mkdefaultbucket).returns bucket + + @file.bucket.should == "mybucket" + end + end + + describe "the flat filetype" do + before do + @type = Puppet::Util::FileType.filetype(:flat) + end + it "should exist" do + @type.should_not be_nil + end + + describe "when the file already exists" do + it "should return the file's contents when asked to read it" do + file = @type.new("/my/file") + File.expects(:exist?).with("/my/file").returns true + File.expects(:read).with("/my/file").returns "my text" + + file.read.should == "my text" + end + + it "should unlink the file when asked to remove it" do + file = @type.new("/my/file") + File.expects(:exist?).with("/my/file").returns true + File.expects(:unlink).with("/my/file") + + file.remove + end + end + + describe "when the file does not exist" do + it "should return an empty string when asked to read the file" do + file = @type.new("/my/file") + File.expects(:exist?).with("/my/file").returns false + + file.read.should == "" + end + end + + describe "when writing the file" do + before do + @file = @type.new("/my/file") + FileUtils.stubs(:cp) + + @tempfile = stub 'tempfile', :print => nil, :close => nil, :flush => nil, :path => "/other/file" + Tempfile.stubs(:new).returns @tempfile + 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 + Tempfile.expects(:new).with("puppet").returns @tempfile + @tempfile.expects(:print).with("my text") + @tempfile.expects(:flush) + @tempfile.expects(:close) + FileUtils.expects(:cp).with(@tempfile.path, "/my/file") + + @file.write "my text" + end + + it "should set the selinux default context on the file" do + @file.expects(:set_selinux_default_context).with("/my/file") + @file.write "eh" + end + end + end +end diff --git a/test/util/filetype.rb b/test/util/filetype.rb index 6c7ede07b..5b01a8bb4 100755 --- a/test/util/filetype.rb +++ b/test/util/filetype.rb @@ -8,97 +8,6 @@ require 'mocha' class TestFileType < Test::Unit::TestCase include PuppetTest - - def test_flat - obj = nil - path = tempfile() - type = nil - - assert_nothing_raised { - type = Puppet::Util::FileType.filetype(:flat) - } - - assert(type, "Could not retrieve flat filetype") - - assert_nothing_raised { - obj = type.new(path) - } - - text = "This is some text\n" - - newtext = nil - assert_nothing_raised { - newtext = obj.read - } - - # The base class doesn't allow a return of nil - assert_equal("", newtext, "Somehow got some text") - - assert_nothing_raised { - obj.write(text) - } - assert_nothing_raised { - newtext = obj.read - } - - assert_equal(text, newtext, "Text was changed somehow") - - File.open(path, "w") { |f| f.puts "someyayness" } - - text = File.read(path) - assert_nothing_raised { - newtext = obj.read - } - - 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 |