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 fixture_git_script_branch(tmpdir): """ Pytest fixture to return a path to a script file """ script_path = tmpdir.join("git_script_branch.py") script_path.write("def main(*args, **kwargs):\n\tpass") return GitScript( f"git://{str(script_path)}", git_args=GitArgs(git_branch="git-test-branch"), create_env=True, )
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 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_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 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