Spaces:
Sleeping
Sleeping
| #!/usr/bin/env python3 | |
| """ | |
| Post-deployment verification script for Hugging Face Space. | |
| Run this after deployment to verify all endpoints are working correctly. | |
| """ | |
| import requests | |
| import json | |
| import time | |
| import argparse | |
| from typing import Dict, Any | |
| def test_endpoint(url: str, method: str = 'GET', data: Dict[Any, Any] = None, timeout: int = 30) -> Dict[str, Any]: | |
| """Test a single endpoint and return results.""" | |
| try: | |
| if method.upper() == 'GET': | |
| response = requests.get(url, timeout=timeout) | |
| elif method.upper() == 'POST': | |
| response = requests.post(url, json=data, timeout=timeout) | |
| else: | |
| return {"success": False, "error": f"Unsupported method: {method}"} | |
| return { | |
| "success": True, | |
| "status_code": response.status_code, | |
| "response_time": response.elapsed.total_seconds(), | |
| "response_data": response.json() if response.headers.get('content-type', '').startswith('application/json') else response.text[:200] | |
| } | |
| except requests.exceptions.Timeout: | |
| return {"success": False, "error": "Request timed out"} | |
| except requests.exceptions.ConnectionError: | |
| return {"success": False, "error": "Connection error"} | |
| except Exception as e: | |
| return {"success": False, "error": str(e)} | |
| def verify_deployment(base_url: str): | |
| """Run comprehensive deployment verification.""" | |
| print(f"π Starting deployment verification for: {base_url}") | |
| print("=" * 60) | |
| tests = [ | |
| { | |
| "name": "Root Endpoint", | |
| "url": f"{base_url}/", | |
| "method": "GET", | |
| "expected_status": 200 | |
| }, | |
| { | |
| "name": "Health Check", | |
| "url": f"{base_url}/api/v1/health", | |
| "method": "GET", | |
| "expected_status": 200 | |
| }, | |
| { | |
| "name": "Static Files Mount (Should return 404, not 500)", | |
| "url": f"{base_url}/static/test.txt", | |
| "method": "GET", | |
| "expected_status": 404 | |
| }, | |
| { | |
| "name": "Generate Horoscope", | |
| "url": f"{base_url}/api/v1/generate-horoscope", | |
| "method": "POST", | |
| "data": { | |
| "terms": ["Test", "Deployment", "Success", "Working", "Happy"], | |
| "date_of_birth": "1990-01-01" | |
| }, | |
| "expected_status": 200 | |
| } | |
| ] | |
| results = [] | |
| for test in tests: | |
| print(f"\nπ Testing: {test['name']}") | |
| print(f" URL: {test['url']}") | |
| result = test_endpoint( | |
| url=test['url'], | |
| method=test['method'], | |
| data=test.get('data'), | |
| timeout=30 | |
| ) | |
| if result['success']: | |
| status_code = result['status_code'] | |
| expected = test['expected_status'] | |
| if status_code == expected: | |
| print(f" β PASS - Status: {status_code}, Time: {result['response_time']:.2f}s") | |
| if 'response_data' in result: | |
| print(f" π Response: {result['response_data']}") | |
| else: | |
| print(f" β FAIL - Expected: {expected}, Got: {status_code}") | |
| print(f" π Response: {result['response_data']}") | |
| else: | |
| print(f" β ERROR - {result['error']}") | |
| results.append({ | |
| "test": test['name'], | |
| "result": result, | |
| "expected_status": test['expected_status'] | |
| }) | |
| # Small delay between tests | |
| time.sleep(1) | |
| # Summary | |
| print("\n" + "=" * 60) | |
| print("π DEPLOYMENT VERIFICATION SUMMARY") | |
| print("=" * 60) | |
| passed = 0 | |
| failed = 0 | |
| for result in results: | |
| test_name = result['test'] | |
| test_result = result['result'] | |
| expected = result['expected_status'] | |
| if test_result['success'] and test_result['status_code'] == expected: | |
| print(f"β {test_name}") | |
| passed += 1 | |
| else: | |
| print(f"β {test_name}") | |
| failed += 1 | |
| print(f"\nπ Results: {passed} passed, {failed} failed") | |
| if failed == 0: | |
| print("π All tests passed! Deployment is successful.") | |
| return True | |
| else: | |
| print("π₯ Some tests failed. Check the errors above.") | |
| return False | |
| def main(): | |
| parser = argparse.ArgumentParser(description='Verify Hugging Face Space deployment') | |
| parser.add_argument( | |
| '--url', | |
| default='https://huggingface.co/spaces/ch404/cardserver', | |
| help='Base URL of the deployed application' | |
| ) | |
| parser.add_argument( | |
| '--wait', | |
| type=int, | |
| default=0, | |
| help='Wait N seconds before starting tests (useful for fresh deployments)' | |
| ) | |
| args = parser.parse_args() | |
| if args.wait > 0: | |
| print(f"β° Waiting {args.wait} seconds for deployment to fully start...") | |
| time.sleep(args.wait) | |
| success = verify_deployment(args.url) | |
| exit(0 if success else 1) | |
| if __name__ == "__main__": | |
| main() | |