Skip to content

Commit f513e3c

Browse files
committed
add unit test for OAuth create
Signed-off-by: Kevin <[email protected]>
1 parent cf1fe4a commit f513e3c

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

src/codeflare_sdk/utils/openshift_oauth.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
from kubernetes import client
88

99

10+
def _get_api_host(api_client: client.ApiClient):
11+
return parse_url(api_client.configuration.host).host
12+
13+
1014
def create_openshift_oauth_objects(cluster_name, namespace):
1115
config_check()
1216
api_client = api_config_handler()
@@ -15,7 +19,7 @@ def create_openshift_oauth_objects(cluster_name, namespace):
1519
tls_secret_name = _gen_tls_secret_name(cluster_name)
1620
service_name = f"{cluster_name}-oauth"
1721
port_name = "oauth-proxy"
18-
host = parse_url(api_client.configuration.host).host
22+
host = _get_api_host(api_client)
1923

2024
# replace "^api" with the expected host
2125
host = f"{gen_dashboard_route_name(cluster_name)}-{namespace}.apps" + host.lstrip(

tests/unit_test.py

+51
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
config_check,
4343
api_config_handler,
4444
)
45+
from codeflare_sdk.utils.openshift_oauth import create_openshift_oauth_objects
4546
from codeflare_sdk.utils.pretty_print import (
4647
print_no_resources_found,
4748
print_app_wrappers_status,
@@ -2294,6 +2295,56 @@ def test_export_env():
22942295
)
22952296

22962297

2298+
# TODO add checks to make sure that when calling generate oauth objects that the fields that are expected to match do
2299+
def test_create_openshift_oauth(mocker: MockerFixture):
2300+
create_namespaced_service_account = MagicMock()
2301+
create_cluster_role_binding = MagicMock()
2302+
create_namespaced_service = MagicMock()
2303+
create_namespaced_ingress = MagicMock()
2304+
mocker.patch.object(
2305+
client.CoreV1Api,
2306+
"create_namespaced_service_account",
2307+
create_namespaced_service_account,
2308+
)
2309+
mocker.patch.object(
2310+
client.RbacAuthorizationV1Api,
2311+
"create_cluster_role_binding",
2312+
create_cluster_role_binding,
2313+
)
2314+
mocker.patch.object(
2315+
client.CoreV1Api, "create_namespaced_service", create_namespaced_service
2316+
)
2317+
mocker.patch.object(
2318+
client.NetworkingV1Api, "create_namespaced_ingress", create_namespaced_ingress
2319+
)
2320+
mocker.patch(
2321+
"codeflare_sdk.utils.openshift_oauth._get_api_host", return_value="foo.com"
2322+
)
2323+
create_openshift_oauth_objects("foo", "bar")
2324+
create_ns_sa_args = create_namespaced_service_account.call_args
2325+
create_crb_args = create_cluster_role_binding.call_args
2326+
create_ns_serv_args = create_namespaced_service.call_args
2327+
create_ns_ingress_args = create_namespaced_ingress.call_args
2328+
assert (
2329+
create_ns_sa_args.kwargs["namespace"] == create_ns_serv_args.kwargs["namespace"]
2330+
)
2331+
assert (
2332+
create_ns_serv_args.kwargs["namespace"]
2333+
== create_ns_ingress_args.kwargs["namespace"]
2334+
)
2335+
assert isinstance(create_ns_sa_args.kwargs["body"], client.V1ServiceAccount)
2336+
assert isinstance(create_crb_args.kwargs["body"], client.V1ClusterRoleBinding)
2337+
assert isinstance(create_ns_serv_args.kwargs["body"], client.V1Service)
2338+
assert isinstance(create_ns_ingress_args.kwargs["body"], client.V1Ingress)
2339+
assert (
2340+
create_ns_serv_args.kwargs["body"].spec.ports[0].name
2341+
== create_ns_ingress_args.kwargs["body"]
2342+
.spec.rules[0]
2343+
.http.paths[0]
2344+
.backend.service.port.name
2345+
)
2346+
2347+
22972348
# Make sure to always keep this function last
22982349
def test_cleanup():
22992350
os.remove("unit-test-cluster.yaml")

0 commit comments

Comments
 (0)