-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_api.py
More file actions
104 lines (89 loc) · 3.13 KB
/
Copy pathtest_api.py
File metadata and controls
104 lines (89 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env python
"""Quick API validation script."""
import requests
import json
BASE_URL = "http://localhost:8080"
def test_health():
"""Test health endpoint."""
response = requests.get(f"{BASE_URL}/health")
print(f"[OK] Health Check: {response.json()}")
return response.status_code == 200
def test_login():
"""Test login and return token."""
response = requests.post(
f"{BASE_URL}/api/auth/login",
json={"email": "admin@toycloud.io", "password": "admin123"}
)
data = response.json()
print(f"[OK] Login Success: {data['user']['email']} ({data['user']['role']})")
return data['access_token']
def test_list_hosts(token):
"""Test listing hosts."""
headers = {"Authorization": f"Bearer {token}"}
response = requests.get(f"{BASE_URL}/api/admin/hosts", headers=headers)
print(f"Response status: {response.status_code}")
print(f"Response text: {response.text[:200]}")
if response.status_code == 200:
hosts = response.json()
print(f"[OK] List Hosts: Found {len(hosts)} hosts")
for host in hosts:
print(f" - {host['name']}: {host['used_capacity']}/{host['capacity']} VMs ({host['utilization_percent']}%)")
return True
else:
print(f"[FAIL] List hosts failed with status {response.status_code}")
return False
def test_create_host(token):
"""Test creating a host."""
headers = {"Authorization": f"Bearer {token}"}
host_data = {
"name": "test-validation-host",
"hostname": "test-validation.toycloud.io",
"ip_address": "192.168.99.99",
"capacity": 5,
"notes": "Auto-created for validation"
}
response = requests.post(f"{BASE_URL}/api/admin/hosts", json=host_data, headers=headers)
if response.status_code == 201:
host = response.json()
print(f"[OK] Create Host: {host['name']} (ID: {host['id']})")
return host['id']
else:
print(f"[FAIL] Create Host Failed: {response.text}")
return None
def test_delete_host(token, host_id):
"""Test deleting a host."""
headers = {"Authorization": f"Bearer {token}"}
response = requests.delete(f"{BASE_URL}/api/admin/hosts/{host_id}", headers=headers)
if response.status_code == 204:
print(f"[OK] Delete Host: Successfully deleted host {host_id}")
return True
else:
print(f"[FAIL] Delete Host Failed: {response.text}")
return False
def main():
print("="*60)
print("ToyCloud API Validation")
print("="*60)
try:
# Test health
if not test_health():
print("[FAIL] Health check failed!")
return
# Test login
token = test_login()
# Test list hosts
test_list_hosts(token)
# Test create host
host_id = test_create_host(token)
# Test delete host
if host_id:
test_delete_host(token, host_id)
print("\n" + "="*60)
print("[SUCCESS] All API Tests Passed!")
print("="*60)
except Exception as e:
print(f"\n[ERROR] {e}")
import traceback
traceback.print_exc()
if __name__ == "__main__":
main()