|
3 | 3 | import subprocess |
4 | 4 | import os |
5 | 5 | from typing import Generator, List, Dict, Any |
| 6 | +from simple_logger.logger import get_logger |
| 7 | + |
6 | 8 | from ocp_resources.namespace import Namespace |
7 | 9 | from ocp_resources.service_account import ServiceAccount |
8 | 10 | from ocp_resources.role_binding import RoleBinding |
9 | 11 | from ocp_resources.role import Role |
| 12 | +from ocp_resources.group import Group |
| 13 | +from ocp_resources.resource import ResourceEditor |
10 | 14 | from kubernetes.dynamic import DynamicClient |
11 | 15 | from pyhelper_utils.shell import run_command |
12 | 16 | from tests.model_registry.utils import generate_random_name, generate_namespace_name |
13 | | -from simple_logger.logger import get_logger |
| 17 | +from utilities.user_utils import create_test_idp, UserTestSession |
| 18 | +from tests.model_registry.rbac.group_utils import create_group |
14 | 19 | from tests.model_registry.constants import MR_INSTANCE_NAME |
15 | 20 |
|
16 | 21 |
|
@@ -89,6 +94,82 @@ def sa_token(service_account: ServiceAccount) -> str: |
89 | 94 | raise |
90 | 95 |
|
91 | 96 |
|
| 97 | +@pytest.fixture(scope="function") |
| 98 | +def add_user_to_group( |
| 99 | + request: pytest.FixtureRequest, |
| 100 | + admin_client: DynamicClient, |
| 101 | + test_idp_user_session: UserTestSession, |
| 102 | +) -> Generator[str, None, None]: |
| 103 | + """ |
| 104 | + Fixture to create a group and add a test user to it. |
| 105 | + Uses create_group context manager to ensure proper cleanup. |
| 106 | +
|
| 107 | + Args: |
| 108 | + request: The pytest request object containing the group name parameter |
| 109 | + admin_client: The admin client for accessing the cluster |
| 110 | + test_idp_user_session: The test user session containing user information |
| 111 | +
|
| 112 | + Yields: |
| 113 | + str: The name of the created group |
| 114 | + """ |
| 115 | + group_name = request.param |
| 116 | + with create_group( |
| 117 | + admin_client=admin_client, |
| 118 | + group_name=group_name, |
| 119 | + users=[test_idp_user_session.username], |
| 120 | + ) as group_name: |
| 121 | + yield group_name |
| 122 | + |
| 123 | + |
| 124 | +@pytest.fixture(scope="function") |
| 125 | +def model_registry_group_with_user( |
| 126 | + request: pytest.FixtureRequest, |
| 127 | + admin_client: DynamicClient, |
| 128 | + test_idp_user_session: UserTestSession, |
| 129 | +) -> Generator[Group, None, None]: |
| 130 | + """ |
| 131 | + Fixture to manage a test user in a specified group. |
| 132 | + Adds the user to the group before the test, then removes them after. |
| 133 | +
|
| 134 | + Args: |
| 135 | + request: The pytest request object containing the group name parameter |
| 136 | + admin_client: The admin client for accessing the cluster |
| 137 | + test_idp_user_session: The test user session containing user information |
| 138 | +
|
| 139 | + Yields: |
| 140 | + Group: The group with the test user added |
| 141 | + """ |
| 142 | + group_name = request.param |
| 143 | + group = Group( |
| 144 | + client=admin_client, |
| 145 | + name=group_name, |
| 146 | + wait_for_resource=True, |
| 147 | + ) |
| 148 | + |
| 149 | + # Add user to group |
| 150 | + with ResourceEditor( |
| 151 | + patches={ |
| 152 | + group: { |
| 153 | + "metadata": {"name": group_name}, |
| 154 | + "users": [test_idp_user_session.username], |
| 155 | + } |
| 156 | + } |
| 157 | + ) as _: |
| 158 | + LOGGER.info(f"Added user {test_idp_user_session.username} to {group_name} group") |
| 159 | + yield group |
| 160 | + |
| 161 | + |
| 162 | +@pytest.fixture(scope="session") |
| 163 | +def test_idp_user_session() -> Generator[UserTestSession, None, None]: |
| 164 | + """ |
| 165 | + Session-scoped fixture that creates a test IDP user and cleans it up after all tests. |
| 166 | + Returns a UserTestSession object that contains all necessary credentials and contexts. |
| 167 | + """ |
| 168 | + with create_test_idp() as idp_session: |
| 169 | + LOGGER.info(f"Created session test IDP user: {idp_session.username}") |
| 170 | + yield idp_session |
| 171 | + |
| 172 | + |
92 | 173 | # --- RBAC Fixtures --- |
93 | 174 |
|
94 | 175 |
|
@@ -128,7 +209,6 @@ def mr_access_role( |
128 | 209 | ) as role: |
129 | 210 | LOGGER.info(f"Role {role.name} created successfully.") |
130 | 211 | yield role |
131 | | - LOGGER.info(f"Role {role.name} deletion initiated by context manager.") |
132 | 212 |
|
133 | 213 |
|
134 | 214 | @pytest.fixture(scope="function") |
@@ -162,7 +242,7 @@ def mr_access_role_binding( |
162 | 242 | subjects_name=f"system:serviceaccounts:{sa_namespace.name}", |
163 | 243 | subjects_api_group="rbac.authorization.k8s.io", # This is the default apiGroup for Group kind |
164 | 244 | # Role reference parameters |
165 | | - role_ref_kind="Role", |
| 245 | + role_ref_kind=mr_access_role.kind, |
166 | 246 | role_ref_name=mr_access_role.name, |
167 | 247 | label=binding_labels, |
168 | 248 | wait_for_resource=True, |
|
0 commit comments