diff --git a/test/lib/puppettest.rb b/test/lib/puppettest.rb index 6e3d8cd4c..e83cc9a04 100755 --- a/test/lib/puppettest.rb +++ b/test/lib/puppettest.rb @@ -1,241 +1,228 @@ require 'puppet' require 'test/unit' # Yay; hackish but it works if ARGV.include?("-d") Puppet.debug = true end module PuppetTest # Find the root of the Puppet tree; this is not the test directory, but # the parent of that dir. def basedir(*list) unless defined? @@basedir - case - when $0 =~ /rake_test_loader/ - @@basedir = File.dirname(Dir.getwd) - when ENV['BASEDIR'] - @@basedir = ENV['BASEDIR'] - else - dir = nil - app = $0.sub /^\.\//, "" - if app =~ /^#{File::SEPARATOR}.+\.rb/ - dir = app - else - dir = File.join(Dir.getwd, app) - end - 3.times { dir = File.dirname(dir) } - @@basedir = dir + Dir.chdir(File.dirname(__FILE__)) do + @@basedir = File.dirname(File.dirname(Dir.getwd)) end end if list.empty? @@basedir else File.join(@@basedir, *list) end end def cleanup(&block) @@cleaners << block end def datadir(*list) File.join(basedir, "test", "data", *list) end def exampledir(*args) unless defined? @@exampledir @@exampledir = File.join(basedir, "examples") end if args.empty? return @@exampledir else return File.join(@@exampledir, *args) end end module_function :basedir, :datadir, :exampledir # Rails clobbers RUBYLIB, thanks def libsetup curlibs = ENV["RUBYLIB"].split(":") $:.reject do |dir| dir =~ /^\/usr/ end.each do |dir| unless curlibs.include?(dir) curlibs << dir end end ENV["RUBYLIB"] = curlibs.join(":") end def rake? $0 =~ /rake_test_loader/ end def setup @memoryatstart = Puppet::Util.memory if defined? @@testcount @@testcount += 1 else @@testcount = 0 end @configpath = File.join(tmpdir, self.class.to_s + "configdir" + @@testcount.to_s + "/" ) unless defined? $user and $group $user = nonrootuser().uid.to_s $group = nonrootgroup().gid.to_s end Puppet.config.clear Puppet[:user] = $user Puppet[:group] = $group Puppet[:confdir] = @configpath Puppet[:vardir] = @configpath unless File.exists?(@configpath) Dir.mkdir(@configpath) end @@tmpfiles = [@configpath, tmpdir()] @@tmppids = [] @@cleaners = [] # If we're running under rake, then disable debugging and such. if rake? and ! Puppet[:debug] Puppet::Log.close Puppet::Log.newdestination tempfile() Puppet[:httplog] = tempfile() else if textmate? Puppet[:color] = false end Puppet::Log.newdestination :console Puppet::Log.level = :debug #$VERBOSE = 1 Puppet.info @method_name Puppet[:trace] = true end Puppet[:ignoreschedules] = true end def tempfile if defined? @@tmpfilenum @@tmpfilenum += 1 else @@tmpfilenum = 1 end f = File.join(self.tmpdir(), self.class.to_s + "_" + @method_name.to_s + @@tmpfilenum.to_s) @@tmpfiles << f return f end def textmate? if ENV["TM_FILENAME"] return true else return false end end def tstdir dir = tempfile() Dir.mkdir(dir) return dir end def tmpdir unless defined? @tmpdir and @tmpdir @tmpdir = case Facter["operatingsystem"].value when "Darwin": "/private/tmp" when "SunOS": "/var/tmp" else "/tmp" end @tmpdir = File.join(@tmpdir, "puppettesting") unless File.exists?(@tmpdir) FileUtils.mkdir_p(@tmpdir) File.chmod(01777, @tmpdir) end end @tmpdir end def teardown stopservices @@cleaners.each { |cleaner| cleaner.call() } @@tmpfiles.each { |file| unless file =~ /tmp/ puts "Not deleting tmpfile %s" % file next end if FileTest.exists?(file) system("chmod -R 755 %s" % file) system("rm -rf %s" % file) end } @@tmpfiles.clear @@tmppids.each { |pid| %x{kill -INT #{pid} 2>/dev/null} } @@tmppids.clear Puppet::Type.allclear Puppet::Storage.clear if defined? Puppet::Rails Puppet::Rails.clear end Puppet.clear @memoryatend = Puppet::Util.memory diff = @memoryatend - @memoryatstart if diff > 1000 Puppet.info "%s#%s memory growth (%s to %s): %s" % [self.class, @method_name, @memoryatstart, @memoryatend, diff] end # reset all of the logs Puppet::Log.close # Just in case there are processes waiting to die... require 'timeout' begin Timeout::timeout(5) do Process.waitall end rescue Timeout::Error # just move on end if File.stat("/dev/null").mode & 007777 != 0666 File.open("/tmp/nullfailure", "w") { |f| f.puts self.class } exit(74) end end end require 'puppettest/support' require 'puppettest/filetesting' require 'puppettest/fakes' require 'puppettest/exetest' require 'puppettest/parsertesting' require 'puppettest/servertest' # $Id$ diff --git a/test/providers/netinfo_mount.rb b/test/providers/mount/netinfo.rb similarity index 100% rename from test/providers/netinfo_mount.rb rename to test/providers/mount/netinfo.rb diff --git a/test/providers/parsedmount.rb b/test/providers/mount/parsed.rb similarity index 98% rename from test/providers/parsedmount.rb rename to test/providers/mount/parsed.rb index 868dde617..e09218e6c 100755 --- a/test/providers/parsedmount.rb +++ b/test/providers/mount/parsed.rb @@ -1,211 +1,211 @@ #!/usr/bin/env ruby -$:.unshift("../lib").unshift("../../lib") if __FILE__ =~ /\.rb$/ +$:.unshift("../../lib").unshift("../../../lib") if __FILE__ =~ /\.rb$/ require 'puppettest' require 'puppettest/fileparsing' require 'puppet' require 'facter' class TestParsedMounts < Test::Unit::TestCase include PuppetTest include PuppetTest::FileParsing def setup super @mount = Puppet.type(:mount) @provider = @mount.provider(:parsed) @oldfiletype = @provider.filetype end def teardown Puppet::FileType.filetype(:ram).clear @provider.filetype = @oldfiletype @provider.clear super end def mkmountargs mount = nil if defined? @pcount @pcount += 1 else @pcount = 1 end args = { :name => "/fspuppet%s" % @pcount, :device => "/dev/dsk%s" % @pcount, } @provider.fields(:parsed).each do |field| unless args.include? field args[field] = "fake%s%s" % [field, @pcount] end end return args end def mkmount hash = mkmountargs() #hash[:provider] = @provider.name fakemodel = fakemodel(:mount, hash[:name]) mount = @provider.new(fakemodel) assert(mount, "Could not create provider mount") hash[:record_type] = :parsed hash[:ensure] = :present mount.state_hash = hash return mount end # Here we just create a fake host type that answers to all of the methods # but does not modify our actual system. def mkfaketype @provider.filetype = Puppet::FileType.filetype(:ram) end def test_default_target should = case Facter.value(:operatingsystem) when "Solaris": "/etc/vfstab" else "/etc/fstab" end assert_equal(should, @provider.default_target) end def test_simplemount mkfaketype target = @provider.default_target # Make sure we start with an empty file assert_equal("", @provider.target_object(target).read, "Got a non-empty starting file") # Now create a provider mount = nil assert_nothing_raised { mount = mkmount } # Make sure we're still empty assert_equal("", @provider.target_object(target).read, "Got a non-empty starting file") # Try flushing it to disk assert_nothing_raised do mount.flush end # Make sure it's now in the file. The file format is validated in # the isomorphic methods. assert(@provider.target_object(target).read.include?("\t%s\t" % mount.state_hash[:name]), "Mount was not written to disk") end unless Facter["operatingsystem"].value == "Darwin" def test_mountsparse tab = fake_fstab fakedataparse(tab) do # Now just make we've got some mounts we know will be there hashes = @provider.target_records(tab).find_all { |i| i.is_a? Hash } assert(hashes.length > 0, "Did not create any hashes") root = hashes.find { |i| i[:name] == "/" } assert(root, "Could not retrieve root mount") end end def test_rootfs fs = nil type = @mount.create :name => "/" provider = type.provider assert(FileTest.exists?(@provider.default_target), "FSTab %s does not exist" % @provider.default_target) assert_nothing_raised do @provider.prefetch end assert_equal(:present, provider.state_hash[:ensure], "Could not find root fs") assert_nothing_raised { assert(provider.mounted?, "Root is considered not mounted") } end end if Puppet::SUIDManager.uid == 0 def test_mountfs fs = nil case Facter.value(:hostname) when "culain": fs = "/ubuntu" when "atalanta": fs = "/mnt" else $stderr.puts "No mount for mount testing; skipping" return end oldtext = @provider.target_object(@provider.default_target).read ftype = @provider.filetype mount = @mount.create :name => fs obj = mount.provider current = nil assert_nothing_raised { current = obj.mounted? } if current # Make sure the original gets remounted. cleanup do unless obj.mounted? obj.mount end end end unless current assert_nothing_raised { obj.mount } end assert_nothing_raised { obj.unmount } assert(! obj.mounted?, "FS still mounted") # Check the actual output of df assert(! obj.df(nil).include?(fs), "%s is still listed in df" % fs) assert_nothing_raised { obj.mount } assert(obj.mounted?, "FS not mounted") assert(obj.df(nil).include?(fs), "%s is not listed in df" % fs) end end def fake_fstab os = Facter['operatingsystem'] if os == "Solaris" name = "solaris.fstab" elsif os == "FreeBSD" name = "freebsd.fstab" else # Catchall for other fstabs name = "linux.fstab" end oldpath = @provider.default_target return fakefile(File::join("data/types/mount", name)) end end # $Id$