def test_update_port_with_no_mac_address(self, mock_get_port_name, mock_update_port): fake_endpoint_id = lib_utils.get_hash() fake_neutron_port_id = uuidutils.generate_uuid() fake_neutron_net_id = uuidutils.generate_uuid() fake_neutron_v4_subnet_id = uuidutils.generate_uuid() fake_neutron_v6_subnet_id = uuidutils.generate_uuid() fake_neutron_port = self._get_fake_port(fake_endpoint_id, fake_neutron_net_id, fake_neutron_port_id, lib_const.PORT_STATUS_ACTIVE, fake_neutron_v4_subnet_id, fake_neutron_v6_subnet_id, '192.168.1.3', 'fe80::f816:3eff:fe1c:36a9', admin_state_up=False, binding_host='')['port'] fake_port_name = '-'.join([fake_endpoint_id, lib_utils.PORT_POSTFIX]) mock_get_port_name.return_value = fake_port_name veth_driver = veth.VethDriver() veth_driver.update_port(fake_neutron_port, fake_endpoint_id, '') mock_get_port_name.assert_called_with(fake_endpoint_id) expected_update_port = { 'port': { 'device_owner': lib_const.DEVICE_OWNER, 'binding:host_id': lib_utils.get_hostname(), 'admin_state_up': True, } } mock_update_port.assert_called_with(fake_neutron_port_id, expected_update_port)
def test_update_port_with_no_changes(self, mock_get_port_name, mock_update_port): fake_endpoint_id = lib_utils.get_hash() fake_neutron_port_id = uuidutils.generate_uuid() fake_neutron_net_id = uuidutils.generate_uuid() fake_neutron_v4_subnet_id = uuidutils.generate_uuid() fake_neutron_v6_subnet_id = uuidutils.generate_uuid() fake_neutron_port = self._get_fake_port( fake_endpoint_id, fake_neutron_net_id, fake_neutron_port_id, lib_const.PORT_STATUS_ACTIVE, fake_neutron_v4_subnet_id, fake_neutron_v6_subnet_id, '192.168.1.3', 'fe80::f816:3eff:fe1c:36a9', binding_host=lib_utils.get_hostname())['port'] fake_port_name = '-'.join([fake_endpoint_id, lib_utils.PORT_POSTFIX]) mock_get_port_name.return_value = fake_port_name veth_driver = veth.VethDriver() veth_driver.update_port(fake_neutron_port, fake_endpoint_id, '') mock_get_port_name.assert_called_with(fake_endpoint_id) mock_update_port.assert_not_called()
def _get_fake_port_map( neutron_network_id, docker_endpoint_id, neutron_subnetv4_id, neutron_subnetv6_id): fake_port = { "port": { "status": "DOWN", "name": utils.get_neutron_port_name(docker_endpoint_id), "allowed_address_pairs": [], "admin_state_up": True, "binding:host_id": lib_utils.get_hostname(), "network_id": neutron_network_id, "tenant_id": "d6700c0c9ffa4f1cb322cd4a1f3906fa", 'device_owner': lib_const.DEVICE_OWNER, 'device_id': docker_endpoint_id, "mac_address": "fa:16:3e:20:57:c3", 'fixed_ips': [{ 'subnet_id': neutron_subnetv4_id, 'ip_address': '192.168.1.2' }, { 'subnet_id': neutron_subnetv6_id, 'ip_address': 'fe80::f816:3eff:fe20:57c4' }], "id": "65c0ee9f-d634-4522-8954-51021b570b0d", "security_groups": [], } } return fake_port
def _get_fake_port_request(neutron_network_id, docker_endpoint_id, neutron_subnetv4_id, neutron_subnetv6_id): fake_port_request = { 'port': { 'name': utils.get_neutron_port_name(docker_endpoint_id), 'admin_state_up': True, "binding:host_id": lib_utils.get_hostname(), 'device_owner': lib_const.DEVICE_OWNER, 'device_id': docker_endpoint_id, 'fixed_ips': [{ 'subnet_id': neutron_subnetv4_id, 'ip_address': '192.168.1.2' }, { 'subnet_id': neutron_subnetv6_id, 'ip_address': 'fe80::f816:3eff:fe20:57c4' }], 'mac_address': "fa:16:3e:20:57:c3", 'network_id': neutron_network_id } } return fake_port_request
def update_port(self, port, endpoint_id, interface_mac): """Updates port information and performs extra driver-specific actions. It returns the updated port dictionary after the required actions performed depending on the binding driver. :param port: a neutron port dictionary returned from python-neutronclient :param endpoint_id: the ID of the endpoint as string :param interface_mac: the MAC address of the endpoint :returns: the updated Neutron port id dictionary as returned by python-neutronclient """ port['name'] = libnet_utils.get_neutron_port_name(endpoint_id) try: updated_port = { 'name': port['name'], 'device_owner': lib_const.DEVICE_OWNER, 'binding:host_id': lib_utils.get_hostname(), } if not port.get('device_id'): updated_port['device_id'] = endpoint_id if interface_mac: updated_port['mac_address'] = interface_mac response_port = app.neutron.update_port(port['id'], {'port': updated_port}) except n_exceptions.NeutronClientException as ex: LOG.error("Error happened during updating a " "Neutron port: %s", ex) raise return response_port['port']
def test_update_port_with_mac_address(self, mock_get_port_name, mock_update_port): fake_endpoint_id = lib_utils.get_hash() fake_neutron_port_id = uuidutils.generate_uuid() fake_neutron_net_id = uuidutils.generate_uuid() fake_neutron_v4_subnet_id = uuidutils.generate_uuid() fake_neutron_v6_subnet_id = uuidutils.generate_uuid() fake_mac_address1 = 'fa:16:3e:20:57:c3' fake_mac_address2 = 'fa:16:3e:20:57:c4' fake_neutron_port = self._get_fake_port( fake_endpoint_id, fake_neutron_net_id, fake_neutron_port_id, lib_const.PORT_STATUS_ACTIVE, fake_neutron_v4_subnet_id, fake_neutron_v6_subnet_id, '192.168.1.3', 'fe80::f816:3eff:fe1c:36a9', fake_mac_address1, admin_state_up=False, binding_host='')['port'] fake_port_name = '-'.join([fake_endpoint_id, lib_utils.PORT_POSTFIX]) mock_get_port_name.return_value = fake_port_name veth_driver = veth.VethDriver() veth_driver.update_port(fake_neutron_port, fake_endpoint_id, fake_mac_address2) mock_get_port_name.assert_called_with(fake_endpoint_id) expected_update_port = { 'port': { 'device_owner': lib_const.DEVICE_OWNER, 'binding:host_id': lib_utils.get_hostname(), 'mac_address': fake_mac_address2, 'admin_state_up': True, } } mock_update_port.assert_called_with(fake_neutron_port_id, expected_update_port)
def test_update_port_with_device_id(self, mock_get_port_name, mock_update_port): fake_endpoint_id = lib_utils.get_hash() fake_neutron_port_id = uuidutils.generate_uuid() fake_neutron_net_id = uuidutils.generate_uuid() fake_neutron_v4_subnet_id = uuidutils.generate_uuid() fake_neutron_v6_subnet_id = uuidutils.generate_uuid() fake_mac_address1 = 'fa:16:3e:20:57:c3' fake_mac_address2 = 'fa:16:3e:20:57:c4' fake_neutron_port = self._get_fake_port( fake_endpoint_id, fake_neutron_net_id, fake_neutron_port_id, lib_const.PORT_STATUS_ACTIVE, fake_neutron_v4_subnet_id, fake_neutron_v6_subnet_id, '192.168.1.3', 'fe80::f816:3eff:fe1c:36a9', fake_mac_address1)['port'] fake_neutron_port.pop('device_id') fake_port_name = '-'.join([fake_endpoint_id, lib_utils.PORT_POSTFIX]) mock_get_port_name.return_value = fake_port_name veth_driver = veth.VethDriver() veth_driver.update_port(fake_neutron_port, fake_endpoint_id, fake_mac_address2) mock_get_port_name.assert_called_with(fake_endpoint_id) expected_update_port = { 'port': { 'name': fake_port_name, 'device_owner': lib_const.DEVICE_OWNER, 'binding:host_id': lib_utils.get_hostname(), 'device_id': fake_endpoint_id, 'mac_address': fake_mac_address2 } } mock_update_port.assert_called_with(fake_neutron_port_id, expected_update_port)
def test_update_port(self, mock_get_seg_id, mock_attach_subport, mock_get_port_name, mock_update_port, mock_get_port_from_host, mock_vlan_check): fake_endpoint_id = lib_utils.get_hash() fake_neutron_port_id = uuidutils.generate_uuid() fake_neutron_net_id = uuidutils.generate_uuid() fake_neutron_trunk_id = uuidutils.generate_uuid() fake_neutron_v4_subnet_id = uuidutils.generate_uuid() fake_neutron_v6_subnet_id = uuidutils.generate_uuid() fake_vm_port_id = uuidutils.generate_uuid() fake_neutron_mac_address1 = 'fa:16:3e:20:57:c3' fake_neutron_mac_address2 = 'fa:16:3e:20:57:c4' fake_vm_mac_address = 'fa:16:3e:20:57:c5' fake_neutron_port = self._get_fake_port( fake_endpoint_id, fake_neutron_net_id, fake_neutron_port_id, lib_const.PORT_STATUS_ACTIVE, fake_neutron_v4_subnet_id, fake_neutron_v6_subnet_id, '192.168.1.3', 'fe80::f816:3eff:fe1c:36a9', fake_neutron_mac_address1)['port'] fake_vm_port = self._get_fake_port( fake_endpoint_id, fake_neutron_net_id, fake_vm_port_id, lib_const.PORT_STATUS_ACTIVE, fake_neutron_v4_subnet_id, fake_neutron_v6_subnet_id, '192.168.1.2', 'fe80::f816:3eff:fe20:57c4', fake_vm_mac_address, None, fake_neutron_trunk_id)['port'] fake_segmentation_id = 1 fake_port_name = 'port1' mock_get_seg_id.return_value = fake_segmentation_id mock_get_port_name.return_value = fake_port_name mock_get_port_from_host.return_value = fake_vm_port mock_vlan_check.return_value = None vlan_driver = vlan.VlanDriver() vlan_driver.update_port(fake_neutron_port, fake_endpoint_id, fake_neutron_mac_address2) mock_get_seg_id.assert_called_with(fake_neutron_port_id) mock_get_port_name.assert_called_with(fake_endpoint_id) mock_attach_subport.assert_called_with(fake_neutron_trunk_id, fake_neutron_port_id, fake_segmentation_id) mock_update_port.assert_called_with( fake_neutron_port_id, { 'port': { 'name': fake_port_name, 'device_owner': lib_const.DEVICE_OWNER, 'binding:host_id': lib_utils.get_hostname(), 'mac_address': fake_neutron_mac_address2 } })
def test_update_port(self, mock_get_seg_id, mock_attach_subport, mock_get_port_name, mock_update_port, mock_get_port_from_host, mock_vlan_check): fake_endpoint_id = lib_utils.get_hash() fake_neutron_port_id = uuidutils.generate_uuid() fake_neutron_net_id = uuidutils.generate_uuid() fake_neutron_trunk_id = uuidutils.generate_uuid() fake_neutron_v4_subnet_id = uuidutils.generate_uuid() fake_neutron_v6_subnet_id = uuidutils.generate_uuid() fake_vm_port_id = uuidutils.generate_uuid() fake_neutron_mac_address1 = 'fa:16:3e:20:57:c3' fake_neutron_mac_address2 = 'fa:16:3e:20:57:c4' fake_vm_mac_address = 'fa:16:3e:20:57:c5' fake_neutron_port = self._get_fake_port( fake_endpoint_id, fake_neutron_net_id, fake_neutron_port_id, lib_const.PORT_STATUS_ACTIVE, fake_neutron_v4_subnet_id, fake_neutron_v6_subnet_id, '192.168.1.3', 'fe80::f816:3eff:fe1c:36a9', fake_neutron_mac_address1, binding_host='', admin_state_up=False)['port'] fake_vm_port = self._get_fake_port( fake_endpoint_id, fake_neutron_net_id, fake_vm_port_id, lib_const.PORT_STATUS_ACTIVE, fake_neutron_v4_subnet_id, fake_neutron_v6_subnet_id, '192.168.1.2', 'fe80::f816:3eff:fe20:57c4', fake_vm_mac_address, None, fake_neutron_trunk_id)['port'] fake_segmentation_id = 1 fake_port_name = 'port1' mock_get_seg_id.return_value = fake_segmentation_id mock_get_port_name.return_value = fake_port_name mock_get_port_from_host.return_value = fake_vm_port mock_vlan_check.return_value = None vlan_driver = vlan.VlanDriver() vlan_driver.update_port(fake_neutron_port, fake_endpoint_id, fake_neutron_mac_address2) mock_get_seg_id.assert_called_with(fake_neutron_port_id) mock_get_port_name.assert_called_with(fake_endpoint_id) mock_attach_subport.assert_called_with(fake_neutron_trunk_id, fake_neutron_port_id, fake_segmentation_id) mock_update_port.assert_called_with(fake_neutron_port_id, {'port': { 'device_owner': lib_const.DEVICE_OWNER, 'binding:host_id': lib_utils.get_hostname(), 'mac_address': fake_neutron_mac_address2, 'admin_state_up': True, }})
def update_port(self, port, endpoint_id, interface_mac, tags=True): """Updates port information and performs extra driver-specific actions. It returns the updated port dictionary after the required actions performed depending on the binding driver. :param port: a neutron port dictionary returned from python-neutronclient :param endpoint_id: the ID of the endpoint as string :param interface_mac: the MAC address of the endpoint :returns: the updated Neutron port id dictionary as returned by python-neutronclient """ try: updated_port = {} hostname = lib_utils.get_hostname() if port['binding:host_id'] != hostname: updated_port['binding:host_id'] = hostname updated_port['device_owner'] = lib_const.DEVICE_OWNER if port['admin_state_up'] is not True: updated_port['admin_state_up'] = True if not tags: # rename the port if tagging is not supported updated_port['name'] = libnet_utils.get_neutron_port_name( endpoint_id) if not port.get('device_id'): updated_port['device_id'] = endpoint_id if interface_mac and port['mac_address'] != interface_mac: updated_port['mac_address'] = interface_mac if updated_port: port = app.neutron.update_port(port['id'], {'port': updated_port})['port'] except n_exceptions.NeutronClientException as ex: LOG.error("Error happened during updating a " "Neutron port: %s", ex) raise return port
def test_get_hostname(self, mock_get_hostname): self.assertEqual('fake_hostname', utils.get_hostname()) mock_get_hostname.assert_called_once()