1- from __future__ import annotations
2-
31from typing import Any
42
53import portforward
64import pytest
75import requests
86import structlog
9- from kubernetes .dynamic import DynamicClient
107from ocp_resources .cron_job import CronJob
118from ocp_resources .network_policy import NetworkPolicy
129from pytest_testconfig import config as py_config
1310
11+ from tests .model_serving .maas_billing .maas_subscription .utils import search_active_api_keys
1412from tests .model_serving .maas_billing .utils import build_maas_headers
1513
1614LOGGER = structlog .get_logger (name = __name__ )
1715
18- MAAS_CLEANUP_CRONJOB_NAME = "maas-api-key-cleanup"
19- MAAS_CLEANUP_NETWORKPOLICY_NAME = "maas-api-cleanup-restrict"
20-
2116
2217@pytest .mark .usefixtures (
2318 "maas_subscription_controller_enabled_latest" ,
@@ -28,18 +23,9 @@ class TestEphemeralKeyCleanup:
2823 """Tests for ephemeral API key cleanup (CronJob + internal endpoint)."""
2924
3025 @pytest .mark .tier1
31- def test_cronjob_exists_and_configured (self , admin_client : DynamicClient ) -> None :
26+ def test_cronjob_exists_and_configured (self , maas_cleanup_cronjob : CronJob ) -> None :
3227 """Verify the maas-api-key-cleanup CronJob exists with expected configuration."""
33- applications_namespace = py_config ["applications_namespace" ]
34-
35- cronjob = CronJob (
36- client = admin_client ,
37- name = MAAS_CLEANUP_CRONJOB_NAME ,
38- namespace = applications_namespace ,
39- )
40- assert cronjob .exists , f"CronJob { MAAS_CLEANUP_CRONJOB_NAME } not found in { applications_namespace } "
41-
42- spec = cronjob .instance .spec
28+ spec = maas_cleanup_cronjob .instance .spec
4329
4430 assert spec .schedule == "*/15 * * * *" , f"Expected schedule '*/15 * * * *', got '{ spec .schedule } '"
4531 assert spec .concurrencyPolicy == "Forbid" , (
@@ -62,26 +48,15 @@ def test_cronjob_exists_and_configured(self, admin_client: DynamicClient) -> Non
6248 LOGGER .info (f"[ephemeral] CronJob validated: schedule={ spec .schedule } , concurrency={ spec .concurrencyPolicy } " )
6349
6450 @pytest .mark .tier1
65- def test_cleanup_networkpolicy_exists (self , admin_client : DynamicClient ) -> None :
51+ def test_cleanup_networkpolicy_exists (self , maas_cleanup_networkpolicy : NetworkPolicy ) -> None :
6652 """Verify the cleanup NetworkPolicy restricts cleanup pod egress to maas-api only."""
67- applications_namespace = py_config ["applications_namespace" ]
68-
69- network_policy = NetworkPolicy (
70- client = admin_client ,
71- name = MAAS_CLEANUP_NETWORKPOLICY_NAME ,
72- namespace = applications_namespace ,
73- )
74- assert network_policy .exists , (
75- f"NetworkPolicy { MAAS_CLEANUP_NETWORKPOLICY_NAME } not found in { applications_namespace } "
76- )
77-
78- spec = network_policy .instance .spec
53+ spec = maas_cleanup_networkpolicy .instance .spec
7954
8055 assert spec .podSelector .matchLabels .get ("app" ) == "maas-api-cleanup" , (
8156 f"NetworkPolicy should target app=maas-api-cleanup pods, got: { spec .podSelector .matchLabels } "
8257 )
83- assert "Egress" in spec . policyTypes , "NetworkPolicy should control Egress traffic"
84- assert "Ingress" in spec .policyTypes , "NetworkPolicy should control Ingress traffic"
58+ for policy_type in ( " Egress" , "Ingress" ):
59+ assert policy_type in spec .policyTypes , f "NetworkPolicy should control { policy_type } traffic"
8560
8661 ingress_rules = getattr (spec , "ingress" , None )
8762 assert ingress_rules in ([], None ), "Cleanup pods should have no inbound traffic allowed"
@@ -93,57 +68,51 @@ def test_cleanup_networkpolicy_exists(self, admin_client: DynamicClient) -> None
9368
9469 @pytest .mark .tier1
9570 @pytest .mark .parametrize ("ocp_token_for_actor" , [{"type" : "free" }], indirect = True )
96- def test_create_ephemeral_key (
71+ def test_ephemeral_key_visible_with_include_filter (
9772 self ,
9873 request_session_http : requests .Session ,
9974 base_url : str ,
10075 ocp_token_for_actor : str ,
10176 ephemeral_api_key : dict [str , Any ],
10277 ) -> None :
103- """Verify ephemeral keys are visible with includeEphemeral filter but hidden by default ."""
78+ """Verify ephemeral key is marked as ephemeral and visible when includeEphemeral=True ."""
10479 key_id = ephemeral_api_key ["id" ]
105- api_keys_endpoint = f"{ base_url } /v1/api-keys"
106- auth_header = build_maas_headers (token = ocp_token_for_actor )
10780
10881 assert ephemeral_api_key .get ("ephemeral" ) is True , "Key should be marked as ephemeral"
10982
110- r_search = request_session_http .post (
111- url = f"{ api_keys_endpoint } /search" ,
112- headers = auth_header ,
113- json = {
114- "filters" : {"status" : ["active" ], "includeEphemeral" : True },
115- "pagination" : {"limit" : 50 , "offset" : 0 },
116- },
117- timeout = 30 ,
118- )
119- assert r_search .status_code == 200 , (
120- f"Expected 200 from search with includeEphemeral=True, "
121- f"got { r_search .status_code } : { (r_search .text or '' )[:200 ]} "
83+ items = search_active_api_keys (
84+ request_session_http = request_session_http ,
85+ base_url = base_url ,
86+ ocp_user_token = ocp_token_for_actor ,
87+ include_ephemeral = True ,
12288 )
123- search_body = r_search .json ()
124- items : list [dict [str , Any ]] = search_body .get ("items" ) or search_body .get ("data" ) or []
12589 assert key_id in [item ["id" ] for item in items ], (
12690 f"Ephemeral key { key_id } should appear in search with includeEphemeral=True"
12791 )
92+ LOGGER .info (f"[ephemeral] Ephemeral key { key_id } visible with includeEphemeral=True" )
12893
129- r_default = request_session_http .post (
130- url = f"{ api_keys_endpoint } /search" ,
131- headers = auth_header ,
132- json = {
133- "filters" : {"status" : ["active" ]},
134- "pagination" : {"limit" : 50 , "offset" : 0 },
135- },
136- timeout = 30 ,
137- )
138- assert r_default .status_code == 200 , (
139- f"Expected 200 from default search, got { r_default .status_code } : { (r_default .text or '' )[:200 ]} "
94+ @pytest .mark .tier1
95+ @pytest .mark .parametrize ("ocp_token_for_actor" , [{"type" : "free" }], indirect = True )
96+ def test_ephemeral_key_hidden_from_default_search (
97+ self ,
98+ request_session_http : requests .Session ,
99+ base_url : str ,
100+ ocp_token_for_actor : str ,
101+ ephemeral_api_key : dict [str , Any ],
102+ ) -> None :
103+ """Verify ephemeral key is hidden from default search when includeEphemeral is not set."""
104+ key_id = ephemeral_api_key ["id" ]
105+
106+ default_items = search_active_api_keys (
107+ request_session_http = request_session_http ,
108+ base_url = base_url ,
109+ ocp_user_token = ocp_token_for_actor ,
110+ include_ephemeral = False ,
140111 )
141- default_body = r_default .json ()
142- default_items : list [dict [str , Any ]] = default_body .get ("items" ) or default_body .get ("data" ) or []
143112 assert key_id not in [item ["id" ] for item in default_items ], (
144113 "Ephemeral key should be excluded from default search (includeEphemeral defaults to False)"
145114 )
146- LOGGER .info (f"[ephemeral] Ephemeral key { key_id } visibility verified " )
115+ LOGGER .info (f"[ephemeral] Ephemeral key { key_id } correctly hidden from default search " )
147116
148117 @pytest .mark .tier1
149118 @pytest .mark .parametrize ("ocp_token_for_actor" , [{"type" : "free" }], indirect = True )
0 commit comments