diff --git a/.pytest_cache/v/cache/lastfailed b/.pytest_cache/v/cache/lastfailed deleted file mode 100644 index 5b10349..0000000 --- a/.pytest_cache/v/cache/lastfailed +++ /dev/null @@ -1,12 +0,0 @@ -{ - "tests/test_clients.py::TestVNCAPIClient::test_get_all_vms": true, - "tests/test_controllers.py::TestVmwareController::test_handle_update_delete_vm": true, - "tests/test_events.py::test_vm_reconfigured": true, - "tests/test_events.py::test_vm_renamed": true, - "tests/test_models.py::TestVirtualMachineModel::test_to_vnc": true, - "tests/test_services.py::TestVirtualMachineInterfaceService::test_remove_vmis_for_vm_model": true, - "tests/test_services.py::TestVirtualMachineInterfaceService::test_rename_vmis": true, - "tests/test_services.py::TestVirtualMachineService::test_destroy_property_filter": true, - "tests/test_services.py::TestVirtualMachineService::test_remove_vm": true, - "tests/test_services.py::TestVirtualMachineService::test_rename_vm": true -} \ No newline at end of file diff --git a/.pytest_cache/v/cache/nodeids b/.pytest_cache/v/cache/nodeids deleted file mode 100644 index a9c63ef..0000000 --- a/.pytest_cache/v/cache/nodeids +++ /dev/null @@ -1,78 +0,0 @@ -[ - "tests/test_clients.py::TestESXiAPIClient::test_read_vm", - "tests/test_clients.py::TestVCenterAPIClient::test_enable_vlan_override", - "tests/test_clients.py::TestVCenterAPIClient::test_set_vlan_id", - "tests/test_clients.py::TestFunctions::test_make_dv_port_spec", - "tests/test_clients.py::TestVNCAPIClient::test_get_all_vms", - "tests/test_clients.py::TestVNCAPIClient::test_update_create_new_vm", - "tests/test_clients.py::TestVNCAPIClient::test_update_create_new_vmi", - "tests/test_clients.py::TestVNCAPIClient::test_update_create_vm", - "tests/test_clients.py::TestVNCAPIClient::test_update_create_vmi", - "tests/test_controllers.py::TestVmwareController::test_handle_tools_deleted_vm", - "tests/test_controllers.py::TestVmwareController::test_handle_tools_status", - "tests/test_controllers.py::TestVmwareController::test_handle_update_delete_vm", - "tests/test_controllers.py::TestVmwareController::test_handle_update_deleted_vm", - "tests/test_controllers.py::TestVmwareController::test_handle_update_no_chng_set", - "tests/test_controllers.py::TestVmwareController::test_handle_update_no_fltr_set", - "tests/test_controllers.py::TestVmwareController::test_handle_update_no_obj_set", - "tests/test_controllers.py::TestVmwareController::test_vm_renamed", - "tests/test_database.py::TestFindVirtualMachineIpAddress::test_delete_vm_model", - "tests/test_database.py::TestFindVirtualMachineIpAddress::test_delete_vmi_model", - "tests/test_database.py::TestFindVirtualMachineIpAddress::test_delete_vn_model", - "tests/test_database.py::TestFindVirtualMachineIpAddress::test_get_vm_model_by_uuid", - "tests/test_database.py::TestFindVirtualMachineIpAddress::test_get_vn_model_by_key", - "tests/test_database.py::TestFindVirtualMachineIpAddress::test_get_vn_model_by_uuid", - "tests/test_events.py::test_vm_created", - "tests/test_events.py::test_vm_renamed", - "tests/test_events.py::test_vm_reconfigured", - "tests/test_models.py::TestFindVirtualMachineIpAddress::test_missing_field", - "tests/test_models.py::TestFindVirtualMachineIpAddress::test_missing_field_in_network", - "tests/test_models.py::TestFindVirtualMachineIpAddress::test_standard_case", - "tests/test_models.py::TestFindVirtualMachineIpAddress::test_unmatched_ip_type", - "tests/test_models.py::TestFindVirtualMachineIpAddress::test_unmatched_portgroup_name", - "tests/test_models.py::TestVirtualMachineModel::test_init", - "tests/test_models.py::TestVirtualMachineModel::test_to_vnc", - "tests/test_models.py::TestVirtualMachineModel::test_update", - "tests/test_models.py::TestVirtualMachineInterfaceModel::test_construct_instance_ip", - "tests/test_models.py::TestVirtualMachineInterfaceModel::test_to_vnc", - "tests/test_models.py::TestVlanIdPool::test_free", - "tests/test_models.py::TestVlanIdPool::test_get_first_available", - "tests/test_models.py::TestVlanIdPool::test_no_available", - "tests/test_models.py::TestVlanIdPool::test_reserve", - "tests/test_models.py::TestVlanIdPool::test_reserve_existing", - "tests/test_models.py::TestVirtualNetworkVlans::test_sync_vlan_ids", - "tests/test_models.py::TestVirtualNetworkVlans::test_vmi_vlan_id_aquisition", - "tests/test_monitors.py::test_pass_update_to_controller", - "tests/test_services.py::TestVirtualMachineService::test_create_property_filter", - "tests/test_services.py::TestVirtualMachineService::test_delete_unused_vms", - "tests/test_services.py::TestVirtualMachineService::test_destroy_property_filter", - "tests/test_services.py::TestVirtualMachineService::test_remove_no_vm", - "tests/test_services.py::TestVirtualMachineService::test_remove_vm", - "tests/test_services.py::TestVirtualMachineService::test_rename_vm", - "tests/test_services.py::TestVirtualMachineService::test_set_tools_running_status", - "tests/test_services.py::TestVirtualMachineService::test_sync_no_vms", - "tests/test_services.py::TestVirtualMachineService::test_sync_vms", - "tests/test_services.py::TestVirtualMachineService::test_update_existing_vm", - "tests/test_services.py::TestVirtualMachineService::test_update_new_vm", - "tests/test_services.py::TestVirtualMachineInterfaceService::test_create_vmis_proper_vm_dpg", - "tests/test_services.py::TestVirtualMachineInterfaceService::test_no_update_for_no_dpgs", - "tests/test_services.py::TestVirtualMachineInterfaceService::test_remove_vmis_for_vm_model", - "tests/test_services.py::TestVirtualMachineInterfaceService::test_remove_vmis_no_vm_model", - "tests/test_services.py::TestVirtualMachineInterfaceService::test_removes_unused_vmis", - "tests/test_services.py::TestVirtualMachineInterfaceService::test_rename_vmis", - "tests/test_services.py::TestVirtualMachineInterfaceService::test_sync_deletes_unused_vmis", - "tests/test_services.py::TestVirtualMachineInterfaceService::test_sync_no_vmis", - "tests/test_services.py::TestVirtualMachineInterfaceService::test_sync_vmis", - "tests/test_services.py::TestVirtualMachineInterfaceService::test_syncs_one_vmi_once", - "tests/test_services.py::TestVirtualMachineInterfaceService::test_update_existing_vmi", - "tests/test_services.py::TestVirtualNetworkService::test_sync_no_vns", - "tests/test_services.py::TestVirtualNetworkService::test_sync_vns", - "tests/test_services.py::TestVMIInstanceIp::test_delete_vmi", - "tests/test_services.py::TestVMIInstanceIp::test_update_vmi", - "tests/test_services.py::TestVNCEnvironmentSetup::test_read_ipam", - "tests/test_services.py::TestVNCEnvironmentSetup::test_read_no_ipam", - "tests/test_services.py::TestVNCEnvironmentSetup::test_read_no_project", - "tests/test_services.py::TestVNCEnvironmentSetup::test_read_no_security_group", - "tests/test_services.py::TestVNCEnvironmentSetup::test_read_project", - "tests/test_services.py::TestVNCEnvironmentSetup::test_read_security_group" -] \ No newline at end of file diff --git a/cvm/clients.py b/cvm/clients.py index 377335d..5aaad2d 100644 --- a/cvm/clients.py +++ b/cvm/clients.py @@ -175,7 +175,7 @@ def set_vlan_id(self, dvs_name, key, vlan_id): except StopIteration: return dv_port_spec = make_dv_port_spec(dv_port, vlan_id) - logger.info('Setting VLAN ID of port %s to %d' % (key, vlan_id)) + logger.info('Setting VLAN ID of port %s to %d', key, vlan_id) dvs.ReconfigureDVPort_Task(port=[dv_port_spec]) def restore_vlan_id(self, dvs_name, key): @@ -214,6 +214,7 @@ def __init__(self, vnc_cfg): self.id_perms.set_enable(True) def delete_vm(self, vnc_vm): + # TODO: Remove code duplication -- see VirtualMachineService._can_delete_from_vnc try: existing_vm = self.read_vm(vnc_vm.uuid) vrouter_uuid = next(pair.value @@ -224,8 +225,8 @@ def delete_vm(self, vnc_vm): self.vnc_lib.virtual_machine_delete(id=vnc_vm.uuid) logger.info('Virtual Machine removed: %s', vnc_vm.name) else: - logger.error('Virtual Machine %s is managed by vRouter %s and cannot be deleted from VNC.' % - (vnc_vm.name, vrouter_uuid)) + logger.error('Virtual Machine %s is managed by vRouter %s and cannot be deleted from VNC.', + vnc_vm.name, vrouter_uuid) except NoIdError: logger.error('Virtual Machine not found: %s', vnc_vm.name) @@ -287,7 +288,7 @@ def read_vmi(self, uuid): try: return self.vnc_lib.virtual_machine_interface_read(id=uuid) except NoIdError: - logger.error('Virtual Machine Interface not found %s' % uuid) + logger.error('Virtual Machine Interface not found %s', uuid) return None def get_vns_by_project(self, project): @@ -357,7 +358,7 @@ def create_and_read_instance_ip(self, instance_ip): # TODO: Refactor this except NoIdError: self.vnc_lib.instance_ip_create(instance_ip) - logger.debug("Created instanceIP: " + instance_ip.name) + logger.debug("Created instanceIP: %s", instance_ip.name) return self._read_instance_ip(instance_ip.uuid) def delete_instance_ip(self, uuid): @@ -440,17 +441,17 @@ def add_port(self, vmi_model): vm_project_id=vmi_model.vn_model.vnc_vn.parent_uuid, ) except Exception, e: - logger.error('There was a problem with vRouter API Client: %s' % e) + logger.error('There was a problem with vRouter API Client: %s', e) def delete_port(self, vmi_uuid): """ Delete port from VRouter Agent. """ try: self.vrouter_api.delete_port(vmi_uuid) except Exception, e: - logger.error('There was a problem with vRouter API Client: %s' % e) + logger.error('There was a problem with vRouter API Client: %s', e) def enable_port(self, vmi_uuid): try: self.vrouter_api.enable_port(vmi_uuid) except Exception, e: - logger.error('There was a problem with vRouter API Client: %s' % e) + logger.error('There was a problem with vRouter API Client: %s', e) diff --git a/cvm/services.py b/cvm/services.py index 54fdc06..3aae00a 100644 --- a/cvm/services.py +++ b/cvm/services.py @@ -82,8 +82,8 @@ def _can_delete_from_vnc(self, vm_model): if any([pair.key == 'vrouter-uuid' and pair.value == vrouter_uuid for pair in existing_vm.get_annotations().key_value_pair]): return True - logger.error('Virtual Machine %s is managed by vRouter %s and cannot be deleted from VNC.' % - (vm_model.name, vrouter_uuid)) + logger.error('Virtual Machine %s is managed by vRouter %s and cannot be deleted from VNC.', + vm_model.name, vrouter_uuid) return False def set_tools_running_status(self, vmware_vm, value): @@ -175,7 +175,6 @@ def _create_or_update(self, vmi_model): self._vnc_api_client.update_or_create_vmi(vmi_model.to_vnc()) vmi_model.construct_instance_ip() if vmi_model.vnc_instance_ip: - self._vnc_api_client.delete_instance_ip(uuid=vmi_model.vnc_instance_ip.uuid) instance_ip = self._vnc_api_client.create_and_read_instance_ip(vmi_model.vnc_instance_ip) vmi_model.vnc_instance_ip = instance_ip self._add_or_update_vrouter_port(vmi_model) @@ -220,6 +219,7 @@ def _delete_from_vnc(self, vmi_model): if vmi_model.vnc_instance_ip: self._vnc_api_client.delete_instance_ip(vmi_model.vnc_instance_ip.uuid) self._vnc_api_client.delete_vmi(vmi_model.uuid) + vmi_model.vnc_vmi = None def _restore_vlan_id(self, vmi_model): with self._vcenter_api_client: @@ -234,8 +234,8 @@ def _can_delete_from_vnc(self, vmi_model): if any([pair.key == 'vrouter-uuid' and pair.value == vrouter_uuid for pair in existing_vmi.get_annotations().key_value_pair]): return True - logger.error('Virtual Machine Interface %s is managed by vRouter %s and cannot be deleted from VNC.' % - (vmi_model.vnc_vmi.display_name, vrouter_uuid)) + logger.error('Virtual Machine Interface %s is managed by vRouter %s and cannot be deleted from VNC.', + vmi_model.vnc_vmi.display_name, vrouter_uuid) return False def _delete_vrouter_port(self, vmi_model): diff --git a/tests/test_clients.py b/tests/test_clients.py index 612cde7..1d7cef3 100644 --- a/tests/test_clients.py +++ b/tests/test_clients.py @@ -127,5 +127,5 @@ def test_get_all_vms(self): all_vms = self.vnc_client.get_all_vms() - self.vnc_lib.virtual_machine_read.assert_called_once_with(['5027a82e-fbc7-0898-b64c-4bf9f5b9d07c']) + self.vnc_lib.virtual_machine_read.assert_called_once_with(id=u'5027a82e-fbc7-0898-b64c-4bf9f5b9d07c') self.assertEqual([vnc_vm], all_vms) diff --git a/tests/test_controllers.py b/tests/test_controllers.py index 9e56c7e..34e15b7 100644 --- a/tests/test_controllers.py +++ b/tests/test_controllers.py @@ -4,7 +4,8 @@ from mock import Mock, patch from pyVmomi import vim, vmodl # pylint: disable=no-name-in-module -from cvm.controllers import VmRenamedHandler, VmwareController +from cvm.controllers import (VmRemovedHandler, VmRenamedHandler, + VmwareController) logging.disable(logging.CRITICAL) @@ -25,8 +26,10 @@ def setUp(self): self.vmi_service = Mock() vm_renamed_handler = VmRenamedHandler(self.vm_service, self.vmi_service) + vm_removed_handler = VmRemovedHandler(self.vm_service, self.vmi_service) + handlers = [vm_renamed_handler, vm_removed_handler] self.vmware_controller = VmwareController(self.vm_service, None, self.vmi_service, - [vm_renamed_handler]) + handlers) @patch.object(VmwareController, '_handle_change') def test_handle_update_no_fltr_set(self, mocked_handle_change): diff --git a/tests/test_events.py b/tests/test_events.py index 5b37960..fe473f4 100644 --- a/tests/test_events.py +++ b/tests/test_events.py @@ -114,10 +114,10 @@ def vm_created_update(vmware_vm_1): @pytest.fixture() -def vm_renamed_update(vmware_vm_1): +def vm_renamed_update(): event = Mock(spec=vim.event.VmRenamedEvent()) - vmware_vm_1.name = 'VM1-renamed' - event.vm.vm = vmware_vm_1 + event.oldName = 'VM1' + event.newName = 'VM1-renamed' return wrap_into_update_set(event) diff --git a/tests/test_models.py b/tests/test_models.py index 91d6d95..7911312 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -113,14 +113,15 @@ def test_init(self): def test_to_vnc(self): vm_model = VirtualMachineModel(self.vmware_vm, self.vm_properties) vm_model.vm_properties['config.instanceUuid'] = 'd376b6b4-943d-4599-862f-d852fd6ba425' - vm_model.vrouter_uuid = '192.168.0.10' + vm_model.vrouter_uuid = 'vrouter_uuid' vnc_vm = vm_model.vnc_vm - self.assertEqual(vnc_vm.name, vm_model.uuid) - self.assertEqual(vnc_vm.uuid, vm_model.uuid) - self.assertEqual(vnc_vm.display_name, vm_model.vrouter_uuid) - self.assertEqual(vnc_vm.fq_name, [vm_model.uuid]) + self.assertEqual(vm_model.uuid, vnc_vm.name) + self.assertEqual(vm_model.uuid, vnc_vm.uuid) + self.assertEqual([vm_model.uuid], vnc_vm.fq_name) + vrouter_annotation = vnc_vm.get_annotations().get_key_value_pair()[0] + self.assertEqual('vrouter_uuid', vrouter_annotation.value) def test_update(self): vm_model = VirtualMachineModel(self.vmware_vm, self.vm_properties) diff --git a/tests/test_services.py b/tests/test_services.py index 6b04b93..ba35e64 100644 --- a/tests/test_services.py +++ b/tests/test_services.py @@ -61,7 +61,9 @@ def test_destroy_property_filter(self): vm_model = Mock() self.database.get_vm_model_by_name.return_value = vm_model - self.vm_service.remove_vm('VM') + with patch('cvm.services.VirtualMachineService._can_delete_from_vnc') as can_delete: + can_delete.return_value = True + self.vm_service.remove_vm('VM') vm_model.destroy_property_filter.assert_called_once() @@ -111,10 +113,12 @@ def test_remove_vm(self): vm_model = Mock(uuid='d376b6b4-943d-4599-862f-d852fd6ba425') self.database.get_vm_model_by_name.return_value = vm_model - self.vm_service.remove_vm('VM') + with patch('cvm.services.VirtualMachineService._can_delete_from_vnc') as can_delete: + can_delete.return_value = True + self.vm_service.remove_vm('VM') self.database.delete_vm_model.assert_called_once_with(vm_model.uuid) - self.vnc_client.delete_vm.assert_called_once_with(vm_model.uuid) + self.vnc_client.delete_vm.assert_called_once_with(vm_model.vnc_vm) def test_remove_no_vm(self): """ Remove VM should do nothing when VM doesn't exist in database. """ @@ -139,13 +143,13 @@ def test_set_tools_running_status(self): def test_rename_vm(self): vm_model = Mock() vm_model.configure_mock(name='VM') - self.database.get_vm_model_by_uuid.return_value = vm_model + self.database.get_vm_model_by_name.return_value = vm_model vmware_vm = Mock() vmware_vm.configure_mock(name='VM-renamed') - self.vm_service.rename_vm(vmware_vm) + self.vm_service.rename_vm('VM', 'VM-renamed') - vm_model.rename.assert_called_once_with(vmware_vm) + vm_model.rename.assert_called_once_with('VM-renamed') self.vnc_client.update_or_create_vm.assert_called_once() self.database.save.assert_called_once_with(vm_model) @@ -280,10 +284,11 @@ def test_remove_vmis_for_vm_model(self): self.database.save(vmi_model) self.database.save(self.vm_model) - with patch.object(self.database, 'delete_vmi_model') as database_del_mock: + with patch('cvm.services.VirtualMachineInterfaceService._can_delete_from_vnc') as can_delete: + can_delete.return_value = True self.vmi_service.remove_vmis_for_vm_model(self.vm_model.name) - database_del_mock.assert_called_once_with(vmi_model.uuid) + self.assertNotIn(vmi_model, self.database.get_all_vmi_models()) self.vnc_client.delete_vmi.assert_called_once_with(vmi_model.uuid) def test_remove_vmis_no_vm_model(self): @@ -305,7 +310,7 @@ def test_rename_vmis(self): self.vm_model.update(*create_vmware_vm_mock(name='VM-renamed')) self.database.save(self.vm_model) - self.vmi_service.rename_vmis(self.vm_model.vmware_vm) + self.vmi_service.rename_vmis('VM-renamed') self.assertEqual('vmi-VM Portgroup-VM-renamed', vmi_model.display_name) self.assertEqual(0, self.vnc_client.create_and_read_instance_ip.call_count)