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/euca.sh b/exercises/euca.sh
index 2f7a17b..e569196 100755
--- a/exercises/euca.sh
+++ b/exercises/euca.sh
@@ -20,6 +20,18 @@
 source ./openrc
 popd
 
+# 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}
+
 # Find a machine image to boot
 IMAGE=`euca-describe-images | grep machine | cut -f2 | head -n1`
 
@@ -27,10 +39,15 @@
 SECGROUP=euca_secgroup
 
 # Add a secgroup
-euca-add-group -d description $SECGROUP
+if ! euca-describe-group | grep -q $SECGROUP; then
+    euca-add-group -d "$SECGROUP description" $SECGROUP
+    if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! euca-describe-group | grep -q $SECGROUP; do sleep 1; done"; then
+        echo "Security group not created"
+        exit 1
+    fi
+fi
 
 # Launch it
-DEFAULT_INSTANCE_TYPE=${DEFAULT_INSTANCE_TYPE:-m1.tiny}
 INSTANCE=`euca-run-instances -g $SECGROUP -t $DEFAULT_INSTANCE_TYPE $IMAGE | grep INSTANCE | cut -f2`
 
 # Assure it has booted within a reasonable time
@@ -42,15 +59,13 @@
 # Allocate floating address
 FLOATING_IP=`euca-allocate-address | cut -f2`
 
-# Release floating address
+# Associate floating address
 euca-associate-address -i $INSTANCE $FLOATING_IP
 
-
 # Authorize pinging
 euca-authorize -P icmp -s 0.0.0.0/0 -t -1:-1 $SECGROUP
 
 # Test we can ping our floating ip within ASSOCIATE_TIMEOUT seconds
-ASSOCIATE_TIMEOUT=${ASSOCIATE_TIMEOUT:-10}
 if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! ping -c1 -w1 $FLOATING_IP; do sleep 1; done"; then
     echo "Couldn't ping server with floating ip"
     exit 1
@@ -65,6 +80,12 @@
 # Release floating address
 euca-disassociate-address $FLOATING_IP
 
+# Wait just a tick for everything above to complete so release doesn't fail
+if ! timeout $ASSOCIATE_TIMEOUT sh -c "while euca-describe-addresses | grep $INSTANCE | grep -q $FLOATING_IP; do sleep 1; done"; then
+    echo "Floating ip $FLOATING_IP not disassociated within $ASSOCIATE_TIMEOUT seconds"
+    exit 1
+fi
+
 # Release floating address
 euca-release-address $FLOATING_IP
 
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)?
-
diff --git a/exercises/volumes.sh b/exercises/volumes.sh
index 6ea9a51..c2288de 100755
--- a/exercises/volumes.sh
+++ b/exercises/volumes.sh
@@ -19,6 +19,21 @@
 source ./openrc
 popd
 
+# 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}
+
 # Get a token for clients that don't support service catalog
 # ==========================================================
 
@@ -41,10 +56,10 @@
 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 | head -1 | 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`
 
 # determinine instance type
 # -------------------------
@@ -52,7 +67,6 @@
 # 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
@@ -73,24 +87,14 @@
 # Waiting for boot
 # ----------------
 
-# 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:-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 $BOOT_TIMEOUT sh -c "while ! nova show $VM_UUID | 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 $VM_UUID | grep "private network" | cut -d"|" -f3`
-#VM_UUID=`nova list | grep $NAME | head -1 | cut -d'|' -f2 | sed 's/ //g'`
 
 # for single node deployments, we can ping private ips
 MULTI_HOST=${MULTI_HOST:-0}
diff --git a/openrc b/openrc
index 4395975..0f327d2 100644
--- a/openrc
+++ b/openrc
@@ -53,13 +53,13 @@
 # export NOVACLIENT_DEBUG=1
 
 # Max time till the vm is bootable
-export BOOT_TIMEOUT=${BOOT_TIMEOUT:-15}
+export BOOT_TIMEOUT=${BOOT_TIMEOUT:-30}
 
 # Max time to wait while vm goes from build to active state
-export ACTIVE_TIMEOUT=${ACTIVE_TIMEOUT:-10}
+export ACTIVE_TIMEOUT=${ACTIVE_TIMEOUT:-30}
 
 # Max time from run instance command until it is running
 export RUNNING_TIMEOUT=${RUNNING_TIMEOUT:-$(($BOOT_TIMEOUT + $ACTIVE_TIMEOUT))}
 
 # Max time to wait for proper IP association and dis-association.
-export ASSOCIATE_TIMEOUT=${ASSOCIATE_TIMEOUT:-10}
+export ASSOCIATE_TIMEOUT=${ASSOCIATE_TIMEOUT:-15}