-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_anyservice.sh
executable file
·97 lines (73 loc) · 1.99 KB
/
test_anyservice.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/bin/bash
MYANYSERVICE=anyservice
VERBOSE="false"
update_anyservice(){
cp -u "./${MYANYSERVICE}.sh" /usr/bin/$MYANYSERVICE
}
test_var_init(){
. ./anyservice.sh any
SERVDIR="./"
SERVFILE="any.service"
cat $SERVDIR/$SERVFILE | grep =
read_config
echo myWorkingDirectory $WorkingDirectory
! [ -n "$WorkingDirectory" ] && echo "test var init FAIL"
}
test_work(){
SERVDIR="/etc/anyservice"
my_test_service="top"
MYTIMETOSLEEP=65
MYPID="/var/run/${my_test_service}.pid"
#Initilisation
cp ./${my_test_service}.service $SERVDIR/
my_run $MYANYSERVICE $my_test_service
my_run $MYANYSERVICE $my_test_service start
#Check monit
$VERBOSE && cat /etc/monit.d/"$my_test_service"*
sleep $MYTIMETOSLEEP
my_run $MYANYSERVICE "$my_test_service" status
#Tests
test_monit_status Running
#Test pid
#TODO need sleep while monit updates status
mupid="$(monit status | grep -A 3 $my_test_service | grep pid | awk '{print $2}')"
echo $mupid
ps aux | grep -m1 "$mupid" | grep "$my_test_service" && echo_correct Pid || echo_incorrect Pid
cat $MYPID
[ "$(ps aux | grep -m1 "^root.*${my_test_service}" | awk '{print $2}')" = "$(cat $MYPID)" ] && echo_correct Pid || echo_incorrect Pid
#Test user
[ "$(ps aux | grep -m1 "$mupid" | awk '{print $1}') " = "root" ] && echo_correct User || echo_incorrect User
sleep $MYTIMETOSLEEP
my_run $MYANYSERVICE "$my_test_service" stop
sleep $MYTIMETOSLEEP
my_run $MYANYSERVICE "$my_test_service" status
#Test pid
kill $mypid &> /dev/null || echo Killed
}
my_run(){
if [ "$VERBOSE" = true ] ; then
$1 $2 $3
else
$1 $2 $3 &> /dev/null
fi
}
echo_correct(){
echo "$1 is correct"
RETVAL="0"
return $RETVAL
}
echo_incorrect(){
echo "$1 is INCORRECT"
RETVAL="1"
return $RETVAL
}
test_monit_status(){
my_run monit status "$my_test_service" | grep -A 3 $MYTIMETOSLEEP | grep "$1" && echo "$1"
}
test_result(){
echo ""
[ "$RETVAL" = 0 ] && echo "Tests DONE" || echo "Tests FAIL"
}
update_anyservice
test_work
test_result