From 475bd27b36419e6c4ccb416d0ed7497893b77343 Mon Sep 17 00:00:00 2001 From: dweinholz Date: Wed, 11 Dec 2024 10:09:06 +0100 Subject: [PATCH] updated unit tests --- .../forc_connector/playbook/playbook.py | 4 ++-- .../forc_connector/playbook/test_playbook.py | 6 +++-- .../forc_connector/template/test_templates.py | 22 +++++++++-------- simple_vm_client/test_openstack_connector.py | 24 ++++--------------- 4 files changed, 23 insertions(+), 33 deletions(-) diff --git a/simple_vm_client/forc_connector/playbook/playbook.py b/simple_vm_client/forc_connector/playbook/playbook.py index caf9838..be27815 100644 --- a/simple_vm_client/forc_connector/playbook/playbook.py +++ b/simple_vm_client/forc_connector/playbook/playbook.py @@ -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, ) @@ -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, ) diff --git a/simple_vm_client/forc_connector/playbook/test_playbook.py b/simple_vm_client/forc_connector/playbook/test_playbook.py index 77135b2..434f5f4 100644 --- a/simple_vm_client/forc_connector/playbook/test_playbook.py +++ b/simple_vm_client/forc_connector/playbook/test_playbook.py @@ -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( @@ -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, ) diff --git a/simple_vm_client/forc_connector/template/test_templates.py b/simple_vm_client/forc_connector/template/test_templates.py index 2aa5a96..eb55c3a 100644 --- a/simple_vm_client/forc_connector/template/test_templates.py +++ b/simple_vm_client/forc_connector/template/test_templates.py @@ -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, @@ -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() @@ -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 @@ -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") diff --git a/simple_vm_client/test_openstack_connector.py b/simple_vm_client/test_openstack_connector.py index c4d777b..c38920d 100644 --- a/simple_vm_client/test_openstack_connector.py +++ b/simple_vm_client/test_openstack_connector.py @@ -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, @@ -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): @@ -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 @@ -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") @@ -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):