From cdf5e432de2cf1c0c5466470a6a0bccf999fcdd4 Mon Sep 17 00:00:00 2001 From: Dan Smith Date: Thu, 21 Feb 2013 13:15:23 -0500 Subject: Fix crash in quantumapi if no network or port id is specified The existing code assumes that if no port id or fixed ip was specified, that the network id passed in is valid. This is not necessarily the case, and results in adding None to the list of network ids, which later causes a check to see if the network id list is empty to fail. This only adds the network id to the list if it was specified, and adds a more explicit check to the region that previously fell through without finding a matching network so that a similar case would be easier to debug in the future. Addresses bug #1131264 Change-Id: I7a8aaea5772707b8bd6313249e74b1302eedefe8 --- nova/network/quantumv2/api.py | 11 +++++++++-- nova/tests/network/test_quantumv2.py | 15 +++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/nova/network/quantumv2/api.py b/nova/network/quantumv2/api.py index 851a544dc..50e38eebd 100644 --- a/nova/network/quantumv2/api.py +++ b/nova/network/quantumv2/api.py @@ -172,9 +172,10 @@ class API(base.Base): available_macs.discard(port['mac_address']) network_id = port['network_id'] ports[network_id] = port - elif fixed_ip: + elif fixed_ip and network_id: fixed_ips[network_id] = fixed_ip - net_ids.append(network_id) + if network_id: + net_ids.append(network_id) nets = self._get_available_networks(context, instance['project_id'], net_ids) @@ -774,6 +775,12 @@ class API(base.Base): network_name = net['name'] break + if network_name is None: + raise exception.NotFound(_('Network %(net)s for ' + 'port %(port_id)s not found!') % + {'net': port['network_id'], + 'port': port['id']}) + network_IPs = [network_model.FixedIP(address=ip_address) for ip_address in [ip['ip_address'] for ip in port['fixed_ips']]] diff --git a/nova/tests/network/test_quantumv2.py b/nova/tests/network/test_quantumv2.py index e9e19ef45..a168e451f 100644 --- a/nova/tests/network/test_quantumv2.py +++ b/nova/tests/network/test_quantumv2.py @@ -631,6 +631,21 @@ class TestQuantumv2(test.TestCase): self.assertRaises(QUANTUM_CLIENT_EXCEPTION, api.allocate_for_instance, self.context, self.instance) + def test_allocate_for_instance_no_port_or_network(self): + class BailOutEarly(Exception): + pass + api = quantumapi.API() + self.mox.StubOutWithMock(api, '_get_available_networks') + # Make sure we get an empty list and then bail out of the rest + # of the function + api._get_available_networks(self.context, self.instance['project_id'], + []).AndRaise(BailOutEarly) + self.mox.ReplayAll() + self.assertRaises(BailOutEarly, + api.allocate_for_instance, + self.context, self.instance, + requested_networks=[(None, None, None)]) + def _deallocate_for_instance(self, number): port_data = number == 1 and self.port_data1 or self.port_data2 self.moxed_client.list_ports( -- cgit