Skip to content

Commit ce9d5bb

Browse files
authored
Testing/CI Architecture Update (#45)
* Testing/CI Architecture Update * Correct workflow command * Made source code accessible to tests * Reformatting
1 parent 82c7850 commit ce9d5bb

File tree

6 files changed

+146
-3
lines changed

6 files changed

+146
-3
lines changed

.github/workflows/python-app.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,12 @@ jobs:
2424
run: |
2525
python -m pip install --upgrade pip
2626
pip install pytest
27-
pip install pytest-dependency
2827
pip install pytest-mock
2928
pip install black==22.3.0
3029
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
3130
- name: Check formatting with black
3231
run: |
3332
black --check .
33+
- name: Test with pytest
34+
run: |
35+
pytest -v tests/unit_test.py

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
dist/
22
.python-version
3+
__pycache__/

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ Can be installed via `pip`: `pip install codeflare-sdk`
1313
## Development
1414

1515
For testing, make sure to have installed:
16-
- `pytest`
16+
- `pytest`, `pytest-mock` (can both be installed with `pip`)
1717
- The remaining dependencies located in `requirements.txt`
18+
- To run the unit tests, run `pytest -v tests/unit_test.py`)
19+
- Any new test functions/scripts can be added into the `tests` folder
1820

19-
NOTE: Self-contained unit/functional tests coming soon, will live in `tests` folder
21+
NOTE: Functional tests coming soon, will live in `tests/func_test.py`
2022

2123
For formatting:
2224
- Currently using black v22.3.0 for format checking

tests/test_clusters.py renamed to tests/demo_test.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
"""
16+
These were the old tests used during initial demo building, and they will soon be fully deprecated.
17+
"""
18+
1519
from codeflare_sdk.cluster.cluster import (
1620
list_all_clusters,
1721
list_all_queued,

tests/func_test.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Copyright 2022 IBM, Red Hat
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from pathlib import Path
16+
import sys
17+
18+
parent = Path(__file__).resolve().parents[1]
19+
sys.path.append(str(parent) + "/src")
20+
21+
# COMING SOON!

tests/unit_test.py

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
# Copyright 2022 IBM, Red Hat
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from pathlib import Path
16+
import sys
17+
18+
parent = Path(__file__).resolve().parents[1]
19+
sys.path.append(str(parent) + "/src")
20+
21+
from codeflare_sdk.cluster.cluster import Cluster, ClusterConfiguration
22+
from codeflare_sdk.cluster.auth import TokenAuthentication, PasswordUserAuthentication
23+
import openshift
24+
import pytest
25+
26+
27+
# For mocking openshift client results
28+
fake_res = openshift.Result("fake")
29+
30+
31+
def arg_side_effect(*args):
32+
fake_res.high_level_operation = args
33+
return fake_res
34+
35+
36+
def att_side_effect(self):
37+
return self.high_level_operation
38+
39+
40+
def test_token_auth_creation():
41+
try:
42+
token_auth = TokenAuthentication()
43+
assert token_auth.token == None
44+
assert token_auth.server == None
45+
46+
token_auth = TokenAuthentication("token")
47+
assert token_auth.token == "token"
48+
assert token_auth.server == None
49+
50+
token_auth = TokenAuthentication("token", "server")
51+
assert token_auth.token == "token"
52+
assert token_auth.server == "server"
53+
54+
token_auth = TokenAuthentication("token", server="server")
55+
assert token_auth.token == "token"
56+
assert token_auth.server == "server"
57+
58+
token_auth = TokenAuthentication(token="token", server="server")
59+
assert token_auth.token == "token"
60+
assert token_auth.server == "server"
61+
62+
except Exception:
63+
assert 0 == 1
64+
65+
66+
def test_token_auth_login_logout(mocker):
67+
mocker.patch("openshift.invoke", side_effect=arg_side_effect)
68+
mock_res = mocker.patch.object(openshift.Result, "out")
69+
mock_res.side_effect = lambda: att_side_effect(fake_res)
70+
71+
token_auth = TokenAuthentication(token="testtoken", server="testserver")
72+
assert token_auth.login() == (
73+
"login",
74+
["--token=testtoken", "--server=testserver:6443"],
75+
)
76+
assert token_auth.logout() == ("logout",)
77+
78+
79+
def test_passwd_auth_creation():
80+
try:
81+
passwd_auth = PasswordUserAuthentication()
82+
assert passwd_auth.username == None
83+
assert passwd_auth.password == None
84+
85+
passwd_auth = PasswordUserAuthentication("user")
86+
assert passwd_auth.username == "user"
87+
assert passwd_auth.password == None
88+
89+
passwd_auth = PasswordUserAuthentication("user", "passwd")
90+
assert passwd_auth.username == "user"
91+
assert passwd_auth.password == "passwd"
92+
93+
passwd_auth = PasswordUserAuthentication("user", password="passwd")
94+
assert passwd_auth.username == "user"
95+
assert passwd_auth.password == "passwd"
96+
97+
passwd_auth = PasswordUserAuthentication(username="user", password="passwd")
98+
assert passwd_auth.username == "user"
99+
assert passwd_auth.password == "passwd"
100+
101+
except Exception:
102+
assert 0 == 1
103+
104+
105+
def test_passwd_auth_login_logout(mocker):
106+
mocker.patch("openshift.invoke", side_effect=arg_side_effect)
107+
mocker.patch("openshift.login", side_effect=arg_side_effect)
108+
mock_res = mocker.patch.object(openshift.Result, "out")
109+
mock_res.side_effect = lambda: att_side_effect(fake_res)
110+
111+
token_auth = PasswordUserAuthentication(username="user", password="passwd")
112+
assert token_auth.login() == ("user", "passwd")
113+
assert token_auth.logout() == ("logout",)

0 commit comments

Comments
 (0)