Add a helper method to volumes test to get fields

* The way we retrieve data from cli output is janky, this
   improves it slightly by creating a helper method.

Change-Id: Ib0889fd56f6a78bed85dad4c5e9e6e34bac9fb0d
This commit is contained in:
Vishvananda Ishaya 2012-02-27 22:41:54 +00:00
parent 995bfe0252
commit 854d8c93b8

View File

@ -55,18 +55,33 @@ IMAGE=`glance -f index | egrep $DEFAULT_IMAGE_NAME | head -1 | cut -d" " -f1`
# determinine instance type # determinine instance type
# ------------------------- # -------------------------
# Helper function to grab a numbered field from python novaclient cli result
# Fields are numbered starting with 1
# Reverse syntax is supported: -1 is the last field, -2 is second to last, etc.
function get_field () {
while read data
do
if [ "$1" -lt 0 ]; then
field="(\$(NF$1))"
else
field="\$$(($1 + 1))"
fi
echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}"
done
}
# List of instance types: # List of instance types:
nova flavor-list nova flavor-list
INSTANCE_TYPE=`nova flavor-list | grep $DEFAULT_INSTANCE_TYPE | cut -d"|" -f2` INSTANCE_TYPE=`nova flavor-list | grep $DEFAULT_INSTANCE_TYPE | get_field 1`
if [[ -z "$INSTANCE_TYPE" ]]; then if [[ -z "$INSTANCE_TYPE" ]]; then
# grab the first flavor in the list to launch if default doesn't exist # grab the first flavor in the list to launch if default doesn't exist
INSTANCE_TYPE=`nova flavor-list | head -n 4 | tail -n 1 | cut -d"|" -f2` INSTANCE_TYPE=`nova flavor-list | head -n 4 | tail -n 1 | get_field 1`
fi fi
NAME="myserver" NAME="myserver"
VM_UUID=`nova boot --flavor $INSTANCE_TYPE --image $IMAGE $NAME --security_groups=$SECGROUP | grep ' id ' | cut -d"|" -f3 | sed 's/ //g'` VM_UUID=`nova boot --flavor $INSTANCE_TYPE --image $IMAGE $NAME --security_groups=$SECGROUP | grep ' id ' | get_field 2`
# Testing # Testing
# ======= # =======
@ -85,7 +100,7 @@ if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova show $VM_UUID | grep status | g
fi fi
# get the IP of the server # get the IP of the server
IP=`nova show $VM_UUID | grep "private network" | cut -d"|" -f3` IP=`nova show $VM_UUID | grep "private network" | get_field 2`
# for single node deployments, we can ping private ips # for single node deployments, we can ping private ips
MULTI_HOST=${MULTI_HOST:-0} MULTI_HOST=${MULTI_HOST:-0}
@ -108,7 +123,7 @@ fi
VOL_NAME="myvol-$(openssl rand -hex 4)" VOL_NAME="myvol-$(openssl rand -hex 4)"
# Verify it doesn't exist # Verify it doesn't exist
if [[ -n "`nova volume-list | grep $VOL_NAME | head -1 | cut -d'|' -f3 | sed 's/ //g'`" ]]; then if [[ -n "`nova volume-list | grep $VOL_NAME | head -1 | get_field 2`" ]]; then
echo "Volume $VOL_NAME already exists" echo "Volume $VOL_NAME already exists"
exit 1 exit 1
fi fi
@ -121,7 +136,7 @@ if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova volume-list | grep $VOL_NAME |
fi fi
# Get volume ID # Get volume ID
VOL_ID=`nova volume-list | grep $VOL_NAME | head -1 | cut -d'|' -f2 | sed 's/ //g'` VOL_ID=`nova volume-list | grep $VOL_NAME | head -1 | get_field 1`
# Attach to server # Attach to server
DEVICE=/dev/vdb DEVICE=/dev/vdb
@ -131,7 +146,7 @@ if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova volume-list | grep $VOL_NAME |
exit 1 exit 1
fi fi
VOL_ATTACH=`nova volume-list | grep $VOL_NAME | head -1 | cut -d'|' -f6 | sed 's/ //g'` VOL_ATTACH=`nova volume-list | grep $VOL_NAME | head -1 | get_field -1`
if [[ "$VOL_ATTACH" != $VM_UUID ]]; then if [[ "$VOL_ATTACH" != $VM_UUID ]]; then
echo "Volume not attached to correct instance" echo "Volume not attached to correct instance"
exit 1 exit 1