Skip to content

Instantly share code, notes, and snippets.

@mevansam
Last active August 29, 2015 14:20
Show Gist options
  • Save mevansam/8f0bcb0f7b991d30de8e to your computer and use it in GitHub Desktop.
Save mevansam/8f0bcb0f7b991d30de8e to your computer and use it in GitHub Desktop.
Run basic VM and volume tests on an OpenStack cluster
#!/bin/bash
if [ -z $1 ] || [ -z $2 ]; then
echo -e "\nusage: ./ostest.sh <start> <end> [ <image> <user> <key_pair> <private_network> <public_network> ]\n"
echo -e " Runs tests numbered from <start> to <end>. Arguments <image>, <user>, <key_pair>,"
echo -e " <private_network> and <public_network> are optional but must provided as one. If"
echo -e " they are not provided then 'cirros-0.3.3', 'cirros', '$(whoami)' 'private01' and "
echo -e " 'public01' will be used.\n"
exit 1
fi
tests=""
for i in $(seq $1 $2); do
tests="${tests}test$i "
done
image=${3:-cirros-0.3.3}
user=${4:-cirros}
keypair=${5:-$(whoami)}
privatenet=${6:-private01}
publicnet=${7:-public01}
vms=$tests
vols=$tests
#set -x
source openrc
cat /dev/null > os-test.log
#
# Create key-pair for current user
#
kp=$(nova --insecure keypair-list 2> /dev/null | awk -v kp=$keypair '$2==kp { print $2 }')
if [ "$kp" == "" ]; then
echo "Creating keypair $keypair..."
[ -e "$HOME/.ssh/id_rsa" ] || (ssh-keygen -N "" -f $HOME/.ssh/id_rsa; chmod 0400 $HOME/.ssh/id_rsa)
[ -e "$HOME/.ssh/id_rsa.pub" ] || ssh-keygen -y -f $HOME/.ssh/id_rsa > $HOME/.ssh/id_rsa.pub
nova --insecure keypair-add --pub-key ~/.ssh/id_rsa.pub $keypair 2> /dev/null
fi
#
# Create VMs
#
for vm in $(echo "$vms"); do
nova --insecure show $vm 2> /dev/null
if [ $? -ne 0 ]; then
echo "Creating VM $vm..."
nova --insecure boot --flavor m1.tiny --image "$image" --key-name "$keypair" --security-groups "all-ports" \
--nic net-id=$(neutron --insecure net-list 2> /dev/null | awk -v net="$privatenet" '$4==net { print $2 }') $vm 2>> os-test.log
if [ $? -ne 0 ]; then
echo "Error creating VM $vm."
exit 1
fi
fi
done
#
# Create Volumes
#
for vol in $(echo "$vols"); do
cinder --insecure show $vol 2> /dev/null
if [ $? -ne 0 ]; then
echo "Creating volume $vol..."
cinder --insecure create --display-name $vol 2 2>> os-test.log
if [ $? -ne 0 ]; then
echo "Error creating VM $vm."
exit 1
fi
fi
done
#
# Wait for VMs to become active and associate each with a floating IP
#
for vm in $(echo "$vms"); do
vmstate=""
while [ "$vmstate" != "ACTIVE" ]; do
echo "Waiting for VM $vm to become active..."
vmstate=$(nova --insecure show $vm 2> /dev/null | awk '/status/ { print $4 }')
sleep 1
done
echo "VM $vm is active"
ip=$(nova --insecure show $vm 2> /dev/null | awk '$3=="network" { print $6 }')
if [ "$ip" == "|" ]; then
ip=$(nova --insecure floating-ip-list 2> /dev/null | awk -v net="$publicnet" '$8==net && $4=="-" { print $2 }' | head -n 1)
[ "$ip" != "" ] || ip=$(nova --insecure floating-ip-create $publicnet 2> /dev/null | awk -v net="$publicnet" '$8==net { print $2 }')
nova --insecure floating-ip-associate $vm $ip 2> /dev/null
if [ $? -ne 0 ]; then
echo "Error associating floating ip '$ip' with VM $vm."
exit 1
fi
fi
echo "VM $vm is available at $ip..."
done
#
# Mount volumes and format them
#
for vm in $(echo "$vms"); do
for v in $(nova --insecure show test1 2> /dev/null | awk '/os-extended-volumes:volumes_attached/ { print substr($5,2,36) }'); do
nova --insecure volume-detach $vm $v 2>> os-test.log
done
vol=$vm
ip=$(nova --insecure show $vm 2> /dev/null | awk '$3=="network" { print $6 }')
volid=$(cinder --insecure list 2> /dev/null | awk -v vol=$vol '$6==vol { print $2 }')
echo "Attaching volume $vol to VM $vm..."
nova --insecure volume-attach $vm $volid /dev/vdc 2>> os-test.log
if [ $? -ne 0 ]; then
echo "Error attaching volume $vol attached to VM $vm."
exit 1
fi
echo "SSH into $user@$ip and formating volume $vol..."
ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no $user@$ip sudo su -l << EOF 2>&1 > /dev/null
while [ ! -e /dev/vdb ]; do
echo "Waiting for volume to attach to $vm at device vdb..."
sleep 1
done
mkfs.ext4 /dev/vdb
mount /dev/vdb /mnt
echo $vol > /mnt/test
umount /mnt
EOF
if [ $? -ne 0 ]; then
echo "Error formatting volume $vol attached to VM $vm."
exit 1
fi
echo "Detaching volume $vol from VM $vm..."
nova --insecure volume-detach $vm $volid 2>> os-test.log
if [ $? -ne 0 ]; then
echo "Error detaching volume $vol attached to VM $vm."
exit 1
fi
done
#
# Cross mount volumes across all VMs and validate test file
#
for vm in $(echo "$vms"); do
for v in $(nova --insecure show test1 2> /dev/null | awk '/os-extended-volumes:volumes_attached/ { print substr($5,2,36) }'); do
nova --insecure volume-detach $vm $v 2>> os-test.log
done
ip=$(nova --insecure show $vm 2> /dev/null | awk '$3=="network" { print $6 }')
for vol in $(echo "$vols"); do
volid=$(cinder --insecure list 2> /dev/null | awk -v vol=$vol '$6==vol { print $2 }')
echo "Attaching volume $vol to VM $vm..."
nova --insecure volume-attach $vm $volid /dev/vdc 2>> os-test.log
if [ $? -ne 0 ]; then
echo "Error attaching volume $vol attached to VM $vm."
exit 1
fi
echo "Validating volume $vol..."
ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no $user@$ip sudo su -l << EOF
while [ ! -e /dev/vdb ]; do
echo "Waiting for volume to attach to $vm at device vdb..."
sleep 1
done
mount /dev/vdb /mnt
test=\$(cat /mnt/test)
umount /mnt
echo "Volume $vol attached to VM $vm test file contents: \$test" | grep "$vol"
EOF
if [ $? -ne 0 ]; then
echo "Error validating volume $vol attached to VM $vm."
exit 1
fi
echo "Detaching volume $vol from VM $vm..."
nova --insecure volume-detach $vm $volid 2>> os-test.log
if [ $? -ne 0 ]; then
echo "Error detaching volume $vol attached to VM $vm."
exit 1
fi
done
done
#
# Delete test resources
#
for vm in $(echo "$vms"); do
echo "Deleting VM $vm..."
nova --insecure delete $vm 2>> os-test.log
if [ $? -ne 0 ]; then
echo "Error deleting VM $vm..."
exit 1
fi
done
for vol in $(echo "$vols"); do
cinder --insecure delete $vol 2>> os-test.log
if [ $? -ne 0 ]; then
echo "Error deleting volume $vol..."
exit 1
fi
done
#set +x
@mevansam
Copy link
Author

mevansam commented May 2, 2015

Run the tests as follows:

$ curl -ks -L https://gist.githubusercontent.com/mevansam/8f0bcb0f7b991d30de8e/raw/49e5d6752e0c25430b0e0967e08edea1971bc6d3/gistfile1.sh -o os-test.sh && chmod 0744 os-test.sh
$ ./os-test.sh 1 4

This will create 4 VMs and 4 volumes named test1, test2, test3 and test4 and perform various operations on them. If you want to run multiple parallel tests then run this script simultaneously from multiple shells, using distinct sequences. However, running parallel tests could create race conditions. For more detailed and thorough testing consider using OpenStack Rally.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment