diff options
| author | luke <luke@980ebf18-57e1-0310-9a29-db15c13687c0> | 2006-11-13 02:07:07 +0000 |
|---|---|---|
| committer | luke <luke@980ebf18-57e1-0310-9a29-db15c13687c0> | 2006-11-13 02:07:07 +0000 |
| commit | bb80c1bb2c977fe462fa9d74031929547c2bbc40 (patch) | |
| tree | 5774dab49b96db6cfb59c7316563a527612f495d /test/providers | |
| parent | 25b575fcdd588a619a89ac3fce474f4f35de3fb1 (diff) | |
Ports are still broken, but I need to work on something else while I am thinking about how to fix them. Stupid /etc/services.
git-svn-id: https://reductivelabs.com/svn/puppet/trunk@1863 980ebf18-57e1-0310-9a29-db15c13687c0
Diffstat (limited to 'test/providers')
| -rwxr-xr-x | test/providers/parsedport.rb | 213 |
1 files changed, 195 insertions, 18 deletions
diff --git a/test/providers/parsedport.rb b/test/providers/parsedport.rb index ff3977dd5..4b29186ec 100755 --- a/test/providers/parsedport.rb +++ b/test/providers/parsedport.rb @@ -3,6 +3,7 @@ $:.unshift("../lib").unshift("../../lib") if __FILE__ =~ /\.rb$/ require 'puppettest' +require 'puppettest/fileparsing' require 'puppet' require 'puppet/type/port' require 'test/unit' @@ -10,46 +11,222 @@ require 'facter' class TestParsedPort < Test::Unit::TestCase include PuppetTest + include PuppetTest::FileParsing def setup super @provider = Puppet.type(:port).provider(:parsed) - @oldfiletype = @provider.filetype end def teardown Puppet::FileType.filetype(:ram).clear @provider.filetype = @oldfiletype + @provider.clear super end + + # Generate a line from a hash. The line might include '\n'. + def genline(hash) + line = [hash[:name], "#{hash[:number]}/%s"].join("\t\t") + if hash[:alias] + line += "\t\t" + hash[:alias].join(" ") + end + if hash[:description] + line += "\t# " + hash[:description] + end + + return hash[:protocols].collect { |p| line % p }.join("\n") + end # Parse our sample data and make sure we regenerate it correctly. def test_portsparse - fakedata("data/types/ports").each do |file| - @provider.path = file - instances = nil - assert_nothing_raised { - instances = @provider.retrieve + files = fakedata("data/types/port") + files.each do |file| + oldtarget = @provider.default_target + cleanup do + @provider.default_target = oldtarget + end + @provider.default_target = file + + assert_nothing_raised("failed to fetch %s" % file) { + @provider.prefetch } - text = @provider.fileobj.read.gsub(/\s+/, ' ') - text.gsub!(/ #.+$/, '') + hashes = @provider.target_records(file).find_all { |i| i.is_a? Hash } + assert(hashes.length > 0, "Did not create any hashes") + dns = hashes.find { |i| i[:name] == "domain" } - dest = tempfile() - @provider.path = dest + assert(dns, "Did not retrieve dns record") + assert_equal("53", dns[:number], "dns number is wrong") - # Now write it back out - assert_nothing_raised { - @provider.store(instances) - } + text = nil + assert_nothing_raised("failed to generate %s" % file) do + text = @provider.to_file(@provider.target_records(file)) + end + + oldlines = File.readlines(file) + newlines = text.chomp.split "\n" + regex = /^(\S+)\s+(\d+)\/(\w+)/ + oldlines.zip(newlines).each do |old, new| + if omatch = regex.match(old) + assert(newmatch = regex.match(new), + "Lines were not equivalent: %s vs %s" % + [old.inspect, new.inspect] + ) + oldfields = omatch.captures and + newfields = newmatch.captures + + assert_equal(oldfields, newfields, + "Lines were not equivalent: %s vs %s" % + [old.inspect, new.inspect] + ) + end + # assert_equal(old.chomp.gsub(/\s+/, ''), + # new.gsub(/\s+/, ''), + # "Lines are not equal in %s" % file) + end + end + end + + # Try parsing the different forms of lines + def test_parsing + # Each of the different possible values for each field. + options = { + :name => "service", + :number => "1", + :alias => [nil, ["null"], %w{null sink}, %w{null sink other}], + :description => [nil, "my description"], + :protocols => [%w{tcp}, %w{udp}, %w{tcp udp}] + } + + # Now go through all of the different iterations and make sure we + # parse them correctly. + keys = options.keys + + name = options[:name] + number = options[:number] + options[:alias].each do |al| + options[:description].each do |desc| + options[:protocols].each do |proto| + hash = {:name => name, :number => number, :alias => al, + :description => desc, :protocols => proto} + line = genline(hash) + + # Try parsing it + record = nil + assert_nothing_raised do + record = @provider.parse_line(line) + end + assert(record, "Did not get record returned") + hash.each do |param, value| + if value + assert_equal(value, record[param], + "did not get %s out of '%s'" % [param, line]) + end + end + + # Now make sure it generates correctly + assert_equal(line, @provider.to_line(record), + "Did not generate %s correctly" % line) + end + end + end + end + + # Make sure we correctly join lines by name, so that they're considered + # a single record. + def test_lines + result = nil + assert_nothing_raised do + result = @provider.lines( +"smtp 25/tcp mail +time 37/tcp timserver +time 37/udp timserver +rlp 39/udp resource # resource location +tacacs 49/tcp # Login Host Protocol (TACACS) +nameserver 42/tcp name # IEN 116 +whois 43/tcp nicname +tacacs 49/udp +re-mail-ck 50/tcp # Remote Mail Checking Protocol +domain 53/tcp nameserver # name-domain server +re-mail-ck 50/udp +domain 53/udp nameserver" + ) + end + + assert_equal([ +"smtp 25/tcp mail", +"time 37/tcp timserver +time 37/udp timserver", +"rlp 39/udp resource # resource location", +"tacacs 49/tcp # Login Host Protocol (TACACS) +tacacs 49/udp", +"nameserver 42/tcp name # IEN 116", +"whois 43/tcp nicname", +"re-mail-ck 50/tcp # Remote Mail Checking Protocol +re-mail-ck 50/udp", +"domain 53/tcp nameserver # name-domain server +domain 53/udp nameserver" +], result) + + end + + # Make sure we correctly handle port merging. + def test_port_merge + fields = [:name, :number, :protocols, :alias, :description] + base = %w{a 1} + + z = proc { |ary| h = {}; fields.zip(ary) { |p,v| h[p] = v if v }; h } + + # Make sure our zipper is working + assert_equal({:name => "a", :number => "1", :protocols => %w{tcp udp}}, + z.call(["a", "1", %w{tcp udp}]) + ) + + # Here we go through the different options, just testing each key + # separately. + { + # The degenerate case - just two protocols + [%w{tcp udp}] => [[%w{tcp}], [%w{udp}]], + + # one alias + [%w{tcp udp}, %w{A}] => [[%w{tcp}, %w{A}], [%w{udp}]], + + # Other side + [%w{tcp udp}, %w{A}] => [[%w{tcp}], [%w{udp}], %w{A}], + + # Both + [%w{tcp udp}, %w{A}] => [[%w{tcp}, %w{A}], [%w{udp}], %w{A}], + + # Adding aliases + [%w{tcp udp}, %w{A B}] => [[%w{tcp}, %w{A}], [%w{udp}], %w{B}], + + # Merging aliases + [%w{tcp udp}, %w{A B}] => [[%w{tcp}, %w{A B}], [%w{udp}], %w{B}], - newtext = @provider.fileobj.read.gsub(/\s+/, ' ') + # One description + [%w{tcp udp}, nil, "desc"] => [[%w{tcp}, nil, "desc"], [%w{udp}] ], - newtext.gsub!(/ #.+$/, '') + # other side + [%w{tcp udp}, nil, "desc"] => [[%w{tcp}], [%w{udp}, nil, "desc"] ], - # Don't worry about difference in whitespace - assert_equal(text.gsub(/\s+/, ' '), newtext.gsub(/\s+/, ' ')) + # Conflicting -- first hash wins + [%w{tcp udp}, nil, "first"] => + [[%w{tcp}, nil, "first"], [%w{udp}, nil, "desc"] ], + }.each do |result, hashes| + assert_equal( + z.call(base + result), + @provider.port_merge( + z.call(base + hashes[0]), + z.call(base + hashes[1]) + ), + "Did not get %s out of %s + %s" % [ + result.inspect, + hashes[0].inspect, + hashes[1].inspect + ] + ) end end end |
