From ff8bff52c3f2a08859a0a2d9659e88ca1a10ab54 Mon Sep 17 00:00:00 2001 From: Mark Vanderwiel Date: Thu, 11 Sep 2014 11:59:25 -0500 Subject: [PATCH] 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 --- CHANGELOG.md | 1 + libraries/endpoints.rb | 2 +- libraries/network.rb | 11 ++++++---- spec/network_spec.rb | 50 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 59 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 70dd82bd..70eaeab5 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/libraries/endpoints.rb b/libraries/endpoints.rb index 9395c200..4b968f63 100644 --- a/libraries/endpoints.rb +++ b/libraries/endpoints.rb @@ -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. diff --git a/libraries/network.rb b/libraries/network.rb index 67f61199..84a754d1 100644 --- a/libraries/network.rb +++ b/libraries/network.rb @@ -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 diff --git a/spec/network_spec.rb b/spec/network_spec.rb index 530043f0..436d8516 100644 --- a/spec/network_spec.rb +++ b/spec/network_spec.rb @@ -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