|
1 | 1 | import pytest |
2 | 2 | from unittest import mock |
3 | 3 | from contextlib import contextmanager |
| 4 | +import openshift_client as oc |
4 | 5 |
|
5 | 6 | import argparse |
6 | 7 | from typing import Any |
@@ -41,7 +42,10 @@ def args() -> argparse.Namespace: |
41 | 42 |
|
42 | 43 | @contextmanager |
43 | 44 | 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 | + ): |
45 | 49 | mock_result = mock.Mock(name="result") |
46 | 50 | mock_result.objects.return_value = pods |
47 | 51 | mock_selector.return_value = mock_result |
@@ -75,3 +79,45 @@ def test_get_logs_selected(args: argparse.Namespace, pods: list[Any], capsys): |
75 | 79 | captured = capsys.readouterr() |
76 | 80 | assert "Logs for pod1" in captured.out |
77 | 81 | 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