def test_get_ports_filter_with_ip_and_device(self): with self._stubs(self.network, self.subnet, self.ports_info4) as ( net, sub, ports): network = dict(name="xx", tenant_id="fake", network_plugin="BASE") xx_network = {"network": network} xx_net = network_api.create_network(self.context, xx_network) subnet = dict(id=2, ip_version=4, next_auto_assign_ip=2, cidr=self.cidr, first_ip=self.ip_network.first, last_ip=self.ip_network.last, ip_policy=None, tenant_id="fake") xx_subnet = {"subnet": subnet} xx_subnet['subnet']['network_id'] = xx_net['id'] subnet_api.create_subnet(self.context, xx_subnet) port_info = {'port': dict(device_id='a')} port_info['port']['network_id'] = xx_net['id'] port_api.create_port(self.context, port_info) port_ids1 = [ports[0]['id'], ports[1]['id']] shared_ip1 = {'ip_address': dict(port_ids=port_ids1, network_id=net['id'], version=4)} ip1 = ip_api.create_ip_address(self.context, shared_ip1) filters = dict(device_id='a') ports = ip_api.get_ports_for_ip_address(self.context, ip1['id'], filters=filters) self.assertEqual(1, len(ports)) filters = dict(device_id='a') ports = port_api.get_ports(self.context, filters=filters) self.assertEqual(2, len(ports))
def _stubs(self): with mock.patch("neutron.common.rpc.get_notifier"), \ mock.patch("neutron.quota.QUOTAS.limit_check"): mac = {'mac_address_range': dict(cidr="AA:BB:CC")} self.context.is_admin = True macrng_api.create_mac_address_range(self.context, mac) self.context.is_admin = False network_info = dict(name="public", tenant_id="fake", network_plugin="BASE", ipam_strategy="ANY") network_info = {"network": network_info} network = network_api.create_network(self.context, network_info) subnet = db_api.subnet_create(self.context, tenant_id="fake", cidr="192.168.10.0/24", network_id=network['id']) fixed_ips = [ dict(subnet_id=subnet['id'], enabled=True, ip_address=self.addr) ] port = dict(port=dict(network_id=network['id'], tenant_id=self.context.tenant_id, device_id=2, fixed_ips=fixed_ips)) port_api.create_port(self.context, port) self.context.is_admin = True filters = {"deallocated": "both"} ip = ip_addr.get_ip_addresses(self.context, **filters) self.context.is_admin = False yield ip[0]
def _stubs(self): with mock.patch("neutron.common.rpc.get_notifier"), \ mock.patch("neutron.quota.QUOTAS.limit_check"): mac = {'mac_address_range': dict(cidr="AA:BB:CC")} self.context.is_admin = True macrng_api.create_mac_address_range(self.context, mac) self.context.is_admin = False network_info = dict(name="public", tenant_id="fake", network_plugin="BASE", ipam_strategy="ANY") network_info = {"network": network_info} network = network_api.create_network(self.context, network_info) subnet = db_api.subnet_create(self.context, tenant_id="fake", cidr="192.168.10.0/24", network_id=network['id']) fixed_ips = [dict(subnet_id=subnet['id'], enabled=True, ip_address=self.addr)] port = dict(port=dict(network_id=network['id'], tenant_id=self.context.tenant_id, device_id=2, fixed_ips=fixed_ips)) port_api.create_port(self.context, port) self.context.is_admin = True filters = {"deallocated": "both"} ip = ip_addr.get_ip_addresses(self.context, **filters) self.context.is_admin = False yield ip[0]
def test_port_created_with_forbidden_mac_range(self): cidr = "192.168.1.0/24" ip_network = netaddr.IPNetwork(cidr) network = dict(id='1', name="public", tenant_id="make", network_plugin="BASE", ipam_strategy="ANY") network = {"network": network} subnet_v4 = dict(id='1', ip_version=4, next_auto_assign_ip=2, cidr=cidr, first_ip=ip_network.first, last_ip=ip_network.last, ip_policy=None, tenant_id="fake") subnet_v4_info = {"subnet": subnet_v4} def _make_body(use_forbidden_mac_range=False): fix_ipv4 = dict(subnet_id=sub_v4s[0]['id']) port_info = \ {"port": dict( fixed_ips=[fix_ipv4], network_id=net['id'], use_forbidden_mac_range=use_forbidden_mac_range )} return port_info with self._stubs(network, [subnet_v4_info]) as ( net, sub_v4s, sub_v6): admin_ctxt = self.context.elevated() port = port_api.create_port( admin_ctxt, _make_body(use_forbidden_mac_range=True)) port_mac = port["mac_address"] self.assertTrue(port_mac.startswith("AA:BB:CC")) with self.assertRaises(q_exc.MacAddressGenerationFailure): port_api.create_port(admin_ctxt, _make_body())
def test_port_created_doesnt_satisfy_ipam_strategy_raises(self): cidr = "192.168.1.0/24" ip_network = netaddr.IPNetwork(cidr) network = dict(id=1, name="public", tenant_id="make", network_plugin="BASE", ipam_strategy="BOTH_REQUIRED") network = {"network": network} subnet_v4 = dict(id=1, ip_version=4, next_auto_assign_ip=2, cidr=cidr, first_ip=ip_network.first, last_ip=ip_network.last, ip_policy=None, tenant_id="fake") subnet_v4_info = {"subnet": subnet_v4} def _make_body_only_v4(ip): fix_ip = dict(ip_address=ip, subnet_id=sub_v4['id']) port_info = { "port": dict(fixed_ips=[fix_ip], network_id=net['id']) } return port_info with self._stubs(network, subnet_v4_info) as (net, sub_v4, sub_v6): ip = "192.168.1.50" with self.assertRaises(q_exc.IpAddressGenerationFailure): port_api.create_port(self.context, _make_body_only_v4(ip))
def test_gather_sg_ports(self): """Checking if gather ports works as designed. """ cidr = "192.168.1.0/24" network = dict(id='1', name="public", tenant_id="make", network_plugin="BASE", ipam_strategy="ANY") network = {"network": network} subnet_v4 = dict(id='1', ip_version=4, cidr=cidr, tenant_id="fake") subnet_v4_info = {"subnet": subnet_v4} with self._stubs(network, subnet_v4_info) as (net, sub_v4, update): port1 = port_api.create_port(self.context, self._make_body(net)) self.assertIsNotNone(port1) port2 = port_api.create_port(self.context, self._make_body(net)) self.assertIsNotNone(port2) sg_body = dict(tenant_id="derp", name="test sg", description="none") sg_body = dict(security_group=sg_body) sg = sg_api.create_security_group(self.context, sg_body) self.assertIsNotNone(sg) sgid = sg['id'] self.assertIsNotNone(sgid) assoc_ports = self._get_assoc_ports(sgid) self.assertEqual(0, len(assoc_ports)) port_body = {'security_groups': [sgid]} port_body = dict(port=port_body) port1 = port_api.update_port(self.context, port1['id'], port_body) self.assertIsNotNone(port1) assoc_ports = self._get_assoc_ports(sgid) self.assertEqual(1, len(assoc_ports)) # NOTE: this is duplicated because update_port modifies the params port_body = {'security_groups': [sgid]} port_body = dict(port=port_body) port2 = port_api.update_port(self.context, port2['id'], port_body) self.assertIsNotNone(port2) assoc_ports = self._get_assoc_ports(sgid) self.assertEqual(2, len(assoc_ports))
def test_port_show(self): with self._stubs(self.net_info, self.sub_info) as (network, subnet): fixed_ips = [ dict(subnet_id=subnet['id'], enabled=True, ip_address="192.168.10.45"), dict(subnet_id=subnet['id'], enabled=True, ip_address="192.168.10.199") ] port = dict(port=dict(network_id=network['id'], tenant_id=self.context.tenant_id, device_id='2', fixed_ips=fixed_ips)) expected = { 'status': "ACTIVE", 'device_owner': None, 'network_id': network["id"], 'tenant_id': self.context.tenant_id, 'admin_state_up': True, 'fixed_ips': fixed_ips, 'device_id': '2' } result = port_api.create_port(self.context, port) result = port_api.get_port(self.context, result['id']) for key in expected.keys(): if key != 'fixed_ips': self.assertEqual(result[key], expected[key], "Mismatch on %s" % key) for ip in result['fixed_ips']: self.assertTrue(ip in fixed_ips, '%s not in %s' % (ip, expected['fixed_ips']))
def test_create_port_multiple_fixed_ipv6(self): with self._stubs(self.net_info, self.sub_info_v6) as (network, subnet): ipv6a = "2001:db8::10" ipv6b = "2001:db8::15" fixed_ips = [ dict(subnet_id=subnet['id'], enabled=True, ip_address=ipv6a), dict(subnet_id=subnet['id'], enabled=True, ip_address=ipv6b) ] port = dict(port=dict(network_id=network['id'], tenant_id=self.context.tenant_id, device_id=2, fixed_ips=fixed_ips)) expected = { 'status': "ACTIVE", 'device_owner': None, 'network_id': network["id"], 'tenant_id': self.context.tenant_id, 'admin_state_up': True, 'fixed_ips': fixed_ips, 'device_id': 2 } result = port_api.create_port(self.context, port) for key in expected.keys(): if key != 'fixed_ips': self.assertEqual(result[key], expected[key], "Mismatch on %s" % key) for ip in result['fixed_ips']: self.assertTrue(ip in expected['fixed_ips'])
def test_port_show(self): with self._stubs(self.net_info, self.sub_info) as (network, subnet): fixed_ips = [dict(subnet_id=subnet['id'], enabled=True, ip_address="192.168.10.45"), dict(subnet_id=subnet['id'], enabled=True, ip_address="192.168.10.199")] port = dict(port=dict(network_id=network['id'], tenant_id=self.context.tenant_id, device_id='2', fixed_ips=fixed_ips)) expected = {'status': "ACTIVE", 'device_owner': None, 'network_id': network["id"], 'tenant_id': self.context.tenant_id, 'admin_state_up': True, 'fixed_ips': fixed_ips, 'device_id': '2'} result = port_api.create_port(self.context, port) result = port_api.get_port(self.context, result['id']) for key in expected.keys(): if key != 'fixed_ips': self.assertEqual(result[key], expected[key], "Mismatch on %s" % key) for ip in result['fixed_ips']: self.assertTrue(ip in fixed_ips, '%s not in %s' % (ip, expected['fixed_ips']))
def test_port_created_with_only_subnet(self): cidr = "192.168.1.0/24" ip_network = netaddr.IPNetwork(cidr) network = dict(id='1', name="public", tenant_id="make", network_plugin="BASE", ipam_strategy="ANY") network = {"network": network} subnet_v4 = dict(id='1', ip_version=4, next_auto_assign_ip=2, cidr=cidr, first_ip=ip_network.first, last_ip=ip_network.last, ip_policy=None, tenant_id="fake") subnet_v4_info = {"subnet": subnet_v4} def _make_body(): fix_ipv4 = dict(subnet_id=sub_v4s[0]['id']) port_info = { "port": dict(fixed_ips=[fix_ipv4], network_id=net['id']) } return port_info with self._stubs(network, [subnet_v4_info]) as (net, sub_v4s, sub_v6): port = port_api.create_port(self.context, _make_body()) port_ip = port["fixed_ips"][0]["ip_address"] self.assertTrue( netaddr.IPAddress(port_ip) in netaddr.IPNetwork(cidr))
def test_port_created_with_only_subnet(self): cidr = "192.168.1.0/24" ip_network = netaddr.IPNetwork(cidr) network = dict(id=1, name="public", tenant_id="make", network_plugin="BASE", ipam_strategy="ANY") network = {"network": network} subnet_v4 = dict(id=1, ip_version=4, next_auto_assign_ip=2, cidr=cidr, first_ip=ip_network.first, last_ip=ip_network.last, ip_policy=None, tenant_id="fake") subnet_v4_info = {"subnet": subnet_v4} def _make_body(): fix_ipv4 = dict(subnet_id=sub_v4s[0]['id']) port_info = {"port": dict(fixed_ips=[fix_ipv4], network_id=net['id'])} return port_info with self._stubs(network, [subnet_v4_info]) as ( net, sub_v4s, sub_v6): port = port_api.create_port(self.context, _make_body()) port_ip = port["fixed_ips"][0]["ip_address"] self.assertTrue(netaddr.IPAddress(port_ip) in netaddr.IPNetwork(cidr))
def test_create_port_multiple_fixed_ipv6(self): with self._stubs(self.net_info, self.sub_info_v6) as (network, subnet): ipv6a = "2001:db8::10" ipv6b = "2001:db8::15" fixed_ips = [dict(subnet_id=subnet['id'], enabled=True, ip_address=ipv6a), dict(subnet_id=subnet['id'], enabled=True, ip_address=ipv6b)] port = dict(port=dict(network_id=network['id'], tenant_id=self.context.tenant_id, device_id=2, fixed_ips=fixed_ips)) expected = {'status': "ACTIVE", 'device_owner': None, 'network_id': network["id"], 'tenant_id': self.context.tenant_id, 'admin_state_up': True, 'fixed_ips': fixed_ips, 'device_id': 2} result = port_api.create_port(self.context, port) for key in expected.keys(): if key != 'fixed_ips': self.assertEqual(result[key], expected[key], "Mismatch on %s" % key) for ip in result['fixed_ips']: self.assertTrue(ip in expected['fixed_ips'])
def test_ports_pagination(self): cidr = "192.168.1.0/24" ip_network = netaddr.IPNetwork(cidr) network = dict(id="1", name="public", tenant_id="make", network_plugin="BASE", ipam_strategy="ANY") network = {"network": network} subnet = dict(id="1", ip_version=4, next_auto_assign_ip=2, cidr=cidr, first_ip=ip_network.first, last_ip=ip_network.last, ip_policy=None, tenant_id="fake") subnet_info = {"subnet": subnet} def _make_body(): fix_ipv4 = dict(subnet_id=sub_ports[0]['id']) port_info = { "port": dict(fixed_ips=[fix_ipv4], network_id=net['id']) } return port_info ports_per_page = 1 with self._stubs(network, subnet_info) as (net, sub_ports): port1 = port_api.create_port(self.context, _make_body()) port2 = port_api.create_port(self.context, _make_body()) res_ports = port_api.get_ports(self.context, ports_per_page, ['id'], None) self.assertEqual(len(res_ports), ports_per_page) res_ports = port_api.get_ports(self.context) self.assertNotEqual(len(res_ports), ports_per_page) # Note (Perkins): Testing for a default sort on created_at, # but created_at is not available, so check that mac addresses, # which are created sequentially, are ordered correctly. res_ports = port_api.get_ports(self.context, 2, ['mac_address'], None) self.assertTrue( res_ports[0]['mac_address'] < res_ports[1]['mac_address']) self.assertTrue(port1['id'] == res_ports[0]['id']) self.assertTrue(port2['id'] == res_ports[1]['id'])
def test_can_create_port_with_adv_svc(self): with self._stubs(self.net_info, self.sub_info) as (network, subnet): port_info = {'port': {'network_id': network['id'], 'tenant_id': 'someoneelse'}} self.context.is_admin = True self.context.is_advsvc = True port_mod = port_api.create_port(self.context, port_info) self.assertIsNotNone(port_mod['id']) self.assertNotEqual(port_mod['tenant_id'], network['tenant_id'])
def test_env_caps_off_sg_async_update(self): """This test ensures that envcaps off works as designed.""" env_set = [ env.Capabilities.SECURITY_GROUPS, env.Capabilities.TENANT_NETWORK_SG, env.Capabilities.EGRESS, ] override = ','.join(env_set) old_override = cfg.CONF.QUARK.environment_capabilities cfg.CONF.set_override("environment_capabilities", override, "QUARK") cidr = "192.168.1.0/24" network = dict(id='1', name="public", tenant_id="make", network_plugin="BASE", ipam_strategy="ANY") network = {"network": network} subnet_v4 = dict(id='1', ip_version=4, cidr=cidr, tenant_id="fake") subnet_v4_info = {"subnet": subnet_v4} try: with self._stubs(network, subnet_v4_info) as (net, sub_v4, update): port1 = port_api.create_port(self.context, self._make_body(net)) self.assertIsNotNone(port1) sg_body = dict(tenant_id="derp", name="test sg", description="none") sg_body = dict(security_group=sg_body) sg = sg_api.create_security_group(self.context, sg_body) self.assertIsNotNone(sg) sgid = sg['id'] self.assertIsNotNone(sgid) port_body = {'security_groups': [sgid]} port_body = dict(port=port_body) port1 = port_api.update_port(self.context, port1['id'], port_body) self.assertIsNotNone(port1) sgr_body = { 'protocol': 'tcp', 'security_group_id': sgid, 'tenant_id': "derp", 'direction': 'ingress' } sgr_body = dict(security_group_rule=sgr_body) sgr = sg_api.create_security_group_rule(self.context, sgr_body) self.assertIsNotNone(sgr) self.assertFalse(update.called) finally: cfg.CONF.set_override("environment_capabilities", old_override, "QUARK")
def test_env_caps_on_sg_async_update(self): """This test ensures that envcaps on works as designed.""" env_set = [ env.Capabilities.SECURITY_GROUPS, env.Capabilities.TENANT_NETWORK_SG, env.Capabilities.EGRESS, env.Capabilities.SG_UPDATE_ASYNC ] override = ','.join(env_set) old_override = cfg.CONF.QUARK.environment_capabilities cfg.CONF.set_override("environment_capabilities", override, "QUARK") cidr = "192.168.1.0/24" network = dict(id='1', name="public", tenant_id="make", network_plugin="BASE", ipam_strategy="ANY") network = {"network": network} subnet_v4 = dict(id='1', ip_version=4, cidr=cidr, tenant_id="fake") subnet_v4_info = {"subnet": subnet_v4} try: with self._stubs(network, subnet_v4_info) as (net, sub_v4, update): port1 = port_api.create_port( self.context, self._make_body(net)) self.assertIsNotNone(port1) sg_body = dict(tenant_id="derp", name="test sg", description="none") sg_body = dict(security_group=sg_body) sg = sg_api.create_security_group(self.context, sg_body) self.assertIsNotNone(sg) sgid = sg['id'] self.assertIsNotNone(sgid) port_body = {'security_groups': [sgid]} port_body = dict(port=port_body) port1 = port_api.update_port(self.context, port1['id'], port_body) sgr_body = {'protocol': 'tcp', 'security_group_id': sgid, 'tenant_id': "derp", 'direction': 'ingress'} sgr_body = dict(security_group_rule=sgr_body) sgr = sg_api.create_security_group_rule(self.context, sgr_body) self.assertIsNotNone(sgr) self.assertTrue(update.called) finally: cfg.CONF.set_override("environment_capabilities", old_override, "QUARK")
def test_ports_pagination(self): cidr = "192.168.1.0/24" ip_network = netaddr.IPNetwork(cidr) network = dict(id="1", name="public", tenant_id="make", network_plugin="BASE", ipam_strategy="ANY") network = {"network": network} subnet = dict(id="1", ip_version=4, next_auto_assign_ip=2, cidr=cidr, first_ip=ip_network.first, last_ip=ip_network.last, ip_policy=None, tenant_id="fake") subnet_info = {"subnet": subnet} def _make_body(): fix_ipv4 = dict(subnet_id=sub_ports[0]['id']) port_info = {"port": dict(fixed_ips=[fix_ipv4], network_id=net['id'])} return port_info ports_per_page = 1 with self._stubs(network, subnet_info) as ( net, sub_ports): port1 = port_api.create_port(self.context, _make_body()) port2 = port_api.create_port(self.context, _make_body()) res_ports = port_api.get_ports(self.context, ports_per_page, ['id'], None) self.assertEqual(len(res_ports), ports_per_page) res_ports = port_api.get_ports(self.context) self.assertNotEqual(len(res_ports), ports_per_page) # Note (Perkins): Testing for a default sort on created_at, # but created_at is not available, so check that mac addresses, # which are created sequentially, are ordered correctly. res_ports = port_api.get_ports(self.context, 2, ['mac_address'], None) self.assertTrue(res_ports[0]['mac_address'] < res_ports[1]['mac_address']) self.assertTrue(port1['id'] == res_ports[0]['id']) self.assertTrue(port2['id'] == res_ports[1]['id'])
def test_port_created_with_forbidden_mac_range(self): cidr = "192.168.1.0/24" ip_network = netaddr.IPNetwork(cidr) network = dict(id='1', name="public", tenant_id="make", network_plugin="BASE", ipam_strategy="ANY") network = {"network": network} subnet_v4 = dict(id='1', ip_version=4, next_auto_assign_ip=2, cidr=cidr, first_ip=ip_network.first, last_ip=ip_network.last, ip_policy=None, tenant_id="fake") subnet_v4_info = {"subnet": subnet_v4} def _make_body(use_forbidden_mac_range=False): fix_ipv4 = dict(subnet_id=sub_v4s[0]['id']) port_info = \ {"port": dict( fixed_ips=[fix_ipv4], network_id=net['id'], use_forbidden_mac_range=use_forbidden_mac_range )} return port_info with self._stubs(network, [subnet_v4_info]) as (net, sub_v4s, sub_v6): admin_ctxt = self.context.elevated() port = port_api.create_port( admin_ctxt, _make_body(use_forbidden_mac_range=True)) port_mac = port["mac_address"] self.assertTrue(port_mac.startswith("AA:BB:CC")) with self.assertRaises(q_exc.MacAddressGenerationFailure): port_api.create_port(admin_ctxt, _make_body())
def test_can_create_port_with_adv_svc(self): with self._stubs(self.net_info, self.sub_info) as (network, subnet): port_info = { 'port': { 'network_id': network['id'], 'tenant_id': 'someoneelse' } } self.context.is_admin = True self.context.is_advsvc = True port_mod = port_api.create_port(self.context, port_info) self.assertIsNotNone(port_mod['id']) self.assertNotEqual(port_mod['tenant_id'], network['tenant_id'])
def _stubs(self, network_info, subnet_info, ports_info): self.ipam = quark.ipam.QuarkIpamANY() with mock.patch("neutron.common.rpc.get_notifier"), mock.patch("neutron.quota.QUOTAS.limit_check"): net = network_api.create_network(self.admin_context, network_info) mac = {"mac_address_range": dict(cidr="AA:BB:CC")} macrng_api.create_mac_address_range(self.admin_context, mac) subnet_info["subnet"]["network_id"] = net["id"] sub = subnet_api.create_subnet(self.context, subnet_info) ports = [] for port_info in ports_info: port_info["port"]["network_id"] = net["id"] ports.append(port_api.create_port(self.context, port_info)) yield net, sub, ports
def _stubs(self, network_info, subnet_info, ports_info): self.ipam = quark.ipam.QuarkIpamANY() with mock.patch("neutron.common.rpc.get_notifier"), \ mock.patch("neutron.quota.QUOTAS.limit_check"): net = network_api.create_network(self.admin_context, network_info) mac = {'mac_address_range': dict(cidr="AA:BB:CC")} macrng_api.create_mac_address_range(self.admin_context, mac) subnet_info['subnet']['network_id'] = net['id'] sub = subnet_api.create_subnet(self.context, subnet_info) ports = [] for port_info in ports_info: port_info['port']['network_id'] = net['id'] ports.append(port_api.create_port(self.context, port_info)) yield net, sub, ports
def _stubs(self, network_info, subnet_info, port_info): self.ipam = quark.ipam.QuarkIpamANY() with contextlib.nested(mock.patch("neutron.common.rpc.get_notifier"), mock.patch("neutron.quota.QUOTAS.limit_check")): net = network_api.create_network(self.context, network_info) mac = {'mac_address_range': dict(cidr="AA:BB:CC")} self.context.is_admin = True macrng_api.create_mac_address_range(self.context, mac) self.context.is_admin = False subnet_info['subnet']['network_id'] = net['id'] port_info['port']['network_id'] = net['id'] sub = subnet_api.create_subnet(self.context, subnet_info) port = port_api.create_port(self.context, port_info) yield net, sub, port
def test_port_created_should_satisfy_ipam_strategy(self): cidr = "192.168.1.0/24" cidr_v6 = "2001:db8::/32" ip_network = netaddr.IPNetwork(cidr) ipv6_network = netaddr.IPNetwork(cidr_v6) network = dict(id='1', name="public", tenant_id="make", network_plugin="BASE", ipam_strategy="BOTH_REQUIRED") network = {"network": network} subnet_v4 = dict(id='1', ip_version=4, next_auto_assign_ip=2, cidr=cidr, first_ip=ip_network.first, last_ip=ip_network.last, ip_policy=None, tenant_id="fake") subnet_v6 = dict(id='2', ip_version=6, next_auto_assign_ip=2, cidr=cidr_v6, first_ip=ipv6_network.first, last_ip=ipv6_network.last, ip_policy=None, tenant_id="fake") subnet_v4_info = {"subnet": subnet_v4} subnet_v6_info = {"subnet": subnet_v6} def _make_body(ipv4, ipv6): fix_ipv4 = dict(ip_address=ipv4, subnet_id=sub_v4['id']) fix_ipv6 = dict(ip_address=ipv6, subnet_id=sub_v6['id']) port_info = { "port": dict(fixed_ips=[fix_ipv4, fix_ipv6], network_id=net['id']) } return port_info with self._stubs(network, subnet_v4_info, subnet_v6_info) as (net, sub_v4, sub_v6): ipv4 = "192.168.1.50" ipv6 = "2001:db8::10" port = port_api.create_port(self.context, _make_body(ipv4, ipv6)) port_ips = [ip["ip_address"] for ip in port["fixed_ips"]] for ip in [ipv4, ipv6]: self.assertTrue(ip in port_ips)
def _stubs(self, network_info, subnet_info, ports_info): self.ipam = quark.ipam.QuarkIpamANY() with contextlib.nested(mock.patch("neutron.common.rpc.get_notifier")): self.context.is_admin = True net = network_api.create_network(self.context, network_info) mac = {'mac_address_range': dict(cidr="AA:BB:CC")} macrng_api.create_mac_address_range(self.context, mac) self.context.is_admin = False subnet_info['subnet']['network_id'] = net['id'] sub = subnet_api.create_subnet(self.context, subnet_info) ports = [] for port_info in ports_info: port_info['port']['network_id'] = net['id'] ports.append(port_api.create_port(self.context, port_info)) yield net, sub, ports
def _stubs(self, network_info, subnet_info, port_info): self.ipam = quark.ipam.QuarkIpamANY() with contextlib.nested( mock.patch("neutron.common.rpc.get_notifier"), mock.patch("neutron.quota.QUOTAS.limit_check")): net = network_api.create_network(self.context, network_info) mac = {'mac_address_range': dict(cidr="AA:BB:CC")} self.context.is_admin = True macrng_api.create_mac_address_range(self.context, mac) self.context.is_admin = False subnet_info['subnet']['network_id'] = net['id'] port_info['port']['network_id'] = net['id'] sub = subnet_api.create_subnet(self.context, subnet_info) port = port_api.create_port(self.context, port_info) yield net, sub, port
def test_port_created_doesnt_satisfy_ipam_strategy_raises(self): cidr = "192.168.1.0/24" ip_network = netaddr.IPNetwork(cidr) network = dict(id=1, name="public", tenant_id="make", network_plugin="BASE", ipam_strategy="BOTH_REQUIRED") network = {"network": network} subnet_v4 = dict(id=1, ip_version=4, next_auto_assign_ip=2, cidr=cidr, first_ip=ip_network.first, last_ip=ip_network.last, ip_policy=None, tenant_id="fake") subnet_v4_info = {"subnet": subnet_v4} def _make_body_only_v4(ip): fix_ip = dict(ip_address=ip, subnet_id=sub_v4['id']) port_info = {"port": dict(fixed_ips=[fix_ip], network_id=net['id'])} return port_info with self._stubs(network, subnet_v4_info) as ( net, sub_v4, sub_v6): ip = "192.168.1.50" with self.assertRaises(q_exc.IpAddressGenerationFailure): port_api.create_port(self.context, _make_body_only_v4(ip))
def test_port_created_with_multiple_fixed_ips_some_only_with_subnet(self): cidr = "192.168.1.0/24" another_cidr = "192.168.2.0/24" cidr_v6 = "2001:db8::/32" ip_network = netaddr.IPNetwork(cidr) another_ip_network = netaddr.IPNetwork(another_cidr) ipv6_network = netaddr.IPNetwork(cidr_v6) network = dict(id=1, name="public", tenant_id="make", network_plugin="BASE", ipam_strategy="BOTH_REQUIRED") network = {"network": network} subnet_v4 = dict(id=1, ip_version=4, next_auto_assign_ip=2, cidr=cidr, first_ip=ip_network.first, last_ip=ip_network.last, ip_policy=None, tenant_id="fake") another_subnet_v4 = dict(id=2, ip_version=4, next_auto_assign_ip=2, cidr=another_cidr, first_ip=another_ip_network.first, last_ip=another_ip_network.last, ip_policy=None, tenant_id="fake") subnet_v6 = dict(id=3, ip_version=6, next_auto_assign_ip=2, cidr=cidr_v6, first_ip=ipv6_network.first, last_ip=ipv6_network.last, ip_policy=None, tenant_id="fake") subnet_v4_info = {"subnet": subnet_v4} another_subnet_v4_info = {"subnet": another_subnet_v4} subnet_v6_info = {"subnet": subnet_v6} def _make_body(ipv4, ipv6): fix_ipv4 = dict(ip_address=ipv4, subnet_id=sub_v4s[0]['id']) another_fix_ipv4 = dict(subnet_id=sub_v4s[1]['id']) fix_ipv6 = dict(ip_address=ipv6, subnet_id=sub_v6['id']) port_info = {"port": dict(fixed_ips=[fix_ipv4, another_fix_ipv4, fix_ipv6], network_id=net['id'])} return port_info with self._stubs(network, [subnet_v4_info, another_subnet_v4_info], subnet_v6_info) as (net, sub_v4s, sub_v6): ipv4 = "192.168.1.50" another_ipv4 = "192.168.2.1" ipv6 = "2001:db8::10" port = port_api.create_port(self.context, _make_body(ipv4, ipv6)) port_ips = [ip["ip_address"] for ip in port["fixed_ips"]] for ip in [ipv4, another_ipv4, ipv6]: self.assertTrue(ip in port_ips)
def _stubs(self, network_info, subnet_info, ports_info): self.ipam = quark.ipam.QuarkIpamANY() with contextlib.nested( mock.patch("neutron.common.rpc.get_notifier")): self.context.is_admin = True net = network_api.create_network(self.context, network_info) mac = {'mac_address_range': dict(cidr="AA:BB:CC")} macrng_api.create_mac_address_range(self.context, mac) self.context.is_admin = False subnet_info['subnet']['network_id'] = net['id'] sub = subnet_api.create_subnet(self.context, subnet_info) ports = [] for port_info in ports_info: port_info['port']['network_id'] = net['id'] ports.append(port_api.create_port(self.context, port_info)) yield net, sub, ports
def test_create_port_single_fixed_ip(self): with self._stubs(self.net_info, self.sub_info) as (network, subnet): fixed_ips = [dict(subnet_id=subnet['id'], enabled=True, ip_address="192.168.10.45")] port = dict(port=dict(network_id=network['id'], tenant_id=self.context.tenant_id, device_id=2, fixed_ips=fixed_ips)) expected = {'status': "ACTIVE", 'device_owner': None, 'network_id': network["id"], 'tenant_id': self.context.tenant_id, 'admin_state_up': True, 'fixed_ips': fixed_ips, 'device_id': 2} result = port_api.create_port(self.context, port) for key in expected.keys(): self.assertEqual(result[key], expected[key], "Mismatch on %s" % key)
def test_port_created_should_satisfy_ipam_strategy(self): cidr = "192.168.1.0/24" cidr_v6 = "2001:db8::/32" ip_network = netaddr.IPNetwork(cidr) ipv6_network = netaddr.IPNetwork(cidr_v6) network = dict(id='1', name="public", tenant_id="make", network_plugin="BASE", ipam_strategy="BOTH_REQUIRED") network = {"network": network} subnet_v4 = dict(id='1', ip_version=4, next_auto_assign_ip=2, cidr=cidr, first_ip=ip_network.first, last_ip=ip_network.last, ip_policy=None, tenant_id="fake") subnet_v6 = dict(id='2', ip_version=6, next_auto_assign_ip=2, cidr=cidr_v6, first_ip=ipv6_network.first, last_ip=ipv6_network.last, ip_policy=None, tenant_id="fake") subnet_v4_info = {"subnet": subnet_v4} subnet_v6_info = {"subnet": subnet_v6} def _make_body(ipv4, ipv6): fix_ipv4 = dict(ip_address=ipv4, subnet_id=sub_v4['id']) fix_ipv6 = dict(ip_address=ipv6, subnet_id=sub_v6['id']) port_info = {"port": dict(fixed_ips=[fix_ipv4, fix_ipv6], network_id=net['id'])} return port_info with self._stubs(network, subnet_v4_info, subnet_v6_info) as ( net, sub_v4, sub_v6): ipv4 = "192.168.1.50" ipv6 = "2001:db8::10" port = port_api.create_port(self.context, _make_body(ipv4, ipv6)) port_ips = [ip["ip_address"] for ip in port["fixed_ips"]] for ip in [ipv4, ipv6]: self.assertTrue(ip in port_ips)
def test_create_port_single_fixed_ip(self): with self._stubs(self.net_info, self.sub_info) as (network, subnet): fixed_ips = [ dict(subnet_id=subnet['id'], enabled=True, ip_address="192.168.10.45") ] port = dict(port=dict(network_id=network['id'], tenant_id=self.context.tenant_id, device_id=2, fixed_ips=fixed_ips)) expected = { 'status': "ACTIVE", 'device_owner': None, 'network_id': network["id"], 'tenant_id': self.context.tenant_id, 'admin_state_up': True, 'fixed_ips': fixed_ips, 'device_id': 2 } result = port_api.create_port(self.context, port) for key in expected.keys(): self.assertEqual(result[key], expected[key], "Mismatch on %s" % key)
def test_create_lots_of_shared_ips_diff_tenant_raises(self): with self._stubs(self.network, self.subnet, self.ports_info4) as ( net, sub, ports): derp_ports = [] derp_context = context.Context("derp", "derp", is_admin=True) for port_info in self.ports_info2_other: port_info['port']['network_id'] = net['id'] port = port_api.create_port(derp_context, port_info) self.assertEqual("derp", port['tenant_id']) derp_ports.append(port) port_ids = [derp_ports[0]['id'], derp_ports[1]['id']] shared_ip = {'ip_address': dict(port_ids=port_ids, network_id=net['id'], version=4, tenant_id="derp")} ip = ip_api.create_ip_address(derp_context, shared_ip) self.assertEqual(ip_types.SHARED, ip['type']) self.assertEqual("derp", ip['tenant_id']) ips = db_api.ip_address_find(self.admin_context, network_id=net['id'], address_type=ip_types.SHARED, scope=db_api.ALL) self.assertEqual(1, len(ips)) port_ids = [ports[1]['id'], ports[2]['id']] shared_ip = {'ip_address': dict(port_ids=port_ids, network_id=net['id'], version=4)} ip = ip_api.create_ip_address(self.context, shared_ip) self.assertEqual(ip_types.SHARED, ip['type']) self.assertEqual("fake", ip['tenant_id']) ips = db_api.ip_address_find(self.admin_context, network_id=net['id'], address_type=ip_types.SHARED, scope=db_api.ALL) self.assertEqual(2, len(ips)) port_ids = [ports[2]['id'], ports[3]['id']] shared_ip = {'ip_address': dict(port_ids=port_ids, network_id=net['id'], version=4)} ip = ip_api.create_ip_address(self.context, shared_ip) self.assertEqual(ip_types.SHARED, ip['type']) self.assertEqual("fake", ip['tenant_id']) ips = db_api.ip_address_find(self.admin_context, network_id=net['id'], address_type=ip_types.SHARED, scope=db_api.ALL) self.assertEqual(3, len(ips)) port_ids = [ports[0]['id'], ports[3]['id']] shared_ip = {'ip_address': dict(port_ids=port_ids, network_id=net['id'], version=4)} ip = ip_api.create_ip_address(self.context, shared_ip) self.assertEqual(ip_types.SHARED, ip['type']) self.assertEqual("fake", ip['tenant_id']) ips = db_api.ip_address_find(self.admin_context, network_id=net['id'], address_type=ip_types.SHARED, scope=db_api.ALL) self.assertEqual(4, len(ips)) # total shared IPs on network ips = db_api.ip_address_find(self.admin_context, network_id=net['id'], tenant_id=self.context.tenant_id, address_type=ip_types.SHARED, scope=db_api.ALL) self.assertEqual(3, len(ips)) port_ids = [ports[0]['id'], ports[1]['id']] shared_ip = {'ip_address': dict(port_ids=port_ids, network_id=net['id'], version=4)} with self.assertRaises(q_exc.CannotCreateMoreSharedIPs): ip_api.create_ip_address(self.context, shared_ip) port_ids = [derp_ports[1]['id'], derp_ports[2]['id']] shared_ip = {'ip_address': dict(port_ids=port_ids, network_id=net['id'], version=4, tenant_id="derp")} ip = ip_api.create_ip_address(derp_context, shared_ip) self.assertEqual(ip_types.SHARED, ip['type']) self.assertEqual("derp", ip['tenant_id']) ips = db_api.ip_address_find(self.admin_context, network_id=net['id'], address_type=ip_types.SHARED, scope=db_api.ALL) self.assertEqual(5, len(ips)) ips = db_api.ip_address_find(self.admin_context, network_id=net['id'], tenant_id='derp', address_type=ip_types.SHARED, scope=db_api.ALL) self.assertEqual(2, len(ips)) port_ids = [derp_ports[0]['id'], derp_ports[2]['id']] shared_ip = {'ip_address': dict(port_ids=port_ids, network_id=net['id'], version=4, tenant_id="derp")} ip = ip_api.create_ip_address(derp_context, shared_ip) self.assertEqual(ip_types.SHARED, ip['type']) self.assertEqual("derp", ip['tenant_id']) ips = db_api.ip_address_find(self.admin_context, network_id=net['id'], address_type=ip_types.SHARED, scope=db_api.ALL) self.assertEqual(6, len(ips)) ips = db_api.ip_address_find(self.admin_context, network_id=net['id'], tenant_id='derp', address_type=ip_types.SHARED, scope=db_api.ALL) self.assertEqual(3, len(ips)) port_ids = [derp_ports[0]['id'], derp_ports[2]['id']] shared_ip = {'ip_address': dict(port_ids=port_ids, network_id=net['id'], version=4, tenant_id="derp")} with self.assertRaises(q_exc.CannotCreateMoreSharedIPs): ip_api.create_ip_address(self.admin_context, shared_ip)
def test_create_lots_of_shared_ips_diff_tenant_raises(self): with self._stubs(self.network, self.subnet, self.ports_info4) as (net, sub, ports): derp_ports = [] derp_context = context.Context("derp", "derp", is_admin=True) for port_info in self.ports_info2_other: port_info['port']['network_id'] = net['id'] port = port_api.create_port(derp_context, port_info) self.assertEqual("derp", port['tenant_id']) derp_ports.append(port) port_ids = [derp_ports[0]['id'], derp_ports[1]['id']] shared_ip = { 'ip_address': dict(port_ids=port_ids, network_id=net['id'], version=4, tenant_id="derp") } ip = ip_api.create_ip_address(derp_context, shared_ip) self.assertEqual(ip_types.SHARED, ip['type']) self.assertEqual("derp", ip['tenant_id']) ips = db_api.ip_address_find(self.admin_context, network_id=net['id'], address_type=ip_types.SHARED, scope=db_api.ALL) self.assertEqual(1, len(ips)) port_ids = [ports[1]['id'], ports[2]['id']] shared_ip = { 'ip_address': dict(port_ids=port_ids, network_id=net['id'], version=4) } ip = ip_api.create_ip_address(self.context, shared_ip) self.assertEqual(ip_types.SHARED, ip['type']) self.assertEqual("fake", ip['tenant_id']) ips = db_api.ip_address_find(self.admin_context, network_id=net['id'], address_type=ip_types.SHARED, scope=db_api.ALL) self.assertEqual(2, len(ips)) port_ids = [ports[2]['id'], ports[3]['id']] shared_ip = { 'ip_address': dict(port_ids=port_ids, network_id=net['id'], version=4) } ip = ip_api.create_ip_address(self.context, shared_ip) self.assertEqual(ip_types.SHARED, ip['type']) self.assertEqual("fake", ip['tenant_id']) ips = db_api.ip_address_find(self.admin_context, network_id=net['id'], address_type=ip_types.SHARED, scope=db_api.ALL) self.assertEqual(3, len(ips)) port_ids = [ports[0]['id'], ports[3]['id']] shared_ip = { 'ip_address': dict(port_ids=port_ids, network_id=net['id'], version=4) } ip = ip_api.create_ip_address(self.context, shared_ip) self.assertEqual(ip_types.SHARED, ip['type']) self.assertEqual("fake", ip['tenant_id']) ips = db_api.ip_address_find(self.admin_context, network_id=net['id'], address_type=ip_types.SHARED, scope=db_api.ALL) self.assertEqual(4, len(ips)) # total shared IPs on network ips = db_api.ip_address_find(self.admin_context, network_id=net['id'], tenant_id=self.context.tenant_id, address_type=ip_types.SHARED, scope=db_api.ALL) self.assertEqual(3, len(ips)) port_ids = [ports[0]['id'], ports[1]['id']] shared_ip = { 'ip_address': dict(port_ids=port_ids, network_id=net['id'], version=4) } with self.assertRaises(q_exc.CannotCreateMoreSharedIPs): ip_api.create_ip_address(self.context, shared_ip) port_ids = [derp_ports[1]['id'], derp_ports[2]['id']] shared_ip = { 'ip_address': dict(port_ids=port_ids, network_id=net['id'], version=4, tenant_id="derp") } ip = ip_api.create_ip_address(derp_context, shared_ip) self.assertEqual(ip_types.SHARED, ip['type']) self.assertEqual("derp", ip['tenant_id']) ips = db_api.ip_address_find(self.admin_context, network_id=net['id'], address_type=ip_types.SHARED, scope=db_api.ALL) self.assertEqual(5, len(ips)) ips = db_api.ip_address_find(self.admin_context, network_id=net['id'], tenant_id='derp', address_type=ip_types.SHARED, scope=db_api.ALL) self.assertEqual(2, len(ips)) port_ids = [derp_ports[0]['id'], derp_ports[2]['id']] shared_ip = { 'ip_address': dict(port_ids=port_ids, network_id=net['id'], version=4, tenant_id="derp") } ip = ip_api.create_ip_address(derp_context, shared_ip) self.assertEqual(ip_types.SHARED, ip['type']) self.assertEqual("derp", ip['tenant_id']) ips = db_api.ip_address_find(self.admin_context, network_id=net['id'], address_type=ip_types.SHARED, scope=db_api.ALL) self.assertEqual(6, len(ips)) ips = db_api.ip_address_find(self.admin_context, network_id=net['id'], tenant_id='derp', address_type=ip_types.SHARED, scope=db_api.ALL) self.assertEqual(3, len(ips)) port_ids = [derp_ports[0]['id'], derp_ports[2]['id']] shared_ip = { 'ip_address': dict(port_ids=port_ids, network_id=net['id'], version=4, tenant_id="derp") } with self.assertRaises(q_exc.CannotCreateMoreSharedIPs): ip_api.create_ip_address(self.admin_context, shared_ip)
def create_port(self, context, port): return ports.create_port(context, port)
def create_port(self, context, port): self._fix_missing_tenant_id(context, port["port"]) return ports.create_port(context, port)