Skip to content

Commit b8f73c2

Browse files
dbasunaglugi0
andauthored
updates to MR related tests to ensure they work against BYOIDC (#862)
* updates to MR related tests to ensure they work against BYOIDC * address tox failure * Move the byoidc secret to a different ns and address coderabbit comments --------- Co-authored-by: Luca Giorgi <lgiorgi@redhat.com>
1 parent 6be5a97 commit b8f73c2

File tree

10 files changed

+296
-144
lines changed

10 files changed

+296
-144
lines changed

tests/conftest.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
login_with_user_password,
4646
get_openshift_token,
4747
download_oc_console_cli,
48+
get_cluster_authentication,
4849
)
4950
from utilities.constants import (
5051
AcceleratorType,
@@ -379,10 +380,7 @@ def kubconfig_filepath() -> str:
379380

380381
@pytest.fixture(scope="session")
381382
def cluster_authentication(admin_client: DynamicClient) -> Authentication | None:
382-
auth = Authentication(client=admin_client, name="cluster")
383-
if auth.exists:
384-
return auth
385-
return None
383+
return get_cluster_authentication(admin_client=admin_client)
386384

387385

388386
@pytest.fixture(scope="session")

tests/model_registry/conftest.py

Lines changed: 73 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,12 @@
3535
from model_registry.types import RegisteredModel
3636

3737
from tests.model_registry.rbac.utils import wait_for_oauth_openshift_deployment
38-
from tests.model_registry.utils import generate_namespace_name, get_rest_headers, wait_for_default_resource_cleanedup
38+
from tests.model_registry.utils import (
39+
generate_namespace_name,
40+
get_rest_headers,
41+
wait_for_default_resource_cleanedup,
42+
get_byoidc_user_credentials,
43+
)
3944
from utilities.general import generate_random_name, wait_for_pods_running
4045

4146
from tests.model_registry.constants import (
@@ -486,13 +491,14 @@ def test_idp_user(
486491
original_user: str,
487492
api_server_url: str,
488493
is_byoidc: bool,
489-
) -> Generator[UserTestSession, None, None]:
494+
) -> Generator[UserTestSession | None, None, None]:
490495
"""
491496
Session-scoped fixture that creates a test IDP user and cleans it up after all tests.
492497
Returns a UserTestSession object that contains all necessary credentials and contexts.
493498
"""
494499
if is_byoidc:
495-
pytest.skip("Working on OIDC support for tests that use test_idp_user")
500+
# For BYOIDC, we would be using a preconfigured group and username for actual api calls.
501+
yield
496502
else:
497503
user_credentials_rbac = request.getfixturevalue(argname="user_credentials_rbac")
498504
_ = request.getfixturevalue(argname="created_htpasswd_secret")
@@ -544,67 +550,84 @@ def original_user() -> str:
544550

545551
@pytest.fixture(scope="module")
546552
def created_htpasswd_secret(
547-
original_user: str, user_credentials_rbac: dict[str, str]
548-
) -> Generator[UserTestSession, None, None]:
553+
is_byoidc: bool, original_user: str, user_credentials_rbac: dict[str, str]
554+
) -> Generator[UserTestSession | None, None, None]:
549555
"""
550556
Session-scoped fixture that creates a test IDP user and cleans it up after all tests.
551557
Returns a UserTestSession object that contains all necessary credentials and contexts.
552558
"""
559+
if is_byoidc:
560+
yield
553561

554-
temp_path, htpasswd_b64 = create_htpasswd_file(
555-
username=user_credentials_rbac["username"], password=user_credentials_rbac["password"]
556-
)
557-
try:
558-
LOGGER.info(f"Creating secret {user_credentials_rbac['secret_name']} in openshift-config namespace")
559-
with Secret(
560-
name=user_credentials_rbac["secret_name"],
561-
namespace="openshift-config",
562-
htpasswd=htpasswd_b64,
563-
type="Opaque",
564-
wait_for_resource=True,
565-
) as secret:
566-
yield secret
567-
finally:
568-
# Clean up the temporary file
569-
temp_path.unlink(missing_ok=True)
562+
else:
563+
temp_path, htpasswd_b64 = create_htpasswd_file(
564+
username=user_credentials_rbac["username"], password=user_credentials_rbac["password"]
565+
)
566+
try:
567+
LOGGER.info(f"Creating secret {user_credentials_rbac['secret_name']} in openshift-config namespace")
568+
with Secret(
569+
name=user_credentials_rbac["secret_name"],
570+
namespace="openshift-config",
571+
htpasswd=htpasswd_b64,
572+
type="Opaque",
573+
wait_for_resource=True,
574+
) as secret:
575+
yield secret
576+
finally:
577+
# Clean up the temporary file
578+
temp_path.unlink(missing_ok=True)
570579

571580

572581
@pytest.fixture(scope="module")
573582
def updated_oauth_config(
574-
admin_client: DynamicClient, original_user: str, user_credentials_rbac: dict[str, str]
583+
is_byoidc: bool, admin_client: DynamicClient, original_user: str, user_credentials_rbac: dict[str, str]
575584
) -> Generator[Any, None, None]:
576-
# Get current providers and add the new one
577-
oauth = OAuth(name="cluster")
578-
identity_providers = oauth.instance.spec.identityProviders
579-
580-
new_idp = {
581-
"name": user_credentials_rbac["idp_name"],
582-
"mappingMethod": "claim",
583-
"type": "HTPasswd",
584-
"htpasswd": {"fileData": {"name": user_credentials_rbac["secret_name"]}},
585-
}
586-
updated_providers = identity_providers + [new_idp]
585+
if is_byoidc:
586+
yield
587+
else:
588+
# Get current providers and add the new one
589+
oauth = OAuth(name="cluster")
590+
identity_providers = oauth.instance.spec.identityProviders
591+
592+
new_idp = {
593+
"name": user_credentials_rbac["idp_name"],
594+
"mappingMethod": "claim",
595+
"type": "HTPasswd",
596+
"htpasswd": {"fileData": {"name": user_credentials_rbac["secret_name"]}},
597+
}
598+
updated_providers = identity_providers + [new_idp]
587599

588-
LOGGER.info("Updating OAuth")
589-
identity_providers_patch = ResourceEditor(patches={oauth: {"spec": {"identityProviders": updated_providers}}})
590-
identity_providers_patch.update(backup_resources=True)
591-
# Wait for OAuth server to be ready
592-
wait_for_oauth_openshift_deployment()
593-
LOGGER.info(f"Added IDP {user_credentials_rbac['idp_name']} to OAuth configuration")
594-
yield
595-
identity_providers_patch.restore()
596-
wait_for_oauth_openshift_deployment()
600+
LOGGER.info("Updating OAuth")
601+
identity_providers_patch = ResourceEditor(patches={oauth: {"spec": {"identityProviders": updated_providers}}})
602+
identity_providers_patch.update(backup_resources=True)
603+
# Wait for OAuth server to be ready
604+
wait_for_oauth_openshift_deployment()
605+
LOGGER.info(f"Added IDP {user_credentials_rbac['idp_name']} to OAuth configuration")
606+
yield
607+
identity_providers_patch.restore()
608+
wait_for_oauth_openshift_deployment()
597609

598610

599611
@pytest.fixture(scope="module")
600-
def user_credentials_rbac() -> dict[str, str]:
601-
random_str = generate_random_name()
602-
return {
603-
"username": f"test-user-{random_str}",
604-
"password": f"test-password-{random_str}",
605-
"idp_name": f"test-htpasswd-idp-{random_str}",
606-
"secret_name": f"test-htpasswd-secret-{random_str}",
607-
}
612+
def user_credentials_rbac(
613+
is_byoidc: bool,
614+
) -> dict[str, str]:
615+
if is_byoidc:
616+
byoidc_creds = get_byoidc_user_credentials(username="mr-non-admin")
617+
return {
618+
"username": byoidc_creds["username"],
619+
"password": byoidc_creds["password"],
620+
"idp_name": "byoidc",
621+
"secret_name": None,
622+
}
623+
else:
624+
random_str = generate_random_name()
625+
return {
626+
"username": f"test-user-{random_str}",
627+
"password": f"test-password-{random_str}",
628+
"idp_name": f"test-htpasswd-idp-{random_str}",
629+
"secret_name": f"test-htpasswd-secret-{random_str}",
630+
}
608631

609632

610633
@pytest.fixture(scope="class")

tests/model_registry/model_catalog/conftest.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525
wait_for_model_catalog_api,
2626
execute_get_command,
2727
get_model_str,
28+
get_mr_user_token,
2829
)
29-
from utilities.infra import get_openshift_token, create_inference_token
30-
from utilities.user_utils import UserTestSession
30+
from utilities.infra import get_openshift_token, create_inference_token, login_with_user_password
3131

3232

3333
LOGGER = get_logger(name=__name__)
@@ -93,10 +93,12 @@ def update_configmap_data_add_model(
9393

9494
@pytest.fixture(scope="class")
9595
def user_token_for_api_calls(
96+
is_byoidc: bool,
97+
admin_client: DynamicClient,
9698
request: pytest.FixtureRequest,
9799
original_user: str,
98100
api_server_url: str,
99-
test_idp_user: UserTestSession,
101+
user_credentials_rbac: dict[str, str],
100102
service_account: ServiceAccount,
101103
) -> Generator[str, None, None]:
102104
param = getattr(request, "param", {})
@@ -106,8 +108,20 @@ def user_token_for_api_calls(
106108
LOGGER.info("Logging in as admin user")
107109
yield get_openshift_token()
108110
elif user == "test":
109-
# TODO: implement byoidc check in get_openshift_token
110-
yield get_openshift_token()
111+
if not is_byoidc:
112+
login_with_user_password(
113+
api_address=api_server_url,
114+
user=user_credentials_rbac["username"],
115+
password=user_credentials_rbac["password"],
116+
)
117+
yield get_openshift_token()
118+
LOGGER.info(f"Logging in as {original_user}")
119+
login_with_user_password(
120+
api_address=api_server_url,
121+
user=original_user,
122+
)
123+
else:
124+
yield get_mr_user_token(admin_client=admin_client, user_credentials_rbac=user_credentials_rbac)
111125
elif user == "sa_user":
112126
yield create_inference_token(service_account)
113127
else:

tests/model_registry/rbac/conftest.py

Lines changed: 49 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ def add_user_to_group(
5757

5858
@pytest.fixture(scope="function")
5959
def model_registry_group_with_user(
60+
is_byoidc: bool,
6061
admin_client: DynamicClient,
6162
test_idp_user: UserTestSession,
6263
) -> Generator[Group, None, None]:
@@ -71,24 +72,28 @@ def model_registry_group_with_user(
7172
Yields:
7273
Group: The group with the test user added
7374
"""
74-
group_name = f"{MR_INSTANCE_NAME}-users"
75-
group = Group(
76-
client=admin_client,
77-
name=group_name,
78-
wait_for_resource=True,
79-
)
75+
if is_byoidc:
76+
# this is no op. byoidc already has a group with user model-registry-user
77+
yield
78+
else:
79+
group_name = f"{MR_INSTANCE_NAME}-users"
80+
group = Group(
81+
client=admin_client,
82+
name=group_name,
83+
wait_for_resource=True,
84+
)
8085

81-
# Add user to group
82-
with ResourceEditor(
83-
patches={
84-
group: {
85-
"metadata": {"name": group_name},
86-
"users": [test_idp_user.username],
86+
# Add user to group
87+
with ResourceEditor(
88+
patches={
89+
group: {
90+
"metadata": {"name": group_name},
91+
"users": [test_idp_user.username],
92+
}
8793
}
88-
}
89-
) as _:
90-
LOGGER.info(f"Added user {test_idp_user.username} to {group_name} group")
91-
yield group
94+
) as _:
95+
LOGGER.info(f"Added user {test_idp_user.username} to {group_name} group")
96+
yield group
9297

9398

9499
@pytest.fixture(scope="function")
@@ -111,18 +116,22 @@ def created_role_binding_group(
111116

112117
@pytest.fixture(scope="function")
113118
def created_role_binding_user(
119+
is_byoidc: bool,
114120
admin_client: DynamicClient,
115121
model_registry_namespace: str,
116122
mr_access_role: Role,
117-
test_idp_user: UserTestSession,
123+
user_credentials_rbac: dict[str, str],
118124
) -> Generator[RoleBinding, None, None]:
125+
if is_byoidc:
126+
user_credentials_rbac["username"] = "mr-non-admin"
127+
LOGGER.info(f"Using user {user_credentials_rbac['username']}")
119128
yield from create_role_binding(
120129
admin_client=admin_client,
121130
model_registry_namespace=model_registry_namespace,
122131
name="test-model-registry-access",
123132
mr_access_role=mr_access_role,
124133
subjects_kind="User",
125-
subjects_name=test_idp_user.username,
134+
subjects_name=user_credentials_rbac["username"],
126135
)
127136

128137

@@ -223,17 +232,26 @@ def model_registry_instance_parametrized(
223232

224233
@pytest.fixture()
225234
def login_as_test_user(
226-
api_server_url: str, original_user: str, test_idp_user: UserTestSession
235+
is_byoidc: bool, api_server_url: str, original_user: str, test_idp_user: UserTestSession
227236
) -> Generator[None, None, None]:
228-
LOGGER.info(f"Logging in as {test_idp_user.username}")
229-
login_with_user_password(
230-
api_address=api_server_url,
231-
user=test_idp_user.username,
232-
password=test_idp_user.password,
233-
)
234-
yield
235-
LOGGER.info(f"Logging in as {original_user}")
236-
login_with_user_password(
237-
api_address=api_server_url,
238-
user=original_user,
239-
)
237+
if is_byoidc:
238+
yield
239+
else:
240+
LOGGER.info(f"Logging in as {test_idp_user.username}")
241+
login_with_user_password(
242+
api_address=api_server_url,
243+
user=test_idp_user.username,
244+
password=test_idp_user.password,
245+
)
246+
yield
247+
LOGGER.info(f"Logging in as {original_user}")
248+
login_with_user_password(
249+
api_address=api_server_url,
250+
user=original_user,
251+
)
252+
253+
254+
@pytest.fixture()
255+
def skip_test_on_byoidc(is_byoidc: bool) -> None:
256+
if is_byoidc:
257+
pytest.skip(reason="This test is meant to skip on byoidc.")

tests/model_registry/rbac/multiple_instance_utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33

44
from tests.model_registry.constants import (
55
MODEL_REGISTRY_DB_SECRET_STR_DATA,
6-
MR_INSTANCE_NAME,
76
DB_BASE_RESOURCES_NAME,
87
NUM_MR_INSTANCES,
98
MODEL_REGISTRY_DB_SECRET_ANNOTATIONS,
109
OAUTH_PROXY_CONFIG_DICT,
10+
MR_INSTANCE_BASE_NAME,
1111
)
1212
from tests.model_registry.utils import (
1313
get_model_registry_db_label_dict,
@@ -76,9 +76,9 @@
7676

7777
model_registry_instance_params = [
7878
{
79-
"name": f"{MR_INSTANCE_NAME}{index}",
79+
"name": f"{MR_INSTANCE_BASE_NAME}{index}",
8080
"namespace": ns_name,
81-
"label": get_mr_standard_labels(resource_name=f"{MR_INSTANCE_NAME}{index}"),
81+
"label": get_mr_standard_labels(resource_name=f"{MR_INSTANCE_BASE_NAME}{index}"),
8282
"grpc": {},
8383
"rest": {},
8484
"mysql": get_mysql_config(base_name=f"{DB_BASE_RESOURCES_NAME}{index}", namespace=ns_name, db_backend="mysql"),

0 commit comments

Comments
 (0)