From 217a54a07e4e6e47dd4d0763f4bf9e314e01b88c Mon Sep 17 00:00:00 2001 From: dweinholz Date: Tue, 19 Dec 2023 17:55:40 +0100 Subject: [PATCH] fixed imports --- Dockerfile | 25 ++- simple_vm_client/__init__.py | 1 - .../template/__init__.py} | 0 .../forc_connector/template/template.py | 6 +- .../__pycache__/__init__.cpython-38.pyc | Bin 191 -> 0 bytes .../openstack_connector.py | 183 +++++++++--------- simple_vm_client/test_openstack_connector.py | 121 ++++++------ 7 files changed, 177 insertions(+), 159 deletions(-) rename simple_vm_client/{scripts/__init__.py,cover => forc_connector/template/__init__.py} (100%) delete mode 100644 simple_vm_client/openstack_connector/__pycache__/__init__.cpython-38.pyc diff --git a/Dockerfile b/Dockerfile index a1b9141..85eb54e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,11 +1,24 @@ FROM python:3.11.4-buster -RUN apt-get update -y -RUN apt-get install -y build-essential + +RUN apt-get update -y \ + && apt-get install -y build-essential \ + && apt-get clean \ + && rm -rf /var/lib/apt/lists/* + WORKDIR /code -ADD requirements.txt /code + +# Copy requirements and install them first to leverage Docker cache +COPY requirements.txt /code RUN pip install -r requirements.txt -ADD requirements.yml /code -ADD ansible.cfg /etc/ansible/ + +COPY requirements.yml /code +COPY ansible.cfg /etc/ansible/ RUN ansible-galaxy install -r requirements.yml -ADD . /code + +# Copy the entire project +COPY . /code + +# Set PYTHONPATH to include the project root +ENV PYTHONPATH /code + WORKDIR /code/simple_vm_client diff --git a/simple_vm_client/__init__.py b/simple_vm_client/__init__.py index 86a570c..e69de29 100644 --- a/simple_vm_client/__init__.py +++ b/simple_vm_client/__init__.py @@ -1 +0,0 @@ -__all__ = ['ttypes', 'constants', 'VirtualMachineService'] diff --git a/simple_vm_client/scripts/__init__.py,cover b/simple_vm_client/forc_connector/template/__init__.py similarity index 100% rename from simple_vm_client/scripts/__init__.py,cover rename to simple_vm_client/forc_connector/template/__init__.py diff --git a/simple_vm_client/forc_connector/template/template.py b/simple_vm_client/forc_connector/template/template.py index 32d3adc..75d24de 100644 --- a/simple_vm_client/forc_connector/template/template.py +++ b/simple_vm_client/forc_connector/template/template.py @@ -7,8 +7,9 @@ import requests import yaml -from ttypes import ResearchEnvironmentTemplate -from util.logger import setup_custom_logger + +from simple_vm_client.ttypes import ResearchEnvironmentTemplate +from simple_vm_client.util.logger import setup_custom_logger # from resenv.backend.Backend import Backend @@ -116,7 +117,6 @@ def update_playbooks(self) -> None: for template_metadata in templates_metadata: try: if template_metadata.get(NEEDS_FORC_SUPPORT, False): - metadata = ResearchEnvironmentMetadata( name=template_metadata[TEMPLATE_NAME], port=template_metadata[PORT], diff --git a/simple_vm_client/openstack_connector/__pycache__/__init__.cpython-38.pyc b/simple_vm_client/openstack_connector/__pycache__/__init__.cpython-38.pyc deleted file mode 100644 index d8194106905d628ca6a2e3e81342e865ba24462a..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 191 zcmWIL<>g`kg5C=ZNg(<$h(HF6K#l_t7qb9~6oz01O-8?!3`HPe1o10HKO;XkRX-)M zEHg#FJijQrxF9h(RX;f=Gc~UyzRU>7$}dgPEyyn_NzBoO2 None: sys.exit(1) self.USE_APPLICATION_CREDENTIALS = ( - os.environ.get("USE_APPLICATION_CREDENTIALS", "False").lower() == "true" + os.environ.get("USE_APPLICATION_CREDENTIALS", "False").lower() == "true" ) if self.USE_APPLICATION_CREDENTIALS: @@ -180,15 +183,15 @@ def load_env_config(self) -> None: self.PROJECT_DOMAIN_ID = os.environ["OS_PROJECT_DOMAIN_ID"] def create_server( - self, - name: str, - image_id: str, - flavor_id: str, - network_id: str, - userdata: str, - key_name: str, - metadata: dict[str, str], - security_groups: list[str], + self, + name: str, + image_id: str, + flavor_id: str, + network_id: str, + userdata: str, + key_name: str, + metadata: dict[str, str], + security_groups: list[str], ) -> Server: logger.info( f"Create Server:\n\tname: {name}\n\timage_id:{image_id}\n\tflavor_id:{flavor_id}\n\tmetadata:{metadata}" @@ -230,7 +233,7 @@ def delete_volume(self, volume_id: str) -> None: raise DefaultException(message=e.message) def create_volume_snapshot( - self, volume_id: str, name: str, description: str + self, volume_id: str, name: str, description: str ) -> str: try: logger.info(f"Create Snapshot for Volume {volume_id}") @@ -273,7 +276,7 @@ def delete_volume_snapshot(self, snapshot_id: str) -> None: raise DefaultException(message=e.message) def create_volume_by_source_volume( - self, volume_name: str, metadata: dict[str, str], source_volume_id: str + self, volume_name: str, metadata: dict[str, str], source_volume_id: str ) -> Volume: logger.info(f"Creating volume from source volume with id {source_volume_id}") try: @@ -289,7 +292,7 @@ def create_volume_by_source_volume( raise ResourceNotAvailableException(message=e.message) def create_volume_by_volume_snap( - self, volume_name: str, metadata: dict[str, str], volume_snap_id: str + self, volume_name: str, metadata: dict[str, str], volume_snap_id: str ) -> Volume: logger.info(f"Creating volume from volume snapshot with id {volume_snap_id}") try: @@ -327,7 +330,7 @@ def get_servers_by_ids(self, ids: list[str]) -> list[Server]: return servers def attach_volume_to_server( - self, openstack_id: str, volume_id: str + self, openstack_id: str, volume_id: str ) -> dict[str, str]: server = self.get_server(openstack_id=openstack_id) volume = self.get_volume(name_or_id=volume_id) @@ -368,7 +371,7 @@ def resize_volume(self, volume_id: str, size: int) -> None: raise DefaultException(message=str(e)) def create_volume( - self, volume_name: str, volume_storage: int, metadata: dict[str, str] + self, volume_name: str, volume_storage: int, metadata: dict[str, str] ) -> Volume: logger.info(f"Creating volume with {volume_storage} GB storage") try: @@ -485,9 +488,9 @@ def get_active_image_by_os_version(self, os_version: str, os_distro: str) -> Ima image_os_distro = metadata.get("os_distro", None) base_image_ref = metadata.get("base_image_ref", None) if ( - os_version == image_os_version - and image.status == "active" - and base_image_ref is None + os_version == image_os_version + and image.status == "active" + and base_image_ref is None ): if os_distro and os_distro == image_os_distro: return image @@ -499,11 +502,11 @@ def get_active_image_by_os_version(self, os_version: str, os_distro: str) -> Ima ) def get_image( - self, - name_or_id: str, - replace_inactive: bool = False, - ignore_not_active: bool = False, - ignore_not_found: bool = False, + self, + name_or_id: str, + replace_inactive: bool = False, + ignore_not_active: bool = False, + ignore_not_found: bool = False, ) -> Image: logger.info(f"Get Image {name_or_id}") @@ -527,12 +530,12 @@ def get_image( return image def create_snapshot( - self, - openstack_id: str, - name: str, - username: str, - base_tags: list[str], - description: str, + self, + openstack_id: str, + name: str, + username: str, + base_tags: list[str], + description: str, ) -> str: logger.info( f"Create Snapshot from Instance {openstack_id} with name {name} for {username}" @@ -644,9 +647,9 @@ def get_gateway_ip(self) -> dict[str, str]: return {"gateway_ip": self.GATEWAY_IP} def create_mount_init_script( - self, - new_volumes: list[dict[str, str]] = None, # type: ignore - attach_volumes: list[dict[str, str]] = None, # type: ignore + self, + new_volumes: list[dict[str, str]] = None, # type: ignore + attach_volumes: list[dict[str, str]] = None, # type: ignore ) -> str: logger.info(f"Create init script for volume ids:{new_volumes}") if not new_volumes and not attach_volumes: @@ -727,7 +730,7 @@ def delete_security_group_rule(self, openstack_id): ) def open_port_range_for_vm_in_project( - self, range_start, range_stop, openstack_id, ethertype="IPV4", protocol="TCP" + self, range_start, range_stop, openstack_id, ethertype="IPV4", protocol="TCP" ): server: Server = self.openstack_connection.get_server_by_id(id=openstack_id) if server is None: @@ -776,13 +779,13 @@ def open_port_range_for_vm_in_project( raise OpenStackConflictException(message=e.message) def create_security_group( - self, - name: str, - udp_port: int = None, # type: ignore - ssh: bool = True, - udp: bool = False, - description: str = "", - research_environment_metadata: ResearchEnvironmentMetadata = None, + self, + name: str, + udp_port: int = None, # type: ignore + ssh: bool = True, + udp: bool = False, + description: str = "", + research_environment_metadata: ResearchEnvironmentMetadata = None, ) -> SecurityGroup: logger.info(f"Create new security group {name}") sec: SecurityGroup = self.openstack_connection.get_security_group( @@ -889,7 +892,7 @@ def is_security_group_in_use(self, security_group_id): return False def get_or_create_research_environment_security_group( - self, resenv_metadata: ResearchEnvironmentMetadata + self, resenv_metadata: ResearchEnvironmentMetadata ): if not resenv_metadata.needs_forc_support: return None @@ -1123,9 +1126,9 @@ def delete_server(self, openstack_id: str) -> None: server=server, security_group=sec ) if ( - sg["name"] != self.DEFAULT_SECURITY_GROUP_NAME - and ("bibigrid" not in sec.name or "master" not in server.name) - and not self.is_security_group_in_use(security_group_id=sec.id) + sg["name"] != self.DEFAULT_SECURITY_GROUP_NAME + and ("bibigrid" not in sec.name or "master" not in server.name) + and not self.is_security_group_in_use(security_group_id=sec.id) ): self.openstack_connection.delete_security_group(sg) self.openstack_connection.compute.delete_server(server.id, force=True) @@ -1162,12 +1165,11 @@ def get_vm_ports(self, openstack_id: str) -> dict[str, str]: return {"port": str(ssh_port), "udp": str(udp_port)} def create_userdata( - self, - volume_ids_path_new: list[dict[str, str]], - volume_ids_path_attach: list[dict[str, str]], - additional_keys: list[str], + self, + volume_ids_path_new: list[dict[str, str]], + volume_ids_path_attach: list[dict[str, str]], + additional_keys: list[str], ) -> str: - unlock_ubuntu_user_script = "#!/bin/bash\npasswd -u ubuntu\n" unlock_ubuntu_user_script_encoded = encodeutils.safe_encode( unlock_ubuntu_user_script.encode("utf-8") @@ -1177,9 +1179,9 @@ def create_userdata( if additional_keys: add_key_script = self.create_add_keys_script(keys=additional_keys) init_script = ( - add_key_script - + encodeutils.safe_encode("\n".encode("utf-8")) - + init_script + add_key_script + + encodeutils.safe_encode("\n".encode("utf-8")) + + init_script ) if volume_ids_path_new or volume_ids_path_attach: mount_script = self.create_mount_init_script( @@ -1187,26 +1189,25 @@ def create_userdata( attach_volumes=volume_ids_path_attach, ) init_script = ( - init_script - + encodeutils.safe_encode("\n".encode("utf-8")) - + mount_script - + init_script + + encodeutils.safe_encode("\n".encode("utf-8")) + + mount_script ) return init_script def start_server( - self, - flavor_name: str, - image_name: str, - servername: str, - metadata: dict[str, str], - public_key: str, - research_environment_metadata: Union[ResearchEnvironmentMetadata, None] = None, - volume_ids_path_new: Union[list[dict[str, str]], None] = None, - volume_ids_path_attach: Union[list[dict[str, str]], None] = None, - additional_keys: Union[list[str], None] = None, - additional_security_group_ids: Union[list[str], None] = None, + self, + flavor_name: str, + image_name: str, + servername: str, + metadata: dict[str, str], + public_key: str, + research_environment_metadata: Union[ResearchEnvironmentMetadata, None] = None, + volume_ids_path_new: Union[list[dict[str, str]], None] = None, + volume_ids_path_attach: Union[list[dict[str, str]], None] = None, + additional_keys: Union[list[str], None] = None, + additional_security_group_ids: Union[list[str], None] = None, ) -> str: logger.info(f"Start Server {servername}") @@ -1288,16 +1289,16 @@ def start_server( raise DefaultException(message=str(e)) def start_server_with_playbook( - self, - flavor_name: str, - image_name: str, - servername: str, - metadata: dict[str, str], - research_environment_metadata: ResearchEnvironmentMetadata, - volume_ids_path_new: list[dict[str, str]] = None, # type: ignore - volume_ids_path_attach: list[dict[str, str]] = None, # type: ignore - additional_keys: list[str] = None, # type: ignore - additional_security_group_ids=None, # type: ignore + self, + flavor_name: str, + image_name: str, + servername: str, + metadata: dict[str, str], + research_environment_metadata: ResearchEnvironmentMetadata, + volume_ids_path_new: list[dict[str, str]] = None, # type: ignore + volume_ids_path_attach: list[dict[str, str]] = None, # type: ignore + additional_keys: list[str] = None, # type: ignore + additional_security_group_ids=None, # type: ignore ) -> tuple[str, str]: logger.info(f"Start Server {servername}") @@ -1433,16 +1434,16 @@ def add_udp_security_group(self, server_id): return def add_cluster_machine( - self, - cluster_id: str, - cluster_user: str, - cluster_group_id: list[str], - image_name: str, - flavor_name: str, - name: str, - key_name: str, - batch_idx: int, - worker_idx: int, + self, + cluster_id: str, + cluster_user: str, + cluster_group_id: list[str], + image_name: str, + flavor_name: str, + name: str, + key_name: str, + batch_idx: int, + worker_idx: int, ) -> str: logger.info(f"Add machine to {cluster_id}") image: Image = self.get_image(name_or_id=image_name, replace_inactive=True) diff --git a/simple_vm_client/test_openstack_connector.py b/simple_vm_client/test_openstack_connector.py index c731cc4..a1af8f8 100644 --- a/simple_vm_client/test_openstack_connector.py +++ b/simple_vm_client/test_openstack_connector.py @@ -16,9 +16,10 @@ from openstack.image.v2 import image as image_module from openstack.network.v2.network import Network from openstack.test import fakes -from openstack_connector.openstack_connector import OpenStackConnector from oslo_utils import encodeutils -from ttypes import ( + +from .openstack_connector.openstack_connector import OpenStackConnector +from .ttypes import ( DefaultException, ImageNotFoundException, OpenStackConflictException, @@ -139,7 +140,7 @@ def test_load_config_yml(self): self.openstack_connector.FORC_SECURITY_GROUP_ID, "forc_security_group_id" ) - @patch("openstack_connector.openstack_connector.logger") + @patch("simple_vm_client.openstack_connector.openstack_connector.logger") def test_load_env_config_username_password(self, mock_logger): openstack_connector = self.init_openstack_connector() @@ -159,7 +160,7 @@ def test_load_env_config_username_password(self, mock_logger): # Assert that logger.info was called with the expected message mock_logger.info.assert_called_once_with("Load environment config: OpenStack") - @patch("openstack_connector.openstack_connector.logger") + @patch("simple_vm_client.openstack_connector.openstack_connector.logger") @patch.dict( os.environ, { @@ -191,7 +192,7 @@ def test_load_env_config_application_credentials(self, mock_logger): ] mock_logger.info.assert_has_calls(expected_calls, any_order=False) - @patch("openstack_connector.openstack_connector.logger") + @patch("simple_vm_client.openstack_connector.openstack_connector.logger") @patch.dict(os.environ, {"OS_AUTH_URL": ""}) def test_load_env_config_missing_os_auth_url(self, mock_logger): openstack_connector = self.init_openstack_connector() @@ -206,7 +207,7 @@ def test_load_env_config_missing_os_auth_url(self, mock_logger): # Assert that sys.exit was called with status code 1 mock_exit.assert_called_once_with(1) - @patch("openstack_connector.openstack_connector.logger") + @patch("simple_vm_client.openstack_connector.openstack_connector.logger") @patch.dict(os.environ, {"USE_APPLICATION_CREDENTIALS": "True"}) def test_load_env_config_missing_app_cred_vars(self, mock_logger): # Create an instance of OpenStackConnector @@ -224,7 +225,7 @@ def test_load_env_config_missing_app_cred_vars(self, mock_logger): # Assert that sys.exit was called with status code 1 mock_exit.assert_called_once_with(1) - @patch("openstack_connector.openstack_connector.logger") + @patch("simple_vm_client.openstack_connector.openstack_connector.logger") @patch.dict( os.environ, { @@ -274,14 +275,14 @@ def test_get_default_security_groups(self): default_security_groups, self.openstack_connector.DEFAULT_SECURITY_GROUPS ) - @patch("openstack_connector.openstack_connector.logger.info") + @patch("simple_vm_client.openstack_connector.openstack_connector.logger.info") def test_get_image(self, mock_logger_info): self.mock_openstack_connection.get_image.return_value = EXPECTED_IMAGE result = self.openstack_connector.get_image(EXPECTED_IMAGE.id) mock_logger_info.assert_called_once_with(f"Get Image {EXPECTED_IMAGE.id}") self.assertEqual(result, EXPECTED_IMAGE) - @patch("openstack_connector.openstack_connector.logger.info") + @patch("simple_vm_client.openstack_connector.openstack_connector.logger.info") def test_get_image_not_found_exception(self, mock_logger_info): # Configure the mock_openstack_connection.get_image to return None self.mock_openstack_connection.get_image.return_value = None @@ -295,7 +296,7 @@ def test_get_image_not_found_exception(self, mock_logger_info): # Assert that the exception contains the expected message and image ID self.assertEqual(context.exception.message, "Image nonexistent_id not found!") - @patch("openstack_connector.openstack_connector.logger.info") + @patch("simple_vm_client.openstack_connector.openstack_connector.logger.info") def test_get_image_not_active_exception(self, mock_logger_info): # Configure the mock_openstack_connection.get_image to return the not active image self.mock_openstack_connection.get_image.return_value = INACTIVE_IMAGE @@ -313,7 +314,7 @@ def test_get_image_not_active_exception(self, mock_logger_info): f"Image {INACTIVE_IMAGE.name} found but not active!", ) - @patch("openstack_connector.openstack_connector.logger.info") + @patch("simple_vm_client.openstack_connector.openstack_connector.logger.info") def test_get_images(self, mock_logger_info): # Configure the mock_openstack_connection.image.images to return the fake images self.mock_openstack_connection.image.images.return_value = IMAGES @@ -328,7 +329,7 @@ def test_get_images(self, mock_logger_info): # Assert that the method returns the expected result self.assertEqual(result, IMAGES) - @patch("openstack_connector.openstack_connector.logger.info") + @patch("simple_vm_client.openstack_connector.openstack_connector.logger.info") def test_get_active_image_by_os_version(self, mock_logger_info): # Generate a set of fake images with different properties os_version = "22.04" @@ -347,7 +348,7 @@ def test_get_active_image_by_os_version(self, mock_logger_info): # Assert that the method returns the expected image self.assertEqual(result, EXPECTED_IMAGE) - @patch("openstack_connector.openstack_connector.logger.info") + @patch("simple_vm_client.openstack_connector.openstack_connector.logger.info") def test_get_active_image_by_os_version_not_found_exception(self, mock_logger_info): # Configure the mock_openstack_connection.list_images to return an empty list self.mock_openstack_connection.list_images.return_value = [] @@ -393,8 +394,8 @@ def test_get_limits(self): self.mock_openstack_connection.get_volume_limits.return_value = volume_limits self.openstack_connector.get_limits() - @patch("openstack_connector.openstack_connector.logger.info") - @patch("openstack_connector.openstack_connector.logger.error") + @patch("simple_vm_client.openstack_connector.openstack_connector.logger.info") + @patch("simple_vm_client.openstack_connector.openstack_connector.logger.error") def test_create_server(self, mock_logger_error, mock_logger_info): # Prepare test data name = "test_server" @@ -443,8 +444,8 @@ def test_create_server(self, mock_logger_error, mock_logger_info): # Check if the method returns the fake server object self.assertEqual(result, fake_server) - @patch("openstack_connector.openstack_connector.logger.info") - @patch("openstack_connector.openstack_connector.logger.exception") + @patch("simple_vm_client.openstack_connector.openstack_connector.logger.info") + @patch("simple_vm_client.openstack_connector.openstack_connector.logger.exception") def test_get_volume(self, mock_logger_exception, mock_logger_info): # Prepare test data name_or_id = "test_volume_id" @@ -467,7 +468,7 @@ def test_get_volume(self, mock_logger_exception, mock_logger_info): # Check if the method returns the fake volume object self.assertEqual(result, fake_volume) - @patch("openstack_connector.openstack_connector.logger.exception") + @patch("simple_vm_client.openstack_connector.openstack_connector.logger.exception") def test_get_volume_exception(self, mock_logger_exception): # Prepare test data name_or_id = "non_existing_volume_id" @@ -484,8 +485,8 @@ def test_get_volume_exception(self, mock_logger_exception): # Check if the method logs the correct exception information mock_logger_exception.assert_called_once_with(f"No Volume with id {name_or_id}") - @patch("openstack_connector.openstack_connector.logger.info") - @patch("openstack_connector.openstack_connector.logger.exception") + @patch("simple_vm_client.openstack_connector.openstack_connector.logger.info") + @patch("simple_vm_client.openstack_connector.openstack_connector.logger.exception") def test_delete_volume(self, mock_logger_exception, mock_logger_info): # Prepare test data volume_id = "test_volume_id" @@ -530,8 +531,8 @@ def test_delete_volume(self, mock_logger_exception, mock_logger_info): ): # Replace Exception with the actual exception type self.openstack_connector.delete_volume(volume_id) - @patch("openstack_connector.openstack_connector.logger.info") - @patch("openstack_connector.openstack_connector.logger.error") + @patch("simple_vm_client.openstack_connector.openstack_connector.logger.info") + @patch("simple_vm_client.openstack_connector.openstack_connector.logger.error") def test_create_volume_snapshot(self, mock_logger_error, mock_logger_info): # Prepare test data volume_id = "test_volume_id" @@ -572,8 +573,8 @@ def test_create_volume_snapshot(self, mock_logger_error, mock_logger_info): volume_id, snapshot_name, snapshot_description ) - @patch("openstack_connector.openstack_connector.logger.info") - @patch("openstack_connector.openstack_connector.logger.exception") + @patch("simple_vm_client.openstack_connector.openstack_connector.logger.info") + @patch("simple_vm_client.openstack_connector.openstack_connector.logger.exception") def test_get_volume_snapshot(self, mock_logger_exception, mock_logger_info): # Prepare test data snapshot_id = "test_snapshot_id" @@ -597,8 +598,8 @@ def test_get_volume_snapshot(self, mock_logger_exception, mock_logger_info): f"No volume Snapshot with id {snapshot_id}" ) - @patch("openstack_connector.openstack_connector.logger.info") - @patch("openstack_connector.openstack_connector.logger.exception") + @patch("simple_vm_client.openstack_connector.openstack_connector.logger.info") + @patch("simple_vm_client.openstack_connector.openstack_connector.logger.exception") def test_delete_volume_snapshot(self, mock_logger_exception, mock_logger_info): # Prepare test data snapshot_id = "test_snapshot_id" @@ -646,7 +647,7 @@ def test_delete_volume_snapshot(self, mock_logger_exception, mock_logger_info): ): # Replace Exception with the actual exception type self.openstack_connector.delete_volume_snapshot(snapshot_id) - @patch("openstack_connector.openstack_connector.logger.info") + @patch("simple_vm_client.openstack_connector.openstack_connector.logger.info") def test_get_servers(self, mock_logger_info): # Prepare test data expected_servers = fakes.generate_fake_resources(server.Server, count=3) @@ -661,9 +662,9 @@ def test_get_servers(self, mock_logger_info): self.assertEqual(result_servers, expected_servers) mock_logger_info.assert_called_once_with("Get servers") - @patch("openstack_connector.openstack_connector.logger.error") - @patch("openstack_connector.openstack_connector.logger.exception") - @patch("openstack_connector.openstack_connector.logger.info") + @patch("simple_vm_client.openstack_connector.openstack_connector.logger.error") + @patch("simple_vm_client.openstack_connector.openstack_connector.logger.exception") + @patch("simple_vm_client.openstack_connector.openstack_connector.logger.info") def test_get_servers_by_ids( self, mock_logger_info, mock_logger_exception, mock_logger_error ): @@ -692,8 +693,8 @@ def test_get_servers_by_ids( mock_logger_error.assert_called_once_with("Requested VM id3 not found!") mock_logger_exception.assert_called_once_with("Requested VM id4 not found!\n ") - @patch("openstack_connector.openstack_connector.logger.exception") - @patch("openstack_connector.openstack_connector.logger.info") + @patch("simple_vm_client.openstack_connector.openstack_connector.logger.exception") + @patch("simple_vm_client.openstack_connector.openstack_connector.logger.info") def test_attach_volume_to_server(self, mock_logger_info, mock_logger_exception): # Prepare test data expected_attachment = { @@ -744,8 +745,8 @@ def test_attach_volume_to_server(self, mock_logger_info, mock_logger_exception): exc_info=True, ) - @patch("openstack_connector.openstack_connector.logger.exception") - @patch("openstack_connector.openstack_connector.logger.info") + @patch("simple_vm_client.openstack_connector.openstack_connector.logger.exception") + @patch("simple_vm_client.openstack_connector.openstack_connector.logger.info") def test_detach_volume(self, mock_logger_info, mock_logger_exception): # Prepare test data expected_server = fakes.generate_fake_resource(server.Server) @@ -787,8 +788,8 @@ def test_detach_volume(self, mock_logger_info, mock_logger_exception): f"Delete volume attachment (server: {server_id} volume: {volume_id}) failed!" ) - @patch("openstack_connector.openstack_connector.logger.exception") - @patch("openstack_connector.openstack_connector.logger.info") + @patch("simple_vm_client.openstack_connector.openstack_connector.logger.exception") + @patch("simple_vm_client.openstack_connector.openstack_connector.logger.info") def test_resize_volume(self, mock_logger_info, mock_logger_exception): # Prepare test data expected_volume = fakes.generate_fake_resource(volume.Volume) @@ -818,8 +819,8 @@ def test_resize_volume(self, mock_logger_info, mock_logger_exception): with self.assertRaises(DefaultException): self.openstack_connector.resize_volume(volume_id, size) - @patch("openstack_connector.openstack_connector.logger.exception") - @patch("openstack_connector.openstack_connector.logger.info") + @patch("simple_vm_client.openstack_connector.openstack_connector.logger.exception") + @patch("simple_vm_client.openstack_connector.openstack_connector.logger.info") def test_create_volume(self, mock_logger_info, mock_logger_exception): # Prepare test data volume_name = "test_volume" @@ -853,7 +854,7 @@ def test_create_volume(self, mock_logger_info, mock_logger_exception): f"Trying to create volume with {volume_storage} GB failed", exc_info=True ) - @patch("openstack_connector.openstack_connector.logger.exception") + @patch("simple_vm_client.openstack_connector.openstack_connector.logger.exception") def test_get_network(self, mock_logger_exception): with tempfile.NamedTemporaryFile(mode="w+", delete=False) as temp_file: temp_file.write(CONFIG_DATA) @@ -877,8 +878,8 @@ def test_get_network(self, mock_logger_exception): ) mock_logger_exception.assert_not_called() # Ensure no exception is logged - @patch("openstack_connector.openstack_connector.logger.exception") - @patch("openstack_connector.openstack_connector.logger.info") + @patch("simple_vm_client.openstack_connector.openstack_connector.logger.exception") + @patch("simple_vm_client.openstack_connector.openstack_connector.logger.info") def test_import_existing_keypair(self, mock_logger_info, mock_logger_exception): # Mock the get_keypair method for existing keypair existing_keypair = fakes.generate_fake_resource(keypair.Keypair) @@ -900,8 +901,8 @@ def test_import_existing_keypair(self, mock_logger_info, mock_logger_exception): self.mock_openstack_connection.delete_keypair.assert_not_called() mock_logger_exception.assert_not_called() - @patch("openstack_connector.openstack_connector.logger.exception") - @patch("openstack_connector.openstack_connector.logger.info") + @patch("simple_vm_client.openstack_connector.openstack_connector.logger.exception") + @patch("simple_vm_client.openstack_connector.openstack_connector.logger.info") def test_import_non_existing_keypair(self, mock_logger_info, mock_logger_exception): # Mock the get_keypair method for non-existing keypair new_keypair = fakes.generate_fake_resource(keypair.Keypair) @@ -927,8 +928,8 @@ def test_import_non_existing_keypair(self, mock_logger_info, mock_logger_excepti self.mock_openstack_connection.delete_keypair.assert_not_called() mock_logger_exception.assert_not_called() - @patch("openstack_connector.openstack_connector.logger.exception") - @patch("openstack_connector.openstack_connector.logger.info") + @patch("simple_vm_client.openstack_connector.openstack_connector.logger.exception") + @patch("simple_vm_client.openstack_connector.openstack_connector.logger.info") def test_import_changed_keypair(self, mock_logger_info, mock_logger_exception): # Mock the get_keypair method for keypair with changed public_key changed_keypair = fakes.generate_fake_resource(keypair.Keypair) @@ -960,8 +961,8 @@ def test_import_changed_keypair(self, mock_logger_info, mock_logger_exception): ) mock_logger_exception.assert_not_called() - @patch("openstack_connector.openstack_connector.logger.exception") - @patch("openstack_connector.openstack_connector.logger.info") + @patch("simple_vm_client.openstack_connector.openstack_connector.logger.exception") + @patch("simple_vm_client.openstack_connector.openstack_connector.logger.info") def test_import_same_keypair(self, mock_logger_info, mock_logger_exception): # Mock the get_keypair method for keypair with same public_key same_keypair = fakes.generate_fake_resource(keypair.Keypair) @@ -983,7 +984,7 @@ def test_import_same_keypair(self, mock_logger_info, mock_logger_exception): mock_logger_info.assert_called_with(f"Get Keypair {same_keypair.name}") mock_logger_exception.assert_not_called() - @patch("openstack_connector.openstack_connector.logger.info") + @patch("simple_vm_client.openstack_connector.openstack_connector.logger.info") def test_create_add_keys_script(self, mock_logger_info): # Prepare test data keys = ["key1", "key2", "key3"] @@ -1003,8 +1004,8 @@ def test_create_add_keys_script(self, mock_logger_info): # Check that the real script content matches the expected content self.assertEqual(result_script, expected_script_content) - @patch("openstack_connector.openstack_connector.socket.socket") - @patch("openstack_connector.openstack_connector.logger.info") + @patch("simple_vm_client.openstack_connector.openstack_connector.socket.socket") + @patch("simple_vm_client.openstack_connector.openstack_connector.logger.info") def test_netcat(self, mock_logger_info, mock_socket): # Replace with the actual host and port host = "example.com" @@ -1026,7 +1027,7 @@ def test_netcat(self, mock_logger_info, mock_socket): f"Checking SSH Connection {host}:{port} Result = 0" ) - @patch("openstack_connector.openstack_connector.logger.info") + @patch("simple_vm_client.openstack_connector.openstack_connector.logger.info") def test_get_flavor(self, mock_logger_info): # Replace with the actual flavor name or ID expected_flavor = fakes.generate_fake_resource(flavor.Flavor) @@ -1044,7 +1045,7 @@ def test_get_flavor(self, mock_logger_info): name_or_id=expected_flavor.name, get_extra=True ) - @mock.patch("openstack_connector.openstack_connector.logger.info") + @mock.patch("simple_vm_client.openstack_connector.openstack_connector.logger.info") def test_get_flavors(self, mock_logger_info): # Replace with the actual flavors you want to simulate expected_flavors = list(fakes.generate_fake_resources(flavor.Flavor, count=3)) @@ -1064,7 +1065,7 @@ def test_get_flavors(self, mock_logger_info): get_extra=True ) - @mock.patch("openstack_connector.openstack_connector.logger.info") + @mock.patch("simple_vm_client.openstack_connector.openstack_connector.logger.info") def test_get_servers_by_bibigrid_id(self, mock_logger_info): # Replace with the actual Bibigrid ID you want to test bibigrid_id = "your_bibigrid_id" @@ -1089,8 +1090,10 @@ def test_get_servers_by_bibigrid_id(self, mock_logger_info): filters={"bibigrid_id": bibigrid_id, "name": bibigrid_id} ) - @mock.patch("openstack_connector.openstack_connector.logger.exception") - @mock.patch("openstack_connector.openstack_connector.logger.info") + @mock.patch( + "simple_vm_client.openstack_connector.openstack_connector.logger.exception" + ) + @mock.patch("simple_vm_client.openstack_connector.openstack_connector.logger.info") def test_create_snapshot(self, mock_logger_info, mock_logger_exception): # Replace with the actual parameters you want to test openstack_id = "your_openstack_id" @@ -1131,8 +1134,10 @@ def test_create_snapshot(self, mock_logger_info, mock_logger_exception): openstack_id, name, username, base_tags, description ) - @mock.patch("openstack_connector.openstack_connector.logger.exception") - @mock.patch("openstack_connector.openstack_connector.logger.info") + @mock.patch( + "simple_vm_client.openstack_connector.openstack_connector.logger.exception" + ) + @mock.patch("simple_vm_client.openstack_connector.openstack_connector.logger.info") def test_delete_image(self, mock_logger_info, mock_logger_exception): # Replace with the actual image_id you want to test fake_image = fakes.generate_fake_resource(image.Image) @@ -1156,7 +1161,7 @@ def test_delete_image(self, mock_logger_info, mock_logger_exception): f"Delete Image {fake_image.id} failed!" ) - @mock.patch("openstack_connector.openstack_connector.logger.info") + @mock.patch("simple_vm_client.openstack_connector.openstack_connector.logger.info") def test_get_public_images(self, mock_logger_info): # Replace with the actual public images you want to test images = list(fakes.generate_fake_resources(image.Image, count=3))