diff options
-rw-r--r-- | lib/puppet/type/file.rb | 8 | ||||
-rwxr-xr-x | lib/puppet/type/file/source.rb | 27 | ||||
-rwxr-xr-x | spec/integration/type/file.rb | 213 | ||||
-rwxr-xr-x | spec/unit/type/file.rb | 81 | ||||
-rwxr-xr-x | spec/unit/type/file/source.rb | 82 | ||||
-rwxr-xr-x | test/ral/type/file.rb | 153 | ||||
-rwxr-xr-x | test/ral/type/filesources.rb | 329 |
7 files changed, 312 insertions, 581 deletions
diff --git a/lib/puppet/type/file.rb b/lib/puppet/type/file.rb index c55ff29a7..155cf62f4 100644 --- a/lib/puppet/type/file.rb +++ b/lib/puppet/type/file.rb @@ -339,6 +339,7 @@ module Puppet @parameters.each do |name, param| param.flush if param.respond_to?(:flush) end + @stat = nil end # Deal with backups. @@ -729,14 +730,15 @@ module Puppet method = :lstat end path = self[:path] + if @stat.nil? or refresh == true begin @stat = File.send(method, self[:path]) rescue Errno::ENOENT => error - @stat = nil + return nil rescue Errno::EACCES => error - self.warning "Could not stat; permission denied" - @stat = nil + warning "Could not stat; permission denied" + return nil end end diff --git a/lib/puppet/type/file/source.rb b/lib/puppet/type/file/source.rb index e43706051..60d4a5708 100755 --- a/lib/puppet/type/file/source.rb +++ b/lib/puppet/type/file/source.rb @@ -89,9 +89,9 @@ module Puppet def change_to_s(currentvalue, newvalue) # newvalue = "{md5}" + @metadata.checksum if @resource.property(:ensure).retrieve == :absent - return "creating from source %s with contents %s" % [metadata.source, @metadata.checksum] + return "creating from source %s with contents %s" % [metadata.source, metadata.checksum] else - return "replacing from source %s with contents %s" % [metadata.source, @metadata.checksum] + return "replacing from source %s with contents %s" % [metadata.source, metadata.checksum] end end @@ -108,8 +108,8 @@ module Puppet raise Puppet::DevError, "No source for content was stored with the metadata" unless metadata.source unless defined?(@content) and @content - unless tmp = Puppet::FileServing::Content.find(@metadata.source) - fail "Could not find any content at %s" % @metadata.source + unless tmp = Puppet::FileServing::Content.find(metadata.source) + fail "Could not find any content at %s" % metadata.source end @content = tmp.content end @@ -147,18 +147,17 @@ module Puppet def insync?(currentvalue) # the only thing this actual state can do is copy files around. Therefore, # only pay attention if the remote is a file. - unless @metadata.ftype == "file" - return true - end + return true unless metadata.ftype == "file" - #FIXARB: Inefficient? Needed to call retrieve on parent's ensure and checksum - parentensure = @resource.property(:ensure).retrieve - if parentensure != :absent and ! @resource.replace? - return true - end + # The file is not in sync if it doesn't even exist. + return false unless resource.stat + + # The file is considered in sync if it exists and 'replace' is false. + return true unless resource.replace? + # Now, we just check to see if the checksums are the same parentchecksum = @resource.property(:checksum).retrieve - result = (!parentchecksum.nil? and (parentchecksum == @metadata.checksum)) + result = (!parentchecksum.nil? and (parentchecksum == metadata.checksum)) # Diff the contents if they ask it. This is quite annoying -- we need to do this in # 'insync?' because they might be in noop mode, but we don't want to do the file @@ -174,7 +173,7 @@ module Puppet end def found? - ! (@metadata.nil? or @metadata.ftype.nil?) + ! (metadata.nil? or metadata.ftype.nil?) end # Provide, and retrieve if necessary, the metadata for this file. Fail diff --git a/spec/integration/type/file.rb b/spec/integration/type/file.rb index b59fa3294..7d5a0c280 100755 --- a/spec/integration/type/file.rb +++ b/spec/integration/type/file.rb @@ -2,127 +2,180 @@ require File.dirname(__FILE__) + '/../../spec_helper' -describe Puppet::Type.type(:file), "when recursing" do - def mkdir +describe Puppet::Type.type(:file) do + def tmpfile(name) + source = Tempfile.new(name) + source.close! + source.path end - def build_path(dir) - Dir.mkdir(dir) - File.chmod(0750, dir) + describe "when recursing" do + def build_path(dir) + Dir.mkdir(dir) + File.chmod(0750, dir) + + @dirs = [dir] + @files = [] + + %w{one two}.each do |subdir| + fdir = File.join(dir, subdir) + Dir.mkdir(fdir) + File.chmod(0750, fdir) + @dirs << fdir + + %w{three}.each do |file| + ffile = File.join(fdir, file) + @files << ffile + File.open(ffile, "w") { |f| f.puts "test %s" % file } + File.chmod(0640, ffile) + end + end + end + + it "should be able to recurse over a nonexistent file" do + @path = tmpfile("file_integration_tests") + + @file = Puppet::Type::File.create(:name => @path, :mode => 0644, :recurse => true) + + @catalog = Puppet::Node::Catalog.new + @catalog.add_resource @file + + lambda { @file.eval_generate }.should_not raise_error + end + + it "should be able to recursively set properties on existing files" do + @path = tmpfile("file_integration_tests") + + build_path(@path) - @dirs = [dir] - @files = [] + @file = Puppet::Type::File.create(:name => @path, :mode => 0644, :recurse => true) - %w{one two}.each do |subdir| - fdir = File.join(dir, subdir) - Dir.mkdir(fdir) - File.chmod(0750, fdir) - @dirs << fdir + @catalog = Puppet::Node::Catalog.new + @catalog.add_resource @file - %w{three}.each do |file| - ffile = File.join(fdir, file) - @files << ffile - File.open(ffile, "w") { |f| f.puts "test %s" % file } - File.chmod(0640, ffile) + @catalog.apply + + @dirs.each do |path| + (File.stat(path).mode & 007777).should == 0755 + end + + @files.each do |path| + (File.stat(path).mode & 007777).should == 0644 end end - end - it "should be able to recurse over a nonexistent file" do - @path = Tempfile.new("file_integration_tests") - @path.close!() - @path = @path.path + it "should be able to recursively make links to other files" do + source = tmpfile("file_link_integration_source") - @file = Puppet::Type::File.create(:name => @path, :mode => 0644, :recurse => true) + build_path(source) - @catalog = Puppet::Node::Catalog.new - @catalog.add_resource @file + dest = tmpfile("file_link_integration_dest") - lambda { @file.eval_generate }.should_not raise_error - end + @file = Puppet::Type::File.create(:name => dest, :target => source, :recurse => true, :ensure => :link) - it "should be able to recursively set properties on existing files" do - @path = Tempfile.new("file_integration_tests") - @path.close!() - @path = @path.path + @catalog = Puppet::Node::Catalog.new + @catalog.add_resource @file - build_path(@path) + @catalog.apply - @file = Puppet::Type::File.create(:name => @path, :mode => 0644, :recurse => true) + @dirs.each do |path| + link_path = path.sub(source, dest) - @catalog = Puppet::Node::Catalog.new - @catalog.add_resource @file + File.lstat(link_path).should be_directory + end - @catalog.apply + @files.each do |path| + link_path = path.sub(source, dest) - @dirs.each do |path| - (File.stat(path).mode & 007777).should == 0755 + File.lstat(link_path).ftype.should == "link" + end end - @files.each do |path| - (File.stat(path).mode & 007777).should == 0644 - end - end + it "should be able to recursively copy files" do + source = tmpfile("file_source_integration_source") - it "should be able to recursively make links to other files" do - source = Tempfile.new("file_link_integration_source") - source.close!() - source = source.path + build_path(source) - build_path(source) + dest = tmpfile("file_source_integration_dest") - dest = Tempfile.new("file_link_integration_dest") - dest.close!() - dest = dest.path + @file = Puppet::Type::File.create(:name => dest, :source => source, :recurse => true) - @file = Puppet::Type::File.create(:name => dest, :target => source, :recurse => true, :ensure => :link) + @catalog = Puppet::Node::Catalog.new + @catalog.add_resource @file - @catalog = Puppet::Node::Catalog.new - @catalog.add_resource @file + @catalog.apply - @catalog.apply + @dirs.each do |path| + newpath = path.sub(source, dest) - @dirs.each do |path| - link_path = path.sub(source, dest) - - File.lstat(link_path).should be_directory - end + File.lstat(newpath).should be_directory + end - @files.each do |path| - link_path = path.sub(source, dest) + @files.each do |path| + newpath = path.sub(source, dest) - File.lstat(link_path).ftype.should == "link" + File.lstat(newpath).ftype.should == "file" + end end end - it "should be able to recursively copy files" do - source = Tempfile.new("file_source_integration_source") - source.close!() - source = source.path + describe "when copying files" do + # Ticket #285. + it "should be able to copy files with pound signs in their names" do + source = tmpfile("filewith#signs") + + dest = tmpfile("destwith#signs") - build_path(source) + File.open(source, "w") { |f| f.print "foo" } - dest = Tempfile.new("file_source_integration_dest") - dest.close!() - dest = dest.path + file = Puppet::Type::File.create(:name => dest, :source => source) + + catalog = Puppet::Node::Catalog.new + catalog.add_resource file + + catalog.apply + + File.read(dest).should == "foo" + end - @file = Puppet::Type::File.create(:name => dest, :source => source, :recurse => true) + it "should be able to copy files with spaces in their names" do + source = tmpfile("filewith spaces") - @catalog = Puppet::Node::Catalog.new - @catalog.add_resource @file + dest = tmpfile("destwith spaces") - @catalog.apply + File.open(source, "w") { |f| f.print "foo" } - @dirs.each do |path| - newpath = path.sub(source, dest) + file = Puppet::Type::File.create(:name => dest, :source => source) - File.lstat(newpath).should be_directory + catalog = Puppet::Node::Catalog.new + catalog.add_resource file + + catalog.apply + + File.read(dest).should == "foo" end - @files.each do |path| - newpath = path.sub(source, dest) + it "should be able to notice changed files in the same process" do + source = tmpfile("source") + dest = tmpfile("dest") + + File.open(source, "w") { |f| f.print "foo" } + + file = Puppet::Type::File.create(:name => dest, :source => source) + + catalog = Puppet::Node::Catalog.new + catalog.add_resource file + catalog.apply + + File.read(dest).should == "foo" + + # Now change the file + File.open(source, "w") { |f| f.print "bar" } + catalog.apply + + # And make sure it's changed + File.read(dest).should == "bar" - File.lstat(newpath).ftype.should == "file" end end end diff --git a/spec/unit/type/file.rb b/spec/unit/type/file.rb index f0ebb49e2..a476d3cd5 100755 --- a/spec/unit/type/file.rb +++ b/spec/unit/type/file.rb @@ -75,12 +75,91 @@ describe Puppet::Type.type(:file) do end end + it "should be able to retrieve a stat instance for the file it is managing" do + Puppet.type(:file).create(:path => "/foo/bar", :source => "/bar/foo").should respond_to(:stat) + end + + describe "when stat'ing its file" do + before do + @resource = Puppet.type(:file).create(:path => "/foo/bar") + @resource[:links] = :manage # so we always use :lstat + end + + it "should use :stat if it is following links" do + @resource[:links] = :follow + File.expects(:stat) + + @resource.stat + end + + it "should use :lstat if is it not following links" do + @resource[:links] = :manage + File.expects(:lstat) + + @resource.stat + end + + it "should stat the path of the file" do + File.expects(:lstat).with("/foo/bar") + + @resource.stat + end + + # This only happens in testing. + it "should return nil if the stat does not exist" do + File.expects(:lstat).returns nil + + @resource.stat.should be_nil + end + + it "should return nil if the file does not exist" do + File.expects(:lstat).raises(Errno::ENOENT) + + @resource.stat.should be_nil + end + + it "should return nil if the file cannot be stat'ed" do + File.expects(:lstat).raises(Errno::EACCES) + + @resource.stat.should be_nil + end + + it "should return the stat instance" do + File.expects(:lstat).returns "mystat" + + @resource.stat.should == "mystat" + end + + it "should cache the stat instance" do + stat = mock 'stat' + File.expects(:lstat).returns stat + + @resource.stat.should equal(@resource.stat) + end + + it "should not cache nil stat values" do + stat = mock 'stat' + File.expects(:lstat).times(2).returns(nil).then.returns(stat) + + @resource.stat.should be_nil + @resource.stat.should equal(stat) + end + end + describe "when flushing" do it "should flush all properties that respond to :flush" do @resource = Puppet.type(:file).create(:path => "/foo/bar", :source => "/bar/foo") @resource.property(:source).expects(:flush) @resource.flush end + + it "should reset its stat reference" do + @resource = Puppet.type(:file).create(:path => "/foo/bar") + File.expects(:lstat).times(2).returns("stat1").then.returns("stat2") + @resource.stat.should == "stat1" + @resource.flush + @resource.stat.should == "stat2" + end end it "should have a method for performing recursion" do @@ -131,7 +210,7 @@ describe Puppet::Type.type(:file) do @metadata = stub 'metadata', :relative_path => "my/file" end - it "should pass its to the :perform_recursion method" do + it "should pass its path to the :perform_recursion method" do @file.expects(:perform_recursion).with(@file[:path]).returns [@metadata] @file.stubs(:newchild) @file.recurse_local diff --git a/spec/unit/type/file/source.rb b/spec/unit/type/file/source.rb index 2883e9c6b..5a677e725 100755 --- a/spec/unit/type/file/source.rb +++ b/spec/unit/type/file/source.rb @@ -6,7 +6,7 @@ source = Puppet::Type.type(:file).attrclass(:source) describe Puppet::Type.type(:file).attrclass(:source) do before do # Wow that's a messy interface to the resource. - @resource = stub 'resource', :uri2obj => true, :[]= => nil, :property => nil + @resource = stub 'resource', :[]= => nil, :property => nil end it "should be a subclass of Property" do @@ -214,6 +214,86 @@ describe Puppet::Type.type(:file).attrclass(:source) do @source.instance_variable_get("@content").should be_nil end end + + describe "when testing whether the local file is in sync" do + before do + @source = source.new(:resource => @resource) + end + + it "should be considered in sync if the remote file is a directory" do + metadata = mock 'data', :ftype => "directory" + @source.expects(:metadata).returns metadata + + @source.must be_insync("some content") + end + + it "should be considered in sync if the remote file is a symlink" do + metadata = mock 'data', :ftype => "link" + @source.expects(:metadata).returns metadata + + @source.must be_insync("some content") + end + + describe "and the remote file is a normal file" do + before do + @metadata = mock 'data', :ftype => "file" + @source.expects(:metadata).returns @metadata + end + + it "should be not considered in sync if the file does not exist" do + @resource.expects(:stat).returns nil + @source.should_not be_insync("some content") + end + + it "should be considered in sync if :replace is false and the file exists" do + @resource.expects(:stat).returns mock('stat') + @resource.expects(:replace?).returns false + @source.must be_insync("some content") + end + + it "should be not considered in sync if :replace is false and the file does not exist" do + @resource.expects(:stat).returns nil + @resource.stubs(:replace?).returns false + @source.should_not be_insync("some content") + end + + it "should not be considered in sync if the local file's contents are not the same as the remote file's contents" + + it "should be considered in sync if the local file's content matches the remote file's contents" + end + end + + def test_insync + source = tempfile() + dest = tempfile() + + file = Puppet::Type.type(:file).create :path => dest, :source => source, :title => "copier" + + property = file.property(:source) + assert(property, "did not get source property") + + # with a directory + Dir.mkdir(source) + currentvalues = file.retrieve + assert(property.insync?(currentvalues[property]), "source property not in sync with directory as source") + Dir.rmdir(source) + + # with a file + File.open(source, "w") { |f| f.puts "yay" } + currentvalues = file.retrieve + p currentvalues[property] + assert(!property.insync?(currentvalues[property]), "source property was in sync when file was missing") + + # With a different file + File.open(dest, "w") { |f| f.puts "foo" } + currentvalues = file.retrieve + assert(!property.insync?(currentvalues[property]), "source property was in sync with different file") + + # with matching files + File.open(dest, "w") { |f| f.puts "yay" } + currentvalues = file.retrieve + assert(property.insync?(currentvalues[property]), "source property was not in sync with matching file") + end it "should have a method for returning the content" do source.new(:resource => @resource).must respond_to(:content) diff --git a/test/ral/type/file.rb b/test/ral/type/file.rb index fc7c39796..f14281d33 100755 --- a/test/ral/type/file.rb +++ b/test/ral/type/file.rb @@ -482,159 +482,6 @@ class TestFile < Test::Unit::TestCase } end - def test_localrecurse - # Create a test directory - path = tempfile() - dir = @file.create :path => path, :mode => 0755, :recurse => true - catalog = mk_catalog(dir) - - Dir.mkdir(path) - - # Make sure we return nothing when there are no children - ret = nil - assert_nothing_raised() { ret = dir.localrecurse(true) } - assert_equal([], ret, "empty dir returned children") - - # Now make a file and make sure we get it - test = File.join(path, "file") - File.open(test, "w") { |f| f.puts "yay" } - assert_nothing_raised() { ret = dir.localrecurse(true) } - fileobj = catalog.resource(:file, test) - assert(fileobj, "child object was not created") - assert_equal([fileobj], ret, "child object was not returned") - - # And that it inherited our recurse setting - assert_equal(true, fileobj[:recurse], "file did not inherit recurse") - - # Make sure it's not returned again - assert_nothing_raised() { ret = dir.localrecurse(true) } - assert_equal([], ret, "child object was returned twice") - - # Now just for completion, make sure we will return many files - files = [] - 10.times do |i| - f = File.join(path, i.to_s) - files << f - File.open(f, "w") do |o| o.puts "" end - end - assert_nothing_raised() { ret = dir.localrecurse(true) } - assert_equal(files.sort, ret.collect { |f| f.title }.sort, - "child object was returned twice") - - # Clean everything up and start over - files << test - files.each do |f| File.unlink(f) end - - # Now make sure we correctly ignore things - dir[:ignore] = "*.out" - bad = File.join(path, "test.out") - good = File.join(path, "yayness") - [good, bad].each do |f| - File.open(f, "w") { |o| o.puts "" } - end - - assert_nothing_raised() { ret = dir.localrecurse(true) } - assert_equal([good], ret.collect { |f| f.title }, "ignore failed") - - # Now make sure purging works - dir[:purge] = true - dir[:ignore] = "svn" - - assert_nothing_raised() { ret = dir.localrecurse(true) } - assert_equal([bad], ret.collect { |f| f.title }, "purge failed") - - badobj = catalog.resource(:file, bad) - assert(badobj, "did not create bad object") - end - - def test_recurse - basedir = tempfile() - FileUtils.mkdir_p(basedir) - - # Create our file - dir = nil - assert_nothing_raised { - dir = Puppet.type(:file).create( - :path => basedir, - :check => %w{owner mode group} - ) - } - - return_nil = false - - # and monkey-patch it - [:localrecurse, :linkrecurse].each do |m| - dir.meta_def(m) do |recurse| - if return_nil # for testing nil return, of course - return nil - else - return [recurse] - end - end - end - - # We have to special-case this, because it returns a list of - # found files. - dir.meta_def(:sourcerecurse) do |recurse| - if return_nil # for testing nil return, of course - return nil - else - return [recurse], [] - end - end - - # First try it with recurse set to false - dir[:recurse] = false - assert_nothing_raised do - assert_nil(dir.recurse) - end - - # Now try it with the different valid positive values - [true, "true", "inf", 50].each do |value| - assert_nothing_raised { dir[:recurse] = value} - - # Now make sure the methods are called appropriately - ret = nil - assert_nothing_raised do - ret = dir.recurse - end - - # We should only call the localrecurse method, so make sure - # that's the case - if value == 50 - # Make sure our counter got decremented - assert_equal([49], ret, "did not call localrecurse") - else - assert_equal([true], ret, "did not call localrecurse") - end - end - - # Make sure it doesn't recurse when we've set recurse to false - [false, "false"].each do |value| - assert_nothing_raised { dir[:recurse] = value } - - ret = nil - assert_nothing_raised() { ret = dir.recurse } - assert_nil(ret) - end - dir[:recurse] = true - - # Now add a target, so we do the linking thing - dir[:target] = tempfile() - ret = nil - assert_nothing_raised { ret = dir.recurse } - assert_equal([true, true], ret, "did not call linkrecurse") - - # And add a source, and make sure we call that - dir[:source] = tempfile() - assert_nothing_raised { ret = dir.recurse } - assert_equal([true, true, true], ret, "did not call linkrecurse") - - # Lastly, make sure we correctly handle returning nil - return_nil = true - assert_nothing_raised { ret = dir.recurse } - end - def test_recurse? file = Puppet::Type.type(:file).create :path => tempfile diff --git a/test/ral/type/filesources.rb b/test/ral/type/filesources.rb index 8977c81b5..9dd1d988e 100755 --- a/test/ral/type/filesources.rb +++ b/test/ral/type/filesources.rb @@ -53,82 +53,6 @@ class TestFileSources < Test::Unit::TestCase destfile = File.join(dest, "file") return source, dest, sourcefile, destfile end - - def test_newchild - path = tempfile() - @@tmpfiles.push path - - FileUtils.mkdir_p path - File.open(File.join(path,"childtest"), "w") { |of| - of.puts "yayness" - } - file = nil - comp = nil - trans = nil - assert_nothing_raised { - file = Puppet.type(:file).create( - :name => path - ) - } - catalog = mk_catalog(file) - child = nil - assert_nothing_raised { - child = file.newchild("childtest", true) - } - assert(child) - assert_raise(Puppet::DevError) { - file.newchild(File.join(path,"childtest"), true) - } - end - - def test_describe - source = tempfile() - dest = tempfile() - - file = Puppet::Type.newfile :path => dest, :source => source, - :title => "copier" - - property = file.property(:source) - - # First try describing with a normal source - result = nil - assert_nothing_raised do - result = property.describe(source) - end - assert_nil(result, "Got a result back when source is missing") - - # Now make a remote directory - Dir.mkdir(source) - assert_nothing_raised do - result = property.describe(source) - end - assert_equal("directory", result[:type]) - - # And as a file - Dir.rmdir(source) - File.open(source, "w") { |f| f.puts "yay" } - assert_nothing_raised do - result = property.describe(source) - end - assert_equal("file", result[:type]) - assert(result[:checksum], "did not get value for checksum") - if Puppet::Util::SUIDManager.uid == 0 - assert(result.has_key?(:owner), "Lost owner in describe") - else - assert(! result.has_key?(:owner), - "Kept owner in describe even tho not root") - end - - # Now let's do the various link things - File.unlink(source) - target = tempfile() - File.open(target, "w") { |f| f.puts "yay" } - File.symlink(target, source) - - # And then make sure links get followed, otherwise - file[:links] = :follow - assert_equal("file", property.describe(source)[:type]) - end def test_source_retrieve source = tempfile() @@ -185,38 +109,6 @@ class TestFileSources < Test::Unit::TestCase "Did not catch later source") end - def test_insync - source = tempfile() - dest = tempfile() - - file = Puppet::Type.newfile :path => dest, :source => source, - :title => "copier" - - property = file.property(:source) - assert(property, "did not get source property") - - # with a directory - Dir.mkdir(source) - currentvalues = file.retrieve - assert(property.insync?(currentvalues[property]), "source property not in sync with directory as source") - Dir.rmdir(source) - - # with a file - File.open(source, "w") { |f| f.puts "yay" } - currentvalues = file.retrieve - assert(!property.insync?(currentvalues[property]), "source property was in sync when file was missing") - - # With a different file - File.open(dest, "w") { |f| f.puts "foo" } - currentvalues = file.retrieve - assert(!property.insync?(currentvalues[property]), "source property was in sync with different file") - - # with matching files - File.open(dest, "w") { |f| f.puts "yay" } - currentvalues = file.retrieve - assert(property.insync?(currentvalues[property]), "source property was not in sync with matching file") - end - def test_source_sync source = tempfile() dest = tempfile() @@ -249,92 +141,6 @@ class TestFileSources < Test::Unit::TestCase assert_equal(File.read(source), File.read(dest), "File was not copied correctly") end - - # XXX This test doesn't cover everything. Specifically, - # it doesn't handle 'ignore' and 'links'. - def test_sourcerecurse - source, dest, sourcefile, destfile = mk_sourcetree - - # The sourcerecurse method will only ever get called when we're - # recursing, so we go ahead and set it. - obj = Puppet::Type.newfile :source => source, :path => dest, :recurse => true - catalog = mk_catalog(obj) - - result = nil - sourced = nil - assert_nothing_raised do - result, sourced = obj.sourcerecurse(true) - end - - assert_equal([destfile], sourced, "Did not get correct list of sourced objects") - dfileobj = catalog.resource(:file, destfile) - assert(dfileobj, "Did not create destfile object") - assert_equal([dfileobj], result) - - # Clean this up so it can be recreated - catalog.remove_resource(dfileobj) - - # Make sure we correctly iterate over the sources - nosource = tempfile() - obj[:source] = [nosource, source] - - result = nil - assert_nothing_raised do - result, sourced = obj.sourcerecurse(true) - end - assert_equal([destfile], sourced, "Did not get correct list of sourced objects") - dfileobj = catalog.resource(:file, destfile) - assert(dfileobj, "Did not create destfile object with a missing source") - assert_equal([dfileobj], result) - dfileobj.remove - - # Lastly, make sure we return an empty array when no sources are there - obj[:source] = [nosource, tempfile()] - - assert_raise(Puppet::Error) do - result, sourced = obj.sourcerecurse(true) - end - end - - def test_simplelocalsource - path = tempfile() - FileUtils.mkdir_p path - frompath = File.join(path,"source") - topath = File.join(path,"dest") - fromfile = nil - tofile = nil - trans = nil - - File.open(frompath, File::WRONLY|File::CREAT|File::APPEND) { |of| - of.puts "yayness" - } - assert_nothing_raised { - tofile = Puppet.type(:file).create( - :name => topath, - :source => frompath - ) - } - - assert_apply(tofile) - - assert(FileTest.exists?(topath), "File #{topath} is missing") - from = File.open(frompath) { |o| o.read } - to = File.open(topath) { |o| o.read } - assert_equal(from,to) - end - - # Make sure a simple recursive copy works - def test_simple_recursive_source - source, dest, sourcefile, destfile = mk_sourcetree - - file = Puppet::Type.newfile :path => dest, :source => source, :recurse => true - - assert_events([:directory_created, :file_created], file) - - assert(FileTest.directory?(dest), "Dest dir was not created") - assert(FileTest.file?(destfile), "dest file was not created") - assert_equal("yay\n", File.read(destfile), "dest file was not copied correctly") - end def recursive_source_test(fromdir, todir) initstorage @@ -507,53 +313,6 @@ class TestFileSources < Test::Unit::TestCase return file end - def test_NetworkSources - server = nil - mounts = { - "/" => "root" - } - - fileserverconf = mkfileserverconf(mounts) - - Puppet[:autosign] = true - - Puppet[:masterport] = 8762 - Puppet[:name] = "puppetmasterd" - Puppet[:certdnsnames] = "localhost" - - serverpid = nil - assert_nothing_raised() { - server = Puppet::Network::HTTPServer::WEBrick.new( - :Handlers => { - :CA => {}, # so that certs autogenerate - :FileServer => { - :Config => fileserverconf - } - } - ) - - } - serverpid = fork { - assert_nothing_raised() { - #trap(:INT) { server.shutdown; Kernel.exit! } - trap(:INT) { server.shutdown } - server.start - } - } - @@tmppids << serverpid - - sleep(1) - - fromdir, todir = run_complex_sources("root") - assert_trees_equal(fromdir,todir) - recursive_source_test(fromdir, todir) - assert_trees_equal(fromdir,todir) - - assert_nothing_raised { - system("kill -INT %s" % serverpid) - } - end - def test_unmountedNetworkSources server = nil mounts = { @@ -730,61 +489,6 @@ class TestFileSources < Test::Unit::TestCase assert(FileTest.file?(dest), "Destination is not a file") end - def test_changes - source = tempfile() - dest = tempfile() - - File.open(source, "w") { |f| f.puts "yay" } - - obj = nil - assert_nothing_raised { - obj = Puppet.type(:file).create( - :name => dest, - :source => source - ) - } - - assert_events([:file_created], obj) - assert_equal(File.read(source), File.read(dest), "Files are not equal") - assert_events([], obj) - - File.open(source, "w") { |f| f.puts "boo" } - - assert_events([:file_changed], obj) - assert_equal(File.read(source), File.read(dest), "Files are not equal") - assert_events([], obj) - - File.open(dest, "w") { |f| f.puts "kaboom" } - - # There are two changes, because first the checksum is noticed, and - # then the source causes a change - assert_events([:file_changed, :file_changed], obj) - assert_equal(File.read(source), File.read(dest), "Files are not equal") - assert_events([], obj) - end - - def test_file_source_with_space - dir = tempfile() - source = File.join(dir, "file with spaces") - Dir.mkdir(dir) - File.open(source, "w") { |f| f.puts "yayness" } - - newdir = tempfile() - newpath = File.join(newdir, "file with spaces") - - file = Puppet::Type.newfile( - :path => newdir, - :source => dir, - :recurse => true - ) - - - assert_apply(file) - - assert(FileTest.exists?(newpath), "Did not create file") - assert_equal("yayness\n", File.read(newpath)) - end - # Make sure files aren't replaced when replace is false, but otherwise # are. def test_replace @@ -827,39 +531,6 @@ class TestFileSources < Test::Unit::TestCase "File was not replaced when :replace was true") end - # Testing #285. This just makes sure that URI parsing works correctly. - def test_fileswithpoundsigns - dir = tstdir() - subdir = File.join(dir, "#dir") - Dir.mkdir(subdir) - file = File.join(subdir, "file") - File.open(file, "w") { |f| f.puts "yayness" } - - dest = tempfile() - source = "file://localhost#{dir}" - obj = Puppet::Type.newfile( - :path => dest, - :source => source, - :recurse => true - ) - - newfile = File.join(dest, "#dir", "file") - - poundsource = "file://localhost#{subdir}" - - sourceobj = path = nil - assert_nothing_raised { - sourceobj, path = obj.uri2obj(poundsource) - } - - assert_equal("/localhost" + URI.escape(subdir), path) - - assert_apply(obj) - - assert(FileTest.exists?(newfile), "File did not get created") - assert_equal("yayness\n", File.read(newfile)) - end - def test_sourceselect dest = tempfile() sources = [] |