diff options
author | luke <luke@980ebf18-57e1-0310-9a29-db15c13687c0> | 2006-11-12 04:12:48 +0000 |
---|---|---|
committer | luke <luke@980ebf18-57e1-0310-9a29-db15c13687c0> | 2006-11-12 04:12:48 +0000 |
commit | bd169b44a0ce58413fb5b031c3f12cd4ca6f4cbe (patch) | |
tree | 0411a38b49c5a311819c878c475672376c0f2578 /test | |
parent | 138150d1f240d3c249b2985540af1a7e327802e1 (diff) | |
download | puppet-bd169b44a0ce58413fb5b031c3f12cd4ca6f4cbe.tar.gz puppet-bd169b44a0ce58413fb5b031c3f12cd4ca6f4cbe.tar.xz puppet-bd169b44a0ce58413fb5b031c3f12cd4ca6f4cbe.zip |
Mounts work again, at least with the parsedfile provider. I still need to create a netinfo provider, but it should be short and easy. And, painfully, I still need to port the other six or so subclasses to this new provider.
git-svn-id: https://reductivelabs.com/svn/puppet/trunk@1859 980ebf18-57e1-0310-9a29-db15c13687c0
Diffstat (limited to 'test')
-rw-r--r-- | test/lib/puppettest/fileparsing.rb | 30 | ||||
-rwxr-xr-x | test/providers/parsedfile.rb | 201 | ||||
-rwxr-xr-x | test/providers/parsedmount.rb | 120 | ||||
-rwxr-xr-x | test/types/mount.rb | 90 | ||||
-rwxr-xr-x | test/util/fileparsing.rb | 60 |
5 files changed, 387 insertions, 114 deletions
diff --git a/test/lib/puppettest/fileparsing.rb b/test/lib/puppettest/fileparsing.rb index 004a0c3b5..9f8124df5 100644 --- a/test/lib/puppettest/fileparsing.rb +++ b/test/lib/puppettest/fileparsing.rb @@ -1,28 +1,26 @@ module PuppetTest::FileParsing # Run an isomorphism test on our parsing process. def fakedataparse(file) - @provider.path = file - instances = nil + oldtarget = @provider.default_target + cleanup do + @provider.default_target = oldtarget + end + @provider.default_target = file + assert_nothing_raised { - instances = @provider.retrieve + @provider.prefetch } - text = @provider.fileobj.read + text = @provider.to_file(@provider.target_records(file)) yield if block_given? - dest = tempfile() - @provider.path = dest - - # Now write it back out - assert_nothing_raised { - @provider.store(instances) - } - - newtext = @provider.fileobj.read - - # Don't worry about difference in whitespace - assert_equal(text.gsub(/\s+/, ' '), newtext.gsub(/\s+/, ' ')) + 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") + end end end diff --git a/test/providers/parsedfile.rb b/test/providers/parsedfile.rb index f7e5a4d48..e4c68dd0c 100755 --- a/test/providers/parsedfile.rb +++ b/test/providers/parsedfile.rb @@ -13,13 +13,14 @@ class TestParsedFile < Test::Unit::TestCase include PuppetTest::FileParsing Puppet::Type.newtype(:parsedfiletype) do + ensurable newstate(:one) do - newvalue(:a) {} - newvalue(:b) {} + newvalue(:a) + newvalue(:b) end newstate(:two) do - newvalue(:c) {} - newvalue(:d) {} + newvalue(:c) + newvalue(:d) end newparam(:name) do @@ -32,7 +33,9 @@ class TestParsedFile < Test::Unit::TestCase # A simple block to skip the complexity of a full transaction. def apply(model) - [:one, :two].each do |st| + [:one, :two, :ensure].each do |st| + Puppet.info "Setting %s: %s => %s" % + [model[:name], st, model.should(st)] model.provider.send(st.to_s + "=", model.should(st)) end end @@ -46,7 +49,8 @@ class TestParsedFile < Test::Unit::TestCase end def mkprovider(name = :parsed) - @provider = @type.provide(name, :parent => Puppet::Provider::ParsedFile) do + @provider = @type.provide(name, :parent => Puppet::Provider::ParsedFile, + :filetype => :ram) do record_line name, :fields => %w{name one two} end end @@ -90,7 +94,7 @@ class TestParsedFile < Test::Unit::TestCase end # The provider converts to strings - assert_equal("yayness", file.name) + assert_equal(:yayness, file.name) end def test_filetype @@ -121,8 +125,8 @@ class TestParsedFile < Test::Unit::TestCase obj = prov.target_object(path) end - # The default filetype is 'flat' - assert_instance_of(Puppet::FileType.filetype(:flat), obj) + # The default filetype is 'ram' + assert_instance_of(Puppet::FileType.filetype(:ram), obj) newobj = nil assert_nothing_raised do @@ -208,6 +212,24 @@ class TestParsedFile < Test::Unit::TestCase assert_equal("b", model.provider.one) assert_equal("b", default.provider.one) assert_equal("d", default.provider.two) + + # Now list all of them and make sure we get everything back + hashes = nil + assert_nothing_raised do + hashes = prov.list + end + + names = nil + assert_nothing_raised do + names = prov.list_by_name + end + + %w{bill jill will}.each do |name| + assert(hashes.find { |r| r[:name] == name}, + "Did not return %s in list" % name) + assert(names.include?(name), + "Did not return %s in list_by_name" % name) + end end # Make sure we can correctly prefetch on a target. @@ -316,8 +338,8 @@ class TestParsedFile < Test::Unit::TestCase assert_nothing_raised { one.flush } # Make sure it changed our file - assert_equal("a", one.provider.one) - assert_equal("c", one.provider.two) + assert_equal(:a, one.provider.one) + assert_equal(:c, one.provider.two) # And make sure it's right on disk assert(prov.target_object(:yayness).read.include?("one a c"), @@ -340,17 +362,19 @@ class TestParsedFile < Test::Unit::TestCase assert_nothing_raised { apply(two) } assert_nothing_raised { two.flush } - assert_equal("b", two.provider.one) + assert_equal(:b, two.provider.one) end def test_creating_file prov = mkprovider + prov.clear - prov.filetype = :ram prov.default_target = :basic model = mkmodel "yay", :target => :basic, :one => "a", :two => "c" + assert_equal(:present, model.should(:ensure)) + apply(model) assert_nothing_raised do @@ -385,16 +409,17 @@ class TestParsedFile < Test::Unit::TestCase second = mkmodel "second", :target => :second mover = mkmodel "mover", :target => :first - # Apply them all [first, second, mover].each do |m| assert_nothing_raised("Could not apply %s" % m[:name]) do apply(m) end end - # Flush - assert_nothing_raised do - [first, second, mover].each do |m| m.flush end + # Flush. + [first, second, mover].each do |m| + assert_nothing_raised do + m.flush + end end check = proc do |target, name| @@ -428,6 +453,148 @@ class TestParsedFile < Test::Unit::TestCase assert(prov.target_object(:first) !~ /mover/, "Mover was not removed from first file") end + + # Make sure that 'ensure' correctly calls 'sync' on all states. + def test_ensure + prov = mkprovider + + prov.filetype = :ram + prov.default_target = :first + + # Make two models, one that starts on disk and one that doesn't + ondisk = mkmodel "ondisk", :target => :first + notdisk = mkmodel "notdisk", :target => :first + + prov.target_object(:first).write "ondisk a c\n" + prov.prefetch + + assert_equal(:present, notdisk.should(:ensure), + "Did not get default ensure value") + + # Try creating the object + assert_nothing_raised { notdisk.provider.create() } + + # Now make sure all of the data is copied over correctly. + notdisk.class.validstates.each do |state| + assert_equal(notdisk.should(state), notdisk.provider.state_hash[state], + "%s was not copied over during creation" % state) + end + + # Flush it to disk and make sure it got copied down + assert_nothing_raised do + notdisk.flush + end + + assert(prov.target_object(:first).read =~ /^notdisk/, + "Did not write out object to disk") + assert(prov.target_object(:first).read =~ /^ondisk/, + "Lost object on disk") + + # Make sure our on-disk model behaves appropriately. + assert_equal(:present, ondisk.provider.ensure) + + # Now destroy the object + assert_nothing_raised { notdisk.provider.destroy() } + + assert_nothing_raised { notdisk.flush } + + # And make sure it's no longer present + assert(prov.target_object(:first).read !~ /^notdisk/, + "Did not remove thing from disk") + assert(prov.target_object(:first).read =~ /^ondisk/, + "Lost object on disk") + assert_equal(:present, ondisk.provider.ensure) + end + + def test_absent_fields + prov = @type.provide(:record, :parent => Puppet::Provider::ParsedFile) do + record_line :record, :fields => %w{name one two}, + :separator => "\s" + end + cleanup { @type.unprovide(:record) } + + records = prov.parse("a d") + + line = records.find { |r| r[:name] == "a" } + assert(line, "Could not find line") + + assert_equal(:absent, line[:one], "field one was not set to absent") + end + + # This test is because in x2puppet I was having problems where multiple + # retrievals somehow destroyed the 'is' values. + def test_value_retrieval + prov = mkprovider + prov.default_target = :yayness + + prov.target_object(:yayness).write "bill a c\njill b d" + + list = Puppet::Type.type(:parsedfiletype).list + + bill = list.find { |r| r[:name] == "bill" } + jill = list.find { |r| r[:name] == "jill" } + assert(bill, "Could not find bill") + assert(jill, "Could not find jill") + + prov = bill.provider + + 4.times do |i| + assert(prov.one, "Did not get a value for 'one' on try %s" % (i + 1)) + end + + # First make sure we can retrieve values multiple times from the + # provider + assert(bill.is(:one), "Bill does not have a value for 'one'") + assert(bill.is(:one), "Bill does not have a value for 'one' on second try") + assert_nothing_raised do + bill.retrieve + end + assert(bill.is(:one), "bill's value for 'one' disappeared") + end + + # Make sure that creating a new model finds existing records in memory + def test_initialize_finds_records + prov = mkprovider + prov.default_target = :yayness + + prov.target_object(:yayness).write "bill a c\njill b d" + + prov.prefetch + + # Now make a model + bill = nil + assert_nothing_raised do + bill = Puppet::Type.type(:parsedfiletype).create :name => "bill" + end + + assert_equal("a", bill.provider.one, + "Record was not found in memory") + end + + # Make sure invalid fields always show up as insync + def test_invalid_fields + prov = @type.provide(:test, :parent => Puppet::Provider::ParsedFile, + :filetype => :ram, :default_target => :yayness) do + record_line :test, :fields => %w{name two} + end + cleanup do @type.unprovide(:test) end + + bill = nil + assert_nothing_raised do + bill = Puppet::Type.type(:parsedfiletype).create :name => "bill", + :one => "a", :two => "c" + end + + assert_apply(bill) + + prov.prefetch + assert_nothing_raised do + bill.retrieve + end + + assert(bill.insync?, + "An invalid field marked the record out of sync") + end end # $Id$ diff --git a/test/providers/parsedmount.rb b/test/providers/parsedmount.rb index 0d146df34..868dde617 100755 --- a/test/providers/parsedmount.rb +++ b/test/providers/parsedmount.rb @@ -13,7 +13,8 @@ class TestParsedMounts < Test::Unit::TestCase def setup super - @provider = Puppet.type(:mount).provider(:parsed) + @mount = Puppet.type(:mount) + @provider = @mount.provider(:parsed) @oldfiletype = @provider.filetype end @@ -21,6 +22,7 @@ class TestParsedMounts < Test::Unit::TestCase def teardown Puppet::FileType.filetype(:ram).clear @provider.filetype = @oldfiletype + @provider.clear super end @@ -33,13 +35,13 @@ class TestParsedMounts < Test::Unit::TestCase @pcount = 1 end args = { - :path => "/fspuppet%s" % @pcount, + :name => "/fspuppet%s" % @pcount, :device => "/dev/dsk%s" % @pcount, } - @provider.fields.each do |field| + @provider.fields(:parsed).each do |field| unless args.include? field - args[field] = "fake%s" % @pcount + args[field] = "fake%s%s" % [field, @pcount] end end @@ -50,15 +52,13 @@ class TestParsedMounts < Test::Unit::TestCase hash = mkmountargs() #hash[:provider] = @provider.name - fakemodel = fakemodel(:mount, hash[:path]) + fakemodel = fakemodel(:mount, hash[:name]) mount = @provider.new(fakemodel) - #mount = Puppet.type(:mount).create(hash) - - hash.each do |name, val| - fakemodel[name] = val - end assert(mount, "Could not create provider mount") + hash[:record_type] = :parsed + hash[:ensure] = :present + mount.state_hash = hash return mount end @@ -69,11 +69,22 @@ class TestParsedMounts < Test::Unit::TestCase @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 - assert_nothing_raised { - assert_equal([], @provider.retrieve) - } + 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 @@ -82,50 +93,49 @@ class TestParsedMounts < Test::Unit::TestCase } # Make sure we're still empty - assert_nothing_raised { - assert_equal([], @provider.retrieve) - } + assert_equal("", @provider.target_object(target).read, + "Got a non-empty starting file") - hash = mount.model.to_hash - - # Try storing it + # Try flushing it to disk assert_nothing_raised do - mount.store(hash) + mount.flush end - # Make sure we get the mount back - assert_nothing_raised { - assert_equal([hash], @provider.retrieve) - } - - # Now remove the whole object - assert_nothing_raised { - mount.store({}) - assert_equal([], @provider.retrieve) - } + # 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 - fakedataparse(fake_fstab) do + tab = fake_fstab + fakedataparse(tab) do # Now just make we've got some mounts we know will be there - hashes = @provider.retrieve.find_all { |i| i.is_a? Hash } + 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[:path] == "/" } + root = hashes.find { |i| i[:name] == "/" } assert(root, "Could not retrieve root mount") end end def test_rootfs fs = nil - @provider.path = fake_fstab() - fakemodel = fakemodel(:mount, "/") - mount = @provider.new(fakemodel) - mount.model[:path] = "/" - assert(mount.hash, "Could not retrieve root fs") + 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(mount.mounted?, "Root is considered not mounted") + assert(provider.mounted?, "Root is considered not mounted") } end end @@ -133,44 +143,28 @@ class TestParsedMounts < Test::Unit::TestCase if Puppet::SUIDManager.uid == 0 def test_mountfs fs = nil - case Facter["hostname"].value + case Facter.value(:hostname) when "culain": fs = "/ubuntu" when "atalanta": fs = "/mnt" - when "figurehead": fs = "/cg4/net/depts" else $stderr.puts "No mount for mount testing; skipping" return end - oldtext = @provider.fileobj.read + oldtext = @provider.target_object(@provider.default_target).read ftype = @provider.filetype - # Make sure the original gets reinstalled. - if ftype == Puppet::FileType.filetype(:netinfo) - cleanup do - IO.popen("niload -r /mounts .", "w") do |file| - file.puts oldtext - end - end - else - cleanup do - @provider.fileobj.write(oldtext) - end - end - - fakemodel = fakemodel(:mount, "/") - obj = @provider.new(fakemodel) - obj.model[:path] = fs + mount = @mount.create :name => fs + obj = mount.provider current = nil - assert_nothing_raised { current = obj.mounted? } if current - # Make sure the original gets reinstalled. + # Make sure the original gets remounted. cleanup do unless obj.mounted? obj.mount @@ -188,10 +182,13 @@ class TestParsedMounts < Test::Unit::TestCase 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 @@ -206,8 +203,7 @@ class TestParsedMounts < Test::Unit::TestCase # Catchall for other fstabs name = "linux.fstab" end - oldpath = @provider.path - cleanup do @provider.path = oldpath end + oldpath = @provider.default_target return fakefile(File::join("data/types/mount", name)) end end diff --git a/test/types/mount.rb b/test/types/mount.rb index 880de4434..5a5a5da85 100755 --- a/test/types/mount.rb +++ b/test/types/mount.rb @@ -14,11 +14,20 @@ class TestMounts < Test::Unit::TestCase attr_accessor :mounted + def self.default_target + :yayness + end + def create @ensure = :present + @model.class.validstates.each do |state| + if value = @model.should(state) + self.send(state.to_s + "=", value) + end + end end - def delete + def destroy @ensure = :absent @mounted = false end @@ -50,12 +59,14 @@ class TestMounts < Test::Unit::TestCase def setup super - @realprovider = Puppet::Type.type(:mount).defaultprovider - Puppet::Type.type(:mount).defaultprovider = FakeMountProvider + @mount = Puppet::Type.type(:mount) + @realprovider = @mount.defaultprovider + @mount.defaultprovider = FakeMountProvider end def teardown Puppet.type(:mount).clear + @realprovider.clear Puppet::Type.type(:mount).defaultprovider = nil super end @@ -73,7 +84,10 @@ class TestMounts < Test::Unit::TestCase :device => "/dev/dsk%s" % @pcount, } - @realprovider.fields.each do |field| + [@mount.validstates, @mount.parameters].flatten.each do |field| + next if field == :provider + next if field == :target + next if field == :ensure unless args.include? field args[field] = "fake%s" % @pcount end @@ -87,24 +101,13 @@ class TestMounts < Test::Unit::TestCase end def test_simplemount - mount = nil - oldprv = Puppet.type(:mount).defaultprovider - Puppet.type(:mount).defaultprovider = nil - assert_nothing_raised { - Puppet.type(:mount).defaultprovider.retrieve - - count = 0 - Puppet.type(:mount).each do |h| - count += 1 - end - - assert_equal(0, count, "Found mounts in empty file somehow") - } - Puppet.type(:mount).defaultprovider = oldprv - mount = mkmount assert_apply(mount) + mount.send(:states).each do |state| + assert_equal(state.should, mount.provider.send(state.name), + "%s was not set to %s" % [state.name, state.should]) + end assert_events([], mount) assert_nothing_raised { mount.retrieve } @@ -155,11 +158,60 @@ class TestMounts < Test::Unit::TestCase def test_list list = nil + assert(@mount.respond_to?(:list), + "No list method defined for mount") + assert_nothing_raised do list = Puppet::Type.type(:mount).list end assert(list.length > 0, "Did not return any mounts") + + root = list.find { |o| o[:name] == "/" } + assert(root, "Could not find root root filesystem in list results") + + assert(root.is(:device), "Device was not set") + assert(root.state(:device).value, "Device was not returned by value method") + + assert_nothing_raised do + root.retrieve + end + + assert(root.is(:device), "Device was not set") + assert(root.state(:device).value, "Device was not returned by value method") + end + + # Make sure we actually remove the object from the file and such. + def test_removal + # Reset the provider so that we're using the real thing + @mount.defaultprovider = nil + + provider = @mount.defaultprovider + assert(provider, "Could not retrieve default provider") + + file = provider.default_target + assert(FileTest.exists?(file), + "FSTab %s does not exist" % file) + + # Now switch to ram, so we're just doing this there, not really on disk. + provider.filetype = :ram + #provider.target_object(file).write text + + mount = mkmount + + mount[:ensure] = :present + + assert_events([:mount_created], mount) + assert_events([], mount) + + mount[:ensure] = :absent + assert_events([:mount_deleted], mount) + assert_events([], mount) + + # Now try listing and making sure the object is actually gone. + list = mount.provider.class.list + assert(! list.find { |r| r[:name] == mount[:name] }, + "Mount was not actually removed") end end diff --git a/test/util/fileparsing.rb b/test/util/fileparsing.rb index f5cf0d255..cebc3051e 100755 --- a/test/util/fileparsing.rb +++ b/test/util/fileparsing.rb @@ -273,6 +273,51 @@ class TestUtilFileParsing < Test::Unit::TestCase end end + # Make sure fields that are marked absent get replaced with the appropriate + # string. + def test_absent_fields + parser = FParser.new + + options = nil + assert_nothing_raised do + options = parser.record_line :record, :fields => %w{one two three} + end + assert_equal("", options[:absent], "Did not set a default absent string") + + result = nil + assert_nothing_raised do + result = parser.to_line(:record_type => :record, + :one => "a", :two => :absent, :three => "b") + end + + assert_equal("a b", result, "Absent was not correctly replaced") + + # Now try using a different replacement character + options[:absent] = "*" # Because cron is a pain in my ass + assert_nothing_raised do + result = parser.to_line(:record_type => :record, + :one => "a", :two => :absent, :three => "b") + end + + assert_equal("a * b", result, "Absent was not correctly replaced") + + # Make sure we deal correctly with the string 'absent' + assert_nothing_raised do + result = parser.to_line(:record_type => :record, + :one => "a", :two => "b", :three => 'absent') + end + + assert_equal("a b absent", result, "Replaced string 'absent'") + + # And, of course, make sure we can swap things around. + assert_nothing_raised do + result = parser.to_line(:record_type => :record, + :one => "a", :two => "b", :three => :absent) + end + + assert_equal("a b *", result, "Absent was not correctly replaced") + end + # Make sure we can specify a different join character than split character def test_split_join_record_line parser = FParser.new @@ -331,6 +376,21 @@ billy three four" end end + def test_valid_attrs + parser = FParser.new + + parser.record_line :record, :fields => %w{one two three} + + assert(parser.valid_attr?(:record, :one), + "one was considered invalid") + + assert(parser.valid_attr?(:record, :ensure), + "ensure was considered invalid") + + assert(! parser.valid_attr?(:record, :four), + "four was considered valid") + end + # Make sure we correctly handle optional fields. We'll skip this # functionality until we really know we need it. def disabled_test_optional_fields |