-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-docker-api.sh
More file actions
executable file
·323 lines (282 loc) · 10.4 KB
/
test-docker-api.sh
File metadata and controls
executable file
·323 lines (282 loc) · 10.4 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
#!/bin/bash
# ADMP Docker API Test Script
# Tests all major API endpoints in the running Docker container
set -e
# Colors
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
API_URL="http://localhost:8080/api"
PASSED=0
FAILED=0
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo " ADMP Docker Container API Tests"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""
# Helper functions
pass() {
echo -e "${GREEN}✓${NC} $1"
PASSED=$((PASSED + 1))
}
fail() {
echo -e "${RED}✗${NC} $1"
echo -e " ${RED}Error: $2${NC}"
FAILED=$((FAILED + 1))
}
test_header() {
echo ""
echo -e "${BLUE}▶${NC} $1"
}
# Test 1: Health Check
test_header "Test 1: Health Check"
HEALTH=$(curl -s http://localhost:8080/health)
if echo "$HEALTH" | jq -e '.status == "healthy"' > /dev/null; then
pass "Health endpoint returned healthy status"
else
fail "Health check failed" "$HEALTH"
fi
# Test 2: System Stats
test_header "Test 2: System Stats"
STATS=$(curl -s ${API_URL}/stats)
if echo "$STATS" | jq -e '.agents and .messages' > /dev/null; then
pass "System stats endpoint returned valid data"
else
fail "System stats check failed" "$STATS"
fi
# Test 3: Register Agent A
test_header "Test 3: Register Agent A (Sender)"
AGENT_A=$(curl -s -X POST ${API_URL}/agents/register \
-H 'Content-Type: application/json' \
-d '{
"agent_type": "docker_test_sender",
"metadata": {
"test": "docker_api_test",
"role": "sender"
}
}')
AGENT_A_ID=$(echo "$AGENT_A" | jq -r '.agent_id' | tr -d '\n')
AGENT_A_SECRET=$(echo "$AGENT_A" | jq -r '.secret_key' | tr -d '\n')
if [ "$AGENT_A_ID" != "null" ] && [ -n "$AGENT_A_ID" ]; then
pass "Agent A registered: $AGENT_A_ID"
else
fail "Agent A registration failed" "$AGENT_A"
fi
# Test 4: Register Agent B with Webhook
test_header "Test 4: Register Agent B (Receiver with Webhook)"
AGENT_B=$(curl -s -X POST ${API_URL}/agents/register \
-H 'Content-Type: application/json' \
-d '{
"agent_type": "docker_test_receiver",
"metadata": {
"test": "docker_api_test",
"role": "receiver"
},
"webhook_url": "http://example.com/webhook"
}')
AGENT_B_ID=$(echo "$AGENT_B" | jq -r '.agent_id' | tr -d '\n')
AGENT_B_SECRET=$(echo "$AGENT_B" | jq -r '.secret_key' | tr -d '\n')
WEBHOOK_SECRET=$(echo "$AGENT_B" | jq -r '.webhook_secret' | tr -d '\n')
if [ "$AGENT_B_ID" != "null" ] && [ -n "$AGENT_B_ID" ]; then
pass "Agent B registered: $AGENT_B_ID"
if [ "$WEBHOOK_SECRET" != "null" ] && [ -n "$WEBHOOK_SECRET" ]; then
pass "Webhook secret generated: ${WEBHOOK_SECRET:0:20}..."
fi
else
fail "Agent B registration failed" "$AGENT_B"
fi
# Test 5: Send Heartbeat
test_header "Test 5: Send Heartbeat"
AGENT_A_ENCODED=$(printf '%s' "$AGENT_A_ID" | jq -sRr @uri)
HEARTBEAT=$(curl -s -X POST "${API_URL}/agents/${AGENT_A_ENCODED}/heartbeat" \
-H 'Content-Type: application/json' \
-d '{
"metadata": {
"last_activity": "testing_apis",
"timestamp": '$(date +%s)'
}
}')
if echo "$HEARTBEAT" | jq -e '.ok == true' > /dev/null; then
STATUS=$(echo "$HEARTBEAT" | jq -r '.status')
pass "Heartbeat sent successfully (status: $STATUS)"
else
fail "Heartbeat failed" "$HEARTBEAT"
fi
# Test 6: Send Message (simplified without signature)
test_header "Test 6: Send Message to Agent B"
# Create a simple message envelope
MESSAGE_ID="msg-test-$(date +%s)"
TIMESTAMP=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
MESSAGE_ENVELOPE='{
"version": "1.0",
"id": "'${MESSAGE_ID}'",
"type": "task.request",
"from": "'${AGENT_A_ID}'",
"to": "'${AGENT_B_ID}'",
"subject": "docker_test",
"body": {
"test": "docker_api_test",
"command": "echo hello",
"timestamp": '$(date +%s)'
},
"timestamp": "'${TIMESTAMP}'",
"ttl_sec": 3600,
"signature": {
"alg": "ed25519",
"kid": "test",
"sig": "dGVzdC1zaWduYXR1cmU="
}
}'
AGENT_B_ENCODED=$(printf '%s' "$AGENT_B_ID" | jq -sRr @uri)
SEND_RESULT=$(curl -s -X POST "${API_URL}/agents/${AGENT_B_ENCODED}/messages" \
-H 'Content-Type: application/json' \
-d "$MESSAGE_ENVELOPE")
SENT_MSG_ID=$(echo "$SEND_RESULT" | jq -r '.message_id')
if [ "$SENT_MSG_ID" != "null" ] && [ -n "$SENT_MSG_ID" ]; then
pass "Message sent to Agent B: $SENT_MSG_ID"
else
fail "Message send failed" "$SEND_RESULT"
fi
# Test 7: Pull Message from Inbox
test_header "Test 7: Pull Message from Inbox"
PULL_RESULT=$(curl -s -X POST "${API_URL}/agents/${AGENT_B_ENCODED}/inbox/pull" \
-H 'Content-Type: application/json' \
-d '{"visibility_timeout": 60}')
if [ $? -eq 0 ] && [ -n "$PULL_RESULT" ]; then
PULLED_MSG_ID=$(echo "$PULL_RESULT" | jq -r '.message_id')
if [ "$PULLED_MSG_ID" != "null" ] && [ -n "$PULLED_MSG_ID" ]; then
pass "Message pulled from inbox: $PULLED_MSG_ID"
LEASE_UNTIL=$(echo "$PULL_RESULT" | jq -r '.lease_until')
pass "Message leased until: $(date -r $((LEASE_UNTIL / 1000)) 2>/dev/null || echo $LEASE_UNTIL)"
else
# Check if inbox was empty (204)
pass "Inbox pull completed (message may have been delivered via webhook)"
fi
fi
# Test 8: Inbox Stats
test_header "Test 8: Check Inbox Statistics"
INBOX_STATS=$(curl -s "${API_URL}/agents/${AGENT_B_ENCODED}/inbox/stats")
if echo "$INBOX_STATS" | jq -e '.total != null' > /dev/null; then
TOTAL=$(echo "$INBOX_STATS" | jq -r '.total')
QUEUED=$(echo "$INBOX_STATS" | jq -r '.queued')
LEASED=$(echo "$INBOX_STATS" | jq -r '.leased')
pass "Inbox stats: total=$TOTAL, queued=$QUEUED, leased=$LEASED"
else
fail "Inbox stats check failed" "$INBOX_STATS"
fi
# Test 9: Get Webhook Configuration
test_header "Test 9: Get Webhook Configuration"
WEBHOOK_CONFIG=$(curl -s "${API_URL}/agents/${AGENT_B_ENCODED}/webhook")
if echo "$WEBHOOK_CONFIG" | jq -e '.webhook_configured == true' > /dev/null; then
WEBHOOK_URL=$(echo "$WEBHOOK_CONFIG" | jq -r '.webhook_url')
pass "Webhook configured: $WEBHOOK_URL"
else
fail "Webhook configuration check failed" "$WEBHOOK_CONFIG"
fi
# Test 10: Update Webhook
test_header "Test 10: Update Webhook URL"
WEBHOOK_UPDATE=$(curl -s -X POST "${API_URL}/agents/${AGENT_B_ENCODED}/webhook" \
-H 'Content-Type: application/json' \
-d '{
"webhook_url": "http://updated-webhook.example.com/hook"
}')
if echo "$WEBHOOK_UPDATE" | jq -e '.webhook_url' > /dev/null; then
NEW_URL=$(echo "$WEBHOOK_UPDATE" | jq -r '.webhook_url')
pass "Webhook updated: $NEW_URL"
else
fail "Webhook update failed" "$WEBHOOK_UPDATE"
fi
# Test 11: Add Trusted Agent
test_header "Test 11: Trust Management - Add Trusted Agent"
TRUST_ADD=$(curl -s -X POST "${API_URL}/agents/${AGENT_A_ENCODED}/trusted" \
-H 'Content-Type: application/json' \
-d "{\"agent_id\": \"${AGENT_B_ID}\"}")
if echo "$TRUST_ADD" | jq -e '.trusted_agents' > /dev/null; then
TRUSTED_COUNT=$(echo "$TRUST_ADD" | jq '.trusted_agents | length')
pass "Agent B added to Agent A's trusted list (count: $TRUSTED_COUNT)"
else
fail "Add trusted agent failed" "$TRUST_ADD"
fi
# Test 12: List Trusted Agents
test_header "Test 12: List Trusted Agents"
TRUST_LIST=$(curl -s "${API_URL}/agents/${AGENT_A_ENCODED}/trusted")
if echo "$TRUST_LIST" | jq -e '.trusted_agents' > /dev/null; then
TRUSTED=$(echo "$TRUST_LIST" | jq -r '.trusted_agents[]' | head -1)
pass "Trusted agents listed: $TRUSTED"
else
fail "List trusted agents failed" "$TRUST_LIST"
fi
# Test 13: Message Status
test_header "Test 13: Check Message Status"
if [ -n "$SENT_MSG_ID" ]; then
MSG_STATUS=$(curl -s "${API_URL}/messages/${SENT_MSG_ID}/status")
if echo "$MSG_STATUS" | jq -e '.status' > /dev/null; then
STATUS=$(echo "$MSG_STATUS" | jq -r '.status')
ATTEMPTS=$(echo "$MSG_STATUS" | jq -r '.attempts')
pass "Message status: $STATUS (attempts: $ATTEMPTS)"
else
fail "Message status check failed" "$MSG_STATUS"
fi
fi
# Test 14: System Stats After Activity
test_header "Test 14: System Stats After Activity"
FINAL_STATS=$(curl -s ${API_URL}/stats)
AGENT_COUNT=$(echo "$FINAL_STATS" | jq -r '.agents.total')
ONLINE_COUNT=$(echo "$FINAL_STATS" | jq -r '.agents.online')
MSG_COUNT=$(echo "$FINAL_STATS" | jq -r '.messages.total')
if [ "$AGENT_COUNT" -ge 2 ]; then
pass "System stats: $AGENT_COUNT agents ($ONLINE_COUNT online), $MSG_COUNT messages"
else
fail "Expected at least 2 agents, got $AGENT_COUNT" "$FINAL_STATS"
fi
# Test 15: Container Health
test_header "Test 15: Docker Container Health"
CONTAINER_HEALTH=$(docker inspect admp-server --format='{{.State.Health.Status}}')
if [ "$CONTAINER_HEALTH" = "healthy" ]; then
pass "Docker container health check: $CONTAINER_HEALTH"
else
fail "Container health check" "Status: $CONTAINER_HEALTH"
fi
# Test 16: OpenAPI Documentation
test_header "Test 16: OpenAPI Documentation Endpoints"
OPENAPI_JSON=$(curl -s http://localhost:8080/openapi.json)
if echo "$OPENAPI_JSON" | jq -e '.openapi' > /dev/null; then
VERSION=$(echo "$OPENAPI_JSON" | jq -r '.openapi')
pass "OpenAPI spec available (version: $VERSION)"
else
fail "OpenAPI spec check failed"
fi
DOCS_PAGE=$(curl -sL http://localhost:8080/docs/ | grep -o "<title>.*</title>")
if echo "$DOCS_PAGE" | grep -q "ADMP"; then
pass "Swagger UI documentation available"
else
fail "Swagger UI check failed"
fi
# Summary
echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo " Test Summary"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""
echo -e "${GREEN}Passed: $PASSED${NC}"
if [ $FAILED -gt 0 ]; then
echo -e "${RED}Failed: $FAILED${NC}"
exit 1
else
echo -e "${GREEN}All tests passed!${NC}"
fi
echo ""
# Display registered agents
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo " Test Agents Created"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""
echo "Agent A (Sender):"
echo " ID: $AGENT_A_ID"
echo ""
echo "Agent B (Receiver):"
echo " ID: $AGENT_B_ID"
echo " Webhook: http://updated-webhook.example.com/hook"
echo ""
exit 0