]> review.fuel-infra Code Review - puppet-modules/puppetlabs-firewall.git/commitdiff
(#14463) Convert port Fixnum into strings
authorSharif Nassar <sharif@mediatemple.net>
Tue, 27 Nov 2012 19:39:59 +0000 (11:39 -0800)
committerDan Carley <dan.carley@gmail.com>
Thu, 29 Nov 2012 17:21:57 +0000 (17:21 +0000)
Avert errors like this:
Parameter dport failed: Munging failed for value 1194 in class dport: can’t convert Fixnum into String

Also, pass along the protocol so Socket can make well informed decisions.

lib/puppet/type/firewall.rb
lib/puppet/util/firewall.rb
spec/unit/puppet/type/firewall_spec.rb
spec/unit/puppet/util/firewall_spec.rb

index 6d6f28953a2bbb9d8bf606525d3043cf1bd86f1e..06b82fc3a49eaa336f288071b1fa0dd8ebcb4362 100644 (file)
@@ -131,7 +131,7 @@ Puppet::Type.newtype(:firewall) do
     EOS
 
     munge do |value|
-      @resource.string_to_port(value)
+      @resource.string_to_port(value, :proto)
     end
 
     def is_to_s(value)
@@ -161,7 +161,7 @@ Puppet::Type.newtype(:firewall) do
     EOS
 
     munge do |value|
-      @resource.string_to_port(value)
+      @resource.string_to_port(value, :proto)
     end
 
     def is_to_s(value)
@@ -191,7 +191,7 @@ Puppet::Type.newtype(:firewall) do
     EOS
 
     munge do |value|
-      @resource.string_to_port(value)
+      @resource.string_to_port(value, :proto)
     end
 
     def is_to_s(value)
index e93650ca2d2f5a8274bbae77a88e82dfba530c98..7ee8cdda69e9af8007caeff02d6f7ba3315cecfa 100644 (file)
@@ -71,15 +71,16 @@ module Puppet::Util::Firewall
   # 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)
+  def string_to_port(value, proto)
+    proto = proto.to_s
     if value.kind_of?(String)
       if value.match(/^\d+(-\d+)?$/)
         return value
       else
-        return Socket.getservbyname(value).to_s
+        return Socket.getservbyname(value, proto).to_s
       end
     else
-      Socket.getservbyname(value)
+      Socket.getservbyname(value.to_s, proto)
     end
   end
 
index ead5c9aff8782b5d563a4afb792ad40222b772a7..6fa5e8e4a60535a9b3251fc0394b11e00677ec49 100755 (executable)
@@ -140,6 +140,11 @@ describe firewall do
         @resource[port].should == ['22','23']
       end
 
+      it "should accept a #{port} as a number" do
+        @resource[port] = 22
+        @resource[port].should == ['22']
+      end
+
       it "should accept a #{port} as a hyphen separated range" do
         @resource[port] = ['22-1000']
         @resource[port].should == ['22-1000']
index c5ab12cf70b035be6353469bc5c78f1428f0a933..097782d95a5ebae39e664d1ab30af52ece6705d2 100644 (file)
@@ -68,8 +68,10 @@ describe 'Puppet::Util::Firewall' do
 
   describe '#string_to_port' do
     subject { resource }
-    specify { subject.string_to_port('80').should == '80' }
-    specify { subject.string_to_port('http').should == '80' }
+    specify { subject.string_to_port('80','tcp').should == '80' }
+    specify { subject.string_to_port(80,'tcp').should == '80' }
+    specify { subject.string_to_port('http','tcp').should == '80' }
+    specify { subject.string_to_port('domain','udp').should == '53' }
   end
 
   describe '#to_hex32' do