-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor-test.sh
executable file
·143 lines (125 loc) · 2.85 KB
/
monitor-test.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/env roundup
# http://bmizerany.github.com/roundup
describe "Monitor specific information on stdout and everything on background."
monitor="$PWD/monitor.sh"
before() {
__DIR__="$PWD"
rm -rf .sandbox
mkdir -p .sandbox
cd .sandbox
}
after() {
rm -rf "$__DIR__/.sandbox"
}
it_shows_usage_with_no_argv()
{
$monitor 2>&1 | grep 'USAGE'
}
it_shows_help_when_asked()
{
$monitor -h 2>&1 | grep -q 'USAGE' && $monitor --help 2>&1 | grep -q 'USAGE'
}
it_shows_message_when_no_action()
{
$monitor -c blah 2>&1 | grep 'missing.*action'
}
it_shows_message_when_invalid_action()
{
$monitor -a blah -l log -c blah 2>&1 | grep 'invalid.*action'
}
it_shows_message_when_no_logfile()
{
$monitor -a start -c sar 2>&1 | grep 'missing.*log'
}
it_can_start_arbitrary_command()
{
local cmd="$monitor -a start -l ${PWD}/db.log -c iostat:1,dm-1"
$cmd &
local m_pid=$!
sleep 2
pgrep -f "$cmd" && \
pgrep -f 'iostat *1 dm-1 *$' && \
pgrep -f 'sadc.*db.log' && \
test -f "${PWD}/db.log"
local rc=$?
# clean up!
kill -TERM $m_pid
wait
exit $rc
}
it_let_no_processes_behind()
{
pgrep -vf '.*monitor.*' && \
pgrep -vf '.*iostat.*' && \
pgrep -vf 'sar.*db.log$'
}
it_can_stop_itself()
{
local cmd="$monitor -a start -l ${PWD}/db.log -c iostat:1,dm-1"
$cmd &
local m_pid=$!
sleep 2
$monitor -a stop
pgrep -fv "$cmd" && \
pgrep -fv 'iostat *1 dm-1 *$' && \
pgrep -fv 'sadc.*db.log' && \
test -f "${PWD}/db.log"
}
it_shows_the_command()
{
local cmd="$monitor -a start -l ${PWD}/db.log -c iostat:1,dm-1"
$cmd >'test-output' 2>&1 &
local m_pid=$!
sleep 2
grep 'avg-cpu' test-output
local rc=$?
kill -TERM $m_pid
exit $rc
}
it_fills_the_db()
{
local cmd="$monitor -a start -l ${PWD}/db.log -c iostat:1,dm-1"
$cmd >'test-output' 2>&1 &
local m_pid=$!
sleep 2
sar -A -f "${PWD}/db.log" 1 2 | grep '^Average.*all'
local rc=$?
kill -TERM $m_pid
exit $rc
}
it_can_be_run_without_command()
{
local cmd="$monitor -a start -l ${PWD}/db.log"
$cmd >'test-output' 2>&1 &
local m_pid=$!
sleep 2
# if it has disappeared, it had a problem.
ps -p $m_pid 2>/dev/null || exit 42
kill -TERM $m_pid
wait $m_pid
exit 0
}
it_fails_if_command_is_a_nonsense()
{
local cmd="$monitor -a start -l ${PWD}/db.log -c myblahfoobar:-df"
$cmd &
local m_pid=$!
sleep 2
ps -p $m_pid 2>/dev/null || exit 0
# cleanup (shouldn't happen)
kill -TERM $m_pid
wait $m_pid
exit 42
}
it_can_have_command_with_dash_in_args()
{
local cmd="$monitor -a start -l ${PWD}/db.log -c dstat:-df,-Dsda1_,_dm-0"
$cmd &
local m_pid=$!
sleep 2
ps -p $m_pid 2>/dev/null || exit 42
# cleanup (should happen)
kill -TERM $m_pid
wait $m_pid
exit 0
}