Repository navigation
Expand file tree
/
Copy pathinterface_attach_test.sh
More file actions
executable file
·231 lines (198 loc) · 7.7 KB
/
Copy pathinterface_attach_test.sh
File metadata and controls
executable file
·231 lines (198 loc) · 7.7 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
#!/bin/bash
# Network Interface Hot-Plug Test
# Colors
GREEN='\033[0;32m'
RED='\033[0;31m'
NC='\033[0m'
TESTS_PASSED=0
TESTS_FAILED=0
# 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')
echo "✓ Got auth token"
# Get project ID
PROJECT_ID=$(curl -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"}}}}}' \
| jq -r '.token.project.id')
# Create network
NETWORK_ID=$(curl -s -X POST "http://localhost:9696/v2.0/networks" \
-H "X-Auth-Token: $TOKEN" \
-H "Content-Type: application/json" \
-d '{
"network": {
"name": "test-interface-network"
}
}' | jq -r '.network.id')
echo "✓ Created network: $NETWORK_ID"
# Create subnet
SUBNET_ID=$(curl -s -X POST "http://localhost:9696/v2.0/subnets" \
-H "X-Auth-Token: $TOKEN" \
-H "Content-Type: application/json" \
-d "{
\"subnet\": {
\"name\": \"test-interface-subnet\",
\"network_id\": \"$NETWORK_ID\",
\"cidr\": \"192.168.100.0/24\",
\"ip_version\": 4,
\"enable_dhcp\": false
}
}" | jq -r '.subnet.id')
echo "✓ Created subnet: $SUBNET_ID"
# Create instance
INSTANCE_ID=$(curl -s -X POST "http://localhost:8774/v2.1/servers" \
-H "X-Auth-Token: $TOKEN" \
-H "Content-Type: application/json" \
-d '{
"server": {
"name": "test-interface-vm",
"flavorRef": "00000000-0000-0000-0000-000000000010"
}
}' | jq -r '.server.id')
if [ -z "$INSTANCE_ID" ] || [ "$INSTANCE_ID" == "null" ]; then
echo "✗ Failed to create instance"
exit 1
fi
echo "✓ Created instance: $INSTANCE_ID"
sleep 2
# Test 1: List interfaces (should be empty initially)
echo ""
echo "Test 1: List interface attachments (initially empty)"
ATTACHMENTS=$(curl -s -X GET "http://localhost:8774/v2.1/servers/$INSTANCE_ID/os-interface" \
-H "X-Auth-Token: $TOKEN" | jq '.interfaceAttachments | length')
if [ "$ATTACHMENTS" -eq 0 ]; then
echo -e "${GREEN}✓${NC} No interfaces attached (as expected)"
((TESTS_PASSED++))
else
echo -e "${RED}✗${NC} Expected 0 interfaces, found $ATTACHMENTS"
((TESTS_FAILED++))
fi
# Test 2: Attach interface (auto-create port)
echo ""
echo "Test 2: Attach interface (auto-create port)"
ATTACH_RESPONSE=$(curl -s -X POST "http://localhost:8774/v2.1/servers/$INSTANCE_ID/os-interface" \
-H "X-Auth-Token: $TOKEN" \
-H "Content-Type: application/json" \
-d "{
\"interfaceAttachment\": {
\"net_id\": \"$NETWORK_ID\"
}
}")
PORT_ID=$(echo "$ATTACH_RESPONSE" | jq -r '.interfaceAttachment.port_id')
FIXED_IP=$(echo "$ATTACH_RESPONSE" | jq -r '.interfaceAttachment.fixed_ips[0].ip_address')
MAC_ADDR=$(echo "$ATTACH_RESPONSE" | jq -r '.interfaceAttachment.mac_addr')
if [ -n "$PORT_ID" ] && [ "$PORT_ID" != "null" ]; then
echo -e "${GREEN}✓${NC} Interface attached successfully"
echo " Port ID: $PORT_ID"
echo " Fixed IP: $FIXED_IP"
echo " MAC Address: $MAC_ADDR"
((TESTS_PASSED++))
else
echo -e "${RED}✗${NC} Failed to attach interface"
echo "Response: $ATTACH_RESPONSE"
((TESTS_FAILED++))
fi
# Test 3: List interfaces (should show 1)
echo ""
echo "Test 3: List interface attachments (should show 1)"
sleep 1
ATTACHMENTS=$(curl -s -X GET "http://localhost:8774/v2.1/servers/$INSTANCE_ID/os-interface" \
-H "X-Auth-Token: $TOKEN")
ATTACHMENTS_COUNT=$(echo "$ATTACHMENTS" | jq '.interfaceAttachments | length')
if [ "$ATTACHMENTS_COUNT" -eq 1 ]; then
echo -e "${GREEN}✓${NC} Found 1 interface attachment"
ATTACHED_PORT=$(echo "$ATTACHMENTS" | jq -r '.interfaceAttachments[0].port_id')
echo " Attached port: $ATTACHED_PORT"
((TESTS_PASSED++))
else
echo -e "${RED}✗${NC} Expected 1 interface, found $ATTACHMENTS_COUNT"
((TESTS_FAILED++))
fi
# Test 4: Attach second interface with specific IP
echo ""
echo "Test 4: Attach second interface with specific IP"
ATTACH2_RESPONSE=$(curl -s -X POST "http://localhost:8774/v2.1/servers/$INSTANCE_ID/os-interface" \
-H "X-Auth-Token: $TOKEN" \
-H "Content-Type: application/json" \
-d "{
\"interfaceAttachment\": {
\"net_id\": \"$NETWORK_ID\",
\"fixed_ip\": \"192.168.100.50\"
}
}")
PORT2_ID=$(echo "$ATTACH2_RESPONSE" | jq -r '.interfaceAttachment.port_id')
FIXED_IP2=$(echo "$ATTACH2_RESPONSE" | jq -r '.interfaceAttachment.fixed_ips[0].ip_address')
if [ "$FIXED_IP2" == "192.168.100.50" ]; then
echo -e "${GREEN}✓${NC} Second interface attached with requested IP: $FIXED_IP2"
echo " Port ID: $PORT2_ID"
((TESTS_PASSED++))
else
echo -e "${RED}✗${NC} Failed to attach with specific IP"
echo "Expected: 192.168.100.50, Got: $FIXED_IP2"
((TESTS_FAILED++))
fi
# Test 5: List interfaces (should show 2)
echo ""
echo "Test 5: List interface attachments (should show 2)"
sleep 1
ATTACHMENTS_COUNT=$(curl -s -X GET "http://localhost:8774/v2.1/servers/$INSTANCE_ID/os-interface" \
-H "X-Auth-Token: $TOKEN" | jq '.interfaceAttachments | length')
if [ "$ATTACHMENTS_COUNT" -eq 2 ]; then
echo -e "${GREEN}✓${NC} Found 2 interface attachments"
((TESTS_PASSED++))
else
echo -e "${RED}✗${NC} Expected 2 interfaces, found $ATTACHMENTS_COUNT"
((TESTS_FAILED++))
fi
# Test 6: Detach first interface
echo ""
echo "Test 6: Detach first interface"
HTTP_CODE=$(curl -s -w "%{http_code}" -o /dev/null -X DELETE "http://localhost:8774/v2.1/servers/$INSTANCE_ID/os-interface/$PORT_ID" \
-H "X-Auth-Token: $TOKEN")
if [ "$HTTP_CODE" == "202" ]; then
echo -e "${GREEN}✓${NC} Interface detached successfully (HTTP 202)"
((TESTS_PASSED++))
else
echo -e "${RED}✗${NC} Failed to detach interface (HTTP $HTTP_CODE)"
((TESTS_FAILED++))
fi
# Test 7: List interfaces (should show 1 remaining)
echo ""
echo "Test 7: List interface attachments (should show 1 remaining)"
sleep 1
ATTACHMENTS=$(curl -s -X GET "http://localhost:8774/v2.1/servers/$INSTANCE_ID/os-interface" \
-H "X-Auth-Token: $TOKEN")
ATTACHMENTS_COUNT=$(echo "$ATTACHMENTS" | jq '.interfaceAttachments | length')
if [ "$ATTACHMENTS_COUNT" -eq 1 ]; then
echo -e "${GREEN}✓${NC} Found 1 remaining interface attachment"
REMAINING_PORT=$(echo "$ATTACHMENTS" | jq -r '.interfaceAttachments[0].port_id')
if [ "$REMAINING_PORT" == "$PORT2_ID" ]; then
echo " Correctly shows second interface still attached"
else
echo -e "${RED}✗${NC} Wrong interface remaining"
fi
((TESTS_PASSED++))
else
echo -e "${RED}✗${NC} Expected 1 interface, found $ATTACHMENTS_COUNT"
((TESTS_FAILED++))
fi
# Cleanup
echo ""
echo "Cleaning up..."
curl -s -X DELETE "http://localhost:8774/v2.1/servers/$INSTANCE_ID" \
-H "X-Auth-Token: $TOKEN" > /dev/null
curl -s -X DELETE "http://localhost:9696/v2.0/networks/$NETWORK_ID" \
-H "X-Auth-Token: $TOKEN" > /dev/null
echo "✓ Cleanup complete"
echo ""
echo "========================================"
echo -e "Tests passed: ${GREEN}$TESTS_PASSED${NC}"
echo -e "Tests failed: ${RED}$TESTS_FAILED${NC}"
if [ $TESTS_FAILED -eq 0 ]; then
echo -e "${GREEN}✅ All interface attachment tests passed${NC}"
else
echo -e "${RED}❌ Some tests failed${NC}"
fi
echo "========================================"