Exemple #1
0
def test_run_workflow_raises_exception_if_namespace_does_not_exist(
    mock_k8s: MagicMock,
    setup_bodywork_test_project: Iterable[bool],
    project_repo_location: Path,
):
    git_repo_url = f"file://{project_repo_location.absolute()}"
    config = get_config_from_git_repo(git_repo_url)
    mock_k8s.namespace_exists.return_value = False
    with raises(BodyworkWorkflowExecutionError, match="not a valid namespace"):
        run_workflow(config, "foo_bar_foo_993", git_repo_url)
Exemple #2
0
def test_failure_stage_does_not_run_for_namespace_exception(
    mock_k8s: MagicMock, project_repo_location: Path
):
    config_path = Path(f"{project_repo_location}/bodywork.yaml")
    config = BodyworkConfig(config_path)
    mock_k8s.namespace_exists.return_value = False
    try:
        run_workflow(config, "foo_bar_foo_993", project_repo_location)
    except BodyworkWorkflowExecutionError:
        pass

    mock_k8s.configure_batch_stage_job.assert_not_called()
Exemple #3
0
def test_failure_stage_does_not_run_for_docker_image_exception(
    mock_k8s: MagicMock, mock_session: MagicMock, project_repo_location: Path
):
    config_path = Path(f"{project_repo_location}/bodywork.yaml")
    config = BodyworkConfig(config_path)
    mock_session().get.return_value = requests.Response().status_code = 401

    try:
        run_workflow(config, "foo_bar_foo_993", project_repo_location)
    except BodyworkWorkflowExecutionError:
        pass

    mock_k8s.configure_batch_stage_job.assert_not_called()
Exemple #4
0
def test_usage_stats_opt_out_does_not_ping_usage_stats_server(
    mock_k8s: MagicMock,
    mock_git_hash: MagicMock,
    mock_git_download: MagicMock,
    mock_session: MagicMock,
    mock_rmtree: MagicMock,
    project_repo_location: Path,
):
    config_path = Path(f"{project_repo_location}/bodywork.yaml")
    config = BodyworkConfig(config_path)

    run_workflow(config, "foo_bar_foo_993", project_repo_location)

    mock_session().get.assert_called_once()
Exemple #5
0
def test_run_workflow_pings_usage_stats_server(
    mock_k8s: MagicMock,
    mock_git_hash: MagicMock,
    mock_git_download: MagicMock,
    mock_session: MagicMock,
    mock_rmtree: MagicMock,
    project_repo_location: Path,
):
    config_path = Path(f"{project_repo_location}/bodywork.yaml")
    config = BodyworkConfig(config_path)
    config.project.usage_stats = True

    run_workflow(config, "foo_bar_foo_993", project_repo_location)

    mock_session().get.assert_called_with(USAGE_STATS_SERVER_URL, params={"type": "workflow"})
Exemple #6
0
def test_run_workflow_adds_git_commit_to_batch_and_service_env_vars(
    mock_k8s: MagicMock,
    mock_git_hash: MagicMock,
    mock_git_download: MagicMock,
    mock_requests: MagicMock,
    mock_rmtree: MagicMock,
    project_repo_location: Path,
):
    commit_hash = "MY GIT COMMIT HASH"
    mock_git_hash.return_value = commit_hash
    expected_result = [
        k8sclient.V1EnvVar(name=GIT_COMMIT_HASH_K8S_ENV_VAR, value=commit_hash)
    ]
    mock_k8s.create_k8s_environment_variables.return_value = expected_result
    mock_k8s.configure_env_vars_from_secrets.return_value = []
    config_path = Path(f"{project_repo_location}/bodywork.yaml")
    config = BodyworkConfig(config_path)

    run_workflow(config, "foo_bar_foo_993", project_repo_location)

    mock_k8s.configure_service_stage_deployment.assert_called_once_with(
        ANY,
        ANY,
        ANY,
        ANY,
        ANY,
        replicas=ANY,
        port=ANY,
        container_env_vars=expected_result,
        image=ANY,
        cpu_request=ANY,
        memory_request=ANY,
        seconds_to_be_ready_before_completing=ANY,
    )
    mock_k8s.configure_batch_stage_job.assert_called_with(
        ANY,
        ANY,
        ANY,
        ANY,
        ANY,
        retries=ANY,
        container_env_vars=expected_result,
        image=ANY,
        cpu_request=ANY,
        memory_request=ANY,
    )
Exemple #7
0
def test_run_workflow_runs_failure_stage_on_failure(
    mock_k8s: MagicMock,
    mock_git_hash: MagicMock,
    mock_git_download: MagicMock,
    mock_requests: MagicMock,
    mock_rmtree: MagicMock,
    project_repo_location: Path,
):
    config_path = Path(f"{project_repo_location}/bodywork.yaml")
    config = BodyworkConfig(config_path)
    config.project.run_on_failure = "on_fail_stage"

    error_message = "Test Error"
    mock_job = MagicMock(k8sclient.V1Job)
    mock_k8s.configure_batch_stage_job.side_effect = [
        k8sclient.ApiException(error_message),
        mock_job,
    ]
    expected_result = [
        k8sclient.V1EnvVar(name=FAILURE_EXCEPTION_K8S_ENV_VAR, value=error_message)
    ]
    mock_k8s.create_k8s_environment_variables.return_value = expected_result
    mock_k8s.configure_env_vars_from_secrets.return_value = []

    try:
        run_workflow(config, "foo_bar_foo_993", project_repo_location)
    except BodyworkWorkflowExecutionError:
        pass

    mock_k8s.configure_batch_stage_job.assert_called_with(
        ANY,
        "on_fail_stage",
        ANY,
        ANY,
        ANY,
        retries=ANY,
        container_env_vars=expected_result,
        image=ANY,
        cpu_request=ANY,
        memory_request=ANY,
    )
Exemple #8
0
def test_failure_of_failure_stage_is_recorded_in_exception(
    mock_k8s: MagicMock,
    mock_git_hash: MagicMock,
    mock_git_download: MagicMock,
    mock_requests: MagicMock,
    mock_rmtree: MagicMock,
    project_repo_location: Path,
):
    config_path = Path(f"{project_repo_location}/bodywork.yaml")
    config = BodyworkConfig(config_path)
    config.project.run_on_failure = "on_fail_stage"

    error_message = "The run-on-failure stage experienced an error"
    mock_k8s.configure_batch_stage_job.side_effect = [
        k8sclient.ApiException("Original Error"),
        k8sclient.ApiException(reason=error_message),
    ]
    mock_k8s.configure_env_vars_from_secrets.return_value = []

    with raises(BodyworkWorkflowExecutionError, match=f"{error_message}"):
        run_workflow(config, "foo_bar_foo_993", project_repo_location)