Skip to content

Commit e945268

Browse files
committed
bl only print logs for kueue specific pods + unit tests
1 parent c38baab commit e945268

File tree

3 files changed

+71
-4
lines changed

3 files changed

+71
-4
lines changed

batchtools/bl.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from .basecommand import Command
99
from .basecommand import SubParserFactory
1010
from .helpers import pretty_print
11+
from .helpers import is_kueue_managed_pod
1112

1213

1314
class LogsCommandArgs(argparse.Namespace):
@@ -63,10 +64,15 @@ def run(args: argparse.Namespace):
6364
print(pretty_print(pod_dict[name]))
6465

6566
else:
66-
# case where user provides no args, print logs for all pods
67+
printed_any = False
6768
for name, pod in pod_dict.items():
68-
print(f"\nLogs for {name}:\n{'-' * 40}")
69-
print(pretty_print(pod))
69+
if is_kueue_managed_pod(pod):
70+
printed_any = True
71+
print(f"\nLogs for {name}:\n{'-' * 40}")
72+
print(pretty_print(pod))
73+
74+
if not printed_any:
75+
print("No Kueue-managed pods found")
7076

7177
except oc.OpenShiftPythonException as e:
7278
sys.exit(f"Error occurred while retrieving logs: {e}")

batchtools/helpers.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,18 @@ def is_kueue_managed_job(job_obj) -> bool:
3737
return True
3838
except Exception:
3939
return False
40+
41+
42+
def is_kueue_managed_pod(pod) -> bool:
43+
try:
44+
owners = getattr(pod.model.metadata, "ownerReferences", []) or []
45+
job_owner = next((o for o in owners if o.kind == "Job"), None)
46+
if not job_owner:
47+
return False
48+
49+
job_name = job_owner.name
50+
job_obj = oc.selector(f"job/{job_name}").object()
51+
return is_kueue_managed_job(job_obj)
52+
53+
except Exception:
54+
return False

tests/test_bl.py

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import pytest
22
from unittest import mock
33
from contextlib import contextmanager
4+
import openshift_client as oc
45

56
import argparse
67
from typing import Any
@@ -41,7 +42,10 @@ def args() -> argparse.Namespace:
4142

4243
@contextmanager
4344
def patch_pods_selector(pods: list[Any]):
44-
with mock.patch("openshift_client.selector") as mock_selector:
45+
with (
46+
mock.patch("openshift_client.selector") as mock_selector,
47+
mock.patch("batchtools.bl.is_kueue_managed_pod", return_value=True),
48+
):
4549
mock_result = mock.Mock(name="result")
4650
mock_result.objects.return_value = pods
4751
mock_selector.return_value = mock_result
@@ -75,3 +79,45 @@ def test_get_logs_selected(args: argparse.Namespace, pods: list[Any], capsys):
7579
captured = capsys.readouterr()
7680
assert "Logs for pod1" in captured.out
7781
assert "Logs for pod2" not in captured.out
82+
83+
84+
def test_no_kueue_managed_pods(args: argparse.Namespace, pods: list[Any], capsys):
85+
with (
86+
patch_pods_selector(pods),
87+
mock.patch("batchtools.bl.is_kueue_managed_pod", return_value=False),
88+
):
89+
args.pod_names = []
90+
LogsCommand.run(args)
91+
captured = capsys.readouterr()
92+
assert "No Kueue-managed pods found" in captured.out
93+
94+
95+
def test_invalid_pod_name(args: argparse.Namespace, pods: list[Any], capsys):
96+
with patch_pods_selector(pods):
97+
args.pod_names = ["pod1", "does-not-exist"]
98+
LogsCommand.run(args)
99+
captured = capsys.readouterr()
100+
101+
# Valid pod still prints logs
102+
assert "Logs for pod1" in captured.out
103+
# Invalid pod hits the error path
104+
assert (
105+
"does-not-exist is not a valid pod. Logs cannot be retrieved."
106+
in captured.out
107+
)
108+
109+
110+
def test_selector_exception_exits_cleanly(args: argparse.Namespace):
111+
# Make openshift_client.selector raise the same exception type used in bl.py
112+
with mock.patch("openshift_client.selector") as mock_selector:
113+
mock_selector.side_effect = oc.OpenShiftPythonException(
114+
"test exception",
115+
mock.Mock(),
116+
)
117+
118+
with pytest.raises(SystemExit) as excinfo:
119+
LogsCommand.run(args)
120+
121+
message = str(excinfo.value)
122+
assert "Error occurred while retrieving logs:" in message
123+
assert "test exception" in message

0 commit comments

Comments
 (0)