Repository navigation
Expand file tree
/
Copy pathquota_test.sh
More file actions
executable file
·263 lines (221 loc) · 8.74 KB
/
Copy pathquota_test.sh
File metadata and controls
executable file
·263 lines (221 loc) · 8.74 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
#!/bin/bash
# Quota Management Test Suite
# Colors
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
TESTS_PASSED=0
TESTS_FAILED=0
TOTAL_TESTS=0
log_test() {
((TOTAL_TESTS++))
echo -e "${YELLOW}[TEST $TOTAL_TESTS]${NC} $1"
}
log_pass() {
echo -e "${GREEN}✓${NC} $1"
((TESTS_PASSED++))
}
log_fail() {
echo -e "${RED}✗${NC} $1"
((TESTS_FAILED++))
}
# Get token
TOKEN=$(curl -i -s -X POST "http://localhost:35357/v3/auth/tokens" \
-H "Content-Type: application/json" \
-d '{"auth":{"identity":{"methods":["password"],"password":{"user":{"name":"admin","password":"secret","domain":{"name":"Default"}}}},"scope":{"project":{"name":"default","domain":{"name":"Default"}}}}}' \
| grep -i "X-Subject-Token:" | awk '{print $2}' | tr -d '\r')
log_pass "Got authentication token"
# Server create refuses an image that does not exist or is not active, so the test boots
# from one it creates and uploads a few bytes to.
IMAGE_ID=$(curl -s -X POST "http://localhost:9292/v2/images" \
-H "X-Auth-Token: $TOKEN" \
-H "Content-Type: application/json" \
-d '{"name": "quota-test-image", "disk_format": "raw", "container_format": "bare"}' | jq -r '.id')
curl -s -o /dev/null -X PUT "http://localhost:9292/v2/images/$IMAGE_ID/file" \
-H "X-Auth-Token: $TOKEN" \
-H "Content-Type: application/octet-stream" \
--data-binary "lightstack test image"
PROJECT_ID="00000000-0000-0000-0000-000000000002"
# Test 1: Get quota set
log_test "Get quota set for project"
QUOTA_RESPONSE=$(curl -s -X GET "http://localhost:8774/v2.1/os-quota-sets/$PROJECT_ID" \
-H "X-Auth-Token: $TOKEN")
INSTANCES_LIMIT=$(echo "$QUOTA_RESPONSE" | jq -r '.quota_set.instances')
CORES_LIMIT=$(echo "$QUOTA_RESPONSE" | jq -r '.quota_set.cores')
RAM_LIMIT=$(echo "$QUOTA_RESPONSE" | jq -r '.quota_set.ram')
if [ "$INSTANCES_LIMIT" != "null" ] && [ -n "$INSTANCES_LIMIT" ]; then
log_pass "Retrieved quota set (instances: $INSTANCES_LIMIT, cores: $CORES_LIMIT, ram: $RAM_LIMIT)"
else
log_fail "Failed to retrieve quota set"
echo "Response: $QUOTA_RESPONSE"
fi
# Test 2: Verify usage information is included
log_test "Verify usage information in quota set"
INSTANCES_USED=$(echo "$QUOTA_RESPONSE" | jq -r '.quota_set.instances_used')
CORES_USED=$(echo "$QUOTA_RESPONSE" | jq -r '.quota_set.cores_used')
if [ "$INSTANCES_USED" != "null" ]; then
log_pass "Usage information included (instances_used: $INSTANCES_USED, cores_used: $CORES_USED)"
else
log_fail "Usage information missing"
fi
# Test 3: Get default quotas
log_test "Get default quota set"
DEFAULTS_RESPONSE=$(curl -s -X GET "http://localhost:8774/v2.1/os-quota-sets/$PROJECT_ID/defaults" \
-H "X-Auth-Token: $TOKEN")
DEFAULT_INSTANCES=$(echo "$DEFAULTS_RESPONSE" | jq -r '.quota_set.instances')
if [ "$DEFAULT_INSTANCES" == "10" ]; then
log_pass "Retrieved default quotas (instances: $DEFAULT_INSTANCES)"
else
log_fail "Failed to retrieve default quotas"
fi
# Test 4: Update quota limits
log_test "Update quota limits (set instances to 3)"
UPDATE_RESPONSE=$(curl -s -X PUT "http://localhost:8774/v2.1/os-quota-sets/$PROJECT_ID" \
-H "X-Auth-Token: $TOKEN" \
-H "Content-Type: application/json" \
-d '{
"quota_set": {
"instances": 3,
"cores": 6
}
}')
NEW_INSTANCES_LIMIT=$(echo "$UPDATE_RESPONSE" | jq -r '.quota_set.instances')
if [ "$NEW_INSTANCES_LIMIT" == "3" ]; then
log_pass "Updated quota limits (instances: $NEW_INSTANCES_LIMIT)"
else
log_fail "Failed to update quota limits"
echo "Response: $UPDATE_RESPONSE"
fi
# Test 5: Create instances up to quota limit
log_test "Create instances up to quota limit (3)"
CREATED_INSTANCES=()
for i in {1..3}; do
INSTANCE_RESPONSE=$(curl -s -X POST "http://localhost:8774/v2.1/servers" \
-H "X-Auth-Token: $TOKEN" \
-H "Content-Type: application/json" \
-d "{
\"server\": {
\"name\": \"quota-test-vm-$i\",
\"flavorRef\": \"00000000-0000-0000-0000-000000000010\",
\"imageRef\": \"$IMAGE_ID\"
}
}")
INSTANCE_ID=$(echo "$INSTANCE_RESPONSE" | jq -r '.server.id')
if [ -n "$INSTANCE_ID" ] && [ "$INSTANCE_ID" != "null" ]; then
CREATED_INSTANCES+=("$INSTANCE_ID")
fi
done
if [ ${#CREATED_INSTANCES[@]} -eq 3 ]; then
log_pass "Created 3 instances successfully"
else
log_fail "Failed to create 3 instances (created ${#CREATED_INSTANCES[@]})"
fi
# Test 6: Try to exceed quota (should fail)
log_test "Try to create instance exceeding quota (should fail with HTTP 403)"
EXCEEDED_RESPONSE=$(curl -s -w "\nHTTP_CODE:%{http_code}" -X POST "http://localhost:8774/v2.1/servers" \
-H "X-Auth-Token: $TOKEN" \
-H "Content-Type: application/json" \
-d '{
"server": {
"name": "quota-test-vm-exceed",
"flavorRef": "00000000-0000-0000-0000-000000000010",
"imageRef": "'"$IMAGE_ID"'"
}
}')
HTTP_CODE=$(echo "$EXCEEDED_RESPONSE" | grep "HTTP_CODE:" | sed 's/HTTP_CODE://')
ERROR_MSG=$(echo "$EXCEEDED_RESPONSE" | grep -v "HTTP_CODE:" | jq -r '.forbidden.message' 2>/dev/null)
# Upstream answers a create over quota with 403 and TooManyInstances' message.
if [ "$HTTP_CODE" == "403" ] && [[ "$ERROR_MSG" == "Quota exceeded for instances: "* ]]; then
log_pass "Correctly rejected with HTTP 403 (Quota exceeded)"
else
log_fail "Expected HTTP 403 naming the instances quota, got HTTP $HTTP_CODE"
echo "Error message: $ERROR_MSG"
fi
# Test 7: Verify usage equals limit
log_test "Verify usage equals limit after creating 3 instances"
QUOTA_RESPONSE=$(curl -s -X GET "http://localhost:8774/v2.1/os-quota-sets/$PROJECT_ID" \
-H "X-Auth-Token: $TOKEN")
INSTANCES_USED=$(echo "$QUOTA_RESPONSE" | jq -r '.quota_set.instances_used')
INSTANCES_LIMIT=$(echo "$QUOTA_RESPONSE" | jq -r '.quota_set.instances')
if [ "$INSTANCES_USED" == "$INSTANCES_LIMIT" ]; then
log_pass "Usage equals limit (used: $INSTANCES_USED, limit: $INSTANCES_LIMIT)"
else
log_fail "Usage does not equal limit (used: $INSTANCES_USED, limit: $INSTANCES_LIMIT)"
fi
# Test 8: Delete one instance
log_test "Delete one instance to free quota"
if [ ${#CREATED_INSTANCES[@]} -gt 0 ]; then
DELETE_ID="${CREATED_INSTANCES[0]}"
curl -s -X DELETE "http://localhost:8774/v2.1/servers/$DELETE_ID" -H "X-Auth-Token: $TOKEN" > /dev/null
sleep 1
log_pass "Deleted instance: $DELETE_ID"
else
log_fail "No instances to delete"
fi
# Test 9: Create instance after freeing quota (should succeed)
log_test "Create instance after freeing quota (should succeed)"
NEW_INSTANCE_RESPONSE=$(curl -s -X POST "http://localhost:8774/v2.1/servers" \
-H "X-Auth-Token: $TOKEN" \
-H "Content-Type: application/json" \
-d '{
"server": {
"name": "quota-test-vm-after-delete",
"flavorRef": "00000000-0000-0000-0000-000000000010",
"imageRef": "'"$IMAGE_ID"'"
}
}')
NEW_INSTANCE_ID=$(echo "$NEW_INSTANCE_RESPONSE" | jq -r '.server.id')
if [ -n "$NEW_INSTANCE_ID" ] && [ "$NEW_INSTANCE_ID" != "null" ]; then
log_pass "Created instance after freeing quota: $NEW_INSTANCE_ID"
CREATED_INSTANCES+=("$NEW_INSTANCE_ID")
else
log_fail "Failed to create instance after freeing quota"
fi
# Test 10: Test cores quota
log_test "Test cores quota enforcement"
# Get current cores usage
QUOTA_RESPONSE=$(curl -s -X GET "http://localhost:8774/v2.1/os-quota-sets/$PROJECT_ID" \
-H "X-Auth-Token: $TOKEN")
CORES_LIMIT=$(echo "$QUOTA_RESPONSE" | jq -r '.quota_set.cores')
if [ "$CORES_LIMIT" == "6" ]; then
log_pass "Cores quota is set to 6"
else
log_fail "Cores quota not set correctly"
fi
# Cleanup
echo ""
echo "Cleaning up test resources..."
for instance_id in "${CREATED_INSTANCES[@]}"; do
curl -s -X DELETE "http://localhost:8774/v2.1/servers/$instance_id" -H "X-Auth-Token: $TOKEN" > /dev/null
done
curl -s -X DELETE "http://localhost:9292/v2/images/$IMAGE_ID" -H "X-Auth-Token: $TOKEN" > /dev/null
# Reset quotas to defaults
curl -s -X PUT "http://localhost:8774/v2.1/os-quota-sets/$PROJECT_ID" \
-H "X-Auth-Token: $TOKEN" \
-H "Content-Type: application/json" \
-d '{
"quota_set": {
"instances": 10,
"cores": 20,
"ram": 51200
}
}' > /dev/null
# Print summary
echo ""
echo "========================================"
echo " QUOTA MANAGEMENT TEST SUMMARY"
echo "========================================"
echo -e "Total Tests: $TOTAL_TESTS"
echo -e "${GREEN}Passed: $TESTS_PASSED${NC}"
echo -e "${RED}Failed: $TESTS_FAILED${NC}"
echo "========================================"
echo ""
if [ $TESTS_FAILED -eq 0 ]; then
echo -e "${GREEN}✅ ALL QUOTA TESTS PASSED${NC}"
exit 0
else
echo -e "${RED}❌ SOME TESTS FAILED${NC}"
exit 1
fi