Allow integer port in os_transport_url

Currently we only allow to use strings
as the data type for ports.

Due to this we need to cast the data type
in the puppet modules because from THT
this is configured as a Number.

This submission allow to use either string
or numbers for the port parameter in the
os_transport_url function.

Change-Id: I9e56f8e2de542b20fe9e6995506cff5bb435e220
Closes-Bug: #1664561
This commit is contained in:
Carlos Camacho 2017-02-21 11:37:22 +01:00
parent 0176f97117
commit b3d0590022
3 changed files with 55 additions and 4 deletions

View File

@ -10,7 +10,7 @@ Valid hash parameteres:
* transport - (string) type of transport, 'rabbit' or 'amqp'
* host - (string) single host
* hosts - (array) array of hosts to use
* port - (string) port to connect to
* port - (string | integer) port to connect to
* username - (string) connection username
* password - (string) connection password
* virtual_host - (string) virtual host to connect to
@ -68,6 +68,7 @@ EOS
# type checking for the parameter hash
v.keys.each do |key|
v[key] = v[key].to_s if key == 'port'
klass = (key == 'hosts') ? Array : String
klass = (key == 'query') ? Hash : klass
unless (v[key].class == klass) or (v[key] == :undef)
@ -105,7 +106,7 @@ EOS
if v.include?('host')
host = function_normalize_ip_for_uri([v['host']])
host += ":#{v['port']}" if v.include?('port')
host += ":#{v['port'].to_s}" if v.include?('port')
if parts.include?(:userinfo)
parts[:hostinfo] = "#{parts[:userinfo]}@#{host}"
else
@ -118,7 +119,7 @@ EOS
# normalize_ip_for_uri may return a string, so check that we still have an
# array
hosts = [hosts] if hosts.kind_of?(String)
hosts = hosts.map{ |h| "#{h}:#{v['port']}" } if v.include?('port')
hosts = hosts.map{ |h| "#{h}:#{v['port'].to_s}" } if v.include?('port')
if parts.include?(:userinfo)
parts[:hostinfo] = hosts.map { |h| "#{parts[:userinfo]}@#{h}" }.join(',')
else

View File

@ -0,0 +1,4 @@
---
fixes:
- Bugfix 1664561. Allow to use Integers and Strings
for the port parameter.

View File

@ -50,6 +50,19 @@ describe 'os_transport_url' do
}).and_return('rabbit://guest:s3cr3t@127.0.0.1:5672/virt?read_timeout=60&ssl=1')
end
it 'with a single host array for hosts and integer port' do
is_expected.to run.with_params({
'transport' => 'rabbit',
'hosts' => [ '127.0.0.1' ],
'port' => 5672,
'username' => 'guest',
'password' => 's3cr3t',
'virtual_host' => 'virt',
'ssl' => '1',
'query' => { 'read_timeout' => '60' },
}).and_return('rabbit://guest:s3cr3t@127.0.0.1:5672/virt?read_timeout=60&ssl=1')
end
it 'with all params for a single host' do
is_expected.to run.with_params({
'transport' => 'rabbit',
@ -63,6 +76,19 @@ describe 'os_transport_url' do
}).and_return('rabbit://guest:s3cr3t@127.0.0.1:5672/virt?read_timeout=60&ssl=1')
end
it 'with all params for a single host and integer port' do
is_expected.to run.with_params({
'transport' => 'rabbit',
'host' => '127.0.0.1',
'port' => 5672,
'username' => 'guest',
'password' => 's3cr3t',
'virtual_host' => 'virt',
'ssl' => '1',
'query' => { 'read_timeout' => '60' },
}).and_return('rabbit://guest:s3cr3t@127.0.0.1:5672/virt?read_timeout=60&ssl=1')
end
it 'with only required params for a single host' do
is_expected.to run.with_params({
'transport' => 'rabbit',
@ -74,7 +100,15 @@ describe 'os_transport_url' do
is_expected.to run.with_params({
'transport' => 'rabbit',
'host' => 'fe80::ca5b:76ff:fe4b:be3b',
'port' => '5672'
'port' => '5672',
}).and_return('rabbit://[fe80::ca5b:76ff:fe4b:be3b]:5672/')
end
it 'with a single ipv6 address and integer port' do
is_expected.to run.with_params({
'transport' => 'rabbit',
'host' => 'fe80::ca5b:76ff:fe4b:be3b',
'port' => 5672,
}).and_return('rabbit://[fe80::ca5b:76ff:fe4b:be3b]:5672/')
end
@ -90,6 +124,18 @@ describe 'os_transport_url' do
}).and_return('rabbit://guest:s3cr3t@1.1.1.1:5672,guest:s3cr3t@2.2.2.2:5672/virt?read_timeout=60')
end
it 'with all params with multiple hosts and integer port' do
is_expected.to run.with_params({
'transport' => 'rabbit',
'hosts' => ['1.1.1.1', '2.2.2.2'],
'port' => 5672,
'username' => 'guest',
'password' => 's3cr3t',
'virtual_host' => 'virt',
'query' => { 'read_timeout' => '60' },
}).and_return('rabbit://guest:s3cr3t@1.1.1.1:5672,guest:s3cr3t@2.2.2.2:5672/virt?read_timeout=60')
end
it 'with only required params for multiple hosts' do
is_expected.to run.with_params({
'transport' => 'rabbit',