Skip to content

Commit

Permalink
updated unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dweinholz committed Dec 11, 2024
1 parent fff6859 commit 475bd27
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 33 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
22 changes: 12 additions & 10 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 @@ -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
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 475bd27

Please sign in to comment.