22Pytest configuration and fixtures for cocalc-api tests.
33"""
44import os
5+ import time
56import uuid
67import pytest
78
@@ -25,6 +26,25 @@ def assert_valid_uuid(value, description="value"):
2526 pytest .fail (f"{ description } should be a valid UUID, got: { value } " )
2627
2728
29+ def cleanup_project (hub , project_id ):
30+ """
31+ Clean up a test project by stopping it and deleting it.
32+
33+ Args:
34+ hub: Hub client instance
35+ project_id: Project ID to cleanup
36+ """
37+ try :
38+ hub .projects .stop (project_id )
39+ except Exception as e :
40+ print (f"Warning: Failed to stop project { project_id } : { e } " )
41+
42+ try :
43+ hub .projects .delete (project_id )
44+ except Exception as e :
45+ print (f"Warning: Failed to delete project { project_id } : { e } " )
46+
47+
2848@pytest .fixture (scope = "session" )
2949def api_key ():
3050 """Get API key from environment variable."""
@@ -59,18 +79,11 @@ def temporary_project(hub, request):
5979 title = f"CoCalc API Test { timestamp } "
6080 description = "Temporary project created by cocalc-api tests"
6181
62- print ("\n " + "=" * 70 )
63- print ("=== Creating temporary project for entire test session ===" )
64- print ("=== THIS SHOULD ONLY PRINT ONCE ===" )
65- print ("=" * 70 )
6682 project_id = hub .projects .create_project (title = title , description = description )
67- print (f"Created project { project_id } " )
68- print ("=" * 70 )
6983
7084 # Start the project so it can respond to API calls
7185 try :
7286 hub .projects .start (project_id )
73- print (f"Starting project { project_id } , waiting for it to become ready..." )
7487
7588 # Wait for project to be ready (can take 10-15 seconds)
7689 from cocalc_api import Project
@@ -81,39 +94,19 @@ def temporary_project(hub, request):
8194 # Try to ping the project to see if it's ready
8295 test_project = Project (project_id = project_id , api_key = hub .api_key , host = hub .host )
8396 test_project .system .ping () # If this succeeds, project is ready
84- print (f"✓ Project { project_id } is ready after { (attempt + 1 ) * 5 } seconds" )
8597 break
8698 except Exception :
8799 if attempt == 9 : # Last attempt
88- print (f"⚠ Warning: Project { project_id } did not become ready within 50 seconds" )
100+ print (f"Warning: Project { project_id } did not become ready within 50 seconds" )
89101
90102 except Exception as e :
91- print (f"⚠ Warning: Failed to start project { project_id } : { e } " )
103+ print (f"Warning: Failed to start project { project_id } : { e } " )
92104
93105 project_info = {'project_id' : project_id , 'title' : title , 'description' : description }
94106
95- # Register cleanup using finalizer (more reliable than yield teardown)
107+ # Register cleanup using finalizer
96108 def cleanup ():
97- print (f"\n === Cleaning up test project '{ title } ' (ID: { project_id } ) ===" )
98-
99- try :
100- # Stop the project first
101- print (f"Stopping project { project_id } ..." )
102- hub .projects .stop (project_id )
103- print ("✓ Project stop command sent" )
104- # Wait for the project process to actually terminate
105- time .sleep (3 )
106- print (f"✓ Waited for project { project_id } to stop" )
107- except Exception as e :
108- print (f"⚠ Failed to stop project { project_id } : { e } " )
109-
110- try :
111- # Delete the project
112- print (f"Deleting project { project_id } ..." )
113- hub .projects .delete (project_id )
114- print (f"✓ Project { project_id } deleted" )
115- except Exception as e :
116- print (f"⚠ Failed to delete project { project_id } : { e } " )
109+ cleanup_project (hub , project_id )
117110
118111 request .addfinalizer (cleanup )
119112
0 commit comments