diff options
| author | luke <luke@980ebf18-57e1-0310-9a29-db15c13687c0> | 2006-12-09 16:40:50 +0000 |
|---|---|---|
| committer | luke <luke@980ebf18-57e1-0310-9a29-db15c13687c0> | 2006-12-09 16:40:50 +0000 |
| commit | cdd1e6e19e7b8fc340ebcf543a30564c76e71eb9 (patch) | |
| tree | 65f7511a9f6386db44c92c502de1d56c7d2f0b6d /test | |
| parent | 01e5b692f0ba209956f4b84c7e597bd867154cbf (diff) | |
Another intermediate commit. Most of the graphing work itself is now done, but I am in the middle of converting files to use the graphs and at the same time am writing some actually decent tests for the file recursion stuff.
git-svn-id: https://reductivelabs.com/svn/puppet/trunk@1899 980ebf18-57e1-0310-9a29-db15c13687c0
Diffstat (limited to 'test')
| -rwxr-xr-x | test/other/transactions.rb | 204 | ||||
| -rwxr-xr-x | test/server/fileserver.rb | 5 | ||||
| -rwxr-xr-x | test/types/file.rb | 305 | ||||
| -rwxr-xr-x | test/types/filesources.rb | 308 |
4 files changed, 728 insertions, 94 deletions
diff --git a/test/other/transactions.rb b/test/other/transactions.rb index c143b3a0c..7342b57ec 100755 --- a/test/other/transactions.rb +++ b/test/other/transactions.rb @@ -11,6 +11,21 @@ require 'puppettest/support/resources' class TestTransactions < Test::Unit::TestCase include PuppetTest::FileTesting include PuppetTest::Support::Resources + + def mkgenerator(&block) + # Create a bogus type that generates new instances with shorter + type = Puppet::Type.newtype(:generator) do + newparam(:name, :namevar => true) + end + if block + type.class_eval(&block) + end + cleanup do + Puppet::Type.rmtype(:generator) + end + + return type + end def test_reports path1 = tempfile() @@ -541,22 +556,19 @@ class TestTransactions < Test::Unit::TestCase graph.to_jpg("normal_relations") end + # Test pre-evaluation generation def test_generate - # Create a bogus type that generates new instances with shorter - Puppet::Type.newtype(:generator) do - newparam(:name, :namevar => true) - + mkgenerator() do def generate ret = [] if title.length > 1 ret << self.class.create(:title => title[0..-2]) + else + return nil end ret end end - cleanup do - Puppet::Type.rmtype(:generator) - end yay = Puppet::Type.newgenerator :title => "yay" rah = Puppet::Type.newgenerator :title => "rah" @@ -571,6 +583,184 @@ class TestTransactions < Test::Unit::TestCase assert(trans.resources.vertex?(Puppet::Type.type(:generator)[name]), "Generated %s was not a vertex" % name) end + + # Now make sure that cleanup gets rid of those generated types. + assert_nothing_raised do + trans.cleanup + end + + %w{ya ra y r}.each do |name| + assert(!trans.resources.vertex?(Puppet::Type.type(:generator)[name]), + "Generated vertex %s was not removed from graph" % name) + assert_nil(Puppet::Type.type(:generator)[name], + "Generated vertex %s was not removed from class" % name) + end + end + + # Test mid-evaluation generation. + def test_eval_generate + $evaluated = {} + type = mkgenerator() do + def eval_generate + ret = [] + if title.length > 1 + ret << self.class.create(:title => title[0..-2]) + else + return nil + end + ret + end + + def evaluate + $evaluated[self.title] = true + return [] + end + end + + yay = Puppet::Type.newgenerator :title => "yay" + rah = Puppet::Type.newgenerator :title => "rah", :subscribe => yay + comp = newcomp(yay, rah) + trans = comp.evaluate + + trans.prepare + + # Now apply the resources, and make sure they appropriately generate + # things. + assert_nothing_raised("failed to apply yay") do + trans.apply(yay) + end + ya = type["ya"] + assert(ya, "Did not generate ya") + assert(trans.relgraph.vertex?(ya), + "Did not add ya to rel_graph") + + # Now make sure the appropriate relationships were added + assert(trans.relgraph.edge?(yay, ya), + "parent was not required by child") + assert(trans.relgraph.edge?(ya, rah), + "rah was not subscribed to ya") + + # And make sure the relationship is a subscription with a callback, + # not just a require. + assert_equal({:callback => :refresh, :event => :ALL_EVENTS}, + trans.relgraph[Puppet::Relationship.new(ya, rah)], + "The label was not retained") + + # Now make sure it in turn eval_generates appropriately + assert_nothing_raised("failed to apply yay") do + trans.apply(type["ya"]) + end + + %w{y}.each do |name| + res = type[name] + assert(res, "Did not generate %s" % name) + assert(trans.relgraph.vertex?(res), + "Did not add %s to rel_graph" % name) + end + + assert_nothing_raised("failed to eval_generate with nil response") do + trans.apply(type["y"]) + end + + assert_equal(%w{yay ya y rah}, trans.sorted_resources.collect { |r| r.title }, + "Did not eval_generate correctly") + + assert_nothing_raised("failed to apply rah") do + trans.apply(rah) + end + + ra = type["ra"] + assert(ra, "Did not generate ra") + assert(trans.relgraph.vertex?(ra), + "Did not add ra to rel_graph" % name) + + # Now make sure this generated resource has the same relationships as the generating + # resource + assert(trans.relgraph.edge?(yay, ra), + "yay is not required by ra") + assert(trans.relgraph.edge?(ya, ra), + "ra is not subscribed to ya") + + # And make sure the relationship is a subscription with a callback, + # not just a require. + assert_equal({:callback => :refresh, :event => :ALL_EVENTS}, + trans.relgraph[Puppet::Relationship.new(ya, ra)], + "The label was not retained") + + # Now make sure that cleanup gets rid of those generated types. + assert_nothing_raised do + trans.cleanup + end + + %w{ya ra y r}.each do |name| + assert(!trans.relgraph.vertex?(type[name]), + "Generated vertex %s was not removed from graph" % name) + assert_nil(type[name], + "Generated vertex %s was not removed from class" % name) + end + + # Now, start over and make sure that everything gets evaluated. + trans = comp.evaluate + assert_nothing_raised do + trans.evaluate + end + + assert_equal(%w{yay ya y rah ra r}.sort, $evaluated.keys.sort, + "Not all resources were evaluated") + end + + def test_tags + res = Puppet::Type.newfile :path => tempfile() + comp = newcomp(res) + + # Make sure they default to none + assert_equal([], comp.evaluate.tags) + + # Make sure we get the main tags + Puppet[:tags] = %w{this is some tags} + assert_equal(%w{this is some tags}, comp.evaluate.tags) + + # And make sure they get processed correctly + Puppet[:tags] = ["one", "two,three", "four"] + assert_equal(%w{one two three four}, comp.evaluate.tags) + + # lastly, make sure we can override them + trans = comp.evaluate + trans.tags = ["one", "two,three", "four"] + assert_equal(%w{one two three four}, comp.evaluate.tags) + end + + def test_tagged? + res = Puppet::Type.newfile :path => tempfile() + comp = newcomp(res) + trans = comp.evaluate + + assert(trans.tagged?(res), "tagged? defaulted to false") + + # Now set some tags + trans.tags = %w{some tags} + + # And make sure it's false + assert(! trans.tagged?(res), "matched invalid tags") + + # Set ignoretags and make sure it sticks + trans.ignoretags = true + assert(trans.tagged?(res), "tags were not ignored") + + # Now make sure we actually correctly match tags + res[:tag] = "mytag" + trans.ignoretags = false + trans.tags = %w{notag} + + assert(! trans.tagged?(res), "tags incorrectly matched") + + trans.tags = %w{mytag yaytag} + assert(trans.tagged?(res), "tags should have matched") + + end + + # Make sure events propagate down the relationship graph appropriately. + def test_trigger end end diff --git a/test/server/fileserver.rb b/test/server/fileserver.rb index 00d235cb2..5e9c60ddc 100755 --- a/test/server/fileserver.rb +++ b/test/server/fileserver.rb @@ -493,7 +493,7 @@ class TestFileServer < Test::Unit::TestCase # create a server with the file assert_nothing_raised { server = Puppet::Server::FileServer.new( - :Local => true, + :Local => false, :Config => conffile ) } @@ -658,7 +658,7 @@ class TestFileServer < Test::Unit::TestCase # start our server with a fast timeout assert_nothing_raised { server = Puppet::Server::FileServer.new( - :Local => true, + :Local => false, :Config => conffile ) } @@ -968,6 +968,7 @@ allow * end + # Test that the fileserver expands the %h and %d things. def test_fileserver_expansion server = nil assert_nothing_raised { diff --git a/test/types/file.rb b/test/types/file.rb index 28cb6c754..d5b493788 100755 --- a/test/types/file.rb +++ b/test/types/file.rb @@ -30,6 +30,7 @@ class TestFile < Test::Unit::TestCase def setup super + @file = Puppet::Type.type(:file) begin initstorage rescue @@ -489,11 +490,173 @@ class TestFile < Test::Unit::TestCase Puppet::Type.allclear } end - + + def test_localrecurse + # Create a test directory + path = tempfile() + dir = @file.create :path => path, :mode => 0755, :recurse => true + + 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 = @file[test] + assert(fileobj, "child object was not created") + assert_equal([fileobj], ret, "child object was not returned") + + # check that the file lists us as a dependency + assert_equal([[:file, dir.title]], fileobj[:require], "dependency was not set up") + + # 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 }, "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 = @file[bad] + assert(badobj, "did not create bad object") + assert_equal(:absent, badobj.should(:ensure), "ensure was not set to absent on 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, :sourcerecurse, :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 + + # 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 + + # Make sure we default to false + assert(! file.recurse?, "Recurse defaulted to true") + + [true, "true", 10, "inf"].each do |value| + file[:recurse] = value + assert(file.recurse?, "%s did not cause recursion" % value) + end + + [false, "false", 0].each do |value| + file[:recurse] = value + assert(! file.recurse?, "%s caused recursion" % value) + end + end + def test_recursion basedir = tempfile() - subdir = File.join(basedir, "this", "is", "sub", "dir") - tmpfile = File.join(subdir,"testing") + subdir = File.join(basedir, "subdir") + tmpfile = File.join(basedir,"testing") FileUtils.mkdir_p(subdir) dir = nil @@ -505,67 +668,33 @@ class TestFile < Test::Unit::TestCase :check => %w{owner mode group} ) } + + children = nil assert_nothing_raised { - dir.evaluate - } - - subobj = nil - assert_nothing_raised { - subobj = Puppet.type(:file)[subdir] + children = dir.eval_generate } - - assert(subobj, "Could not retrieve %s object" % subdir) + + assert_equal([subdir], children.collect {|c| c.title }, + "Incorrect generated children") + + dir.class[subdir].remove File.open(tmpfile, "w") { |f| f.puts "yayness" } - - dir.evaluate - - file = nil + assert_nothing_raised { - file = Puppet.type(:file)[tmpfile] + children = dir.eval_generate } - assert(file, "Could not retrieve %s object" % tmpfile) + assert_equal([subdir, tmpfile].sort, children.collect {|c| c.title }.sort, + "Incorrect generated children") + + File.unlink(tmpfile) #system("rm -rf %s" % basedir) Puppet.type(:file).clear end end -=begin - def test_ignore - - end -=end - - # XXX disabled until i change how dependencies work - def disabled_test_recursionwithcreation - path = "/tmp/this/directory/structure/does/not/exist" - @@tmpfiles.push "/tmp/this" - - file = nil - assert_nothing_raised { - file = mkfile( - :name => path, - :recurse => true, - :ensure => "file" - ) - } - - trans = nil - comp = newcomp("recursewithfiles", file) - assert_nothing_raised { - trans = comp.evaluate - } - - events = nil - assert_nothing_raised { - events = trans.evaluate.collect { |e| e.event.to_s } - } - - puts "events are %s" % events.join(", ") - end - def test_filetype_retrieval file = nil @@ -621,7 +750,7 @@ class TestFile < Test::Unit::TestCase } assert_nothing_raised { - dir.retrieve + dir.eval_generate } obj = nil @@ -645,7 +774,7 @@ class TestFile < Test::Unit::TestCase def test_path dir = tempfile() - path = File.join(dir, "and", "a", "sub", "dir") + path = File.join(dir, "subdir") assert_nothing_raised("Could not make file") { FileUtils.mkdir_p(File.dirname(path)) @@ -663,7 +792,7 @@ class TestFile < Test::Unit::TestCase } assert_nothing_raised { - dirobj.evaluate + dirobj.generate } assert_nothing_raised { @@ -968,10 +1097,57 @@ class TestFile < Test::Unit::TestCase assert_events([], file) assert_events([], file) end + + def test_linkrecurse + dest = tempfile() + link = @file.create :path => tempfile(), :recurse => true, :ensure => dest + + ret = nil + + # Start with nothing, just to make sure we get nothing back + assert_nothing_raised { ret = link.linkrecurse(true) } + assert_nil(ret, "got a return when the dest doesn't exist") + + # then with a directory with only one file + Dir.mkdir(dest) + one = File.join(dest, "one") + File.open(one, "w") { |f| f.puts "" } + link[:ensure] = dest + assert_nothing_raised { ret = link.linkrecurse(true) } + + assert_equal(:directory, link.should(:ensure), "ensure was not set to directory") + assert_equal([File.join(link.title, "one")], ret.collect { |f| f.title }, + "Did not get linked file") + oneobj = @file[File.join(link.title, "one")] + assert_equal(one, oneobj.should(:target), "target was not set correctly") + + oneobj.remove + File.unlink(one) + + # Then make sure we get multiple files + returns = [] + 5.times do |i| + path = File.join(dest, i.to_s) + returns << File.join(link.title, i.to_s) + File.open(path, "w") { |f| f.puts "" } + end + assert_nothing_raised { ret = link.linkrecurse(true) } + + assert_equal(returns.sort, ret.collect { |f| f.title }, + "Did not get links back") + + returns.each do |path| + obj = @file[path] + assert(path, "did not get obj for %s" % path) + sdest = File.join(dest, File.basename(path)) + assert_equal(sdest, obj.should(:target), + "target was not set correctly for %s" % path) + end + end def test_simplerecursivelinking source = tempfile() - dest = tempfile() + path = tempfile() subdir = File.join(source, "subdir") file = File.join(subdir, "file") @@ -982,20 +1158,21 @@ class TestFile < Test::Unit::TestCase assert_nothing_raised { link = Puppet.type(:file).create( :ensure => source, - :path => dest, + :path => path, :recurse => true ) } assert_apply(link) - subdest = File.join(dest, "subdir") - linkpath = File.join(subdest, "file") - assert(File.directory?(dest), "dest is not a dir") - assert(File.directory?(subdest), "subdest is not a dir") + sublink = File.join(path, "subdir") + linkpath = File.join(sublink, "file") + assert(File.directory?(path), "dest is not a dir") + assert(File.directory?(sublink), "subdest is not a dir") assert(File.symlink?(linkpath), "path is not a link") assert_equal(file, File.readlink(linkpath)) + assert_nil(@file[sublink], "objects were not removed") assert_events([], link) end @@ -1077,12 +1254,14 @@ class TestFile < Test::Unit::TestCase objects = [] objects << Puppet.type(:exec).create( :command => "mkdir %s; touch %s/file" % [source, source], + :title => "yay", :path => ENV["PATH"] ) objects << Puppet.type(:file).create( :ensure => source, :path => dest, - :recurse => true + :recurse => true, + :require => objects[0] ) assert_apply(*objects) @@ -1371,7 +1550,7 @@ class TestFile < Test::Unit::TestCase assert_equal("yayness", File.read(path), "Content did not get set correctly") end - # Make sure unmanaged files can be purged. + # Make sure unmanaged files are be purged. def test_purge sourcedir = tempfile() destdir = tempfile() @@ -1386,12 +1565,12 @@ class TestFile < Test::Unit::TestCase File.open(randfile, "w") { |f| f.puts "footest" } lfobj = Puppet::Type.newfile(:path => localfile, :content => "rahtest") + destobj = Puppet::Type.newfile(:path => destdir, :source => sourcedir, :recurse => true) - assert_apply(lfobj, destobj) assert(FileTest.exists?(dsourcefile), "File did not get copied") diff --git a/test/types/filesources.rb b/test/types/filesources.rb index 4c1139a0b..bae4c7d5f 100755 --- a/test/types/filesources.rb +++ b/test/types/filesources.rb @@ -11,26 +11,37 @@ class TestFileSources < Test::Unit::TestCase include PuppetTest::FileTesting def setup super - begin - initstorage - rescue - system("rm -rf %s" % Puppet[:statefile]) - end if defined? @port @port += 1 else @port = 8800 end + @file = Puppet::Type.type(:file) + end + + def use_storage + begin + initstorage + rescue + system("rm -rf %s" % Puppet[:statefile]) + end end def initstorage Puppet::Storage.init Puppet::Storage.load end - - def clearstorage - Puppet::Storage.store - Puppet::Storage.clear + + # Make a simple recursive tree. + def mk_sourcetree + source = tempfile() + sourcefile = File.join(source, "file") + Dir.mkdir source + File.open(sourcefile, "w") { |f| f.puts "yay" } + + dest = tempfile() + destfile = File.join(dest, "file") + return source, dest, sourcefile, destfile end def test_newchild @@ -58,10 +69,244 @@ class TestFileSources < Test::Unit::TestCase 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" + + state = file.state(:source) + + # First try describing with a normal source + result = nil + assert_nothing_raised do + result = state.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 = state.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 = state.describe(source) + end + assert_equal("file", result[:type]) + assert(result[:checksum], "did not get value for checksum") + if Puppet::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) + + file[:links] = :ignore + assert_nil(state.describe(source), + "Links were not ignored") + + file[:links] = :manage + # We can't manage links at this point + assert_raise(Puppet::FileServerError) do + state.describe(source) + end + + # And then make sure links get followed, otherwise + file[:links] = :follow + assert_equal("file", state.describe(source)[:type]) + end + + def test_source_retrieve + source = tempfile() + dest = tempfile() + + file = Puppet::Type.newfile :path => dest, :source => source, + :title => "copier" + + assert(file.state(:checksum), "source state did not create checksum state") + state = file.state(:source) + assert(state, "did not get source state") + + # Make sure the munge didn't actually change the source + assert_equal(source, state.should, "munging changed the source") + + # First try it with a missing source + assert_nothing_raised do + state.retrieve + end + + # And make sure the state considers itself in sync, since there's nothing + # to do + assert(state.insync?, "source thinks there's work to do with no file or dest") + + # Now make the dest a directory, and make sure the object sets :ensure up to + # create a directory + Dir.mkdir(source) + assert_nothing_raised do + state.retrieve + end + assert_equal(:directory, file.should(:ensure), + "Did not set to create directory") + + # And make sure the source state won't try to do anything with a remote dir + assert(state.insync?, "Source was out of sync even tho remote is dir") + + # Now remove the source, and make sure :ensure was not modified + Dir.rmdir(source) + assert_nothing_raised do + state.retrieve + end + assert_equal(:directory, file.should(:ensure), + "Did not keep :ensure setting") + + # Now have a remote file and make sure things work correctly + File.open(source, "w") { |f| f.puts "yay" } + File.chmod(0755, source) + + assert_nothing_raised do + state.retrieve + end + assert_equal(:file, file.should(:ensure), + "Did not make correct :ensure setting") + assert_equal(0755, file.should(:mode), + "Mode was not copied over") + + # Now let's make sure that we get the first found source + fake = tempfile() + state.should = [fake, source] + assert_nothing_raised do + state.retrieve + end + assert_equal(Digest::MD5.hexdigest(File.read(source)), state.checksum.sub(/^\{\w+\}/, ''), + "Did not catch later source") + end + + def test_insync + source = tempfile() + dest = tempfile() + + file = Puppet::Type.newfile :path => dest, :source => source, + :title => "copier" + + state = file.state(:source) + assert(state, "did not get source state") + + # Try it with no source at all + file.retrieve + assert(state.insync?, "source state not in sync with missing source") + + # with a directory + Dir.mkdir(source) + file.retrieve + assert(state.insync?, "source state not in sync with directory as source") + Dir.rmdir(source) + + # with a file + File.open(source, "w") { |f| f.puts "yay" } + file.retrieve + assert(!state.insync?, "source state was in sync when file was missing") + + # With a different file + File.open(dest, "w") { |f| f.puts "foo" } + file.retrieve + assert(!state.insync?, "source state was in sync with different file") + + # with matching files + File.open(dest, "w") { |f| f.puts "yay" } + file.retrieve + assert(state.insync?, "source state was not in sync with matching file") + end + + def test_source_sync + source = tempfile() + dest = tempfile() + + file = Puppet::Type.newfile :path => dest, :source => source, + :title => "copier" + state = file.state(:source) + + File.open(source, "w") { |f| f.puts "yay" } + + file.retrieve + assert(! state.insync?, "source thinks it's in sync") + + event = nil + assert_nothing_raised do + event = state.sync + end + assert_equal(:file_created, event) + assert_equal(File.read(source), File.read(dest), + "File was not copied correctly") + + # Now write something different + File.open(source, "w") { |f| f.puts "rah" } + file.retrieve + assert(! state.insync?, "source should be out of sync") + assert_nothing_raised do + event = state.sync + end + assert_equal(:file_changed, event) + 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 + + result = nil + assert_nothing_raised do + result = obj.sourcerecurse(true) + end + dfileobj = @file[destfile] + assert(dfileobj, "Did not create destfile object") + assert_equal([dfileobj], result) + + # Clean this up so it can be recreated + dfileobj.remove + + # Make sure we correctly iterate over the sources + nosource = tempfile() + obj[:source] = [nosource, source] + + result = nil + assert_nothing_raised do + result = obj.sourcerecurse(true) + end + dfileobj = @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_nothing_raised do + result = obj.sourcerecurse(true) + end + assert_equal([], result, "Sourcerecurse failed when all sources are missing") + end def test_simplelocalsource path = tempfile() - @@tmpfiles.push path FileUtils.mkdir_p path frompath = File.join(path,"source") topath = File.join(path,"dest") @@ -85,7 +330,19 @@ class TestFileSources < Test::Unit::TestCase from = File.open(frompath) { |o| o.read } to = File.open(topath) { |o| o.read } assert_equal(from,to) - @@tmpfiles.push path + 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) @@ -110,17 +367,16 @@ class TestFileSources < Test::Unit::TestCase def run_complex_sources(networked = false) path = tempfile() - @@tmpfiles.push path # first create the source directory FileUtils.mkdir_p path - # okay, let's create a directory structure fromdir = File.join(path,"fromdir") Dir.mkdir(fromdir) FileUtils.cd(fromdir) { - mkranddirsandfiles() + File.open("one", "w") { |f| f.puts "onefile"} + File.open("two", "w") { |f| f.puts "twofile"} } todir = File.join(path, "todir") @@ -130,28 +386,36 @@ class TestFileSources < Test::Unit::TestCase end recursive_source_test(source, todir) - return [fromdir,todir] + return [fromdir,todir, File.join(todir, "one"), File.join(todir, "two")] end def test_complex_sources_twice - fromdir, todir = run_complex_sources + fromdir, todir, one, two = run_complex_sources + assert_trees_equal(fromdir,todir) + recursive_source_test(fromdir, todir) assert_trees_equal(fromdir,todir) + # Now remove the whole tree and try it again. + [one, two].each do |f| File.unlink(f) end + Dir.rmdir(todir) recursive_source_test(fromdir, todir) assert_trees_equal(fromdir,todir) end def test_sources_with_deleted_destfiles - fromdir, todir = run_complex_sources - # then delete some files + fromdir, todir, one, two = run_complex_sources assert(FileTest.exists?(todir)) - missing_files = delete_random_files(todir) + + # We shouldn't have a 'two' file object in memory + assert_nil(@file[two], "object for 'two' is still in memory") + # then delete a file + File.unlink(two) + + puts "yay" # and run recursive_source_test(fromdir, todir) - missing_files.each { |file| - assert(FileTest.exists?(file), "Deleted file %s is still missing" % file) - } + assert(FileTest.exists?(two), "Deleted file was not recopied") # and make sure they're still equal assert_trees_equal(fromdir,todir) |
