def _stubs(self, subnets=None, routes=None): if routes is None: routes = [] route_models = [] for route in routes: r = models.Route() r.update(route) route_models.append(r) if isinstance(subnets, list): subnet_models = [] for subnet in subnets: s_dict = subnet.copy() s_dict["routes"] = route_models s = models.Subnet(network=models.Network()) s.update(s_dict) subnet_models.append(s) elif subnets: mod = models.Subnet(network=models.Network()) mod.update(subnets) mod["routes"] = route_models subnet_models = mod else: subnet_models = None with mock.patch("quark.db.api.subnet_find") as subnet_find: subnet_find.return_value = subnet_models yield
def test_validate_ports_on_network_raise_segment(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 mock_ports[i].ip_addresses.append(mock_address) with self.assertRaises(n_exc.BadRequest): ip_addresses.validate_and_fetch_segment( mock_ports, "2")
def _stubs(self, nets=None, subnets=None): net_mods = [] subnet_mods = [] if subnets: for subnet in subnets: subnet_mod = models.Subnet() subnet_mod.update(subnet) subnet_mods.append(subnet_mod) if nets: for net in nets: net_mod = models.Network() net_mod["network_plugin"] = "BASE" net_mod.update(net) net_mod["subnets"] = subnet_mods net_mods.append(net_mod) else: if nets: net_mods = nets.copy() net_mods["subnets"] = subnet_mods else: net_mods = nets db_mod = "quark.db.api" with mock.patch("%s.network_find" % db_mod) as net_find: net_find.return_value = net_mods yield
def _stubs(self, net=None, subnet=None, ports=None, find_net=False): net_mod = net subnet_mod = None if net: net_mod = models.Network() net_mod.update(net) if subnet: subnet_mod = models.Subnet() subnet_mod.update(subnet) found_net = None if find_net: found_net = models.Network() db_mod = "quark.db.api" with contextlib.nested( mock.patch("%s.network_create" % db_mod), mock.patch("%s.subnet_create" % db_mod), mock.patch("quark.drivers.base.BaseDriver.create_network"), mock.patch("%s.network_find" % db_mod)) as (net_create, sub_create, driver_net_create, net_find): net_create.return_value = net_mod sub_create.return_value = subnet_mod net_find.return_value = found_net yield net_create
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 _stubs(self, nets=None, subnets=None): net_mods = [] if isinstance(nets, list): for net in nets: subnet_mods = [] subnets = net.pop('subnets', []) for subnet in subnets: subnet_mod = models.Subnet() subnet_mod.update(subnet) subnet_mods.append(subnet_mod) net_mod = models.Network() net_mod.update(net) net_mod["subnets"] = subnet_mods net_mods.append(net_mod) else: if nets: net_mods = nets.copy() else: net_mods = nets db_mod = "quark.db.api" db_api.STRATEGY = network_strategy.JSONStrategy(self.strategy_json) network_strategy.STRATEGY = network_strategy.JSONStrategy( self.strategy_json) with mock.patch("%s._network_find" % db_mod) as net_find: net_find.return_value = net_mods yield net_find
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 _stubs(self, subnet=None, network=None, routes=None, dns=None): if network: net = models.Network() net.update(network) network = net subnet_mod = models.Subnet(network=models.Network()) dns_ips = subnet.pop("dns_nameservers", []) host_routes = subnet.pop("host_routes", []) subnet_mod.update(subnet) subnet["dns_nameservers"] = dns_ips subnet["host_routes"] = host_routes routes = routes or [] dns = dns or [] route_models = [models.Route(**r) for r in routes] dns_models = [models.DNSNameserver(**d) for d in dns] with contextlib.nested( mock.patch("quark.db.api.subnet_create"), 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): subnet_create.return_value = subnet_mod net_find.return_value = network route_create.side_effect = route_models dns_create.side_effect = dns_models yield subnet_create, dns_create, route_create
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 main(): config.init(sys.argv[1:]) if not cfg.CONF.config_file: sys.exit(_("ERROR: Unable to find configuration file via the default" " search paths (~/.neutron/, ~/, /etc/neutron/, /etc/) and" " the '--config-file' option!")) config.setup_logging() session = neutron_db_api.get_session() subnets = [ models.Subnet(name="public_v4", network_id="00000000-0000-0000-0000-000000000000", id="00000000-0000-0000-0000-000000000000", tenant_id="rackspace", segment_id="rackspace", do_not_use=True, _cidr="0.0.0.0/0"), models.Subnet(name="public_v6", network_id="00000000-0000-0000-0000-000000000000", id="11111111-1111-1111-1111-111111111111", tenant_id="rackspace", segment_id="rackspace", do_not_use=True, _cidr="::/0"), models.Subnet(name="private_v4", network_id="11111111-1111-1111-1111-111111111111", id="22222222-2222-2222-2222-222222222222", tenant_id="rackspace", segment_id="rackspace", do_not_use=True, _cidr="0.0.0.0/0"), models.Subnet(name="private_v6", network_id="11111111-1111-1111-1111-111111111111", id="33333333-3333-3333-3333-333333333333", tenant_id="rackspace", segment_id="rackspace", do_not_use=True, _cidr="::/0")] try: session.bulk_save_objects(subnets) except DBDuplicateEntry: LOG.warn("Provider subnets previously inserted into database")
def _stubs(self, subnets=None): if subnets is None: subnets = [] subnet_models = [] for subnet in subnets: s = models.Subnet() s.update(subnet) subnet_models.append(s) network = models.Network() network.update(dict(id=1, subnets=subnet_models)) 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): net_find.return_value = network subnet_find.return_value = subnet_models subnet_create.return_value = models.Subnet( network=models.Network(), cidr="192.168.1.1/24") yield subnet_create
def _stubs(self, subnet): s = models.Subnet(network=models.Network(id=1, subnets=[])) s.update(subnet) 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): net_find.return_value = s["network"] subnet_find.return_value = [] subnet_create.return_value = s yield subnet_create
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 subnet_create(context, **subnet_dict): subnet = models.Subnet() subnet.update(subnet_dict) subnet["tenant_id"] = context.tenant_id context.session.add(subnet) return subnet
def migrate_networks(self): """1. Migrate the m.ip_blocks -> q.quark_networks Migration of ip_blocks to networks requires one take into consideration that blocks can have 'children' blocks. A scan of the melange tables shows though that this feature hasn't been used. An ip_block has a cidr which maps to a corresponding subnet in quark. """ blocks = self.melange_session.query(melange.IpBlocks).all() networks = dict() """Create the networks using the network_id. It is assumed that a network can only belong to one tenant""" for block in blocks: init_id(self.json_data, 'networks', trim_br(block.network_id)) if trim_br(block.network_id) not in networks: networks[trim_br(block.network_id)] = { "tenant_id": block.tenant_id, "name": block.network_name, "max_allocation": block.max_allocation, "created_at": block.created_at } elif trim_br(block.network_id) in networks: if networks[trim_br( block.network_id )]["created_at"] > block.created_at: # noqa networks[trim_br( block.network_id )]["created_at"] = block.created_at # noqa elif networks[trim_br( block.network_id)]["tenant_id"] != block.tenant_id: r = "Found different tenant on network:{0} != {1}"\ .format(networks[trim_br( block.network_id)]["tenant_id"], block.tenant_id) self.log.critical(r) set_reason(self.json_data, 'networks', trim_br(block.network_id), r) raise Exception for net in networks: cache_net = networks[net] q_network = quarkmodels.Network( id=net, tenant_id=cache_net["tenant_id"], name=cache_net["name"], max_allocation=cache_net["max_allocation"]) self.add_to_session(q_network, 'networks', net) blocks_without_policy = 0 for block in blocks: init_id(self.json_data, 'subnets', block.id) q_subnet = quarkmodels.Subnet(id=block.id, network_id=trim_br(block.network_id), tenant_id=block.tenant_id, cidr=block.cidr, do_not_use=block.omg_do_not_use, created_at=block.created_at) self.add_to_session(q_subnet, 'subnets', q_subnet.id) q_dns1 = quarkmodels.DNSNameserver( tenant_id=block.tenant_id, created_at=block.created_at, ip=int(netaddr.IPAddress(block.dns1)), # noqa subnet_id=q_subnet.id) q_dns2 = quarkmodels.DNSNameserver( tenant_id=block.tenant_id, created_at=block.created_at, ip=int(netaddr.IPAddress(block.dns2)), # noqa subnet_id=q_subnet.id) self.new_to_session(q_dns1) self.new_to_session(q_dns2) self.migrate_ips(block=block) self.migrate_routes(block=block) # caching policy_ids for use in migrate_policies if block.policy_id: if block.policy_id not in self.policy_ids.keys(): self.policy_ids[block.policy_id] = {} self.policy_ids[block.policy_id][block.id] =\ trim_br(block.network_id) else: self.log.warning("Found block without a policy: {0}".format( block.id)) blocks_without_policy += 1 # have to add new routes as well: new_gates = 0 for block in blocks: if block.gateway: self.migrate_new_routes(block) new_gates += 1 self.log.info( "Cached {0} policy_ids. {1} blocks found without policy.".format( len(self.policy_ids), blocks_without_policy)) self.log.info("{0} brand new gateways created.".format(new_gates))
def _stubs(self, host_routes=None, new_routes=None, find_routes=True, new_dns_servers=None): if host_routes is None: host_routes = [] if new_routes: new_routes = [ models.Route(cidr=r["destination"], gateway=r["nexthop"], subnet_id=1) for r in new_routes ] if new_dns_servers: new_dns_servers = [ models.DNSNameserver(ip=ip, subnet_id=1) for ip in new_dns_servers ] subnet = dict(id=1, network_id=1, tenant_id=self.context.tenant_id, ip_version=4, cidr="172.16.0.0/24", 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.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 ] with contextlib.nested( mock.patch("quark.db.api.subnet_find"), mock.patch("quark.db.api.subnet_update"), mock.patch("quark.db.api.dns_create"), mock.patch("quark.db.api.route_find"), mock.patch("quark.db.api.route_update"), mock.patch("quark.db.api.route_create"), ) as (subnet_find, subnet_update, 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 subnet_update.return_value = new_subnet_mod yield dns_create, route_update, route_create