Skip to content

Commit 6a8ba7c

Browse files
committed
Added unit tests for new authentication methods
1 parent c432462 commit 6a8ba7c

File tree

1 file changed

+36
-73
lines changed

1 file changed

+36
-73
lines changed

tests/unit_test.py

+36-73
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
parent = Path(__file__).resolve().parents[1]
2222
sys.path.append(str(parent) + "/src")
2323

24-
from kubernetes import client
24+
from kubernetes import client, config
2525
from codeflare_sdk.cluster.awload import AWManager
2626
from codeflare_sdk.cluster.cluster import (
2727
Cluster,
@@ -35,8 +35,8 @@
3535
)
3636
from codeflare_sdk.cluster.auth import (
3737
TokenAuthentication,
38-
PasswordUserAuthentication,
3938
Authentication,
39+
KubeConfigFileAuthentication
4040
)
4141
from codeflare_sdk.utils.pretty_print import (
4242
print_no_resources_found,
@@ -65,7 +65,6 @@
6565
)
6666

6767
import openshift
68-
from openshift import OpenShiftPythonException
6968
from openshift.selector import Selector
7069
import ray
7170
from torchx.specs import AppDryRunInfo, AppDef
@@ -89,27 +88,8 @@ def att_side_effect(self):
8988
return self.high_level_operation
9089

9190

92-
def att_side_effect_tls(self):
93-
if "--insecure-skip-tls-verify" in self.high_level_operation[1]:
94-
return self.high_level_operation
95-
else:
96-
raise OpenShiftPythonException(
97-
"The server uses a certificate signed by unknown authority"
98-
)
99-
100-
10191
def test_token_auth_creation():
10292
try:
103-
token_auth = TokenAuthentication()
104-
assert token_auth.token == None
105-
assert token_auth.server == None
106-
assert token_auth.skip_tls == False
107-
108-
token_auth = TokenAuthentication("token")
109-
assert token_auth.token == "token"
110-
assert token_auth.server == None
111-
assert token_auth.skip_tls == False
112-
11393
token_auth = TokenAuthentication("token", "server")
11494
assert token_auth.token == "token"
11595
assert token_auth.server == "server"
@@ -130,79 +110,62 @@ def test_token_auth_creation():
130110
assert token_auth.server == "server"
131111
assert token_auth.skip_tls == True
132112

113+
token_auth = TokenAuthentication(token="token", server="server", skip_tls=False)
114+
assert token_auth.token == "token"
115+
assert token_auth.server == "server"
116+
assert token_auth.skip_tls == False
117+
118+
token_auth = TokenAuthentication(token="token", server="server", skip_tls=False, ca_cert_path="path/to/cert")
119+
assert token_auth.token == "token"
120+
assert token_auth.server == "server"
121+
assert token_auth.skip_tls == False
122+
assert token_auth.ca_cert_path == "path/to/cert"
123+
133124
except Exception:
134125
assert 0 == 1
135126

136127

137128
def test_token_auth_login_logout(mocker):
138-
mocker.patch("openshift.invoke", side_effect=arg_side_effect)
139-
mock_res = mocker.patch.object(openshift.Result, "out")
140-
mock_res.side_effect = lambda: att_side_effect(fake_res)
129+
mocker.patch.object(client, 'ApiClient')
141130

142131
token_auth = TokenAuthentication(token="testtoken", server="testserver:6443")
143132
assert token_auth.login() == (
144-
"login",
145-
["--token=testtoken", "--server=testserver:6443"],
133+
"Logged into testserver:6443"
146134
)
147135
assert token_auth.logout() == (
148-
"logout",
149-
["--token=testtoken", "--server=testserver:6443"],
136+
"Successfully logged out of testserver:6443"
150137
)
151138

152139

153140
def test_token_auth_login_tls(mocker):
154-
mocker.patch("openshift.invoke", side_effect=arg_side_effect)
155-
mock_res = mocker.patch.object(openshift.Result, "out")
156-
mock_res.side_effect = lambda: att_side_effect_tls(fake_res)
157-
158-
# FIXME - Pytest mocker not allowing caught exception
159-
# token_auth = TokenAuthentication(token="testtoken", server="testserver")
160-
# assert token_auth.login() == "Error: certificate auth failure, please set `skip_tls=True` in TokenAuthentication"
141+
mocker.patch.object(client, 'ApiClient')
161142

162143
token_auth = TokenAuthentication(
163144
token="testtoken", server="testserver:6443", skip_tls=True
164145
)
165146
assert token_auth.login() == (
166-
"login",
167-
["--token=testtoken", "--server=testserver:6443", "--insecure-skip-tls-verify"],
147+
"Logged into testserver:6443"
148+
)
149+
token_auth = TokenAuthentication(
150+
token="testtoken", server="testserver:6443", skip_tls=False
151+
)
152+
assert token_auth.login() == (
153+
"Logged into testserver:6443"
154+
)
155+
token_auth = TokenAuthentication(
156+
token="testtoken", server="testserver:6443", skip_tls=False, ca_cert_path="path/to/cert"
157+
)
158+
assert token_auth.login() == (
159+
"Logged into testserver:6443"
168160
)
169161

162+
def test_load_kube_config(mocker):
163+
kube_config_auth = KubeConfigFileAuthentication()
164+
kube_config_auth.kube_config_path = '/path/to/your/config'
165+
mocker.patch.object(config, 'load_kube_config')
170166

171-
def test_passwd_auth_creation():
172-
try:
173-
passwd_auth = PasswordUserAuthentication()
174-
assert passwd_auth.username == None
175-
assert passwd_auth.password == None
176-
177-
passwd_auth = PasswordUserAuthentication("user")
178-
assert passwd_auth.username == "user"
179-
assert passwd_auth.password == None
180-
181-
passwd_auth = PasswordUserAuthentication("user", "passwd")
182-
assert passwd_auth.username == "user"
183-
assert passwd_auth.password == "passwd"
184-
185-
passwd_auth = PasswordUserAuthentication("user", password="passwd")
186-
assert passwd_auth.username == "user"
187-
assert passwd_auth.password == "passwd"
188-
189-
passwd_auth = PasswordUserAuthentication(username="user", password="passwd")
190-
assert passwd_auth.username == "user"
191-
assert passwd_auth.password == "passwd"
192-
193-
except Exception:
194-
assert 0 == 1
195-
196-
197-
def test_passwd_auth_login_logout(mocker):
198-
mocker.patch("openshift.invoke", side_effect=arg_side_effect)
199-
mocker.patch("openshift.login", side_effect=arg_side_effect)
200-
mock_res = mocker.patch.object(openshift.Result, "out")
201-
mock_res.side_effect = lambda: att_side_effect(fake_res)
202-
203-
token_auth = PasswordUserAuthentication(username="user", password="passwd")
204-
assert token_auth.login() == ("user", "passwd")
205-
assert token_auth.logout() == ("logout",)
167+
response = kube_config_auth.load_kube_config()
168+
assert response == "Loaded user config file at path %s"%kube_config_auth.kube_config_path
206169

207170

208171
def test_auth_coverage():

0 commit comments

Comments
 (0)