Skip to content

Commit 8722607

Browse files
author
pintaoz
committed
Add test_job test
1 parent 0beb253 commit 8722607

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

src/hyperpod_cli/service/list_training_jobs.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from hyperpod_cli.clients.kubernetes_client import (
1919
KubernetesClient,
2020
)
21+
from hyperpod_cli.utils import setup_logger
2122
from kubernetes.client.rest import ApiException
2223
from kubernetes.client import (
2324
V1ResourceAttributes
@@ -49,6 +50,8 @@ def list_training_jobs(
4950
k8s_client = KubernetesClient()
5051

5152
jobs: List = []
53+
logger = setup_logger(__name__)
54+
logger.debug(namespace)
5255
try:
5356
if all_namespaces:
5457
namespaces: List[str] = k8s_client.list_namespaces()

test/unit_tests/clients/test_kubernetes_client.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -662,26 +662,38 @@ def test_get_cluster_queue(
662662
name="test-cluster-queue",
663663
)
664664

665+
@patch("kubernetes.config.load_kube_config")
665666
@patch(
666-
"kubernetes.client.CoreV1Api",
667+
"kubernetes.client.CoreV1Api.read_namespace",
667668
return_value=Mock(
668669
read_namespace=Mock(
669-
return_value=V1Namespace(metadata=V1ObjectMeta(name="test"))
670+
return_value=V1Namespace(metadata=V1ObjectMeta(name="kubeflow"))
670671
)
671672
),
672673
)
673-
def test_check_if_namespace_exists_true(self, mock_core_client: Mock):
674+
def test_check_if_namespace_exists_true(
675+
self,
676+
mock_core_client: Mock,
677+
mock_kube_config: Mock,
678+
):
679+
mock_kube_config.return_value = None
674680
test_client = KubernetesClient()
675681
result = test_client.check_if_namespace_exists("kubeflow")
676682
self.assertTrue(result)
677683

684+
@patch("kubernetes.config.load_kube_config")
678685
@patch(
679-
"kubernetes.client.CoreV1Api",
686+
"kubernetes.client.CoreV1Api.read_namespace",
680687
side_effect=client.rest.ApiException(
681688
status=404,
682689
),
683690
)
684-
def test_check_if_namespace_exists_false(self, mock_core_client: Mock):
691+
def test_check_if_namespace_exists_false(
692+
self,
693+
mock_core_client: Mock,
694+
mock_kube_config: Mock,
695+
):
696+
mock_kube_config.return_value = None
685697
test_client = KubernetesClient()
686698
result = test_client.check_if_namespace_exists("abcdef")
687699
self.assertFalse(result)

test/unit_tests/test_job.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,18 @@ def test_list_job_when_subprocess_command_gives_exception(
258258
"Unexpected error happens when trying to list training job",
259259
result.output,
260260
)
261+
262+
@mock.patch("hyperpod_cli.clients.kubernetes_client.KubernetesClient.__new__")
263+
def test_list_job_when_namespace_not_exist(
264+
self,
265+
mock_kubernetes_client: mock.Mock,
266+
):
267+
mock_client_instance = mock_kubernetes_client.return_value
268+
mock_client_instance.check_if_namespace_exists.return_value = False
269+
result = self.runner.invoke(list_jobs, ["--namespace", "abcdef"])
270+
mock_client_instance.check_if_namespace_exists.assert_any_call("abcdef")
271+
self.assertEqual(result.exit_code, 1)
272+
self.assertIn("Namespace abcdef does not exist!", result.output)
261273

262274
@mock.patch("hyperpod_cli.service.list_pods.ListPods")
263275
@mock.patch("hyperpod_cli.service.list_pods.ListPods.list_pods_for_training_job")

0 commit comments

Comments
 (0)