diff --git a/test/Rakefile b/test/Rakefile index 4c7ca6ef3..d39da95aa 100644 --- a/test/Rakefile +++ b/test/Rakefile @@ -1,70 +1,82 @@ require 'rake/testtask' require 'find' include Find include FileTest $exclusions = %W(lib data) $test_library_paths = %W(lib ../lib) -$test_dirs = [] # this is used to generate all the targets. -$test_files = [] # this is ONLY used for the top-level test suite. - -# Collect all of our test directories, skipping specifically excluded dirs. -$test_dirs = Dir.glob("*").find_all { |f| directory?(f) }.reject { |d| - $exclusions.include?(File.basename(d)) -} - -# another find for the test files themselves: this could probably be rolled -# into the original find loop. - -$test_dirs.each do |dir| - find(dir) do |path| - if File.basename(path) =~ /^\.+/ - prune - elsif path != dir and ! directory?(path) - $test_files.push path + +filemap = Hash.new { |hash, key| hash[key] = [] } + +allfiles = [] + +# First collect the entire file list. +find(".") do |f| + # Get rid of the leading ./ + f = f.sub(/^\.\//, '') + + file = File.basename(f) + dir = File.dirname(f) + + # Prune . directories and excluded dirs + if (file =~ /^\./ and f != ".") or $exclusions.include?(File.basename(file)) + prune + next + end + next if f == "." + next if dir == "." + + # If we're a ruby script, then add it to the list of files for that dir + if file =~ /\.rb/ + allfiles << f + # Add it to all of the parent dirs, not just our own + parts = File.split(dir) + if parts[0] == "." + parts.shift end + parts.each_with_index { |part, i| + path = File.join(parts[0..i]) + filemap[path] << f + } end end desc "Run the full test suite" -Rake::TestTask.new 'test' do |t| +task :test do |t| t.libs << $test_library_paths - t.test_files = $test_files.sort + + # Add every file as a test file to run + t.test_files = allfiles t.verbose = true end -#task :test => $test_dirs task :default => :test -$test_dirs.sort.each do |path| - files = [] - - find(path) do |file| - if directory? file and path != file - prune - elsif file =~ /.rb$/ - files.push file - end - end +# Now create a task for every directory +filemap.each do |dir, files| + ns = dir.gsub "/", ":" - Rake::TestTask.new path.to_sym do |t| - t.libs << $test_library_paths - t.test_files = files.sort - t.verbose = true - end - -# task path.to_sym => files.collect { |x| path + ':' + File.basename(x, '.rb') } - - namespace path.to_sym do + # First create a separate task for each file in the namespace. + namespace ns do files.each do |file| Rake::TestTask.new File.basename(file, '.rb').to_sym do |t| t.libs << $test_library_paths + ['..'] t.test_files = [ file ] t.verbose = true end end end + + # Then create a task that matches the directory itself. + Rake::TestTask.new dir do |t| + t.libs << $test_library_paths + t.test_files = files.sort + t.verbose = true + end + + # And alias it with a slash on the end + Rake::TestTask.new(dir + "/" => dir) end # $Id$ diff --git a/test/client/master.rb b/test/client/master.rb index 9318fdcd6..987649a74 100755 --- a/test/client/master.rb +++ b/test/client/master.rb @@ -1,429 +1,432 @@ #!/usr/bin/env ruby $:.unshift("../lib").unshift("../../lib") if __FILE__ =~ /\.rb$/ require 'puppet' require 'puppet/client' require 'puppet/server' require 'puppettest' class TestMasterClient < Test::Unit::TestCase include PuppetTest::ServerTest class FakeTrans def initialize @counters = Hash.new { |h,k| h[k] = 0 } end [:evaluate, :report, :cleanup, :addtimes, :tags, :ignoreschedules].each do |m| define_method(m.to_s + "=") do |*args| @counters[m] += 1 end define_method(m) do |*args| @counters[m] += 1 end define_method(m.to_s + "?") do @counters[m] end end end class FakeComponent attr_accessor :trans def evaluate @trans = FakeTrans.new @trans end end def mkmaster(file = nil) master = nil file ||= mktestmanifest() # create our master assert_nothing_raised() { # this is the default server setup master = Puppet::Server::Master.new( :Manifest => file, :UseNodes => false, :Local => true ) } return master end def mkclient(master = nil) master ||= mkmaster() client = nil assert_nothing_raised() { client = Puppet::Client::MasterClient.new( :Master => master ) } return client end def mk_fake_client server = Puppet::Server::Master.new :Code => "" master = Puppet::Client::MasterClient.new :Server => server, :Local => true # Now create some objects objects = FakeComponent.new master.send(:instance_variable_set, "@objects", objects) class << master def report(r) @reported ||= 0 @reported += 1 end def reported @reported ||= 0 @reported end end return master, objects end def test_apply master, objects = mk_fake_client check = Proc.new do |hash| assert(objects.trans, "transaction was not created") trans = objects.trans hash[:yes].each do |m| assert_equal(1, trans.send(m.to_s + "?"), "did not call #{m} enough times") end hash[:no].each do |m| assert_equal(0, trans.send(m.to_s + "?"), "called #{m} too many times") end end # First try it with no arguments assert_nothing_raised do master.apply end check.call :yes => %w{evaluate cleanup addtimes}, :no => %w{report tags ignoreschedules} assert_equal(0, master.reported, "master sent report with reports disabled") # Now enable reporting and make sure the report method gets called Puppet[:report] = true assert_nothing_raised do master.apply end check.call :yes => %w{evaluate cleanup addtimes}, :no => %w{tags ignoreschedules} assert_equal(1, master.reported, "master did not send report") # Now try it with tags enabled assert_nothing_raised do master.apply("tags") end check.call :yes => %w{evaluate cleanup tags addtimes}, :no => %w{ignoreschedules} assert_equal(2, master.reported, "master did not send report") # and ignoreschedules assert_nothing_raised do master.apply("tags", true) end check.call :yes => %w{evaluate cleanup tags ignoreschedules addtimes}, :no => %w{} assert_equal(3, master.reported, "master did not send report") end def test_disable manifest = mktestmanifest master = mkmaster(manifest) client = mkclient(master) assert(! FileTest.exists?(@createdfile)) assert_nothing_raised { client.disable } assert_nothing_raised { client.run } assert(! FileTest.exists?(@createdfile), "Disabled client ran") assert_nothing_raised { client.enable } assert_nothing_raised { client.run } assert(FileTest.exists?(@createdfile), "Enabled client did not run") end # Make sure we're getting the client version in our list of facts def test_clientversionfact facts = nil assert_nothing_raised { facts = Puppet::Client::MasterClient.facts } assert_equal(Puppet.version.to_s, facts["clientversion"]) end # Make sure the client correctly locks itself def test_locking manifest = mktestmanifest master = nil # First test with a networked master client = Puppet::Client::MasterClient.new( :Server => "localhost" ) assert_nothing_raised do client.lock do pid = nil assert(client.locked?, "Client is not locked") assert(client.lockpid.is_a?(Integer), "PID #{client.lockpid} is, um, not a pid") end end assert(! client.locked?) # Now test with a local client client = mkclient assert_nothing_raised do client.lock do pid = nil assert(! client.locked?, "Local client is locked") end end assert(! client.locked?) end # Make sure non-string facts don't make things go kablooie def test_nonstring_facts # Add a nonstring fact Facter.add("nonstring") do setcode { 1 } end assert_equal(1, Facter.nonstring, "Fact was a string from facter") client = mkclient() assert(! FileTest.exists?(@createdfile)) assert_nothing_raised { client.run } end # This method is supposed def test_download source = tempfile() dest = tempfile() sfile = File.join(source, "file") dfile = File.join(dest, "file") Dir.mkdir(source) File.open(sfile, "w") {|f| f.puts "yay"} files = [] assert_nothing_raised do files = Puppet::Client::MasterClient.download(:dest => dest, :source => source, :name => "testing") end assert(FileTest.directory?(dest), "dest dir was not created") assert(FileTest.file?(dfile), "dest file was not created") assert_equal(File.read(sfile), File.read(dfile), "Dest file had incorrect contents") assert_equal([dest, dfile].sort, files.sort, "Changed files were not returned correctly") end def test_getplugins Puppet[:pluginsource] = tempfile() Dir.mkdir(Puppet[:pluginsource]) myplugin = File.join(Puppet[:pluginsource], "myplugin.rb") File.open(myplugin, "w") do |f| f.puts %{Puppet::Type.newtype(:myplugin) do newparam(:argument) do isnamevar end end } end assert_nothing_raised { Puppet::Client::MasterClient.getplugins } destfile = File.join(Puppet[:plugindest], "myplugin.rb") assert(File.exists?(destfile), "Did not get plugin") obj = Puppet::Type.type(:myplugin) assert(obj, "Did not define type") assert(obj.validattr?(:argument), "Did not get namevar") # Now modify the file and make sure the type is replaced File.open(myplugin, "w") do |f| f.puts %{Puppet::Type.newtype(:myplugin) do newparam(:yayness) do isnamevar end newparam(:rahness) do end end } end assert_nothing_raised { Puppet::Client::MasterClient.getplugins } destfile = File.join(Puppet[:pluginpath], "myplugin.rb") obj = Puppet::Type.type(:myplugin) assert(obj, "Did not define type") assert(obj.validattr?(:yayness), "Did not get namevar") assert(obj.validattr?(:rahness), "Did not get other var") assert(! obj.validattr?(:argument), "Old namevar is still valid") # Now try it again, to make sure we don't have any objects lying around assert_nothing_raised { Puppet::Client::MasterClient.getplugins } end def test_getfacts Puppet[:factsource] = tempfile() Dir.mkdir(Puppet[:factsource]) hostname = Facter.value(:hostname) myfact = File.join(Puppet[:factsource], "myfact.rb") File.open(myfact, "w") do |f| f.puts %{Facter.add("myfact") do setcode { "yayness" } end } end assert_nothing_raised { Puppet::Client::MasterClient.getfacts } destfile = File.join(Puppet[:factdest], "myfact.rb") assert(File.exists?(destfile), "Did not get fact") assert_equal(hostname, Facter.value(:hostname), "Lost value to hostname") assert_equal("yayness", Facter.value(:myfact), "Did not get correct fact value") # Now modify the file and make sure the type is replaced File.open(myfact, "w") do |f| f.puts %{Facter.add("myfact") do setcode { "funtest" } end } end assert_nothing_raised { Puppet::Client::MasterClient.getfacts } assert_equal("funtest", Facter.value(:myfact), "Did not reload fact") assert_equal(hostname, Facter.value(:hostname), "Lost value to hostname") # Now run it again and make sure the fact still loads assert_nothing_raised { Puppet::Client::MasterClient.getfacts } assert_equal("funtest", Facter.value(:myfact), "Did not reload fact") assert_equal(hostname, Facter.value(:hostname), "Lost value to hostname") end # Make sure we load all facts on startup. def test_loadfacts dirs = [tempfile(), tempfile()] count = 0 names = [] dirs.each do |dir| Dir.mkdir(dir) name = "fact%s" % count names << name file = File.join(dir, "%s.rb" % name) # Write out a plugin file File.open(file, "w") do |f| f.puts %{Facter.add("#{name}") do setcode { "#{name}" } end } end count += 1 end Puppet[:factpath] = dirs.join(":") names.each do |name| assert_nil(Facter.value(name), "Somehow retrieved invalid fact") end assert_nothing_raised { Puppet::Client::MasterClient.loadfacts } names.each do |name| assert_equal(name, Facter.value(name), "Did not retrieve facts") end end if Process.uid == 0 # Testing #283. Make sure plugins et al are downloaded as the running user. def test_download_ownership dir = tstdir() dest = tstdir() file = File.join(dir, "file") File.open(file, "w") { |f| f.puts "funtest" } user = nonrootuser() group = nonrootgroup() - FileUtils.chown_R(user.name, group.name, dir) + chowner = Puppet::Type.type(:file).create :path => dir, + :owner => user.name, :group => group.name, :recurse => true + assert_apply(chowner) + chowner.remove assert_equal(user.uid, File.stat(file).uid) assert_equal(group.gid, File.stat(file).gid) assert_nothing_raised { Puppet::Client::MasterClient.download(:dest => dest, :source => dir, :name => "testing" ) {} } destfile = File.join(dest, "file") assert(FileTest.exists?(destfile), "Did not create destfile") assert_equal(Process.uid, File.stat(destfile).uid) end end end # $Id$ diff --git a/test/types/host.rb b/test/types/host.rb index 280959c45..580ecbf3b 100755 --- a/test/types/host.rb +++ b/test/types/host.rb @@ -1,155 +1,155 @@ #!/usr/bin/env ruby $:.unshift("../lib").unshift("../../lib") if __FILE__ =~ /\.rb$/ require 'puppettest' require 'puppet' require 'test/unit' require 'facter' class TestHost < Test::Unit::TestCase include PuppetTest def setup super @hosttype = Puppet.type(:host) @provider = @hosttype.defaultprovider # Make sure they aren't using something funky like netinfo unless @provider.name == :parsed @hosttype.defaultprovider = @hosttype.provider(:parsed) end cleanup do @hosttype.defaultprovider = nil end if @provider.respond_to?(:default_target=) @default_file = @provider.default_target cleanup do @provider.default_target = @default_file end @target = tempfile() @provider.default_target = @target end end def mkhost if defined? @hcount @hcount += 1 else @hcount = 1 end host = nil assert_nothing_raised { host = Puppet.type(:host).create( :name => "fakehost%s" % @hcount, :ip => "192.168.27.%s" % @hcount, :alias => "alias%s" % @hcount ) } return host end def test_list assert_nothing_raised do @hosttype.defaultprovider.prefetch end count = 0 @hosttype.each do |h| count += 1 end assert_equal(0, count, "Found hosts in empty file somehow") end # Darwin will actually write to netinfo here. if Facter.value(:operatingsystem) != "Darwin" or Process.uid == 0 def test_simplehost host = nil assert_nothing_raised { host = Puppet.type(:host).create( :name => "culain", :ip => "192.168.0.3" ) } assert_events([:host_created], host) assert_nothing_raised { host.retrieve } assert_equal(:present, host.is(:ensure)) host[:ensure] = :absent - assert_events([:host_deleted], host) + assert_events([:host_removed], host) assert_nothing_raised { host.retrieve } assert_equal(:absent, host.is(:ensure)) end def test_moddinghost # We want to actually use the netinfo provider on darwin if Facter.value(:operatingsystem) == "Darwin" Puppet::Type.type(:host).defaultprovider = nil end host = mkhost() if Facter.value(:operatingsystem) == "Darwin" assert_equal(:netinfo, host[:provider], "Got incorrect provider") end cleanup do host[:ensure] = :absent assert_apply(host) end assert_events([:host_created], host) host.retrieve # This was a hard bug to track down. assert_instance_of(String, host.is(:ip)) host[:alias] = %w{madstop kirby yayness} assert_events([:host_changed], host) host.retrieve if Facter.value(:operatingsystem) == "Darwin" # Netinfo can't handle arrays right now assert_equal(%w{madstop}, host.is(:alias)) else assert_equal(%w{madstop kirby yayness}, host.is(:alias)) end host[:ensure] = :absent assert_events([:host_removed], host) end end def test_aliasisstate assert_equal(:state, @hosttype.attrtype(:alias)) end def test_multivalues host = mkhost assert_raise(Puppet::Error) { host[:alias] = "puppetmasterd yayness" } end def test_puppetalias host = mkhost() assert_nothing_raised { host[:alias] = "testing" } same = host.class["testing"] assert(same, "Could not retrieve by alias") end end # $Id$