2015-11-24 18:18:01 +05:30
#!/bin/bash
###############################################################################
2016-04-20 15:02:16 +05:30
# TODO: Support other OSes. #
2015-11-24 18:18:01 +05:30
###############################################################################
2016-01-18 14:44:46 +05:30
ORIGIN_DIR = $PWD
autostart = "no"
2016-04-20 15:02:16 +05:30
destroy_after_test = "no"
os = "fedora"
destroy_now = "no"
run_tests_args = ""
redirect = ">/dev/null 2>&1"
ssh = "no"
custom_cflags = ""
pushd ( ) {
command pushd " $@ " >/dev/null
}
popd ( ) {
command popd " $@ " >/dev/null
}
2016-01-18 14:44:46 +05:30
function parse_args ( ) {
args = ` getopt \
--options a \
2016-04-20 15:02:16 +05:30
--long autostart,os:,destroy-now,destroy-after-test,verbose,ssh \
2016-01-18 14:44:46 +05:30
-n 'run-tests-in-vagrant.sh' \
-- " $@ " `
eval set -- " $args "
while true; do
case " $1 " in
-a| --autostart) autostart = "yes" ; shift ; ;
2016-04-20 15:02:16 +05:30
--destroy-after-test) destroy_after_test = "yes" ; shift ; ;
--destroy-now) destroy_now = "yes" ; shift ; ;
--ssh) sshvm = "yes" ; shift ; ;
--os)
case " $2 " in
"" ) shift 2 ; ;
*) os = " $2 " ; shift 2 ; ;
esac ; ;
--verbose) redirect = "" ; shift ; ;
2016-01-18 14:44:46 +05:30
--) shift ; break ; ;
*) echo "Internal error!" ; exit 1; ;
esac
done
2016-04-20 15:02:16 +05:30
run_tests_args = " $@ "
2016-01-18 14:44:46 +05:30
}
2015-11-24 18:18:01 +05:30
function force_location( )
{
current_dir = $( dirname $0 ) ;
2016-04-20 15:02:16 +05:30
if [ ! -f ${ current_dir } /tests/vagrant/vagrant-template-fedora/Vagrantfile ] ; then
2015-11-24 18:18:01 +05:30
echo "Aborting."
echo "The tests/vagrant subdirectory seems to be missing."
echo "Please correct the problem and try again."
exit 1
fi
}
function vagrant_check( )
{
2016-04-20 15:02:16 +05:30
vagrant -v >/dev/null 2>& 1;
2015-11-24 18:18:01 +05:30
if [ $? -ne 0 ] ; then
echo "Aborting"
echo "Vagrant not found. Please install Vagrant and try again."
2015-12-30 18:15:04 +05:30
echo "On Fedora, run " dnf install vagrant vagrant-libvirt" "
2015-11-24 18:18:01 +05:30
exit 1
fi
}
function ansible_check( )
{
2015-12-30 18:15:04 +05:30
ansible --version >/dev/null 2>& 1 ;
2015-11-24 18:18:01 +05:30
if [ $? -ne 0 ] ; then
echo "Aborting"
echo "Ansible not found. Please install Ansible and try again."
2015-12-30 18:15:04 +05:30
echo "On Fedora, run " dnf install ansible" "
2015-11-24 18:18:01 +05:30
exit 1
2016-04-20 15:02:16 +05:30
fi
}
function set_branchname_from_git_branch( )
{
BRANCHNAME = ` git rev-parse --abbrev-ref HEAD`
if [ $? -ne 0 ] ; then
echo "Could not get branch name from git, will exit"
exit 1
fi
}
function destroy_vm_and_exit( )
{
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!CAUTION!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
echo " This will destroy VM and delete tests/vagrant/ ${ BRANCHNAME } dir "
echo
while true; do
read -p "Do you want to continue?" yn
case $yn in
[ Yy] * ) break; ;
* ) echo "Did not get an yes, exiting." ; exit 1 ; ;
esac
done
if [ -d " tests/vagrant/ ${ BRANCHNAME } " ] ; then
pushd " tests/vagrant/ ${ BRANCHNAME } "
eval vagrant destroy $redirect
popd
rm -rf " tests/vagrant/ ${ BRANCHNAME } "
exit 0
2015-11-24 18:18:01 +05:30
else
2016-04-20 15:02:16 +05:30
echo "Could not find vagrant dir for corresponding git branch, exiting"
exit 1
2015-11-24 18:18:01 +05:30
fi
}
2015-12-30 18:15:04 +05:30
2016-04-20 15:02:16 +05:30
function create_vagrant_dir( )
{
mkdir -p tests/vagrant/$BRANCHNAME
if [ -d " tests/vagrant/vagrant-template- ${ os } " ] ; then
echo " Copying tests/vagrant/vagrant-template- ${ os } dir to tests/vagrant/ ${ BRANCHNAME } .... "
cp -R tests/vagrant/vagrant-template-${ os } /* tests/vagrant/$BRANCHNAME
else
echo " Could not find template files for requested os $os , exiting "
exit 1
fi
}
2015-11-24 18:18:01 +05:30
2016-04-20 15:02:16 +05:30
function start_vm( )
{
echo "Doing vagrant up...."
pushd " tests/vagrant/ ${ BRANCHNAME } "
eval vagrant up $redirect
if [ $? -eq 0 ]
then
popd
else
echo "Vagrant up failed, exiting...." ;
popd
exit 1
fi
}
2015-11-24 18:18:01 +05:30
2016-04-20 15:02:16 +05:30
function set_vm_attributes( )
{
if [ " x $autostart " = = "xyes" ] ; then
virsh autostart ${ BRANCHNAME } _vagrant-testVM
fi
}
2015-11-24 18:18:01 +05:30
2016-04-20 15:02:16 +05:30
function copy_source_code( )
{
echo "Copying source code from host machine to VM...."
pushd " tests/vagrant/ ${ BRANCHNAME } "
vagrant ssh-config > ssh_config
rsync -az -e "ssh -F ssh_config" --rsync-path= "sudo rsync" " $ORIGIN_DIR /. " vagrant-testVM:/home/vagrant/glusterfs
if [ $? -eq 0 ]
then
popd
else
echo "Copy failed, exiting...."
popd
exit 1
fi
}
function compile_gluster( )
{
echo "Source compile and install Gluster...."
pushd " tests/vagrant/ ${ BRANCHNAME } "
vagrant ssh -c " cd /home/vagrant/glusterfs ; sudo make clean $redirect " -- -t
vagrant ssh -c " cd /home/vagrant/glusterfs ; sudo ./autogen.sh $redirect " -- -t
if [ $? -ne 0 ]
then
echo "autogen failed, exiting...."
popd
exit 1
fi
2015-12-30 18:15:04 +05:30
2016-04-20 15:02:16 +05:30
# GCC on fedora complains about uninitialized variables and
# GCC on centos6 does not under don't warn on uninitialized variables flag.
if [ " x $os " = = "fedora" ] ; then
custom_cflags = "CFLAGS='-g -O0 -Werror -Wall -Wno-error=cpp -Wno-error=maybe-uninitialized'"
else
custom_cflags = "CFLAGS='-g -O0 -Werror -Wall'"
fi
2015-12-30 18:15:04 +05:30
2016-04-20 15:02:16 +05:30
custom_cflags =
vagrant ssh -c " cd /home/vagrant/glusterfs ; \
sudo \
$custom_cflags \
./configure \
--prefix= /usr \
--exec-prefix= /usr \
--bindir= /usr/bin \
--sbindir= /usr/sbin \
--sysconfdir= /etc \
--datadir= /usr/share \
--includedir= /usr/include \
--libdir= /usr/lib64 \
--libexecdir= /usr/libexec \
--localstatedir= /var \
--sharedstatedir= /var/lib \
--mandir= /usr/share/man \
--infodir= /usr/share/info \
--libdir= /usr/lib64 \
--enable-debug $redirect " -- -t
if [ $? -ne 0 ]
then
echo "configure failed, exiting...."
popd
exit 1
fi
vagrant ssh -c " cd /home/vagrant/glusterfs; sudo make -j install $redirect " -- -t
if [ $? -ne 0 ]
then
echo "make failed, exiting...."
popd
exit 1
fi
popd
}
2015-11-24 18:18:01 +05:30
2016-04-20 15:02:16 +05:30
function run_tests( )
{
pushd " tests/vagrant/ ${ BRANCHNAME } "
vagrant ssh -c " cd /home/vagrant/glusterfs; sudo ./run-tests.sh $run_tests_args " -- -t
popd
}
2015-11-24 18:18:01 +05:30
2016-04-20 15:02:16 +05:30
function ssh_into_vm_using_exec( )
{
pushd " tests/vagrant/ ${ BRANCHNAME } "
exec vagrant ssh
popd
}
2015-12-30 18:15:04 +05:30
2015-11-24 18:18:01 +05:30
echo
2016-04-20 15:02:16 +05:30
parse_args " $@ "
2015-11-24 18:18:01 +05:30
2016-04-20 15:02:16 +05:30
# Check environment for dependencies
force_location
vagrant_check
ansible_check
2015-11-24 18:18:01 +05:30
2016-04-20 15:02:16 +05:30
# We have one vm per git branch, query git branch
set_branchname_from_git_branch
2015-11-24 18:18:01 +05:30
2016-04-20 15:02:16 +05:30
if [ " x $destroy_now " = = "xyes" ] ; then
destroy_vm_and_exit
2015-12-30 18:15:04 +05:30
fi
2015-11-24 18:18:01 +05:30
2016-04-20 15:02:16 +05:30
if [ " x $sshvm " = = "xyes" ] ; then
ssh_into_vm_using_exec
2015-12-30 18:15:04 +05:30
fi
2015-11-24 18:18:01 +05:30
2016-04-20 15:02:16 +05:30
create_vagrant_dir
start_vm
set_vm_attributes
copy_source_code
compile_gluster
run_tests
if [ " x $destroy_after_test " = = "xyes" ] ; then
destroy_vm_and_exit
fi