Skip to content

Commit 4e926e7

Browse files
committed
Created few test cases
1 parent 87fcc5d commit 4e926e7

File tree

3 files changed

+264
-0
lines changed

3 files changed

+264
-0
lines changed

test/json.sh

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# https://github.com/dominictarr/JSON.sh
2+
# Code is licensed under the MIT license
3+
# Copyright (c) 2011 Dominic Tarr
4+
5+
throw () {
6+
echo "$*" >&2
7+
exit 1
8+
}
9+
10+
tokenize () {
11+
local ESCAPE='(\\[^u[:cntrl:]]|\\u[0-9a-fA-F]{4})'
12+
local CHAR='[^[:cntrl:]"\\]'
13+
local STRING="\"$CHAR*($ESCAPE$CHAR*)*\""
14+
local NUMBER='-?(0|[1-9][0-9]*)([.][0-9]*)?([eE][+-]?[0-9]*)?'
15+
local KEYWORD='null|false|true'
16+
local SPACE='[[:space:]]+'
17+
egrep -ao "$STRING|$NUMBER|$KEYWORD|$SPACE|." --color=never |
18+
egrep -v "^$SPACE$" # eat whitespace
19+
}
20+
21+
parse_array () {
22+
local index=0
23+
local ary=''
24+
read -r token
25+
case "$token" in
26+
']') ;;
27+
*)
28+
while :
29+
do
30+
parse_value "$1" "$index"
31+
let index=$index+1
32+
ary="$ary""$value"
33+
read -r token
34+
case "$token" in
35+
']') break ;;
36+
',') ary="$ary," ;;
37+
*) throw "EXPECTED , or ] GOT ${token:-EOF}" ;;
38+
esac
39+
read -r token
40+
done
41+
;;
42+
esac
43+
value=`printf '[%s]' $ary`
44+
}
45+
46+
parse_object () {
47+
local key
48+
local obj=''
49+
read -r token
50+
case "$token" in
51+
'}') ;;
52+
*)
53+
while :
54+
do
55+
case "$token" in
56+
'"'*'"') key=$token ;;
57+
*) throw "EXPECTED string GOT ${token:-EOF}" ;;
58+
esac
59+
read -r token
60+
case "$token" in
61+
':') ;;
62+
*) throw "EXPECTED : GOT ${token:-EOF}" ;;
63+
esac
64+
read -r token
65+
parse_value "$1" "$key"
66+
obj="$obj$key:$value"
67+
read -r token
68+
case "$token" in
69+
'}') break ;;
70+
',') obj="$obj," ;;
71+
*) throw "EXPECTED , or } GOT ${token:-EOF}" ;;
72+
esac
73+
read -r token
74+
done
75+
;;
76+
esac
77+
value=`printf '{%s}' "$obj"`
78+
}
79+
80+
parse_value () {
81+
local jpath="${1:+$1,}$2"
82+
case "$token" in
83+
'{') parse_object "$jpath" ;;
84+
'[') parse_array "$jpath" ;;
85+
# At this point, the only valid single-character tokens are digits.
86+
''|[^0-9]) throw "EXPECTED value GOT ${token:-EOF}" ;;
87+
*) value=$token ;;
88+
esac
89+
printf "[%s]\t%s\n" "$jpath" "$value"
90+
}
91+
92+
parse () {
93+
read -r token
94+
parse_value
95+
read -r token
96+
case "$token" in
97+
'') ;;
98+
*) throw "EXPECTED EOF GOT $token" ;;
99+
esac
100+
}
101+
102+
if [ $0 = $BASH_SOURCE ];
103+
then
104+
tokenize | parse
105+
fi

test/test_actions.sh

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#!/bin/bash
2+
3+
# Define some variables
4+
source /etc/profile.d/vesta.sh
5+
V_BIN="$VESTA/bin"
6+
V_TEST="$VESTA/test"
7+
8+
# Define functions
9+
tmp_user() {
10+
MATRIX='0123456789'
11+
LENGTH=4
12+
while [ ${n:=1} -le $LENGTH ]; do
13+
rand="$rand${MATRIX:$(($RANDOM%${#MATRIX})):1}"
14+
let n+=1
15+
done
16+
echo "tmp_$rand"
17+
}
18+
19+
echo_result() {
20+
echo -en "$1"
21+
echo -en '\033[60G'
22+
echo -n '['
23+
24+
if [ "$2" -ne 0 ]; then
25+
echo -n 'FAILED'
26+
echo -n ']'
27+
echo -ne '\r\n'
28+
echo "$4"
29+
echo "RETURN VALUE $2"
30+
cat $3
31+
else
32+
echo -n ' OK '
33+
echo -n ']'
34+
fi
35+
echo -ne '\r\n'
36+
}
37+
38+
# Create random username
39+
user=$(tmp_user)
40+
while [ ! -z "$(grep "^$user:" /etc/passwd)" ]; do
41+
user=$(tmp_user)
42+
done
43+
44+
# Create random tmpfile
45+
tmpfile=$(mktemp -p /tmp )
46+
47+
# Add new user
48+
cmd="v_add_user $user $user $user@vestacp.com default Super Test"
49+
$cmd > $tmpfile 2>> $tmpfile
50+
echo_result "Adding new user $user" "$?" "$tmpfile" "$cmd"
51+
52+
# Change system shell
53+
cmd="v_change_user_shell $user bash"
54+
$cmd > $tmpfile 2>> $tmpfile
55+
echo_result "Changing system shell to /bin/bash" "$?" "$tmpfile" "$cmd"
56+
57+
# Change name servers
58+
cmd="v_change_user_ns $user ns0.com ns1.com ns2.com ns3.com"
59+
$cmd > $tmpfile 2>> $tmpfile
60+
echo_result "Changing nameservers" "$?" "$tmpfile" "$cmd"
61+
62+
# Add cron job
63+
cmd="v_add_cron_job $user 1 1 1 1 1 echo"
64+
$cmd > $tmpfile 2>> $tmpfile
65+
echo_result "Adding cron job" "$?" "$tmpfile" "$cmd"
66+
67+
# Suspend cron job
68+
cmd="v_suspend_cron_job $user 1"
69+
$cmd > $tmpfile 2>> $tmpfile
70+
echo_result "Suspending cron job" "$?" "$tmpfile" "$cmd"
71+
72+
# Unsuspend cron job
73+
cmd="v_unsuspend_cron_job $user 1"
74+
$cmd > $tmpfile 2>> $tmpfile
75+
echo_result "Unsuspending cron job" "$?" "$tmpfile" "$cmd"
76+
77+
# Delete cron job
78+
cmd="v_delete_cron_job $user 1"
79+
$cmd > $tmpfile 2>> $tmpfile
80+
echo_result "Deleting cron job" "$?" "$tmpfile" "$cmd"
81+
82+
# Add cron job
83+
cmd="v_add_cron_job $user 1 1 1 1 1 echo 1"
84+
$cmd > $tmpfile 2>> $tmpfile
85+
echo_result "Adding cron job" "$?" "$tmpfile" "$cmd"
86+
87+
# Add cron job
88+
cmd="v_add_cron_job $user 1 1 1 1 1 echo 1"
89+
$cmd > $tmpfile 2>> $tmpfile
90+
if [ "$?" -eq 4 ]; then
91+
retval=0
92+
else
93+
retval=1
94+
fi
95+
echo_result "Dublicate cron job check" "$retval" "$tmpfile" "$cmd"
96+
97+
# Check ip
98+
#cat /proc/net/dev|cut -f 1 -d :|tail -n1
99+
#v_add_sys_ip 192.168.11.11 255.255.255.255 venet0 ekho
100+
101+
# Delete new user
102+
cmd="v_delete_user $user"
103+
$cmd > $tmpfile 2>> $tmpfile
104+
echo_result "Deleting user $user" "$?" "$tmpfile" "$cmd"
105+

test/test_json_listing.sh

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/bin/bash
2+
3+
source /etc/profile.d/vesta.sh
4+
5+
V_BIN="$VESTA/bin"
6+
V_TEST="$VESTA/test"
7+
8+
commands='v_list_cron_jobs vesta json
9+
v_list_db_bases vesta json
10+
v_list_db_base vesta vesta_read json
11+
v_list_db_host mysql localhost json
12+
v_list_db_hosts mysql json
13+
v_list_dns_domains vesta json
14+
v_list_dns_templates json
15+
v_list_sys_config json
16+
v_list_sys_interfaces json
17+
v_list_sys_ips json
18+
v_list_sys_rrd json
19+
v_list_user vesta json
20+
v_list_user_backups vesta json
21+
v_list_user_ips vesta json
22+
v_list_user_ns vesta json
23+
v_list_user_packages json
24+
v_list_users json
25+
v_list_web_domains vesta json
26+
v_list_web_domain vesta default.vesta.domain json
27+
v_list_web_templates vesta json'
28+
29+
IFS=$'\n'
30+
for cmd in $commands; do
31+
script=$(echo $cmd |cut -f 1 -d ' ')
32+
arg1=$(echo $cmd |cut -f 2 -d ' ')
33+
arg2=$(echo $cmd |cut -f 3 -d ' ')
34+
arg3=$(echo $cmd |cut -f 4 -d ' ')
35+
$V_BIN/$script $arg1 $arg2 $arg3 | $V_TEST/json.sh >/dev/null 2>/dev/null
36+
retval="$?"
37+
echo -en "$cmd"
38+
echo -en '\033[60G'
39+
echo -n '['
40+
41+
if [ "$retval" -ne 0 ]; then
42+
echo -n 'FAILED'
43+
echo -n ']'
44+
echo -ne '\r\n'
45+
$V_BIN/$script $arg1 $arg2 $arg3 | $V_TEST/json.sh
46+
else
47+
echo -n ' OK '
48+
echo -n ']'
49+
fi
50+
echo -ne '\r\n'
51+
52+
done
53+
54+
exit

0 commit comments

Comments
 (0)