2
2
Pytest configuration and fixtures for cocalc-api tests.
3
3
"""
4
4
import os
5
+ import time
5
6
import uuid
6
7
import pytest
7
8
@@ -25,6 +26,25 @@ def assert_valid_uuid(value, description="value"):
25
26
pytest .fail (f"{ description } should be a valid UUID, got: { value } " )
26
27
27
28
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
+
28
48
@pytest .fixture (scope = "session" )
29
49
def api_key ():
30
50
"""Get API key from environment variable."""
@@ -59,18 +79,11 @@ def temporary_project(hub, request):
59
79
title = f"CoCalc API Test { timestamp } "
60
80
description = "Temporary project created by cocalc-api tests"
61
81
62
- print ("\n " + "=" * 70 )
63
- print ("=== Creating temporary project for entire test session ===" )
64
- print ("=== THIS SHOULD ONLY PRINT ONCE ===" )
65
- print ("=" * 70 )
66
82
project_id = hub .projects .create_project (title = title , description = description )
67
- print (f"Created project { project_id } " )
68
- print ("=" * 70 )
69
83
70
84
# Start the project so it can respond to API calls
71
85
try :
72
86
hub .projects .start (project_id )
73
- print (f"Starting project { project_id } , waiting for it to become ready..." )
74
87
75
88
# Wait for project to be ready (can take 10-15 seconds)
76
89
from cocalc_api import Project
@@ -81,39 +94,19 @@ def temporary_project(hub, request):
81
94
# Try to ping the project to see if it's ready
82
95
test_project = Project (project_id = project_id , api_key = hub .api_key , host = hub .host )
83
96
test_project .system .ping () # If this succeeds, project is ready
84
- print (f"✓ Project { project_id } is ready after { (attempt + 1 ) * 5 } seconds" )
85
97
break
86
98
except Exception :
87
99
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" )
89
101
90
102
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 } " )
92
104
93
105
project_info = {'project_id' : project_id , 'title' : title , 'description' : description }
94
106
95
- # Register cleanup using finalizer (more reliable than yield teardown)
107
+ # Register cleanup using finalizer
96
108
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 )
117
110
118
111
request .addfinalizer (cleanup )
119
112
0 commit comments