def _stubs(self, ports=None, addrs=None): port_models = [] addr_models = None if addrs: addr_models = [] for address in addrs: a = models.IPAddress() a.update(address) addr_models.append(a) if isinstance(ports, list): for port in ports: port_model = models.Port() port_model.update(port) if addr_models: port_model.ip_addresses = addr_models port_models.append(port_model) elif ports is None: port_models = None else: port_model = models.Port() port_model.update(ports) if addr_models: port_model.ip_addresses = addr_models port_models = port_model db_mod = "quark.db.api" with contextlib.nested(mock.patch("%s.port_find" % db_mod)) as (port_find, ): port_find.return_value = port_models yield
def test_get_ip_address_port(self, mock_dbapi, mock_ipam, *args): port = dict(mac_address="AA:BB:CC:DD:EE:FF", network_id=1, tenant_id=self.context.tenant_id, device_id=2, bridge="xenbr0", device_owner='network:dhcp', id=100) port2 = dict(mac_address="AA:BB:CC:DD:EE:FF", network_id=1, tenant_id=self.context.tenant_id, device_id=2, bridge="xenbr0", device_owner='network:dhcp', id=100) ip = dict(id=1, address=3232235876, address_readable="192.168.1.100", subnet_id=1, network_id=2, version=4) port_model = models.Port() port_model.update(port) port_model2 = models.Port() port_model2.update(port2) ip_model = models.IPAddress() ip_model.update(ip) mock_dbapi.port_find.return_value = port_model mock_ipam.allocate_ip_address.side_effect = ( self._alloc_stub(ip_model)) mock_dbapi.ip_address_find.return_value = ip_model res = self.plugin.get_port_for_ip_address(self.context, 1, 100) mock_dbapi.ip_address_find.assert_called_with(self.context, scope=mock_dbapi.ONE, id=1) mock_dbapi.port_find.assert_called_with(self.context, fields=None, id=100, ip_address_id=[1], scope=mock_dbapi.ONE) self.assertEqual(port["id"], res["id"]) self.assertEqual(port["device_id"], res["device_id"]) self.assertFalse('mac_address' in res) self.assertFalse('network_id' in res) self.assertFalse('bridge' in res) self.assertFalse('tenant_id' in res)
def test_update_port_associations_for_ip(self, associate_mock, disassociate_mock, get_associations_mock): self.context.session.add = mock.Mock() self.context.session.delete = mock.Mock() mock_ports = [models.Port(id=str(x), network_id="2", ip_addresses=[]) for x in xrange(4)] mock_address = models.IPAddress(id="1", address=3232235876, address_readable="192.168.1.100", subnet_id="1", network_id="2", version=4, used_by_tenant_id="1") mock_address.ports = mock_ports new_port_list = mock_ports[1:3] new_port_list.append(models.Port(id="4", network_id="2", ip_addresses=[])) get_associations_mock.return_value = mock_ports # NOTE(thomasem): Should be the new address after associating # any new ports in the list. mock_new_address = associate_mock.return_value db_api.update_port_associations_for_ip(self.context, new_port_list, mock_address) associate_mock.assert_called_once_with(self.context, set([new_port_list[2]]), mock_address) disassociate_mock.assert_called_once_with(self.context, set([mock_ports[0], mock_ports[3]]), mock_new_address)
def test_deleting_inactive_shared_ip(self, mock_dbapi, mock_ipam, *args): port = dict(id=100, network_id=2, backend_key="derp", device_id="y") port2 = dict(id=101, network_id=2, backend_key="derp", device_id="x") ip = dict(id=1, address=3232235876, address_readable="192.168.1.100", subnet_id=1, network_id=2, version=4, address_type="shared") port_model = models.Port() port_model2 = models.Port() port_model.update(port) port_model2.update(port2) ip_model = models.IPAddress() ip_model.update(ip) ip_model.ports = [port_model, port_model2] mock_dbapi.port_find.return_value = port_model mock_dbapi.ip_address_find.return_value = ip_model mock_ipam.allocate_ip_address.side_effect = ( self._alloc_stub(ip_model)) self.plugin.delete_ip_address(self.context, 1) self.assertFalse(mock_dbapi.ip_address_delete.called) self.assertTrue(mock_ipam.deallocate_ip_address.called)
def test_validate_ports_on_network_valid(self): mock_ports = [models.Port(id="1", network_id="2"), models.Port(id="2", network_id="2")] for p in mock_ports: p.ip_addresses.append(models.IPAddress(id="1", network_id="2")) p.ip_addresses[-1].subnet = models.Subnet(id="1", segment_id="1") r = ip_addresses.validate_and_fetch_segment( mock_ports, "2") self.assertEqual(r, "1")
def test_validate_ports_on_network_raise_network(self): mock_ports = [models.Port(id="1", network_id="2"), models.Port(id="2", network_id="3")] mock_addresses = [models.IPAddress(id="1", network_id="2"), models.IPAddress(id="2", network_id="3")] for i, ip_address in enumerate(mock_addresses): ip_address.subnet = models.Subnet(id="1", segment_id="2") mock_ports[i].ip_addresses.append(ip_address) with self.assertRaises(n_exc.BadRequest): ip_addresses.validate_and_fetch_segment( mock_ports, "2")
def test_validate_ports_on_network_raise_segment_multiple_ips(self): mock_ports = [models.Port(id="1", network_id="2"), models.Port(id="2", network_id="2")] mock_subnets = [models.Subnet(id="1", segment_id="2"), models.Subnet(id="2", segment_id="3")] for i, subnet in enumerate(mock_subnets): mock_address = models.IPAddress(id="2", network_id="2") mock_address.subnet = subnet for x in xrange(i + 1): mock_ports[x].ip_addresses.append(mock_address) with self.assertRaises(n_exc.BadRequest): ip_addresses.validate_and_fetch_segment( mock_ports, "2")
def _stubs(self, port, addr, addr2=None, port_ips=None): port_model = None addr_model = None addr_model2 = None if port: port_model = models.Port() port_model.update(port) if port_ips: port_model["ip_addresses"] = [] for ip in port_ips: ip_mod = models.IPAddress() ip_mod.update(ip) port_model["ip_addresses"].append(ip_mod) if addr: addr_model = models.IPAddress() addr_model.update(addr) if addr2: addr_model2 = models.IPAddress() addr_model2.update(addr2) 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")) 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 ip_find.return_value = addr_model yield port_find, alloc_ip, ip_find
def _stubs(self, ips, ports): with mock.patch("quark.db.api.ip_address_find") as ip_find: ip_models = [] port_models = [] for port in ports: p = models.Port() p.update(port) port_models.append(p) if isinstance(ips, list): for ip in ips: version = ip.pop("version") ip_mod = models.IPAddress() ip_mod.update(ip) ip_mod.version = version ip_mod.ports = port_models ip_models.append(ip_mod) ip_find.return_value = ip_models else: if ips: version = ips.pop("version") ip_mod = models.IPAddress() ip_mod.update(ips) ip_mod.version = version ip_mod.ports = port_models ip_find.return_value = ip_mod else: ip_find.return_value = ips yield
def test_delete_security_group_with_ports(self): port = models.Port() group = {'name': 'foo', 'description': 'bar', 'id': 1, 'tenant_id': self.context.tenant_id, 'ports': [port]} with self._stubs(group): with self.assertRaises(sg_ext.SecurityGroupInUse): self.plugin.delete_security_group(self.context, 1)
def _stubs(self, net=None, ports=None, subnets=None): subnets = subnets or [] net_mod = net port_mods = [] subnet_mods = [] for port in ports: port_model = models.Port() port_model.update(port) port_mods.append(port_model) for subnet in subnets: subnet_mod = models.Subnet() subnet_mod.update(subnet) subnet_mods.append(subnet_mod) if net: net_mod = models.Network() net_mod.update(net) net_mod.ports = port_mods net_mod["subnets"] = subnet_mods net_mod["network_plugin"] = "BASE" db_mod = "quark.db.api" with contextlib.nested( mock.patch("%s.network_find" % db_mod), mock.patch("%s.network_delete" % db_mod), mock.patch("quark.drivers.base.BaseDriver.delete_network"), mock.patch("%s.subnet_delete" % db_mod)) as (net_find, net_delete, driver_net_delete, subnet_del): net_find.return_value = net_mod yield net_delete
def test_create_ip_address_calls_port_associate_ip(self, mock_dbapi, mock_ipam, *args): old_cfg = cfg.CONF.QUARK.ipaddr_allow_fixed_ip cfg.CONF.set_override('ipaddr_allow_fixed_ip', True, "QUARK") port = dict(id=1, network_id=2, ip_addresses=[]) ip = dict(id=1, address=3232235876, address_readable="192.168.1.100", subnet_id=1, network_id=2, version=4, tenant_id=1) port_model = models.Port() port_model.update(port) ip_model = models.IPAddress() ip_model.update(ip) mock_dbapi.port_find.return_value = port_model mock_ipam.allocate_ip_address.side_effect = ( self._alloc_stub(ip_model)) ip_address = { "network_id": ip["network_id"], "version": 4, 'device_ids': [2], "port_ids": [1] } self.plugin.create_ip_address(self.context, dict(ip_address=ip_address)) mock_dbapi.port_associate_ip.assert_called_once_with( self.context, [port_model], ip_model) cfg.CONF.set_override('ipaddr_allow_fixed_ip', old_cfg, "QUARK")
def _stubs(self, ports, addr, addr_ports=False): port_models = [] addr_model = None for port in ports: port_model = models.Port() port_model.update(port) port_models.append(port_model) if addr: addr_model = models.IPAddress() addr_model.update(addr) if addr_ports: addr_model.ports = port_models db_mod = "quark.db.api" with mock.patch("%s.port_find" % db_mod) as port_find, \ mock.patch("%s.ip_address_find" % db_mod) as ip_find, \ mock.patch("%s.port_associate_ip" % db_mod) as \ port_associate_ip, \ mock.patch("%s.port_disassociate_ip" % db_mod) as \ port_disassociate_ip, \ mock.patch("quark.plugin_modules.ip_addresses" ".validate_and_fetch_segment"), \ mock.patch("quark.plugin_modules.ip_addresses.ipam_driver") \ as mock_ipam: port_find.return_value = port_models ip_find.return_value = addr_model port_associate_ip.side_effect = _port_associate_stub port_disassociate_ip.side_effect = _port_disassociate_stub mock_ipam.deallocate_ip_address.side_effect = ( _ip_deallocate_stub) yield
def _stubs(self, port, addr): port_model = None addr_model = None if port: port_model = models.Port() port_model.update(port) if addr: addr_model = models.IPAddress() addr_model.update(addr) def _alloc_ip(context, new_addr, net_id, port_m, *args, **kwargs): new_addr.extend([addr_model]) with mock.patch("quark.db.api.network_find"), \ mock.patch("quark.db.api.port_find") as port_find, \ mock.patch("quark.plugin_modules.ip_addresses.ipam_driver") \ as mock_ipam, \ mock.patch("quark.plugin_modules.ip_addresses.db_api" ".port_associate_ip") as mock_port_associate_ip, \ mock.patch("quark.plugin_modules.ip_addresses" ".validate_and_fetch_segment"): port_find.return_value = port_model mock_ipam.allocate_ip_address.side_effect = _alloc_ip mock_port_associate_ip.side_effect = _port_associate_stub yield
def _stubs(self, net=None, ports=None, subnets=None): subnets = subnets or [] net_mod = net port_mods = [] subnet_mods = [] for port in ports: port_model = models.Port() port_model.update(port) port_mods.append(port_model) for subnet in subnets: subnet_mod = models.Subnet() subnet_mod.update(subnet) subnet_mods.append(subnet_mod) if net: net_mod = models.Network() net_mod.update(net) net_mod.ports = port_mods net_mod["subnets"] = subnet_mods net_mod["network_plugin"] = "BASE" db_mod = "quark.db.api" strategy_prefix = "quark.network_strategy.JSONStrategy" with mock.patch("%s.network_find" % db_mod) as net_find, \ mock.patch("%s.network_delete" % db_mod) as net_delete, \ mock.patch("quark.drivers.base.BaseDriver.delete_network"), \ mock.patch("%s.subnet_delete" % db_mod), \ mock.patch("%s.is_provider_network" % strategy_prefix) as \ is_provider_network: net_find.return_value = net_mod is_provider_network.return_value = True yield net_delete
def test_create_ip_address_address_type_fixed(self, mock_dbapi, mock_ipam, *args): cfg.CONF.set_override('ipam_reuse_after', 100, "QUARK") old_cfg = cfg.CONF.QUARK.ipaddr_allow_fixed_ip cfg.CONF.set_override('ipaddr_allow_fixed_ip', True, "QUARK") ports = [dict(id=1, network_id=2, ip_addresses=[])] ip = dict(id=1, address=3232235876, address_readable="192.168.1.100", subnet_id=1, network_id=2, version=4, tenant_id=1) port_models = [models.Port(**p) for p in ports] ip_model = models.IPAddress() ip_model.update(ip) mock_dbapi.port_find.side_effect = port_models mock_ipam.allocate_ip_address.side_effect = ( self._alloc_stub(ip_model)) ip_address = {"network_id": ip["network_id"], "version": 4, 'device_ids': [2], "port_ids": [pm.id for pm in port_models]} self.plugin.create_ip_address(self.context, dict(ip_address=ip_address)) # NOTE(thomasem): Having to assert that [ip_model] was passed instead # of an empty list due to the expected behavior of this method being # that it mutates the passed in list. So, after it's run, the list # has already been mutated and it's a reference to that list that # we're checking. This method ought to be changed to return the new # IP and let the caller mutate the list, not the other way around. mock_ipam.allocate_ip_address.assert_called_once_with( self.context, [ip_model], ip['network_id'], None, 100, version=ip_address['version'], ip_addresses=[], segment_id=None, address_type="fixed") cfg.CONF.set_override('ipaddr_allow_fixed_ip', old_cfg, "QUARK")
def test_port_disassociate_ip(self): self.context.session.add = mock.Mock() self.context.session.delete = mock.Mock() mock_ports = [models.Port(id=str(x), network_id="2", ip_addresses=[]) for x in xrange(4)] mock_address = models.IPAddress(id="1", address=3232235876, address_readable="192.168.1.100", subnet_id="1", network_id="2", version=4, used_by_tenant_id="1") mock_assocs = [] for p in mock_ports: assoc = models.PortIpAssociation() assoc.port_id = p.id assoc.port = p assoc.ip_address_id = mock_address.id assoc.ip_address = mock_address mock_assocs.append(assoc) r = db_api.port_disassociate_ip(self.context, mock_ports[1:3], mock_address) self.assertEqual(len(r.associations), 2) self.assertEqual(r.associations[0], mock_assocs[0]) self.assertEqual(r.associations[1], mock_assocs[3]) self.context.session.add.assert_called_once_with(r) self.context.session.delete.assert_has_calls( [mock.call(mock_assocs[1]), mock.call(mock_assocs[2])])
def _stubs(self): ports = [{"device_id": 1, "mac_address": 1}] vifs = ["1.1", "2.2", "3.3"] security_groups = [{"id": 1, "name": "test_group"}] with contextlib.nested( mock.patch("neutron.context.get_admin_context"), mock.patch("quark.db.api.security_group_rule_find"), mock.patch("quark.db.api.ports_with_security_groups_find"), mock.patch("%s._get_connection" % TOOL_MOD)) as (get_admin_ctxt, rule_find, db_ports_groups, get_conn): connection_mock = mock.MagicMock() get_conn.return_value = connection_mock ports_with_groups_mock = mock.MagicMock() port_mods = [] sg_mods = [models.SecurityGroup(**sg) for sg in security_groups] for port in ports: port_mod = models.Port(**port) port_mod.security_groups = sg_mods port_mods.append(port_mod) sg_rule = models.SecurityGroupRule() rule_find.return_value = [sg_rule] db_ports_groups.return_value = ports_with_groups_mock ctxt_mock = mock.MagicMock() get_admin_ctxt.return_value = ctxt_mock ports_with_groups_mock.all.return_value = port_mods connection_mock.vif_keys.return_value = vifs connection_mock.serialize_rules.return_value = "rules" yield (get_conn, connection_mock, db_ports_groups, ctxt_mock, sg_rule)
def port_create(context, **port_dict): port = models.Port() port.update(port_dict) port["tenant_id"] = context.tenant_id if "addresses" in port_dict: port["ip_addresses"].extend(port_dict["addresses"]) context.session.add(port) return port
def _stubs(self, flip=None, port=None, ips=None, network=None): port_model = None if port: port_model = models.Port() port_model.update(dict(port=port)) if ips: for ip in ips: ip_model = models.IPAddress() ip_model.update(ip) addr_type = ip.get("address_type") if addr_type == "floating" and "fixed_ip_addr" in ip: fixed_ip = models.IPAddress() fixed_ip.update( next(ip_addr for ip_addr in ips if (ip_addr["address_readable"] == ip["fixed_ip_addr"]))) ip_model.fixed_ip = fixed_ip port_model.ip_addresses.append(ip_model) flip_model = None if flip: flip_model = models.IPAddress() flip_model.update(flip) net_model = None if network: net_model = models.Network() net_model.update(network) def _alloc_ip(context, new_addr, net_id, port_m, *args, **kwargs): new_addr.append(flip_model) def _port_assoc(context, ports, addr, enable_port=None): addr.ports = ports return addr def _flip_fixed_ip_assoc(context, addr, fixed_ip): addr.fixed_ip = fixed_ip return addr with contextlib.nested( mock.patch("quark.db.api.floating_ip_find"), mock.patch("quark.db.api.network_find"), mock.patch("quark.db.api.port_find"), mock.patch("quark.ipam.QuarkIpam.allocate_ip_address"), mock.patch("quark.drivers.unicorn_driver.UnicornDriver" ".register_floating_ip"), mock.patch("quark.db.api.port_associate_ip"), mock.patch("quark.db.api.floating_ip_associate_fixed_ip")) as ( flip_find, net_find, port_find, alloc_ip, mock_reg_flip, port_assoc, fixed_ip_assoc): flip_find.return_value = flip_model net_find.return_value = net_model port_find.return_value = port_model alloc_ip.side_effect = _alloc_ip port_assoc.side_effect = _port_assoc fixed_ip_assoc.side_effect = _flip_fixed_ip_assoc yield
def port_create(context, **port_dict): port = models.Port(tags=[]) port.update(port_dict) port["tenant_id"] = context.tenant_id if "addresses" in port_dict: port["ip_addresses"].extend(port_dict["addresses"]) PORT_TAG_REGISTRY.set_all(port, **port_dict) context.session.add(port) return port
def migrate_interfaces(self): env = 'ordpreprod' creds = get_connection_creds(env) nova = query.Nova(creds['nova_url'], creds['nova_username'], creds['nova_password']) melanged = query.Melange(creds['melange_url'], creds['melange_username'], creds['melange_password']) # grab all instances from nova instances = nova.get_instances_hashed_by_id() # grab all interfaces from melange interfaces_good = melanged.get_interfaces_hashed_by_device_id() interfaces_all = self.melange_session.query(melange.Interfaces).all() no_network_count = 0 good_device_ids = [] for k, v in interfaces_good.iteritems(): if k not in instances: # this is not a garbage interface # print 'interface device_id |%s| found in nova!!' % k # self.log.critical("NVP_TEMP_KEY needs to be updated.") good_device_ids.append(k) for interface in interfaces_all: if interface.device_id in good_device_ids: init_id(self.json_data, "interfaces", interface.id) if interface.id not in self.interface_network: set_reason(self.json_data, "interfaces", interface.id, "no network") no_network_count += 1 continue network_id = self.interface_network[interface.id] self.interface_tenant[interface.id] = interface.tenant_id q_port = quarkmodels.Port( id=interface.id, device_id=interface.device_id, tenant_id=interface.tenant_id, created_at=interface.created_at, backend_key=interface.vif_id_on_device, network_id=network_id) lswitch_id = str(uuid4()) q_nvp_switch = optdriver.LSwitch(id=lswitch_id, nvp_id=network_id, network_id=network_id, display_name=network_id) port_id = interface.vif_id_on_device if not port_id: port_id = "NVP_TEMP_KEY" q_nvp_port = optdriver.LSwitchPort(port_id=port_id, switch_id=lswitch_id) self.port_cache[interface.id] = q_port self.add_to_session(q_port, "interfaces", q_port.id) self.add_to_session(q_nvp_switch, "switch", q_nvp_switch.id) self.add_to_session(q_nvp_port, "nvp_port", q_nvp_port.id) self.log.info("Found {0} interfaces without a network.".format( str(no_network_count)))
def _stubs(self, port): port_model = None if port: port_model = models.Port() port_model.update(port) with contextlib.nested( mock.patch("quark.db.api.port_find"), mock.patch("quark.db.api.port_update"), mock.patch("quark.ipam.QuarkIpam.allocate_ip_address"), mock.patch("quark.ipam.QuarkIpam.deallocate_ip_address")) as ( port_find, port_update, alloc_ip, dealloc_ip): port_find.return_value = port_model yield port_find, port_update, alloc_ip, dealloc_ip
def test_create_port_net_at_max(self): network = dict(id=1, ports=[models.Port()]) mac = dict(address="aa:bb:cc:dd:ee:ff") port_name = "foobar" ip = dict() port = dict(port=dict(mac_address=mac["address"], network_id=1, tenant_id=self.context.tenant_id, device_id=2, name=port_name)) with self._stubs(port=port["port"], network=network, addr=ip, mac=mac): with self.assertRaises(exceptions.OverQuota): self.plugin.create_port(self.context, port)
def test_get_next_available_fixed_ip_with_no_avail_fixed_ips(self): port = models.Port() port.update(dict(id=1)) fixed_ip_addr = netaddr.IPAddress("192.168.0.1") fixed_ip = models.IPAddress() fixed_ip.update( dict(address_type="fixed", address=int(fixed_ip_addr), version=4, address_readable=str(fixed_ip_addr), allocated_at=datetime.datetime.now())) flip_addr = netaddr.IPAddress("10.0.0.1") flip = models.IPAddress() flip.update( dict(address_type="floating", address=int(flip_addr), version=4, address_readable=str(flip_addr), allocated_at=datetime.datetime.now())) flip.fixed_ip = fixed_ip port.ip_addresses.append(fixed_ip) port.ip_addresses.append(flip) fixed_ip_addr = netaddr.IPAddress("192.168.0.2") fixed_ip = models.IPAddress() fixed_ip.update( dict(address_type="fixed", address=int(fixed_ip_addr), version=4, address_readable=str(fixed_ip_addr), allocated_at=datetime.datetime.now())) flip_addr = netaddr.IPAddress("10.0.0.2") flip = models.IPAddress() flip.update( dict(address_type="floating", address=int(flip_addr), version=4, address_readable=str(flip_addr), allocated_at=datetime.datetime.now())) flip.fixed_ip = fixed_ip port.ip_addresses.append(fixed_ip) port.ip_addresses.append(flip) next_fixed_ip = floating_ips._get_next_available_fixed_ip(port) self.assertEqual(next_fixed_ip, None)
def test_port_associate_ip(self): self.context.session.add = mock.Mock() mock_ports = [models.Port(id=str(x), network_id="2", ip_addresses=[]) for x in xrange(4)] mock_address = models.IPAddress(id="1", address=3232235876, address_readable="192.168.1.100", subnet_id="1", network_id="2", version=4, used_by_tenant_id="1") r = db_api.port_associate_ip(self.context, mock_ports, mock_address) self.assertEqual(len(r.associations), len(mock_ports)) for assoc, port in zip(r.associations, mock_ports): self.assertEqual(assoc.port, port) self.assertEqual(assoc.ip_address, mock_address) self.assertEqual(assoc.enabled, False)
def test_get_next_available_fixed_ip_with_single_fixed_ip(self): port = models.Port() port.update(dict(id=1)) fixed_ip_addr = netaddr.IPAddress('192.168.0.1') fixed_ip = models.IPAddress() fixed_ip.update(dict(address_type="fixed", address=int(fixed_ip_addr), version=4, address_readable=str(fixed_ip_addr), allocated_at=datetime.datetime.now())) port.ip_addresses.append(fixed_ip) next_fixed_ip = floating_ips._get_next_available_fixed_ip(port) self.assertEqual(next_fixed_ip["address_readable"], '192.168.0.1')
def test_port_associate_ip_enable_port(self): self.context.session.add = mock.Mock() mock_port = models.Port(id="1", network_id="2", ip_addresses=[]) mock_address = models.IPAddress(id="1", address=3232235876, address_readable="192.168.1.100", subnet_id="1", network_id="2", version=4, used_by_tenant_id="1") r = db_api.port_associate_ip(self.context, [mock_port], mock_address, enable_port="1") self.assertEqual(len(r.associations), 1) assoc = r.associations[0] self.assertEqual(assoc.port, mock_port) self.assertEqual(assoc.ip_address, mock_address) self.assertEqual(assoc.enabled, True) self.context.session.add.assert_called_once_with(assoc)
def _stubs(self, port, addr): port_model = None addr_model = None if port: port_model = models.Port() port_model.update(port) if addr: addr_model = models.IPAddress() addr_model.update(addr) with contextlib.nested( mock.patch("quark.db.api.port_find"), mock.patch("quark.ipam.QuarkIpam.allocate_ip_address") ) as (port_find, alloc_ip): port_find.return_value = port_model alloc_ip.return_value = addr_model yield
def _stubs(self, port=None): port_models = None if port: port_model = models.Port() port_model.update(port) for ip in port["fixed_ips"]: port_model.ip_addresses.append( models.IPAddress(id=1, address=ip["ip_address"], subnet_id=ip["subnet_id"])) port_models = port_model db_mod = "quark.db.api" with mock.patch("%s.port_find" % db_mod) as port_find: port_find.return_value = port_models yield port_find