diff options
-rw-r--r-- | lib/puppet/provider.rb | 20 | ||||
-rwxr-xr-x | spec/unit/ral/provider/mount/parsed.rb | 182 | ||||
-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 |
6 files changed, 211 insertions, 280 deletions
diff --git a/lib/puppet/provider.rb b/lib/puppet/provider.rb index 53a2c5605..26c1254e3 100644 --- a/lib/puppet/provider.rb +++ b/lib/puppet/provider.rb @@ -284,16 +284,16 @@ class Puppet::Provider end end - def self.to_s - unless defined? @str - if self.resource_type - @str = "%s provider %s" % [resource_type.name, self.name] - else - @str = "unattached provider %s" % [self.name] - end - end - @str - end +# def self.to_s +# unless defined? @str +# if self.resource_type +# @str = "%s provider %s" % [resource_type.name, self.name] +# else +# @str = "unattached provider %s" % [self.name] +# end +# end +# @str +# end dochook(:defaults) do if @defaults.length > 0 diff --git a/spec/unit/ral/provider/mount/parsed.rb b/spec/unit/ral/provider/mount/parsed.rb new file mode 100755 index 000000000..89928891a --- /dev/null +++ b/spec/unit/ral/provider/mount/parsed.rb @@ -0,0 +1,182 @@ +#!/usr/bin/env ruby +# +# Created by Luke Kanies on 2007-9-12. +# Copyright (c) 2006. All rights reserved. + +require File.dirname(__FILE__) + '/../../../../spec_helper' + +require 'puppettest/support/utils' +require 'puppettest/fileparsing' + +module ParsedMountTesting + include PuppetTest::Support::Utils + include PuppetTest::FileParsing + + def setup + @mount_class = Puppet.type(:mount) + @provider_class = @mount_class.provider(:parsed) + 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_class.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_class.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_class.name + + fakeresource = stub :type => :mount, :name => hash[:name] + fakeresource.stubs(:[]).with(:name).returns(hash[:name]) + fakeresource.stubs(:should).with(:target).returns(nil) + + mount = @provider_class.new(fakeresource) + 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.stubs(:filetype).returns(Puppet::Util::FileType.filetype(:ram)) + end +end + +provider_class = Puppet::Type.type(:mount).provider(:parsed) + +describe provider_class do + include ParsedMountTesting + + it "should be able to parse all of the example mount tabs" do + tab = fake_fstab + @provider = @provider_class + + # LAK:FIXME Again, a relatively bad test, but I don't know how to rspec-ify this. + # I suppose this is more of an integration test? I dunno. + fakedataparse(tab) do + # Now just make we've got some mounts we know will be there + hashes = @provider_class.target_records(tab).find_all { |i| i.is_a? Hash } + (hashes.length > 0).should be_true + root = hashes.find { |i| i[:name] == "/" } + + proc { @provider_class.to_file(hashes) }.should_not raise_error + end + end + + # LAK:FIXME I can't mock Facter because this test happens at parse-time. + it "should default to /etc/vfstab on Solaris and /etc/fstab everywhere else" do + should = case Facter.value(:operatingsystem) + when "Solaris": "/etc/vfstab" + else + "/etc/fstab" + end + Puppet::Type.type(:mount).provider(:parsed).default_target.should == should + end +end + +describe provider_class, " when mounting an absent filesystem" do + include ParsedMountTesting + + # #730 - Make sure 'flush' is called when a mount is moving from absent to mounted + it "should flush the fstab to disk" do + mount = mkmount + + # Mark the mount as absent + mount.property_hash[:ensure] = :absent + + mount.stubs(:mountcmd) # just so we don't actually try to mount anything + + mount.expects(:flush) + mount.mount + end +end + +describe provider_class, " when modifying the filesystem tab" do + include ParsedMountTesting + before do + @mount = mkmount + @target = @provider_class.default_target + + # Never write to disk, only to RAM. + @provider_class.stubs(:filetype).returns(Puppet::Util::FileType.filetype(:ram)) + end + + it "should write the mount to disk when :flush is called" do + @mount.flush + + text = @provider_class.target_object(@provider_class.default_target).read + text.should == @mount.class.to_line(@mount.property_hash) + "\n" + end +end + +describe provider_class, " when parsing information about the root filesystem" do + confine "Mount type not tested on Darwin" => Facter["operatingsystem"].value != "Darwin" + include ParsedMountTesting + + before do + @mount = @mount_class.create :name => "/" + @provider = @mount.provider + end + + it "should have a filesystem tab" do + FileTest.should be_exist(@provider_class.default_target) + end + + it "should find the root filesystem" do + @provider_class.prefetch("/" => @mount) + @mount.provider.property_hash[:ensure].should == :present + end + + it "should determine that the root fs is mounted" do + @provider_class.prefetch("/" => @mount) + @mount.provider.should be_mounted + end + + after do + Puppet::Type.allclear + end +end + +describe provider_class, " when mounting and unmounting" do + include ParsedMountTesting + + it "should call the 'mount' command to mount the filesystem" + + it "should call the 'unmount' command to unmount the filesystem" + + it "should specify the filesystem when remounting a filesystem" +end 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 - |