Created
April 27, 2017 21:34
-
-
Save mrunalp/563712e9d75b685f03148320bcbb5016 to your computer and use it in GitHub Desktop.
Spawn a VM and run tests.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash -ue | |
usage() | |
{ | |
cat << _EOF_ | |
Usage: $0 options | |
OPTIONS: | |
-h Display help. | |
-i Path to the image. | |
-p Prepare image. | |
-l Launch image in a vm. | |
-n Domain name for the image when launched. | |
-t Run the tests on the vm. | |
-d Debug. | |
_EOF_ | |
} | |
domain_name= | |
image_path= | |
PREPARE= | |
LAUNCH= | |
TEST= | |
DEBUG= | |
while getopts “hi:n:pltd” OPTION | |
do | |
case $OPTION in | |
h) | |
usage | |
exit 0 | |
;; | |
i) | |
image_path=$OPTARG | |
;; | |
n) | |
domain_name=$OPTARG | |
;; | |
p) | |
PREPARE=1 | |
;; | |
l) | |
LAUNCH=1 | |
;; | |
t) | |
TEST=1 | |
;; | |
d) | |
DEBUG=1 | |
;; | |
?) | |
usage | |
exit 1 | |
;; | |
esac | |
done | |
# Helper function to get the IP Address of the running vm. | |
function get_ip_address_for_domain() { | |
dom=$1 | |
mac=$(virsh domiflist $dom | tail -n2 | head -n1 | awk '{print $5}') | |
grep $mac /var/lib/libvirt/dnsmasq/default.leases | awk '{print $3}' | |
} | |
# Setup image to allow password-less ssh and copy in the test script. | |
function prepare_image() { | |
image_path=$1 | |
echo "Enabling password-less ssh and copying in test script to the image..." | |
guestfish -a $image_path <<_EOF_ | |
run | |
mount /dev/sda3 / | |
write-append /ostree/deploy/rh-atomic-controller/current/etc/ssh/sshd_config "PermitEmptyPasswords yes" | |
copy-in geard_test.sh /ostree/deploy/rh-atomic-controller/current/bin/ | |
_EOF_ | |
} | |
# Launch the image | |
function launch_image() { | |
image_path=$1 | |
domain_name=$2 | |
echo "Importing and starting image..." | |
virt-install --connect qemu:///system --ram 2048 -n $domain_name -r 2048 --os-type=linux --os-variant=rhel7 --disk path=$image_path,device=disk,bus=virtio,format=qcow2 --vcpus=2 --graphics spice --noautoconsole --import | |
} | |
# Run tests | |
function run_tests() { | |
domain_name=$1 | |
set +e | |
ipaddr=$(get_ip_address_for_domain $domain_name) | |
set -e | |
i=0 | |
while ! nc ${ipaddr} 22 < /dev/null; do | |
set +e | |
ipaddr=$(get_ip_address_for_domain $domain_name) | |
set -e | |
echo "IP Address of $domain_name: ${ipaddr}" | |
i=$((i+1)) | |
if [ $i -eq 10 ]; then | |
echo "Timed out waiting for vm to be accessible over ssh." | |
exit 1 | |
fi | |
echo "Waiting to be able to ssh into the image..." | |
sleep 1 | |
done | |
echo "Starting tests on the vm..." | |
ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no root@"${ipaddr}" /bin/geard_test.sh | |
} | |
if [[ ! -z $PREPARE ]]; then | |
if [[ -z $image_path ]]; then | |
echo "Image path required for prepare command." | |
echo | |
usage | |
exit 1 | |
else | |
echo "Preparing image $image_path" | |
prepare_image $image_path | |
fi | |
fi | |
if [[ ! -z $LAUNCH ]]; then | |
if [[ -z $image_path ]] || [[ -z $domain_name ]]; then | |
echo "Image path and domain name are required for launch command." | |
echo | |
usage | |
exit 1 | |
else | |
echo "Launching image $image_path" | |
launch_image $image_path $domain_name | |
fi | |
fi | |
if [[ ! -z $TEST ]]; then | |
if [[ -z $domain_name ]]; then | |
echo "Domain name required for running tests." | |
echo | |
usage | |
exit 1 | |
else | |
echo "Running tests on the vm $domain_name" | |
run_tests $domain_name | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment