|
5 | 5 | from model_registry import ModelRegistry as ModelRegistryClient |
6 | 6 | from tests.model_registry.rbac.utils import build_mr_client_args |
7 | 7 | from utilities.infra import create_inference_token |
8 | | -from mr_openapi.exceptions import ForbiddenException |
| 8 | +from mr_openapi.exceptions import ForbiddenException, UnauthorizedException |
9 | 9 | from ocp_resources.service_account import ServiceAccount |
| 10 | +from timeout_sampler import TimeoutSampler |
10 | 11 |
|
11 | 12 | LOGGER = get_logger(name=__name__) |
12 | 13 |
|
@@ -69,21 +70,29 @@ def test_service_account_access_granted( |
69 | 70 | LOGGER.info(f"Targeting Model Registry REST endpoint: {model_registry_instance_rest_endpoint[0]}") |
70 | 71 | LOGGER.info("Applied RBAC Role/Binding via fixtures. Expecting access GRANT.") |
71 | 72 |
|
72 | | - # Create a fresh token to bypass OAuth proxy cache from previous test |
| 73 | + # Create a fresh token to bypass kube-rbac-proxy cache from previous test |
73 | 74 | fresh_token = create_inference_token(model_service_account=service_account) |
| 75 | + client_args = build_mr_client_args( |
| 76 | + rest_endpoint=model_registry_instance_rest_endpoint[0], token=fresh_token, author="rbac-test-granted" |
| 77 | + ) |
| 78 | + LOGGER.debug(f"Attempting client connection with args: {client_args}") |
| 79 | + |
| 80 | + # Retry for up to 2 minutes to allow RBAC propagation |
| 81 | + # Accept UnauthorizedException (401) as a transient error during RBAC propagation |
| 82 | + sampler = TimeoutSampler( |
| 83 | + wait_timeout=120, |
| 84 | + sleep=5, |
| 85 | + func=lambda: ModelRegistryClient(**client_args), |
| 86 | + exceptions_dict={UnauthorizedException: []}, |
| 87 | + ) |
74 | 88 |
|
75 | 89 | try: |
76 | | - client_args = build_mr_client_args( |
77 | | - rest_endpoint=model_registry_instance_rest_endpoint[0], token=fresh_token, author="rbac-test-granted" |
78 | | - ) |
79 | | - LOGGER.debug(f"Attempting client connection with args: {client_args}") |
80 | | - mr_client_success = ModelRegistryClient(**client_args) |
| 90 | + # Get the first successful result |
| 91 | + mr_client_success = next(iter(sampler)) |
81 | 92 | assert mr_client_success is not None, "Client initialization failed after granting permissions" |
82 | 93 | LOGGER.info("Client instantiated successfully after granting permissions.") |
83 | | - |
84 | 94 | except Exception as e: |
85 | | - # If we get an exception here, it's unexpected, especially 403 |
86 | | - LOGGER.error(f"Received unexpected general error after granting access: {e}", exc_info=True) |
| 95 | + LOGGER.error(f"Failed to access Model Registry after granting permissions: {e}", exc_info=True) |
87 | 96 | raise |
88 | 97 |
|
89 | 98 | LOGGER.info("--- RBAC Test Completed Successfully ---") |
0 commit comments