def test_filled_initialization(self, kube_secret): task = DeleteNamespacedJob( job_name="test", namespace="test", kube_kwargs={"test": "test"}, kubernetes_api_key_secret="test", ) assert task.job_name == "test" assert task.namespace == "test" assert task.kube_kwargs == {"test": "test"} assert task.kubernetes_api_key_secret == "test"
def test_api_key_pulled_from_secret(self, monkeypatch, kube_secret): task = DeleteNamespacedJob(job_name="test") client = MagicMock() monkeypatch.setattr("prefect.tasks.kubernetes.job.client", client) api_key = {} conf_call = MagicMock() conf_call.return_value.api_key = api_key monkeypatch.setattr( "prefect.tasks.kubernetes.job.client.Configuration", conf_call) task.run() assert api_key == {"authorization": "test_key"}
def test_api_key_pulled_from_secret(self, monkeypatch): task = DeleteNamespacedJob(job_name="test") client = MagicMock() monkeypatch.setattr("prefect.tasks.kubernetes.job.client", client) conf_call = MagicMock() monkeypatch.setattr( "prefect.tasks.kubernetes.job.client.Configuration", conf_call) with set_temporary_config({"cloud.use_local_secrets": True}): with prefect.context(secrets=dict(KUBERNETES_API_KEY="test_key")): task.run() assert conf_call.called
def test_kube_kwargs_value_is_replaced(self, monkeypatch, kube_secret): task = DeleteNamespacedJob(job_name="test", kube_kwargs={"test": "a"}) config = MagicMock() monkeypatch.setattr("prefect.tasks.kubernetes.job.config", config) batchapi = MagicMock() monkeypatch.setattr( "prefect.tasks.kubernetes.job.client", MagicMock(BatchV1Api=MagicMock(return_value=batchapi)), ) task.run(kube_kwargs={"test": "b"}) assert batchapi.delete_namespaced_job.call_args[1]["test"] == "b"
def test_kube_config_in_cluster(self, monkeypatch): config = MagicMock() monkeypatch.setattr("prefect.tasks.kubernetes.job.config", config) batchapi = MagicMock() monkeypatch.setattr( "prefect.tasks.kubernetes.job.client", MagicMock(BatchV1Api=MagicMock(return_value=batchapi)), ) task = DeleteNamespacedJob(job_name="test", kube_kwargs={"test": "a"}, kubernetes_api_key_secret=None) task.run(kube_kwargs={"test": "b"}) assert config.load_incluster_config.called
def test_invalid_body_raises_error(self, kube_secret): task = DeleteNamespacedJob() with pytest.raises(ValueError): task.run(job_name=None)
def test_empty_name_raises_error(self, kube_secret): task = DeleteNamespacedJob() with pytest.raises(ValueError): task.run()
def test_empty_initialization(self, kube_secret): task = DeleteNamespacedJob() assert not task.job_name assert task.namespace == "default" assert task.kube_kwargs == {} assert task.kubernetes_api_key_secret == "KUBERNETES_API_KEY"
import yaml from prefect import Flow from prefect.tasks.kubernetes import CreateNamespacedJob, DeleteNamespacedJob with open("example_job.yaml") as file: job = yaml.safe_load(file) job_task = CreateNamespacedJob() delete_task = DeleteNamespacedJob() with Flow("asdf") as f: t1 = job_task(body=job) t2 = delete_task(job_name="pi") f.run()
def test_empty_kube_kwargs_value_is_updated(self, kube_secret, api_client): task = DeleteNamespacedJob(job_name="test") task.run(kube_kwargs={"test": "a"}) assert api_client.delete_namespaced_job.call_args[1]["test"] == "a"