Skip to content

Commit

Permalink
test: added unit tests for delete_job() method
Browse files Browse the repository at this point in the history
Signed-off-by: Bobbins228 <[email protected]>
  • Loading branch information
Bobbins228 committed Aug 12, 2024
1 parent 725b09e commit 92ebe28
Showing 1 changed file with 76 additions and 1 deletion.
77 changes: 76 additions & 1 deletion sdk/python/kubeflow/training/api/training_client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"}}]
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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")

0 comments on commit 92ebe28

Please sign in to comment.