3535LOCATION = "global"
3636PREFIX = "python_sample_sha_custom_module" # Prefix used for identifying test modules
3737
38+ # Global list to track created modules
39+ created_modules = []
40+
3841
3942@pytest .fixture (scope = "session" , autouse = True )
4043def setup_environment ():
41- """Fixture to ensure a clean environment by removing test modules before running tests."""
4244 if not ORGANIZATION_ID :
4345 pytest .fail ("GCLOUD_ORGANIZATION environment variable is not set." )
4446
45- print (f"Cleaning up existing custom modules for organization: { ORGANIZATION_ID } " )
46- cleanup_existing_custom_modules (ORGANIZATION_ID )
4747
48+ @pytest .fixture (scope = "session" , autouse = True )
49+ def cleanup_after_tests (request ):
50+ """Fixture to clean up created custom modules after the test session."""
51+ def teardown ():
52+ print ("\n Created Custom Modules:" )
53+ print_all_created_modules ()
54+ print ("Cleaning up created custom modules..." )
55+ cleanup_created_custom_modules ()
4856
49- def cleanup_existing_custom_modules (org_id : str ):
50- """
51- Deletes all custom modules matching a specific naming pattern.
52- Args:
53- org_id: The organization ID.
54- """
55- client = securitycentermanagement_v1 .SecurityCenterManagementClient ()
56- parent = f"organizations/{ org_id } /locations/global"
57- print (f"Parent path: { parent } " )
58- try :
59- custom_modules = client .list_security_health_analytics_custom_modules (
60- request = {"parent" : parent }
61- )
62- for module in custom_modules :
63- if module .display_name .startswith (PREFIX ):
64- client .delete_security_health_analytics_custom_module (
65- request = {"name" : module .name }
66- )
67- print (f"Deleted custom module: { module .name } " )
68- except NotFound as e :
69- print (f"Resource not found: { e } " )
70- except Exception as e :
71- print (f"Unexpected error during cleanup: { e } " )
72- raise
57+ request .addfinalizer (teardown )
7358
7459
7560def add_custom_module (org_id : str ):
@@ -130,6 +115,7 @@ def test_create_security_health_analytics_custom_module():
130115
131116 # Run the function to create the custom module
132117 response = security_health_analytics_custom_modules .create_security_health_analytics_custom_module (parent )
118+ created_modules .append (response .name )
133119
134120 assert response is not None , "Custom module creation failed."
135121 # Verify that the custom module was created
@@ -143,6 +129,7 @@ def test_create_security_health_analytics_custom_module():
143129def test_get_security_health_analytics_custom_module ():
144130
145131 module_name , module_id = add_custom_module (ORGANIZATION_ID )
132+ created_modules .append (module_name )
146133 parent = f"organizations/{ ORGANIZATION_ID } /locations/{ LOCATION } "
147134
148135 # Retrieve the custom module
@@ -161,6 +148,7 @@ def test_get_security_health_analytics_custom_module():
161148def test_delete_security_health_analytics_custom_module ():
162149
163150 module_name , module_id = add_custom_module (ORGANIZATION_ID )
151+ created_modules .append (module_name )
164152 parent = f"organizations/{ ORGANIZATION_ID } /locations/{ LOCATION } "
165153
166154 try :
@@ -180,6 +168,7 @@ def test_delete_security_health_analytics_custom_module():
180168def test_list_security_health_analytics_custom_module ():
181169
182170 module_name , module_id = add_custom_module (ORGANIZATION_ID )
171+ created_modules .append (module_name )
183172 parent = f"organizations/{ ORGANIZATION_ID } /locations/{ LOCATION } "
184173 # Retrieve the custom modules
185174 custom_modules = security_health_analytics_custom_modules .list_security_health_analytics_custom_module (parent )
@@ -205,6 +194,7 @@ def test_list_security_health_analytics_custom_module():
205194def test_update_security_health_analytics_custom_module ():
206195
207196 module_name , module_id = add_custom_module (ORGANIZATION_ID )
197+ created_modules .append (module_name )
208198 parent = f"organizations/{ ORGANIZATION_ID } /locations/{ LOCATION } "
209199 # Retrieve the custom modules
210200 updated_custom_module = security_health_analytics_custom_modules .update_security_health_analytics_custom_module (parent , module_id )
@@ -213,3 +203,47 @@ def test_update_security_health_analytics_custom_module():
213203 response_org_id = updated_custom_module .name .split ("/" )[1 ] # Extract organization ID from the name field
214204 assert response_org_id == ORGANIZATION_ID , f"Organization ID mismatch: Expected { ORGANIZATION_ID } , got { response_org_id } ."
215205 assert updated_custom_module .enablement_state == securitycentermanagement_v1 .SecurityHealthAnalyticsCustomModule .EnablementState .DISABLED
206+
207+
208+ def print_all_created_modules ():
209+ """Print all created custom modules."""
210+ if not created_modules :
211+ print ("No custom modules were created." )
212+ else :
213+ for module in created_modules :
214+ print (module )
215+
216+
217+ def cleanup_created_custom_modules ():
218+ """
219+ Deletes all created custom modules in this test session.
220+ """
221+ client = securitycentermanagement_v1 .SecurityCenterManagementClient ()
222+
223+ for module in list (created_modules ):
224+ if not custom_module_exists (module ):
225+ print (f"Module not found (already deleted): { module } " )
226+ created_modules .remove (module )
227+ continue
228+ try :
229+ client .delete_security_health_analytics_custom_module (
230+ request = {"name" : module }
231+ )
232+ print (f"Deleted custom module: { module } " )
233+ created_modules .remove (module )
234+ except Exception as e :
235+ print (f"Failed to delete module { module } : { e } " )
236+ raise
237+
238+
239+ def custom_module_exists (module_name ):
240+ client = securitycentermanagement_v1 .SecurityCenterManagementClient ()
241+ try :
242+ client .get_security_health_analytics_custom_module (
243+ request = {"name" : module_name }
244+ )
245+ return True
246+ except Exception as e :
247+ if "404" in str (e ):
248+ return False
249+ raise
0 commit comments