diff --git a/requirements.txt b/requirements.txt index ab0cdd10..d3435128 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,7 +1,7 @@ setuptools==69.5.1 thrift==0.20.0 python-keystoneclient==5.4.0 -openstacksdk==3.3.0 +openstacksdk==3.1.0 deprecated==1.2.14 Click==8.1.7 ansible==9.5.1 @@ -17,4 +17,5 @@ types-PyYAML==6.0.12.20240311 sympy==1.13.0 colorama==0.4.6 types-redis==4.6.0.20240425 -gevent \ No newline at end of file +gevent +pytest diff --git a/simple_vm_client/bibigrid_connector/test_bibigrid_connector.py b/simple_vm_client/bibigrid_connector/test_bibigrid_connector.py index acb22681..35177498 100644 --- a/simple_vm_client/bibigrid_connector/test_bibigrid_connector.py +++ b/simple_vm_client/bibigrid_connector/test_bibigrid_connector.py @@ -6,7 +6,11 @@ import requests from simple_vm_client.bibigrid_connector.bibigrid_connector import BibigridConnector -from simple_vm_client.ttypes import ClusterInfo, ClusterInstance +from simple_vm_client.ttypes import ( + ClusterInfo, + ClusterInstance, + ClusterNotFoundException, +) HOST = "example.com" PORT = 8080 @@ -200,7 +204,7 @@ def test_start_cluster(self, mock_logger_info, mock_post): # Call the method to test result = self.connector.start_cluster( - public_key=public_key, + public_keys=[public_key], master_instance=DEFAULT_MASTER_INSTANCE, worker_instances=DEFAULT_WORKER_INSTANCES, user=user, @@ -271,13 +275,16 @@ def test_get_clusters_info(self, mock_logger_info, mock_get): self.assertEqual(result, [{"cluster-id": "fake_cluster_id"}]) mock_logger_info.assert_called_once_with("Get clusters info") - @patch.object(BibigridConnector, "get_clusters_info") + @patch( + "simple_vm_client.bibigrid_connector.bibigrid_connector.BibigridConnector.get_clusters_info" + ) @patch("simple_vm_client.bibigrid_connector.bibigrid_connector.logger.info") def test_get_cluster_info_none(self, mock_logger_info, mock_get_clusters_info): mock_get_clusters_info.return_value = [] - result = self.connector.get_cluster_info("fake_cluster_id") + with self.assertRaises(ClusterNotFoundException): + self.connector.get_cluster_info("fake_cluster_id") + mock_logger_info.assert_any_call("Get Cluster info from fake_cluster_id") - self.assertIsNone(result) @patch.object(BibigridConnector, "get_clusters_info") @patch("simple_vm_client.bibigrid_connector.bibigrid_connector.logger.info") diff --git a/simple_vm_client/openstack_connector/openstack_connector.py b/simple_vm_client/openstack_connector/openstack_connector.py index 12abb040..6b6895a6 100644 --- a/simple_vm_client/openstack_connector/openstack_connector.py +++ b/simple_vm_client/openstack_connector/openstack_connector.py @@ -520,18 +520,27 @@ def get_servers_by_bibigrid_id(self, bibigrid_id: str) -> list[Server]: servers: list[Server] = self.openstack_connection.list_servers(filters=filters) return servers - def get_active_image_by_os_version(self, os_version: str, os_distro: str) -> Image: + def get_active_image_by_os_version( + self, os_version: str, os_distro: Union[str, None] + ) -> Image: logger.info(f"Get active Image by os-version: {os_version}") images = self.openstack_connection.list_images() for image in images: + image_os_version = image.get("os_version", None) image_os_distro = image.get("os_distro", None) - base_image_ref = image.get("properties", {}).get("base_image_ref", None) + image_properties = image.get("properties", None) + if image_properties: + + base_image_ref = image_properties.get("base_image_ref", None) + else: + base_image_ref = None if ( 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 elif os_distro is None: diff --git a/simple_vm_client/test_openstack_connector.py b/simple_vm_client/test_openstack_connector.py index 69a5ea29..8e9dbe51 100644 --- a/simple_vm_client/test_openstack_connector.py +++ b/simple_vm_client/test_openstack_connector.py @@ -4,7 +4,7 @@ import tempfile import unittest from unittest import mock -from unittest.mock import MagicMock, call, patch +from unittest.mock import ANY, MagicMock, call, patch from openstack.block_storage.v3 import volume from openstack.block_storage.v3.limits import Limit @@ -86,6 +86,8 @@ id="image_id_2", status="active", name="image_2", + os_version="22.04", + os_distro="ubuntu", metadata={"os_version": "22.04", "os_distro": "ubuntu"}, tags=["portalclient"], ) @@ -93,6 +95,8 @@ id="image_inactive", status="building", name="image_inactive", + os_version="22.04", + os_distro="ubuntu", metadata={"os_version": "22.04", "os_distro": "ubuntu"}, tags=["portalclient"], ) @@ -102,6 +106,8 @@ id="image_id_1", status="inactive", name="image_1", + os_version="22.04", + os_distro="ubuntu", metadata={"os_version": "22.04", "os_distro": "ubuntu"}, tags=["portalclient"], ), @@ -110,6 +116,8 @@ id="image_id_3", status="active", name="image_3", + os_version="22.04", + os_distro="ubuntu", metadata={"os_version": "22.04", "os_distro": "centos"}, tags=["portalclient"], ), @@ -1837,7 +1845,7 @@ def test_start_server( image=fake_image.id, flavor=fake_flavor.id, network=[fake_network.id], - key_name=server_keypair.name, + key_name=ANY, meta=metadata, volumes=["volume1", "volume2"], userdata="userdata", @@ -1858,7 +1866,7 @@ def test_start_server( ) self.openstack_connector.openstack_connection.create_keypair.assert_called_once_with( - name=server_keypair.name, public_key=public_key + name=ANY, public_key=public_key ) mock_get_volumes.assert_called_once_with( @@ -1867,9 +1875,9 @@ def test_start_server( ) self.openstack_connector.openstack_connection.get_keypair.assert_called_once_with( - name_or_id=server_keypair.name + name_or_id=ANY ) - mock_delete_keypair.assert_any_call(key_name=server_keypair.name) + mock_delete_keypair.assert_any_call(key_name=ANY) # Check the result self.assertEqual(result, server.id) diff --git a/simple_vm_client/test_virtualmachinehandler.py b/simple_vm_client/test_virtualmachinehandler.py index 060b23e4..dc10ddf3 100644 --- a/simple_vm_client/test_virtualmachinehandler.py +++ b/simple_vm_client/test_virtualmachinehandler.py @@ -452,6 +452,7 @@ def test_start_server_with_res(self): additional_keys=[], research_environment_metadata="res_metadata", additional_security_group_ids=[], + slurm_version=None, ) def test_start_server(self): @@ -478,6 +479,7 @@ def test_start_server(self): additional_keys=[], research_environment_metadata=None, additional_security_group_ids=[], + slurm_version=None, ) def test_start_server_with_custom_key(self): @@ -598,13 +600,13 @@ def test_start_cluster(self): master = MagicMock() worker_instances = [MagicMock()] self.handler.start_cluster( - public_key="pub", + public_keys=["pub"], master_instance=master, worker_instances=worker_instances, user=USERNAME, ) self.handler.bibigrid_connector.start_cluster.assert_called_once_with( - public_key="pub", + public_keys=["pub"], master_instance=master, worker_instances=worker_instances, user=USERNAME,