fix for auth during live-migration

Post step could fail due to auth token expiration.
get_instance_nw_info fails with authentication required,
because there are several calls to neutron api, some of them
are admin context, while others try to use token from request
context. This patch ensure that if admin context is initially used,
all subsequent calls will use the same initialized client

Closes-Bug: #1647451

Change-Id: I8962a9cd472cbbb5b9b67c5b164ff29fd8f5558a
This commit is contained in:
Timofey Durakov 2016-12-01 19:03:24 +03:00
parent e83a357234
commit 4a5ecf1e29
2 changed files with 116 additions and 41 deletions

View File

@ -1283,7 +1283,7 @@ class API(base_api.NetworkAPI):
return network_model.NetworkInfo.hydrate(nw_info)
def _gather_port_ids_and_networks(self, context, instance, networks=None,
port_ids=None):
port_ids=None, neutron=None):
"""Return an instance's complete list of port_ids and networks."""
if ((networks is None and port_ids is not None) or
@ -1302,7 +1302,7 @@ class API(base_api.NetworkAPI):
if networks is None:
networks = self._get_available_networks(context,
instance.project_id,
net_ids)
net_ids, neutron)
# an interface was added/removed from instance.
else:
@ -2049,8 +2049,8 @@ class API(base_api.NetworkAPI):
network_IPs.append(fixed)
return network_IPs
def _nw_info_get_subnets(self, context, port, network_IPs):
subnets = self._get_subnets_from_port(context, port)
def _nw_info_get_subnets(self, context, port, network_IPs, client=None):
subnets = self._get_subnets_from_port(context, port, client)
for subnet in subnets:
subnet['ips'] = [fixed_ip for fixed_ip in network_IPs
if fixed_ip.is_in_subnet(subnet)]
@ -2167,7 +2167,7 @@ class API(base_api.NetworkAPI):
current_neutron_ports = data.get('ports', [])
nw_info_refresh = networks is None and port_ids is None
networks, port_ids = self._gather_port_ids_and_networks(
context, instance, networks, port_ids)
context, instance, networks, port_ids, client)
nw_info = network_model.NetworkInfo()
if preexisting_port_ids is None:
@ -2192,7 +2192,7 @@ class API(base_api.NetworkAPI):
current_neutron_port)
subnets = self._nw_info_get_subnets(context,
current_neutron_port,
network_IPs)
network_IPs, client)
devname = "tap" + current_neutron_port['id']
devname = devname[:network_model.NIC_NAME_LEN]
@ -2225,7 +2225,7 @@ class API(base_api.NetworkAPI):
return nw_info
def _get_subnets_from_port(self, context, port):
def _get_subnets_from_port(self, context, port, client=None):
"""Return the subnets for a given port."""
fixed_ips = port['fixed_ips']
@ -2236,8 +2236,10 @@ class API(base_api.NetworkAPI):
# related to the port. To avoid this, the method returns here.
if not fixed_ips:
return []
if not client:
client = get_client(context)
search_opts = {'id': [ip['subnet_id'] for ip in fixed_ips]}
data = get_client(context).list_subnets(**search_opts)
data = client.list_subnets(**search_opts)
ipam_subnets = data.get('subnets', [])
subnets = []
@ -2251,7 +2253,7 @@ class API(base_api.NetworkAPI):
# attempt to populate DHCP server field
search_opts = {'network_id': subnet['network_id'],
'device_owner': 'network:dhcp'}
data = get_client(context).list_ports(**search_opts)
data = client.list_ports(**search_opts)
dhcp_ports = data.get('ports', [])
for p in dhcp_ports:
for ip_pair in p['fixed_ips']:

View File

@ -800,11 +800,6 @@ class TestNeutronv2Base(test.TestCase):
class TestNeutronv2(TestNeutronv2Base):
def setUp(self):
super(TestNeutronv2, self).setUp()
neutronapi.get_client(mox.IgnoreArg()).MultipleTimes().AndReturn(
self.moxed_client)
def test_get_instance_nw_info_1(self):
# Test to get one port in one network and subnet.
neutronapi.get_client(mox.IgnoreArg(),
@ -1023,6 +1018,8 @@ class TestNeutronv2(TestNeutronv2Base):
api.db.instance_info_cache_update(
mox.IgnoreArg(),
self.instance['uuid'], mox.IgnoreArg()).AndReturn(fake_info_cache)
neutronapi.get_client(mox.IgnoreArg(), admin=True).AndReturn(
self.moxed_client)
self.moxed_client.list_ports(
tenant_id=self.instance['project_id'],
device_id=self.instance['uuid']).AndReturn(
@ -1030,9 +1027,6 @@ class TestNeutronv2(TestNeutronv2Base):
self.moxed_client.list_networks(
id=[self.port_data1[0]['network_id']]).AndReturn(
{'networks': self.nets1})
neutronapi.get_client(mox.IgnoreArg(),
admin=True).MultipleTimes().AndReturn(
self.moxed_client)
net_info_cache = []
for port in self.port_data3:
@ -1095,10 +1089,12 @@ class TestNeutronv2(TestNeutronv2Base):
def test_allocate_for_instance_1(self):
# Allocate one port in one network env.
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
self._allocate_for_instance(1)
def test_allocate_for_instance_2(self):
# Allocate one port in two networks env.
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
api = self._stub_allocate_for_instance(net_idx=2)
self.assertRaises(exception.NetworkAmbiguous,
api.allocate_for_instance,
@ -1106,17 +1102,20 @@ class TestNeutronv2(TestNeutronv2Base):
def test_allocate_for_instance_accepts_macs_kwargs_None(self):
# The macs kwarg should be accepted as None.
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
self._allocate_for_instance(1, macs=None)
def test_allocate_for_instance_accepts_macs_kwargs_set(self):
# The macs kwarg should be accepted, as a set, the
# _allocate_for_instance helper checks that the mac is used to create a
# port.
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
self._allocate_for_instance(1, macs=set(['ab:cd:ef:01:23:45']))
def test_allocate_for_instance_with_mac_added_to_port(self):
requested_networks = objects.NetworkRequestList(
objects=[objects.NetworkRequest(port_id=uuids.portid_1)])
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
# NOTE(johngarbutt) we override the provided mac with a new one
self._allocate_for_instance(net_idx=1,
requested_networks=requested_networks,
@ -1127,6 +1126,7 @@ class TestNeutronv2(TestNeutronv2Base):
def test_allocate_for_instance_accepts_only_portid(self):
# Make sure allocate_for_instance works when only a portid is provided
self._returned_nw_info = self.port_data1
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
result = self._allocate_for_instance(
requested_networks=objects.NetworkRequestList(
objects=[objects.NetworkRequest(port_id=uuids.portid_1,
@ -1152,6 +1152,7 @@ class TestNeutronv2(TestNeutronv2Base):
objects = [
objects.NetworkRequest(network_id=self.nets2[1]['id']),
objects.NetworkRequest(port_id=uuids.portid_1)])
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
api = self._stub_allocate_for_instance(
net_idx=2, requested_networks=requested_networks,
macs=set(['my_mac1']),
@ -1173,6 +1174,7 @@ class TestNeutronv2(TestNeutronv2Base):
requested_networks = objects.NetworkRequestList(
objects=[objects.NetworkRequest(network_id=self.nets2[1]['id']),
objects.NetworkRequest(network_id=self.nets2[0]['id'])])
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
api = self._stub_allocate_for_instance(
net_idx=2, requested_networks=requested_networks,
macs=set(['my_mac2']),
@ -1192,11 +1194,13 @@ class TestNeutronv2(TestNeutronv2Base):
requested_networks = objects.NetworkRequestList(
objects=[objects.NetworkRequest(network_id=self.nets2[1]['id']),
objects.NetworkRequest(network_id=self.nets2[0]['id'])])
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
self._allocate_for_instance(
net_idx=2, requested_networks=requested_networks,
macs=set(['my_mac2', 'my_mac1']))
def test_allocate_for_instance_without_requested_networks(self):
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
api = self._stub_allocate_for_instance(net_idx=3)
self.assertRaises(exception.NetworkAmbiguous,
api.allocate_for_instance,
@ -1211,6 +1215,7 @@ class TestNeutronv2(TestNeutronv2Base):
objects=[objects.NetworkRequest(network_id=net['id'])
for net in (self.nets3[0], self.nets3[2], self.nets3[1])])
requested_networks[0].tag = 'foo'
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
self._allocate_for_instance(net_idx=2,
requested_networks=requested_networks)
self.assertEqual(2, len(self._vifs_created))
@ -1229,6 +1234,7 @@ class TestNeutronv2(TestNeutronv2Base):
requested_networks = objects.NetworkRequestList(
objects=[objects.NetworkRequest(network_id=net['id'])
for net in (self.nets3[1], self.nets3[0], self.nets3[2])])
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
self._allocate_for_instance(net_idx=3,
requested_networks=requested_networks)
@ -1238,6 +1244,7 @@ class TestNeutronv2(TestNeutronv2Base):
# able to associate the default security group to the port
# requested to be created. We expect an exception to be
# raised.
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
self.assertRaises(exception.SecurityGroupCannotBeApplied,
self._allocate_for_instance, net_idx=4,
_break='post_list_extensions')
@ -1246,6 +1253,7 @@ class TestNeutronv2(TestNeutronv2Base):
requested_networks = objects.NetworkRequestList(
objects=[objects.NetworkRequest(
network_id=uuids.non_existent_uuid)])
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
api = self._stub_allocate_for_instance(net_idx=9,
requested_networks=requested_networks,
_break='post_list_networks')
@ -1259,12 +1267,14 @@ class TestNeutronv2(TestNeutronv2Base):
requested_networks = objects.NetworkRequestList(
objects=[objects.NetworkRequest(network_id=self.nets1[0]['id'],
address='10.0.1.0')])
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
self._allocate_for_instance(net_idx=1,
requested_networks=requested_networks)
def test_allocate_for_instance_with_requested_networks_with_port(self):
requested_networks = objects.NetworkRequestList(
objects=[objects.NetworkRequest(port_id=uuids.portid_1)])
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
self._allocate_for_instance(net_idx=1,
requested_networks=requested_networks)
@ -1277,6 +1287,7 @@ class TestNeutronv2(TestNeutronv2Base):
tenant_id=self.instance.project_id,
shared=False).AndReturn(
{'networks': model.NetworkInfo([])})
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
self.moxed_client.list_networks(shared=True).AndReturn(
{'networks': model.NetworkInfo([])})
self.mox.ReplayAll()
@ -1302,6 +1313,7 @@ class TestNeutronv2(TestNeutronv2Base):
requested_networks = objects.NetworkRequestList(
objects=[objects.NetworkRequest(network_id=net['id'])
for net in (self.nets2[0], self.nets2[1])])
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
self.moxed_client.list_networks(
id=[uuids.my_netid1, uuids.my_netid2]).AndReturn(
{'networks': self.nets2})
@ -1367,6 +1379,7 @@ class TestNeutronv2(TestNeutronv2Base):
requested_networks = objects.NetworkRequestList(
objects=[objects.NetworkRequest(network_id=net['id'])
for net in (self.nets2[0], self.nets2[1])])
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
self.moxed_client.list_networks(
id=[uuids.my_netid1, uuids.my_netid2]).AndReturn(
{'networks': self.nets2})
@ -1392,6 +1405,7 @@ class TestNeutronv2(TestNeutronv2Base):
self.instance = fake_instance.fake_instance_obj(self.context,
**self.instance)
api = neutronapi.API()
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
self.mox.StubOutWithMock(api, '_get_available_networks')
# Make sure we get an empty list and then bail out of the rest
# of the function
@ -1413,6 +1427,7 @@ class TestNeutronv2(TestNeutronv2Base):
# allocated during _that_ run.
new_port = {'id': uuids.fake}
self._returned_nw_info = self.port_data1 + [new_port]
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
nw_info = self._allocate_for_instance()
self.assertEqual([new_port], nw_info)
@ -1420,6 +1435,7 @@ class TestNeutronv2(TestNeutronv2Base):
# If a port is already in use, an exception should be raised.
requested_networks = objects.NetworkRequestList(
objects=[objects.NetworkRequest(port_id=uuids.portid_1)])
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
api = self._stub_allocate_for_instance(
requested_networks=requested_networks,
_break='pre_list_networks',
@ -1432,6 +1448,7 @@ class TestNeutronv2(TestNeutronv2Base):
# If a port is not found, an exception should be raised.
requested_networks = objects.NetworkRequestList(
objects=[objects.NetworkRequest(port_id=uuids.non_existent_uuid)])
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
api = self._stub_allocate_for_instance(
requested_networks=requested_networks,
_break='pre_list_networks')
@ -1443,6 +1460,7 @@ class TestNeutronv2(TestNeutronv2Base):
self.tenant_id = 'invalid_id'
requested_networks = objects.NetworkRequestList(
objects=[objects.NetworkRequest(port_id=uuids.portid_1)])
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
api = self._stub_allocate_for_instance(
requested_networks=requested_networks,
_break='pre_list_networks')
@ -1456,6 +1474,7 @@ class TestNeutronv2(TestNeutronv2Base):
"""
self.instance = fake_instance.fake_instance_obj(self.context,
**self.instance)
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
# no networks in the tenant
self.moxed_client.list_networks(
tenant_id=self.instance.project_id,
@ -1476,6 +1495,7 @@ class TestNeutronv2(TestNeutronv2Base):
"""
self.instance = fake_instance.fake_instance_obj(self.context,
**self.instance)
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
# network found in the tenant
self.moxed_client.list_networks(
tenant_id=self.instance.project_id,
@ -1497,12 +1517,14 @@ class TestNeutronv2(TestNeutronv2Base):
"""
admin_ctx = context.RequestContext('userid', uuids.my_tenant,
is_admin=True)
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
api = self._stub_allocate_for_instance(net_idx=8)
api.allocate_for_instance(admin_ctx, self.instance)
def test_allocate_for_instance_with_external_shared_net(self):
"""Only one network is available, it's external and shared."""
ctx = context.RequestContext('userid', uuids.my_tenant)
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
api = self._stub_allocate_for_instance(net_idx=10)
api.allocate_for_instance(ctx, self.instance)
@ -1529,6 +1551,7 @@ class TestNeutronv2(TestNeutronv2Base):
'admin_state_up': True,
'fixed_ips': [],
'mac_address': 'fake_mac', })
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
self.moxed_client.list_ports(
device_id=self.instance.uuid).AndReturn(
{'ports': ret_data})
@ -1591,6 +1614,7 @@ class TestNeutronv2(TestNeutronv2Base):
**self.instance)
mock_preexisting.return_value = []
port_data = self.port_data1
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
self.moxed_client.list_ports(
device_id=self.instance.uuid).AndReturn(
{'ports': port_data})
@ -1617,15 +1641,15 @@ class TestNeutronv2(TestNeutronv2Base):
self.instance['info_cache'] = self._fake_instance_info_cache(
net_info_cache, self.instance['uuid'])
api = neutronapi.API()
neutronapi.get_client(mox.IgnoreArg(), admin=True).AndReturn(
neutronapi.get_client(mox.IgnoreArg()).AndReturn(
self.moxed_client)
self.moxed_client.list_ports(
tenant_id=self.instance['project_id'],
device_id=self.instance['uuid']).AndReturn(
{'ports': port_data[1:]})
neutronapi.get_client(mox.IgnoreArg()).MultipleTimes().AndReturn(
self.moxed_client)
net_ids = [port['network_id'] for port in port_data]
neutronapi.get_client(mox.IgnoreArg(), admin=True).AndReturn(
self.moxed_client)
self.moxed_client.list_networks(id=net_ids).AndReturn(
{'networks': nets})
float_data = number == 1 and self.float_data1 or self.float_data2
@ -1662,11 +1686,13 @@ class TestNeutronv2(TestNeutronv2Base):
def test_list_ports(self):
search_opts = {'parm': 'value'}
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
self.moxed_client.list_ports(**search_opts)
self.mox.ReplayAll()
neutronapi.API().list_ports(self.context, **search_opts)
def test_show_port(self):
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
self.moxed_client.show_port('foo').AndReturn(
{'port': self.port_data1[0]})
self.mox.ReplayAll()
@ -1676,6 +1702,7 @@ class TestNeutronv2(TestNeutronv2Base):
requested_networks = [(uuids.my_netid1, None, None, None),
(uuids.my_netid2, None, None, None)]
ids = [uuids.my_netid1, uuids.my_netid2]
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
self.moxed_client.list_networks(
id=mox.SameElementsAs(ids)).AndReturn(
{'networks': self.nets2})
@ -1693,6 +1720,7 @@ class TestNeutronv2(TestNeutronv2Base):
requested_networks = [(uuids.my_netid1, None, None, None),
(uuids.my_netid2, None, None, None)]
ids = [uuids.my_netid1, uuids.my_netid2]
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
self.moxed_client.list_networks(
id=mox.SameElementsAs(ids)).AndReturn(
{'networks': self.nets2})
@ -1705,6 +1733,7 @@ class TestNeutronv2(TestNeutronv2Base):
def test_validate_networks_ex_1(self):
requested_networks = [(uuids.my_netid1, None, None, None)]
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
self.moxed_client.list_networks(
id=mox.SameElementsAs([uuids.my_netid1])).AndReturn(
{'networks': self.nets1})
@ -1726,6 +1755,7 @@ class TestNeutronv2(TestNeutronv2Base):
(uuids.my_netid2, None, None, None),
(uuids.my_netid3, None, None, None)]
ids = [uuids.my_netid1, uuids.my_netid2, uuids.my_netid3]
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
self.moxed_client.list_networks(
id=mox.SameElementsAs(ids)).AndReturn(
{'networks': self.nets1})
@ -1744,7 +1774,7 @@ class TestNeutronv2(TestNeutronv2Base):
objects=[objects.NetworkRequest(network_id=uuids.my_netid1),
objects.NetworkRequest(network_id=uuids.my_netid1)])
ids = [uuids.my_netid1, uuids.my_netid1]
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
self.moxed_client.list_networks(
id=mox.SameElementsAs(ids)).AndReturn(
{'networks': self.nets1})
@ -1763,6 +1793,8 @@ class TestNeutronv2(TestNeutronv2Base):
requested_networks = objects.NetworkRequestList(
objects=[objects.NetworkRequest(network_id=net['id'])
for net in (self.nets6[0], self.nets6[1])])
neutronapi.get_client(mox.IgnoreArg()).MultipleTimes().AndReturn(
self.moxed_client)
self._allocate_for_instance(net_idx=6,
requested_networks=requested_networks)
@ -1771,6 +1803,8 @@ class TestNeutronv2(TestNeutronv2Base):
requested_networks = objects.NetworkRequestList(
objects=[objects.NetworkRequest(port_id=port['id'])
for port in (self.port_data1[0], self.port_data3[0])])
neutronapi.get_client(mox.IgnoreArg()).MultipleTimes().AndReturn(
self.moxed_client)
self._allocate_for_instance(net_idx=6,
requested_networks=requested_networks)
@ -1781,11 +1815,14 @@ class TestNeutronv2(TestNeutronv2Base):
objects.NetworkRequest(port_id=self.port_data1[0]['id']),
objects.NetworkRequest(network_id=uuids.my_netid2),
objects.NetworkRequest(port_id=self.port_data3[0]['id'])])
neutronapi.get_client(mox.IgnoreArg()).MultipleTimes().AndReturn(
self.moxed_client)
self._allocate_for_instance(net_idx=7,
requested_networks=requested_networks)
def test_validate_networks_not_specified(self):
requested_networks = objects.NetworkRequestList(objects=[])
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
self.moxed_client.list_networks(
tenant_id=self.context.project_id,
shared=False).AndReturn(
@ -1809,11 +1846,10 @@ class TestNeutronv2(TestNeutronv2Base):
port_id=uuids.portid_1)])
PortNotFound = exceptions.PortNotFoundClient()
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
self.moxed_client.show_port(requested_networks[0].port_id).AndRaise(
PortNotFound)
self.mox.ReplayAll()
# Expected call from setUp.
neutronapi.get_client(None)
api = neutronapi.API()
self.assertRaises(exception.PortNotFound,
api.validate_networks,
@ -1830,11 +1866,10 @@ class TestNeutronv2(TestNeutronv2Base):
port_id=fake_port_id)])
NeutronNotFound = exceptions.NeutronClientException(status_code=0)
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
self.moxed_client.show_port(requested_networks[0].port_id).AndRaise(
NeutronNotFound)
self.mox.ReplayAll()
# Expected call from setUp.
neutronapi.get_client(None)
api = neutronapi.API()
exc = self.assertRaises(exception.NovaException,
api.validate_networks,
@ -1847,6 +1882,7 @@ class TestNeutronv2(TestNeutronv2Base):
def test_validate_networks_port_in_use(self):
requested_networks = objects.NetworkRequestList(
objects=[objects.NetworkRequest(port_id=self.port_data3[0]['id'])])
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
self.moxed_client.show_port(self.port_data3[0]['id']).\
AndReturn({'port': self.port_data3[0]})
@ -1864,6 +1900,7 @@ class TestNeutronv2(TestNeutronv2Base):
requested_networks = objects.NetworkRequestList(
objects=[objects.NetworkRequest(port_id=port_a['id'])])
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
self.moxed_client.show_port(port_a['id']).AndReturn({'port': port_a})
self.mox.ReplayAll()
@ -1877,6 +1914,7 @@ class TestNeutronv2(TestNeutronv2Base):
requested_networks = objects.NetworkRequestList(
objects=[objects.NetworkRequest(network_id='his_netid4')])
ids = ['his_netid4']
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
self.moxed_client.list_networks(
id=mox.SameElementsAs(ids)).AndReturn(
{'networks': self.nets4})
@ -1901,6 +1939,7 @@ class TestNeutronv2(TestNeutronv2Base):
requested_networks = objects.NetworkRequestList(
objects=[objects.NetworkRequest(port_id=port_a['id']),
objects.NetworkRequest(port_id=port_b['id'])])
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
self.moxed_client.show_port(port_a['id']).AndReturn(
{'port': port_a})
self.moxed_client.show_port(port_b['id']).AndReturn(
@ -1924,6 +1963,7 @@ class TestNeutronv2(TestNeutronv2Base):
requested_networks = objects.NetworkRequestList(
objects=[objects.NetworkRequest(port_id=port_a['id']),
objects.NetworkRequest(port_id=port_b['id'])])
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
self.moxed_client.show_port(port_a['id']).AndReturn({'port': port_a})
self.moxed_client.show_port(port_b['id']).AndReturn({'port': port_b})
self.mox.ReplayAll()
@ -1939,6 +1979,7 @@ class TestNeutronv2(TestNeutronv2Base):
objects=[objects.NetworkRequest(network_id=uuids.my_netid1),
objects.NetworkRequest(network_id=uuids.my_netid2)])
ids = [uuids.my_netid1, uuids.my_netid2]
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
self.moxed_client.list_networks(
id=mox.SameElementsAs(ids)).AndReturn(
{'networks': self.nets2})
@ -1963,6 +2004,7 @@ class TestNeutronv2(TestNeutronv2Base):
requested_networks = objects.NetworkRequestList(
objects=[objects.NetworkRequest(network_id=uuids.my_netid1),
objects.NetworkRequest(port_id=port_b['id'])])
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
self.moxed_client.show_port(port_b['id']).AndReturn({'port': port_b})
ids = [uuids.my_netid1]
self.moxed_client.list_networks(
@ -1988,6 +2030,7 @@ class TestNeutronv2(TestNeutronv2Base):
port_b['device_owner'] = None
requested_networks = objects.NetworkRequestList(
objects=[objects.NetworkRequest(port_id=port_b['id'])])
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
self.moxed_client.show_port(port_b['id']).AndReturn({'port': port_b})
self.mox.ReplayAll()
api = neutronapi.API()
@ -2003,6 +2046,7 @@ class TestNeutronv2(TestNeutronv2Base):
objects=[objects.NetworkRequest(network_id=uuids.my_netid1),
objects.NetworkRequest(network_id=uuids.my_netid2)])
ids = [uuids.my_netid1, uuids.my_netid2]
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
self.moxed_client.list_networks(
id=mox.SameElementsAs(ids)).AndReturn(
{'networks': self.nets2})
@ -2026,6 +2070,7 @@ class TestNeutronv2(TestNeutronv2Base):
objects=[objects.NetworkRequest(network_id=uuids.my_netid1),
objects.NetworkRequest(network_id=uuids.my_netid2)])
ids = [uuids.my_netid1, uuids.my_netid2]
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
self.moxed_client.list_networks(
id=mox.SameElementsAs(ids)).AndReturn(
{'networks': self.nets2})
@ -2051,6 +2096,7 @@ class TestNeutronv2(TestNeutronv2Base):
requested_networks = objects.NetworkRequestList(
objects=[objects.NetworkRequest(port_id=port_a['id']),
objects.NetworkRequest(port_id=port_b['id'])])
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
self.moxed_client.show_port(port_a['id']).AndReturn({'port': port_a})
self.moxed_client.show_port(port_b['id']).AndReturn({'port': port_b})
@ -2065,6 +2111,7 @@ class TestNeutronv2(TestNeutronv2Base):
if port_data is None:
port_data = self.port_data2
address = self.port_address
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
self.moxed_client.list_ports(
fixed_ips=MyComparator('ip_address=%s' % address)).AndReturn(
{'ports': port_data})
@ -2094,6 +2141,7 @@ class TestNeutronv2(TestNeutronv2Base):
def _get_available_networks(self, prv_nets, pub_nets,
req_ids=None, context=None):
api = neutronapi.API()
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
nets = prv_nets + pub_nets
if req_ids:
mox_list_params = {'id': req_ids}
@ -2140,6 +2188,7 @@ class TestNeutronv2(TestNeutronv2Base):
def test_get_floating_ip_pools(self):
api = neutronapi.API()
search_opts = {'router:external': True}
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
self.moxed_client.list_networks(**search_opts).\
AndReturn({'networks': [self.fip_pool, self.fip_pool_nova]})
self.mox.ReplayAll()
@ -2175,6 +2224,7 @@ class TestNeutronv2(TestNeutronv2Base):
fip_id = fip_data['id']
net_id = fip_data['floating_network_id']
address = fip_data['floating_ip_address']
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
if by_address:
self.moxed_client.list_floatingips(floating_ip_address=address).\
AndReturn({'floatingips': [fip_data]})
@ -2213,6 +2263,7 @@ class TestNeutronv2(TestNeutronv2Base):
def test_get_floating_ip_by_address_not_found(self):
api = neutronapi.API()
address = self.fip_unassociated['floating_ip_address']
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
self.moxed_client.list_floatingips(floating_ip_address=address).\
AndReturn({'floatingips': []})
self.mox.ReplayAll()
@ -2224,6 +2275,7 @@ class TestNeutronv2(TestNeutronv2Base):
api = neutronapi.API()
NeutronNotFound = exceptions.NeutronClientException(status_code=404)
floating_ip_id = self.fip_unassociated['id']
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
self.moxed_client.show_floatingip(floating_ip_id).\
AndRaise(NeutronNotFound)
self.mox.ReplayAll()
@ -2235,6 +2287,7 @@ class TestNeutronv2(TestNeutronv2Base):
api = neutronapi.API()
NeutronNotFound = exceptions.NeutronClientException(status_code=0)
floating_ip_id = self.fip_unassociated['id']
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
self.moxed_client.show_floatingip(floating_ip_id).\
AndRaise(NeutronNotFound)
self.mox.ReplayAll()
@ -2245,6 +2298,7 @@ class TestNeutronv2(TestNeutronv2Base):
def test_get_floating_ip_by_address_multiple_found(self):
api = neutronapi.API()
address = self.fip_unassociated['floating_ip_address']
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
self.moxed_client.list_floatingips(floating_ip_address=address).\
AndReturn({'floatingips': [self.fip_unassociated] * 2})
self.mox.ReplayAll()
@ -2255,6 +2309,7 @@ class TestNeutronv2(TestNeutronv2Base):
def test_get_floating_ips_by_project(self):
api = neutronapi.API()
project_id = self.context.project_id
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
self.moxed_client.list_floatingips(tenant_id=project_id).\
AndReturn({'floatingips': [self.fip_unassociated,
self.fip_associated]})
@ -2276,6 +2331,7 @@ class TestNeutronv2(TestNeutronv2Base):
associated=False):
api = neutronapi.API()
address = fip_data['floating_ip_address']
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
self.moxed_client.list_floatingips(floating_ip_address=address).\
AndReturn({'floatingips': [fip_data]})
if associated:
@ -2304,6 +2360,7 @@ class TestNeutronv2(TestNeutronv2Base):
search_opts = {'router:external': True,
'fields': 'id',
'name': pool_name}
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
self.moxed_client.list_networks(**search_opts).\
AndReturn({'networks': [self.fip_pool]})
self.moxed_client.create_floatingip(
@ -2320,6 +2377,7 @@ class TestNeutronv2(TestNeutronv2Base):
search_opts = {'router:external': True,
'fields': 'id',
'name': pool_name}
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
self.moxed_client.list_networks(**search_opts).\
AndReturn({'networks': [self.fip_pool]})
self.moxed_client.create_floatingip(
@ -2336,6 +2394,7 @@ class TestNeutronv2(TestNeutronv2Base):
search_opts = {'router:external': True,
'fields': 'id',
'name': pool_name}
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
self.moxed_client.list_networks(**search_opts).\
AndReturn({'networks': [self.fip_pool]})
self.moxed_client.create_floatingip(
@ -2351,6 +2410,7 @@ class TestNeutronv2(TestNeutronv2Base):
search_opts = {'router:external': True,
'fields': 'id',
'id': pool_id}
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
self.moxed_client.list_networks(**search_opts).\
AndReturn({'networks': [self.fip_pool]})
self.moxed_client.create_floatingip(
@ -2367,6 +2427,7 @@ class TestNeutronv2(TestNeutronv2Base):
search_opts = {'router:external': True,
'fields': 'id',
'name': pool_name}
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
self.moxed_client.list_networks(**search_opts).\
AndReturn({'networks': [self.fip_pool_nova]})
self.moxed_client.create_floatingip(
@ -2380,7 +2441,7 @@ class TestNeutronv2(TestNeutronv2Base):
api = neutronapi.API()
address = self.fip_unassociated['floating_ip_address']
fip_id = self.fip_unassociated['id']
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
self.moxed_client.list_floatingips(floating_ip_address=address).\
AndReturn({'floatingips': [self.fip_unassociated]})
self.moxed_client.delete_floatingip(fip_id)
@ -2392,7 +2453,7 @@ class TestNeutronv2(TestNeutronv2Base):
address = self.fip_unassociated['floating_ip_address']
fip_id = self.fip_unassociated['id']
floating_ip = {'address': address}
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
self.moxed_client.list_floatingips(floating_ip_address=address).\
AndReturn({'floatingips': [self.fip_unassociated]})
self.moxed_client.delete_floatingip(fip_id)
@ -2406,7 +2467,7 @@ class TestNeutronv2(TestNeutronv2Base):
fip_id = self.fip_unassociated['id']
floating_ip = {'address': address}
instance = self._fake_instance_object(self.instance)
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
self.moxed_client.list_floatingips(floating_ip_address=address).\
AndReturn({'floatingips': [self.fip_unassociated]})
self.moxed_client.delete_floatingip(fip_id)
@ -2418,7 +2479,7 @@ class TestNeutronv2(TestNeutronv2Base):
def test_release_floating_ip_associated(self):
api = neutronapi.API()
address = self.fip_associated['floating_ip_address']
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
self.moxed_client.list_floatingips(floating_ip_address=address).\
AndReturn({'floatingips': [self.fip_associated]})
self.mox.ReplayAll()
@ -2446,6 +2507,8 @@ class TestNeutronv2(TestNeutronv2Base):
search_opts = {'device_owner': 'compute:nova',
'device_id': instance.uuid}
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
self.moxed_client.list_ports(**search_opts).\
AndReturn({'ports': [self.port_data2[1]]})
self.moxed_client.list_floatingips(floating_ip_address=address).\
@ -2468,6 +2531,7 @@ class TestNeutronv2(TestNeutronv2Base):
search_opts = {'device_owner': 'compute:nova',
'device_id': self.instance2['uuid']}
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
self.moxed_client.list_ports(**search_opts).\
AndReturn({'ports': [self.port_data2[0]]})
self.moxed_client.list_floatingips(floating_ip_address=address).\
@ -2496,6 +2560,7 @@ class TestNeutronv2(TestNeutronv2Base):
search_opts = {'device_owner': 'compute:nova',
'device_id': self.instance['uuid']}
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
self.moxed_client.list_ports(**search_opts).\
AndReturn({'ports': [self.port_data2[0]]})
@ -2509,7 +2574,7 @@ class TestNeutronv2(TestNeutronv2Base):
api = neutronapi.API()
address = self.fip_associated['floating_ip_address']
fip_id = self.fip_associated['id']
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
self.moxed_client.list_floatingips(floating_ip_address=address).\
AndReturn({'floatingips': [self.fip_associated]})
self.moxed_client.update_floatingip(
@ -2525,6 +2590,7 @@ class TestNeutronv2(TestNeutronv2Base):
self._setup_mock_for_refresh_cache(api, [instance])
network_id = uuids.my_netid1
search_opts = {'network_id': network_id}
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
self.moxed_client.list_subnets(
**search_opts).AndReturn({'subnets': self.subnet_data_n})
@ -2558,6 +2624,7 @@ class TestNeutronv2(TestNeutronv2Base):
search_opts = {'device_id': self.instance['uuid'],
'device_owner': zone,
'fixed_ips': 'ip_address=%s' % address}
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
self.moxed_client.list_ports(
**search_opts).AndReturn({'ports': self.port_data1})
port_req_body = {
@ -2577,6 +2644,7 @@ class TestNeutronv2(TestNeutronv2Base):
def test_list_floating_ips_without_l3_support(self):
api = neutronapi.API()
NeutronNotFound = exceptions.NotFound()
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
self.moxed_client.list_floatingips(
fixed_ip_address='1.1.1.1', port_id=1).AndRaise(NeutronNotFound)
self.mox.ReplayAll()
@ -2592,6 +2660,7 @@ class TestNeutronv2(TestNeutronv2Base):
'id': 'port-id',
}
api = neutronapi.API()
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
self.mox.StubOutWithMock(api, '_get_floating_ips_by_fixed_and_port')
api._get_floating_ips_by_fixed_and_port(
self.moxed_client, '1.1.1.1', 'port-id').AndReturn(
@ -2614,10 +2683,10 @@ class TestNeutronv2(TestNeutronv2Base):
fake_ips = [model.IP(x['ip_address']) for x in fake_port['fixed_ips']]
api = neutronapi.API()
self.mox.StubOutWithMock(api, '_get_subnets_from_port')
api._get_subnets_from_port(self.context, fake_port).AndReturn(
api._get_subnets_from_port(
self.context, fake_port, None).AndReturn(
[fake_subnet])
self.mox.ReplayAll()
neutronapi.get_client(uuids.fake)
subnets = api._nw_info_get_subnets(self.context, fake_port, fake_ips)
self.assertEqual(1, len(subnets))
self.assertEqual(1, len(subnets[0]['ips']))
@ -2634,6 +2703,7 @@ class TestNeutronv2(TestNeutronv2Base):
fake_nets = [{'id': 'net-id', 'name': 'foo', 'tenant_id': 'tenant',
'mtu': 9000}]
api = neutronapi.API()
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
self.mox.ReplayAll()
neutronapi.get_client(uuids.fake)
net, iid = api._nw_info_build_network(fake_port, fake_nets,
@ -2688,6 +2758,7 @@ class TestNeutronv2(TestNeutronv2Base):
fake_subnets = [model.Subnet(cidr='1.0.0.0/8')]
fake_nets = [{'id': 'net-id2', 'name': 'foo', 'tenant_id': 'tenant'}]
api = neutronapi.API()
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
self.mox.ReplayAll()
neutronapi.get_client(uuids.fake)
net, iid = api._nw_info_build_network(fake_port, fake_nets,
@ -2709,6 +2780,7 @@ class TestNeutronv2(TestNeutronv2Base):
fake_subnets = [model.Subnet(cidr='1.0.0.0/8')]
fake_nets = [{'id': 'net-id', 'name': 'foo', 'tenant_id': 'tenant'}]
api = neutronapi.API()
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
self.mox.ReplayAll()
neutronapi.get_client(uuids.fake)
net, iid = api._nw_info_build_network(fake_port, fake_nets,
@ -2737,6 +2809,7 @@ class TestNeutronv2(TestNeutronv2Base):
fake_subnets = [model.Subnet(cidr='1.0.0.0/8')]
fake_nets = [{'id': 'net-id', 'name': 'foo', 'tenant_id': 'tenant'}]
api = neutronapi.API()
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
self.mox.ReplayAll()
neutronapi.get_client(uuids.fake)
net, iid = api._nw_info_build_network(fake_port, fake_nets,
@ -2852,8 +2925,8 @@ class TestNeutronv2(TestNeutronv2Base):
'tenant_id': uuids.fake,
}
]
neutronapi.get_client(mox.IgnoreArg(), admin=True).MultipleTimes(
).AndReturn(self.moxed_client)
neutronapi.get_client(mox.IgnoreArg(), admin=True).AndReturn(
self.moxed_client)
self.moxed_client.list_ports(
tenant_id=uuids.fake, device_id=uuids.instance).AndReturn(
{'ports': fake_ports})
@ -2867,13 +2940,13 @@ class TestNeutronv2(TestNeutronv2Base):
self.moxed_client, '1.1.1.1', requested_port['id']).AndReturn(
[{'floating_ip_address': '10.0.0.1'}])
for requested_port in requested_ports:
api._get_subnets_from_port(self.context, requested_port
).AndReturn(fake_subnets)
api._get_subnets_from_port(self.context, requested_port,
self.moxed_client).AndReturn(
fake_subnets)
self.mox.StubOutWithMock(api, '_get_preexisting_port_ids')
api._get_preexisting_port_ids(fake_inst).AndReturn(['port5'])
self.mox.ReplayAll()
neutronapi.get_client(uuids.fake)
fake_inst.info_cache = objects.InstanceInfoCache.new(
self.context, uuids.instance)
fake_inst.info_cache.network_info = model.NetworkInfo.hydrate([])
@ -2964,8 +3037,8 @@ class TestNeutronv2(TestNeutronv2Base):
]
fake_subnets = [model.Subnet(cidr='1.0.0.0/8')]
neutronapi.get_client(mox.IgnoreArg(), admin=True).MultipleTimes(
).AndReturn(self.moxed_client)
neutronapi.get_client(mox.IgnoreArg(), admin=True).AndReturn(
self.moxed_client)
self.moxed_client.list_ports(
tenant_id=uuids.fake, device_id=uuids.instance).AndReturn(
{'ports': fake_ports})
@ -2977,7 +3050,6 @@ class TestNeutronv2(TestNeutronv2Base):
mock_nw_info_get_subnets.return_value = fake_subnets
self.mox.ReplayAll()
neutronapi.get_client(uuids.fake)
nw_infos = api._build_network_info_model(
self.context, fake_inst)
@ -2991,7 +3063,7 @@ class TestNeutronv2(TestNeutronv2Base):
subnet_data1[0]['host_routes'] = [
{'destination': '192.168.0.0/24', 'nexthop': '1.0.0.10'}
]
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
self.moxed_client.list_subnets(
id=[port_data['fixed_ips'][0]['subnet_id']]
).AndReturn({'subnets': subnet_data1})
@ -3011,6 +3083,7 @@ class TestNeutronv2(TestNeutronv2Base):
def test_get_all_empty_list_networks(self):
api = neutronapi.API()
neutronapi.get_client(mox.IgnoreArg()).AndReturn(self.moxed_client)
self.moxed_client.list_networks().AndReturn({'networks': []})
self.mox.ReplayAll()
networks = api.get_all(self.context)