From e500730a7746a1a2e7d205b11d55d920163c274c Mon Sep 17 00:00:00 2001 From: Maxime Noel Date: Wed, 11 Dec 2024 10:20:42 +1300 Subject: [PATCH] Fix paths for test main program --- .../tests/test_main_program.py | 41 +++++++++++-------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/jenkins_pipelines/scripts/test_environment_cleaner/tests/test_main_program.py b/jenkins_pipelines/scripts/test_environment_cleaner/tests/test_main_program.py index 94da21c14..235c7ed49 100644 --- a/jenkins_pipelines/scripts/test_environment_cleaner/tests/test_main_program.py +++ b/jenkins_pipelines/scripts/test_environment_cleaner/tests/test_main_program.py @@ -6,15 +6,15 @@ # Add the root directory of the project to the sys.path sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '../..'))) -from test_environment_cleaner_program.TestEnvironmentCleaner.py import main # Ensure the import is correct +from test_environment_cleaner_program.TestEnvironmentCleaner import main # Ensure the import is correct class TestMainProgram(unittest.TestCase): - @patch("test_environment_cleaner_program.TestEnvironmentCleaner.py.ResourceManager") - @patch("test_environment_cleaner_program.TestEnvironmentCleaner.py.SSHClientManager") + @patch("test_environment_cleaner_program.TestEnvironmentCleaner.ResourceManager") + @patch("test_environment_cleaner_program.TestEnvironmentCleaner.SSHClientManager") def test_delete_users_mode(self, MockSSHClientManager, MockResourceManager): # Mock the arguments - test_args = ["test_environment_cleaner_program.TestEnvironmentCleaner.py", "--url", "http://test-url.com", "--mode", "delete_users", "--product_version", "5.0"] + test_args = ["test_environment_cleaner_program.TestEnvironmentCleaner", "--url", "http://test-url.com", "--mode", "delete_users", "--product_version", "5.0"] with patch.object(sys, 'argv', test_args): # Mock ResourceManager and its methods mock_resource_manager = MockResourceManager.return_value @@ -29,10 +29,10 @@ def test_delete_users_mode(self, MockSSHClientManager, MockResourceManager): mock_resource_manager.delete_users.assert_called_once() mock_resource_manager.logout_session.assert_called_once() - @patch("test_environment_cleaner_program.TestEnvironmentCleaner.py.ResourceManager") - @patch("test_environment_cleaner_program.TestEnvironmentCleaner.py.SSHClientManager") + @patch("test_environment_cleaner_program.TestEnvironmentCleaner.ResourceManager") + @patch("test_environment_cleaner_program.TestEnvironmentCleaner.SSHClientManager") def test_delete_known_hosts_mode(self, MockSSHClientManager, MockResourceManager): - test_args = ["test_environment_cleaner_program.TestEnvironmentCleaner.py", "--url", "http://test-url.com", "--mode", "delete_known_hosts", "--product_version", "5.0"] + test_args = ["test_environment_cleaner_program.TestEnvironmentCleaner", "--url", "http://test-url.com", "--mode", "delete_known_hosts", "--product_version", "5.0"] with patch.object(sys, 'argv', test_args): # Mock SSHClientManager and its methods mock_ssh_manager = MockSSHClientManager.return_value @@ -47,11 +47,11 @@ def test_delete_known_hosts_mode(self, MockSSHClientManager, MockResourceManager mock_ssh_manager.run_command.assert_any_call("rm /var/lib/containers/storage/volumes/var-salt/_data/.ssh/known_hosts") mock_ssh_manager.close.assert_called_once() - @patch("test_environment_cleaner_program.TestEnvironmentCleaner.py.ResourceManager") - @patch("test_environment_cleaner_program.TestEnvironmentCleaner.py.SSHClientManager") + @patch("test_environment_cleaner_program.TestEnvironmentCleaner.ResourceManager") + @patch("test_environment_cleaner_program.TestEnvironmentCleaner.SSHClientManager") def test_invalid_mode(self, MockSSHClientManager, MockResourceManager): # Define the test argument with an invalid mode - test_args = ["TestEnvironmentCleaner.py", "--url", "http://test-url.com", "--mode", "false", "--product_version", "5.0"] + test_args = ["suse_manager_cleaner_program.TestEnvironmentCleaner", "--url", "http://test-url.com", "--mode", "false", "--product_version", "5.0"] # Patch sys.argv with the test arguments with patch.object(sys, 'argv', test_args): @@ -62,17 +62,22 @@ def test_invalid_mode(self, MockSSHClientManager, MockResourceManager): # Assert the error message is in the captured stderr expected_error_message = ( - "TestEnvironmentCleaner.py: error: argument --mode: invalid choice: 'false' " - "(choose from 'delete_users', 'delete_activation_keys', 'delete_config_projects', " - "'delete_software_channels', 'delete_systems', 'delete_repositories', 'full_cleanup', " - "'delete_salt_keys', 'delete_known_hosts', 'update_custom_repositories', 'delete_distributions')" + "usage: use_manager_cleaner_program.TestEnvironmentCleaner [-h] --url URL --mode " + "{delete_users,delete_activation_keys,delete_config_projects,delete_software_channels," + "delete_systems,delete_repositories,full_cleanup,delete_salt_keys,delete_known_hosts,update_custom_repositories,delete_distributions}" + " [--default-resources-to-delete [{proxy,monitoring-server,retail} ...]] --product_version {5.0,4.3,uyuni}" + "suse_manager_cleaner_program.TestEnvironmentCleaner: error: argument --mode: invalid choice: 'false' (choose from 'delete_users'," + " 'delete_activation_keys', 'delete_config_projects', 'delete_software_channels', 'delete_systems'," + " 'delete_repositories', 'full_cleanup', 'delete_salt_keys', 'delete_known_hosts', 'update_custom_repositories'," + " 'delete_distributions')" ) - self.assertIn(expected_error_message, fake_err.getvalue()) + cleaned_error = " ".join(fake_err.getvalue().split()) + self.assertIn(expected_error_message, cleaned_error) - @patch("test_environment_cleaner_program.TestEnvironmentCleaner.py.ResourceManager") - @patch("test_environment_cleaner_program.TestEnvironmentCleaner.py.SSHClientManager") + @patch("test_environment_cleaner_program.TestEnvironmentCleaner.ResourceManager") + @patch("test_environment_cleaner_program.TestEnvironmentCleaner.SSHClientManager") def test_update_custom_repositories_mode(self, MockSSHClientManager, MockResourceManager): - test_args = ["test_environment_cleaner_program.TestEnvironmentCleaner.py", "--url", "http://test-url.com", "--mode", "update_custom_repositories", "--product_version", "5.0"] + test_args = ["test_environment_cleaner_program.TestEnvironmentCleaner", "--url", "http://test-url.com", "--mode", "update_custom_repositories", "--product_version", "5.0"] with patch.object(sys, 'argv', test_args): # Mock SSHClientManager and its methods mock_ssh_manager = MockSSHClientManager.return_value