-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_upload.py
More file actions
166 lines (136 loc) · 5.42 KB
/
test_upload.py
File metadata and controls
166 lines (136 loc) · 5.42 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/env python3
"""Test script to verify package upload functionality"""
import requests
import json
import os
import tempfile
BASE_URL = "http://localhost:5001"
def test_login():
"""Test login and get token"""
login_data = {
"username": "testuser",
"password": "testpass123"
}
response = requests.post(f"{BASE_URL}/api/auth/login", json=login_data)
if response.status_code == 200:
token = response.json()['access_token']
print("✓ Login successful")
return token
else:
print(f"✗ Login failed: {response.status_code} - {response.text}")
return None
def test_create_org(token):
"""Test organization creation"""
headers = {"Authorization": f"Bearer {token}"}
org_data = {
"name": "test-org",
"description": "Test organization for uploads",
"is_public": True
}
response = requests.post(f"{BASE_URL}/api/organizations", json=org_data, headers=headers)
if response.status_code in [200, 201, 409]: # 409 = already exists
print("✓ Organization ready")
return True
else:
print(f"✗ Organization creation failed: {response.status_code} - {response.text}")
return False
def test_create_repo(token, package_type):
"""Test repository creation"""
headers = {"Authorization": f"Bearer {token}"}
repo_data = {
"name": f"test-{package_type}-repo",
"description": f"Test {package_type} repository",
"package_type": package_type,
"visibility": "public"
}
response = requests.post(f"{BASE_URL}/api/organizations/test-org/repositories", json=repo_data, headers=headers)
if response.status_code in [200, 201, 409]: # 409 = already exists
print(f"✓ {package_type} repository ready")
return True
else:
print(f"✗ {package_type} repository creation failed: {response.status_code} - {response.text}")
return False
def test_upload_file(token, package_type):
"""Test file upload"""
headers = {"Authorization": f"Bearer {token}"}
# Create a test file
with tempfile.NamedTemporaryFile(mode='w', suffix='.txt', delete=False) as f:
f.write(f"Test {package_type} package content\nThis is a test file for the {package_type} repository.")
test_file_path = f.name
try:
with open(test_file_path, 'rb') as f:
files = {'file': (f'test-{package_type}-package.txt', f, 'text/plain')}
data = {
'name': f'test-{package_type}-package',
'version': '1.0.0',
'description': f'Test {package_type} package'
}
response = requests.post(
f"{BASE_URL}/api/organizations/test-org/repositories/test-{package_type}-repo/artifacts",
files=files,
data=data,
headers=headers
)
if response.status_code in [200, 201]:
print(f"✓ {package_type} file upload successful")
return True
else:
print(f"✗ {package_type} file upload failed: {response.status_code} - {response.text}")
return False
finally:
os.unlink(test_file_path)
def test_list_artifacts(token, package_type):
"""Test listing artifacts"""
headers = {"Authorization": f"Bearer {token}"}
response = requests.get(
f"{BASE_URL}/api/organizations/test-org/repositories/test-{package_type}-repo/artifacts",
headers=headers
)
if response.status_code == 200:
artifacts = response.json()
print(f"✓ {package_type} artifacts listed: {len(artifacts)} found")
return True
else:
print(f"✗ {package_type} artifacts listing failed: {response.status_code} - {response.text}")
return False
def test_docker_registry():
"""Test Docker Registry API V2"""
try:
response = requests.get(f"{BASE_URL}/v2/")
if response.status_code == 200:
print("✓ Docker Registry API V2 endpoint responding")
return True
else:
print(f"✗ Docker Registry API V2 failed: {response.status_code}")
return False
except Exception as e:
print(f"✗ Docker Registry API V2 error: {e}")
return False
def main():
print("Testing Artifact Repository Functionality\n")
# Test basic authentication
token = test_login()
if not token:
print("Cannot proceed without authentication")
return
# Test organization setup
if not test_create_org(token):
print("Cannot proceed without organization")
return
# Test different package types
package_types = ['docker', 'npm', 'python', 'maven', 'generic']
for package_type in package_types:
print(f"\n--- Testing {package_type.upper()} ---")
if test_create_repo(token, package_type):
if test_upload_file(token, package_type):
test_list_artifacts(token, package_type)
# Test Docker Registry API
print(f"\n--- Testing Docker Registry API ---")
test_docker_registry()
print(f"\n🎉 Testing complete!")
print(f"You can now:")
print(f"1. View artifacts in the web UI")
print(f"2. Try Docker commands with your registry")
print(f"3. Upload more files through the web interface")
if __name__ == "__main__":
main()