#!/bin/bash
#test script config variables
go_debug="go_debug"
_date=$(date +%F-%H-%M-%S)
config_file="/etc/container/ve.cfg"
proc_file="/proc/lve/list"
confdir=./

function Usage {
    echo "Usage: lvectl_test [-h] [-c] [-p] [-t] [-r]"
    echo -e "\t-h\tshow this message"
    echo -e "\t-c\tmake cleanup of $proc_file and $config_file"
    echo -e "\t-p\tprepare for the tests"
    echo -e "\t-t\trun tests"
    echo -e "\t-r\trestore original config"
}
# if no parameters were given - show usage
if [ -z "$1" ]; then 
    Usage
    exit 1;
fi;
#if running without root exit
if [ ! $UID -eq 0 ]; then
    echo "Run script as root."
    exit 1;
fi;
#if lve module is not loaded, exit
if [ -z "$(lsmod | grep lve)" ]; then 
    echo "LVE module is nod loaded, exiting"
    exit 1;
fi;

function Debug {
    if [ -n "$go_debug" ]; then
        if [ -n "$2" ]; then
            echo -e -n "$1"
        else
            echo -e "$1"
        fi
    fi
}

function Cleanup {
## Cleanup all existing contexts
Debug "Cleaning up"
Debug "\tFlushing all VEs" "-n"
lvectl --flush all 2>&1 >/dev/null
if [ $? -eq 0 ]; then
    Debug "\t\t\tOK"
fi;
}

function Restore {
Debug "Restoring default parameters"
/sbin/service lvectl stop
if [ $? -eq 0 ]; then
    if [ -e $config_file.$_date ]; then 
        mv $config_file.$_date $config_file
        rm -f $config_file.$_date
    else 
	/bin/cp -f $confdir/clean_ve_config $config_file
    fi;
    /sbin/service lvectl start
else 
    Debug "Couldn't stop lvectl, exiting abnormally\tERROR"
fi;
return 0;
}

function Prep {
if [ -z "$(rpm -q lve-utils)" ]; then
    echo ERROR: lve-utils is not installed.
    exit 1
fi;
}


function Test {
Debug "\tBackuping and removing all configs" "-n"
mv $config_file $config_file.$_date
cp $confdir/clean_ve_config $config_file
Debug "\tDONE"
Debug "Running test"
#-============================================================-
# test if lvectl with wrong parameters returns positive response
Debug "\tChecking returned value" "-n"
lvectl --blabla > /dev/null 2>&1
if [ $? -eq 0 ]; then
    Debug "\t\t\tFAIL"
else
    Debug "\t\t\tOK"
fi;
#-===========================================================-
# checking containers creation with default parameters in memory and config
Debug "\tChecking --new option with default limits"
ve_count=$(cat $proc_file | wc -l)
ve_config_count=$(cat $config_file | wc -l)

lvectl --new --ve 20000
new_ve_count=$(cat $proc_file | wc -l)
new_ve_config_count=$(cat $config_file | wc -l)
Debug "\t\tChecking VE in memory" "-n"
if [ "$ve_count" -ne "$new_ve_count" ]; then
    Debug "\t\tOK"
else
    Debug "\t\tFAIL"
    NEW_ERROR_MEMORY="Error"
fi;
Debug "\t\tChecking in config" "-n"
if [ "$ve_config_count" -ne "$new_ve_config_count" ] ; then
    Debug "\t\tOK"
else
    Debug "\t\tFAIL"
    NEW_ERROR_CONFIG="Error"
fi;
#-===========================================================-
Debug "\tChechking --set option"
cp $proc_file /tmp/tmp_proc_file
cp $config_file /tmp/tmp_config_file

Debug "\t\tReturned value" "-n"; 
lvectl --set --ve 20000 --cpu 75 --io 75 
if [ $? -ne 0 ]; then Debug "\t\t\tERROR"; else Debug "\t\t\tOK"; fi;

Debug "\t\tChanges in memory" "-n"; 
diff -u $proc_file /tmp/tmp_proc_file > /dev/null
if [ $? -ne 0 ]; then Debug "\t\tOK"; else Debug "\t\tFAIL"; SET_ERROR_MEMORY="Error";fi;

Debug "\t\tChanges in config" "-n"; 
diff -u $config_file /tmp/tmp_config_file > /dev/null
if [ $? -ne 0 ]; then Debug "\t\tOK"; else Debug "\t\tFAIL"; SET_ERROR_CONFIG="Error";fi;

rm -f /tmp/tmp_config_file /tmp/tmp_proc_file
#-===========================================================-
Debug "\tChecking --apply option"
cp $proc_file /tmp/tmp_proc_file
ve_count=$(cat $proc_file | wc -l )
#if in --new was an error and no VE containers availiable, testing only creation of VEs via --apply option
if [ $ve_count -eq 1 ]; then APPLY_CREATE_CHECK="check"; fi; 

/bin/cp -f $confdir/apply_ve_config $config_file

Debug "\t\tReturned value" "-n"; 
lvectl --apply
if [ $? -ne 0 ]; then Debug "\t\t\tERROR"; else Debug "\t\t\tOK"; fi;

Debug "\t\tChanges in memory" "-n"; 
diff -u $proc_file /tmp/tmp_proc_file > /dev/null
if [ $? -ne 0 ]; then Debug "\t\tOK"; else Debug "\t\tFAIL"; APPLY_ERROR_MEMORY="Error"; fi;

/bin/cp -f $confdir/apply_create_ve_config $config_file

Debug "\t\tChecking creation of VEs " "-n"
lvectl --apply
if [ $? -eq 0 ]; then Debug "\tOK"; else Debug "\t\tFAIL"; APPLY_CREATE_ERROR_MEMORY="Error"; fi;

Debug "\t\tChanges in memory " "\n"
new_ve_count=$(cat $proc_file | wc -l )
if [ $new_ve_count -gt $ve_count ]; then 
    Debug "\t\tOK"
else
    Debug "\t\tFAIL";
    APPLY_CREATE_ERROR_MEMORY="Error";
fi;

rm -f /tmp/tmp_proc_file
#-===========================================================-
Debug "\tChecking --destroy option"
cp $proc_file /tmp/tmp_proc_file
cp $config_file /tmp/tmp_config_file

ve_count=$(cat $proc_file | wc -l )
if [ $ve_count -eq 1 ]; then Debug "\t\tThere isn't any active VE, skipping test"; return 1; fi; 

Debug "\t\tReturned value" "-n"; 
lvectl --destroy --ve 20000
if [ $? -ne 0 ]; then Debug "\t\t\tERROR"; else Debug "\t\t\tOK"; fi;

Debug "\t\tChanges in memory" "-n"; 
diff -u $proc_file /tmp/tmp_proc_file > /dev/null
if [ $? -ne 0 ]; then Debug "\t\tOK"; else Debug "\t\tFAIL"; fi;

Debug "\t\tChanges in config" "-n"; 
diff -u $config_file /tmp/tmp_config_file > /dev/null
if [ $? -ne 0 ]; then Debug "\t\tOK"; else Debug "\t\tFAIL"; fi;

rm -f /tmp/tmp_config_file /tmp/tmp_proc_file
#-===========================================================-
Debug "\tChecking --flush option"
#testing lvectl --flush default
lvectl --new --ve 20002
cp $proc_file /tmp/tmp_proc_file
cp $config_file /tmp/tmp_config_file

ve_count=$(cat $proc_file | wc -l )
if [ $ve_count -eq 1 ]; then Debug "\t\tThere isn't any active VE, skipping test"; return 1; fi; 

Debug "\t\tFlushing default VEs"; 
Debug "\t\t\tReturned value " "-n";
lvectl --flush default
if [ $? -ne 0 ]; then Debug "\t\tERROR"; else Debug "\t\tOK"; fi;

Debug "\t\t\tChanges in memory" "-n"; 
diff -u $proc_file /tmp/tmp_proc_file > /dev/null
if [ $? -ne 0 ]; then Debug "\tOK"; else Debug "\tFAIL"; fi;

rm -f /tmp/tmp_config_file /tmp/tmp_proc_file

# testint lvectl --flush all

cp $proc_file /tmp/tmp_proc_file
cp $config_file /tmp/tmp_config_file

ve_count=$(cat $proc_file | wc -l )
if [ $ve_count -eq 1 ]; then Debug "\t\tThere isn't any active VE, skipping test"; return 1; fi; 

Debug "\t\tFlushing all VEs"; 
Debug "\t\t\tReturned value " "-n";
lvectl --flush all
if [ $? -ne 0 ]; then Debug "\t\tERROR"; else Debug "\t\tOK"; fi;

Debug "\t\t\tChanges in memory" "-n"; 
diff -u $proc_file /tmp/tmp_proc_file > /dev/null
if [ $? -ne 0 ]; then Debug "\tOK"; else Debug "\tFAIL"; fi;

rm -f /tmp/tmp_config_file /tmp/tmp_proc_file
#-===========================================================-
Debug "\t\tCheckig creation of flushed VE0 " "-n"
lvectl --new --ve 20002
if [ $? -ne 0 ]; then Debug "\t\tOK"; else Debug "\t\tOK BUG"; fi;
}

#---------------------------------------------------------------#
while getopts ":cptrh" Arg
do
    case $Arg in
	c )
	    Cleanup
	;;
	p )
	    Prep
	;;
	t )
	    Test
	;;
	r )
	    Restore
	;;
	h ) 
	    Usage
	;;
	* )
	    Usage
	;;
    esac
done;

exit $?