Created few test cases

This commit is contained in:
Serghey Rodin 2012-01-10 23:52:01 +02:00
parent 87fcc5d83b
commit 4e926e746f
3 changed files with 264 additions and 0 deletions

105
test/json.sh Executable file
View file

@ -0,0 +1,105 @@
# https://github.com/dominictarr/JSON.sh
# Code is licensed under the MIT license
# Copyright (c) 2011 Dominic Tarr
throw () {
echo "$*" >&2
exit 1
}
tokenize () {
local ESCAPE='(\\[^u[:cntrl:]]|\\u[0-9a-fA-F]{4})'
local CHAR='[^[:cntrl:]"\\]'
local STRING="\"$CHAR*($ESCAPE$CHAR*)*\""
local NUMBER='-?(0|[1-9][0-9]*)([.][0-9]*)?([eE][+-]?[0-9]*)?'
local KEYWORD='null|false|true'
local SPACE='[[:space:]]+'
egrep -ao "$STRING|$NUMBER|$KEYWORD|$SPACE|." --color=never |
egrep -v "^$SPACE$" # eat whitespace
}
parse_array () {
local index=0
local ary=''
read -r token
case "$token" in
']') ;;
*)
while :
do
parse_value "$1" "$index"
let index=$index+1
ary="$ary""$value"
read -r token
case "$token" in
']') break ;;
',') ary="$ary," ;;
*) throw "EXPECTED , or ] GOT ${token:-EOF}" ;;
esac
read -r token
done
;;
esac
value=`printf '[%s]' $ary`
}
parse_object () {
local key
local obj=''
read -r token
case "$token" in
'}') ;;
*)
while :
do
case "$token" in
'"'*'"') key=$token ;;
*) throw "EXPECTED string GOT ${token:-EOF}" ;;
esac
read -r token
case "$token" in
':') ;;
*) throw "EXPECTED : GOT ${token:-EOF}" ;;
esac
read -r token
parse_value "$1" "$key"
obj="$obj$key:$value"
read -r token
case "$token" in
'}') break ;;
',') obj="$obj," ;;
*) throw "EXPECTED , or } GOT ${token:-EOF}" ;;
esac
read -r token
done
;;
esac
value=`printf '{%s}' "$obj"`
}
parse_value () {
local jpath="${1:+$1,}$2"
case "$token" in
'{') parse_object "$jpath" ;;
'[') parse_array "$jpath" ;;
# At this point, the only valid single-character tokens are digits.
''|[^0-9]) throw "EXPECTED value GOT ${token:-EOF}" ;;
*) value=$token ;;
esac
printf "[%s]\t%s\n" "$jpath" "$value"
}
parse () {
read -r token
parse_value
read -r token
case "$token" in
'') ;;
*) throw "EXPECTED EOF GOT $token" ;;
esac
}
if [ $0 = $BASH_SOURCE ];
then
tokenize | parse
fi

105
test/test_actions.sh Executable file
View file

@ -0,0 +1,105 @@
#!/bin/bash
# Define some variables
source /etc/profile.d/vesta.sh
V_BIN="$VESTA/bin"
V_TEST="$VESTA/test"
# Define functions
tmp_user() {
MATRIX='0123456789'
LENGTH=4
while [ ${n:=1} -le $LENGTH ]; do
rand="$rand${MATRIX:$(($RANDOM%${#MATRIX})):1}"
let n+=1
done
echo "tmp_$rand"
}
echo_result() {
echo -en "$1"
echo -en '\033[60G'
echo -n '['
if [ "$2" -ne 0 ]; then
echo -n 'FAILED'
echo -n ']'
echo -ne '\r\n'
echo "$4"
echo "RETURN VALUE $2"
cat $3
else
echo -n ' OK '
echo -n ']'
fi
echo -ne '\r\n'
}
# Create random username
user=$(tmp_user)
while [ ! -z "$(grep "^$user:" /etc/passwd)" ]; do
user=$(tmp_user)
done
# Create random tmpfile
tmpfile=$(mktemp -p /tmp )
# Add new user
cmd="v_add_user $user $user $user@vestacp.com default Super Test"
$cmd > $tmpfile 2>> $tmpfile
echo_result "Adding new user $user" "$?" "$tmpfile" "$cmd"
# Change system shell
cmd="v_change_user_shell $user bash"
$cmd > $tmpfile 2>> $tmpfile
echo_result "Changing system shell to /bin/bash" "$?" "$tmpfile" "$cmd"
# Change name servers
cmd="v_change_user_ns $user ns0.com ns1.com ns2.com ns3.com"
$cmd > $tmpfile 2>> $tmpfile
echo_result "Changing nameservers" "$?" "$tmpfile" "$cmd"
# Add cron job
cmd="v_add_cron_job $user 1 1 1 1 1 echo"
$cmd > $tmpfile 2>> $tmpfile
echo_result "Adding cron job" "$?" "$tmpfile" "$cmd"
# Suspend cron job
cmd="v_suspend_cron_job $user 1"
$cmd > $tmpfile 2>> $tmpfile
echo_result "Suspending cron job" "$?" "$tmpfile" "$cmd"
# Unsuspend cron job
cmd="v_unsuspend_cron_job $user 1"
$cmd > $tmpfile 2>> $tmpfile
echo_result "Unsuspending cron job" "$?" "$tmpfile" "$cmd"
# Delete cron job
cmd="v_delete_cron_job $user 1"
$cmd > $tmpfile 2>> $tmpfile
echo_result "Deleting cron job" "$?" "$tmpfile" "$cmd"
# Add cron job
cmd="v_add_cron_job $user 1 1 1 1 1 echo 1"
$cmd > $tmpfile 2>> $tmpfile
echo_result "Adding cron job" "$?" "$tmpfile" "$cmd"
# Add cron job
cmd="v_add_cron_job $user 1 1 1 1 1 echo 1"
$cmd > $tmpfile 2>> $tmpfile
if [ "$?" -eq 4 ]; then
retval=0
else
retval=1
fi
echo_result "Dublicate cron job check" "$retval" "$tmpfile" "$cmd"
# Check ip
#cat /proc/net/dev|cut -f 1 -d :|tail -n1
#v_add_sys_ip 192.168.11.11 255.255.255.255 venet0 ekho
# Delete new user
cmd="v_delete_user $user"
$cmd > $tmpfile 2>> $tmpfile
echo_result "Deleting user $user" "$?" "$tmpfile" "$cmd"

54
test/test_json_listing.sh Executable file
View file

@ -0,0 +1,54 @@
#!/bin/bash
source /etc/profile.d/vesta.sh
V_BIN="$VESTA/bin"
V_TEST="$VESTA/test"
commands='v_list_cron_jobs vesta json
v_list_db_bases vesta json
v_list_db_base vesta vesta_read json
v_list_db_host mysql localhost json
v_list_db_hosts mysql json
v_list_dns_domains vesta json
v_list_dns_templates json
v_list_sys_config json
v_list_sys_interfaces json
v_list_sys_ips json
v_list_sys_rrd json
v_list_user vesta json
v_list_user_backups vesta json
v_list_user_ips vesta json
v_list_user_ns vesta json
v_list_user_packages json
v_list_users json
v_list_web_domains vesta json
v_list_web_domain vesta default.vesta.domain json
v_list_web_templates vesta json'
IFS=$'\n'
for cmd in $commands; do
script=$(echo $cmd |cut -f 1 -d ' ')
arg1=$(echo $cmd |cut -f 2 -d ' ')
arg2=$(echo $cmd |cut -f 3 -d ' ')
arg3=$(echo $cmd |cut -f 4 -d ' ')
$V_BIN/$script $arg1 $arg2 $arg3 | $V_TEST/json.sh >/dev/null 2>/dev/null
retval="$?"
echo -en "$cmd"
echo -en '\033[60G'
echo -n '['
if [ "$retval" -ne 0 ]; then
echo -n 'FAILED'
echo -n ']'
echo -ne '\r\n'
$V_BIN/$script $arg1 $arg2 $arg3 | $V_TEST/json.sh
else
echo -n ' OK '
echo -n ']'
fi
echo -ne '\r\n'
done
exit