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_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=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 ip_address_create(context, **address_dict): ip_address = models.IPAddress() address = address_dict.pop("address") ip_address.update(address_dict) ip_address["address"] = int(address) ip_address["address_readable"] = str(address) ip_address["tenant_id"] = context.tenant_id ip_address["_deallocated"] = 0 context.session.add(ip_address) return ip_address
def migrate_ips(self, block=None): """3. Migrate m.ip_addresses -> q.quark_ip_addresses This migration is complicated. I believe q.subnets will need to be populated during this step as well. m.ip_addresses is scattered all over the place and it is not a 1:1 relationship between m -> q. Some more thought will be needed for this one. First we need to use m.ip_addresses to find the relationship between the ip_block and the m.interfaces. After figuring out that it will then be possible to create a q.subnet connected to the network. """ addresses = self.melange_session.query(melange.IpAddresses)\ .filter_by(ip_block_id=block.id).all() for address in addresses: init_id(self.json_data, 'ips', address.id) """Populate interface_network cache""" interface = address.interface_id if interface is not None and\ interface not in self.interface_network: self.interface_network[interface] = \ trim_br(block.network_id) if interface in self.interface_network and\ self.interface_network[interface] != \ trim_br(block.network_id): self.log.error("Found interface with different " "network id: {0} != {1}".format( self.interface_network[interface], trim_br(block.network_id))) deallocated = False deallocated_at = None # If marked for deallocation # put it into the quark ip table as deallocated if address.marked_for_deallocation == 1: deallocated = True deallocated_at = address.deallocated_at ip_address = netaddr.IPAddress(address.address) q_ip = quarkmodels.IPAddress( id=address.id, created_at=address.created_at, used_by_tenant_id=address.used_by_tenant_id, network_id=trim_br(block.network_id), subnet_id=block.id, version=ip_address.version, address_readable=address.address, deallocated_at=deallocated_at, _deallocated=deallocated, address=int(ip_address.ipv6())) # Populate interface_ip cache if interface not in self.interface_ip: self.interface_ip[interface] = set() self.interface_ip[interface].add(q_ip) self.add_to_session(q_ip, 'ips', q_ip.id)
def _stubs(self, flip=None): flip_model = None if flip: flip_model = models.IPAddress() flip_model.update(flip) with contextlib.nested( mock.patch("quark.db.api.floating_ip_find"), mock.patch("quark.ipam.QuarkIpam.deallocate_ip_address"), mock.patch("quark.drivers.unicorn_driver.UnicornDriver" ".remove_floating_ip") ) as (flip_find, mock_dealloc, mock_remove_flip): flip_find.return_value = flip_model yield
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 _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
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, subnet, ips): ip_mods = [] subnet_mod = None if subnet: subnet_mod = models.Subnet() subnet_mod.update(subnet) for ip in ips: ip_mod = models.IPAddress() ip_mod.update(ip) ip_mods.append(ip_mod) 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): if subnet_mod: subnet_mod.allocated_ips = ip_mods sub_find.return_value = subnet_mod yield sub_delete
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 contextlib.nested( mock.patch("%s.port_find" % db_mod), mock.patch("%s.ip_address_find" % db_mod), ) as (port_find, ip_find): port_find.return_value = port_models ip_find.return_value = addr_model yield
def _stubs(self, flip=None): flip_model = None if flip: flip_model = models.IPAddress() flip_model.update(flip) with contextlib.nested( mock.patch("quark.db.api.floating_ip_find"), mock.patch("quark.db.api.floating_ip_disassociate_fixed_ip"), mock.patch("quark.db.api.port_disassociate_ip"), mock.patch("quark.db.api.ip_address_deallocate"), mock.patch("quark.ipam.QuarkIpam.deallocate_ip_address"), mock.patch("quark.drivers.unicorn_driver.UnicornDriver" ".remove_floating_ip"), mock.patch("quark.billing.notify"), 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 build_payload.return_value = {'respek': '4reelz'} yield