Fix with statement deprecation warnings

Refactor nested statements for multiple context managers to
remove deprecation warnings. Additionally, refactor imports
to use current exception libraries.

JIRA:NCP-2066
Change-Id: Ie3953d109c13d03bf5028d54dc3f53d8d3649315
This commit is contained in:
Kyle Haley 2016-08-31 18:10:12 -07:00
parent 8d49a9ef6b
commit 391fd5bb7c
23 changed files with 641 additions and 748 deletions

View File

@ -76,9 +76,8 @@ class QuarkSubnetsPaginationFunctionalTest(BaseFunctionalTest):
class QuarkPortsPaginationFunctionalTest(BaseFunctionalTest): class QuarkPortsPaginationFunctionalTest(BaseFunctionalTest):
@contextlib.contextmanager @contextlib.contextmanager
def _stubs(self, network_info, subnet_info): def _stubs(self, network_info, subnet_info):
with contextlib.nested( with mock.patch("neutron.common.rpc.get_notifier"), \
mock.patch("neutron.common.rpc.get_notifier"), mock.patch("neutron.quota.QUOTAS.limit_check"):
mock.patch("neutron.quota.QUOTAS.limit_check")):
self.context.is_admin = True self.context.is_admin = True
net = network_api.create_network(self.context, network_info) net = network_api.create_network(self.context, network_info)
mac = {'mac_address_range': dict(cidr="AA:BB:CC")} mac = {'mac_address_range': dict(cidr="AA:BB:CC")}

View File

@ -142,9 +142,8 @@ class QuarkTestReserveIPAdminWithPorts(BaseFunctionalTest):
@contextlib.contextmanager @contextlib.contextmanager
def _stubs(self): def _stubs(self):
with contextlib.nested( with mock.patch("neutron.common.rpc.get_notifier"), \
mock.patch("neutron.common.rpc.get_notifier"), mock.patch("neutron.quota.QUOTAS.limit_check"):
mock.patch("neutron.quota.QUOTAS.limit_check")):
mac = {'mac_address_range': dict(cidr="AA:BB:CC")} mac = {'mac_address_range': dict(cidr="AA:BB:CC")}
self.context.is_admin = True self.context.is_admin = True
macrng_api.create_mac_address_range(self.context, mac) macrng_api.create_mac_address_range(self.context, mac)

View File

@ -19,6 +19,7 @@ import netaddr
import contextlib import contextlib
from neutron.common import exceptions as q_exc from neutron.common import exceptions as q_exc
from neutron_lib import exceptions as n_exc
from quark.db import api as db_api from quark.db import api as db_api
import quark.plugin_modules.mac_address_ranges as macrng_api import quark.plugin_modules.mac_address_ranges as macrng_api
@ -53,9 +54,8 @@ class QuarkFindPortsSorted(BaseFunctionalTest):
class QuarkCreatePortSatisfyIpam(BaseFunctionalTest): class QuarkCreatePortSatisfyIpam(BaseFunctionalTest):
@contextlib.contextmanager @contextlib.contextmanager
def _stubs(self, network_info, subnet_v4_info, subnet_v6_info=None): def _stubs(self, network_info, subnet_v4_info, subnet_v6_info=None):
with contextlib.nested( with mock.patch("neutron.common.rpc.get_notifier"), \
mock.patch("neutron.common.rpc.get_notifier"), mock.patch("neutron.quota.QUOTAS.limit_check"):
mock.patch("neutron.quota.QUOTAS.limit_check")):
self.context.is_admin = True self.context.is_admin = True
net = network_api.create_network(self.context, network_info) net = network_api.create_network(self.context, network_info)
mac = {'mac_address_range': dict(cidr="AA:BB:CC")} mac = {'mac_address_range': dict(cidr="AA:BB:CC")}
@ -130,16 +130,15 @@ class QuarkCreatePortSatisfyIpam(BaseFunctionalTest):
with self._stubs(network, subnet_v4_info) as ( with self._stubs(network, subnet_v4_info) as (
net, sub_v4, sub_v6): net, sub_v4, sub_v6):
ip = "192.168.1.50" ip = "192.168.1.50"
with self.assertRaises(q_exc.IpAddressGenerationFailure): with self.assertRaises(n_exc.IpAddressGenerationFailure):
port_api.create_port(self.context, _make_body_only_v4(ip)) port_api.create_port(self.context, _make_body_only_v4(ip))
class QuarkCreatePortWithIpNotMandatory(BaseFunctionalTest): class QuarkCreatePortWithIpNotMandatory(BaseFunctionalTest):
@contextlib.contextmanager @contextlib.contextmanager
def _stubs(self, network_info, subnet_v4_infos, subnet_v6_info=None): def _stubs(self, network_info, subnet_v4_infos, subnet_v6_info=None):
with contextlib.nested( with mock.patch("neutron.common.rpc.get_notifier"), \
mock.patch("neutron.common.rpc.get_notifier"), mock.patch("neutron.quota.QUOTAS.limit_check"):
mock.patch("neutron.quota.QUOTAS.limit_check")):
self.context.is_admin = True self.context.is_admin = True
net = network_api.create_network(self.context, network_info) net = network_api.create_network(self.context, network_info)
mac = {'mac_address_range': dict(cidr="AA:BB:CC")} mac = {'mac_address_range': dict(cidr="AA:BB:CC")}
@ -238,9 +237,8 @@ class QuarkCreatePortWithIpNotMandatory(BaseFunctionalTest):
class QuarkCreatePortWithForbiddenMacRange(BaseFunctionalTest): class QuarkCreatePortWithForbiddenMacRange(BaseFunctionalTest):
@contextlib.contextmanager @contextlib.contextmanager
def _stubs(self, network_info, subnet_v4_infos, subnet_v6_info=None): def _stubs(self, network_info, subnet_v4_infos, subnet_v6_info=None):
with contextlib.nested( with mock.patch("neutron.common.rpc.get_notifier"), \
mock.patch("neutron.common.rpc.get_notifier"), mock.patch("neutron.quota.QUOTAS.limit_check"):
mock.patch("neutron.quota.QUOTAS.limit_check")):
self.context.is_admin = True self.context.is_admin = True
net = network_api.create_network(self.context, network_info) net = network_api.create_network(self.context, network_info)
mac = {'mac_address_range': dict(cidr="AA:BB:CC", do_not_use=True)} mac = {'mac_address_range': dict(cidr="AA:BB:CC", do_not_use=True)}
@ -353,9 +351,8 @@ class QuarkPortFixedIPOperations(BaseFunctionalTest):
@contextlib.contextmanager @contextlib.contextmanager
def _stubs(self, network_info, subnet_info): def _stubs(self, network_info, subnet_info):
with contextlib.nested( with mock.patch("neutron.common.rpc.get_notifier"), \
mock.patch("neutron.common.rpc.get_notifier"), mock.patch("neutron.quota.QUOTAS.limit_check"):
mock.patch("neutron.quota.QUOTAS.limit_check")):
mac = {'mac_address_range': dict(cidr="AA:BB:CC")} mac = {'mac_address_range': dict(cidr="AA:BB:CC")}
self.context.is_admin = True self.context.is_admin = True
macrng_api.create_mac_address_range(self.context, mac) macrng_api.create_mac_address_range(self.context, mac)
@ -516,9 +513,8 @@ class QuarkAdvancedServiceCreatePort(BaseFunctionalTest):
@contextlib.contextmanager @contextlib.contextmanager
def _stubs(self, network_info, subnet_info): def _stubs(self, network_info, subnet_info):
with contextlib.nested( with mock.patch("neutron.common.rpc.get_notifier"), \
mock.patch("neutron.common.rpc.get_notifier"), mock.patch("neutron.quota.QUOTAS.limit_check"):
mock.patch("neutron.quota.QUOTAS.limit_check")):
mac = {'mac_address_range': dict(cidr="AA:BB:CC")} mac = {'mac_address_range': dict(cidr="AA:BB:CC")}
self.context.is_admin = True self.context.is_admin = True
macrng_api.create_mac_address_range(self.context, mac) macrng_api.create_mac_address_range(self.context, mac)
@ -549,7 +545,7 @@ class QuarkAdvancedServiceCreatePort(BaseFunctionalTest):
'tenant_id': 'someoneelse'}} 'tenant_id': 'someoneelse'}}
self.context.is_admin = True self.context.is_admin = True
self.context.is_advsvc = False self.context.is_advsvc = False
self.assertRaises(q_exc.NotAuthorized, self.assertRaises(n_exc.NotAuthorized,
port_api.create_port, self.context, port_info) port_api.create_port, self.context, port_info)
def test_cant_create_port_without_admin(self): def test_cant_create_port_without_admin(self):
@ -562,5 +558,5 @@ class QuarkAdvancedServiceCreatePort(BaseFunctionalTest):
# check, quark will first attempt to retrieve the network but # check, quark will first attempt to retrieve the network but
# since networks are scoped by tenant when it is not an admin, # since networks are scoped by tenant when it is not an admin,
# it will not be found # it will not be found
self.assertRaises(q_exc.NetworkNotFound, self.assertRaises(n_exc.NetworkNotFound,
port_api.create_port, self.context, port_info) port_api.create_port, self.context, port_info)

View File

@ -35,7 +35,7 @@ class QuarkCreateRoutes(BaseFunctionalTest):
@contextlib.contextmanager @contextlib.contextmanager
def _stubs(self, network, subnet, ip_policy): def _stubs(self, network, subnet, ip_policy):
self.ipam = quark.ipam.QuarkIpamANY() self.ipam = quark.ipam.QuarkIpamANY()
with contextlib.nested(mock.patch("neutron.common.rpc.get_notifier")): with mock.patch("neutron.common.rpc.get_notifier"):
net = network_api.create_network(self.context, network) net = network_api.create_network(self.context, network)
subnet['subnet']['network_id'] = net['id'] subnet['subnet']['network_id'] = net['id']
sub1 = subnet_api.create_subnet(self.context, subnet) sub1 = subnet_api.create_subnet(self.context, subnet)

View File

@ -77,12 +77,10 @@ class SecurityGroupsAsyncUpdateTests(BaseFunctionalTest):
cls = 'QuarkSGAsyncProcessClient' cls = 'QuarkSGAsyncProcessClient'
mod_path = 'quark.worker_plugins.sg_update_worker.%s' % cls mod_path = 'quark.worker_plugins.sg_update_worker.%s' % cls
job_path = 'quark.plugin_modules.jobs' job_path = 'quark.plugin_modules.jobs'
with contextlib.nested( with mock.patch("neutron.common.rpc.get_notifier"), \
mock.patch("neutron.common.rpc.get_notifier"), mock.patch("neutron.quota.QUOTAS.limit_check"), \
mock.patch("neutron.quota.QUOTAS.limit_check"), mock.patch("%s.add_job_to_context" % job_path), \
mock.patch("%s.add_job_to_context" % job_path), mock.patch("%s.start_update" % mod_path) as update:
mock.patch("%s.start_update" % mod_path)) as \
(notifier, limit_check, addjob, update):
self.context.is_admin = True self.context.is_admin = True
net = network_api.create_network(self.context, network_info) net = network_api.create_network(self.context, network_info)
mac = {'mac_address_range': dict(cidr="AA:BB:CC")} mac = {'mac_address_range': dict(cidr="AA:BB:CC")}

View File

@ -73,9 +73,8 @@ class QuarkSharedIPs(BaseFunctionalTest):
@contextlib.contextmanager @contextlib.contextmanager
def _stubs(self, network_info, subnet_info, ports_info): def _stubs(self, network_info, subnet_info, ports_info):
self.ipam = quark.ipam.QuarkIpamANY() self.ipam = quark.ipam.QuarkIpamANY()
with contextlib.nested( with mock.patch("neutron.common.rpc.get_notifier"), \
mock.patch("neutron.common.rpc.get_notifier"), mock.patch("neutron.quota.QUOTAS.limit_check"):
mock.patch("neutron.quota.QUOTAS.limit_check")):
net = network_api.create_network(self.admin_context, network_info) net = network_api.create_network(self.admin_context, network_info)
mac = {'mac_address_range': dict(cidr="AA:BB:CC")} mac = {'mac_address_range': dict(cidr="AA:BB:CC")}
macrng_api.create_mac_address_range(self.admin_context, mac) macrng_api.create_mac_address_range(self.admin_context, mac)
@ -481,8 +480,7 @@ class QuarkSharedIPsQuotaCheck(BaseFunctionalTest):
@contextlib.contextmanager @contextlib.contextmanager
def _stubs(self, network_info, subnet_info, ports_info): def _stubs(self, network_info, subnet_info, ports_info):
self.ipam = quark.ipam.QuarkIpamANY() self.ipam = quark.ipam.QuarkIpamANY()
with contextlib.nested( with mock.patch("neutron.common.rpc.get_notifier"):
mock.patch("neutron.common.rpc.get_notifier")):
self.context.is_admin = True self.context.is_admin = True
net = network_api.create_network(self.context, network_info) net = network_api.create_network(self.context, network_info)
mac = {'mac_address_range': dict(cidr="AA:BB:CC")} mac = {'mac_address_range': dict(cidr="AA:BB:CC")}
@ -667,9 +665,8 @@ class QuarkTestSharedIpAddressesQuota(BaseFunctionalTest):
@contextlib.contextmanager @contextlib.contextmanager
def _stubs(self, network_info, subnet_info, ports_info): def _stubs(self, network_info, subnet_info, ports_info):
self.ipam = quark.ipam.QuarkIpamANY() self.ipam = quark.ipam.QuarkIpamANY()
with contextlib.nested( with mock.patch("neutron.common.rpc.get_notifier"), \
mock.patch("neutron.common.rpc.get_notifier"), mock.patch("neutron.quota.QUOTAS.limit_check"):
mock.patch("neutron.quota.QUOTAS.limit_check")):
net = network_api.create_network(self.admin_context, network_info) net = network_api.create_network(self.admin_context, network_info)
mac = {'mac_address_range': dict(cidr="AA:BB:CC")} mac = {'mac_address_range': dict(cidr="AA:BB:CC")}
macrng_api.create_mac_address_range(self.admin_context, mac) macrng_api.create_mac_address_range(self.admin_context, mac)

View File

@ -62,7 +62,7 @@ class QuarkGetSubnetsFromPlugin(BaseFunctionalTest):
@contextlib.contextmanager @contextlib.contextmanager
def _stubs(self, network, subnet): def _stubs(self, network, subnet):
self.ipam = quark.ipam.QuarkIpamANY() self.ipam = quark.ipam.QuarkIpamANY()
with contextlib.nested(mock.patch("neutron.common.rpc.get_notifier")): with mock.patch("neutron.common.rpc.get_notifier"):
net = network_api.create_network(self.context, network) net = network_api.create_network(self.context, network)
subnet['subnet']['network_id'] = net['id'] subnet['subnet']['network_id'] = net['id']
sub1 = subnet_api.create_subnet(self.context, subnet) sub1 = subnet_api.create_subnet(self.context, subnet)
@ -93,7 +93,7 @@ class QuarkCreateSubnets(BaseFunctionalTest):
@contextlib.contextmanager @contextlib.contextmanager
def _stubs(self, network, subnet): def _stubs(self, network, subnet):
self.ipam = quark.ipam.QuarkIpamANY() self.ipam = quark.ipam.QuarkIpamANY()
with contextlib.nested(mock.patch("neutron.common.rpc.get_notifier")): with mock.patch("neutron.common.rpc.get_notifier"):
net = network_api.create_network(self.context, network) net = network_api.create_network(self.context, network)
subnet['subnet']['network_id'] = net['id'] subnet['subnet']['network_id'] = net['id']
sub1 = subnet_api.create_subnet(self.context, subnet) sub1 = subnet_api.create_subnet(self.context, subnet)
@ -227,7 +227,7 @@ class QuarkUpdateSubnets(BaseFunctionalTest):
@contextlib.contextmanager @contextlib.contextmanager
def _stubs(self, network, subnet): def _stubs(self, network, subnet):
self.ipam = quark.ipam.QuarkIpamANY() self.ipam = quark.ipam.QuarkIpamANY()
with contextlib.nested(mock.patch("neutron.common.rpc.get_notifier")): with mock.patch("neutron.common.rpc.get_notifier"):
net = network_api.create_network(self.context, network) net = network_api.create_network(self.context, network)
subnet['subnet']['network_id'] = net['id'] subnet['subnet']['network_id'] = net['id']
sub1 = subnet_api.create_subnet(self.context, subnet) sub1 = subnet_api.create_subnet(self.context, subnet)

View File

@ -82,7 +82,7 @@ class TestQuarkIpamAllocateFromV6Subnet(QuarkIpamBaseFunctionalTest):
@contextlib.contextmanager @contextlib.contextmanager
def _stubs(self, network, subnet, ip_policy): def _stubs(self, network, subnet, ip_policy):
self.ipam = quark.ipam.QuarkIpamANY() self.ipam = quark.ipam.QuarkIpamANY()
with contextlib.nested(mock.patch("neutron.common.rpc.get_notifier")): with mock.patch("neutron.common.rpc.get_notifier"):
net = network_api.create_network(self.context, network) net = network_api.create_network(self.context, network)
subnet['subnet']['network_id'] = net['id'] subnet['subnet']['network_id'] = net['id']
sub = subnet_api.create_subnet(self.context, subnet) sub = subnet_api.create_subnet(self.context, subnet)

View File

@ -18,7 +18,7 @@ import contextlib
import datetime import datetime
import mock import mock
import netaddr import netaddr
from neutron.common import exceptions as ex from neutron_lib import exceptions as n_exc
from quark.billing import IP_ASSOC from quark.billing import IP_ASSOC
from quark.billing import IP_DISASSOC from quark.billing import IP_DISASSOC
@ -36,18 +36,15 @@ class TestRemoveFloatingIPs(test_quark_plugin.TestQuarkPlugin):
flip_model = models.IPAddress() flip_model = models.IPAddress()
flip_model.update(flip) flip_model.update(flip)
with contextlib.nested( with mock.patch("quark.db.api.floating_ip_find") as flip_find, \
mock.patch("quark.db.api.floating_ip_find"), mock.patch("quark.db.api.floating_ip_disassociate_fixed_ip"), \
mock.patch("quark.db.api.floating_ip_disassociate_fixed_ip"), mock.patch("quark.db.api.port_disassociate_ip"), \
mock.patch("quark.db.api.port_disassociate_ip"), mock.patch("quark.db.api.ip_address_deallocate"), \
mock.patch("quark.db.api.ip_address_deallocate"), mock.patch("quark.ipam.QuarkIpam.deallocate_ip_address"), \
mock.patch("quark.ipam.QuarkIpam.deallocate_ip_address"), mock.patch("quark.drivers.unicorn_driver.UnicornDriver"
mock.patch("quark.drivers.unicorn_driver.UnicornDriver" ".remove_floating_ip"), \
".remove_floating_ip"), mock.patch("quark.billing.notify"), \
mock.patch("quark.billing.notify"), mock.patch("quark.billing.build_payload") as build_payload:
mock.patch("quark.billing.build_payload")
) as (flip_find, db_fixed_ip_disassoc, db_port_disassoc, db_dealloc,
mock_dealloc, mock_remove_flip, notify, build_payload):
flip_find.return_value = flip_model flip_find.return_value = flip_model
build_payload.return_value = {'respek': '4reelz'} build_payload.return_value = {'respek': '4reelz'}
yield yield
@ -230,19 +227,18 @@ class TestCreateFloatingIPs(test_quark_plugin.TestQuarkPlugin):
addr.fixed_ips.append(fixed_ip) addr.fixed_ips.append(fixed_ip)
return addr return addr
with contextlib.nested( with mock.patch("quark.db.api.floating_ip_find") as flip_find, \
mock.patch("quark.db.api.floating_ip_find"), mock.patch("quark.db.api.network_find") as net_find, \
mock.patch("quark.db.api.network_find"), mock.patch("quark.db.api.port_find") as port_find, \
mock.patch("quark.db.api.port_find"), mock.patch("quark.ipam.QuarkIpam.allocate_ip_address") as \
mock.patch("quark.ipam.QuarkIpam.allocate_ip_address"), alloc_ip, \
mock.patch("quark.drivers.unicorn_driver.UnicornDriver" mock.patch("quark.drivers.unicorn_driver.UnicornDriver"
".register_floating_ip"), ".register_floating_ip"), \
mock.patch("quark.db.api.port_associate_ip"), mock.patch("quark.db.api.port_associate_ip") as port_assoc, \
mock.patch("quark.db.api.floating_ip_associate_fixed_ip"), mock.patch("quark.db.api.floating_ip_associate_fixed_ip") as \
mock.patch("quark.billing.notify"), fixed_ip_assoc, \
mock.patch("quark.billing.build_payload") mock.patch("quark.billing.notify"), \
) as (flip_find, net_find, port_find, alloc_ip, mock_reg_flip, mock.patch("quark.billing.build_payload") as build_payload:
port_assoc, fixed_ip_assoc, notify, build_payload):
flip_find.return_value = flip_model flip_find.return_value = flip_model
net_find.return_value = net_model net_find.return_value = net_model
port_find.return_value = port_model port_find.return_value = port_model
@ -348,14 +344,14 @@ class TestCreateFloatingIPs(test_quark_plugin.TestQuarkPlugin):
def test_create_without_network_id_fails(self): def test_create_without_network_id_fails(self):
with self._stubs(): with self._stubs():
with self.assertRaises(ex.BadRequest): with self.assertRaises(n_exc.BadRequest):
request = dict(port_id=2, floating_ip_address="10.0.0.1") request = dict(port_id=2, floating_ip_address="10.0.0.1")
self.plugin.create_floatingip(self.context, self.plugin.create_floatingip(self.context,
dict(floatingip=request)) dict(floatingip=request))
def test_create_with_invalid_network_fails(self): def test_create_with_invalid_network_fails(self):
with self._stubs(): with self._stubs():
with self.assertRaises(ex.NetworkNotFound): with self.assertRaises(n_exc.NetworkNotFound):
request = dict(floating_network_id=123, request = dict(floating_network_id=123,
port_id=2, floating_ip_address="10.0.0.1") port_id=2, floating_ip_address="10.0.0.1")
self.plugin.create_floatingip(self.context, self.plugin.create_floatingip(self.context,
@ -366,7 +362,7 @@ class TestCreateFloatingIPs(test_quark_plugin.TestQuarkPlugin):
ipam_strategy="ANY") ipam_strategy="ANY")
with self._stubs(network=network): with self._stubs(network=network):
with self.assertRaises(ex.PortNotFound): with self.assertRaises(n_exc.PortNotFound):
request = dict(floating_network_id=network["id"], request = dict(floating_network_id=network["id"],
port_id=2, floating_ip_address="10.0.0.1") port_id=2, floating_ip_address="10.0.0.1")
self.plugin.create_floatingip(self.context, self.plugin.create_floatingip(self.context,
@ -565,25 +561,24 @@ class TestUpdateFloatingIPs(test_quark_plugin.TestQuarkPlugin):
"""We don't want to notify from tests""" """We don't want to notify from tests"""
pass pass
with contextlib.nested( with mock.patch("quark.db.api.floating_ip_find") as flip_find, \
mock.patch("quark.db.api.floating_ip_find"), mock.patch("quark.db.api.network_find") as net_find, \
mock.patch("quark.db.api.network_find"), mock.patch("quark.db.api.subnet_find") as subnet_find, \
mock.patch("quark.db.api.subnet_find"), mock.patch("quark.db.api.port_find") as port_find, \
mock.patch("quark.db.api.port_find"), mock.patch("quark.drivers.unicorn_driver.UnicornDriver"
mock.patch("quark.drivers.unicorn_driver.UnicornDriver" ".register_floating_ip"), \
".register_floating_ip"), mock.patch("quark.drivers.unicorn_driver.UnicornDriver"
mock.patch("quark.drivers.unicorn_driver.UnicornDriver" ".update_floating_ip"), \
".update_floating_ip"), mock.patch("quark.drivers.unicorn_driver.UnicornDriver"
mock.patch("quark.drivers.unicorn_driver.UnicornDriver" ".remove_floating_ip"), \
".remove_floating_ip"), mock.patch("quark.db.api.port_associate_ip") as port_assoc, \
mock.patch("quark.db.api.port_associate_ip"), mock.patch("quark.db.api.port_disassociate_ip") as \
mock.patch("quark.db.api.port_disassociate_ip"), port_dessoc, \
mock.patch("quark.db.api.floating_ip_associate_fixed_ip"), mock.patch("quark.db.api.floating_ip_associate_fixed_ip") as \
mock.patch("quark.db.api.floating_ip_disassociate_fixed_ip"), flip_assoc, \
mock.patch("quark.billing.notify") mock.patch("quark.db.api.floating_ip_disassociate_fixed_ip") \
) as (flip_find, net_find, subnet_find, port_find, reg_flip, as flip_dessoc, \
update_flip, rem_flip, port_assoc, port_dessoc, flip_assoc, mock.patch("quark.billing.notify") as notify:
flip_dessoc, notify):
flip_find.return_value = flip_model flip_find.return_value = flip_model
net_find.return_value = net_model net_find.return_value = net_model
subnet_find.return_value = subnet_model subnet_find.return_value = subnet_model
@ -698,7 +693,7 @@ class TestUpdateFloatingIPs(test_quark_plugin.TestQuarkPlugin):
address_readable=str(addr)) address_readable=str(addr))
with self._stubs(flip=flip): with self._stubs(flip=flip):
with self.assertRaises(ex.PortNotFound): with self.assertRaises(n_exc.PortNotFound):
content = dict(port_id="123") content = dict(port_id="123")
self.plugin.update_floatingip(self.context, flip["id"], self.plugin.update_floatingip(self.context, flip["id"],
dict(floatingip=content)) dict(floatingip=content))
@ -763,7 +758,7 @@ class TestUpdateFloatingIPs(test_quark_plugin.TestQuarkPlugin):
def test_update_with_missing_port_id_param_should_fail(self): def test_update_with_missing_port_id_param_should_fail(self):
with self._stubs(): with self._stubs():
with self.assertRaises(ex.BadRequest): with self.assertRaises(n_exc.BadRequest):
content = {} content = {}
self.plugin.update_floatingip(self.context, "123", self.plugin.update_floatingip(self.context, "123",
dict(floatingip=content)) dict(floatingip=content))

View File

@ -69,18 +69,14 @@ class TestIpAddresses(test_quark_plugin.TestQuarkPlugin):
def _alloc_ip(context, new_addr, net_id, port_m, *args, **kwargs): def _alloc_ip(context, new_addr, net_id, port_m, *args, **kwargs):
new_addr.extend([addr_model]) new_addr.extend([addr_model])
with contextlib.nested( with mock.patch("quark.db.api.network_find"), \
mock.patch("quark.db.api.network_find"), mock.patch("quark.db.api.port_find") as port_find, \
mock.patch("quark.db.api.port_find"), mock.patch("quark.plugin_modules.ip_addresses.ipam_driver") \
mock.patch( as mock_ipam, \
"quark.plugin_modules.ip_addresses.ipam_driver"), mock.patch("quark.plugin_modules.ip_addresses.db_api"
mock.patch( ".port_associate_ip") as mock_port_associate_ip, \
"quark.plugin_modules.ip_addresses.db_api" mock.patch("quark.plugin_modules.ip_addresses"
".port_associate_ip"), ".validate_and_fetch_segment"):
mock.patch(
"quark.plugin_modules.ip_addresses"
".validate_and_fetch_segment")
) as (net_f, port_find, mock_ipam, mock_port_associate_ip, validate):
port_find.return_value = port_model port_find.return_value = port_model
mock_ipam.allocate_ip_address.side_effect = _alloc_ip mock_ipam.allocate_ip_address.side_effect = _alloc_ip
mock_port_associate_ip.side_effect = _port_associate_stub mock_port_associate_ip.side_effect = _port_associate_stub
@ -433,16 +429,16 @@ class TestQuarkUpdateIPAddress(test_quark_plugin.TestQuarkPlugin):
addr_model.ports = port_models addr_model.ports = port_models
db_mod = "quark.db.api" db_mod = "quark.db.api"
with contextlib.nested( with mock.patch("%s.port_find" % db_mod) as port_find, \
mock.patch("%s.port_find" % db_mod), mock.patch("%s.ip_address_find" % db_mod) as ip_find, \
mock.patch("%s.ip_address_find" % db_mod), mock.patch("%s.port_associate_ip" % db_mod) as \
mock.patch("%s.port_associate_ip" % db_mod), port_associate_ip, \
mock.patch("%s.port_disassociate_ip" % db_mod), mock.patch("%s.port_disassociate_ip" % db_mod) as \
mock.patch("quark.plugin_modules.ip_addresses" port_disassociate_ip, \
".validate_and_fetch_segment"), mock.patch("quark.plugin_modules.ip_addresses"
mock.patch("quark.plugin_modules.ip_addresses.ipam_driver") ".validate_and_fetch_segment"), \
) as (port_find, ip_find, port_associate_ip, mock.patch("quark.plugin_modules.ip_addresses.ipam_driver") \
port_disassociate_ip, val, mock_ipam): as mock_ipam:
port_find.return_value = port_models port_find.return_value = port_models
ip_find.return_value = addr_model ip_find.return_value = addr_model
port_associate_ip.side_effect = _port_associate_stub port_associate_ip.side_effect = _port_associate_stub

View File

@ -81,12 +81,11 @@ class TestQuarkCreateIpPolicies(test_quark_plugin.TestQuarkPlugin):
@contextlib.contextmanager @contextlib.contextmanager
def _stubs(self, ip_policy, subnets=None, nets=None): def _stubs(self, ip_policy, subnets=None, nets=None):
db_mod = "quark.db.api" db_mod = "quark.db.api"
with contextlib.nested( with mock.patch("%s.subnet_find" % db_mod) as subnet_find, \
mock.patch("%s.subnet_find" % db_mod), mock.patch("%s.network_find" % db_mod) as net_find, \
mock.patch("%s.network_find" % db_mod), mock.patch("%s.ip_policy_create" % db_mod) as \
mock.patch("%s.ip_policy_create" % db_mod), ip_policy_create, \
mock.patch("%s.route_find" % db_mod) mock.patch("%s.route_find" % db_mod) as route_find:
) as (subnet_find, net_find, ip_policy_create, route_find):
subnet_find.return_value = subnets if subnets else None subnet_find.return_value = subnets if subnets else None
net_find.return_value = nets if nets else None net_find.return_value = nets if nets else None
ip_policy_create.return_value = ip_policy ip_policy_create.return_value = ip_policy
@ -297,12 +296,11 @@ class TestQuarkUpdateIpPolicies(test_quark_plugin.TestQuarkPlugin):
if not networks: if not networks:
networks = [] networks = []
db_mod = "quark.db.api" db_mod = "quark.db.api"
with contextlib.nested( with mock.patch("%s.ip_policy_find" % db_mod) as ip_policy_find, \
mock.patch("%s.ip_policy_find" % db_mod), mock.patch("%s.subnet_find" % db_mod) as subnet_find, \
mock.patch("%s.subnet_find" % db_mod), mock.patch("%s.network_find" % db_mod) as network_find, \
mock.patch("%s.network_find" % db_mod), mock.patch("%s.ip_policy_update" % db_mod) as \
mock.patch("%s.ip_policy_update" % db_mod), ip_policy_update:
) as (ip_policy_find, subnet_find, network_find, ip_policy_update):
ip_policy_find.return_value = ip_policy ip_policy_find.return_value = ip_policy
subnet_find.return_value = subnets subnet_find.return_value = subnets
network_find.return_value = networks network_find.return_value = networks
@ -440,10 +438,8 @@ class TestQuarkDeleteIpPolicies(test_quark_plugin.TestQuarkPlugin):
@contextlib.contextmanager @contextlib.contextmanager
def _stubs(self, ip_policy): def _stubs(self, ip_policy):
db_mod = "quark.db.api" db_mod = "quark.db.api"
with contextlib.nested( with mock.patch("%s.ip_policy_find" % db_mod) as ip_policy_find, \
mock.patch("%s.ip_policy_find" % db_mod), mock.patch("%s.ip_policy_delete" % db_mod) as ip_policy_delete:
mock.patch("%s.ip_policy_delete" % db_mod),
) as (ip_policy_find, ip_policy_delete):
ip_policy_find.return_value = ip_policy ip_policy_find.return_value = ip_policy
yield ip_policy_find, ip_policy_delete yield ip_policy_find, ip_policy_delete
@ -473,12 +469,10 @@ class TestQuarkUpdatePolicySubnetWithRoutes(test_quark_plugin.TestQuarkPlugin):
def _stubs(self, ip_policy, subnets=None, routes=None): def _stubs(self, ip_policy, subnets=None, routes=None):
subnets = subnets or [] subnets = subnets or []
db_mod = "quark.db.api" db_mod = "quark.db.api"
with contextlib.nested( with mock.patch("%s.ip_policy_find" % db_mod) as ip_policy_find, \
mock.patch("%s.ip_policy_find" % db_mod), mock.patch("%s.subnet_find" % db_mod) as subnet_find, \
mock.patch("%s.subnet_find" % db_mod), mock.patch("%s.route_find" % db_mod) as route_find, \
mock.patch("%s.route_find" % db_mod), mock.patch("%s.ip_policy_update" % db_mod) as ip_policy_update:
mock.patch("%s.ip_policy_update" % db_mod),
) as (ip_policy_find, subnet_find, route_find, ip_policy_update):
ip_policy_find.return_value = ip_policy ip_policy_find.return_value = ip_policy
subnet_find.return_value = subnets subnet_find.return_value = subnets
route_find.return_value = routes route_find.return_value = routes

View File

@ -144,10 +144,9 @@ class TestQuarkDeleteMacAddressRanges(test_quark_plugin.TestQuarkPlugin):
db_mod = "quark.db.api" db_mod = "quark.db.api"
old_context = self.context old_context = self.context
self.context = self.context.elevated() self.context = self.context.elevated()
with contextlib.nested( with mock.patch("%s.mac_address_range_find" % db_mod) as mar_find, \
mock.patch("%s.mac_address_range_find" % db_mod), mock.patch("%s.mac_address_range_delete" % db_mod) as \
mock.patch("%s.mac_address_range_delete" % db_mod), mar_delete:
) as (mar_find, mar_delete):
mar_find.return_value = mac_range mar_find.return_value = mac_range
yield mar_delete yield mar_delete
self.context = old_context self.context = old_context

View File

@ -199,10 +199,8 @@ class TestQuarkUpdateNetwork(test_quark_plugin.TestQuarkPlugin):
net_mod = net.copy() net_mod = net.copy()
db_mod = "quark.db.api" db_mod = "quark.db.api"
with contextlib.nested( with mock.patch("%s.network_find" % db_mod) as net_find, \
mock.patch("%s.network_find" % db_mod), mock.patch("%s.network_update" % db_mod) as net_update:
mock.patch("%s.network_update" % db_mod)
) as (net_find, net_update):
net_find.return_value = net_mod net_find.return_value = net_mod
net_update.return_value = net_mod net_update.return_value = net_mod
yield net_update yield net_update
@ -266,14 +264,12 @@ class TestQuarkDeleteNetwork(test_quark_plugin.TestQuarkPlugin):
db_mod = "quark.db.api" db_mod = "quark.db.api"
strategy_prefix = "quark.network_strategy.JSONStrategy" strategy_prefix = "quark.network_strategy.JSONStrategy"
with contextlib.nested( with mock.patch("%s.network_find" % db_mod) as net_find, \
mock.patch("%s.network_find" % db_mod), mock.patch("%s.network_delete" % db_mod) as net_delete, \
mock.patch("%s.network_delete" % db_mod), mock.patch("quark.drivers.base.BaseDriver.delete_network"), \
mock.patch("quark.drivers.base.BaseDriver.delete_network"), mock.patch("%s.subnet_delete" % db_mod), \
mock.patch("%s.subnet_delete" % db_mod), mock.patch("%s.is_provider_network" % strategy_prefix) as \
mock.patch("%s.is_provider_network" % strategy_prefix) is_provider_network:
) as (net_find, net_delete, driver_net_delete, subnet_del,
is_provider_network):
net_find.return_value = net_mod net_find.return_value = net_mod
is_provider_network.return_value = True is_provider_network.return_value = True
yield net_delete yield net_delete
@ -341,12 +337,10 @@ class TestQuarkCreateNetwork(test_quark_plugin.TestQuarkPlugin):
found_net = models.Network() found_net = models.Network()
db_mod = "quark.db.api" db_mod = "quark.db.api"
with contextlib.nested( with mock.patch("%s.network_create" % db_mod) as net_create, \
mock.patch("%s.network_create" % db_mod), mock.patch("%s.subnet_create" % db_mod) as sub_create, \
mock.patch("%s.subnet_create" % db_mod), mock.patch("quark.drivers.base.BaseDriver.create_network"), \
mock.patch("quark.drivers.base.BaseDriver.create_network"), mock.patch("%s.network_find" % db_mod) as net_find:
mock.patch("%s.network_find" % db_mod)
) as (net_create, sub_create, driver_net_create, net_find):
net_create.return_value = net_mod net_create.return_value = net_mod
sub_create.return_value = subnet_mod sub_create.return_value = subnet_mod
net_find.return_value = found_net net_find.return_value = found_net

View File

@ -57,9 +57,7 @@ class TestQuarkGetPorts(test_quark_plugin.TestQuarkPlugin):
port_model.ip_addresses = addr_models port_model.ip_addresses = addr_models
port_models = port_model port_models = port_model
with contextlib.nested( with mock.patch("quark.db.api.port_find") as port_find:
mock.patch("quark.db.api.port_find")
) as (port_find,):
port_find.return_value = port_models port_find.return_value = port_models
yield yield
@ -237,9 +235,7 @@ class TestQuarkGetPortsProviderSubnetIds(test_quark_plugin.TestQuarkPlugin):
port_model, addr_model) port_model, addr_model)
port_models = port_model port_models = port_model
with contextlib.nested( with mock.patch("quark.db.api.port_find") as port_find:
mock.patch("quark.db.api.port_find")
) as (port_find,):
port_find.return_value = port_models port_find.return_value = port_models
yield yield
@ -308,9 +304,8 @@ class TestQuarkGetPortsByIPAddress(test_quark_plugin.TestQuarkPlugin):
ip_mod.ports = [port_model] ip_mod.ports = [port_model]
addr_models.append(ip_mod) addr_models.append(ip_mod)
with contextlib.nested( with mock.patch("quark.db.api.port_find_by_ip_address") as \
mock.patch("quark.db.api.port_find_by_ip_address") port_find_by_addr:
) as (port_find_by_addr,):
port_find_by_addr.return_value = addr_models port_find_by_addr.return_value = addr_models
yield yield
@ -354,15 +349,14 @@ class TestQuarkCreatePortFailure(test_quark_plugin.TestQuarkPlugin):
port_model.update(port) port_model.update(port)
port_models = port_model port_models = port_model
with contextlib.nested( with mock.patch("quark.db.api.port_create") as port_create, \
mock.patch("quark.db.api.port_create"), mock.patch("quark.db.api.network_find") as net_find, \
mock.patch("quark.db.api.network_find"), mock.patch("quark.db.api.port_find") as port_find, \
mock.patch("quark.db.api.port_find"), mock.patch("quark.ipam.QuarkIpam.allocate_ip_address") as \
mock.patch("quark.ipam.QuarkIpam.allocate_ip_address"), alloc_ip, \
mock.patch("quark.ipam.QuarkIpam.allocate_mac_address"), mock.patch("quark.ipam.QuarkIpam.allocate_mac_address") as \
mock.patch("quark.db.api.port_count_all"), alloc_mac, \
) as (port_create, net_find, port_find, alloc_ip, alloc_mac, mock.patch("quark.db.api.port_count_all") as port_count:
port_count):
port_create.return_value = port_models port_create.return_value = port_models
net_find.return_value = network net_find.return_value = network
port_find.return_value = models.Port() port_find.return_value = models.Port()
@ -412,15 +406,12 @@ class TestQuarkCreatePortRM9305(test_quark_plugin.TestQuarkPlugin):
port_models = port_model port_models = port_model
db_mod = "quark.db.api" db_mod = "quark.db.api"
ipam = "quark.ipam.QuarkIpam" ipam = "quark.ipam.QuarkIpam"
with contextlib.nested( with mock.patch("%s.port_create" % db_mod) as port_create, \
mock.patch("%s.port_create" % db_mod), mock.patch("%s.network_find" % db_mod) as net_find, \
mock.patch("%s.network_find" % db_mod), mock.patch("%s.port_find" % db_mod) as port_find, \
mock.patch("%s.port_find" % db_mod), mock.patch("%s.allocate_ip_address" % ipam) as alloc_ip, \
mock.patch("%s.allocate_ip_address" % ipam), mock.patch("%s.allocate_mac_address" % ipam) as alloc_mac, \
mock.patch("%s.allocate_mac_address" % ipam), mock.patch("%s.port_count_all" % db_mod) as port_count:
mock.patch("%s.port_count_all" % db_mod),
) as (port_create, net_find, port_find, alloc_ip, alloc_mac,
port_count):
port_create.return_value = port_models port_create.return_value = port_models
net_find.return_value = network net_find.return_value = network
port_find.return_value = None port_find.return_value = None
@ -515,16 +506,16 @@ class TestQuarkCreatePortsSameDevBadRequest(test_quark_plugin.TestQuarkPlugin):
new_ips.extend([ip_mod]) new_ips.extend([ip_mod])
return mock.DEFAULT return mock.DEFAULT
with contextlib.nested( with mock.patch("quark.db.api.port_create") as port_create, \
mock.patch("quark.db.api.port_create"), mock.patch("quark.db.api.network_find") as net_find, \
mock.patch("quark.db.api.network_find"), mock.patch("quark.ipam.QuarkIpam.allocate_ip_address") as \
mock.patch("quark.ipam.QuarkIpam.allocate_ip_address"), alloc_ip, \
mock.patch("quark.ipam.QuarkIpam.allocate_mac_address"), mock.patch("quark.ipam.QuarkIpam.allocate_mac_address") as \
mock.patch("quark.db.api.port_count_all"), alloc_mac, \
mock.patch("neutron.quota.QuotaEngine.limit_check"), mock.patch("quark.db.api.port_count_all") as port_count, \
mock.patch("quark.db.api.subnet_find"), mock.patch("neutron.quota.QuotaEngine.limit_check") as \
) as (port_create, net_find, alloc_ip, alloc_mac, port_count, limit_check, \
limit_check, subnet_find): mock.patch("quark.db.api.subnet_find") as subnet_find:
port_create.side_effect = _create_db_port port_create.side_effect = _create_db_port
net_find.return_value = network net_find.return_value = network
alloc_ip.side_effect = _alloc_ip alloc_ip.side_effect = _alloc_ip
@ -741,15 +732,15 @@ class TestQuarkPortCreateQuota(test_quark_plugin.TestQuarkPlugin):
port_model.update(port) port_model.update(port)
port_models = port_model port_models = port_model
with contextlib.nested( with mock.patch("quark.db.api.port_create") as port_create, \
mock.patch("quark.db.api.port_create"), mock.patch("quark.db.api.network_find") as net_find, \
mock.patch("quark.db.api.network_find"), mock.patch("quark.ipam.QuarkIpam.allocate_ip_address") as \
mock.patch("quark.ipam.QuarkIpam.allocate_ip_address"), alloc_ip, \
mock.patch("quark.ipam.QuarkIpam.allocate_mac_address"), mock.patch("quark.ipam.QuarkIpam.allocate_mac_address") as \
mock.patch("quark.db.api.port_count_all"), alloc_mac, \
mock.patch("neutron.quota.QuotaEngine.limit_check") mock.patch("quark.db.api.port_count_all") as port_count, \
) as (port_create, net_find, alloc_ip, alloc_mac, port_count, mock.patch("neutron.quota.QuotaEngine.limit_check") as \
limit_check): limit_check:
port_create.return_value = port_models port_create.return_value = port_models
net_find.return_value = network net_find.return_value = network
alloc_ip.return_value = addr alloc_ip.return_value = addr
@ -804,12 +795,12 @@ class TestQuarkUpdatePort(test_quark_plugin.TestQuarkPlugin):
port_model.network = net_model port_model.network = net_model
port_model.update(port) port_model.update(port)
with contextlib.nested( with mock.patch("quark.db.api.port_find") as port_find, \
mock.patch("quark.db.api.port_find"), mock.patch("quark.db.api.port_update") as port_update, \
mock.patch("quark.db.api.port_update"), mock.patch("quark.ipam.QuarkIpam.allocate_ip_address") as \
mock.patch("quark.ipam.QuarkIpam.allocate_ip_address"), alloc_ip, \
mock.patch("quark.ipam.QuarkIpam.deallocate_ips_by_port"), mock.patch("quark.ipam.QuarkIpam.deallocate_ips_by_port") as \
) as (port_find, port_update, alloc_ip, dealloc_ip): dealloc_ip:
port_find.return_value = port_model port_find.return_value = port_model
port_update.return_value = port_model port_update.return_value = port_model
if new_ips: if new_ips:
@ -931,18 +922,18 @@ class TestQuarkUpdatePortSecurityGroups(test_quark_plugin.TestQuarkPlugin):
port_model.update(port) port_model.update(port)
port_model["security_groups"].append(sg_mod) port_model["security_groups"].append(sg_mod)
with contextlib.nested( with mock.patch("quark.db.api.port_find") as port_find, \
mock.patch("quark.db.api.port_find"), mock.patch("quark.db.api.port_update") as port_update, \
mock.patch("quark.db.api.port_update"), mock.patch("quark.ipam.QuarkIpam.allocate_ip_address") as \
mock.patch("quark.ipam.QuarkIpam.allocate_ip_address"), alloc_ip, \
mock.patch("quark.ipam.QuarkIpam.deallocate_ips_by_port"), mock.patch("quark.ipam.QuarkIpam.deallocate_ips_by_port") as \
mock.patch("neutron.quota.QuotaEngine.limit_check"), dealloc_ip, \
mock.patch("quark.plugin_modules.ports.STRATEGY" mock.patch("neutron.quota.QuotaEngine.limit_check"), \
".is_provider_network"), mock.patch("quark.plugin_modules.ports.STRATEGY"
mock.patch("quark.db.api.security_group_find"), ".is_provider_network") as net_strat, \
mock.patch("quark.drivers.base.BaseDriver.update_port") mock.patch("quark.db.api.security_group_find") as sg_find, \
) as (port_find, port_update, alloc_ip, dealloc_ip, limit_check, mock.patch("quark.drivers.base.BaseDriver.update_port") as \
net_strat, sg_find, driver_port_update): driver_port_update:
port_find.return_value = port_model port_find.return_value = port_model
def _port_update(context, port_db, **kwargs): def _port_update(context, port_db, **kwargs):
@ -1033,12 +1024,11 @@ class TestQuarkUpdatePortSetsIps(test_quark_plugin.TestQuarkPlugin):
port_model = models.Port() port_model = models.Port()
port_model['network'] = net_model port_model['network'] = net_model
port_model.update(port) port_model.update(port)
with contextlib.nested( with mock.patch("quark.db.api.port_find") as port_find, \
mock.patch("quark.db.api.port_find"), mock.patch("quark.db.api.port_update") as port_update, \
mock.patch("quark.db.api.port_update"), mock.patch("quark.ipam.QuarkIpam.deallocate_ips_by_port") as \
mock.patch("quark.ipam.QuarkIpam.deallocate_ips_by_port"), dealloc_ip, \
mock.patch("neutron.quota.QuotaEngine.limit_check") mock.patch("neutron.quota.QuotaEngine.limit_check"):
) as (port_find, port_update, dealloc_ip, limit_check):
port_find.return_value = port_model port_find.return_value = port_model
port_update.return_value = port_model port_update.return_value = port_model
alloc_ip = mock.patch("quark.ipam.QuarkIpam.allocate_ip_address", alloc_ip = mock.patch("quark.ipam.QuarkIpam.allocate_ip_address",
@ -1080,13 +1070,13 @@ class TestQuarkCreatePortOnSharedNetworks(test_quark_plugin.TestQuarkPlugin):
port_model.update(port) port_model.update(port)
port_models = port_model port_models = port_model
with contextlib.nested( with mock.patch("quark.db.api.port_create") as port_create, \
mock.patch("quark.db.api.port_create"), mock.patch("quark.db.api.network_find") as net_find, \
mock.patch("quark.db.api.network_find"), mock.patch("quark.ipam.QuarkIpam.allocate_ip_address") as \
mock.patch("quark.ipam.QuarkIpam.allocate_ip_address"), alloc_ip, \
mock.patch("quark.ipam.QuarkIpam.allocate_mac_address"), mock.patch("quark.ipam.QuarkIpam.allocate_mac_address") as \
mock.patch("neutron.quota.QuotaEngine.limit_check") alloc_mac, \
) as (port_create, net_find, alloc_ip, alloc_mac, limit_check): mock.patch("neutron.quota.QuotaEngine.limit_check"):
port_create.return_value = port_models port_create.return_value = port_models
net_find.return_value = network net_find.return_value = network
alloc_ip.return_value = addr alloc_ip.return_value = addr
@ -1145,14 +1135,14 @@ class TestQuarkDeletePort(test_quark_plugin.TestQuarkPlugin):
port_model.network = net_model port_model.network = net_model
port_models = port_model port_models = port_model
with contextlib.nested( with mock.patch("quark.db.api.port_find") as port_find, \
mock.patch("quark.db.api.port_find"), mock.patch("quark.ipam.QuarkIpam.deallocate_ips_by_port") as \
mock.patch("quark.ipam.QuarkIpam.deallocate_ips_by_port"), dealloc_ip, \
mock.patch("quark.ipam.QuarkIpam.deallocate_mac_address"), mock.patch("quark.ipam.QuarkIpam.deallocate_mac_address") as \
mock.patch("quark.db.api.port_delete"), dealloc_mac, \
mock.patch("quark.drivers.base.BaseDriver.delete_port") mock.patch("quark.db.api.port_delete") as db_port_del, \
) as (port_find, dealloc_ip, dealloc_mac, db_port_del, mock.patch("quark.drivers.base.BaseDriver.delete_port") as \
driver_port_del): driver_port_del:
port_find.return_value = port_models port_find.return_value = port_models
dealloc_ip.return_value = addr dealloc_ip.return_value = addr
dealloc_mac.return_value = mac dealloc_mac.return_value = mac
@ -1318,25 +1308,24 @@ class TestPortDriverSelection(test_quark_plugin.TestQuarkPlugin):
bar_ipam.allocate_mac_address.return_value = mac bar_ipam.allocate_mac_address.return_value = mac
ipam = {"FOO": foo_ipam, "BAR": bar_ipam} ipam = {"FOO": foo_ipam, "BAR": bar_ipam}
with contextlib.nested( with mock.patch("quark.db.api.port_create") as port_create, \
mock.patch("quark.db.api.port_create"), mock.patch("quark.db.api.network_find") as net_find, \
mock.patch("quark.db.api.network_find"), mock.patch("oslo_utils.uuidutils.generate_uuid") as gen_uuid, \
mock.patch("oslo_utils.uuidutils.generate_uuid"), mock.patch("quark.plugin_views._make_port_dict"), \
mock.patch("quark.plugin_views._make_port_dict"), mock.patch("quark.db.api.port_count_all") as port_count, \
mock.patch("quark.db.api.port_count_all"), mock.patch("neutron.quota.QuotaEngine.limit_check"), \
mock.patch("neutron.quota.QuotaEngine.limit_check"), mock.patch("quark.plugin_modules.ports.registry."
mock.patch("quark.plugin_modules.ports.registry." "DRIVER_REGISTRY.drivers",
"DRIVER_REGISTRY.drivers", new_callable=mock.PropertyMock(
new_callable=mock.PropertyMock(return_value=drivers)), return_value=drivers)), \
mock.patch("quark.plugin_modules.ports.registry." mock.patch("quark.plugin_modules.ports.registry."
"DRIVER_REGISTRY.port_driver_compat_map", "DRIVER_REGISTRY.port_driver_compat_map",
new_callable=mock.PropertyMock( new_callable=mock.PropertyMock(
return_value=compat_map)), return_value=compat_map)), \
mock.patch("quark.plugin_modules.ports.ipam." mock.patch("quark.plugin_modules.ports.ipam."
"IPAM_REGISTRY.strategies", "IPAM_REGISTRY.strategies",
new_callable=mock.PropertyMock(return_value=ipam)) new_callable=mock.PropertyMock(
) as (port_create, net_find, gen_uuid, make_port, return_value=ipam)):
port_count, limit_check, _, _, _):
net_find.return_value = network net_find.return_value = network
gen_uuid.return_value = 1 gen_uuid.return_value = 1
port_count.return_value = 0 port_count.return_value = 0
@ -1770,17 +1759,16 @@ class TestQuarkPortCreateFiltering(test_quark_plugin.TestQuarkPlugin):
network["network_plugin"] = "BASE" network["network_plugin"] = "BASE"
network["ipam_strategy"] = "ANY" network["ipam_strategy"] = "ANY"
with contextlib.nested( with mock.patch("quark.db.api.port_create") as port_create, \
mock.patch("quark.db.api.port_create"), mock.patch("quark.db.api.network_find") as net_find, \
mock.patch("quark.db.api.network_find"), mock.patch("quark.ipam.QuarkIpam.allocate_ip_address") as \
mock.patch("quark.ipam.QuarkIpam.allocate_ip_address"), alloc_ip, \
mock.patch("quark.ipam.QuarkIpam.allocate_mac_address"), mock.patch("quark.ipam.QuarkIpam.allocate_mac_address") as \
mock.patch("oslo_utils.uuidutils.generate_uuid"), alloc_mac, \
mock.patch("quark.plugin_views._make_port_dict"), mock.patch("oslo_utils.uuidutils.generate_uuid") as gen_uuid, \
mock.patch("quark.db.api.port_count_all"), mock.patch("quark.plugin_views._make_port_dict"), \
mock.patch("neutron.quota.QuotaEngine.limit_check") mock.patch("quark.db.api.port_count_all") as port_count, \
) as (port_create, net_find, alloc_ip, alloc_mac, gen_uuid, make_port, mock.patch("neutron.quota.QuotaEngine.limit_check"):
port_count, limit_check):
net_find.return_value = network net_find.return_value = network
alloc_ip.return_value = addr alloc_ip.return_value = addr
alloc_mac.return_value = mac alloc_mac.return_value = mac
@ -1870,13 +1858,12 @@ class TestQuarkPortCreateFiltering(test_quark_plugin.TestQuarkPlugin):
class TestQuarkPortUpdateFiltering(test_quark_plugin.TestQuarkPlugin): class TestQuarkPortUpdateFiltering(test_quark_plugin.TestQuarkPlugin):
@contextlib.contextmanager @contextlib.contextmanager
def _stubs(self): def _stubs(self):
with contextlib.nested( with mock.patch("quark.db.api.port_find") as port_find, \
mock.patch("quark.db.api.port_find"), mock.patch("quark.db.api.port_update") as port_update, \
mock.patch("quark.db.api.port_update"), mock.patch("quark.drivers.registry."
mock.patch("quark.drivers.registry.DriverRegistry.get_driver"), "DriverRegistry.get_driver"), \
mock.patch("quark.plugin_views._make_port_dict"), mock.patch("quark.plugin_views._make_port_dict"), \
mock.patch("neutron.quota.QuotaEngine.limit_check") mock.patch("neutron.quota.QuotaEngine.limit_check"):
) as (port_find, port_update, get_driver, make_port, limit_check):
yield port_find, port_update yield port_find, port_update
def test_update_port_attribute_filtering(self): def test_update_port_attribute_filtering(self):
@ -1930,15 +1917,12 @@ class TestQuarkPortCreateAsAdvancedService(test_quark_plugin.TestQuarkPlugin):
port_models = port_model port_models = port_model
db_mod = "quark.db.api" db_mod = "quark.db.api"
ipam = "quark.ipam.QuarkIpam" ipam = "quark.ipam.QuarkIpam"
with contextlib.nested( with mock.patch("%s.port_create" % db_mod) as port_create, \
mock.patch("%s.port_create" % db_mod), mock.patch("%s.network_find" % db_mod) as net_find, \
mock.patch("%s.network_find" % db_mod), mock.patch("%s.port_find" % db_mod) as port_find, \
mock.patch("%s.port_find" % db_mod), mock.patch("%s.allocate_ip_address" % ipam) as alloc_ip, \
mock.patch("%s.allocate_ip_address" % ipam), mock.patch("%s.allocate_mac_address" % ipam) as alloc_mac, \
mock.patch("%s.allocate_mac_address" % ipam), mock.patch("%s.port_count_all" % db_mod) as port_count:
mock.patch("%s.port_count_all" % db_mod),
) as (port_create, net_find, port_find, alloc_ip, alloc_mac,
port_count):
port_create.return_value = port_models port_create.return_value = port_models
net_find.return_value = network net_find.return_value = network
port_find.return_value = None port_find.return_value = None

View File

@ -57,17 +57,16 @@ class TestQuarkCreateRoutes(test_quark_plugin.TestQuarkPlugin):
@contextlib.contextmanager @contextlib.contextmanager
def _stubs(self, create_route, find_routes, subnet): def _stubs(self, create_route, find_routes, subnet):
db_mod = "quark.db.api" db_mod = "quark.db.api"
with contextlib.nested( # QuotaEngine.limit_check must be mocked:
mock.patch("%s.route_create" % db_mod), # This module can't run independently otherwise, as the Quota
mock.patch("%s.route_find" % db_mod), # model isn't defined in this test unit, and no clean way to
mock.patch("%s.subnet_find" % db_mod), # use a model such that it would be defined. In other words,
# This module can't run independently otherwise, as the Quota # running the other tests still has side-effects, which should
# model isn't defined in this test unit, and no clean way to # be investigated and cleaned up later.
# use a model such that it would be defined. In other words, with mock.patch("%s.route_create" % db_mod) as route_create, \
# running the other tests still has side-effects, which should mock.patch("%s.route_find" % db_mod) as route_find, \
# be investigated and cleaned up later. mock.patch("%s.subnet_find" % db_mod) as subnet_find, \
mock.patch("neutron.quota.QuotaEngine.limit_check") mock.patch("neutron.quota.QuotaEngine.limit_check"):
) as (route_create, route_find, subnet_find, quota):
route_create.return_value = create_route route_create.return_value = create_route
route_find.return_value = find_routes route_find.return_value = find_routes
subnet_find.return_value = subnet subnet_find.return_value = subnet
@ -117,10 +116,8 @@ class TestQuarkDeleteRoutes(test_quark_plugin.TestQuarkPlugin):
@contextlib.contextmanager @contextlib.contextmanager
def _stubs(self, route): def _stubs(self, route):
db_mod = "quark.db.api" db_mod = "quark.db.api"
with contextlib.nested( with mock.patch("%s.route_delete" % db_mod) as route_delete, \
mock.patch("%s.route_delete" % db_mod), mock.patch("%s.route_find" % db_mod) as route_find:
mock.patch("%s.route_find" % db_mod),
) as (route_delete, route_find):
route_find.return_value = route route_find.return_value = route
yield route_delete, route_find yield route_delete, route_find

View File

@ -152,10 +152,8 @@ class TestQuarkUpdateSecurityGroup(test_quark_plugin.TestQuarkPlugin):
updated_group = group.copy() updated_group = group.copy()
updated_group["name"] = "bar" updated_group["name"] = "bar"
with contextlib.nested( with mock.patch("quark.db.api.security_group_find") as db_find, \
mock.patch("quark.db.api.security_group_find"), mock.patch("quark.db.api.security_group_update") as db_update:
mock.patch("quark.db.api.security_group_update"),
) as (db_find, db_update):
db_find.return_value = group db_find.return_value = group
db_update.return_value = updated_group db_update.return_value = updated_group
update = dict(security_group=dict(name="bar")) update = dict(security_group=dict(name="bar"))
@ -179,10 +177,8 @@ class TestQuarkCreateSecurityGroup(test_quark_plugin.TestQuarkPlugin):
dbgroup = models.SecurityGroup() dbgroup = models.SecurityGroup()
dbgroup.update(security_group) dbgroup.update(security_group)
with contextlib.nested( with mock.patch("quark.db.api.security_group_find") as db_find, \
mock.patch("quark.db.api.security_group_find"), mock.patch("quark.db.api.security_group_create") as db_create:
mock.patch("quark.db.api.security_group_create"),
) as (db_find, db_create):
db_find.return_value.count.return_value = other db_find.return_value.count.return_value = other
db_create.return_value = dbgroup db_create.return_value = dbgroup
yield db_create yield db_create
@ -234,10 +230,9 @@ class TestQuarkDeleteSecurityGroup(test_quark_plugin.TestQuarkPlugin):
dbgroup = models.SecurityGroup() dbgroup = models.SecurityGroup()
dbgroup.update(security_group) dbgroup.update(security_group)
with contextlib.nested( with mock.patch("quark.db.api.security_group_find") as group_find, \
mock.patch("quark.db.api.security_group_find"), mock.patch("quark.db.api.security_group_delete") as \
mock.patch("quark.db.api.security_group_delete"), db_group_delete:
) as (group_find, db_group_delete):
group_find.return_value = dbgroup group_find.return_value = dbgroup
db_group_delete.return_value = dbgroup db_group_delete.return_value = dbgroup
yield db_group_delete yield db_group_delete
@ -303,13 +298,15 @@ class TestQuarkCreateSecurityGroupRule(test_quark_plugin.TestQuarkPlugin):
dbrule["group_id"] = rule['security_group_id'] dbrule["group_id"] = rule['security_group_id']
return dbrule return dbrule
with contextlib.nested( with mock.patch("quark.db.api.security_group_find") as group_find, \
mock.patch("quark.db.api.security_group_find"), mock.patch("quark.db.api.security_group_rule_find") as \
mock.patch("quark.db.api.security_group_rule_find"), rule_find, \
mock.patch("quark.db.api.security_group_rule_create"), mock.patch("quark.db.api.security_group_rule_create") as \
mock.patch("quark.protocols.human_readable_protocol"), rule_create, \
mock.patch("neutron.quota.QuotaEngine.limit_check") mock.patch("quark.protocols.human_readable_protocol") as \
) as (group_find, rule_find, rule_create, human, limit_check): human, \
mock.patch("neutron.quota.QuotaEngine.limit_check") as \
limit_check:
group_find.return_value = dbgroup group_find.return_value = dbgroup
rule_find.return_value.count.return_value = group.get( rule_find.return_value.count.return_value = group.get(
'port_rules', None) if group else 0 'port_rules', None) if group else 0
@ -435,11 +432,11 @@ class TestQuarkDeleteSecurityGroupRule(test_quark_plugin.TestQuarkPlugin):
dbrule = models.SecurityGroupRule() dbrule = models.SecurityGroupRule()
dbrule.update(dict(rule, group=dbgroup)) dbrule.update(dict(rule, group=dbgroup))
with contextlib.nested( with mock.patch("quark.db.api.security_group_find") as group_find, \
mock.patch("quark.db.api.security_group_find"), mock.patch("quark.db.api.security_group_rule_find") as \
mock.patch("quark.db.api.security_group_rule_find"), rule_find, \
mock.patch("quark.db.api.security_group_rule_delete"), mock.patch("quark.db.api.security_group_rule_delete") as \
) as (group_find, rule_find, db_group_delete): db_group_delete:
group_find.return_value = dbgroup group_find.return_value = dbgroup
rule_find.return_value = dbrule rule_find.return_value = dbrule
yield db_group_delete yield db_group_delete
@ -545,16 +542,16 @@ class TestQuarkProtocolHandling(test_quark_plugin.TestQuarkPlugin):
class TestSecurityGroupExceptions(test_quark_plugin.TestQuarkPlugin): class TestSecurityGroupExceptions(test_quark_plugin.TestQuarkPlugin):
@contextlib.contextmanager @contextlib.contextmanager
def _stubs(self, finds_rule=True): def _stubs(self, finds_rule=True):
with contextlib.nested( with mock.patch("quark.db.api.security_group_find") as group_find, \
mock.patch("quark.db.api.security_group_find"), mock.patch("quark.db.api.security_group_rule_find") as \
mock.patch("quark.db.api.security_group_rule_find"), rule_find, \
mock.patch("neutron.extensions.securitygroup." mock.patch("neutron.extensions.securitygroup."
"SecurityGroupNotFound.__init__"), "SecurityGroupNotFound.__init__") as group_exc, \
mock.patch("neutron.extensions.securitygroup." mock.patch("neutron.extensions.securitygroup."
"SecurityGroupRuleNotFound.__init__"), "SecurityGroupRuleNotFound.__init__") as \
mock.patch("quark.plugin_modules.security_groups." rule_exc, \
"_validate_security_group_rule") mock.patch("quark.plugin_modules.security_groups."
) as (group_find, rule_find, group_exc, rule_exc, validate): "_validate_security_group_rule"):
group_find.return_value = None group_find.return_value = None
rule_find.return_value = None rule_find.return_value = None
if finds_rule: if finds_rule:

View File

@ -181,12 +181,10 @@ class TestQuarkCreateSubnetOverlapping(test_quark_plugin.TestQuarkPlugin):
subnet_models.append(s) subnet_models.append(s)
network = models.Network() network = models.Network()
network.update(dict(id=1, subnets=subnet_models)) network.update(dict(id=1, subnets=subnet_models))
with contextlib.nested( with mock.patch("quark.db.api.network_find") as net_find, \
mock.patch("quark.db.api.network_find"), mock.patch("quark.db.api.subnet_find") as subnet_find, \
mock.patch("quark.db.api.subnet_find"), mock.patch("quark.db.api.subnet_create") as subnet_create, \
mock.patch("quark.db.api.subnet_create"), mock.patch("neutron.common.rpc.get_notifier"):
mock.patch("neutron.common.rpc.get_notifier")
) as (net_find, subnet_find, subnet_create, get_notifier):
net_find.return_value = network net_find.return_value = network
subnet_find.return_value = subnet_models subnet_find.return_value = subnet_models
subnet_create.return_value = models.Subnet( subnet_create.return_value = models.Subnet(
@ -239,14 +237,11 @@ class TestQuarkCreateSubnetAllocationPools(test_quark_plugin.TestQuarkPlugin):
return mock.patch.object(models.Subnet, "allocation_pools") return mock.patch.object(models.Subnet, "allocation_pools")
return mock.MagicMock() return mock.MagicMock()
with contextlib.nested( with mock.patch("quark.db.api.network_find") as net_find, \
mock.patch("quark.db.api.network_find"), mock.patch("quark.db.api.subnet_find") as subnet_find, \
mock.patch("quark.db.api.subnet_find"), mock.patch("quark.db.api.subnet_create") as subnet_create, \
mock.patch("quark.db.api.subnet_create"), mock.patch("neutron.common.rpc.get_notifier"), \
mock.patch("neutron.common.rpc.get_notifier"), _allocation_pools_mock() as alloc_pools_method:
_allocation_pools_mock(),
) as (net_find, subnet_find, subnet_create, get_notifier,
alloc_pools_method):
net_find.return_value = s["network"] net_find.return_value = s["network"]
subnet_find.return_value = [] subnet_find.return_value = []
subnet_create.return_value = s subnet_create.return_value = s
@ -401,19 +396,18 @@ class TestQuarkCreateSubnet(test_quark_plugin.TestQuarkPlugin):
if allocation_pools is not None: if allocation_pools is not None:
return mock.patch.object(models.Subnet, "allocation_pools") return mock.patch.object(models.Subnet, "allocation_pools")
return mock.MagicMock() return mock.MagicMock()
with contextlib.nested( with mock.patch("quark.db.api.subnet_create") as subnet_create, \
mock.patch("quark.db.api.subnet_create"), mock.patch("quark.db.api.network_find") as net_find, \
mock.patch("quark.db.api.network_find"), mock.patch("quark.db.api.dns_create") as dns_create, \
mock.patch("quark.db.api.dns_create"), mock.patch("quark.db.api.route_create") as route_create, \
mock.patch("quark.db.api.route_create"), mock.patch("quark.db.api.subnet_find"), \
mock.patch("quark.db.api.subnet_find"), mock.patch("neutron.common.rpc.get_notifier"), \
mock.patch("neutron.common.rpc.get_notifier"), _allocation_pools_mock() as alloc_pools_method, \
_allocation_pools_mock(), mock.patch("sqlalchemy.orm.session."
mock.patch("sqlalchemy.orm.session.SessionTransaction.commit"), "SessionTransaction.commit"), \
mock.patch( mock.patch("sqlalchemy.orm.unitofwork."
"sqlalchemy.orm.unitofwork.UOWTransaction.register_object") "UOWTransaction.register_object") as \
) as (subnet_create, net_find, dns_create, route_create, subnet_find, register_object:
get_notifier, alloc_pools_method, commit, register_object):
subnet_create.return_value = subnet_mod subnet_create.return_value = subnet_mod
net_find.return_value = network net_find.return_value = network
route_create.side_effect = route_models route_create.side_effect = route_models
@ -857,16 +851,13 @@ class TestQuarkAllocationPoolCache(test_quark_plugin.TestQuarkPlugin):
gateway=r["nexthop"], gateway=r["nexthop"],
subnet_id=subnet_mod["id"]) subnet_id=subnet_mod["id"])
for r in host_routes] for r in host_routes]
with contextlib.nested( with mock.patch("quark.db.api.subnet_find") as subnet_find, \
mock.patch("quark.db.api.subnet_find"), mock.patch("quark.db.api.subnet_update") as subnet_update, \
mock.patch("quark.db.api.subnet_update"), mock.patch("quark.db.api.dns_create"), \
mock.patch("quark.db.api.dns_create"), mock.patch("quark.db.api.route_find") as route_find, \
mock.patch("quark.db.api.route_find"), mock.patch("quark.db.api.route_update"), \
mock.patch("quark.db.api.route_update"), mock.patch("quark.db.api.route_create"), \
mock.patch("quark.db.api.route_create"), mock.patch("sqlalchemy.orm.session.SessionTransaction.commit"):
mock.patch("sqlalchemy.orm.session.SessionTransaction.commit")
) as (subnet_find, subnet_update, dns_create, route_find,
route_update, route_create, commit):
subnet_find.return_value = subnet_mod subnet_find.return_value = subnet_mod
if has_subnet: if has_subnet:
route_find.return_value = (subnet_mod["routes"][0] if route_find.return_value = (subnet_mod["routes"][0] if
@ -963,19 +954,17 @@ class TestQuarkUpdateSubnet(test_quark_plugin.TestQuarkPlugin):
gateway=r["nexthop"], gateway=r["nexthop"],
subnet_id=subnet_mod["id"]) subnet_id=subnet_mod["id"])
for r in host_routes] for r in host_routes]
with contextlib.nested( with mock.patch("quark.db.api.subnet_find") as subnet_find, \
mock.patch("quark.db.api.subnet_find"), mock.patch("quark.db.api.subnet_update") as subnet_update, \
mock.patch("quark.db.api.subnet_update"), mock.patch("quark.db.api.dns_create") as dns_create, \
mock.patch("quark.db.api.dns_create"), mock.patch("quark.db.api.route_find") as route_find, \
mock.patch("quark.db.api.route_find"), mock.patch("quark.db.api.route_update") as route_update, \
mock.patch("quark.db.api.route_update"), mock.patch("quark.db.api.route_create") as route_create, \
mock.patch("quark.db.api.route_create"), mock.patch("sqlalchemy.orm.session."
mock.patch("sqlalchemy.orm.session.SessionTransaction.commit"), "SessionTransaction.commit"), \
mock.patch( mock.patch("sqlalchemy.orm.unitofwork."
"sqlalchemy.orm.unitofwork.UOWTransaction.register_object") "UOWTransaction.register_object") as \
) as (subnet_find, subnet_update, register_object:
dns_create,
route_find, route_update, route_create, commit, register_object):
subnet_find.return_value = subnet_mod subnet_find.return_value = subnet_mod
if has_subnet: if has_subnet:
route_find.return_value = (subnet_mod["routes"][0] if route_find.return_value = (subnet_mod["routes"][0] if
@ -1216,12 +1205,11 @@ class TestQuarkDeleteSubnet(test_quark_plugin.TestQuarkPlugin):
ip_mods.append(ip_mod) ip_mods.append(ip_mod)
strategy_prefix = "quark.network_strategy.JSONStrategy" strategy_prefix = "quark.network_strategy.JSONStrategy"
with contextlib.nested( with mock.patch("quark.db.api.subnet_find") as sub_find, \
mock.patch("quark.db.api.subnet_find"), mock.patch("quark.db.api.subnet_delete") as sub_delete, \
mock.patch("quark.db.api.subnet_delete"), mock.patch("neutron.common.rpc.get_notifier"), \
mock.patch("neutron.common.rpc.get_notifier"), mock.patch("%s.is_provider_network" % strategy_prefix) as \
mock.patch("%s.is_provider_network" % strategy_prefix) is_provider_network:
) as (sub_find, sub_delete, get_notifier, is_provider_network):
if subnet_mod: if subnet_mod:
subnet_mod.allocated_ips = ip_mods subnet_mod.allocated_ips = ip_mods
sub_find.return_value = subnet_mod sub_find.return_value = subnet_mod
@ -1288,17 +1276,16 @@ class TestSubnetsQuotas(test_quark_plugin.TestQuarkPlugin):
s["_allocation_pool_cache"] = None s["_allocation_pool_cache"] = None
subnet = models.Subnet(**s) subnet = models.Subnet(**s)
subnets.append(subnet) subnets.append(subnet)
with contextlib.nested( with mock.patch("quark.plugin_modules.subnets.get_subnets") as \
mock.patch("quark.plugin_modules.subnets.get_subnets"), get_subnets, \
mock.patch("quark.db.api.subnet_find"), mock.patch("quark.db.api.subnet_find") as sub_find, \
mock.patch("quark.db.api.network_find"), mock.patch("quark.db.api.network_find"), \
mock.patch("quark.db.api.subnet_create"), mock.patch("quark.db.api.subnet_create") as sub_create, \
mock.patch("quark.db.api.subnet_delete"), mock.patch("quark.db.api.subnet_delete"), \
mock.patch("neutron.common.rpc.get_notifier"), mock.patch("neutron.common.rpc.get_notifier") as notify, \
mock.patch("oslo_utils.timeutils.utcnow"), mock.patch("oslo_utils.timeutils.utcnow") as time_func, \
mock.patch("quark.plugin_modules.subnets._validate_subnet_cidr") mock.patch("quark.plugin_modules.subnets."
) as (get_subnets, sub_find, net_find, sub_create, sub_del, notify, "_validate_subnet_cidr"):
time_func, sub_validate):
sub_create.return_value = subnets[0] sub_create.return_value = subnets[0]
sub_find.return_value = subnets[0] sub_find.return_value = subnets[0]
retsubs = [] retsubs = []
@ -1473,17 +1460,14 @@ class TestQuarkDiagnoseSubnets(test_quark_plugin.TestQuarkPlugin):
class TestQuarkCreateSubnetAttrFilters(test_quark_plugin.TestQuarkPlugin): class TestQuarkCreateSubnetAttrFilters(test_quark_plugin.TestQuarkPlugin):
@contextlib.contextmanager @contextlib.contextmanager
def _stubs(self): def _stubs(self):
with contextlib.nested( with mock.patch("quark.db.api.subnet_create") as subnet_create, \
mock.patch("quark.db.api.subnet_create"), mock.patch("quark.db.api.network_find") as net_find, \
mock.patch("quark.db.api.network_find"), mock.patch("quark.db.api.dns_create"), \
mock.patch("quark.db.api.dns_create"), mock.patch("quark.db.api.route_create") as route_create, \
mock.patch("quark.db.api.route_create"), mock.patch("quark.plugin_views._make_subnet_dict"), \
mock.patch("quark.plugin_views._make_subnet_dict"), mock.patch("quark.db.api.subnet_find"), \
mock.patch("quark.db.api.subnet_find"), mock.patch("neutron.common.rpc.get_notifier"), \
mock.patch("neutron.common.rpc.get_notifier"), mock.patch("sqlalchemy.orm.session.SessionTransaction.commit"):
mock.patch("sqlalchemy.orm.session.SessionTransaction.commit")
) as (subnet_create, net_find, dns_create, route_create, sub_dict,
subnet_find, get_notifier, commit):
route_create.return_value = models.Route() route_create.return_value = models.Route()
yield subnet_create, net_find yield subnet_create, net_find
@ -1534,17 +1518,14 @@ class TestQuarkUpdateSubnetAttrFilters(test_quark_plugin.TestQuarkPlugin):
@contextlib.contextmanager @contextlib.contextmanager
def _stubs(self): def _stubs(self):
pool_mod = "quark.allocation_pool.AllocationPools" pool_mod = "quark.allocation_pool.AllocationPools"
with contextlib.nested( with mock.patch("quark.db.api.subnet_find") as subnet_find, \
mock.patch("quark.db.api.subnet_find"), mock.patch("quark.db.api.subnet_update") as subnet_update, \
mock.patch("quark.db.api.subnet_update"), mock.patch("quark.db.api.dns_create"), \
mock.patch("quark.db.api.dns_create"), mock.patch("quark.db.api.route_find"), \
mock.patch("quark.db.api.route_find"), mock.patch("quark.db.api.route_update"), \
mock.patch("quark.db.api.route_update"), mock.patch("quark.db.api.route_create"), \
mock.patch("quark.db.api.route_create"), mock.patch(pool_mod), \
mock.patch(pool_mod), mock.patch("quark.plugin_views._make_subnet_dict"):
mock.patch("quark.plugin_views._make_subnet_dict")
) as (subnet_find, subnet_update, dns_create, route_find,
route_update, route_create, make_subnet, gateway_exclude):
yield subnet_update, subnet_find yield subnet_update, subnet_find
def test_update_subnet_attr_filters(self): def test_update_subnet_attr_filters(self):

View File

@ -112,20 +112,24 @@ class QuarkMacAddressAllocateDeallocated(QuarkIpamBaseTest):
mac_range = dict(id=1, first_address=0, last_address=255, mac_range = dict(id=1, first_address=0, last_address=255,
next_auto_assign_mac=0, do_not_use=do_not_use, next_auto_assign_mac=0, do_not_use=do_not_use,
cidr="AA:BB:CC/24") cidr="AA:BB:CC/24")
with contextlib.nested( with mock.patch("quark.db.api.mac_address_reallocate") as \
mock.patch("quark.db.api.mac_address_reallocate"), addr_realloc, \
mock.patch("quark.db.api.mac_address_reallocate_find"), mock.patch("quark.db.api.mac_address_reallocate_find") as \
mock.patch("quark.db.api." addr_realloc_find, \
"mac_address_range_find_allocation_counts"), mock.patch("quark.db.api."
mock.patch("quark.db.api.mac_address_create"), "mac_address_range_find_allocation_counts") as \
mock.patch("quark.db.api.mac_address_range_find"), mac_range_count, \
mock.patch("quark.db.api.mac_address_delete"), mock.patch("quark.db.api.mac_address_create") as mac_create, \
mock.patch("quark.db.api.mac_range_update_next_auto_assign_mac"), mock.patch("quark.db.api.mac_address_range_find") as \
mock.patch("quark.db.api.mac_range_update_set_full"), range_find, \
mock.patch("sqlalchemy.orm.session.Session.refresh") mock.patch("quark.db.api.mac_address_delete") as mac_delete, \
) as (addr_realloc, addr_realloc_find, mac_range_count, mock.patch("quark.db.api."
mac_create, range_find, mac_delete, mac_auto_assign, set_full, "mac_range_update_next_auto_assign_mac") as \
refresh): mac_auto_assign, \
mock.patch("quark.db.api.mac_range_update_set_full") as \
set_full, \
mock.patch("sqlalchemy.orm.session.Session.refresh") as \
refresh:
address_mod = models.MacAddress(**address) address_mod = models.MacAddress(**address)
range_mod = models.MacAddressRange(**mac_range) range_mod = models.MacAddressRange(**mac_range)
if mac_find: if mac_find:
@ -174,14 +178,16 @@ class QuarkNewMacAddressAllocation(QuarkIpamBaseTest):
def _stubs(self, addresses=None, ranges=None): def _stubs(self, addresses=None, ranges=None):
if not addresses: if not addresses:
addresses = [None] addresses = [None]
with contextlib.nested( with mock.patch("quark.db.api.mac_address_find") as mac_find, \
mock.patch("quark.db.api.mac_address_find"), mock.patch("quark.db.api."
mock.patch("quark.db.api." "mac_address_range_find_allocation_counts") as \
"mac_address_range_find_allocation_counts"), mac_range_count, \
mock.patch("quark.db.api.mac_range_update_next_auto_assign_mac"), mock.patch("quark.db.api."
mock.patch("quark.db.api.mac_range_update_set_full"), "mac_range_update_next_auto_assign_mac"), \
mock.patch("sqlalchemy.orm.session.Session.refresh") mock.patch("quark.db.api.mac_range_update_set_full") as \
) as (mac_find, mac_range_count, mac_auto, mac_set_full, refresh): mac_set_full, \
mock.patch("sqlalchemy.orm.session.Session.refresh") as \
refresh:
address_mod = [mac_helper(a) for a in addresses] address_mod = [mac_helper(a) for a in addresses]
range_mod = (range_helper(ranges[0]), ranges[1]) range_mod = (range_helper(ranges[0]), ranges[1])
mac_find.side_effect = address_mod mac_find.side_effect = address_mod
@ -264,12 +270,12 @@ class QuarkNewMacAddressAllocationCreateConflict(QuarkIpamBaseTest):
def _stubs(self, addresses=None, ranges=None): def _stubs(self, addresses=None, ranges=None):
if not addresses: if not addresses:
addresses = [None] addresses = [None]
with contextlib.nested( with mock.patch("quark.db.api.mac_address_find") as mac_find, \
mock.patch("quark.db.api.mac_address_find"), mock.patch("quark.db.api.mac_address_create") as \
mock.patch("quark.db.api.mac_address_create"), mac_create, \
mock.patch("quark.db.api." mock.patch("quark.db.api."
"mac_address_range_find_allocation_counts"), "mac_address_range_find_allocation_counts") as \
) as (mac_find, mac_create, mac_range_count): mac_range_count:
mac_find.side_effect = [None, None] mac_find.side_effect = [None, None]
address_mod = [mac_helper(a) for a in addresses] address_mod = [mac_helper(a) for a in addresses]
mac_create.side_effect = address_mod mac_create.side_effect = address_mod
@ -295,12 +301,13 @@ class QuarkNewMacAddressReallocationDeadlocks(QuarkIpamBaseTest):
addresses = [None] addresses = [None]
old_override = cfg.CONF.QUARK.mac_address_retry_max old_override = cfg.CONF.QUARK.mac_address_retry_max
cfg.CONF.set_override('mac_address_retry_max', 1, 'QUARK') cfg.CONF.set_override('mac_address_retry_max', 1, 'QUARK')
with contextlib.nested( with mock.patch("quark.db.api.mac_address_reallocate") as \
mock.patch("quark.db.api.mac_address_reallocate"), mac_realloc, \
mock.patch("quark.db.api.mac_address_create"), mock.patch("quark.db.api.mac_address_create") as \
mock.patch("quark.db.api." mac_create, \
"mac_address_range_find_allocation_counts"), mock.patch("quark.db.api."
) as (mac_realloc, mac_create, mac_range_count): "mac_address_range_find_allocation_counts") as \
mac_range_count:
mac_realloc.side_effect = [Exception, None] mac_realloc.side_effect = [Exception, None]
mac_create.side_effect = addresses mac_create.side_effect = addresses
mac_range_count.return_value = ranges mac_range_count.return_value = ranges
@ -321,12 +328,11 @@ class QuarkNewMacAddressReallocationDeadlocks(QuarkIpamBaseTest):
class QuarkMacAddressDeallocation(QuarkIpamBaseTest): class QuarkMacAddressDeallocation(QuarkIpamBaseTest):
@contextlib.contextmanager @contextlib.contextmanager
def _stubs(self, mac, mac_range): def _stubs(self, mac, mac_range):
with contextlib.nested( with mock.patch("quark.db.api.mac_address_find") as mac_find, \
mock.patch("quark.db.api.mac_address_find"), mock.patch("quark.db.api.mac_address_update") as mac_update, \
mock.patch("quark.db.api.mac_address_update"), mock.patch("quark.db.api.mac_address_range_find") as \
mock.patch("quark.db.api.mac_address_range_find"), range_find, \
mock.patch("quark.db.api.mac_address_delete") mock.patch("quark.db.api.mac_address_delete") as mac_delete:
) as (mac_find, mac_update, range_find, mac_delete):
mac_update.return_value = mac mac_update.return_value = mac
mac_find.return_value = mac mac_find.return_value = mac
range_find.return_value = mac_range range_find.return_value = mac_range
@ -478,18 +484,20 @@ class QuarkIpamTestBothIpAllocation(QuarkIpamBaseTest):
def _stubs(self, addresses=None, subnets=None): def _stubs(self, addresses=None, subnets=None):
if not addresses: if not addresses:
addresses = [None, None] addresses = [None, None]
with contextlib.nested( with mock.patch("quark.db.api.ip_address_reallocate") as \
mock.patch("quark.db.api.ip_address_reallocate"), addr_realloc, \
mock.patch("quark.db.api.ip_address_reallocate_find"), mock.patch("quark.db.api.ip_address_reallocate_find") as \
mock.patch("quark.db.api.ip_address_find"), addr_realloc_find, \
mock.patch("quark.db.api.subnet_find_ordered_by_most_full"), mock.patch("quark.db.api.ip_address_find") as addr_find, \
mock.patch("quark.db.api.subnet_find"), mock.patch("quark.db.api.subnet_find_ordered_by_most_full") \
mock.patch("quark.db.api.subnet_update_next_auto_assign_ip"), as subnet_alloc_find, \
mock.patch("quark.db.api.subnet_update_set_full"), mock.patch("quark.db.api.subnet_find") as subnet_find, \
mock.patch("sqlalchemy.orm.session.Session.refresh") mock.patch("quark.db.api.subnet_update_next_auto_assign_ip") \
) as (addr_realloc, addr_realloc_find, addr_find, as subnet_update, \
subnet_alloc_find, subnet_find, subnet_update, mock.patch("quark.db.api.subnet_update_set_full") as \
subnet_set_full, refresh): subnet_set_full, \
mock.patch("sqlalchemy.orm.session.Session.refresh") as \
refresh:
addr_mods = [] addr_mods = []
sub_mods = [] sub_mods = []
for a in addresses: for a in addresses:
@ -887,16 +895,19 @@ class QuarkIpamTestBothRequiredIpAllocation(QuarkIpamBaseTest):
if not addresses: if not addresses:
addresses = [None, None] addresses = [None, None]
self.context.session.add = mock.Mock() self.context.session.add = mock.Mock()
with contextlib.nested( with mock.patch("quark.db.api.ip_address_reallocate") as \
mock.patch("quark.db.api.ip_address_reallocate"), addr_realloc, \
mock.patch("quark.db.api.ip_address_reallocate_find"), mock.patch("quark.db.api.ip_address_reallocate_find") as \
mock.patch("quark.db.api.ip_address_find"), addr_realloc_find, \
mock.patch("quark.db.api.subnet_find_ordered_by_most_full"), mock.patch("quark.db.api.ip_address_find") as addr_find, \
mock.patch("quark.db.api.subnet_update_next_auto_assign_ip"), mock.patch("quark.db.api.subnet_find_ordered_by_most_full") \
mock.patch("quark.db.api.subnet_update_set_full"), as subnet_find, \
mock.patch("sqlalchemy.orm.session.Session.refresh") mock.patch("quark.db.api.subnet_update_next_auto_assign_ip") \
) as (addr_realloc, addr_realloc_find, addr_find, as subnet_update, \
subnet_find, subnet_update, subnet_set_full, refresh): mock.patch("quark.db.api.subnet_update_set_full") as \
subnet_set_full, \
mock.patch("sqlalchemy.orm.session.Session.refresh") as \
refresh:
addrs = [ip_helper(a) for a in addresses] addrs = [ip_helper(a) for a in addresses]
addr_realloc.side_effect = addrs[:1] addr_realloc.side_effect = addrs[:1]
addr_realloc_find.side_effect = addrs[:1] addr_realloc_find.side_effect = addrs[:1]
@ -1129,12 +1140,14 @@ class QuarkIpamAllocateFromV6Subnet(QuarkIpamBaseTest):
ip_mod["allocated_at"] = timeutils.utcnow() ip_mod["allocated_at"] = timeutils.utcnow()
ip_mod["version"] = ip_address.version ip_mod["version"] = ip_address.version
with contextlib.nested( with mock.patch("quark.db.models.IPPolicy.get_cidrs_ip_set") as \
mock.patch("quark.db.models.IPPolicy.get_cidrs_ip_set"), policy_find, \
mock.patch("quark.db.api.ip_address_find"), mock.patch("quark.db.api.ip_address_find") as \
mock.patch("quark.db.api.ip_address_create"), ip_address_find, \
mock.patch("quark.db.api.ip_address_update") mock.patch("quark.db.api.ip_address_create") as \
) as (policy_find, ip_address_find, ip_create, ip_update): ip_create, \
mock.patch("quark.db.api.ip_address_update") as \
ip_update:
policy_find.return_value = policies policy_find.return_value = policies
ip_address_find.return_value = ip_mod ip_address_find.return_value = ip_mod
ip_create.return_value = ip_mod ip_create.return_value = ip_mod
@ -1194,16 +1207,19 @@ class QuarkNewIPAddressAllocation(QuarkIpamBaseTest):
if not addresses: if not addresses:
addresses = [None] addresses = [None]
self.context.session.add = mock.Mock() self.context.session.add = mock.Mock()
with contextlib.nested( with mock.patch("quark.db.api.ip_address_reallocate") as \
mock.patch("quark.db.api.ip_address_reallocate"), addr_realloc, \
mock.patch("quark.db.api.ip_address_reallocate_find"), mock.patch("quark.db.api.ip_address_reallocate_find") as \
mock.patch("quark.db.api.ip_address_find"), addr_realloc_find, \
mock.patch("quark.db.api.subnet_find_ordered_by_most_full"), mock.patch("quark.db.api.ip_address_find") as addr_find, \
mock.patch("quark.db.api.subnet_update_next_auto_assign_ip"), mock.patch("quark.db.api.subnet_find_ordered_by_most_full") \
mock.patch("quark.db.api.subnet_update_set_full"), as subnet_find, \
mock.patch("sqlalchemy.orm.session.Session.refresh") mock.patch("quark.db.api.subnet_update_next_auto_assign_ip") \
) as (addr_realloc, addr_realloc_find, addr_find, as subnet_update, \
subnet_find, subnet_update, subnet_set_full, refresh): mock.patch("quark.db.api.subnet_update_set_full") as \
subnet_set_full, \
mock.patch("sqlalchemy.orm.session.Session.refresh") as \
refresh:
addrs = [ip_helper(a) for a in addresses] addrs = [ip_helper(a) for a in addresses]
addr_realloc.side_effect = addrs[:1] addr_realloc.side_effect = addrs[:1]
addr_realloc_find.side_effect = addrs[:1] addr_realloc_find.side_effect = addrs[:1]
@ -1496,15 +1512,16 @@ class QuarkIPAddressAllocationTestRetries(QuarkIpamBaseTest):
@contextlib.contextmanager @contextlib.contextmanager
def _stubs(self, address=None, subnets=None): def _stubs(self, address=None, subnets=None):
self.context.session.add = mock.Mock() self.context.session.add = mock.Mock()
with contextlib.nested( with mock.patch("quark.db.api.ip_address_find") as addr_find, \
mock.patch("quark.db.api.ip_address_find"), mock.patch("quark.db.api.ip_address_create") as addr_create, \
mock.patch("quark.db.api.ip_address_create"), mock.patch("quark.db.api.subnet_find_ordered_by_most_full") \
mock.patch("quark.db.api.subnet_find_ordered_by_most_full"), as subnet_find, \
mock.patch("quark.db.api.subnet_update_next_auto_assign_ip"), mock.patch("quark.db.api.subnet_update_next_auto_assign_ip") \
mock.patch("quark.db.api.subnet_update_set_full"), as subnet_update, \
mock.patch("sqlalchemy.orm.session.Session.refresh") mock.patch("quark.db.api.subnet_update_set_full") as \
) as (addr_find, addr_create, subnet_find, subnet_update, subnet_set_full, \
subnet_set_full, refresh): mock.patch("sqlalchemy.orm.session.Session.refresh") as \
refresh:
addr_find.side_effect = [None, None, None] addr_find.side_effect = [None, None, None]
addr_mods = [] addr_mods = []
for a in address: for a in address:
@ -1635,14 +1652,14 @@ class QuarkIPAddressAllocateDeallocated(QuarkIpamBaseTest):
@contextlib.contextmanager @contextlib.contextmanager
def _stubs(self, ip_find, subnet, address, addresses_found, def _stubs(self, ip_find, subnet, address, addresses_found,
sub_found=True): sub_found=True):
with contextlib.nested( with mock.patch("quark.db.api.ip_address_reallocate") as \
mock.patch("quark.db.api.ip_address_reallocate"), addr_realloc, \
mock.patch("quark.db.api.ip_address_reallocate_find"), mock.patch("quark.db.api.ip_address_reallocate_find") as \
mock.patch("quark.db.api.ip_address_find"), addr_realloc_find, \
mock.patch("quark.db.api.ip_address_update"), mock.patch("quark.db.api.ip_address_find") as addr_find, \
mock.patch("quark.ipam.QuarkIpamANY._choose_available_subnet") mock.patch("quark.db.api.ip_address_update") as addr_update, \
) as (addr_realloc, addr_realloc_find, addr_find, addr_update, mock.patch("quark.ipam.QuarkIpamANY."
choose_subnet): "_choose_available_subnet") as choose_subnet:
addr_mod = models.IPAddress(**address) addr_mod = models.IPAddress(**address)
subnet_mod = models.Subnet(**subnet) subnet_mod = models.Subnet(**subnet)
subnet_mod["next_auto_assign_ip"] = subnet["next_auto_assign_ip"] subnet_mod["next_auto_assign_ip"] = subnet["next_auto_assign_ip"]
@ -1703,13 +1720,15 @@ class TestQuarkIpPoliciesIpAllocation(QuarkIpamBaseTest):
if not addresses: if not addresses:
addresses = [None] addresses = [None]
self.context.session.add = mock.Mock() self.context.session.add = mock.Mock()
with contextlib.nested( with mock.patch("quark.db.api.ip_address_find") as addr_find, \
mock.patch("quark.db.api.ip_address_find"), mock.patch("quark.db.api.subnet_find_ordered_by_most_full") \
mock.patch("quark.db.api.subnet_find_ordered_by_most_full"), as subnet_find, \
mock.patch("quark.db.api.subnet_update_next_auto_assign_ip"), mock.patch("quark.db.api.subnet_update_next_auto_assign_ip") \
mock.patch("quark.db.api.subnet_update_set_full"), as subnet_update, \
mock.patch("sqlalchemy.orm.session.Session.refresh") mock.patch("quark.db.api.subnet_update_set_full") as \
) as (addr_find, subnet_find, subnet_update, subnet_set_full, refresh): subnet_set_full, \
mock.patch("sqlalchemy.orm.session.Session.refresh") as \
refresh:
addr_find.side_effect = [ip_helper(a) for a in addresses] addr_find.side_effect = [ip_helper(a) for a in addresses]
sub_mods = [] sub_mods = []
if subnets: if subnets:
@ -1813,16 +1832,16 @@ class QuarkIPAddressAllocationNotifications(QuarkIpamBaseTest):
def _stubs(self, address, addresses=None, subnets=None, deleted_at=None): def _stubs(self, address, addresses=None, subnets=None, deleted_at=None):
if not addresses: if not addresses:
addresses = [None] addresses = [None]
with contextlib.nested( with mock.patch("quark.db.api.ip_address_find") as addr_find, \
mock.patch("quark.db.api.ip_address_find"), mock.patch("quark.db.api.ip_address_create") as addr_create, \
mock.patch("quark.db.api.ip_address_create"), mock.patch("quark.db.api.subnet_find_ordered_by_most_full") \
mock.patch("quark.db.api.subnet_find_ordered_by_most_full"), as subnet_find, \
mock.patch("quark.db.api.subnet_update_next_auto_assign_ip"), mock.patch("quark.db.api.subnet_update_next_auto_assign_ip") \
mock.patch("sqlalchemy.orm.session.Session.refresh"), as subnet_update, \
mock.patch("neutron.common.rpc.get_notifier"), mock.patch("sqlalchemy.orm.session.Session.refresh") as \
mock.patch("oslo_utils.timeutils.utcnow"), refresh, \
) as (addr_find, addr_create, subnet_find, subnet_update, refresh, mock.patch("neutron.common.rpc.get_notifier") as notify, \
notify, timeutils): mock.patch("oslo_utils.timeutils.utcnow") as timeutils:
addrs_found = [] addrs_found = []
for a in addresses: for a in addresses:
if a: if a:
@ -1952,12 +1971,14 @@ class QuarkIpamTestV6IpGeneration(QuarkIpamBaseTest):
class QuarkIpamTestSelectSubnet(QuarkIpamBaseTest): class QuarkIpamTestSelectSubnet(QuarkIpamBaseTest):
@contextlib.contextmanager @contextlib.contextmanager
def _stubs(self, subnet, count, increments=True, marks_full=True): def _stubs(self, subnet, count, increments=True, marks_full=True):
with contextlib.nested( with mock.patch("quark.db.api.subnet_find_ordered_by_most_full") \
mock.patch("quark.db.api.subnet_find_ordered_by_most_full"), as subnet_find, \
mock.patch("quark.db.api.subnet_update_next_auto_assign_ip"), mock.patch("quark.db.api.subnet_update_next_auto_assign_ip") \
mock.patch("quark.db.api.subnet_update_set_full"), as subnet_incr, \
mock.patch("sqlalchemy.orm.session.Session.refresh"), mock.patch("quark.db.api.subnet_update_set_full") as \
) as (subnet_find, subnet_incr, subnet_set_full, refresh): subnet_set_full, \
mock.patch("sqlalchemy.orm.session.Session.refresh") as \
refresh:
sub_mods = [] sub_mods = []
sub_mods.append((subnet_helper(subnet), count)) sub_mods.append((subnet_helper(subnet), count))
@ -2049,12 +2070,13 @@ class QuarkIpamTestSelectSubnet(QuarkIpamBaseTest):
class QuarkIpamTestSelectSubnetLocking(QuarkIpamBaseTest): class QuarkIpamTestSelectSubnetLocking(QuarkIpamBaseTest):
@contextlib.contextmanager @contextlib.contextmanager
def _stubs(self, subnet, count, increments=True, marks_full=True): def _stubs(self, subnet, count, increments=True, marks_full=True):
with contextlib.nested( with mock.patch("quark.db.api.subnet_find_ordered_by_most_full") as \
mock.patch("quark.db.api.subnet_find_ordered_by_most_full"), subnet_find, \
mock.patch("quark.db.api.subnet_update_next_auto_assign_ip"), mock.patch("quark.db.api.subnet_update_next_auto_assign_ip") \
mock.patch("quark.db.api.subnet_update_set_full"), as subnet_incr, \
mock.patch("sqlalchemy.orm.session.Session.refresh"), mock.patch("quark.db.api.subnet_update_set_full") as \
) as (subnet_find, subnet_incr, subnet_set_full, refresh): subnet_set_full, \
mock.patch("sqlalchemy.orm.session.Session.refresh"):
sub_mods = [] sub_mods = []
sub_mods.append((subnet_helper(subnet), count)) sub_mods.append((subnet_helper(subnet), count))
@ -2228,12 +2250,13 @@ class IronicIpamTestSelectSubnet(QuarkIpamBaseTest):
@contextlib.contextmanager @contextlib.contextmanager
def _stubs(self, subnet, count, increments=True, marks_full=True): def _stubs(self, subnet, count, increments=True, marks_full=True):
with contextlib.nested( with mock.patch("quark.db.api.subnet_find_unused") as subnet_find, \
mock.patch("quark.db.api.subnet_find_unused"), mock.patch("quark.db.api.subnet_update_next_auto_assign_ip") \
mock.patch("quark.db.api.subnet_update_next_auto_assign_ip"), as subnet_incr, \
mock.patch("quark.db.api.subnet_update_set_full"), mock.patch("quark.db.api.subnet_update_set_full") as \
mock.patch("sqlalchemy.orm.session.Session.refresh"), subnet_set_full, \
) as (subnet_find, subnet_incr, subnet_set_full, refresh): mock.patch("sqlalchemy.orm.session.Session.refresh") as \
refresh:
sub_mods = [] sub_mods = []
sub_mods.append((subnet_helper(subnet), count)) sub_mods.append((subnet_helper(subnet), count))

View File

@ -1,4 +1,3 @@
import contextlib
import datetime import datetime
import os import os
import tempfile import tempfile
@ -96,10 +95,8 @@ class Test2748e48cee3a(BaseMigrationTest):
dict(id="222", created_at=datetime.date(1970, 1, 1), dict(id="222", created_at=datetime.date(1970, 1, 1),
ip_policy_id="111", cidr="192.168.10.0/16")) ip_policy_id="111", cidr="192.168.10.0/16"))
with contextlib.nested( with mock.patch("oslo_utils.uuidutils") as uuid, \
mock.patch("oslo_utils.uuidutils"), mock.patch("oslo_utils.timeutils") as tu:
mock.patch("oslo_utils.timeutils")
) as (uuid, tu):
tu.utcnow.return_value = datetime.datetime(2004, 2, 14) tu.utcnow.return_value = datetime.datetime(2004, 2, 14)
uuid.generate_uuid.return_value = "foo" uuid.generate_uuid.return_value = "foo"
alembic_command.upgrade(self.config, '2748e48cee3a') alembic_command.upgrade(self.config, '2748e48cee3a')
@ -121,10 +118,8 @@ class Test2748e48cee3a(BaseMigrationTest):
dict(id="222", created_at=datetime.date(1970, 1, 1), dict(id="222", created_at=datetime.date(1970, 1, 1),
ip_policy_id="111", cidr="fd00::/7")) ip_policy_id="111", cidr="fd00::/7"))
with contextlib.nested( with mock.patch("oslo_utils.uuidutils") as uuid, \
mock.patch("oslo_utils.uuidutils"), mock.patch("oslo_utils.timeutils") as tu:
mock.patch("oslo_utils.timeutils")
) as (uuid, tu):
tu.utcnow.return_value = datetime.datetime(2004, 2, 14) tu.utcnow.return_value = datetime.datetime(2004, 2, 14)
uuid.generate_uuid.return_value = "foo" uuid.generate_uuid.return_value = "foo"
alembic_command.upgrade(self.config, '2748e48cee3a') alembic_command.upgrade(self.config, '2748e48cee3a')
@ -168,10 +163,8 @@ class Test2748e48cee3a(BaseMigrationTest):
dict(id="223", created_at=dt, ip_policy_id="113", dict(id="223", created_at=dt, ip_policy_id="113",
cidr="0.0.0.0/24")) cidr="0.0.0.0/24"))
with contextlib.nested( with mock.patch("oslo_utils.uuidutils") as uuid, \
mock.patch("oslo_utils.uuidutils"), mock.patch("oslo_utils.timeutils") as tu:
mock.patch("oslo_utils.timeutils")
) as (uuid, tu):
tu.utcnow.return_value = datetime.datetime(2004, 2, 14) tu.utcnow.return_value = datetime.datetime(2004, 2, 14)
uuid.generate_uuid.return_value = "foo" uuid.generate_uuid.return_value = "foo"
alembic_command.upgrade(self.config, '2748e48cee3a') alembic_command.upgrade(self.config, '2748e48cee3a')
@ -202,10 +195,8 @@ class Test2748e48cee3a(BaseMigrationTest):
dict(id="223", created_at=datetime.date(1970, 1, 1), dict(id="223", created_at=datetime.date(1970, 1, 1),
ip_policy_id="111", cidr="192.168.10.0/23")) ip_policy_id="111", cidr="192.168.10.0/23"))
with contextlib.nested( with mock.patch("oslo_utils.uuidutils") as uuid, \
mock.patch("oslo_utils.uuidutils"), mock.patch("oslo_utils.timeutils") as tu:
mock.patch("oslo_utils.timeutils")
) as (uuid, tu):
tu.utcnow.return_value = datetime.datetime(2004, 2, 14) tu.utcnow.return_value = datetime.datetime(2004, 2, 14)
uuid.generate_uuid.return_value = "foo" uuid.generate_uuid.return_value = "foo"
alembic_command.upgrade(self.config, '2748e48cee3a') alembic_command.upgrade(self.config, '2748e48cee3a')
@ -276,10 +267,8 @@ class Test45a07fac3d38(BaseMigrationTest):
self.ip_policy_cidrs.insert(), self.ip_policy_cidrs.insert(),
dict(id="222", created_at=datetime.date(1970, 1, 1), dict(id="222", created_at=datetime.date(1970, 1, 1),
ip_policy_id="111", cidr="192.168.10.13/32")) ip_policy_id="111", cidr="192.168.10.13/32"))
with contextlib.nested( with mock.patch("oslo_utils.uuidutils") as uuid, \
mock.patch("oslo_utils.uuidutils"), mock.patch("oslo_utils.timeutils") as tu:
mock.patch("oslo_utils.timeutils")
) as (uuid, tu):
uuid.generate_uuid.side_effect = (1, 2, 3) uuid.generate_uuid.side_effect = (1, 2, 3)
tu.utcnow.return_value = datetime.datetime(1970, 1, 1) tu.utcnow.return_value = datetime.datetime(1970, 1, 1)
alembic_command.upgrade(self.config, '45a07fac3d38') alembic_command.upgrade(self.config, '45a07fac3d38')
@ -305,10 +294,8 @@ class Test45a07fac3d38(BaseMigrationTest):
self.ip_policy_cidrs.insert(), self.ip_policy_cidrs.insert(),
dict(id="222", created_at=datetime.date(1970, 1, 1), dict(id="222", created_at=datetime.date(1970, 1, 1),
ip_policy_id="111", cidr="fd00::3/128")) ip_policy_id="111", cidr="fd00::3/128"))
with contextlib.nested( with mock.patch("oslo_utils.uuidutils") as uuid, \
mock.patch("oslo_utils.uuidutils"), mock.patch("oslo_utils.timeutils") as tu:
mock.patch("oslo_utils.timeutils")
) as (uuid, tu):
uuid.generate_uuid.side_effect = (1, 2, 3) uuid.generate_uuid.side_effect = (1, 2, 3)
tu.utcnow.return_value = datetime.datetime(1970, 1, 1) tu.utcnow.return_value = datetime.datetime(1970, 1, 1)
alembic_command.upgrade(self.config, '45a07fac3d38') alembic_command.upgrade(self.config, '45a07fac3d38')
@ -479,10 +466,8 @@ class Test552b213c2b8c(BaseMigrationTest):
self.subnets.insert(), self.subnets.insert(),
dict(id="000", tenant_id="foo", _cidr="192.168.10.0/24", dict(id="000", tenant_id="foo", _cidr="192.168.10.0/24",
ip_policy_id=None)) ip_policy_id=None))
with contextlib.nested( with mock.patch("oslo_utils.uuidutils") as uuid, \
mock.patch("oslo_utils.uuidutils"), mock.patch("oslo_utils.timeutils") as tu:
mock.patch("oslo_utils.timeutils")
) as (uuid, tu):
dt = datetime.datetime(1970, 1, 1) dt = datetime.datetime(1970, 1, 1)
tu.utcnow.return_value = dt tu.utcnow.return_value = dt
uuid.generate_uuid.side_effect = ("666", "667", "668") uuid.generate_uuid.side_effect = ("666", "667", "668")
@ -514,10 +499,8 @@ class Test552b213c2b8c(BaseMigrationTest):
self.subnets.insert(), self.subnets.insert(),
dict(id="000", tenant_id="foo", _cidr="fd00::/64", dict(id="000", tenant_id="foo", _cidr="fd00::/64",
ip_policy_id=None)) ip_policy_id=None))
with contextlib.nested( with mock.patch("oslo_utils.uuidutils") as uuid, \
mock.patch("oslo_utils.uuidutils"), mock.patch("oslo_utils.timeutils") as tu:
mock.patch("oslo_utils.timeutils")
) as (uuid, tu):
dt = datetime.datetime(1970, 1, 1) dt = datetime.datetime(1970, 1, 1)
tu.utcnow.return_value = dt tu.utcnow.return_value = dt
uuid.generate_uuid.side_effect = ("666", "667", "668") uuid.generate_uuid.side_effect = ("666", "667", "668")
@ -563,10 +546,8 @@ class Test552b213c2b8c(BaseMigrationTest):
dict(id="221", created_at=dt, dict(id="221", created_at=dt,
ip_policy_id="111", cidr="192.168.10.13/32")) ip_policy_id="111", cidr="192.168.10.13/32"))
with contextlib.nested( with mock.patch("oslo_utils.uuidutils") as uuid, \
mock.patch("oslo_utils.uuidutils"), mock.patch("oslo_utils.timeutils") as tu:
mock.patch("oslo_utils.timeutils")
) as (uuid, tu):
tu.utcnow.return_value = dt tu.utcnow.return_value = dt
uuid.generate_uuid.side_effect = ("5", "6", "7", "8", "9", "10") uuid.generate_uuid.side_effect = ("5", "6", "7", "8", "9", "10")
alembic_command.upgrade(self.config, '552b213c2b8c') alembic_command.upgrade(self.config, '552b213c2b8c')

View File

@ -123,9 +123,7 @@ class TestNVPDriver(test_base.TestBase):
class TestNVPDriverCreateNetwork(TestNVPDriver): class TestNVPDriverCreateNetwork(TestNVPDriver):
@contextlib.contextmanager @contextlib.contextmanager
def _stubs(self): def _stubs(self):
with contextlib.nested( with mock.patch("%s._connection" % self.d_pkg) as conn:
mock.patch("%s._connection" % self.d_pkg),
) as (conn,):
connection = self._create_connection() connection = self._create_connection()
conn.return_value = connection conn.return_value = connection
yield connection yield connection
@ -164,12 +162,11 @@ class TestNVPDriverDefaultTransportZoneBindings(TestNVPDriver):
@contextlib.contextmanager @contextlib.contextmanager
def _stubs(self): def _stubs(self):
with contextlib.nested( with mock.patch("quark.drivers.nvp_driver.SA_REGISTRY."
mock.patch("quark.drivers.nvp_driver.SA_REGISTRY." "get_strategy") as sa_get_strategy, \
"get_strategy"), mock.patch("%s._connection" % self.d_pkg) as conn, \
mock.patch("%s._connection" % self.d_pkg), mock.patch("%s._lswitches_for_network" % self.d_pkg) as \
mock.patch("%s._lswitches_for_network" % self.d_pkg), switch_list:
) as (sa_get_strategy, conn, switch_list):
connection = self._create_connection() connection = self._create_connection()
conn.return_value = connection conn.return_value = connection
@ -216,9 +213,7 @@ class TestNVPDriverProviderNetwork(TestNVPDriver):
@contextlib.contextmanager @contextlib.contextmanager
def _stubs(self, tz): def _stubs(self, tz):
with contextlib.nested( with mock.patch("%s._connection" % self.d_pkg) as conn:
mock.patch("%s._connection" % self.d_pkg),
) as (conn,):
connection = self._create_connection() connection = self._create_connection()
switch = self._create_lswitch(1, False) switch = self._create_lswitch(1, False)
switch.transport_zone = mock.Mock() switch.transport_zone = mock.Mock()
@ -351,10 +346,9 @@ class TestNVPDriverProviderNetwork(TestNVPDriver):
class TestNVPDriverDeleteNetwork(TestNVPDriver): class TestNVPDriverDeleteNetwork(TestNVPDriver):
@contextlib.contextmanager @contextlib.contextmanager
def _stubs(self, network_exists=True): def _stubs(self, network_exists=True):
with contextlib.nested( with mock.patch("%s._connection" % self.d_pkg) as conn, \
mock.patch("%s._connection" % self.d_pkg), mock.patch("%s._lswitches_for_network" % self.d_pkg) as \
mock.patch("%s._lswitches_for_network" % self.d_pkg), switch_list:
) as (conn, switch_list):
connection = self._create_connection() connection = self._create_connection()
conn.return_value = connection conn.return_value = connection
if network_exists: if network_exists:
@ -383,11 +377,10 @@ class TestNVPDriverDeleteNetwork(TestNVPDriver):
class TestNVPDriverDeleteNetworkWithExceptions(TestNVPDriver): class TestNVPDriverDeleteNetworkWithExceptions(TestNVPDriver):
@contextlib.contextmanager @contextlib.contextmanager
def _stubs(self, network_exists=True, exception=None): def _stubs(self, network_exists=True, exception=None):
with contextlib.nested( with mock.patch("%s._connection" % self.d_pkg) as conn, \
mock.patch("%s._connection" % self.d_pkg), mock.patch("%s._lswitches_for_network" % self.d_pkg) as \
mock.patch("%s._lswitches_for_network" % self.d_pkg), switch_list, \
mock.patch("%s._lswitch_delete" % self.d_pkg), mock.patch("%s._lswitch_delete" % self.d_pkg) as switch_delete:
) as (conn, switch_list, switch_delete):
connection = self._create_connection() connection = self._create_connection()
conn.return_value = connection conn.return_value = connection
if network_exists: if network_exists:
@ -437,12 +430,12 @@ class TestNVPDriverCreatePort(TestNVPDriver):
'''In all cases an lswitch should be queried.''' '''In all cases an lswitch should be queried.'''
@contextlib.contextmanager @contextlib.contextmanager
def _stubs(self, has_lswitch=True, maxed_ports=False, net_details=None): def _stubs(self, has_lswitch=True, maxed_ports=False, net_details=None):
with contextlib.nested( with mock.patch("%s._connection" % self.d_pkg) as conn, \
mock.patch("%s._connection" % self.d_pkg), mock.patch("%s._next_connection" % self.d_pkg), \
mock.patch("%s._next_connection" % self.d_pkg), mock.patch("%s._lswitches_for_network" % self.d_pkg) as \
mock.patch("%s._lswitches_for_network" % self.d_pkg), get_switches, \
mock.patch("%s._get_network_details" % self.d_pkg), mock.patch("%s._get_network_details" % self.d_pkg) as \
) as (conn, next_conn, get_switches, get_net_dets): get_net_dets:
connection = self._create_connection(has_switches=has_lswitch, connection = self._create_connection(has_switches=has_lswitch,
maxed_ports=maxed_ports) maxed_ports=maxed_ports)
conn.return_value = connection conn.return_value = connection
@ -591,10 +584,8 @@ class TestNVPDriverCreatePort(TestNVPDriver):
class TestNVPDriverUpdatePort(TestNVPDriver): class TestNVPDriverUpdatePort(TestNVPDriver):
@contextlib.contextmanager @contextlib.contextmanager
def _stubs(self): def _stubs(self):
with contextlib.nested( with mock.patch("%s._connection" % self.d_pkg) as conn, \
mock.patch("%s._connection" % self.d_pkg), mock.patch("%s._next_connection" % self.d_pkg):
mock.patch("%s._next_connection" % self.d_pkg),
) as (conn, next_conn):
connection = self._create_connection() connection = self._create_connection()
connection.securityprofile = self._create_security_profile() connection.securityprofile = self._create_security_profile()
conn.return_value = connection conn.return_value = connection
@ -629,9 +620,7 @@ class TestNVPDriverUpdatePort(TestNVPDriver):
class TestNVPDriverLswitchesForNetwork(TestNVPDriver): class TestNVPDriverLswitchesForNetwork(TestNVPDriver):
@contextlib.contextmanager @contextlib.contextmanager
def _stubs(self, single_switch=True): def _stubs(self, single_switch=True):
with contextlib.nested( with mock.patch("%s._connection" % self.d_pkg) as conn:
mock.patch("%s._connection" % self.d_pkg),
) as (conn,):
connection = self._create_connection( connection = self._create_connection(
has_switches=True, switch_count=1) has_switches=True, switch_count=1)
conn.return_value = connection conn.return_value = connection
@ -696,10 +685,8 @@ class TestSwitchCopying(TestNVPDriver):
class TestNVPDriverDeletePort(TestNVPDriver): class TestNVPDriverDeletePort(TestNVPDriver):
@contextlib.contextmanager @contextlib.contextmanager
def _stubs(self, switch_count=1): def _stubs(self, switch_count=1):
with contextlib.nested( with mock.patch("%s._connection" % self.d_pkg) as conn, \
mock.patch("%s._connection" % self.d_pkg), mock.patch("%s._next_connection" % self.d_pkg):
mock.patch("%s._next_connection" % self.d_pkg),
) as (conn, next_conn):
connection = self._create_connection(switch_count=switch_count) connection = self._create_connection(switch_count=switch_count)
conn.return_value = connection conn.return_value = connection
yield connection yield connection
@ -736,10 +723,8 @@ class TestNVPDriverDeletePort(TestNVPDriver):
class TestNVPDriverDeletePortWithExceptions(TestNVPDriver): class TestNVPDriverDeletePortWithExceptions(TestNVPDriver):
@contextlib.contextmanager @contextlib.contextmanager
def _stubs(self, switch_exception=None, delete_exception=None): def _stubs(self, switch_exception=None, delete_exception=None):
with contextlib.nested( with mock.patch("%s._connection" % self.d_pkg) as conn, \
mock.patch("%s._connection" % self.d_pkg), mock.patch("%s._lswitch_from_port" % self.d_pkg) as switch:
mock.patch("%s._lswitch_from_port" % self.d_pkg),
) as (conn, switch):
connection = self._create_connection() connection = self._create_connection()
conn.return_value = connection conn.return_value = connection
if switch_exception: if switch_exception:
@ -820,10 +805,8 @@ class TestNVPDriverDeletePortWithExceptions(TestNVPDriver):
class TestNVPDriverCreateSecurityGroup(TestNVPDriver): class TestNVPDriverCreateSecurityGroup(TestNVPDriver):
@contextlib.contextmanager @contextlib.contextmanager
def _stubs(self): def _stubs(self):
with contextlib.nested( with mock.patch("%s._connection" % self.d_pkg) as conn, \
mock.patch("%s._connection" % self.d_pkg), mock.patch("%s._next_connection" % self.d_pkg):
mock.patch("%s._next_connection" % self.d_pkg),
) as (conn, next_conn):
connection = self._create_connection() connection = self._create_connection()
connection.securityprofile = self._create_security_profile() connection.securityprofile = self._create_security_profile()
conn.return_value = connection conn.return_value = connection
@ -875,10 +858,8 @@ class TestNVPDriverCreateSecurityGroup(TestNVPDriver):
class TestNVPDriverDeleteSecurityGroup(TestNVPDriver): class TestNVPDriverDeleteSecurityGroup(TestNVPDriver):
@contextlib.contextmanager @contextlib.contextmanager
def _stubs(self): def _stubs(self):
with contextlib.nested( with mock.patch("%s._connection" % self.d_pkg) as conn, \
mock.patch("%s._connection" % self.d_pkg), mock.patch("%s._next_connection" % self.d_pkg):
mock.patch("%s._next_connection" % self.d_pkg),
) as (conn, next_conn):
connection = self._create_connection() connection = self._create_connection()
connection.securityprofile = self._create_security_profile() connection.securityprofile = self._create_security_profile()
conn.return_value = connection conn.return_value = connection
@ -905,10 +886,8 @@ class TestNVPDriverDeleteSecurityGroup(TestNVPDriver):
class TestNVPDriverUpdateSecurityGroup(TestNVPDriver): class TestNVPDriverUpdateSecurityGroup(TestNVPDriver):
@contextlib.contextmanager @contextlib.contextmanager
def _stubs(self): def _stubs(self):
with contextlib.nested( with mock.patch("%s._connection" % self.d_pkg) as conn, \
mock.patch("%s._connection" % self.d_pkg), mock.patch("%s._next_connection" % self.d_pkg):
mock.patch("%s._next_connection" % self.d_pkg),
) as (conn, next_conn):
connection = self._create_connection() connection = self._create_connection()
connection.securityprofile = self._create_security_profile() connection.securityprofile = self._create_security_profile()
conn.return_value = connection conn.return_value = connection
@ -966,10 +945,8 @@ class TestNVPDriverUpdateSecurityGroup(TestNVPDriver):
class TestNVPDriverCreateSecurityGroupRule(TestNVPDriver): class TestNVPDriverCreateSecurityGroupRule(TestNVPDriver):
@contextlib.contextmanager @contextlib.contextmanager
def _stubs(self): def _stubs(self):
with contextlib.nested( with mock.patch("%s._connection" % self.d_pkg) as conn, \
mock.patch("%s._connection" % self.d_pkg), mock.patch("%s._next_connection" % self.d_pkg):
mock.patch("%s._next_connection" % self.d_pkg),
) as (conn, next_conn):
connection = self._create_connection() connection = self._create_connection()
connection.securityprofile = self._create_security_profile() connection.securityprofile = self._create_security_profile()
connection.securityrule = self._create_security_rule() connection.securityrule = self._create_security_rule()
@ -1050,9 +1027,7 @@ class TestNVPDriverDeleteSecurityGroupRule(TestNVPDriver):
for rule in rules: for rule in rules:
rulelist['logical_port_%s_rules' % rule.pop('direction')].append( rulelist['logical_port_%s_rules' % rule.pop('direction')].append(
rule) rule)
with contextlib.nested( with mock.patch("%s._connection" % self.d_pkg) as conn:
mock.patch("%s._connection" % self.d_pkg),
) as (conn,):
connection = self._create_connection() connection = self._create_connection()
connection.securityprofile = self._create_security_profile() connection.securityprofile = self._create_security_profile()
connection.securityrule = self._create_security_rule() connection.securityrule = self._create_security_rule()
@ -1135,10 +1110,8 @@ class TestNVPGetConnection(TestNVPDriver):
retries=1, retries=1,
backoff=0, backoff=0,
usages=0)) usages=0))
with contextlib.nested( with mock.patch("aiclib.nvp.Connection") as aiclib_conn, \
mock.patch("aiclib.nvp.Connection"), mock.patch("%s._next_connection" % self.d_pkg) as next_conn:
mock.patch("%s._next_connection" % self.d_pkg)
) as (aiclib_conn, next_conn):
yield aiclib_conn, next_conn yield aiclib_conn, next_conn
cfg.CONF.clear_override("controller_connection", "NVP") cfg.CONF.clear_override("controller_connection", "NVP")
@ -1208,9 +1181,7 @@ class TestNVPNextConnection(TestNVPDriver):
conn2["ip_address"] = "192.168.0.2" conn2["ip_address"] = "192.168.0.2"
self.driver.nvp_connections.extend([conn1, conn2]) self.driver.nvp_connections.extend([conn1, conn2])
with contextlib.nested( with mock.patch("random.randint") as randint:
mock.patch("random.randint")
) as (randint,):
randint.return_value = 1 randint.return_value = 1
yield randint yield randint
cfg.CONF.clear_override("controller_connection", "NVP") cfg.CONF.clear_override("controller_connection", "NVP")

View File

@ -51,11 +51,11 @@ class TestOptimizedNVPDriverDeleteNetwork(TestOptimizedNVPDriver):
'''Need to ensure that network of X switches deletes X switches.''' '''Need to ensure that network of X switches deletes X switches.'''
@contextlib.contextmanager @contextlib.contextmanager
def _stubs(self, switch_count=1): def _stubs(self, switch_count=1):
with contextlib.nested( with mock.patch("%s._connection" % self.d_pkg) as conn, \
mock.patch("%s._connection" % self.d_pkg), mock.patch("%s._lswitch_select_by_nvp_id" % self.d_pkg) as \
mock.patch("%s._lswitch_select_by_nvp_id" % self.d_pkg), select_switch, \
mock.patch("%s._lswitches_for_network" % self.d_pkg), mock.patch("%s._lswitches_for_network" % self.d_pkg) as \
) as (conn, select_switch, get_switches): get_switches:
connection = self._create_connection() connection = self._create_connection()
switch = self._create_lswitch_mock() switch = self._create_lswitch_mock()
conn.return_value = connection conn.return_value = connection
@ -104,12 +104,12 @@ class TestOptimizedNVPDriverDeleteNetworkWithExceptions(
@contextlib.contextmanager @contextlib.contextmanager
def _stubs(self, switch_count=1, error_code=500): def _stubs(self, switch_count=1, error_code=500):
with contextlib.nested( with mock.patch("%s._connection" % self.d_pkg) as conn, \
mock.patch("%s._connection" % self.d_pkg), mock.patch("%s._lswitch_select_by_nvp_id" % self.d_pkg) as \
mock.patch("%s._lswitch_select_by_nvp_id" % self.d_pkg), select_switch, \
mock.patch("%s._lswitches_for_network" % self.d_pkg), mock.patch("%s._lswitches_for_network" % self.d_pkg) as \
mock.patch("%s._lswitch_delete" % self.d_pkg) get_switches, \
) as (conn, select_switch, get_switches, delete_switch): mock.patch("%s._lswitch_delete" % self.d_pkg) as delete_switch:
connection = self._create_connection() connection = self._create_connection()
switch = self._create_lswitch_mock() switch = self._create_lswitch_mock()
conn.return_value = connection conn.return_value = connection
@ -150,14 +150,14 @@ class TestOptimizedNVPDriverDeletePortMultiSwitch(TestOptimizedNVPDriver):
@contextlib.contextmanager @contextlib.contextmanager
def _stubs(self, port_count=2, exception=None): def _stubs(self, port_count=2, exception=None):
with contextlib.nested( with mock.patch("%s._connection" % self.d_pkg) as conn, \
mock.patch("%s._connection" % self.d_pkg), mock.patch("%s._lport_select_by_id" % self.d_pkg) as \
mock.patch("%s._lport_select_by_id" % self.d_pkg), select_port, \
mock.patch("%s._lswitch_select_by_nvp_id" % self.d_pkg), mock.patch("%s._lswitch_select_by_nvp_id" % self.d_pkg) as \
mock.patch("%s._lswitches_for_network" % self.d_pkg), select_switch, \
mock.patch("%s._lport_delete" % self.d_pkg), mock.patch("%s._lswitches_for_network" % self.d_pkg) as \
) as (conn, select_port, select_switch, two_switch, \
two_switch, port_delete): mock.patch("%s._lport_delete" % self.d_pkg) as port_delete:
connection = self._create_connection() connection = self._create_connection()
port = self._create_lport_mock(port_count) port = self._create_lport_mock(port_count)
switch = self._create_lswitch_mock() switch = self._create_lswitch_mock()
@ -243,12 +243,13 @@ class TestOptimizedNVPDriverDeletePortSingleSwitch(TestOptimizedNVPDriver):
@contextlib.contextmanager @contextlib.contextmanager
def _stubs(self, port_count=1): def _stubs(self, port_count=1):
with contextlib.nested( with mock.patch("%s._connection" % self.d_pkg) as conn, \
mock.patch("%s._connection" % self.d_pkg), mock.patch("%s._lport_select_by_id" % self.d_pkg) as \
mock.patch("%s._lport_select_by_id" % self.d_pkg), select_port, \
mock.patch("%s._lswitch_select_by_nvp_id" % self.d_pkg), mock.patch("%s._lswitch_select_by_nvp_id" % self.d_pkg) as \
mock.patch("%s._lswitches_for_network" % self.d_pkg), select_switch, \
) as (conn, select_port, select_switch, one_switch): mock.patch("%s._lswitches_for_network" % self.d_pkg) as \
one_switch:
connection = self._create_connection() connection = self._create_connection()
switch = self._create_lswitch_mock() switch = self._create_lswitch_mock()
conn.return_value = connection conn.return_value = connection
@ -271,12 +272,13 @@ class TestOptimizedNVPDriverDeletePortMissing(TestOptimizedNVPDriver):
@contextlib.contextmanager @contextlib.contextmanager
def _stubs(self, port_count=2): def _stubs(self, port_count=2):
with contextlib.nested( with mock.patch("%s._connection" % self.d_pkg) as conn, \
mock.patch("%s._connection" % self.d_pkg), mock.patch("%s._lport_select_by_id" % self.d_pkg) as \
mock.patch("%s._lport_select_by_id" % self.d_pkg), select_port, \
mock.patch("%s._lswitch_select_by_nvp_id" % self.d_pkg), mock.patch("%s._lswitch_select_by_nvp_id" % self.d_pkg) as \
mock.patch("%s._lswitches_for_network" % self.d_pkg), select_switch, \
) as (conn, select_port, select_switch, one_switch): mock.patch("%s._lswitches_for_network" % self.d_pkg) as \
one_switch:
connection = self._create_connection() connection = self._create_connection()
port = self._create_lport_mock(port_count) port = self._create_lport_mock(port_count)
switch = self._create_lswitch_mock() switch = self._create_lswitch_mock()
@ -301,15 +303,17 @@ class TestOptimizedNVPDriverCreatePort(TestOptimizedNVPDriver):
@contextlib.contextmanager @contextlib.contextmanager
def _stubs(self, has_lswitch=True, maxed_ports=False): def _stubs(self, has_lswitch=True, maxed_ports=False):
with contextlib.nested( with mock.patch("%s._connection" % self.d_pkg) as conn, \
mock.patch("%s._connection" % self.d_pkg), mock.patch("%s._lswitch_select_free" % self.d_pkg) as \
mock.patch("%s._lswitch_select_free" % self.d_pkg), select_free, \
mock.patch("%s._lswitch_select_first" % self.d_pkg), mock.patch("%s._lswitch_select_first" % self.d_pkg) as \
mock.patch("%s._lswitch_select_by_nvp_id" % self.d_pkg), select_first, \
mock.patch("%s._lswitch_create_optimized" % self.d_pkg), mock.patch("%s._lswitch_select_by_nvp_id" % self.d_pkg) as \
mock.patch("%s._get_network_details" % self.d_pkg) select_by_id, \
) as (conn, select_free, select_first, mock.patch("%s._lswitch_create_optimized" % self.d_pkg) as \
select_by_id, create_opt, get_net_dets): create_opt, \
mock.patch("%s._get_network_details" % self.d_pkg) as \
get_net_dets:
connection = self._create_connection() connection = self._create_connection()
conn.return_value = connection conn.return_value = connection
if has_lswitch: if has_lswitch:
@ -446,10 +450,9 @@ class TestOptimizedNVPDriverUpdatePort(TestOptimizedNVPDriver):
mod_path = "quark.drivers.%s" mod_path = "quark.drivers.%s"
op_path = "optimized_nvp_driver.OptimizedNVPDriver" op_path = "optimized_nvp_driver.OptimizedNVPDriver"
lport_path = "%s._lport_select_by_id" % op_path lport_path = "%s._lport_select_by_id" % op_path
with contextlib.nested( with mock.patch(mod_path % "nvp_driver.NVPDriver.update_port") as \
mock.patch(mod_path % "nvp_driver.NVPDriver.update_port"), update_port, \
mock.patch(mod_path % lport_path), mock.patch(mod_path % lport_path) as port_find:
) as (update_port, port_find):
ret_port = quark.drivers.optimized_nvp_driver.LSwitchPort() ret_port = quark.drivers.optimized_nvp_driver.LSwitchPort()
port_find.return_value = ret_port port_find.return_value = ret_port
update_port.return_value = dict(switch_id=2) update_port.return_value = dict(switch_id=2)
@ -467,10 +470,9 @@ class TestCreateSecurityGroups(TestOptimizedNVPDriver):
class TestDeleteSecurityGroups(TestOptimizedNVPDriver): class TestDeleteSecurityGroups(TestOptimizedNVPDriver):
def test_delete_security_group(self): def test_delete_security_group(self):
mod_path = "quark.drivers.nvp_driver.NVPDriver" mod_path = "quark.drivers.nvp_driver.NVPDriver"
with contextlib.nested( with mock.patch("%s._connection" % self.d_pkg), \
mock.patch("%s._connection" % self.d_pkg), mock.patch("%s._query_security_group" % self.d_pkg), \
mock.patch("%s._query_security_group" % self.d_pkg), mock.patch("%s.delete_security_group" % mod_path):
mock.patch("%s.delete_security_group" % mod_path)):
session_delete = self.context.session.delete session_delete = self.context.session.delete
self.context.session.delete = mock.Mock(return_value=None) self.context.session.delete = mock.Mock(return_value=None)
@ -483,11 +485,11 @@ class TestSecurityGroupRules(TestOptimizedNVPDriver):
@contextlib.contextmanager @contextlib.contextmanager
def _stubs(self, rules=None): def _stubs(self, rules=None):
rules = rules or [] rules = rules or []
with contextlib.nested( with mock.patch("%s._connection" % self.d_pkg) as conn, \
mock.patch("%s._connection" % self.d_pkg), mock.patch("%s._query_security_group" % self.d_pkg) as \
mock.patch("%s._query_security_group" % self.d_pkg), query_sec_group, \
mock.patch("%s._check_rule_count_per_port" % self.d_pkg), mock.patch("%s._check_rule_count_per_port" % self.d_pkg) \
) as (conn, query_sec_group, rule_count): as rule_count:
query_sec_group.return_value = (quark.drivers.optimized_nvp_driver. query_sec_group.return_value = (quark.drivers.optimized_nvp_driver.
SecurityProfile()) SecurityProfile())
connection = self._create_connection() connection = self._create_connection()

View File

@ -152,11 +152,11 @@ class QuarkRedisSgToolPurgeOrphans(QuarkRedisSgToolBase):
def _stubs(self): def _stubs(self):
ports = [{"device_id": 1, "mac_address": 1}] ports = [{"device_id": 1, "mac_address": 1}]
vifs = ["1.1", "2.2", "3.3"] vifs = ["1.1", "2.2", "3.3"]
with contextlib.nested( with mock.patch("neutron.context.get_admin_context") as \
mock.patch("neutron.context.get_admin_context"), get_admin_ctxt, \
mock.patch("quark.db.api.ports_with_security_groups_find"), mock.patch("quark.db.api.ports_with_security_groups_find") \
mock.patch("%s._get_connection" % TOOL_MOD) as db_ports_groups, \
) as (get_admin_ctxt, db_ports_groups, get_conn): mock.patch("%s._get_connection" % TOOL_MOD) as get_conn:
connection_mock = mock.MagicMock() connection_mock = mock.MagicMock()
get_conn.return_value = connection_mock get_conn.return_value = connection_mock
ports_with_groups_mock = mock.MagicMock() ports_with_groups_mock = mock.MagicMock()
@ -216,12 +216,13 @@ class QuarkRedisSgToolWriteGroups(QuarkRedisSgToolBase):
vifs = ["1.1", "2.2", "3.3"] vifs = ["1.1", "2.2", "3.3"]
security_groups = [{"id": 1, "name": "test_group"}] security_groups = [{"id": 1, "name": "test_group"}]
with contextlib.nested( with mock.patch("neutron.context.get_admin_context") as \
mock.patch("neutron.context.get_admin_context"), get_admin_ctxt, \
mock.patch("quark.db.api.security_group_rule_find"), mock.patch("quark.db.api.security_group_rule_find") as \
mock.patch("quark.db.api.ports_with_security_groups_find"), rule_find, \
mock.patch("%s._get_connection" % TOOL_MOD) mock.patch("quark.db.api.ports_with_security_groups_find") \
) as (get_admin_ctxt, rule_find, db_ports_groups, get_conn): as db_ports_groups, \
mock.patch("%s._get_connection" % TOOL_MOD) as get_conn:
connection_mock = mock.MagicMock() connection_mock = mock.MagicMock()
get_conn.return_value = connection_mock get_conn.return_value = connection_mock
ports_with_groups_mock = mock.MagicMock() ports_with_groups_mock = mock.MagicMock()

View File

@ -12,7 +12,6 @@
# implied. # implied.
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import contextlib
import json import json
import mock import mock
@ -57,10 +56,8 @@ class TestRespAsyncIDMiddleware(test_base.TestBase):
def test_mw_none_context(self): def test_mw_none_context(self):
mw = jobmw.filter_factory(self.conf)(self.app) mw = jobmw.filter_factory(self.conf)(self.app)
with contextlib.nested( with mock.patch('%s._get_resp' % mw_mock_path) as get_resp, \
mock.patch('%s._get_resp' % mw_mock_path), mock.patch('%s._get_ctx' % mw_mock_path) as get_ctx:
mock.patch('%s._get_ctx' % mw_mock_path)) as \
(get_resp, get_ctx):
get_resp.return_value = self.resp_return get_resp.return_value = self.resp_return
get_ctx.return_value = self.none_ctx get_ctx.return_value = self.none_ctx
resp = mw.__call__.request('/', method='GET', body=self.body) resp = mw.__call__.request('/', method='GET', body=self.body)
@ -71,10 +68,8 @@ class TestRespAsyncIDMiddleware(test_base.TestBase):
def test_mw_empty_context(self): def test_mw_empty_context(self):
mw = jobmw.filter_factory(self.conf)(self.app) mw = jobmw.filter_factory(self.conf)(self.app)
with contextlib.nested( with mock.patch('%s._get_resp' % mw_mock_path) as get_resp, \
mock.patch('%s._get_resp' % mw_mock_path), mock.patch('%s._get_ctx' % mw_mock_path) as get_ctx:
mock.patch('%s._get_ctx' % mw_mock_path)) as \
(get_resp, get_ctx):
get_resp.return_value = self.resp_return get_resp.return_value = self.resp_return
get_ctx.return_value = self.no_ctx get_ctx.return_value = self.no_ctx
resp = mw.__call__.request('/', method='GET', body=self.body) resp = mw.__call__.request('/', method='GET', body=self.body)
@ -85,10 +80,8 @@ class TestRespAsyncIDMiddleware(test_base.TestBase):
def test_mw_missing_context(self): def test_mw_missing_context(self):
mw = jobmw.filter_factory(self.conf)(self.app) mw = jobmw.filter_factory(self.conf)(self.app)
with contextlib.nested( with mock.patch('%s._get_resp' % mw_mock_path) as get_resp, \
mock.patch('%s._get_resp' % mw_mock_path), mock.patch('%s._get_ctx' % mw_mock_path) as get_ctx:
mock.patch('%s._get_ctx' % mw_mock_path)) as \
(get_resp, get_ctx):
get_resp.return_value = self.resp_return get_resp.return_value = self.resp_return
get_ctx.return_value = self.random_ctx get_ctx.return_value = self.random_ctx
resp = mw.__call__.request('/', method='GET', body=self.body) resp = mw.__call__.request('/', method='GET', body=self.body)
@ -99,10 +92,8 @@ class TestRespAsyncIDMiddleware(test_base.TestBase):
def test_mw_modify_resp(self): def test_mw_modify_resp(self):
mw = jobmw.filter_factory(self.conf)(self.app) mw = jobmw.filter_factory(self.conf)(self.app)
with contextlib.nested( with mock.patch('%s._get_resp' % mw_mock_path) as get_resp, \
mock.patch('%s._get_resp' % mw_mock_path), mock.patch('%s._get_ctx' % mw_mock_path) as get_ctx:
mock.patch('%s._get_ctx' % mw_mock_path)) as \
(get_resp, get_ctx):
get_resp.return_value = self.resp_return get_resp.return_value = self.resp_return
get_ctx.return_value = self.job_ctx get_ctx.return_value = self.job_ctx
resp = mw.__call__.request('/', method='GET', body=self.body) resp = mw.__call__.request('/', method='GET', body=self.body)
@ -116,10 +107,8 @@ class TestRespAsyncIDMiddleware(test_base.TestBase):
def test_mw_error_resp(self): def test_mw_error_resp(self):
mw = jobmw.filter_factory(self.conf)(self.app) mw = jobmw.filter_factory(self.conf)(self.app)
with contextlib.nested( with mock.patch('%s._get_resp' % mw_mock_path) as get_resp, \
mock.patch('%s._get_resp' % mw_mock_path), mock.patch('%s._get_ctx' % mw_mock_path) as get_ctx:
mock.patch('%s._get_ctx' % mw_mock_path)) as \
(get_resp, get_ctx):
get_resp.return_value = self.err_resp get_resp.return_value = self.err_resp
get_ctx.return_value = self.job_ctx get_ctx.return_value = self.job_ctx
resp = mw.__call__.request('/', method='GET', body=self.body) resp = mw.__call__.request('/', method='GET', body=self.body)