def setUp(self): super(L3AgentNDPProxyTestFramework, self).setUp() self.conf.set_override('extensions', ['ndp_proxy'], 'agent') self.agent = neutron_l3_agent.L3NATAgentWithStateReport(HOSTNAME, self.conf) self.np_ext = np.NDPProxyAgentExtension() port_id1 = uuidutils.generate_uuid() port1_binding = ports_obj.PortBinding(port_id=port_id1, host=self.agent.host) port1_obj = ports_obj.Port(id=port_id1, bindings=[port1_binding]) port_id2 = uuidutils.generate_uuid() port2_binding = ports_obj.PortBinding(port_id=port_id1, host='fake_host') port2_obj = ports_obj.Port(id=port_id2, bindings=[port2_binding]) self.ports = [port1_obj, port2_obj] self.port_binding_map = {port_id1: port1_binding, port_id2: port2_binding} self.ndpproxy1 = np_obj.NDPProxy( context=None, id=uuidutils.generate_uuid(), router_id=uuidutils.generate_uuid(), port_id=port_id1, ip_address='2002::1:3') self.ndpproxy2 = np_obj.NDPProxy( context=None, id=uuidutils.generate_uuid(), router_id=uuidutils.generate_uuid(), port_id=port_id2, ip_address='2002::1:4') self.ndp_proxies = [self.ndpproxy1, self.ndpproxy2] agent_configurations = { 'agent_mode': constants.L3_AGENT_MODE_DVR_NO_EXTERNAL} self.agent_obj = agent_obj.Agent( id=uuidutils.generate_uuid(), host=self.agent.host, agent_type=constants.AGENT_TYPE_L3, configurations=agent_configurations) self._set_pull_mock()
def test_distributed_port_binding_deleted_by_port_deletion(self): network_id = uuidutils.generate_uuid() network_obj.Network(self.ctx, id=network_id).create() device_owner = constants.DEVICE_OWNER_DVR_INTERFACE port = port_obj.Port(self.ctx, id=uuidutils.generate_uuid(), network_id=network_id, mac_address=netaddr.EUI('00-11-22-33-44-55'), admin_state_up=True, status=constants.PORT_STATUS_ACTIVE, device_id='device_id', device_owner=device_owner) port.create() port_obj.DistributedPortBinding( self.ctx, port_id=port.id, host='host', vif_type=portbindings.VIF_TYPE_UNBOUND, vnic_type=portbindings.VNIC_NORMAL, router_id='router_id', status=constants.PORT_STATUS_DOWN).create() port_obj.DistributedPortBinding( self.ctx, port_id=port.id, host='another-host', vif_type=portbindings.VIF_TYPE_UNBOUND, vnic_type=portbindings.VNIC_NORMAL, router_id='router_id', status=constants.PORT_STATUS_DOWN).create() with warnings.catch_warnings(record=True) as warning_list: port.delete() self.assertEqual([], warning_list, 'Warnings: %s' % ';'.join([str(w) for w in warning_list])) ports = ml2_db.get_distributed_port_bindings(self.ctx, port.id) self.assertEqual(0, len(ports))
def test_port_without_device_id_no_notify(self): port = port_obj.Port(self.ctx, id=self.port_uuid, device_id="", device_owner=n_const.DEVICE_OWNER_DHCP, status=n_const.PORT_STATUS_ACTIVE) self._record_port_status_changed_helper(n_const.PORT_STATUS_ACTIVE, sql_attr.NO_VALUE, port)
def setUp(self): super(TestCacheBackedPluginApi, self).setUp() self._api = rpc.CacheBackedPluginApi(lib_topics.PLUGIN) self._api._legacy_interface = mock.Mock() self._api.remote_resource_cache = mock.Mock() self._network_id = uuidutils.generate_uuid() self._segment_id = uuidutils.generate_uuid() self._segment = network.NetworkSegment( id=self._segment_id, network_id=self._network_id, network_type=constants.TYPE_FLAT) self._port_id = uuidutils.generate_uuid() self._network = network.Network(id=self._network_id, segments=[self._segment]) self._port = ports.Port( id=self._port_id, network_id=self._network_id, mac_address=netaddr.EUI('fa:16:3e:ec:c7:d9'), admin_state_up=True, security_group_ids=set([uuidutils.generate_uuid()]), fixed_ips=[], allowed_address_pairs=[], device_owner=constants.DEVICE_OWNER_COMPUTE_PREFIX, bindings=[ ports.PortBinding(port_id=self._port_id, host='host1', status=constants.ACTIVE, profile={}) ], binding_levels=[ ports.PortBindingLevel(port_id=self._port_id, host='host1', level=0, segment=self._segment) ])
def test_non_compute_instances_no_notify(self): device_id = '32102d7b-1cf4-404d-b50a-97aae1f55f87' port = port_obj.Port(self.ctx, id=self.port_uuid, device_id=device_id, device_owner=n_const.DEVICE_OWNER_DHCP, status=n_const.PORT_STATUS_ACTIVE) self._record_port_status_changed_helper(n_const.PORT_STATUS_ACTIVE, sql_attr.NO_VALUE, port)
def _make_port(self): net = self._make_net() mac_address = netaddr.EUI('1') port = port_obj.Port(self.ctx, network_id=net.id, device_owner='3', project_id='1', admin_state_up=True, status='DOWN', device_id='2', mac_address=mac_address) port.create() return port
def test_port_without_device_owner_no_notify(self): device_id = '32102d7b-1cf4-404d-b50a-97aae1f55f87' port = port_obj.Port(self.ctx, id=self.port_uuid, device_id=device_id, device_owner="", status=n_const.PORT_STATUS_ACTIVE) self._record_port_status_changed_helper(n_const.PORT_STATUS_ACTIVE, sql_attr.NO_VALUE, port)
def _get_port(self, vif_type, vnic_type): port_id = uuidutils.generate_uuid() port_binding = ports.PortBinding(self.ctxt, port_id=port_id, vif_type=vif_type, vnic_type=vnic_type) return ports.Port(self.ctxt, id=uuidutils.generate_uuid(), binding=port_binding)
def _create_one_port(self, mac_address_int, network_id): mac_address = netaddr.EUI(mac_address_int) port = port_obj.Port( self.ctx, project_id=self.project_id, network_id=network_id, device_owner='', admin_state_up=True, status='DOWN', device_id='2', mac_address=mac_address) port.create() return port
def test__legacy_notifier_resource_update(self): updated_port = ports.Port(id=self._port_id, name='updated_port') self._api._legacy_notifier(resources.PORT, events.AFTER_UPDATE, self, mock.ANY, changed_fields=set(['name']), resource_id=self._port_id, existing=self._port, updated=updated_port) self._api._legacy_interface.port_delete.assert_not_called() self._api._legacy_interface.port_update.assert_called_once_with( mock.ANY, port={'id': self._port_id}, port_id=self._port_id) self._api._legacy_interface.binding_deactivate.assert_not_called()
def _create_port(self, **port_attrs): attrs = {'project_id': uuidutils.generate_uuid(), 'admin_state_up': True, 'status': 'ACTIVE', 'device_id': 'fake_device', 'device_owner': 'fake_owner', 'mac_address': tools.get_random_EUI()} attrs.update(port_attrs) port = ports.Port(self.context, **attrs) port.create() return port
def _setup_neutron_port(self, network_id, port_id): mac_address = db_base_plugin_v2.NeutronDbPluginV2._generate_mac() port = port_obj.Port(self.ctx, id=port_id, network_id=network_id, mac_address=netaddr.EUI(mac_address), admin_state_up=True, status='DOWN', device_id='', device_owner='') port.create() return port
def _make_port_ovo(self, ip, **kwargs): attrs = {'id': uuidutils.generate_uuid(), 'network_id': uuidutils.generate_uuid(), 'security_group_ids': set(), 'device_owner': 'compute:None', 'allowed_address_pairs': []} attrs['fixed_ips'] = [ports.IPAllocation( port_id=attrs['id'], subnet_id=uuidutils.generate_uuid(), network_id=attrs['network_id'], ip_address=ip)] attrs.update(**kwargs) p = ports.Port(self.ctx, **attrs) self.rcache.record_resource_update(self.ctx, 'Port', p) return p
def _create_port_object(self, name=None, device_owner=None, status=nl_const.PORT_STATUS_ACTIVE): port_data = { 'id': uuidutils.generate_uuid(), 'project_id': 'fake_tenant_id', 'status': status } if name: port_data['name'] = name if device_owner: port_data['device_owner'] = device_owner return port_objects.Port(**port_data)
def test__legacy_notifier_no_new_binding_activated(self): updated_port = ports.Port( id=self._port_id, name='updated_port', bindings=[ports.PortBinding(port_id=self._port_id, host='host2', status=constants.ACTIVE)]) self._api._legacy_notifier(resources.PORT, events.AFTER_UPDATE, self, mock.ANY, changed_fields=set(['name', 'bindings']), resource_id=self._port_id, existing=self._port, updated=updated_port) self._api._legacy_interface.port_update.assert_called_once_with( mock.ANY, port={'id': self._port_id}, port_id=self._port_id) self._api._legacy_interface.port_delete.assert_not_called() self._api._legacy_interface.binding_deactivate.assert_not_called()
def test__legacy_notifier_resource_update(self): updated_port = ports.Port(id=self._port_id, name='updated_port') self._api._legacy_notifier(resources.PORT, events.AFTER_UPDATE, self, payload=events.DBEventPayload( mock.ANY, metadata={ 'changed_fields': set(['name']) }, resource_id=self._port_id, states=(self._port, updated_port))) self._api._legacy_interface.port_delete.assert_not_called() self._api._legacy_interface.port_update.assert_called_once_with( mock.ANY, port={'id': self._port_id}, port_id=self._port_id) self._api._legacy_interface.binding_deactivate.assert_not_called()
def test_notify_port_active_direct(self): device_id = '32102d7b-1cf4-404d-b50a-97aae1f55f87' port_id = 'bee50827-bcee-4cc8-91c1-a27b0ce54222' port = port_obj.Port(self.ctx, id=port_id, device_id=device_id, device_owner=DEVICE_OWNER_COMPUTE) expected_event = {'server_uuid': device_id, 'name': nova.VIF_PLUGGED, 'status': 'completed', 'tag': port_id} self.nova_notifier.notify_port_active_direct(port) self.assertEqual( 1, len(self.nova_notifier.batch_notifier.pending_events)) self.assertEqual(expected_event, self.nova_notifier.batch_notifier.pending_events[0])
def test_update_db_subnet_new_pools_exception(self, pool_mock): context = mock.Mock() mocks = self._prepare_mocks_with_pool_mock(pool_mock) mocks['ipam'] = ipam_pluggable_backend.IpamPluggableBackend() new_port = {'fixed_ips': [{'ip_address': '192.168.1.20', 'subnet_id': uuidutils.generate_uuid()}, {'ip_address': '192.168.1.50', 'subnet_id': uuidutils.generate_uuid()}]} db_port = port_obj.Port(context, id=uuidutils.generate_uuid(), network_id=uuidutils.generate_uuid()) old_port = {'fixed_ips': [{'ip_address': '192.168.1.10', 'subnet_id': uuidutils.generate_uuid()}, {'ip_address': '192.168.1.50', 'subnet_id': uuidutils.generate_uuid()}]} changes = mocks['ipam'].Changes( add=[{'ip_address': '192.168.1.20', 'subnet_id': uuidutils.generate_uuid()}], original=[{'ip_address': '192.168.1.50', 'subnet_id': uuidutils.generate_uuid()}], remove=[{'ip_address': '192.168.1.10', 'subnet_id': uuidutils.generate_uuid()}]) mocks['ipam']._delete_ip_allocation = mock.Mock() mocks['ipam']._make_port_dict = mock.Mock(return_value=old_port) mocks['ipam']._update_ips_for_port = mock.Mock(return_value=changes) mocks['ipam']._update_db_port = mock.Mock( side_effect=db_exc.DBDeadlock) # emulate raising exception on rollback actions mocks['ipam']._ipam_deallocate_ips = mock.Mock(side_effect=ValueError) mocks['ipam']._ipam_allocate_ips = mock.Mock(side_effect=ValueError) # Validate original exception (DBDeadlock) is not overridden by # exception raised on rollback (ValueError) with mock.patch.object(port_obj.IPAllocation, 'create'): self.assertRaises(db_exc.DBDeadlock, mocks['ipam'].update_port_with_ips, context, None, db_port, new_port, mock.Mock()) mocks['ipam']._ipam_deallocate_ips.assert_called_once_with( context, mocks['driver'], db_port, changes.add, revert_on_fail=False) mocks['ipam']._ipam_allocate_ips.assert_called_once_with( context, mocks['driver'], db_port, changes.remove, revert_on_fail=False)
def test_notify_port_status_all_values(self): states = [n_const.PORT_STATUS_ACTIVE, n_const.PORT_STATUS_DOWN, n_const.PORT_STATUS_ERROR, n_const.PORT_STATUS_BUILD, sql_attr.NO_VALUE] device_id = '32102d7b-1cf4-404d-b50a-97aae1f55f87' # test all combinations for previous_port_status in states: for current_port_status in states: port = port_obj.Port(self.ctx, id=self.port_uuid, device_id=device_id, device_owner=DEVICE_OWNER_COMPUTE, status=current_port_status) self._record_port_status_changed_helper(current_port_status, previous_port_status, port)
def _setup_port_binding(self, **kwargs): with self.ctx.session.begin(subtransactions=True): mac = netaddr.EUI(net.get_random_mac( 'fa:16:3e:00:00:00'.split(':')), dialect=netaddr.mac_unix_expanded) port_id = uuidutils.generate_uuid() network_id = kwargs.get('network_id', TEST_NETWORK_ID) device_owner = kwargs.get('device_owner', '') device_id = kwargs.get('device_id', uuidutils.generate_uuid()) host = kwargs.get('host', helpers.HOST) port_obj.Port(self.ctx, id=port_id, network_id=network_id, mac_address=mac, admin_state_up=True, status=constants.PORT_STATUS_ACTIVE, device_id=device_id, device_owner=device_owner).create() port_binding_cls = models.PortBinding binding_kwarg = { 'port_id': port_id, 'host': host, 'vif_type': portbindings.VIF_TYPE_UNBOUND, 'vnic_type': portbindings.VNIC_NORMAL } if device_owner == constants.DEVICE_OWNER_DVR_INTERFACE: port_binding_cls = models.DistributedPortBinding binding_kwarg['router_id'] = TEST_ROUTER_ID binding_kwarg['status'] = constants.PORT_STATUS_DOWN self.ctx.session.add(port_binding_cls(**binding_kwarg)) if network_id == TEST_HA_NETWORK_ID: agent = self.get_l3_agent_by_host(host) l3_hamode.L3HARouterAgentPortBinding( self.ctx, port_id=port_id, router_id=device_id, l3_agent_id=agent['id'], state=kwargs.get( 'host_state', constants.HA_ROUTER_STATE_ACTIVE)).create()
def _setup_neutron_network(self, network_id, port_ids): with self.ctx.session.begin(subtransactions=True): self.ctx.session.add(models_v2.Network(id=network_id)) ports = [] for port_id in port_ids: mac_address = (db_base_plugin_v2.NeutronDbPluginV2. _generate_mac()) port = port_obj.Port(self.ctx, id=port_id, network_id=network_id, mac_address=netaddr.EUI(mac_address), admin_state_up=True, status='ACTIVE', device_id='', device_owner='') port.create() ports.append(port) return ports
def _setup_neutron_network(self, network_id, port_ids): with db_api.context_manager.writer.using(self.ctx): network_obj.Network(self.ctx, id=network_id).create() ports = [] for port_id in port_ids: mac_address = ( db_base_plugin_v2.NeutronDbPluginV2._generate_mac()) port = port_obj.Port(self.ctx, id=port_id, network_id=network_id, mac_address=netaddr.EUI(mac_address), admin_state_up=True, status='ACTIVE', device_id='', device_owner='') port.create() ports.append(port) return ports
def _test__legacy_notifier_binding_activated(self): updated_port = ports.Port( id=self._port_id, name='updated_port', bindings=[ports.PortBinding(port_id=self._port_id, host='host2', status=constants.ACTIVE), ports.PortBinding(port_id=self._port_id, host='host1', status=constants.INACTIVE)]) self._api._legacy_notifier( resources.PORT, events.AFTER_UPDATE, self, payload=events.DBEventPayload( mock.ANY, metadata={ 'changed_fields': set(['name', 'bindings']) }, resource_id=self._port_id, states=(self._port, updated_port))) self._api._legacy_interface.port_update.assert_not_called() self._api._legacy_interface.port_delete.assert_not_called()
def _create_port(context, device_id, ip_address): network_id = 'd339eb89-3b7c-4a29-8c02-83ed329ea6d5' subnet_id = '1f9206e1-0872-4bc1-a600-3efed860ee64' fixed_ips = { 'subnet_id': subnet_id, 'network_id': network_id, 'ip_address': ip_address} ip_allocation = ports.IPAllocation(context, **fixed_ips) port_attrs = { 'network_id': network_id, 'fixed_ips': [ip_allocation] } attrs = {'project_id': 'fake', 'admin_state_up': True, 'status': 'ACTIVE', 'device_id': device_id, 'device_owner': 'compute:nova', 'mac_address': tools.get_random_EUI()} attrs.update(**port_attrs) port = ports.Port(context, **attrs) return port
def _initialize_objs(self): self.qos_policies = [] self.ports = [] self.networks = [] for net_idx in range(2): qos_policy = policy_obj.QosPolicy(self.ctx, id=uuidutils.generate_uuid(), project_id=self.project_id) qos_policy.create() self.qos_policies.append(qos_policy) # Any QoS policy should have at least one rule, in order to have # the port dictionary extended with the QoS policy information; see # QoSPlugin._extend_port_resource_request qos_rule = rule_obj.QosDscpMarkingRule( self.ctx, dscp=20, id=uuidutils.generate_uuid(), qos_policy_id=qos_policy.id) qos_rule.create() network = network_obj.Network(self.ctx, id=uuidutils.generate_uuid(), project_id=self.project_id) network.create() self.networks.append(network) for port_idx in range(3): mac_address = netaddr.EUI(net_idx * 16 + port_idx) port = port_obj.Port(self.ctx, project_id=self.project_id, network_id=network.id, device_owner='', admin_state_up=True, status='DOWN', device_id='2', mac_address=mac_address) port.create() self.ports.append(port)
def setUp(self): super(NDPProxyExtensionTestCaseBase, self).setUp() self.context = context.get_admin_context() self.connection = mock.Mock() self.ext_port_id = _uuid() self.ex_net_id = _uuid() self.ex_gw_port = { 'id': self.ext_port_id, 'network_id': self.ex_net_id, 'gw_port_host': HOSTNAME } self.fake_router_id = _uuid() self.port_id = _uuid() self.agent_api = l3_ext_api.L3AgentExtensionAPI(None, None) self.np_ext = np.NDPProxyAgentExtension() self.np_ext.consume_api(self.agent_api) self.np_ext.initialize(self.connection, lib_const.L3_AGENT_MODE) self.ndpproxy = np_obj.NDPProxy(context=None, id=_uuid(), router_id=self.fake_router_id, port_id=self.port_id, ip_address='2002::1:3') port_binding = ports_obj.PortBinding(port_id=self.port_id, host=HOSTNAME) port_obj = ports_obj.Port(id=self.port_id, bindings=[port_binding]) self.ndp_proxies = [self.ndpproxy] self.ports = [port_obj] agent_configurations = { 'agent_mode': lib_const.L3_AGENT_MODE_DVR_NO_EXTERNAL } self.agent_obj = agent_obj.Agent(id=_uuid(), host=HOSTNAME, agent_type=lib_const.AGENT_TYPE_L3, configurations=agent_configurations) self.ip_wrapper = mock.patch('neutron.agent.linux.' 'ip_lib.IPWrapper').start() self._set_pull_mock()
def setUp(self): super(TestL3GwModeMixin, self).setUp() plugin = __name__ + '.' + TestDbIntPlugin.__name__ self.setup_coreplugin(plugin) self.target_object = TestDbIntPlugin() # Patch the context ctx_patcher = mock.patch('neutron_lib.context', autospec=True) mock_context = ctx_patcher.start() self.context = mock_context.get_admin_context() # This ensure also calls to elevated work in unit tests self.context.elevated.return_value = self.context self.context.session = db_api.get_writer_session() # Create sample data for tests self.ext_net_id = _uuid() self.int_net_id = _uuid() self.int_sub_id = _uuid() self.tenant_id = 'the_tenant' self.network = net_obj.Network(self.context, id=self.ext_net_id, project_id=self.tenant_id, admin_state_up=True, status=constants.NET_STATUS_ACTIVE) self.net_ext = net_obj.ExternalNetwork(self.context, network_id=self.ext_net_id) self.network.create() self.net_ext.create() self.router = l3_models.Router(id=_uuid(), name=None, tenant_id=self.tenant_id, admin_state_up=True, status=constants.NET_STATUS_ACTIVE, enable_snat=True, gw_port_id=None) self.context.session.add(self.router) self.context.session.flush() self.router_gw_port = port_obj.Port( self.context, id=FAKE_GW_PORT_ID, project_id=self.tenant_id, device_id=self.router.id, device_owner=l3_db.DEVICE_OWNER_ROUTER_GW, admin_state_up=True, status=constants.PORT_STATUS_ACTIVE, mac_address=netaddr.EUI(FAKE_GW_PORT_MAC), network_id=self.ext_net_id) self.router_gw_port.create() self.router.gw_port_id = self.router_gw_port.id self.context.session.add(self.router) self.context.session.flush() self.fip_ext_port = port_obj.Port( self.context, id=FAKE_FIP_EXT_PORT_ID, project_id=self.tenant_id, admin_state_up=True, device_id=self.router.id, device_owner=l3_db.DEVICE_OWNER_FLOATINGIP, status=constants.PORT_STATUS_ACTIVE, mac_address=netaddr.EUI(FAKE_FIP_EXT_PORT_MAC), network_id=self.ext_net_id) self.fip_ext_port.create() self.context.session.flush() self.int_net = net_obj.Network(self.context, id=self.int_net_id, project_id=self.tenant_id, admin_state_up=True, status=constants.NET_STATUS_ACTIVE) self.int_sub = subnet_obj.Subnet( self.context, id=self.int_sub_id, project_id=self.tenant_id, ip_version=4, cidr=utils.AuthenticIPNetwork('3.3.3.0/24'), gateway_ip=netaddr.IPAddress('3.3.3.1'), network_id=self.int_net_id) self.router_port = port_obj.Port( self.context, id=FAKE_ROUTER_PORT_ID, project_id=self.tenant_id, admin_state_up=True, device_id=self.router.id, device_owner=l3_db.DEVICE_OWNER_ROUTER_INTF, status=constants.PORT_STATUS_ACTIVE, mac_address=netaddr.EUI(FAKE_ROUTER_PORT_MAC), network_id=self.int_net_id) self.router_port_ip_info = port_obj.IPAllocation( self.context, port_id=self.router_port.id, network_id=self.int_net.id, subnet_id=self.int_sub_id, ip_address='3.3.3.1') self.int_net.create() self.int_sub.create() self.router_port.create() self.router_port_ip_info.create() self.context.session.flush() self.fip_int_port = port_obj.Port( self.context, id=FAKE_FIP_INT_PORT_ID, project_id=self.tenant_id, admin_state_up=True, device_id='something', device_owner=constants.DEVICE_OWNER_COMPUTE_PREFIX + 'nova', status=constants.PORT_STATUS_ACTIVE, mac_address=netaddr.EUI(FAKE_FIP_INT_PORT_MAC), network_id=self.int_net_id) self.fip_int_ip_info = port_obj.IPAllocation( self.context, port_id=self.fip_int_port.id, network_id=self.int_net.id, subnet_id=self.int_sub_id, ip_address='3.3.3.3') self.fip = l3_models.FloatingIP(id=_uuid(), floating_ip_address='1.1.1.2', floating_network_id=self.ext_net_id, floating_port_id=FAKE_FIP_EXT_PORT_ID, fixed_port_id=None, fixed_ip_address=None, router_id=None) self.fip_int_port.create() self.fip_int_ip_info.create() self.context.session.add(self.fip) self.context.session.flush() self.context.session.expire_all() self.fip_request = { 'port_id': FAKE_FIP_INT_PORT_ID, 'tenant_id': self.tenant_id }