address_for does not check for nil interface addresses

Add checks to address_for to check for no addresses for interface
or no address for interface family.

Change-Id: I3f81a80457df7a421cf60ba192fb8d7062f641a6
Closes-Bug: #1368292
This commit is contained in:
Mark Vanderwiel 2014-09-11 11:59:25 -05:00
parent 7697c97231
commit ff8bff52c3
4 changed files with 59 additions and 5 deletions

View File

@ -5,6 +5,7 @@ This file is used to list changes made in each version of cookbook-openstack-com
## 10.1.0
* Adding identity admin bind host endpoint to allow flexibility and consistency
* Fix logging.conf No section error
* Raise error for nil address returned from address_for
## 10.0.3
* Adding identity internal endpoint variables to support new endpoint usage in identity cookbook

View File

@ -90,7 +90,7 @@ module ::Openstack # rubocop:disable Documentation
end
end
# Return the IPv4 address for the hash.
# Return the address for the hash.
#
# If the bind_interface is set, then return the first IP on the interface.
# otherwise return the IP specified in the host attribute.

View File

@ -24,11 +24,14 @@ module ::Openstack # rubocop:disable Documentation
#
# @param [String] interface The interface to query.
# @param [String] family The protocol family to use.
# @return [String] The address.
# @return [String] The address or log error when address is nil
def address_for(interface, family = node['openstack']['endpoints']['family'])
interface_node = node['network']['interfaces'][interface]['addresses']
interface_node.select do |address, data|
return address if data['family'] == family
end
fail "Interface #{interface} has no addresses assigned" if interface_node.to_a.empty?
address = interface_node.find { |addr, data| data['family'] == family }
fail "Interface #{interface} has no address for family #{family}" if address.nil?
address[0]
end
end

View File

@ -60,5 +60,55 @@ describe 'openstack-common::default' do
).to eq('::1')
end
end
describe '#address_for failures' do
it 'fails when addresses for interface is nil' do
node.set['network'] = {
'interfaces' => {
'lo' => {
'addresses' => nil
}
}
}
allow(subject).to receive(:address_for).with('lo')
.and_raise('Interface lo has no addresses assigned')
expect { subject.address_for('lo') }
.to raise_error(RuntimeError, 'Interface lo has no addresses assigned')
end
it 'fails when no addresses are avaiable for interface' do
node.set['network'] = {
'interfaces' => {
'lo' => {
'addresses' => {}
}
}
}
allow(subject).to receive(:address_for).with('lo')
.and_raise('Interface lo has no addresses assigned')
expect { subject.address_for('lo') }
.to raise_error(RuntimeError, 'Interface lo has no addresses assigned')
end
it 'fails when no address is available for interface family' do
node.set['network'] = {
'interfaces' => {
'lo' => {
'addresses' => {
'127.0.0.1' => {
'family' => 'inet',
'prefixlen' => '8',
'netmask' => '255.0.0.0',
'scope' => 'Node'
}
}
}
}
}
allow(subject).to receive(:address_for).with('lo', 'inet6')
.and_raise('Interface lo has no address for family inet6')
expect { subject.address_for('lo', 'inet6') }
.to raise_error(RuntimeError, 'Interface lo has no address for family inet6')
end
end
end
end