blob: bc33fe8279295a683c07ae44bbdd897c60a8fec5 [file] [log] [blame]
Dan Wendlandtea23e682012-08-22 05:53:17 -07001#!/usr/bin/env bash
2#
3
Dean Troyer6d04fd72012-12-21 11:03:37 -06004# **quantum-adv-test.sh**
Dan Wendlandtea23e682012-08-22 05:53:17 -07005
Dean Troyer6d04fd72012-12-21 11:03:37 -06006# Perform integration testing of Nova and other components with Quantum.
Dan Wendlandtea23e682012-08-22 05:53:17 -07007
8echo "*********************************************************************"
9echo "Begin DevStack Exercise: $0"
10echo "*********************************************************************"
11
12# This script exits on an error so that errors don't compound and you see
13# only the first error that occured.
14
15set -o errtrace
Dean Troyer6d04fd72012-12-21 11:03:37 -060016
Dan Wendlandtea23e682012-08-22 05:53:17 -070017trap failed ERR
18failed() {
19 local r=$?
20 set +o errtrace
21 set +o xtrace
22 echo "Failed to execute"
23 echo "Starting cleanup..."
24 delete_all
25 echo "Finished cleanup"
26 exit $r
27}
28
29# Print the commands being run so that we can see the command that triggers
30# an error. It is also useful for following allowing as the install occurs.
31set -o xtrace
32
Dan Wendlandtea23e682012-08-22 05:53:17 -070033# Environment
Dean Troyer6d04fd72012-12-21 11:03:37 -060034# -----------
Dan Wendlandtea23e682012-08-22 05:53:17 -070035
36# Keep track of the current directory
37EXERCISE_DIR=$(cd $(dirname "$0") && pwd)
38TOP_DIR=$(cd $EXERCISE_DIR/..; pwd)
39
40# Import common functions
41source $TOP_DIR/functions
42
43# Import configuration
44source $TOP_DIR/openrc
45
Dan Wendlandtea23e682012-08-22 05:53:17 -070046# If quantum is not enabled we exit with exitcode 55 which mean
47# exercise is skipped.
48is_service_enabled quantum && is_service_enabled q-agt && is_service_enabled q-dhcp || exit 55
49
Nachi Ueno5db5bfa2012-10-29 11:25:29 -070050# Import quantum fucntions
51source $TOP_DIR/lib/quantum
Nachi Ueno5db5bfa2012-10-29 11:25:29 -070052
53# Import exercise configuration
54source $TOP_DIR/exerciserc
55
Dean Troyer6d04fd72012-12-21 11:03:37 -060056# Quantum Settings
57# ----------------
Dan Wendlandtea23e682012-08-22 05:53:17 -070058
59TENANTS="DEMO1"
60# TODO (nati)_Test public network
61#TENANTS="DEMO1,DEMO2"
62
63PUBLIC_NAME="admin"
64DEMO1_NAME="demo1"
65DEMO2_NAME="demo2"
66
67PUBLIC_NUM_NET=1
68DEMO1_NUM_NET=1
69DEMO2_NUM_NET=2
70
71PUBLIC_NET1_CIDR="200.0.0.0/24"
Nachi Ueno5db5bfa2012-10-29 11:25:29 -070072DEMO1_NET1_CIDR="10.10.0.0/24"
73DEMO2_NET1_CIDR="10.20.0.0/24"
74DEMO2_NET2_CIDR="10.20.1.0/24"
Dan Wendlandtea23e682012-08-22 05:53:17 -070075
76PUBLIC_NET1_GATEWAY="200.0.0.1"
Nachi Ueno5db5bfa2012-10-29 11:25:29 -070077DEMO1_NET1_GATEWAY="10.10.0.1"
78DEMO2_NET1_GATEWAY="10.20.0.1"
79DEMO2_NET2_GATEWAY="10.20.1.1"
Dan Wendlandtea23e682012-08-22 05:53:17 -070080
81PUBLIC_NUM_VM=1
82DEMO1_NUM_VM=1
83DEMO2_NUM_VM=2
84
85PUBLIC_VM1_NET='admin-net1'
86DEMO1_VM1_NET='demo1-net1'
87# Multinic settings. But this is fail without nic setting in OS image
88DEMO2_VM1_NET='demo2-net1'
89DEMO2_VM2_NET='demo2-net2'
90
91PUBLIC_NUM_ROUTER=1
92DEMO1_NUM_ROUTER=1
93DEMO2_NUM_ROUTER=1
94
95PUBLIC_ROUTER1_NET="admin-net1"
96DEMO1_ROUTER1_NET="demo1-net1"
97DEMO2_ROUTER1_NET="demo2-net1"
98
Dan Wendlandtea23e682012-08-22 05:53:17 -070099KEYSTONE="keystone"
100
Dean Troyer6d04fd72012-12-21 11:03:37 -0600101# Manually create a token by querying keystone (sending JSON data). Keystone
Dan Wendlandtea23e682012-08-22 05:53:17 -0700102# returns a token and catalog of endpoints. We use python to parse the token
103# and save it.
104
105TOKEN=`keystone token-get | grep ' id ' | awk '{print $4}'`
106
Dean Troyer6d04fd72012-12-21 11:03:37 -0600107# Various functions
108# -----------------
109
Dan Wendlandtea23e682012-08-22 05:53:17 -0700110function foreach_tenant {
111 COMMAND=$1
112 for TENANT in ${TENANTS//,/ };do
113 eval ${COMMAND//%TENANT%/$TENANT}
114 done
115}
116
117function foreach_tenant_resource {
118 COMMAND=$1
119 RESOURCE=$2
120 for TENANT in ${TENANTS//,/ };do
121 eval 'NUM=$'"${TENANT}_NUM_$RESOURCE"
122 for i in `seq $NUM`;do
123 local COMMAND_LOCAL=${COMMAND//%TENANT%/$TENANT}
124 COMMAND_LOCAL=${COMMAND_LOCAL//%NUM%/$i}
125 eval $COMMAND_LOCAL
126 done
127 done
128}
129
130function foreach_tenant_vm {
131 COMMAND=$1
132 foreach_tenant_resource "$COMMAND" 'VM'
133}
134
135function foreach_tenant_net {
136 COMMAND=$1
137 foreach_tenant_resource "$COMMAND" 'NET'
138}
139
140function get_image_id {
141 local IMAGE_ID=$(glance image-list | egrep " $DEFAULT_IMAGE_NAME " | get_field 1)
142 echo "$IMAGE_ID"
143}
144
145function get_tenant_id {
146 local TENANT_NAME=$1
147 local TENANT_ID=`keystone tenant-list | grep " $TENANT_NAME " | head -n 1 | get_field 1`
148 echo "$TENANT_ID"
149}
150
151function get_user_id {
152 local USER_NAME=$1
153 local USER_ID=`keystone user-list | grep $USER_NAME | awk '{print $2}'`
154 echo "$USER_ID"
155}
156
157function get_role_id {
158 local ROLE_NAME=$1
159 local ROLE_ID=`keystone role-list | grep $ROLE_NAME | awk '{print $2}'`
160 echo "$ROLE_ID"
161}
162
163function get_network_id {
164 local NETWORK_NAME="$1"
165 local NETWORK_ID=`quantum net-list -F id -- --name=$NETWORK_NAME | awk "NR==4" | awk '{print $2}'`
166 echo $NETWORK_ID
167}
168
169function get_flavor_id {
170 local INSTANCE_TYPE=$1
171 local FLAVOR_ID=`nova flavor-list | grep $INSTANCE_TYPE | awk '{print $2}'`
172 echo "$FLAVOR_ID"
173}
174
175function confirm_server_active {
176 local VM_UUID=$1
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700177 if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova show $VM_UUID | grep status | grep -q ACTIVE; do sleep 1; done"; then
Dean Troyer6d04fd72012-12-21 11:03:37 -0600178 echo "server '$VM_UUID' did not become active!"
179 false
180 fi
Dan Wendlandtea23e682012-08-22 05:53:17 -0700181}
182
183function add_tenant {
184 local TENANT=$1
185 local USER=$2
186
187 $KEYSTONE tenant-create --name=$TENANT
188 $KEYSTONE user-create --name=$USER --pass=${ADMIN_PASSWORD}
189
190 local USER_ID=$(get_user_id $USER)
191 local TENANT_ID=$(get_tenant_id $TENANT)
192
193 $KEYSTONE user-role-add --user-id $USER_ID --role-id $(get_role_id Member) --tenant-id $TENANT_ID
194}
195
196function remove_tenant {
197 local TENANT=$1
198 local TENANT_ID=$(get_tenant_id $TENANT)
Dan Wendlandtea23e682012-08-22 05:53:17 -0700199 $KEYSTONE tenant-delete $TENANT_ID
200}
201
202function remove_user {
203 local USER=$1
204 local USER_ID=$(get_user_id $USER)
Dan Wendlandtea23e682012-08-22 05:53:17 -0700205 $KEYSTONE user-delete $USER_ID
206}
207
Dan Wendlandtea23e682012-08-22 05:53:17 -0700208function create_tenants {
209 source $TOP_DIR/openrc admin admin
210 add_tenant demo1 demo1 demo1
211 add_tenant demo2 demo2 demo2
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700212 source $TOP_DIR/openrc demo demo
Dan Wendlandtea23e682012-08-22 05:53:17 -0700213}
214
215function delete_tenants_and_users {
216 source $TOP_DIR/openrc admin admin
217 remove_user demo1
218 remove_tenant demo1
219 remove_user demo2
220 remove_tenant demo2
221 echo "removed all tenants"
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700222 source $TOP_DIR/openrc demo demo
Dan Wendlandtea23e682012-08-22 05:53:17 -0700223}
224
225function create_network {
226 local TENANT=$1
227 local GATEWAY=$2
228 local CIDR=$3
229 local NUM=$4
230 local EXTRA=$5
231 local NET_NAME="${TENANT}-net$NUM"
232 local ROUTER_NAME="${TENANT}-router${NUM}"
233 source $TOP_DIR/openrc admin admin
234 local TENANT_ID=$(get_tenant_id $TENANT)
235 source $TOP_DIR/openrc $TENANT $TENANT
236 local NET_ID=$(quantum net-create --tenant_id $TENANT_ID $NET_NAME $EXTRA| grep ' id ' | awk '{print $4}' )
237 quantum subnet-create --ip_version 4 --tenant_id $TENANT_ID --gateway $GATEWAY $NET_ID $CIDR
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700238 quantum-debug probe-create $NET_ID
239 source $TOP_DIR/openrc demo demo
Dan Wendlandtea23e682012-08-22 05:53:17 -0700240}
241
242function create_networks {
243 foreach_tenant_net 'create_network ${%TENANT%_NAME} ${%TENANT%_NET%NUM%_GATEWAY} ${%TENANT%_NET%NUM%_CIDR} %NUM% ${%TENANT%_NET%NUM%_EXTRA}'
244 #TODO(nati) test security group function
245 # allow ICMP for both tenant's security groups
246 #source $TOP_DIR/openrc demo1 demo1
247 #$NOVA secgroup-add-rule default icmp -1 -1 0.0.0.0/0
248 #source $TOP_DIR/openrc demo2 demo2
249 #$NOVA secgroup-add-rule default icmp -1 -1 0.0.0.0/0
250}
251
252function create_vm {
253 local TENANT=$1
254 local NUM=$2
255 local NET_NAMES=$3
256 source $TOP_DIR/openrc $TENANT $TENANT
257 local NIC=""
258 for NET_NAME in ${NET_NAMES//,/ };do
259 NIC="$NIC --nic net-id="`get_network_id $NET_NAME`
260 done
261 #TODO (nati) Add multi-nic test
262 #TODO (nati) Add public-net test
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700263 local VM_UUID=`nova boot --flavor $(get_flavor_id m1.tiny) \
Dan Wendlandtea23e682012-08-22 05:53:17 -0700264 --image $(get_image_id) \
265 $NIC \
266 $TENANT-server$NUM | grep ' id ' | cut -d"|" -f3 | sed 's/ //g'`
267 die_if_not_set VM_UUID "Failure launching $TENANT-server$NUM" VM_UUID
268 confirm_server_active $VM_UUID
269}
270
271function create_vms {
272 foreach_tenant_vm 'create_vm ${%TENANT%_NAME} %NUM% ${%TENANT%_VM%NUM%_NET}'
273}
274
275function ping_ip {
276 # Test agent connection. Assumes namespaces are disabled, and
277 # that DHCP is in use, but not L3
278 local VM_NAME=$1
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700279 local NET_NAME=$2
280 IP=`nova show $VM_NAME | grep 'network' | awk '{print $5}'`
281 ping_check $NET_NAME $IP $BOOT_TIMEOUT
Dan Wendlandtea23e682012-08-22 05:53:17 -0700282}
283
284function check_vm {
285 local TENANT=$1
286 local NUM=$2
287 local VM_NAME="$TENANT-server$NUM"
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700288 local NET_NAME=$3
Dan Wendlandtea23e682012-08-22 05:53:17 -0700289 source $TOP_DIR/openrc $TENANT $TENANT
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700290 ping_ip $VM_NAME $NET_NAME
Dan Wendlandtea23e682012-08-22 05:53:17 -0700291 # TODO (nati) test ssh connection
292 # TODO (nati) test inter connection between vm
Dan Wendlandtea23e682012-08-22 05:53:17 -0700293 # TODO (nati) test dhcp host routes
294 # TODO (nati) test multi-nic
Dan Wendlandtea23e682012-08-22 05:53:17 -0700295}
296
297function check_vms {
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700298 foreach_tenant_vm 'check_vm ${%TENANT%_NAME} %NUM% ${%TENANT%_VM%NUM%_NET}'
Dan Wendlandtea23e682012-08-22 05:53:17 -0700299}
300
301function shutdown_vm {
302 local TENANT=$1
303 local NUM=$2
304 source $TOP_DIR/openrc $TENANT $TENANT
305 VM_NAME=${TENANT}-server$NUM
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700306 nova delete $VM_NAME
Dan Wendlandtea23e682012-08-22 05:53:17 -0700307}
308
309function shutdown_vms {
310 foreach_tenant_vm 'shutdown_vm ${%TENANT%_NAME} %NUM%'
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700311 if ! timeout $TERMINATE_TIMEOUT sh -c "while nova list | grep -q ACTIVE; do sleep 1; done"; then
Dan Wendlandtea23e682012-08-22 05:53:17 -0700312 echo "Some VMs failed to shutdown"
313 false
314 fi
315}
316
317function delete_network {
318 local TENANT=$1
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700319 local NUM=$2
320 local NET_NAME="${TENANT}-net$NUM"
Dan Wendlandtea23e682012-08-22 05:53:17 -0700321 source $TOP_DIR/openrc admin admin
322 local TENANT_ID=$(get_tenant_id $TENANT)
323 #TODO(nati) comment out until l3-agent merged
324 #for res in port subnet net router;do
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700325 for net_id in `quantum net-list -c id -c name | grep $NET_NAME | awk '{print $2}'`;do
326 delete_probe $net_id
327 quantum subnet-list | grep $net_id | awk '{print $2}' | xargs -I% quantum subnet-delete %
328 quantum net-delete $net_id
Dan Wendlandtea23e682012-08-22 05:53:17 -0700329 done
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700330 source $TOP_DIR/openrc demo demo
Dan Wendlandtea23e682012-08-22 05:53:17 -0700331}
332
333function delete_networks {
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700334 foreach_tenant_net 'delete_network ${%TENANT%_NAME} ${%NUM%}'
Dan Wendlandtea23e682012-08-22 05:53:17 -0700335 #TODO(nati) add secuirty group check after it is implemented
336 # source $TOP_DIR/openrc demo1 demo1
337 # nova secgroup-delete-rule default icmp -1 -1 0.0.0.0/0
338 # source $TOP_DIR/openrc demo2 demo2
339 # nova secgroup-delete-rule default icmp -1 -1 0.0.0.0/0
340}
341
342function create_all {
343 create_tenants
344 create_networks
345 create_vms
346}
347
348function delete_all {
349 shutdown_vms
350 delete_networks
351 delete_tenants_and_users
352}
353
354function all {
355 create_all
356 check_vms
357 delete_all
358}
359
Dean Troyer6d04fd72012-12-21 11:03:37 -0600360# Test functions
361# --------------
362
Dan Wendlandtea23e682012-08-22 05:53:17 -0700363function test_functions {
364 IMAGE=$(get_image_id)
365 echo $IMAGE
366
367 TENANT_ID=$(get_tenant_id demo)
368 echo $TENANT_ID
369
370 FLAVOR_ID=$(get_flavor_id m1.tiny)
371 echo $FLAVOR_ID
372
373 NETWORK_ID=$(get_network_id admin)
374 echo $NETWORK_ID
375}
376
Dean Troyer6d04fd72012-12-21 11:03:37 -0600377# Usage and main
378# --------------
379
Dan Wendlandtea23e682012-08-22 05:53:17 -0700380usage() {
381 echo "$0: [-h]"
382 echo " -h, --help Display help message"
383 echo " -t, --tenant Create tenants"
384 echo " -n, --net Create networks"
385 echo " -v, --vm Create vms"
386 echo " -c, --check Check connection"
387 echo " -x, --delete-tenants Delete tenants"
388 echo " -y, --delete-nets Delete networks"
389 echo " -z, --delete-vms Delete vms"
390 echo " -T, --test Test functions"
391}
392
393main() {
394
395 echo Description
396 echo
397 echo Copyright 2012, Cisco Systems
398 echo Copyright 2012, Nicira Networks, Inc.
399 echo Copyright 2012, NTT MCL, Inc.
400 echo
401 echo Please direct any questions to dedutta@cisco.com, dan@nicira.com, nachi@nttmcl.com
402 echo
403
404
405 if [ $# -eq 0 ] ; then
406 # if no args are provided, run all tests
407 all
408 else
409
410 while [ "$1" != "" ]; do
411 case $1 in
412 -h | --help ) usage
413 exit
414 ;;
415 -n | --net ) create_networks
416 exit
417 ;;
418 -v | --vm ) create_vms
419 exit
420 ;;
421 -t | --tenant ) create_tenants
422 exit
423 ;;
424 -c | --check ) check_vms
425 exit
426 ;;
427 -T | --test ) test_functions
428 exit
429 ;;
430 -x | --delete-tenants ) delete_tenants_and_users
431 exit
432 ;;
433 -y | --delete-nets ) delete_networks
434 exit
435 ;;
436 -z | --delete-vms ) shutdown_vms
437 exit
438 ;;
439 -a | --all ) all
440 exit
441 ;;
442 * ) usage
443 exit 1
444 esac
445 shift
446 done
447 fi
448}
449
Dean Troyer6d04fd72012-12-21 11:03:37 -0600450# Kick off script
451# ---------------
Dan Wendlandtea23e682012-08-22 05:53:17 -0700452
Dan Wendlandtea23e682012-08-22 05:53:17 -0700453echo $*
454main $*
455
456set +o xtrace
457echo "*********************************************************************"
458echo "SUCCESS: End DevStack Exercise: $0"
459echo "*********************************************************************"