Skip to content

Commit 6544652

Browse files
committed
unit-tests: add unit tests for get_oc_whoami_username
Signed-off-by: rkishner <rkishner@redhat.com>
1 parent d46261a commit 6544652

File tree

1 file changed

+89
-1
lines changed

1 file changed

+89
-1
lines changed

utilities/unittests/test_cluster.py

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44

55
from unittest.mock import MagicMock, patch
66

7-
from utilities.cluster import cache_admin_client
7+
import pytest
8+
from timeout_sampler import TimeoutExpiredError
9+
10+
from utilities.cluster import cache_admin_client, get_oc_whoami_username
811

912

1013
class TestCacheAdminClient:
@@ -99,3 +102,88 @@ def test_cache_admin_client_cache_info(self, mock_get_client):
99102
info = cache_admin_client.cache_info()
100103
assert info.hits == 2
101104
assert info.misses == 1
105+
106+
107+
class TestGetOcWhoamiUsername:
108+
"""Test cases for get_oc_whoami_username function"""
109+
110+
@patch("utilities.cluster.TimeoutSampler")
111+
def test_returns_username_on_first_attempt(self, mock_sampler):
112+
"""Test that username is returned immediately when oc whoami succeeds"""
113+
mock_sampler.return_value = ["system:admin"]
114+
115+
result = get_oc_whoami_username()
116+
117+
assert result == "system:admin"
118+
119+
@patch("utilities.cluster.TimeoutSampler")
120+
def test_returns_username_after_retry(self, mock_sampler):
121+
"""Test that username is returned after an initial empty result"""
122+
mock_sampler.return_value = ["", "system:admin"]
123+
124+
result = get_oc_whoami_username()
125+
126+
assert result == "system:admin"
127+
128+
@patch("utilities.cluster.TimeoutSampler")
129+
def test_raises_on_timeout(self, mock_sampler):
130+
"""Test that TimeoutExpiredError propagates when oc whoami never succeeds"""
131+
mock_sampler.side_effect = TimeoutExpiredError("Timeout")
132+
133+
with pytest.raises(TimeoutExpiredError):
134+
get_oc_whoami_username()
135+
136+
@patch("utilities.cluster.TimeoutSampler")
137+
def test_sampler_called_with_correct_args(self, mock_sampler):
138+
"""Test that TimeoutSampler is constructed with the expected parameters"""
139+
mock_sampler.return_value = ["kubeadmin"]
140+
141+
get_oc_whoami_username(wait_timeout=60, sleep=5)
142+
143+
call_kwargs = mock_sampler.call_args[1]
144+
assert call_kwargs["wait_timeout"] == 60
145+
assert call_kwargs["sleep"] == 5
146+
147+
@patch("utilities.cluster.TimeoutSampler")
148+
@patch("utilities.cluster.run_command")
149+
def test_whoami_inner_function_success(self, mock_run_command, mock_sampler):
150+
"""Test that _whoami calls run_command and returns stripped stdout on success"""
151+
mock_run_command.return_value = (True, "system:admin\n", "")
152+
153+
def call_func_once(*args, **kwargs):
154+
return [kwargs["func"]()]
155+
156+
mock_sampler.side_effect = call_func_once
157+
158+
result = get_oc_whoami_username()
159+
160+
assert result == "system:admin"
161+
mock_run_command.assert_called_once_with(
162+
command=["oc", "whoami"],
163+
capture_output=True,
164+
check=False,
165+
timeout=3,
166+
)
167+
168+
@patch("utilities.cluster.TimeoutSampler")
169+
@patch("utilities.cluster.run_command")
170+
def test_whoami_inner_function_failure_returns_empty(self, mock_run_command, mock_sampler):
171+
"""Test that _whoami returns empty string when run_command fails, then retries"""
172+
mock_run_command.side_effect = [(False, "", "error"), (True, "kubeadmin\n", "")]
173+
174+
call_count = 0
175+
176+
def call_func_twice(*args, **kwargs):
177+
nonlocal call_count
178+
results = []
179+
for _ in range(2):
180+
results.append(kwargs["func"]())
181+
call_count = len(results)
182+
return results
183+
184+
mock_sampler.side_effect = call_func_twice
185+
186+
result = get_oc_whoami_username()
187+
188+
assert result == "kubeadmin"
189+
assert mock_run_command.call_count == 2

0 commit comments

Comments
 (0)