Skip to content

Commit

Permalink
Merge pull request #652 from deNBI/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
dweinholz authored Dec 11, 2024
2 parents 7c9910e + 32a2aba commit 63a6adc
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 52 deletions.
4 changes: 2 additions & 2 deletions simple_vm_client/forc_connector/playbook/playbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def copy_and_init_research_environment(self) -> None:
return

shutil.copytree(
f"{Template.get_playbook_resenvs_dir()}/{self.research_environment_template}",
f"{Template.get_playbook_resenvs_dir()}{self.research_environment_template}",
self.directory.name,
dirs_exist_ok=True,
)
Expand Down Expand Up @@ -232,7 +232,7 @@ def copy_and_init_conda_packages(self) -> None:
return

shutil.copytree(
f"{Template.get_playbook_resenvs_dir()}/{CONDA}",
f"{Template.get_playbook_resenvs_dir()}{CONDA}",
self.directory.name,
dirs_exist_ok=True,
)
Expand Down
6 changes: 4 additions & 2 deletions simple_vm_client/forc_connector/playbook/test_playbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,9 @@ def test_copy_and_init_conda_packages(self, mock_isfile, mock_open, mock_copytre

# Assert
mock_copytree.assert_called_once_with(
f"{instance.playbooks_dir}/{CONDA}", mock_tempdir.name, dirs_exist_ok=True
f"{instance.playbooks_dir}resenvs/{CONDA}",
mock_tempdir.name,
dirs_exist_ok=True,
)

mock_open.assert_any_call(
Expand Down Expand Up @@ -556,7 +558,7 @@ def test_copy_and_init_research_environment(

# Assert
mock_copytree.assert_called_once_with(
f"{instance.playbooks_dir}/template_name",
f"{instance.playbooks_dir}resenvs/template_name",
instance.directory.name,
dirs_exist_ok=True,
)
Expand Down
28 changes: 15 additions & 13 deletions simple_vm_client/forc_connector/template/test_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,13 @@ def init_template(

return template

@patch("tempfile.mkstemp")
@patch("requests.get")
@patch("zipfile.ZipFile")
@patch("builtins.open", create=True)
@patch("simple_vm_client.forc_connector.template.template.logger.info")
def test_download_and_extract_playbooks(
self, mock_logger_info, mock_open, mock_zipfile, mock_requests
self, mock_logger_info, mock_open, mock_zipfile, mock_requests, mock_tempfile
):
template = self.init_template(
github_playbook_repo=TestTemplate.GITHUB_REPO_STAGING,
Expand All @@ -140,6 +141,7 @@ def test_download_and_extract_playbooks(
mock_response = Mock()
mock_response.content = b"Mock content"
mock_requests.return_value = mock_response
mock_tempfile.return_value = ["", FILENAME]

# Call the method to test
template._download_and_extract_playbooks()
Expand Down Expand Up @@ -182,7 +184,7 @@ def test_copy_resenvs_templates(
mock_glob.return_value = ["/path/to/directory/resenvs"]

def mock_glob_side_effect(pattern):
if pattern == Template.get_playbook_dir() + "*":
if pattern == Template.get_playbook_resenvs_dir() + "*":
return ["/path/to/directory/resenvs"]
else:
return []
Expand All @@ -193,12 +195,12 @@ def mock_glob_side_effect(pattern):
template._copy_resenvs_templates()

# Assert that glob.glob was called with the correct parameters
mock_glob.assert_called_once_with(Template.get_playbook_dir() + "*")
mock_glob.assert_called_once_with(Template.get_playbook_resenvs_dir() + "*")

# Assert that shutil.copytree was called with the correct parameters
mock_copytree.assert_called_once_with(
"/path/to/directory/resenvs",
Template.get_playbook_dir(),
Template.get_playbook_resenvs_dir(),
dirs_exist_ok=True,
)

Expand Down Expand Up @@ -233,33 +235,33 @@ def test_update_loaded_templates(self, mock_isdir, mock_listdir):
template._update_loaded_templates()

# Assert that os.listdir was called with the correct parameters
mock_listdir.assert_called_once_with(Template.get_playbook_dir())
mock_listdir.assert_called_once_with(Template.get_playbook_resenvs_dir())

# Assert that os.path.isdir was called for each template
mock_isdir.assert_any_call(
os.path.join(Template.get_playbook_dir(), "template1")
os.path.join(Template.get_playbook_resenvs_dir(), "template1")
)
mock_isdir.assert_any_call(
os.path.join(Template.get_playbook_dir(), "template2")
os.path.join(Template.get_playbook_resenvs_dir(), "template2")
)
mock_isdir.assert_any_call(
os.path.join(Template.get_playbook_dir(), "non_template")
os.path.join(Template.get_playbook_resenvs_dir(), "non_template")
)
with pytest.raises(AssertionError):
mock_isdir.assert_called_with(
os.path.join(Template.get_playbook_dir(), "packer")
os.path.join(Template.get_playbook_resenvs_dir(), "packer")
)
with pytest.raises(AssertionError):
mock_isdir.assert_called_with(
os.path.join(Template.get_playbook_dir(), ".github")
os.path.join(Template.get_playbook_resenvs_dir(), ".github")
)
with pytest.raises(AssertionError):
mock_isdir.assert_called_with(
os.path.join(Template.get_playbook_dir(), "cluster")
os.path.join(Template.get_playbook_resenvs_dir(), "cluster")
)
with pytest.raises(AssertionError):
mock_isdir.assert_called_with(
os.path.join(Template.get_playbook_dir(), "optional")
os.path.join(Template.get_playbook_resenvs_dir(), "optional")
)

# Assert that the _all_templates attribute is updated correctly
Expand Down Expand Up @@ -294,7 +296,7 @@ def test_install_ansible_galaxy_requirements(self, mock_logger_info, mock_os_pop
# Assertions
mock_logger_info.assert_any_call("Installing Ansible galaxy requirements..")
mock_os_popen.assert_called_with(
f"ansible-galaxy install -r {Template.get_playbook_dir()}/packer/requirements.yml"
f"ansible-galaxy install -r {Template.get_playbook_resenvs_dir()}/packer/requirements.yml"
)
mock_os_popen_instance.read.assert_called_once()
mock_logger_info.assert_any_call("Mocked output")
Expand Down
36 changes: 20 additions & 16 deletions simple_vm_client/forc_connector/test_forc_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ def test_get_playbook_status(self):
fake_server = fakes.generate_fake_resource(Server)
fake_server.task_state = None
fake_playbook = MagicMock()
self.forc_connector._active_playbooks[fake_server.id] = fake_playbook
ForcConnector.active_playbooks[fake_server.id] = fake_playbook
self.forc_connector.redis_connection.exists.return_value = 1
self.forc_connector.redis_connection.hget.return_value = (
VmTaskStates.PREPARE_PLAYBOOK_BUILD.value.encode("utf-8")
Expand Down Expand Up @@ -428,24 +428,28 @@ def test_create_and_deploy_playbook(self, mock_playbook):
playbook_mock = MagicMock()
mock_playbook.return_value = playbook_mock

self.forc_connector.redis_connection.hget.return_value = key.encode("utf-8")
res = self.forc_connector.create_and_deploy_playbook(
public_key=key,
research_environment_template="vscode",
create_only_backend=False,
conda_packages=[],
apt_packages=[],
openstack_id=openstack_id,
port=80,
ip="192.168.0.1",
cloud_site="Bielefeld",
base_url="base_url",
)
with patch.object(
self.forc_connector.template, "is_update_locked", return_value=False
) as mock_is_update_locked:

self.forc_connector.redis_connection.hget.return_value = key.encode("utf-8")
res = self.forc_connector.create_and_deploy_playbook(
public_key=key,
research_environment_template="vscode",
create_only_backend=False,
conda_packages=[],
apt_packages=[],
openstack_id=openstack_id,
port=80,
ip="192.168.0.1",
cloud_site="Bielefeld",
base_url="base_url",
)
self.forc_connector.redis_connection.hset.assert_called_once_with(
openstack_id, "status", VmTaskStates.BUILD_PLAYBOOK.value
)
self.assertEqual(res, 0)
active_play = self.forc_connector._active_playbooks[openstack_id]
active_play = ForcConnector.active_playbooks[openstack_id]
self.assertEqual(active_play, playbook_mock)

@patch("simple_vm_client.forc_connector.forc_connector.requests.post")
Expand Down Expand Up @@ -617,7 +621,7 @@ def test_get_playbook_logs(self):
self.forc_connector.redis_connection.exists.return_value = 1
playbook_mock = MagicMock()
playbook_mock.get_logs.return_value = "status", "stdout", "stderr"
self.forc_connector._active_playbooks = {openstack_id: playbook_mock}
ForcConnector.active_playbooks = {openstack_id: playbook_mock}
self.forc_connector.get_playbook_logs(openstack_id=openstack_id)
self.forc_connector.redis_connection.exists.assert_called_once_with(
openstack_id
Expand Down
24 changes: 5 additions & 19 deletions simple_vm_client/test_openstack_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
from openstack.network.v2 import security_group, security_group_rule
from openstack.network.v2.network import Network
from openstack.test import fakes
from oslo_utils import encodeutils

from simple_vm_client.forc_connector.template.template import (
ResearchEnvironmentMetadata,
Expand Down Expand Up @@ -1107,22 +1106,12 @@ def test_create_add_keys_script(self, mock_logger_info):
result_script = self.openstack_connector.create_add_keys_script(keys)

# Assertions
expected_script_content = (
'#!/bin/bash\ndeclare -a keys_to_add=("key1" "key2" "key3" )'
'\necho "Found keys: ${#keys_to_add[*]}"\nfor ix in ${!keys_to_add[*]}'
'\ndo\n printf "\\n%s" "${keys_to_add[$ix]}" >> /home/ubuntu/.ssh/authorized_keys'
"\n\ndone\n"
)
expected_script_content = encodeutils.safe_encode(
expected_script_content.encode("utf-8")
)
for key in keys:
self.assertIn(key.encode("utf-8"), result_script) # Encode the key to bytes

# Additional assertions
mock_logger_info.assert_called_once_with("create add key script")

# Check that the real script content matches the expected content
self.assertEqual(result_script, expected_script_content)

@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):
Expand Down Expand Up @@ -2324,8 +2313,6 @@ def test_get_server_openstack_exception(self):
with self.assertRaises(DefaultException):
self.openstack_connector.get_server("someid")



@patch.object(OpenStackConnector, "get_server")
def test_rescue_server_success(self, mock_get_server):
# Arrange
Expand All @@ -2348,10 +2335,10 @@ def test_rescue_server_conflict_exception(
# Arrange
server_mock = fakes.generate_fake_resource(server.Server)
mock_get_server.return_value = server_mock
self.openstack_connector.openstack_connection.compute.rescue_server.side_effect = ConflictException
(
"Unit Test"
self.openstack_connector.openstack_connection.compute.rescue_server.side_effect = (
ConflictException
)
("Unit Test")
# Act
with self.assertRaises(OpenStackConflictException):
self.openstack_connector.rescue_server(openstack_id="some_openstack_id")
Expand Down Expand Up @@ -2390,7 +2377,6 @@ def test_unrescue_server_conflict_exception(
mock_logger_exception.assert_called_once_with(
"Unrescue Server some_openstack_id failed!"
)


@patch.object(OpenStackConnector, "get_server")
def test_set_server_metadata_success(self, mock_get_server):
Expand Down

0 comments on commit 63a6adc

Please sign in to comment.