Skip to content
Merged
Show file tree
Hide file tree
Changes from 33 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
b6074c4
test: Add test to verify model registry access for different users
fege May 2, 2025
acb802b
ci: Merge branch 'main' of github.com:fege/opendatahub-tests into rba…
fege May 2, 2025
5ddf96a
change: Add user to a group to gain the access permission
fege May 2, 2025
3fe23e6
fix: typo in logs
fege May 2, 2025
6fd0843
fix: remove users from group at the end of the run, remove utils func…
fege May 5, 2025
bcbce95
fix: test adding a new group
fege May 6, 2025
736043b
change: add test to add single user
fege May 6, 2025
1d2db68
ci: Merge branch 'main' of github.com:fege/opendatahub-tests into rba…
fege May 6, 2025
a382fc3
fix: apply review comments
fege May 6, 2025
659323a
change: move creation of role to a fixture
fege May 7, 2025
3ee143e
fix: be sure that the user is in the group
fege May 7, 2025
7d60efe
ci: Merge branch 'main' of github.com:fege/opendatahub-tests into rba…
fege May 7, 2025
2c6a2d3
change: refactor the code
fege May 8, 2025
7e643ba
change: Add docstring
fege May 8, 2025
28b80f7
ci: Merge branch 'main' of github.com:fege/opendatahub-tests into rba…
fege May 8, 2025
79af41d
fix: correct usage of get_endpoint_from_mr_service
fege May 8, 2025
538666f
change: Create idp and users to use in the test run
fege May 19, 2025
2a1189d
change: use Group in the new_group utils
fege May 19, 2025
450a899
fix: use Namespace
fege May 19, 2025
b5ae7f7
fix: add pytest marker
fege May 19, 2025
593c693
ci: Merge branch 'main' of github.com:fege/opendatahub-tests into rba…
fege May 19, 2025
5cf69c9
fix: address review comments
fege May 19, 2025
a8d9014
ci: Merge branch 'main' of github.com:fege/opendatahub-tests into rba…
fege May 20, 2025
72260be
fix: remove no_collect mark and update tests
fege May 20, 2025
d172d9c
Merge branch 'main' into rbac_tests
dbasunag May 20, 2025
32fad04
fix: change group and idp functions
fege May 21, 2025
c90d66c
change: use wrapper functions and re-arrange the code in utils
fege May 22, 2025
d1067d4
change: use wrapper functions and re-arrange the code in utils
fege May 22, 2025
b3328b0
change: remove check
fege May 22, 2025
2099d45
ci: Merge branch 'main' of github.com:fege/opendatahub-tests into rba…
fege May 22, 2025
1d0d469
fix: address comment
fege May 22, 2025
c0ded9f
fix: adjust docstring
fege May 22, 2025
0f0b014
change: remove logs
fege May 22, 2025
e221ec2
ci: Merge branch 'main' of github.com:fege/opendatahub-tests into rba…
fege May 22, 2025
c6082f4
fix: change namespace
fege May 22, 2025
603a22b
fix: fix wronge merge resolution
fege May 22, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
86 changes: 83 additions & 3 deletions tests/model_registry/rbac/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@
import subprocess
import os
from typing import Generator, List, Dict, Any
from simple_logger.logger import get_logger

from ocp_resources.namespace import Namespace
from ocp_resources.service_account import ServiceAccount
from ocp_resources.role_binding import RoleBinding
from ocp_resources.role import Role
from ocp_resources.group import Group
from ocp_resources.resource import ResourceEditor
from kubernetes.dynamic import DynamicClient
from pyhelper_utils.shell import run_command
from tests.model_registry.utils import generate_random_name, generate_namespace_name
from simple_logger.logger import get_logger
from utilities.user_utils import create_test_idp, UserTestSession
from tests.model_registry.rbac.group_utils import create_group
from tests.model_registry.constants import MR_INSTANCE_NAME


Expand Down Expand Up @@ -89,6 +94,82 @@ def sa_token(service_account: ServiceAccount) -> str:
raise


@pytest.fixture(scope="function")
def add_user_to_group(
request: pytest.FixtureRequest,
admin_client: DynamicClient,
test_idp_user_session: UserTestSession,
) -> Generator[str, None, None]:
"""
Fixture to create a group and add a test user to it.
Uses create_group context manager to ensure proper cleanup.

Args:
request: The pytest request object containing the group name parameter
admin_client: The admin client for accessing the cluster
test_idp_user_session: The test user session containing user information

Yields:
str: The name of the created group
"""
group_name = request.param
with create_group(
admin_client=admin_client,
group_name=group_name,
users=[test_idp_user_session.username],
) as group_name:
yield group_name


@pytest.fixture(scope="function")
def model_registry_group_with_user(
request: pytest.FixtureRequest,
admin_client: DynamicClient,
test_idp_user_session: UserTestSession,
) -> Generator[Group, None, None]:
"""
Fixture to manage a test user in a specified group.
Adds the user to the group before the test, then removes them after.

Args:
request: The pytest request object containing the group name parameter
admin_client: The admin client for accessing the cluster
test_idp_user_session: The test user session containing user information

Yields:
Group: The group with the test user added
"""
group_name = request.param
group = Group(
client=admin_client,
name=group_name,
wait_for_resource=True,
)

# Add user to group
with ResourceEditor(
patches={
group: {
"metadata": {"name": group_name},
"users": [test_idp_user_session.username],
}
}
) as _:
LOGGER.info(f"Added user {test_idp_user_session.username} to {group_name} group")
yield group

Comment thread
dbasunag marked this conversation as resolved.

@pytest.fixture(scope="session")
def test_idp_user_session() -> Generator[UserTestSession, None, None]:
"""
Session-scoped fixture that creates a test IDP user and cleans it up after all tests.
Returns a UserTestSession object that contains all necessary credentials and contexts.
"""
with create_test_idp() as idp_session:
LOGGER.info(f"Created session test IDP user: {idp_session.username}")
yield idp_session


# --- RBAC Fixtures ---


Expand Down Expand Up @@ -128,7 +209,6 @@ def mr_access_role(
) as role:
LOGGER.info(f"Role {role.name} created successfully.")
yield role
LOGGER.info(f"Role {role.name} deletion initiated by context manager.")


@pytest.fixture(scope="function")
Expand Down Expand Up @@ -162,7 +242,7 @@ def mr_access_role_binding(
subjects_name=f"system:serviceaccounts:{sa_namespace.name}",
subjects_api_group="rbac.authorization.k8s.io", # This is the default apiGroup for Group kind
# Role reference parameters
role_ref_kind="Role",
role_ref_kind=mr_access_role.kind,
role_ref_name=mr_access_role.name,
label=binding_labels,
wait_for_resource=True,
Expand Down
37 changes: 37 additions & 0 deletions tests/model_registry/rbac/group_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from contextlib import contextmanager
from typing import Generator
from simple_logger.logger import get_logger
from kubernetes.dynamic import DynamicClient
from ocp_resources.group import Group

LOGGER = get_logger(name=__name__)


@contextmanager
def create_group(
admin_client: DynamicClient,
group_name: str,
users: list[str] | None = None,
wait_for_resource: bool = True,
) -> Generator[str, None, None]:
"""
Factory function to create an OpenShift group with optional users.
Uses context manager to ensure proper cleanup.

Args:
admin_client: The admin client to use for group operations
group_name: Name of the group to create
users: Optional list of usernames to add to the group
wait_for_resource: Whether to wait for the group to be ready

Yields:
The group name
"""
with Group(
client=admin_client,
name=group_name,
users=users or [],
wait_for_resource=wait_for_resource,
) as _:
LOGGER.info(f"Group {group_name} created successfully.")
yield group_name
Loading