1+ import secrets
2+ import string
13from collections .abc import Generator
4+ from contextlib import ExitStack
25from typing import Any
36
47import pytest
58import requests
69from kubernetes .dynamic import DynamicClient
10+ from ocp_resources .data_science_cluster import DataScienceCluster
11+ from ocp_resources .deployment import Deployment
712from ocp_resources .llm_inference_service import LLMInferenceService
813from ocp_resources .maas_auth_policy import MaaSAuthPolicy
914from ocp_resources .maas_model_ref import MaaSModelRef
1015from ocp_resources .maas_subscription import MaaSSubscription
1116from ocp_resources .namespace import Namespace
17+ from ocp_resources .resource import ResourceEditor
18+ from ocp_resources .secret import Secret
19+ from ocp_resources .service import Service
1220from ocp_resources .service_account import ServiceAccount
1321from pytest_testconfig import config as py_config
1422from simple_logger .logger import get_logger
1523
1624from tests .model_serving .maas_billing .maas_subscription .utils import (
25+ MAAS_DB_NAMESPACE ,
1726 MAAS_SUBSCRIPTION_NAMESPACE ,
1827 create_api_key ,
28+ get_maas_postgres_resources ,
1929 patch_llmisvc_with_maas_router_and_tiers ,
30+ wait_for_postgres_connection_log ,
31+ wait_for_postgres_deployment_ready ,
2032)
2133from tests .model_serving .maas_billing .utils import build_maas_headers
34+ from utilities .constants import DscComponents
2235from utilities .general import generate_random_name
2336from utilities .infra import create_inference_token , create_ns , login_with_user_password
2437from utilities .llmd_constants import ContainerImages , ModelStorage
3043CHAT_COMPLETIONS = OpenAIEnpoints .CHAT_COMPLETIONS
3144
3245
46+ @pytest .fixture (scope = "session" )
47+ def maas_subscription_controller_enabled_latest (
48+ dsc_resource : DataScienceCluster ,
49+ maas_postgres_prereqs : None ,
50+ maas_gateway_api : None ,
51+ ) -> Generator [DataScienceCluster , Any , Any ]:
52+ """
53+ ensures postgres prerequisites exist before MaaS is switched to Managed.
54+ """
55+ component_patch = {
56+ DscComponents .KSERVE : {"modelsAsService" : {"managementState" : DscComponents .ManagementState .MANAGED }}
57+ }
58+
59+ with ResourceEditor (patches = {dsc_resource : {"spec" : {"components" : component_patch }}}):
60+ dsc_resource .wait_for_condition (
61+ condition = "ModelsAsServiceReady" ,
62+ status = "True" ,
63+ timeout = 900 ,
64+ )
65+ dsc_resource .wait_for_condition (
66+ condition = "Ready" ,
67+ status = "True" ,
68+ timeout = 600 ,
69+ )
70+ yield dsc_resource
71+
72+ dsc_resource .wait_for_condition (condition = "Ready" , status = "True" , timeout = 600 )
73+
74+
3375@pytest .fixture (scope = "class" )
3476def maas_inference_service_tinyllama_free (
3577 admin_client : DynamicClient ,
@@ -253,7 +295,7 @@ def model_url_tinyllama_free(
253295) -> str :
254296 deployment_name = maas_inference_service_tinyllama_free .name
255297 url = f"{ maas_scheme } ://{ maas_host } /llm/{ deployment_name } { CHAT_COMPLETIONS } "
256- LOGGER .info ("MaaS: constructed model_url=%s (deployment=%s)" , url , deployment_name )
298+ LOGGER .info (f "MaaS: constructed model_url={ url } (deployment={ deployment_name } )" )
257299 return url
258300
259301
@@ -265,16 +307,79 @@ def model_url_tinyllama_premium(
265307) -> str :
266308 deployment_name = maas_inference_service_tinyllama_premium .name
267309 url = f"{ maas_scheme } ://{ maas_host } /llm/{ deployment_name } { CHAT_COMPLETIONS } "
268- LOGGER .info ("MaaS: constructed model_url=%s (deployment=%s)" , url , deployment_name )
310+ LOGGER .info (f "MaaS: constructed model_url={ url } (deployment={ deployment_name } )" )
269311 return url
270312
271313
314+ @pytest .fixture (scope = "session" )
315+ def maas_postgres_credentials () -> dict [str , str ]:
316+ alphabet = string .ascii_letters + string .digits
317+
318+ postgres_user = f"maas-{ generate_random_name ()} "
319+ postgres_db = f"maas-{ generate_random_name ()} "
320+ postgres_password = "" .join (secrets .choice (alphabet ) for _ in range (32 ))
321+
322+ LOGGER .info (
323+ f"Generated PostgreSQL test credentials: "
324+ f"user={ postgres_user } , db={ postgres_db } , password_length={ len (postgres_password )} "
325+ )
326+
327+ return {
328+ "postgres_user" : postgres_user ,
329+ "postgres_password" : postgres_password ,
330+ "postgres_db" : postgres_db ,
331+ }
332+
333+
334+ @pytest .fixture (scope = "session" )
335+ def maas_postgres_prereqs (
336+ admin_client : DynamicClient ,
337+ maas_postgres_credentials : dict [str , str ],
338+ ) -> Generator [dict [Any , Any ], Any , Any ]:
339+ """
340+ Prepare PostgreSQL resources required by maas-api before MaaS API key tests run.
341+ """
342+ resources = get_maas_postgres_resources (
343+ client = admin_client ,
344+ namespace = MAAS_DB_NAMESPACE ,
345+ teardown_resources = True ,
346+ postgres_user = maas_postgres_credentials ["postgres_user" ],
347+ postgres_password = maas_postgres_credentials ["postgres_password" ],
348+ postgres_db = maas_postgres_credentials ["postgres_db" ],
349+ )
350+
351+ resources_instances : dict [Any , Any ] = {}
352+
353+ with ExitStack () as stack :
354+ for kind_name in [Secret , Service , Deployment ]:
355+ resources_instances [kind_name ] = []
356+
357+ for resource_obj in resources [kind_name ]:
358+ resources_instances [kind_name ].append (stack .enter_context (resource_obj ))
359+
360+ for deployment in resources_instances [Deployment ]:
361+ deployment .wait_for_condition (condition = "Available" , status = "True" , timeout = 180 )
362+
363+ wait_for_postgres_deployment_ready (
364+ admin_client = admin_client ,
365+ namespace = MAAS_DB_NAMESPACE ,
366+ timeout = 180 ,
367+ )
368+ wait_for_postgres_connection_log (
369+ admin_client = admin_client ,
370+ namespace = MAAS_DB_NAMESPACE ,
371+ timeout = 180 ,
372+ )
373+
374+ yield resources_instances
375+
376+
272377@pytest .fixture (scope = "class" )
273378def maas_api_key_for_actor (
274379 request_session_http : requests .Session ,
275380 base_url : str ,
276381 ocp_token_for_actor : str ,
277- maas_controller_enabled_latest : None ,
382+ maas_subscription_controller_enabled_latest : None ,
278383 maas_gateway_api : None ,
279384 maas_api_gateway_reachable : None ,
280385) -> str :
0 commit comments