Skip to content

Commit

Permalink
Fix paths for test main program
Browse files Browse the repository at this point in the history
  • Loading branch information
maximenoel8 committed Dec 10, 2024
1 parent cce1179 commit e500730
Showing 1 changed file with 23 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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):
Expand All @@ -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
Expand Down

0 comments on commit e500730

Please sign in to comment.