Increase exercise robustness

* increase some timeouts
* tolerate existing security groups and rules
* add optional DEFAULT_IMAGE_NAME to select the image to boot
* fix image lists via glance

Change-Id: I31ae743e602f69a2c9f872273273f542fc4afda3
diff --git a/exercises/floating_ips.sh b/exercises/floating_ips.sh
index 9c207cd..f7b5240 100755
--- a/exercises/floating_ips.sh
+++ b/exercises/floating_ips.sh
@@ -24,9 +24,28 @@
 source ./openrc
 popd
 
-# Set some defaults
+# Max time to wait while vm goes from build to active state
+ACTIVE_TIMEOUT=${ACTIVE_TIMEOUT:-30}
 
+# Max time till the vm is bootable
+BOOT_TIMEOUT=${BOOT_TIMEOUT:-30}
+
+# Max time to wait for proper association and dis-association.
+ASSOCIATE_TIMEOUT=${ASSOCIATE_TIMEOUT:-15}
+
+# Instance type to create
+DEFAULT_INSTANCE_TYPE=${DEFAULT_INSTANCE_TYPE:-m1.tiny}
+
+# Boot this image, use first AMi image if unset
+DEFAULT_IMAGE_NAME=${DEFAULT_IMAGE_NAME:-ami}
+
+# Security group name
+SECGROUP=${SECGROUP:-test_secgroup}
+
+# Default floating IP pool name
 DEFAULT_FLOATING_POOL=${DEFAULT_FLOATING_POOL:-nova}
+
+# Additional floating IP pool and range
 TEST_FLOATING_POOL=${TEST_FLOATING_POOL:-test}
 
 # Get a token for clients that don't support service catalog
@@ -51,28 +70,32 @@
 nova image-list
 
 # But we recommend using glance directly
-glance -A $TOKEN index
+glance -f -A $TOKEN index
 
-# Let's grab the id of the first AMI image to launch
-IMAGE=`glance -A $TOKEN index | egrep ami | cut -d" " -f1`
+# Grab the id of the image to launch
+IMAGE=`glance -f -A $TOKEN index | egrep $DEFAULT_IMAGE_NAME | head -1 | cut -d" " -f1`
 
 # Security Groups
 # ---------------
-SECGROUP=test_secgroup
 
 # List of secgroups:
 nova secgroup-list
 
 # Create a secgroup
-nova secgroup-create $SECGROUP "test_secgroup description"
+if ! nova secgroup-list | grep -q $SECGROUP; then
+    nova secgroup-create $SECGROUP "$SECGROUP description"
+    if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! nova secgroup-list | grep -q $SECGROUP; do sleep 1; done"; then
+        echo "Security group not created"
+        exit 1
+    fi
+fi
 
-# determine flavor
-# ----------------
+# determinine instance type
+# -------------------------
 
-# List of flavors:
+# List of instance types:
 nova flavor-list
 
-DEFAULT_INSTANCE_TYPE=${DEFAULT_INSTANCE_TYPE:-m1.tiny}
 INSTANCE_TYPE=`nova flavor-list | grep $DEFAULT_INSTANCE_TYPE | cut -d"|" -f2`
 if [[ -z "$INSTANCE_TYPE" ]]; then
     # grab the first flavor in the list to launch if default doesn't exist
@@ -81,7 +104,7 @@
 
 NAME="myserver"
 
-nova boot --flavor $INSTANCE_TYPE --image $IMAGE $NAME --security_groups=$SECGROUP
+VM_UUID=`nova boot --flavor $INSTANCE_TYPE --image $IMAGE $NAME --security_groups=$SECGROUP | grep ' id ' | cut -d"|" -f3 | sed 's/ //g'`
 
 # Testing
 # =======
@@ -93,23 +116,14 @@
 # Waiting for boot
 # ----------------
 
-# Max time to wait while vm goes from build to active state
-ACTIVE_TIMEOUT=${ACTIVE_TIMEOUT:-10}
-
-# Max time till the vm is bootable
-BOOT_TIMEOUT=${BOOT_TIMEOUT:-15}
-
-# Max time to wait for proper association and dis-association.
-ASSOCIATE_TIMEOUT=${ASSOCIATE_TIMEOUT:-10}
-
 # check that the status is active within ACTIVE_TIMEOUT seconds
-if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova show $NAME | grep status | grep -q ACTIVE; do sleep 1; done"; then
+if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova show $VM_UUID | grep status | grep -q ACTIVE; do sleep 1; done"; then
     echo "server didn't become active!"
     exit 1
 fi
 
 # get the IP of the server
-IP=`nova show $NAME | grep "private network" | cut -d"|" -f3`
+IP=`nova show $VM_UUID | grep "private network" | cut -d"|" -f3`
 
 # for single node deployments, we can ping private ips
 MULTI_HOST=${MULTI_HOST:-0}
@@ -129,8 +143,14 @@
 # Security Groups & Floating IPs
 # ------------------------------
 
-# allow icmp traffic (ping)
-nova secgroup-add-rule $SECGROUP icmp -1 -1 0.0.0.0/0
+if ! nova secgroup-list-rules $SECGROUP | grep -q icmp; then
+    # allow icmp traffic (ping)
+    nova secgroup-add-rule $SECGROUP icmp -1 -1 0.0.0.0/0
+    if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! nova secgroup-list-rules $SECGROUP | grep -q icmp; do sleep 1; done"; then
+        echo "Security group rule not created"
+        exit 1
+    fi
+fi
 
 # List rules for a secgroup
 nova secgroup-list-rules $SECGROUP
@@ -145,7 +165,7 @@
 fi
 
 # add floating ip to our server
-nova add-floating-ip $NAME $FLOATING_IP
+nova add-floating-ip $VM_UUID $FLOATING_IP
 
 # test we can ping our floating ip within ASSOCIATE_TIMEOUT seconds
 if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! ping -c1 -w1 $FLOATING_IP; do sleep 1; done"; then
@@ -153,7 +173,7 @@
     exit 1
 fi
 
-# Allocate an IP from it
+# Allocate an IP from second floating pool
 TEST_FLOATING_IP=`nova floating-ip-create $TEST_FLOATING_POOL | grep $TEST_FLOATING_POOL | cut -d '|' -f2`
 
 # list floating addresses
@@ -182,11 +202,10 @@
 nova floating-ip-delete $TEST_FLOATING_IP
 
 # shutdown the server
-nova delete $NAME
+nova delete $VM_UUID
 
 # Delete a secgroup
 nova secgroup-delete $SECGROUP
 
 # FIXME: validate shutdown within 5 seconds
 # (nova show $NAME returns 1 or status != ACTIVE)?
-