]> review.fuel-infra Code Review - puppet-modules/puppetlabs-firewall.git/commitdiff
(#14463) Fix to pass unit tests
authorSharif Nassar <sharif@mediatemple.net>
Tue, 27 Nov 2012 22:32:46 +0000 (14:32 -0800)
committerDan Carley <dan.carley@gmail.com>
Thu, 29 Nov 2012 17:22:11 +0000 (17:22 +0000)
* Add default protocol to fix the test for converting a string 'ssh' to a port
number was failing like so:
  1) Puppet::Type::Firewall dport should convert a port name for dport to its number
     Failure/Error: @resource[port] = 'ssh'
     Puppet::Error:
       Parameter dport failed: Munging failed for value "ssh" in class dport: no such service ssh/proto
     # ./lib/puppet/type/../../puppet/util/firewall.rb:84:in `getservbyname'
     # ./lib/puppet/type/../../puppet/util/firewall.rb:84:in `string_to_port'
     # ./lib/puppet/type/firewall.rb:164:in `unsafe_munge'
     # ./spec/unit/puppet/type/firewall_spec.rb:161

* Always convert the response .to_s

lib/puppet/util/firewall.rb

index 7ee8cdda69e9af8007caeff02d6f7ba3315cecfa..cdf20aa88e2b871cacaeeccc0057e9f9a065eb6a 100644 (file)
@@ -65,14 +65,18 @@ module Puppet::Util::Firewall
     end
   end
 
-  # This method takes a string and attempts to convert it to a port number
-  # if valid.
+  # This method takes a string and a protocol and attempts to convert
+  # it to a port number if valid.
   #
   # If the string already contains a port number or perhaps a range of ports
   # in the format 22:1000 for example, it simply returns the string and does
   # nothing.
   def string_to_port(value, proto)
     proto = proto.to_s
+    unless proto =~ /^(tcp|udp)$/
+      proto = 'tcp'
+    end
+
     if value.kind_of?(String)
       if value.match(/^\d+(-\d+)?$/)
         return value
@@ -80,7 +84,7 @@ module Puppet::Util::Firewall
         return Socket.getservbyname(value, proto).to_s
       end
     else
-      Socket.getservbyname(value.to_s, proto)
+      Socket.getservbyname(value.to_s, proto).to_s
     end
   end