diff options
| author | Luke Kanies <luke@madstop.com> | 2007-11-08 16:30:51 -0600 |
|---|---|---|
| committer | Luke Kanies <luke@madstop.com> | 2007-11-08 16:30:51 -0600 |
| commit | 8f04446c9473cf80328dd9cbc9a1d15b6057409a (patch) | |
| tree | b3bbcc5745aca496a5c071310b00633a9d19d35d /test | |
| parent | dfe774f55e98db085d8f5729a4b1229513c6c2b0 (diff) | |
Fixing the 'mount' tests so that they no longer
modify the local system and they run fine as
non-root users.
Diffstat (limited to 'test')
| -rwxr-xr-x | test/lib/puppettest.rb | 8 | ||||
| -rw-r--r-- | test/lib/puppettest/fileparsing.rb | 22 | ||||
| -rw-r--r-- | test/lib/puppettest/support/utils.rb | 9 | ||||
| -rwxr-xr-x | test/ral/providers/mount/parsed.rb | 250 |
4 files changed, 19 insertions, 270 deletions
diff --git a/test/lib/puppettest.rb b/test/lib/puppettest.rb index eb50077b8..7bd2f107d 100755 --- a/test/lib/puppettest.rb +++ b/test/lib/puppettest.rb @@ -79,10 +79,6 @@ module PuppetTest end end - def cleanup(&block) - @@cleaners << block - end - def datadir(*list) File.join(basedir, "test", "data", *list) end @@ -101,6 +97,10 @@ module PuppetTest module_function :basedir, :datadir, :exampledir + def cleanup(&block) + @@cleaners << block + end + # Rails clobbers RUBYLIB, thanks def libsetup curlibs = ENV["RUBYLIB"].split(":") diff --git a/test/lib/puppettest/fileparsing.rb b/test/lib/puppettest/fileparsing.rb index 9f7716ab2..11de00220 100644 --- a/test/lib/puppettest/fileparsing.rb +++ b/test/lib/puppettest/fileparsing.rb @@ -2,20 +2,11 @@ module PuppetTest::FileParsing # Run an isomorphism test on our parsing process. def fakedataparse(*files) files.each do |file| - oldtarget = @provider.default_target - cleanup do - @provider.default_target = oldtarget - end - @provider.default_target = file + @provider.stubs(:default_target).returns(file) - assert_nothing_raised("failed to fetch %s" % file) { - @provider.prefetch - } + @provider.prefetch - text = nil - assert_nothing_raised("failed to generate %s" % file) do - text = @provider.to_file(@provider.target_records(file)) - end + text = @provider.to_file(@provider.target_records(file)) text.gsub!(/^# HEADER.+\n/, '') yield if block_given? @@ -23,8 +14,11 @@ module PuppetTest::FileParsing oldlines = File.readlines(file) newlines = text.chomp.split "\n" oldlines.zip(newlines).each do |old, new| - assert_equal(old.chomp.gsub(/\s+/, ''), new.gsub(/\s+/, ''), - "Lines are not equal in %s" % file) + if self.is_a?(Test::Unit::TestCase) + assert_equal(old.chomp.gsub(/\s+/, ''), new.gsub(/\s+/, ''), "File was not written back out correctly") + else + new.gsub(/\s+/, '').should == old.chomp.gsub(/\s+/, '') + end end end end diff --git a/test/lib/puppettest/support/utils.rb b/test/lib/puppettest/support/utils.rb index ee29a8875..d9bd6b2b6 100644 --- a/test/lib/puppettest/support/utils.rb +++ b/test/lib/puppettest/support/utils.rb @@ -1,6 +1,8 @@ require 'puppettest' -module PuppetTest +module PuppetTest::Support +end +module PuppetTest::Support::Utils def gcdebug(type) Puppet.warning "%s: %s" % [type, ObjectSpace.each_object(type) { |o| }] end @@ -112,7 +114,7 @@ module PuppetTest end def fakefile(name) - ary = [basedir, "test"] + ary = [PuppetTest.basedir, "test"] ary += name.split("/") file = File.join(ary) unless FileTest.exists?(file) @@ -171,3 +173,6 @@ module PuppetTest end end +module PuppetTest + include PuppetTest::Support::Utils +end diff --git a/test/ral/providers/mount/parsed.rb b/test/ral/providers/mount/parsed.rb deleted file mode 100755 index a7f1f5074..000000000 --- a/test/ral/providers/mount/parsed.rb +++ /dev/null @@ -1,250 +0,0 @@ -#!/usr/bin/env ruby - -require File.dirname(__FILE__) + '/../../../lib/puppettest' - -require 'mocha' -require 'puppettest' -require 'puppettest/fileparsing' -require 'facter' - -module MountTesting - include PuppetTest - include PuppetTest::FileParsing - - def setup - super - @mount = Puppet.type(:mount) - @provider = @mount.provider(:parsed) - - @oldfiletype = @provider.filetype - end - - def teardown - Puppet::Util::FileType.filetype(:ram).clear - @provider.filetype = @oldfiletype - @provider.clear - super - 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 - - 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 - - fakeresource = fakeresource(:mount, hash[:name]) - - mount = @provider.new(fakeresource) - assert(mount, "Could not create provider mount") - hash[:record_type] = :parsed - hash[:ensure] = :present - mount.property_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::Util::FileType.filetype(:ram) - end -end - -class TestParsedMounts < Test::Unit::TestCase - include MountTesting - - 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.property_hash[:name]), "Mount was not written to disk") - - # now make a change - assert_nothing_raised { mount.dump = 5 } - assert_nothing_raised { mount.flush } - - @provider.prefetch - assert_equal(5, mount.dump, "did not flush change to disk") - end - - # #730 - Make sure 'flush' is called when a mount is moving from absent to mounted - def test_flush_when_mounting_absent_fs - @provider.filetype = :ram - mount = mkmount - - mount.expects(:flush) - mount.expects(:mountcmd) # just so we don't actually try to mount anything - mount.mount - end -end - -class TestParsedMountsNonDarwin < PuppetTest::TestCase - confine "Mount type not tested on Darwin" => Facter["operatingsystem"].value != "Darwin" - include MountTesting - - 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") - - assert_nothing_raised("Could not rewrite file") do - @provider.to_file(hashes) - end - 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("/" => type) - end - - assert_equal(:present, type.provider.property_hash[:ensure], - "Could not find root fs with provider %s" % provider.class.name) - - assert_nothing_raised { - assert(provider.mounted?, "Root is considered not mounted") - } - end -end - -class TestParsedMountsNonDarwinAsRoot < PuppetTest::TestCase - confine "Mount type not tested on Darwin" => Facter["operatingsystem"].value != "Darwin" - confine "Not running as root" => Puppet.features.root? - - include MountTesting - - 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(obj.mounted?, "filesystem is not mounted") - - assert_nothing_raised { - obj.unmount - } - assert(! obj.mounted?, "FS still mounted") - # Check the actual output of mountcmd - assert(! obj.mountcmd().include?(fs), "%s is still listed in mountcmd" % fs) - assert_nothing_raised { - obj.mount - } - assert(obj.mounted?, "FS not mounted") - assert(obj.mountcmd().include?(fs), "%s is not listed in mountcmd" % fs) - - # Now try remounting - assert_nothing_raised("Could not remount filesystem") do - obj.remount - end - end -end - |
