Example #1
0
    def test_run_unsuccessful_path(self, kube_secret, api_client):
        unsuccessful_job_status = MagicMock()
        unsuccessful_job_status.status.active = None
        unsuccessful_job_status.status.failed = 1
        unsuccessful_job_status.status.succeeded = None

        api_client.read_namespaced_job_status = MagicMock(
            return_value=unsuccessful_job_status
        )

        task = RunNamespacedJob(body={"metadata": {"name": "failure"}})
        with pytest.raises(signals.FAIL) as failed:
            task.run()

        assert api_client.create_namespaced_job.call_count == 1
        assert api_client.create_namespaced_job.call_args[1]["namespace"] == "default"
        assert api_client.create_namespaced_job.call_args[1]["body"] == {
            "metadata": {"name": "failure"}
        }

        assert api_client.read_namespaced_job_status.call_count == 1
        assert api_client.read_namespaced_job_status.call_args[1]["name"] == "failure"
        assert (
            api_client.read_namespaced_job_status.call_args[1]["namespace"] == "default"
        )

        assert api_client.delete_namespaced_job.call_count == 0
Example #2
0
    def test_run_successful_path_without_deleting_resources_after_completion(
        self, kube_secret, api_client, successful_job_status
    ):
        api_client.read_namespaced_job_status = MagicMock(
            return_value=successful_job_status
        )

        task = RunNamespacedJob(
            body={"metadata": {"name": "success"}}, delete_job_after_completion=False
        )
        task.run()

        assert api_client.create_namespaced_job.call_count == 1
        assert api_client.create_namespaced_job.call_args[1]["namespace"] == "default"
        assert api_client.create_namespaced_job.call_args[1]["body"] == {
            "metadata": {"name": "success"}
        }

        assert api_client.read_namespaced_job_status.call_count == 1
        assert api_client.read_namespaced_job_status.call_args[1]["name"] == "success"
        assert (
            api_client.read_namespaced_job_status.call_args[1]["namespace"] == "default"
        )

        assert api_client.delete_namespaced_job.call_count == 0
Example #3
0
    def test_empty_kube_kwargs_value_is_updated(self, kube_secret, api_client,
                                                successful_job_status):
        api_client.read_namespaced_job_status = MagicMock(
            return_value=successful_job_status)

        task = RunNamespacedJob(body={"metadata": {"name": "a"}})

        task.run(kube_kwargs={"test": "a"})
        assert api_client.create_namespaced_job.call_args[1]["test"] == "a"
Example #4
0
    def test_run_with_logging(self, kube_secret, api_client,
                              successful_job_status, read_pod_logs):
        api_client.read_namespaced_job_status = MagicMock(
            return_value=successful_job_status)

        api_client.list_namespaced_pod = MagicMock(return_value=MagicMock(
            items=[
                Box({
                    "metadata": {
                        "name": "test-pod"
                    },
                    "status": {
                        "phase": "Running"
                    },
                })
            ]))

        task = RunNamespacedJob(body={"metadata": {
            "name": "success"
        }},
                                log_level="info")
        task.run()

        assert api_client.create_namespaced_job.call_count == 1
        assert api_client.create_namespaced_job.call_args[1][
            "namespace"] == "default"
        assert api_client.create_namespaced_job.call_args[1]["body"] == {
            "metadata": {
                "name": "success"
            }
        }

        assert api_client.read_namespaced_job_status.call_count == 1
        assert api_client.read_namespaced_job_status.call_args[1][
            "name"] == "success"
        assert (api_client.read_namespaced_job_status.call_args[1]["namespace"]
                == "default")

        assert api_client.list_namespaced_pod.call_count == 1
        assert api_client.list_namespaced_pod.call_args[1][
            "label_selector"].startswith("controller-uid=")
        assert api_client.list_namespaced_pod.call_args[1][
            "namespace"] == "default"

        assert read_pod_logs.call_count == 1
        assert read_pod_logs.call_args[1]["pod_name"] == "test-pod"
        assert read_pod_logs.call_args[1]["namespace"] == "default"

        assert api_client.delete_namespaced_job.call_count == 1
        assert api_client.delete_namespaced_job.call_args[1][
            "name"] == "success"
        assert api_client.delete_namespaced_job.call_args[1][
            "namespace"] == "default"
Example #5
0
    def test_body_value_is_appended(
        self, kube_secret, api_client, successful_job_status
    ):
        api_client.read_namespaced_job_status = MagicMock(
            return_value=successful_job_status
        )
        task = RunNamespacedJob(body={"metadata": {"name": "a"}})

        task.run(body={"a": "test"})
        assert api_client.create_namespaced_job.call_args[1]["body"] == {
            "a": "test",
            "metadata": {"name": "a"},
        }
Example #6
0
 def test_invalid_body_raises_error(self, kube_secret):
     task = RunNamespacedJob()
     with pytest.raises(ValueError):
         task.run(body=None)
Example #7
0
 def test_empty_body_raises_error(self, kube_secret):
     task = RunNamespacedJob()
     with pytest.raises(ValueError):
         task.run()
Example #8
0
 def test_invalid_log_level_raises_error(self, kube_secret):
     task = RunNamespacedJob()
     with pytest.raises(ValueError):
         task.run(body={"test": "test"}, log_level="invalid")