diff --git a/quark/plugin.py b/quark/plugin.py index 17d46dc..d73c93b 100644 --- a/quark/plugin.py +++ b/quark/plugin.py @@ -16,7 +16,6 @@ """ v2 Neutron Plug-in API Quark Implementation """ -from neutron.db import api as neutron_db_api from neutron.extensions import securitygroup as sg_ext from neutron import neutron_plugin_base_v2 from neutron.openstack.common import log as logging @@ -24,7 +23,6 @@ from neutron import quota from oslo.config import cfg from quark.api import extensions -from quark.db import models from quark.plugin_modules import ip_addresses from quark.plugin_modules import ip_policies from quark.plugin_modules import mac_address_ranges @@ -92,8 +90,6 @@ class Plugin(neutron_plugin_base_v2.NeutronPluginBaseV2, def __init__(self): LOG.info("Starting quark plugin") - neutron_db_api.configure_db() - neutron_db_api.register_models(base=models.BASEV2) def _fix_missing_tenant_id(self, context, resource): """Will add the tenant_id to the context from body. diff --git a/quark/tests/plugin_modules/test_ports.py b/quark/tests/plugin_modules/test_ports.py index 7dca64d..1f27fb4 100644 --- a/quark/tests/plugin_modules/test_ports.py +++ b/quark/tests/plugin_modules/test_ports.py @@ -212,11 +212,13 @@ class TestQuarkCreatePort(test_quark_plugin.TestQuarkPlugin): mock.patch("%s.network_find" % db_mod), mock.patch("%s.allocate_ip_address" % ipam), mock.patch("%s.allocate_mac_address" % ipam), - ) as (port_create, net_find, alloc_ip, alloc_mac): + mock.patch("%s.port_count_all" % db_mod), + ) as (port_create, net_find, alloc_ip, alloc_mac, port_count): port_create.return_value = port_models net_find.return_value = network alloc_ip.return_value = addr alloc_mac.return_value = mac + port_count.return_value = 0 yield port_create def test_create_port(self): @@ -521,7 +523,7 @@ class TestQuarkUpdatePortSetsIps(test_quark_plugin.TestQuarkPlugin): net_model = models.Network() net_model["network_plugin"] = "BASE" port_model = models.Port() - port_model.network = net_model + port_model['network'] = net_model port_model.update(port) with contextlib.nested( mock.patch("quark.db.api.port_find"), @@ -579,7 +581,7 @@ class TestQuarkPostUpdatePort(test_quark_plugin.TestQuarkPlugin): with contextlib.nested( mock.patch("quark.db.api.port_find"), mock.patch("quark.ipam.QuarkIpam.allocate_ip_address"), - mock.patch("quark.db.api.ip_address_find") + mock.patch("quark.db.api.ip_address_find"), ) as (port_find, alloc_ip, ip_find): port_find.return_value = port_model alloc_ip.return_value = addr_model2 if addr_model2 else addr_model @@ -587,9 +589,10 @@ class TestQuarkPostUpdatePort(test_quark_plugin.TestQuarkPlugin): yield port_find, alloc_ip, ip_find def test_post_update_port_no_ports(self): - with self.assertRaises(exceptions.PortNotFound): - self.plugin.post_update_port(self.context, 1, - {"port": {"network_id": 1}}) + with self._stubs(port=None, addr=None): + with self.assertRaises(exceptions.PortNotFound): + self.plugin.post_update_port(self.context, 1, + {"port": {"network_id": 1}}) def test_post_update_port_fixed_ips_empty_body(self): port = dict(port=dict(network_id=1, tenant_id=self.context.tenant_id, @@ -940,11 +943,14 @@ class TestQuarkPortCreateFiltering(test_quark_plugin.TestQuarkPlugin): mock.patch("%s.allocate_mac_address" % ipam), mock.patch("neutron.openstack.common.uuidutils.generate_uuid"), mock.patch("quark.plugin_views._make_port_dict"), - ) as (port_create, net_find, alloc_ip, alloc_mac, gen_uuid, make_port): + mock.patch("%s.port_count_all" % db_mod), + ) as (port_create, net_find, alloc_ip, alloc_mac, gen_uuid, make_port, + port_count): net_find.return_value = network alloc_ip.return_value = addr alloc_mac.return_value = mac gen_uuid.return_value = 1 + port_count.return_value = 0 yield port_create, alloc_mac, net_find def test_create_port_attribute_filtering(self): diff --git a/quark/tests/plugin_modules/test_subnets.py b/quark/tests/plugin_modules/test_subnets.py index eb6412f..6cec72a 100644 --- a/quark/tests/plugin_modules/test_subnets.py +++ b/quark/tests/plugin_modules/test_subnets.py @@ -176,8 +176,9 @@ class TestQuarkCreateSubnetOverlapping(test_quark_plugin.TestQuarkPlugin): with contextlib.nested( mock.patch("quark.db.api.network_find"), mock.patch("quark.db.api.subnet_find"), - mock.patch("quark.db.api.subnet_create") - ) as (net_find, subnet_find, subnet_create): + mock.patch("quark.db.api.subnet_create"), + mock.patch("neutron.common.rpc.get_notifier") + ) as (net_find, subnet_find, subnet_create, get_notifier): net_find.return_value = network subnet_find.return_value = subnet_models subnet_create.return_value = models.Subnet( @@ -226,7 +227,8 @@ class TestQuarkCreateSubnetAllocationPools(test_quark_plugin.TestQuarkPlugin): mock.patch("quark.db.api.network_find"), mock.patch("quark.db.api.subnet_find"), mock.patch("quark.db.api.subnet_create"), - ) as (net_find, subnet_find, subnet_create): + mock.patch("neutron.common.rpc.get_notifier"), + ) as (net_find, subnet_find, subnet_create, get_notifier): net_find.return_value = s["network"] subnet_find.return_value = [] subnet_create.return_value = s @@ -338,7 +340,10 @@ class TestQuarkCreateSubnet(test_quark_plugin.TestQuarkPlugin): mock.patch("quark.db.api.network_find"), mock.patch("quark.db.api.dns_create"), mock.patch("quark.db.api.route_create"), - ) as (subnet_create, net_find, dns_create, route_create): + mock.patch("quark.db.api.subnet_find"), + mock.patch("neutron.common.rpc.get_notifier"), + ) as (subnet_create, net_find, dns_create, route_create, subnet_find, + get_notifier): subnet_create.return_value = subnet_mod net_find.return_value = network route_create.side_effect = route_models @@ -697,8 +702,9 @@ class TestQuarkUpdateSubnet(test_quark_plugin.TestQuarkPlugin): nexthop="172.16.0.1")] @contextlib.contextmanager - def _stubs(self, host_routes=None, new_routes=None, find_routes=True, - new_dns_servers=None, new_ip_policy=None, ip_version=4): + def _stubs(self, has_subnet=True, host_routes=None, new_routes=None, + find_routes=True, new_dns_servers=None, new_ip_policy=None, + ip_version=4): if host_routes is None: host_routes = [] if new_routes: @@ -719,28 +725,30 @@ class TestQuarkUpdateSubnet(test_quark_plugin.TestQuarkPlugin): cidr = "172.16.0.0/24" else: cidr = "2607:f0d0:1002:51::0/64" - subnet = dict( - id=1, - network_id=1, - tenant_id=self.context.tenant_id, - ip_version=ip_version, - cidr=cidr, - host_routes=host_routes, - dns_nameservers=["4.2.2.1", "4.2.2.2"], - enable_dhcp=None) - dns_ips = subnet.pop("dns_nameservers", []) - host_routes = subnet.pop("host_routes", []) - subnet_mod = models.Subnet() + subnet_mod = None + if has_subnet: + subnet = dict( + id=1, + network_id=1, + tenant_id=self.context.tenant_id, + ip_version=ip_version, + cidr=cidr, + host_routes=host_routes, + dns_nameservers=["4.2.2.1", "4.2.2.2"], + enable_dhcp=None) - subnet_mod.update(subnet) + dns_ips = subnet.pop("dns_nameservers", []) + host_routes = subnet.pop("host_routes", []) + subnet_mod = models.Subnet() + subnet_mod.update(subnet) - subnet_mod["dns_nameservers"] = [models.DNSNameserver(ip=ip) - for ip in dns_ips] - subnet_mod["routes"] = [models.Route(cidr=r["destination"], - gateway=r["nexthop"], - subnet_id=subnet_mod["id"]) - for r in host_routes] + subnet_mod["dns_nameservers"] = [models.DNSNameserver(ip=ip) + for ip in dns_ips] + subnet_mod["routes"] = [models.Route(cidr=r["destination"], + gateway=r["nexthop"], + subnet_id=subnet_mod["id"]) + for r in host_routes] with contextlib.nested( mock.patch("quark.db.api.subnet_find"), mock.patch("quark.db.api.subnet_update"), @@ -752,23 +760,25 @@ class TestQuarkUpdateSubnet(test_quark_plugin.TestQuarkPlugin): dns_create, route_find, route_update, route_create): subnet_find.return_value = subnet_mod - route_find.return_value = (subnet_mod["routes"][0] if - subnet_mod["routes"] and - find_routes else None) - new_subnet_mod = models.Subnet(network=models.Network()) - new_subnet_mod.update(subnet_mod) - if new_routes: - new_subnet_mod["routes"] = new_routes - if new_dns_servers: - new_subnet_mod["dns_nameservers"] = new_dns_servers - if new_ip_policy: - new_subnet_mod["ip_policy"] = new_ip_policy - subnet_update.return_value = new_subnet_mod + if has_subnet: + route_find.return_value = (subnet_mod["routes"][0] if + subnet_mod["routes"] and + find_routes else None) + new_subnet_mod = models.Subnet(network=models.Network()) + new_subnet_mod.update(subnet_mod) + if new_routes: + new_subnet_mod["routes"] = new_routes + if new_dns_servers: + new_subnet_mod["dns_nameservers"] = new_dns_servers + if new_ip_policy: + new_subnet_mod["ip_policy"] = new_ip_policy + subnet_update.return_value = new_subnet_mod yield dns_create, route_update, route_create def test_update_subnet_not_found(self): - with self.assertRaises(exceptions.SubnetNotFound): - self.plugin.update_subnet(self.context, 1, {}) + with self._stubs(has_subnet=False): + with self.assertRaises(exceptions.SubnetNotFound): + self.plugin.update_subnet(self.context, 1, {}) def test_update_subnet_dns_nameservers(self): new_dns_servers = ["1.1.1.2"] @@ -946,8 +956,9 @@ class TestQuarkDeleteSubnet(test_quark_plugin.TestQuarkPlugin): db_mod = "quark.db.api" with contextlib.nested( mock.patch("%s.subnet_find" % db_mod), - mock.patch("%s.subnet_delete" % db_mod) - ) as (sub_find, sub_delete): + mock.patch("%s.subnet_delete" % db_mod), + mock.patch("neutron.common.rpc.get_notifier") + ) as (sub_find, sub_delete, get_notifier): if subnet_mod: subnet_mod.allocated_ips = ip_mods sub_find.return_value = subnet_mod @@ -1110,8 +1121,11 @@ class TestQuarkCreateSubnetAttrFilters(test_quark_plugin.TestQuarkPlugin): mock.patch("quark.db.api.network_find"), mock.patch("quark.db.api.dns_create"), mock.patch("quark.db.api.route_create"), - mock.patch("quark.plugin_views._make_subnet_dict") - ) as (subnet_create, net_find, dns_create, route_create, sub_dict): + mock.patch("quark.plugin_views._make_subnet_dict"), + mock.patch("quark.db.api.subnet_find"), + mock.patch("neutron.common.rpc.get_notifier") + ) as (subnet_create, net_find, dns_create, route_create, sub_dict, + subnet_find, get_notifier): yield subnet_create, net_find def test_create_subnet(self):