From 137987d1bc7628f98eca36b99b8ee36a5122f7e8 Mon Sep 17 00:00:00 2001 From: dweinholz Date: Wed, 17 Jan 2024 13:03:51 +0100 Subject: [PATCH] fixed tests --- .gitignore | 2 +- .../openstack_connector.py | 17 +++++++++++------ simple_vm_client/test_openstack_connector.py | 19 ++++++++----------- simple_vm_client/util/thrift_converter.py | 2 +- 4 files changed, 21 insertions(+), 19 deletions(-) diff --git a/.gitignore b/.gitignore index c210e3f..347834a 100644 --- a/.gitignore +++ b/.gitignore @@ -22,7 +22,7 @@ wheels/ *.egg-info/ .installed.cfg *.egg -.env.dsi +.env.* # PyInstaller # Usually these files are written by a python script from a template # before PyInstaller builds the exe, so as to inject date/other infos into it. diff --git a/simple_vm_client/openstack_connector/openstack_connector.py b/simple_vm_client/openstack_connector/openstack_connector.py index d73e947..13fd9d9 100644 --- a/simple_vm_client/openstack_connector/openstack_connector.py +++ b/simple_vm_client/openstack_connector/openstack_connector.py @@ -77,10 +77,10 @@ def __init__(self, config_file: str): self.APPLICATION_CREDENTIAL_ID = "" self.APPLICATION_CREDENTIAL_SECRET = "" self.USE_APPLICATION_CREDENTIALS: bool = False - self.NOVA_MICROVERSION = '2.1' + self.NOVA_MICROVERSION = "2.1" self.load_env_config() - print("loading config file") + logger.info(f"Loading config file -- {config_file}") self.load_config_yml(config_file) try: @@ -102,9 +102,11 @@ def __init__(self, config_file: str): project_name=self.PROJECT_NAME, user_domain_name=self.USER_DOMAIN_NAME, project_domain_id=self.PROJECT_DOMAIN_ID, - compute_api_version=self.NOVA_MICROVERSION + compute_api_version=self.NOVA_MICROVERSION, ) self.openstack_connection.authorize() + self.get_network() + logger.info("Connected to Openstack") self.create_or_get_default_ssh_security_group() except Exception: @@ -392,10 +394,13 @@ def create_volume( raise ResourceNotAvailableException(message=e.message) def get_network(self) -> Network: - network: Network = self.openstack_connection.network.find_network(self.NETWORK) + logger.info(f"Getting Network: {self.NETWORK}") + network: Network = self.openstack_connection.get_network( + name_or_id=self.NETWORK + ) if network is None: - logger.exception(f"Network {network} not found!") - raise Exception(f"Network {network} not found!") + logger.exception(f"Network {self.NETWORK} not found!") + raise Exception(f"Network {self.NETWORK} not found!") return network def import_keypair(self, keyname: str, public_key: str) -> dict[str, str]: diff --git a/simple_vm_client/test_openstack_connector.py b/simple_vm_client/test_openstack_connector.py index fd4edb4..f60e614 100644 --- a/simple_vm_client/test_openstack_connector.py +++ b/simple_vm_client/test_openstack_connector.py @@ -956,9 +956,7 @@ def test_create_volume(self, mock_logger_info, mock_logger_exception): ) def test_network_not_found(self): - self.openstack_connector.openstack_connection.network.find_network.return_value = ( - None - ) + self.openstack_connector.openstack_connection.get_network.return_value = None with self.assertRaises(Exception): self.openstack_connector.get_network() @@ -969,20 +967,19 @@ def test_get_network(self, mock_logger_exception): # Call the load_config_yml method with the temporary file path self.openstack_connector.load_config_yml(temp_file.name) + network = fakes.generate_fake_resource(Network) # Mock the find_network method - self.mock_openstack_connection.network.find_network.return_value = Network( - id="my_network" - ) + self.mock_openstack_connection.get_network.return_value = network # Call the get_network method result_network = self.openstack_connector.get_network() # Assertions self.assertIsInstance(result_network, Network) - self.assertEqual(result_network.id, "my_network") - self.mock_openstack_connection.network.find_network.assert_called_once_with( - self.openstack_connector.NETWORK + self.assertEqual(result_network.id, network.id) + self.mock_openstack_connection.get_network.assert_called_once_with( + name_or_id=self.openstack_connector.NETWORK ) mock_logger_exception.assert_not_called() # Ensure no exception is logged @@ -1509,7 +1506,7 @@ def test_start_server_with_playbook( self.openstack_connector.openstack_connection.get_flavor.return_value = ( fake_flavor ) - self.openstack_connector.openstack_connection.network.find_network.return_value = ( + self.openstack_connector.openstack_connection.get_network.return_value = ( fake_network ) mock_get_volumes.return_value = ["volume1", "volume2"] @@ -1787,7 +1784,7 @@ def test_start_server( self.openstack_connector.openstack_connection.get_flavor.return_value = ( fake_flavor ) - self.openstack_connector.openstack_connection.network.find_network.return_value = ( + self.openstack_connector.openstack_connection.get_network.return_value = ( fake_network ) self.openstack_connector.openstack_connection.compute.find_keypair.return_value = ( diff --git a/simple_vm_client/util/thrift_converter.py b/simple_vm_client/util/thrift_converter.py index 0cfd560..4be7d00 100644 --- a/simple_vm_client/util/thrift_converter.py +++ b/simple_vm_client/util/thrift_converter.py @@ -68,7 +68,7 @@ def os_to_thrift_volume(openstack_volume: OpenStack_Volume) -> Volume: device = openstack_volume.attachments[0]["device"] server_id = openstack_volume.attachments[0]["server_id"] except Exception as e: - logger.log(f"Exception on os_to_thrift_volume:\n{e}") + logger.error(f"Exception on os_to_thrift_volume:\n{e}") volume = Volume( status=openstack_volume.status, id=openstack_volume.id,