Permalink
Browse files

(#14463) Convert port Fixnum into strings

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.
  • Loading branch information...
1 parent b4ae95d commit 53d3ef9da087b684331601fd112f57b13f931c6a Sharif Nassar committed with dcarley Nov 27, 2012
View
6 lib/puppet/type/firewall.rb
@@ -131,7 +131,7 @@
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 @@ def should_to_s(value)
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 @@ def should_to_s(value)
EOS
munge do |value|
- @resource.string_to_port(value)
+ @resource.string_to_port(value, :proto)
end
def is_to_s(value)
View
7 lib/puppet/util/firewall.rb
@@ -71,15 +71,16 @@ def log_level_name_to_number(value)
# 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
View
5 spec/unit/puppet/type/firewall_spec.rb
@@ -140,6 +140,11 @@
@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']
View
6 spec/unit/puppet/util/firewall_spec.rb
@@ -68,8 +68,10 @@
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

0 comments on commit 53d3ef9

Please sign in to comment.