Skip to content

Commit

Permalink
Merge pull request #547 from deNBI/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
dweinholz authored Jul 18, 2024
2 parents 948821e + f76cfad commit aa1e826
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 16 deletions.
5 changes: 3 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
gevent
pytest
17 changes: 12 additions & 5 deletions simple_vm_client/bibigrid_connector/test_bibigrid_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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")
Expand Down
13 changes: 11 additions & 2 deletions simple_vm_client/openstack_connector/openstack_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
18 changes: 13 additions & 5 deletions simple_vm_client/test_openstack_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -86,13 +86,17 @@
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"],
)
INACTIVE_IMAGE = image_module.Image(
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"],
)
Expand All @@ -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"],
),
Expand All @@ -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"],
),
Expand Down Expand Up @@ -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",
Expand All @@ -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(
Expand All @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions simple_vm_client/test_virtualmachinehandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit aa1e826

Please sign in to comment.