-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_lambda_deployment.py
More file actions
332 lines (279 loc) · 11.9 KB
/
Copy pathtest_lambda_deployment.py
File metadata and controls
332 lines (279 loc) · 11.9 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#!/usr/bin/env python3
"""
Test script for Lambda-based deployment (without API Gateway)
Tests individual Lambda functions and Vercel frontend
"""
import json
import sys
import time
import requests
from typing import Dict, Any, Optional
class LambdaDeploymentTester:
def __init__(self, vercel_url: str):
self.vercel_url = vercel_url.rstrip('/')
self.test_results = []
# Lambda function names from your deployment
self.lambda_functions = {
'generate_url': 'hatchmark-generate-url-dev',
'register_asset': 'hatchmark-register-asset-dev',
'verify_artwork': 'hatchmark-verify-artwork-dev'
}
# AWS resources from your stack
self.aws_resources = {
'ingestion_bucket': 'hatchmark-ingestion-bucket-dev-581150859000',
'processed_bucket': 'hatchmark-processed-bucket-dev-581150859000',
'dynamodb_table': 'hatchmark-assets-dev',
'sqs_queue': 'https://sqs.eu-west-1.amazonaws.com/581150859000/hatchmark-watermarking-dev'
}
def log_test(self, test_name: str, success: bool, message: str, details: Optional[Dict] = None):
"""Log test results"""
status = "✅ PASS" if success else "❌ FAIL"
print(f"{status} {test_name}: {message}")
self.test_results.append({
'test': test_name,
'success': success,
'message': message,
'details': details or {}
})
if details and not success:
print(f" Details: {json.dumps(details, indent=2)}")
def test_frontend_accessibility(self):
"""Test if the Vercel frontend is accessible"""
try:
response = requests.get(self.vercel_url, timeout=10)
if response.status_code == 200:
content_type = response.headers.get('content-type', '')
if 'text/html' in content_type:
self.log_test(
"Frontend Accessibility",
True,
f"Frontend is accessible at {self.vercel_url}",
{"status_code": response.status_code}
)
return True
else:
self.log_test(
"Frontend Accessibility",
False,
"Frontend returned non-HTML content",
{"content_type": content_type}
)
else:
self.log_test(
"Frontend Accessibility",
False,
f"Frontend returned status {response.status_code}"
)
except Exception as e:
self.log_test(
"Frontend Accessibility",
False,
f"Failed to connect to frontend: {str(e)}"
)
return False
def test_lambda_functions(self):
"""Test Lambda function accessibility"""
import subprocess
for func_type, func_name in self.lambda_functions.items():
try:
# Test if Lambda function exists and is accessible
result = subprocess.run([
'wsl', 'aws', 'lambda', 'get-function',
'--function-name', func_name
], capture_output=True, text=True, timeout=10)
if result.returncode == 0:
self.log_test(
f"Lambda Function - {func_type}",
True,
f"Function {func_name} is accessible"
)
else:
self.log_test(
f"Lambda Function - {func_type}",
False,
f"Function {func_name} not accessible",
{"error": result.stderr}
)
except Exception as e:
self.log_test(
f"Lambda Function - {func_type}",
False,
f"Error checking function {func_name}: {str(e)}"
)
def test_lambda_invocation(self):
"""Test actual Lambda function invocation"""
import subprocess
import tempfile
# Test generate-url function
try:
payload = {"filename": "test-image.jpg"}
with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as f:
json.dump(payload, f)
payload_file = f.name
result = subprocess.run([
'wsl', 'aws', 'lambda', 'invoke',
'--function-name', self.lambda_functions['generate_url'],
'--payload', f'file://{payload_file}',
'/tmp/response.json'
], capture_output=True, text=True, timeout=15)
if result.returncode == 0:
# Read response
with open('/tmp/response.json', 'r') as f:
response = json.load(f)
if 'uploadUrl' in str(response):
self.log_test(
"Lambda Invocation - Generate URL",
True,
"Generate URL function working correctly"
)
else:
self.log_test(
"Lambda Invocation - Generate URL",
False,
"Generate URL function returned unexpected response",
{"response": response}
)
else:
self.log_test(
"Lambda Invocation - Generate URL",
False,
"Failed to invoke Generate URL function",
{"error": result.stderr}
)
except Exception as e:
self.log_test(
"Lambda Invocation - Generate URL",
False,
f"Error testing Lambda invocation: {str(e)}"
)
def test_aws_resources(self):
"""Test AWS resources accessibility"""
import subprocess
# Test S3 buckets
for bucket_type, bucket_name in [
('Ingestion Bucket', self.aws_resources['ingestion_bucket']),
('Processed Bucket', self.aws_resources['processed_bucket'])
]:
try:
result = subprocess.run([
'wsl', 'aws', 's3', 'ls', f's3://{bucket_name}/'
], capture_output=True, text=True, timeout=10)
success = result.returncode == 0
self.log_test(
f"AWS Resource - {bucket_type}",
success,
f"Bucket {bucket_name} is accessible" if success else f"Bucket {bucket_name} not accessible"
)
except Exception as e:
self.log_test(
f"AWS Resource - {bucket_type}",
False,
f"Error checking bucket {bucket_name}: {str(e)}"
)
# Test DynamoDB table
try:
result = subprocess.run([
'wsl', 'aws', 'dynamodb', 'describe-table',
'--table-name', self.aws_resources['dynamodb_table']
], capture_output=True, text=True, timeout=10)
success = result.returncode == 0
self.log_test(
"AWS Resource - DynamoDB Table",
success,
f"DynamoDB table {self.aws_resources['dynamodb_table']} is accessible" if success else "DynamoDB table not accessible"
)
except Exception as e:
self.log_test(
"AWS Resource - DynamoDB Table",
False,
f"Error checking DynamoDB table: {str(e)}"
)
def test_frontend_configuration(self):
"""Test if frontend is configured to work with Lambda functions"""
try:
response = requests.get(self.vercel_url, timeout=10)
if response.status_code == 200:
content = response.text
# Check for signs of proper configuration
has_app_content = 'hatchmark' in content.lower()
has_react = 'react' in content.lower() or 'div' in content
config_ok = has_app_content and has_react
self.log_test(
"Frontend Configuration",
config_ok,
"Frontend appears properly configured" if config_ok else "Frontend may have configuration issues",
{
"has_app_content": has_app_content,
"has_react_elements": has_react
}
)
return config_ok
except Exception as e:
self.log_test(
"Frontend Configuration",
False,
f"Error testing frontend configuration: {str(e)}"
)
return False
def run_comprehensive_test(self):
"""Run all tests and provide a summary"""
print(f"🚀 Testing Lambda-based Deployment")
print(f"Frontend URL: {self.vercel_url}")
print(f"AWS Region: eu-west-1 (detected from SQS URL)")
print("=" * 60)
# Run all tests
self.test_frontend_accessibility()
time.sleep(0.5)
self.test_frontend_configuration()
time.sleep(0.5)
self.test_lambda_functions()
time.sleep(0.5)
self.test_lambda_invocation()
time.sleep(0.5)
self.test_aws_resources()
# Print summary
print("\n" + "=" * 60)
print("📊 TEST SUMMARY")
print("=" * 60)
passed = sum(1 for result in self.test_results if result['success'])
total = len(self.test_results)
print(f"Tests Passed: {passed}/{total}")
if passed == total:
print("🎉 All tests passed! Your deployment looks good!")
return True
elif passed >= total * 0.7: # 70% pass rate
print("✅ Most tests passed! Your deployment is mostly working.")
print("⚠️ Check the failed tests for minor issues.")
else:
print("⚠️ Several tests failed. Your deployment needs attention.")
failed_tests = [result for result in self.test_results if not result['success']]
if failed_tests:
print("\nFailed Tests:")
for test in failed_tests:
print(f" • {test['test']}: {test['message']}")
return passed >= total * 0.7
def main():
"""Main function to run the tests"""
if len(sys.argv) != 2:
print("Usage: python test_lambda_deployment.py <vercel_url>")
print("Example: python test_lambda_deployment.py https://your-app.vercel.app")
sys.exit(1)
vercel_url = sys.argv[1]
if not vercel_url.startswith('https://'):
print("❌ Vercel URL should start with https://")
sys.exit(1)
tester = LambdaDeploymentTester(vercel_url)
success = tester.run_comprehensive_test()
# Save results
results_file = 'lambda_test_results.json'
with open(results_file, 'w') as f:
json.dump({
'vercel_url': vercel_url,
'timestamp': time.strftime('%Y-%m-%d %H:%M:%S'),
'overall_success': success,
'test_results': tester.test_results
}, f, indent=2)
print(f"\n📄 Detailed results saved to: {results_file}")
sys.exit(0 if success else 1)
if __name__ == "__main__":
main()