def test_git_args_input_eq_works_as_expected(self): """ Verify GitArgs equality """ ga1 = GitArgs("git://test.com", "HEAD", "master") ga2 = GitArgs("git://test.com", "HEAD", "master") ga3 = GitArgs("test") assert ga1 == ga2 assert ga1 != ga3 assert ga1 != object()
def test_get_hash_when_branch_given(mock_ls_remote_fn): mock_ls_remote_fn.side_effect = [ "69e93d57916f837ee93ca125f2785f0f6e21974d\\feature_branch", "69e93d57916f837ee93ca125f2785f0f6e21974d\tfeature_branch", ] result1 = GitManager.get_commit_hash( GitArgs(git_repo="https://gitlab.com/", git_branch="feature_branch")) result2 = GitManager.get_commit_hash( GitArgs(git_repo="https://gitlab.com/", git_branch="feature_branch")) assert mock_ls_remote_fn.call_args == mock.call("ls_remote", "-h", "https://gitlab.com/", "feature_branch") assert "69e93d57916f837ee93ca125f2785f0f6e21974d" == result1 assert "69e93d57916f837ee93ca125f2785f0f6e21974d" == result2
def test_script_worker_calls_correct_function_on_message_type( self, mock_env_fn, mock_load_fn, mock_run_fn, caplog): mp = multiprocessing.get_context() script = GitScript("git://test.py", GitArgs()) env_evt = EventMessage("test", "ENV", script) load_evt = EventMessage("test", "LOAD", script) run_evt = EventMessage("test", "RUN", ("init", None)) work_q = MPQueue(ctx=mp) work_q.put(env_evt) work_q.put(load_evt) work_q.put(run_evt) _proc_worker_wrapper_helper(mp, caplog, ScriptWorker, args=(work_q, ), expect_shutdown_evt=True) env_args, _ = mock_env_fn.call_args assert env_args[0].msg_type == env_evt.msg_type mock_env_fn.assert_called_once() load_args, _ = mock_load_fn.call_args assert load_args[0].msg_type == load_evt.msg_type mock_load_fn.assert_called_once() run_args, _ = mock_run_fn.call_args assert run_args[0].msg_type == run_evt.msg_type mock_run_fn.assert_called_once()
def test_get_module_calls_git_load_function(self, mock_git_load): mock_git_load.side_effect = [ MagicMock(importlib.machinery.SourceFileLoader) ] git_script = GitScript("git://test/script.py", GitArgs()) _ = ModuleFactory.get_module(git_script) mock_git_load.assert_called_once_with(git_script)
def fixture_git_script(tmpdir): """ Pytest fixture to return a path to a git script file """ script_path = tmpdir.join("git_script.py") script_path.write("def main(*args, **kwargs):\n\tpass") return GitScript(f"git://{str(script_path)}", git_args=GitArgs(), create_env=True)
def test_get_hash_from_main_branch_when_branch_not_given(mock_ls_remote_fn): mock_ls_remote_fn.return_value = "69e93d57916f837ee93ca125f2785f0f6e21974d\\main" result = GitManager.get_commit_hash( GitArgs(git_repo="https://gitlab.com/")) assert mock_ls_remote_fn.call_args == mock.call("ls_remote", "https://gitlab.com/", "HEAD") assert "69e93d57916f837ee93ca125f2785f0f6e21974d" == result
def test_git_args_input_accepts_expected_values(self): """ Verify that GitArgs arguments. """ git_args = GitArgs(git_repo="git://test.com", git_branch="master", git_commit="HEAD") assert git_args.git_repo == "git://test.com" assert git_args.git_commit == "HEAD"
def test_clone_not_done_if_already_cloned(mock_commit_hash_fn, mock_clone_fn): commit = "123abc" mock_commit_hash_fn.side_effect = [commit] with mock.patch("os.path.exists") as mock_exists: mock_exists.side_effect = [True] GitManager.clone_repo( GitArgs(git_repo="https://test.com/fake-repo-name.git")) mock_clone_fn.assert_not_called()
def test_environment_is_returned_when_hash_exists(env_manager, env_object): commit = COMMIT_HASH env_manager._envs = {commit: env_object} # pylint: disable=protected-access result = env_manager.create_env(GitArgs(git_commit=commit)) assert result == env_object # Clean up after the test del env_manager._envs[commit] # pylint: disable=protected-access
def test_git_args_default_values_are_as_expected(self): """ Verify that GitArgs default values are set as expected if not provided. """ git_args = GitArgs() assert (git_args.git_repo == "https://gitlab.com/ska-telescope/oso/ska-oso-scripting.git") assert git_args.git_branch == "master" assert git_args.git_commit is None
def test_clone_raises_error_if_clone_dir_not_found( mock_commit_hash_fn, mock_clone_fn, base_dir # pylint: disable=unused-argument ): GitManager.base_dir = base_dir commit = "ghi789" mock_commit_hash_fn.side_effect = [commit] with pytest.raises(IOError): GitManager.clone_repo(GitArgs())
def test_repo_is_full_cloned_and_commit_checked_out_when_hash_given( mock_proj_name_fn, mock_commit_hash_fn, test_repo, base_dir): GitManager.base_dir = base_dir commit = test_repo[2] mock_commit_hash_fn.side_effect = [commit] # Mock project name so that it doesn't include full test file structure in the name mock_proj_name_fn.side_effect = ["test-repo"] GitManager.clone_repo( GitArgs(git_repo=f"file://{base_dir}test-repo", git_commit=commit)) expected_path = base_dir + "test-repo/" + commit assert "initial-file.txt" in os.listdir(expected_path) assert "feature-a-file.txt" in os.listdir(expected_path)
def test_on_load(self, mock_module_fn, mp, caplog): """ """ mock_module_fn.side_effect = MagicMock() script = GitScript("git://test.py", GitArgs()) evt = EventMessage("test", "LOAD", script) work_q = MPQueue(ctx=mp) work_q.put(evt) _proc_worker_wrapper_helper(mp, caplog, ScriptWorker, args=(work_q, ), expect_shutdown_evt=True) assert mock_module_fn.called_once_with(script)
def fixture_git_sys_path_script(tmpdir): """ Pytest fixture to return a path to a script file """ script_path = tmpdir.join("git_sys_path_script.py") script_path.write(""" import sys def main(site_package): print(sys.path) assert site_package in sys.path """) return GitScript(f"git://{str(script_path)}", git_args=GitArgs(), create_env=True)
def test_create_sends_load_and_run_messages_for_gitscript(self, manager): """ Verify that a call to ProcessManager.create() sends the load and run init messages to ScriptWorker when git script is created. """ manager.ctx.Proc = MagicMock() git_script = GitScript(script_uri="git://test-script.py", git_args=GitArgs()) manager.create(git_script, init_args=ProcedureInput()) q = manager.script_queues[1] load_msg = q.safe_get() run_msg = q.safe_get() assert load_msg.msg_type == "LOAD" assert load_msg.msg == git_script assert run_msg.msg_type == "RUN" assert run_msg.msg == ("init", None)
def test_repo_is_shallow_cloned_from_branch(mock_commit_hash_fn, mock_clone_fn, base_dir): GitManager.base_dir = base_dir commit = "def456" mock_commit_hash_fn.side_effect = [commit] mock_clone_fn.side_effect = mock_clone_repo GitManager.clone_repo(GitArgs(git_branch="feature-branch"), ) expected_path = base_dir + "ska-telescope-oso-ska-oso-scripting/" + commit mock_clone_fn.assert_called_once_with( "https://gitlab.com/ska-telescope/oso/ska-oso-scripting.git", expected_path, depth=1, single_branch=True, branch="feature-branch", ) assert "initial-file.txt" in os.listdir(expected_path)
def test_repo_is_shallow_cloned_from_main_when_defaults_given( mock_commit_hash_fn, mock_clone_fn, base_dir): GitManager.base_dir = base_dir commit = "abcd123" mock_commit_hash_fn.side_effect = [commit] mock_clone_fn.side_effect = mock_clone_repo GitManager.clone_repo(GitArgs()) expected_path = base_dir + "ska-telescope-oso-ska-oso-scripting/" + commit mock_clone_fn.assert_called_once_with( "https://gitlab.com/ska-telescope/oso/ska-oso-scripting.git", expected_path, depth=1, single_branch=True, branch="master", ) assert "initial-file.txt" in os.listdir(expected_path)
def test_create_sends_env_message(self, manager): """ Verify that a call to ProcessManager.create() sends the env message to the ScriptWorker when script type is GitScript and create_env is True. """ manager.ctx.Proc = MagicMock() manager.em.create_env = MagicMock() expected_env = Environment("1", None, None, "/", "/site-packages") manager.em.create_env.side_effect = [expected_env] git_script = GitScript(script_uri="git://test-script.py", git_args=GitArgs(), create_env=True) manager.create(git_script, init_args=ProcedureInput()) q = manager.script_queues[1] env_msg = q.safe_get() assert env_msg.msg_type == "ENV" assert env_msg.msg == git_script _, kwargs = manager.ctx.Proc.call_args assert expected_env == kwargs["environment"]
def test_environment_is_created_when_hash_is_new(mock_subprocess_fn, mock_venv_fn, mock_git_fn, env_manager): commit = COMMIT_HASH mock_git_fn.return_value = commit mock_venv_fn.side_effect = None mock_subprocess_fn.return_value = subprocess.CompletedProcess( args="", returncode=1, stdout=bytes( os.path.join(env_manager.base_dir, COMMIT_HASH, VENV_SITE_PKGS), "utf-8", ), ) result = env_manager.create_env(GitArgs()) assert 1 == len(env_manager._envs) # pylint: disable=protected-access assert result.env_id == commit assert result.site_packages == os.path.join(env_manager.base_dir, commit, VENV_SITE_PKGS) del env_manager._envs[commit] # pylint: disable=protected-access
def test_git_script_raises_error_on_incorrect_prefix(self): with pytest.raises(ValueError) as e: _ = GitScript("incorrectprefix://script.py", git_args=GitArgs()) assert "Incorrect prefix for GitScript: incorrectprefix://script.py" in str( e)
def test_git_script_object_creation(self): script = GitScript("git://script.py", git_args=GitArgs()) assert isinstance(script, GitScript) assert script.script_uri == "git://script.py" assert script.git_args == GitArgs() assert script.create_env is False