Skip to content

Commit 16d23d7

Browse files
Bobbins228openshift-merge-robot
authored andcommitted
Added unit tests for auth methods
1 parent 438da28 commit 16d23d7

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

src/codeflare_sdk/cluster/auth.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,12 +167,12 @@ def config_check() -> str:
167167
if os.path.isfile("%s/.kube/config" % home_directory):
168168
try:
169169
config.load_kube_config()
170-
except Exception as e:
170+
except Exception as e: # pragma: no cover
171171
print(e)
172172
elif "KUBERNETES_PORT" in os.environ:
173173
try:
174174
config.load_incluster_config()
175-
except Exception as e:
175+
except Exception as e: # pragma: no cover
176176
print(e)
177177
else:
178178
raise PermissionError(

tests/unit_test.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
TokenAuthentication,
3838
Authentication,
3939
KubeConfigFileAuthentication,
40+
config_check,
41+
api_config_handler,
4042
)
4143
from codeflare_sdk.utils.pretty_print import (
4244
print_no_resources_found,
@@ -150,6 +152,46 @@ def test_token_auth_login_tls(mocker):
150152
assert token_auth.login() == ("Logged into testserver:6443")
151153

152154

155+
def test_config_check_no_config_file(mocker):
156+
mocker.patch("os.path.expanduser", return_value="/mock/home/directory")
157+
mocker.patch("os.path.isfile", return_value=False)
158+
mocker.patch("codeflare_sdk.cluster.auth.config_path", None)
159+
mocker.patch("codeflare_sdk.cluster.auth.api_client", None)
160+
161+
with pytest.raises(PermissionError) as e:
162+
config_check()
163+
164+
165+
def test_config_check_with_incluster_config(mocker):
166+
mocker.patch("os.path.expanduser", return_value="/mock/home/directory")
167+
mocker.patch("os.path.isfile", return_value=False)
168+
mocker.patch.dict(os.environ, {"KUBERNETES_PORT": "number"})
169+
mocker.patch("kubernetes.config.load_incluster_config", side_effect=None)
170+
mocker.patch("codeflare_sdk.cluster.auth.config_path", None)
171+
mocker.patch("codeflare_sdk.cluster.auth.api_client", None)
172+
173+
result = config_check()
174+
assert result == None
175+
176+
177+
def test_config_check_with_existing_config_file(mocker):
178+
mocker.patch("os.path.expanduser", return_value="/mock/home/directory")
179+
mocker.patch("os.path.isfile", return_value=True)
180+
mocker.patch("kubernetes.config.load_kube_config", side_effect=None)
181+
mocker.patch("codeflare_sdk.cluster.auth.config_path", None)
182+
mocker.patch("codeflare_sdk.cluster.auth.api_client", None)
183+
184+
result = config_check()
185+
assert result == None
186+
187+
188+
def test_config_check_with_config_path_and_no_api_client(mocker):
189+
mocker.patch("codeflare_sdk.cluster.auth.config_path", "/mock/config/path")
190+
mocker.patch("codeflare_sdk.cluster.auth.api_client", None)
191+
result = config_check()
192+
assert result == "/mock/config/path"
193+
194+
153195
def test_load_kube_config(mocker):
154196
mocker.patch.object(config, "load_kube_config")
155197
kube_config_auth = KubeConfigFileAuthentication(
@@ -162,6 +204,10 @@ def test_load_kube_config(mocker):
162204
== "Loaded user config file at path %s" % kube_config_auth.kube_config_path
163205
)
164206

207+
kube_config_auth = KubeConfigFileAuthentication(kube_config_path=None)
208+
response = kube_config_auth.load_kube_config()
209+
assert response == "Please specify a config file path"
210+
165211

166212
def test_auth_coverage():
167213
abstract = Authentication()

0 commit comments

Comments
 (0)