Skip to content

Instantly share code, notes, and snippets.

@cloudnull
Last active August 29, 2015 13:57
Show Gist options
  • Save cloudnull/9607061 to your computer and use it in GitHub Desktop.
Save cloudnull/9607061 to your computer and use it in GitHub Desktop.
Test Cinder, This will create an instance, create a cinder volume, and then attach the cinder volume to the instance. If successful the script will detach the cinder volume, delete the volume, delete the volume type and then delete the instance.
#!/usr/bin/env bash
set -e
set -u
set -v
source $HOME/openrc
# Max time to wait for volume operations (specifically create and delete)
ACTIVE_TIMEOUT="300"
SUPERTEST_HASH=$(tr -dc 'a-zA-Z0-9' < /dev/urandom | head -c 9)
VOLUME_TIMEOUT=${VOLUME_TIMEOUT:-300}
# Default volume name
DEFAULT_VOLUME_NAME=${DEFAULT_VOLUME_NAME:-test-volume-${SUPERTEST_HASH}}
# Default volume name
DEFAULT_TYPE_NAME=${DEFAULT_TYPE_NAME:-test-type-${SUPERTEST_HASH}}
# Boot this image, use first AMi image if unset
DEFAULT_IMAGE_NAME=${DEFAULT_IMAGE_ID:-"cirros-image"}
IMAGE=$(nova image-list | grep -w ${DEFAULT_IMAGE_NAME} | awk '{print $2}')
# Instance Name
DEFAULT_INSTANCE_NAME=${DEFAULT_INSTANCE_NAME:-test-type-${SUPERTEST_HASH}}
# Instance type to create
DEFAULT_INSTANCE_TYPE=${DEFAULT_INSTANCE_TYPE:-m1.tiny}
# Find the instance type ID
INSTANCE_TYPE=$(nova flavor-list | grep -w $DEFAULT_INSTANCE_TYPE | head -1 | awk '{print $2}')
NET_ID=$(neutron net-list | grep -A2 id | tail -n 1 | awk '{print $2}')
# Create a new Volume Type
if ! TYPE_ID=$(cinder type-create ${DEFAULT_TYPE_NAME}| grep -w ${DEFAULT_TYPE_NAME} | awk '{print $2}'); then
echo "could not create volume type ${DEFAULT_TYPE_NAME}"
exit 1
fi
# Create a new Volume
if VOLUME_ID=$(cinder create --display-name ${DEFAULT_VOLUME_NAME} --volume-type ${DEFAULT_TYPE_NAME} 1 | grep -w id | awk '{print $4}') ; then
if ! timeout ${VOLUME_TIMEOUT} sh -c "while ! cinder list | grep ${DEFAULT_VOLUME_NAME} | grep available ; do sleep 1 ; done"; then
echo "volume ${DEFAULT_VOLUME_NAME} did not become available in ${VOLUME_TIMEOUT} seconds"
exit 1
fi
else
echo "Unable to create volume ${DEFAULT_VOLUME_NAME}"
exit 1
fi
echo "nova boot --flavor ${INSTANCE_TYPE} --image ${IMAGE} --nic net-id=${NET_ID} ${DEFAULT_INSTANCE_NAME}"
INSTANCE_ID="$(nova boot --flavor ${INSTANCE_TYPE} --image ${IMAGE} --nic net-id=${NET_ID} ${DEFAULT_INSTANCE_NAME} | grep -w id | awk '{print $4}')"
echo "New Instance $INSTANCE_ID"
if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova list | grep -w ${INSTANCE_ID} | grep ACTIVE; do sleep 1; done"; then
echo "Instance ${DEFAULT_INSTANCE_NAME} failed to go active after ${ACTIVE_TIMEOUT} seconds"
exit 1
fi
# Attach Volume
echo "nova volume-detatch ${INSTANCE_ID} ${VOLUME_ID} \"/dev/sdx\""
nova volume-attach ${INSTANCE_ID} ${VOLUME_ID} "/dev/sdx"
if ! timeout $ACTIVE_TIMEOUT sh -c "while ! cinder list | grep -w ${VOLUME_ID} | grep in-use; do sleep 1; done"; then
echo "Instance ${DEFAULT_INSTANCE_NAME} failed to go active after ${ACTIVE_TIMEOUT} seconds"
exit 1
fi
# Detatch Volume
echo "nova volume-detatch ${INSTANCE_ID} ${VOLUME_ID}"
nova volume-detach ${INSTANCE_ID} ${VOLUME_ID}
if ! timeout $ACTIVE_TIMEOUT sh -c "while ! cinder list | grep -w ${VOLUME_ID} | grep available; do sleep 1; done"; then
echo "Instance ${DEFAULT_INSTANCE_NAME} failed to go active after ${ACTIVE_TIMEOUT} seconds"
exit 1
fi
# Delete a Cinder Volume
if cinder delete ${VOLUME_ID}; then
if ! timeout ${VOLUME_TIMEOUT} sh -c "while cinder show ${VOLUME_ID} ; do sleep 1 ; done"; then
echo "volume did not get deleted properly within ${VOLUME_TIMEOUT} seconds"
exit 1
fi
else
echo "could not delete volume ${VOLUME_ID}"
exit 1
fi
# Delete Volume Type
cinder type-delete $(cinder type-list|grep ${DEFAULT_TYPE_NAME} | cut -d'|' -f2)
# Delete Instance
nova delete ${INSTANCE_ID}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment