Skip to content

Commit

Permalink
[SDK] test: add unit tests for delete_job() method (#2232)
Browse files Browse the repository at this point in the history
Signed-off-by: Bobbins228 <[email protected]>
  • Loading branch information
Bobbins228 authored Sep 2, 2024
1 parent 2455f7d commit c64a5a6
Showing 1 changed file with 75 additions and 4 deletions.
79 changes: 75 additions & 4 deletions sdk/python/kubeflow/training/api/training_client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
TrainingClient,
constants,
)
from kubeflow.training.models import V1DeleteOptions
from kubernetes.client import (
V1Container,
V1ObjectMeta,
Expand All @@ -31,6 +32,7 @@
LIST_RESPONSE = [
{"metadata": {"name": DUMMY_POD_NAME}},
]
SUCCESS = "success"


def conditional_error_handler(*args, **kwargs):
Expand Down Expand Up @@ -219,7 +221,7 @@ def __init__(self, kind) -> None:
(
"valid flow",
{"job": create_job(), "namespace": TEST_NAME},
"success",
SUCCESS,
),
(
"valid flow to create job from func",
Expand All @@ -232,7 +234,7 @@ def __init__(self, kind) -> None:
"packages_to_install": ["boto3==1.34.14"],
"pip_index_url": "https://pypi.custom.com/simple",
},
"success",
SUCCESS,
),
(
"valid flow to create job using image",
Expand All @@ -242,7 +244,7 @@ def __init__(self, kind) -> None:
"base_image": "docker.io/test-training",
"num_workers": 2,
},
"success",
SUCCESS,
),
]

Expand Down Expand Up @@ -425,6 +427,7 @@ def training_client():
return_value=Mock(
create_namespaced_custom_object=Mock(side_effect=conditional_error_handler),
patch_namespaced_custom_object=Mock(side_effect=conditional_error_handler),
delete_namespaced_custom_object=Mock(side_effect=conditional_error_handler),
),
), patch(
"kubernetes.client.CoreV1Api",
Expand All @@ -448,7 +451,7 @@ def test_create_job(training_client, test_name, kwargs, expected_output):
print("Executing test:", test_name)
try:
training_client.create_job(**kwargs)
assert expected_output == "success"
assert expected_output == SUCCESS
except Exception as e:
assert type(e) is expected_output
print("test execution complete")
Expand Down Expand Up @@ -531,3 +534,71 @@ def test_wait_for_job_conditions(training_client, test_name, kwargs, expected_ou
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": constants.PYTORCHJOB_KIND},
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": constants.PYTORCHJOB_KIND,
},
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 c64a5a6

Please sign in to comment.