From bd40074f3117d78e2080b01bb439cc1f146091eb Mon Sep 17 00:00:00 2001 From: Josh Usiskin <56369778+jusiskin@users.noreply.github.com> Date: Fri, 9 Aug 2024 13:30:25 -0500 Subject: [PATCH] fix: install-deadline-worker doesn't create queues persistence dir on Windows (#377) Signed-off-by: Josh Usiskin <56369778+jusiskin@users.noreply.github.com> --- .../installer/win_installer.py | 6 +++ test/unit/install/test_windows_installer.py | 54 +++++++++++++++---- test/unit/windows/test_win_logon.py | 2 +- 3 files changed, 52 insertions(+), 10 deletions(-) diff --git a/src/deadline_worker_agent/installer/win_installer.py b/src/deadline_worker_agent/installer/win_installer.py index bf5c6234..b0e5be87 100644 --- a/src/deadline_worker_agent/installer/win_installer.py +++ b/src/deadline_worker_agent/installer/win_installer.py @@ -429,6 +429,7 @@ def provision_directories(agent_username: str) -> WorkerAgentDirectories: - %PROGRAMDATA%/Amazon/Deadline - %PROGRAMDATA%/Amazon/Deadline/Logs - %PROGRAMDATA%/Amazon/Deadline/Cache + - %PROGRAMDATA%/Amazon/Deadline/Cache/queues - %PROGRAMDATA%/Amazon/Deadline/Config Parameters @@ -462,6 +463,11 @@ def provision_directories(agent_username: str) -> WorkerAgentDirectories: os.makedirs(deadline_persistence_subdir, exist_ok=True) logging.info(f"Done provisioning persistence directory ({deadline_persistence_subdir})") + deadline_persistence_queues_subdir = os.path.join(deadline_persistence_subdir, "queues") + logging.info(f"Provisioning persistence directory ({deadline_persistence_queues_subdir})") + os.makedirs(deadline_persistence_queues_subdir, exist_ok=True) + logging.info(f"Done provisioning persistence directory ({deadline_persistence_queues_subdir})") + deadline_config_subdir = os.path.join(deadline_dir, "Config") logging.info(f"Provisioning config directory ({deadline_config_subdir})") os.makedirs(deadline_config_subdir, exist_ok=True) diff --git a/test/unit/install/test_windows_installer.py b/test/unit/install/test_windows_installer.py index 5c475922..1809c2a0 100644 --- a/test/unit/install/test_windows_installer.py +++ b/test/unit/install/test_windows_installer.py @@ -1,5 +1,6 @@ # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +import os import sys import typing from typing import Generator @@ -96,17 +97,16 @@ def test_start_windows_installer_fails_when_windows_job_user_not_exists( ) -@patch("deadline_worker_agent.installer.win_installer.users_equal", return_value=True) -@patch("deadline_worker_agent.installer.win_installer.check_account_existence", return_value=True) -@patch.object(shell, "IsUserAnAdmin", return_value=True) def test_start_windows_installer_fails_when_windows_job_user_is_agent_user( - users_equal: MagicMock, - is_user_and_admin: MagicMock, - check_account_existence: MagicMock, parsed_kwargs: dict, ) -> None: # GIVEN - with pytest.raises(win_installer.InstallerFailedException) as exc_info: + with ( + patch.object(win_installer, "users_equal", return_value=True) as mock_users_equal, + patch.object(win_installer, "check_account_existence", return_value=True), + patch.object(shell, "IsUserAnAdmin", return_value=True), + pytest.raises(win_installer.InstallerFailedException) as exc_info, + ): # WHEN win_installer.start_windows_installer(**parsed_kwargs) @@ -116,8 +116,8 @@ def test_start_windows_installer_fails_when_windows_job_user_is_agent_user( == f"Argument for windows-job-user cannot be the same as the worker agent user: {parsed_kwargs['user_name']}. " "If you wish to run jobs as the agent user, set run_jobs_as_agent_user = true in the agent configuration file." ) - assert users_equal.called_once_with( - parsed_kwargs["user_name"], parsed_kwargs["windows_job_user"] + mock_users_equal.assert_called_once_with( + parsed_kwargs["windows_job_user"], parsed_kwargs["user_name"] ) @@ -889,3 +889,39 @@ def closes_handle_if_set_value_errors( # THEN assert raised_exc.value is mock_RegSetValueEx.side_effect mock_CloseHandle.assert_called_once_with(mock_RegOpenKeyEx.return_value) + + +class TestProvisionDirectories: + """Tests for the provision_directories function""" + + @pytest.fixture(autouse=True) + def mock_os_makedirs(self) -> Generator[MagicMock, None, None]: + with patch.object(win_installer.os, "makedirs") as mock_os_makedirs: + yield mock_os_makedirs + + @pytest.fixture(autouse=True) + def mock_set_windows_permissions(self) -> Generator[MagicMock, None, None]: + with patch.object( + win_installer, "_set_windows_permissions" + ) as mock_set_windows_permissions: + yield mock_set_windows_permissions + + @pytest.fixture + def agent_username(self) -> str: + return "deadline-worker" + + def test_creates_queue_persistence_dir( + self, + mock_os_makedirs: MagicMock, + agent_username: str, + ) -> None: + """Tests that provision_directories creates the queue persistence directory""" + + # WHEN + win_installer.provision_directories(agent_username) + + # THEN + mock_os_makedirs.assert_any_call( + os.path.join(os.environ["PROGRAMDATA"], "Amazon", "Deadline", "Cache", "queues"), + exist_ok=True, + ) diff --git a/test/unit/windows/test_win_logon.py b/test/unit/windows/test_win_logon.py index 902c416d..e4186ced 100644 --- a/test/unit/windows/test_win_logon.py +++ b/test/unit/windows/test_win_logon.py @@ -370,7 +370,7 @@ def test_password_uses_secrets(self, secrets_choice: MagicMock, alphabet: str): # THEN assert secrets_choice.call_count >= 256 - assert secrets_choice.has_calls( + secrets_choice.assert_has_calls( [call(alphabet) for _ in range(secrets_choice.call_count)] )