diff options
author | luke <luke@980ebf18-57e1-0310-9a29-db15c13687c0> | 2006-09-03 22:01:46 +0000 |
---|---|---|
committer | luke <luke@980ebf18-57e1-0310-9a29-db15c13687c0> | 2006-09-03 22:01:46 +0000 |
commit | daa79e2da4d7a8a32c461da9bfb01c7a98763a5c (patch) | |
tree | b14d23f24c0a151cd9f9e5387c7c2218d0ef76a4 | |
parent | b657850bf5cc02b25200720a57d5e3e381a98b62 (diff) | |
download | puppet-daa79e2da4d7a8a32c461da9bfb01c7a98763a5c.tar.gz puppet-daa79e2da4d7a8a32c461da9bfb01c7a98763a5c.tar.xz puppet-daa79e2da4d7a8a32c461da9bfb01c7a98763a5c.zip |
Intermediate commit; ports are not working yet
git-svn-id: https://reductivelabs.com/svn/puppet/trunk@1549 980ebf18-57e1-0310-9a29-db15c13687c0
-rwxr-xr-x | lib/puppet/type/parsedtype/port.rb | 261 | ||||
-rwxr-xr-x | lib/puppet/type/port.rb | 130 | ||||
-rw-r--r-- | test/providers/parsedhost.rb | 23 | ||||
-rwxr-xr-x | test/providers/parsedsshkey.rb | 21 | ||||
-rw-r--r-- | test/puppettest.rb | 24 | ||||
-rwxr-xr-x | test/types/port.rb | 53 |
6 files changed, 170 insertions, 342 deletions
diff --git a/lib/puppet/type/parsedtype/port.rb b/lib/puppet/type/parsedtype/port.rb deleted file mode 100755 index f8a08913f..000000000 --- a/lib/puppet/type/parsedtype/port.rb +++ /dev/null @@ -1,261 +0,0 @@ -require 'etc' -require 'facter' -require 'puppet/type/parsedtype' -require 'puppet/type/state' - -module Puppet - newtype(:port, Puppet::Type::ParsedType) do - newstate(:protocols) do - desc "The protocols the port uses. Valid values are *udp* and *tcp*. - Most services have both protocols, but not all. If you want - both protocols, you must specify that; Puppet replaces the - current values, it does not merge with them. If you specify - multiple protocols they must be as an array." - - def is=(value) - case value - when String - @is = value.split(/\s+/) - else - @is = value - end - end - - def is - @is - end - - # We actually want to return the whole array here, not just the first - # value. - def should - if defined? @should - if @should[0] == :absent - return :absent - else - return @should - end - else - return nil - end - end - - validate do |value| - valids = ["udp", "tcp", "ddp", :absent] - unless valids.include? value - raise Puppet::Error, - "Protocols can be either 'udp' or 'tcp', not %s" % value - end - end - end - - newstate(:number) do - desc "The port number." - end - - newstate(:description) do - desc "The port description." - isoptional - end - - newstate(:alias) do - desc "Any aliases the port might have. Multiple values must be - specified as an array. Note that this state has the same name as - one of the metaparams; using this state to set aliases will make - those aliases available in your Puppet scripts and also on disk." - - isoptional - - # We have to override the feeding mechanism; it might be nil or - # white-space separated - def is=(value) - # If it's just whitespace, ignore it - case value - when /^\s+$/ - @is = nil - when String - @is = value.split(/\s+/) - when Symbol - @is = value - else - raise Puppet::DevError, "Invalid value %s" % value.inspect - end - end - - # We actually want to return the whole array here, not just the first - # value. - def should - if defined? @should - if @should[0] == :absent - return :absent - else - return @should - end - else - return nil - end - end - - validate do |value| - if value.is_a? String and value =~ /\s/ - raise Puppet::Error, - "Aliases cannot have whitespace in them: %s" % - value.inspect - end - end - - munge do |value| - unless value == "absent" or value == :absent - # Add the :alias metaparam in addition to the state - @parent.newmetaparam( - @parent.class.metaparamclass(:alias), value - ) - end - value - end - end - - newparam(:name) do - desc "The port name." - - isnamevar - end - - @doc = "Installs and manages port entries. For most systems, these - entries will just be in /etc/services, but some systems (notably OS X) - will have different solutions." - - @path = "/etc/services" - @fields = [:ip, :name, :alias] - - @filetype = Puppet::FileType.filetype(:flat) - - # Parse a services file - # - # This method also stores existing comments, and it stores all port - # info in order, mostly so that comments are retained in the order - # they were written and in proximity to the same ports. - def self.parse(text) - count = 0 - hash = {} - text.chomp.split("\n").each { |line| - hash.clear - case line - when /^#/, /^\s*$/: - # add comments and blank lines to the list as they are - @instances << line - else - if line.sub!(/^(\S+)\s+(\d+)\/(\w+)\s*/, '') - hash[:name] = $1 - hash[:number] = $2 - hash[:protocols] = $3 - - unless line == "" - line.sub!(/^([^#]+)\s*/) do |value| - aliases = $1 - - # Remove any trailing whitespace - aliases.strip! - unless aliases =~ /^\s*$/ - hash[:alias] = aliases - end - - "" - end - - line.sub!(/^\s*#\s*(.+)$/) do |value| - desc = $1 - unless desc =~ /^\s*$/ - hash[:description] = desc.sub(/\s*$/, '') - end - - "" - end - end - else - if line =~ /^\s+\d+/ and - Facter["operatingsystem"].value == "Darwin" - #Puppet.notice "Skipping wonky OS X port entry %s" % - # line.inspect - next - end - raise Puppet::Error, "Could not match '%s'" % line - end - - # If there's already a service with this name, then check - # to see if the only difference is the proto; if so, just - # add our proto and walk away - if obj = self[hash[:name]] - if obj.portmerge(hash) - next - end - end - - hash2obj(hash) - - count += 1 - end - } - end - - def portmerge(hash) - unless @states.include?(:protocols) - return false - end - - # This method is only called from parsing, so we only worry - # about 'is' values. - proto = self.state(:protocols).is - - if proto.nil? or proto == :absent - # We are an unitialized object; we've got 'should' - # values but no 'is' values - return false - end - - # If this is happening, our object exists - self.is = [:ensure, :present] - - if hash[:protocols] - # The protocol can be a symbol, so... - if proto.is_a?(Symbol) - proto = [] - end - # Check to see if it already includes our proto - unless proto.include?(hash[:protocols]) - # We are missing their proto - proto << hash[:protocols] - @states[:protocols].is = proto - end - end - - if hash.include?(:description) and ! @states.include?(:description) - self.is = [:description, hash[:description]] - end - - return true - end - - # Convert the current object into one or more services entry. - def to_record - self.state(:protocols).value.collect { |proto| - str = "%s\t%s/%s" % [self[:name], self.value(:number), - proto] - - if value = self.value(:alias) and value != :absent - str += "\t%s" % value.join(" ") - else - str += "\t" - end - - if value = self.value(:description) and value != :absent - str += "\t# %s" % value - else - str += "\t" - end - str - }.join("\n") - end - end -end - -# $Id$ diff --git a/lib/puppet/type/port.rb b/lib/puppet/type/port.rb new file mode 100755 index 000000000..d655a291a --- /dev/null +++ b/lib/puppet/type/port.rb @@ -0,0 +1,130 @@ +require 'etc' +require 'facter' +require 'puppet/type/parsedtype' +require 'puppet/type/state' + +module Puppet + newtype(:port, Puppet::Type::ParsedType) do + + @doc = "Installs and manages port entries. For most systems, these + entries will just be in /etc/services, but some systems (notably OS X) + will have different solutions." + + newstate(:protocols) do + desc "The protocols the port uses. Valid values are *udp* and *tcp*. + Most services have both protocols, but not all. If you want + both protocols, you must specify that; Puppet replaces the + current values, it does not merge with them. If you specify + multiple protocols they must be as an array." + + def is=(value) + case value + when String + @is = value.split(/\s+/) + else + @is = value + end + end + + def is + @is + end + + # We actually want to return the whole array here, not just the first + # value. + def should + if defined? @should + if @should[0] == :absent + return :absent + else + return @should + end + else + return nil + end + end + + validate do |value| + valids = ["udp", "tcp", "ddp", :absent] + unless valids.include? value + raise Puppet::Error, + "Protocols can be either 'udp' or 'tcp', not %s" % value + end + end + end + + newstate(:number) do + desc "The port number." + end + + newstate(:description) do + desc "The port description." + isoptional + end + + newstate(:alias) do + desc "Any aliases the port might have. Multiple values must be + specified as an array. Note that this state has the same name as + one of the metaparams; using this state to set aliases will make + those aliases available in your Puppet scripts and also on disk." + + isoptional + + # We have to override the feeding mechanism; it might be nil or + # white-space separated + def is=(value) + # If it's just whitespace, ignore it + case value + when /^\s+$/ + @is = nil + when String + @is = value.split(/\s+/) + when Symbol + @is = value + else + raise Puppet::DevError, "Invalid value %s" % value.inspect + end + end + + # We actually want to return the whole array here, not just the first + # value. + def should + if defined? @should + if @should[0] == :absent + return :absent + else + return @should + end + else + return nil + end + end + + validate do |value| + if value.is_a? String and value =~ /\s/ + raise Puppet::Error, + "Aliases cannot have whitespace in them: %s" % + value.inspect + end + end + + munge do |value| + unless value == "absent" or value == :absent + # Add the :alias metaparam in addition to the state + @parent.newmetaparam( + @parent.class.metaparamclass(:alias), value + ) + end + value + end + end + + newparam(:name) do + desc "The port name." + + isnamevar + end + end +end + +# $Id$ diff --git a/test/providers/parsedhost.rb b/test/providers/parsedhost.rb index 1d982c813..bb122129a 100644 --- a/test/providers/parsedhost.rb +++ b/test/providers/parsedhost.rb @@ -194,28 +194,7 @@ class TestParsedHostProvider < Test::Unit::TestCase # Parse our sample data and make sure we regenerate it correctly. def test_hostsparse - fakedata("data/types/hosts").each { |file| - @provider.path = file - instances = nil - assert_nothing_raised { - instances = @provider.retrieve - } - - text = @provider.fileobj.read - - 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+/, ' ')) - } + fakedata("data/types/hosts").each do |file| fakedataparse(file) end end # Make sure we can modify the file elsewhere and those modifications will diff --git a/test/providers/parsedsshkey.rb b/test/providers/parsedsshkey.rb index f0295c47a..7f21a58b4 100755 --- a/test/providers/parsedsshkey.rb +++ b/test/providers/parsedsshkey.rb @@ -30,26 +30,7 @@ class TestParsedSSHKey < Test::Unit::TestCase def test_keysparse fakedata("data/types/sshkey").each { |file| - @provider.path = file - instances = nil - assert_nothing_raised { - instances = @provider.retrieve - } - - text = @provider.fileobj.read - - 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+/, ' ')) + fakedataparse(file) } end end diff --git a/test/puppettest.rb b/test/puppettest.rb index 21b539288..5056f2d40 100644 --- a/test/puppettest.rb +++ b/test/puppettest.rb @@ -440,6 +440,30 @@ module TestPuppet return file end + # Run an isomorphism test on our parsing process. + def fakedataparse(file) + @provider.path = file + instances = nil + assert_nothing_raised { + instances = @provider.retrieve + } + + text = @provider.fileobj.read + + 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+/, ' ')) + end + # wrap how to retrieve the masked mode def filemode(file) File.stat(file).mode & 007777 diff --git a/test/types/port.rb b/test/types/port.rb index debd7ed89..e550bd83e 100755 --- a/test/types/port.rb +++ b/test/types/port.rb @@ -14,23 +14,25 @@ require 'facter' class TestPort < Test::Unit::TestCase include TestPuppet + def setup super @porttype = Puppet.type(:port) - @oldfiletype = @porttype.filetype - end - def teardown - @porttype.filetype = @oldfiletype - Puppet.type(:file).clear - super - end + @provider = @porttype.defaultprovider - # Here we just create a fake host type that answers to all of the methods - # but does not modify our actual system. - def mkfaketype - pfile = tempfile() - @porttype.path = pfile + # Make sure they aren't using something funky like netinfo + unless @provider.name == :parsed + @porttype.defaultprovider = @porttype.provider(:parsed) + end + + cleanup do @porttype.defaultprovider = nil end + + oldpath = @provider.path + cleanup do + @provider.path = oldpath + end + @provider.path = tempfile() end def mkport @@ -55,7 +57,6 @@ class TestPort < Test::Unit::TestCase end def test_simpleport - mkfaketype host = nil assert_nothing_raised { assert_nil(Puppet.type(:port).retrieve) @@ -77,29 +78,7 @@ class TestPort < Test::Unit::TestCase } end - def test_portsparse - fakedata("data/types/ports").each { |file| - @porttype.path = file - Puppet.info "Parsing %s" % file - assert_nothing_raised { - @porttype.retrieve - } - - # Now just make we've got some ports we know will be there - dns = @porttype["domain"] - assert(dns, "Could not retrieve DNS port") - - assert_equal("53", dns.is(:number), "DNS number was wrong") - %w{udp tcp}.each { |v| - assert(dns.is(:protocols).include?(v), "DNS did not include proto %s" % v) - } - - @porttype.clear - } - end - def test_moddingport - mkfaketype port = nil port = mkport @@ -123,7 +102,6 @@ class TestPort < Test::Unit::TestCase end def test_removal - mkfaketype port = mkport() assert_nothing_raised { port[:ensure] = :present @@ -142,7 +120,6 @@ class TestPort < Test::Unit::TestCase end def test_modifyingfile - mkfaketype() ports = [] names = [] @@ -174,8 +151,6 @@ class TestPort < Test::Unit::TestCase end def test_addingstates - mkfaketype - port = mkport() assert_events([:port_created], port) |