From 92ebe2800db3288ad79f5fee43fb19ee2036e2b2 Mon Sep 17 00:00:00 2001 From: Bobbins228 Date: Mon, 12 Aug 2024 12:29:31 +0100 Subject: [PATCH] test: added unit tests for delete_job() method Signed-off-by: Bobbins228 --- .../training/api/training_client_test.py | 77 ++++++++++++++++++- 1 file changed, 76 insertions(+), 1 deletion(-) diff --git a/sdk/python/kubeflow/training/api/training_client_test.py b/sdk/python/kubeflow/training/api/training_client_test.py index 90ae04637f..18078addcc 100644 --- a/sdk/python/kubeflow/training/api/training_client_test.py +++ b/sdk/python/kubeflow/training/api/training_client_test.py @@ -15,6 +15,7 @@ from kubernetes.client import V1PodSpec from kubernetes.client import V1PodTemplateSpec from kubernetes.client import V1ResourceRequirements +from kubeflow.training.models import V1DeleteOptions import pytest LIST_RESPONSE = [{"metadata": {"name": "Dummy V1PodList"}}] @@ -42,6 +43,13 @@ def get(self, timeout): return MockResponse() +def delete_namespaced_custom_object_response(*args, **kwargs): + if args[2] == "timeout": + raise multiprocessing.TimeoutError() + elif args[2] == "runtime": + raise RuntimeError() + + def generate_container() -> V1Container: return V1Container( name="pytorch", @@ -257,7 +265,10 @@ def training_client(): return_value=Mock( create_namespaced_custom_object=Mock( side_effect=create_namespaced_custom_object_response - ) + ), + delete_namespaced_custom_object=Mock( + side_effect=delete_namespaced_custom_object_response + ), ), ), patch( "kubernetes.client.CoreV1Api", @@ -309,3 +320,67 @@ def test_get_job_pods( except Exception as e: assert type(e) is expected_output print("test execution complete") + + +test_data_delete_job = [ + ( + "valid flow with default namespace", + { + "name": TEST_NAME, + }, + "success", + ), + ( + "invalid extra parameter", + {"name": TEST_NAME, "namespace": TEST_NAME, "example": "test"}, + TypeError, + ), + ( + "invalid job kind", + {"name": TEST_NAME, "job_kind": "invalid_job_kind"}, + RuntimeError, + ), + ( + "job name missing", + {"namespace": TEST_NAME, "job_kind": "PyTorchJob"}, + TypeError, + ), + ( + "delete_namespaced_custom_object timeout error", + {"name": TEST_NAME, "namespace": "timeout"}, + TimeoutError, + ), + ( + "delete_namespaced_custom_object runtime error", + {"name": TEST_NAME, "namespace": "runtime"}, + RuntimeError, + ), + ( + "valid flow", + {"name": TEST_NAME, "namespace": TEST_NAME, "job_kind": "PyTorchJob"}, + "success", + ), + ( + "valid flow with delete options", + { + "name": TEST_NAME, + "delete_options": V1DeleteOptions(grace_period_seconds=30), + }, + "success", + ), +] + + +@pytest.mark.parametrize("test_name,kwargs,expected_output", test_data_delete_job) +def test_delete_job(training_client, test_name, kwargs, expected_output): + """ + test delete_job function of training client + """ + print("Executing test: ", test_name) + + try: + training_client.delete_job(**kwargs) + assert expected_output == "success" + except Exception as e: + assert type(e) is expected_output + print("test execution complete")