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..0962cd3 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() @@ -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 [] @@ -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, ) @@ -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/forc_connector/test_forc_connector.py b/simple_vm_client/forc_connector/test_forc_connector.py index 877b861..1eb77fc 100644 --- a/simple_vm_client/forc_connector/test_forc_connector.py +++ b/simple_vm_client/forc_connector/test_forc_connector.py @@ -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") @@ -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") @@ -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 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):