Merge lp:~percona-toolkit-dev/percona-toolkit/pt-stalk-quiet into lp:percona-toolkit/2.1

Proposed by Daniel Nichter on 2013-01-24
Status: Merged
Approved by: Daniel Nichter on 2013-01-24
Approved revision: 527
Merged at revision: 521
Proposed branch: lp:~percona-toolkit-dev/percona-toolkit/pt-stalk-quiet
Merge into: lp:percona-toolkit/2.1
Diff against target: 910 lines (+391/-87)
13 files modified
bin/pt-ioprofile (+15/-6)
bin/pt-mysql-summary (+15/-6)
bin/pt-pmp (+1/-1)
bin/pt-sift (+1/-1)
bin/pt-stalk (+53/-14)
bin/pt-summary (+15/-6)
lib/PerconaTest.pm (+33/-1)
lib/bash/log_warn_die.sh (+14/-5)
t/lib/bash/log_warn_die.sh (+83/-10)
t/pt-stalk/option_sanity.t (+24/-0)
t/pt-stalk/pt-stalk.t (+90/-33)
t/pt-stalk/samples/plugin002.sh (+5/-0)
util/test-bash-functions (+42/-4)
To merge this branch: bzr merge lp:~percona-toolkit-dev/percona-toolkit/pt-stalk-quiet
Reviewer Review Type Date Requested Status
Daniel Nichter Approve on 2013-01-24
Review via email: mp+144766@code.launchpad.net
To post a comment you must log in.
525. By Daniel Nichter on 2013-01-24

Fix default log level in log_warn_die.sh.

526. By Daniel Nichter on 2013-01-24

Fix typo in t/pt-stalk/option_sanity.t.

527. By Daniel Nichter on 2013-01-24

Fix error caused on some boxes by using diff when it's not defined.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'bin/pt-ioprofile'
2--- bin/pt-ioprofile 2013-01-03 00:54:18 +0000
3+++ bin/pt-ioprofile 2013-01-24 19:10:25 +0000
4@@ -20,23 +20,32 @@
5 PTDEBUG="${PTDEBUG:-""}"
6 EXIT_STATUS=0
7
8+ts() {
9+ TS=$(date +%F-%T | tr ':-' '_')
10+ echo "$TS $*"
11+}
12+
13+info() {
14+ [ ${OPT_VERBOSE:-3} -ge 3 ] && ts "$*"
15+}
16+
17 log() {
18- TS=$(date +%F-%T | tr ':-' '_');
19- echo "$TS $*"
20+ [ ${OPT_VERBOSE:-3} -ge 2 ] && ts "$*"
21 }
22
23 warn() {
24- log "$*" >&2
25+ [ ${OPT_VERBOSE:-3} -ge 1 ] && ts "$*" >&2
26 EXIT_STATUS=1
27 }
28
29 die() {
30- warn "$*"
31+ ts "$*" >&2
32+ EXIT_STATUS=1
33 exit 1
34 }
35
36 _d () {
37- [ "$PTDEBUG" ] && echo "# $PTFUNCNAME: $(log "$*")" >&2
38+ [ "$PTDEBUG" ] && echo "# $PTFUNCNAME: $(ts "$*")" >&2
39 }
40
41 # ###########################################################################
42@@ -861,7 +870,7 @@
43 # Execute the program if it was not included from another file.
44 # This makes it possible to include without executing, and thus test.
45 if [ "${0##*/}" = "$TOOL" ] \
46- || [ "${0##*/}" = "bash" -a "$_" = "$0" ]; then
47+ || [ "${0##*/}" = "bash" -a "${_:-""}" = "$0" ]; then
48
49 # Parse command line options. We must do this first so we can
50 # see if --daemonize was specified.
51
52=== modified file 'bin/pt-mysql-summary'
53--- bin/pt-mysql-summary 2013-01-03 00:54:18 +0000
54+++ bin/pt-mysql-summary 2013-01-24 19:10:25 +0000
55@@ -22,23 +22,32 @@
56 PTDEBUG="${PTDEBUG:-""}"
57 EXIT_STATUS=0
58
59+ts() {
60+ TS=$(date +%F-%T | tr ':-' '_')
61+ echo "$TS $*"
62+}
63+
64+info() {
65+ [ ${OPT_VERBOSE:-3} -ge 3 ] && ts "$*"
66+}
67+
68 log() {
69- TS=$(date +%F-%T | tr ':-' '_');
70- echo "$TS $*"
71+ [ ${OPT_VERBOSE:-3} -ge 2 ] && ts "$*"
72 }
73
74 warn() {
75- log "$*" >&2
76+ [ ${OPT_VERBOSE:-3} -ge 1 ] && ts "$*" >&2
77 EXIT_STATUS=1
78 }
79
80 die() {
81- warn "$*"
82+ ts "$*" >&2
83+ EXIT_STATUS=1
84 exit 1
85 }
86
87 _d () {
88- [ "$PTDEBUG" ] && echo "# $PTFUNCNAME: $(log "$*")" >&2
89+ [ "$PTDEBUG" ] && echo "# $PTFUNCNAME: $(ts "$*")" >&2
90 }
91
92 # ###########################################################################
93@@ -2412,7 +2421,7 @@
94 # Execute the program if it was not included from another file.
95 # This makes it possible to include without executing, and thus test.
96 if [ "${0##*/}" = "$TOOL" ] \
97- || [ "${0##*/}" = "bash" -a "$_" = "$0" ]; then
98+ || [ "${0##*/}" = "bash" -a "${_:-""}" = "$0" ]; then
99
100 # Set up temporary dir.
101 mk_tmpdir
102
103=== modified file 'bin/pt-pmp'
104--- bin/pt-pmp 2013-01-03 00:54:18 +0000
105+++ bin/pt-pmp 2013-01-24 19:10:25 +0000
106@@ -215,7 +215,7 @@
107 # Execute the program if it was not included from another file. This makes it
108 # possible to include without executing, and thus test.
109 if [ "${0##*/}" = "$TOOL" ] \
110- || [ "${0##*/}" = "bash" -a "$_" = "$0" ]; then
111+ || [ "${0##*/}" = "bash" -a "${_:-""}" = "$0" ]; then
112 mk_tmpdir
113 main "$@"
114 rm_tmpdir
115
116=== modified file 'bin/pt-sift'
117--- bin/pt-sift 2013-01-03 00:54:18 +0000
118+++ bin/pt-sift 2013-01-24 19:10:25 +0000
119@@ -587,7 +587,7 @@
120 # Execute the program if it was not included from another file. This makes it
121 # possible to include without executing, and thus test.
122 if [ "${0##*/}" = "$TOOL" ] \
123- || [ "${0##*/}" = "bash" -a "$_" = "$0" ]; then
124+ || [ "${0##*/}" = "bash" -a "${_:-""}" = "$0" ]; then
125 main "${@:-""}"
126 fi
127
128
129=== modified file 'bin/pt-stalk'
130--- bin/pt-stalk 2013-01-03 00:54:18 +0000
131+++ bin/pt-stalk 2013-01-24 19:10:25 +0000
132@@ -22,23 +22,32 @@
133 PTDEBUG="${PTDEBUG:-""}"
134 EXIT_STATUS=0
135
136+ts() {
137+ TS=$(date +%F-%T | tr ':-' '_')
138+ echo "$TS $*"
139+}
140+
141+info() {
142+ [ ${OPT_VERBOSE:-3} -ge 3 ] && ts "$*"
143+}
144+
145 log() {
146- TS=$(date +%F-%T | tr ':-' '_');
147- echo "$TS $*"
148+ [ ${OPT_VERBOSE:-3} -ge 2 ] && ts "$*"
149 }
150
151 warn() {
152- log "$*" >&2
153+ [ ${OPT_VERBOSE:-3} -ge 1 ] && ts "$*" >&2
154 EXIT_STATUS=1
155 }
156
157 die() {
158- warn "$*"
159+ ts "$*" >&2
160+ EXIT_STATUS=1
161 exit 1
162 }
163
164 _d () {
165- [ "$PTDEBUG" ] && echo "# $PTFUNCNAME: $(log "$*")" >&2
166+ [ "$PTDEBUG" ] && echo "# $PTFUNCNAME: $(ts "$*")" >&2
167 }
168
169 # ###########################################################################
170@@ -962,6 +971,10 @@
171 :
172 }
173
174+after_interval_sleep() {
175+ :
176+}
177+
178 after_stalk() {
179 :
180 }
181@@ -1066,9 +1079,7 @@
182 local seconds="$1"
183 local msg="${2:-""}"
184 if oktorun; then
185- if [ -n "$msg" ]; then
186- log "$msg"
187- fi
188+ [ "$msg" ] && info "$msg"
189 sleep $seconds
190 fi
191 }
192@@ -1126,7 +1137,11 @@
193 fi
194
195 local msg="Check results: $OPT_VARIABLE=$value, matched=${matched:-no}, cycles_true=$cycles_true"
196- log "$msg"
197+ if [ "$matched" ]; then
198+ log "$msg"
199+ else
200+ info "$msg"
201+ fi
202 elif [ "$OPT_COLLECT" ]; then
203 # Make the next if condition true.
204 matched=1
205@@ -1140,7 +1155,7 @@
206 # ##################################################################
207 # Start collecting, maybe.
208 # ##################################################################
209- log "Collect triggered"
210+ log "Collect $ITER triggered"
211
212 # Send email to whomever that collect has been triggered.
213 if [ "$OPT_NOTIFY_BY_EMAIL" ]; then
214@@ -1166,8 +1181,8 @@
215 "$margin"
216 if [ $? -eq 0 ]; then
217 # There should be enough disk space, so collect.
218- log "$msg" >> "$OPT_DEST/$prefix-trigger"
219- log "pt-stalk ran with $RAN_WITH" >> "$OPT_DEST/$prefix-trigger"
220+ ts "$msg" >> "$OPT_DEST/$prefix-trigger"
221+ ts "pt-stalk ran with $RAN_WITH" >> "$OPT_DEST/$prefix-trigger"
222 last_prefix="$prefix"
223
224 # Plugin hook:
225@@ -1181,7 +1196,7 @@
226 collect "$OPT_DEST" "$prefix"
227 ) >> "$OPT_DEST/$prefix-output" 2>&1 &
228 local collector_pid=$!
229- log "Collector PID $collector_pid"
230+ log "Collect $ITER PID $collector_pid"
231
232 # Plugin hook:
233 after_collect $collector_pid
234@@ -1199,6 +1214,7 @@
235 # ##################################################################
236 # Done collecting.
237 # ##################################################################
238+ log "Collect $ITER done"
239 ITER=$((ITER + 1))
240 cycles_true=0
241 sleep_ok "$OPT_SLEEP" "Sleeping $OPT_SLEEP seconds after collect"
242@@ -1208,6 +1224,9 @@
243 else
244 # Trigger/check/value is ok, sleep until next check.
245 sleep_ok "$OPT_INTERVAL"
246+
247+ # Plugin hook:
248+ after_interval_sleep
249 fi
250 done
251
252@@ -1291,7 +1310,7 @@
253 # Execute the program if it was not included from another file.
254 # This makes it possible to include without executing, and thus test.
255 if [ "${0##*/}" = "$TOOL" ] \
256- || [ "${0##*/}" = "bash" -a "$_" = "$0" ]; then
257+ || [ "${0##*/}" = "bash" -a "${_:-""}" = "$0" ]; then
258
259 # Parse command line options. We must do this first so we can
260 # see if --daemonize was specified.
261@@ -1737,6 +1756,10 @@
262 Called after sleeping L<"--sleep"> seconds for the collector process to finish.
263 This hook is called after C<after_collect>.
264
265+=item after_interval_sleep
266+
267+Called after sleeping L<"--interval"> seconds after each trigger check.
268+
269 =item after_stalk
270
271 Called after stalking. Since pt-stalk stalks forever by default,
272@@ -1840,6 +1863,22 @@
273
274 The variable to compare against the threshold. See L<"--function"> for details.
275
276+=item --verbose
277+
278+type: int; default: 2
279+
280+Print more or less information while running. Since the tool is designed
281+to be a long-running daemon, the default verbosity level only prints the
282+most important information. If you run the tool interactively, you may
283+want to use a higher verbosity level.
284+
285+ LEVEL PRINTS
286+ ===== =====================================
287+ 0 Errors
288+ 1 Warnings
289+ 2 Matching triggers and collection info
290+ 3 Non-matching triggers
291+
292 =item --version
293
294 Print tool's version and exit.
295
296=== modified file 'bin/pt-summary'
297--- bin/pt-summary 2013-01-03 00:54:18 +0000
298+++ bin/pt-summary 2013-01-24 19:10:25 +0000
299@@ -29,23 +29,32 @@
300 PTDEBUG="${PTDEBUG:-""}"
301 EXIT_STATUS=0
302
303+ts() {
304+ TS=$(date +%F-%T | tr ':-' '_')
305+ echo "$TS $*"
306+}
307+
308+info() {
309+ [ ${OPT_VERBOSE:-3} -ge 3 ] && ts "$*"
310+}
311+
312 log() {
313- TS=$(date +%F-%T | tr ':-' '_');
314- echo "$TS $*"
315+ [ ${OPT_VERBOSE:-3} -ge 2 ] && ts "$*"
316 }
317
318 warn() {
319- log "$*" >&2
320+ [ ${OPT_VERBOSE:-3} -ge 1 ] && ts "$*" >&2
321 EXIT_STATUS=1
322 }
323
324 die() {
325- warn "$*"
326+ ts "$*" >&2
327+ EXIT_STATUS=1
328 exit 1
329 }
330
331 _d () {
332- [ "$PTDEBUG" ] && echo "# $PTFUNCNAME: $(log "$*")" >&2
333+ [ "$PTDEBUG" ] && echo "# $PTFUNCNAME: $(ts "$*")" >&2
334 }
335
336 # ###########################################################################
337@@ -2280,7 +2289,7 @@
338 # Execute the program if it was not included from another file. This makes it
339 # possible to include without executing, and thus test.
340 if [ "${0##*/}" = "$TOOL" ] \
341- || [ "${0##*/}" = "bash" -a "$_" = "$0" ]; then
342+ || [ "${0##*/}" = "bash" -a "${_:-""}" = "$0" ]; then
343
344 # Set up temporary dir.
345 mk_tmpdir
346
347=== modified file 'lib/PerconaTest.pm'
348--- lib/PerconaTest.pm 2013-01-03 00:19:16 +0000
349+++ lib/PerconaTest.pm 2013-01-24 19:10:25 +0000
350@@ -58,7 +58,6 @@
351 wait_until
352 wait_for
353 wait_until_slave_running
354- wait_until_no_lag
355 test_log_parser
356 test_protocol_parser
357 test_packet_parser
358@@ -325,6 +324,39 @@
359 );
360 };
361
362+sub kill_program {
363+ my (%args) = @_;
364+
365+ my $pid_file = $args{pid_file};
366+ my $pid = $args{pid};
367+
368+ if ( $pid_file ) {
369+ chomp($pid = `cat $pid_file 2>/dev/null`);
370+ }
371+
372+ if ( $pid ) {
373+ PTDEVDEBUG && _d('Killing PID', $pid);
374+ kill(15, $pid);
375+ wait_until(
376+ sub { my $is_alive = kill(0, $pid); return !$is_alive; },
377+ 1.5, # sleep between tries
378+ 15, # max time to try
379+ );
380+ if ( kill(0, $pid) ) {
381+ warn "PID $pid did not die; using kill -9\n";
382+ kill(9, $pid);
383+ }
384+ }
385+ else {
386+ PTDEVDEBUG && _d('No PID to kill');
387+ }
388+
389+ if ( $pid_file && -f $pid_file ) {
390+ PTDEVDEBUG && _d('Removing PID file', $pid_file);
391+ unlink $pid_file;
392+ }
393+}
394+
395 sub not_running {
396 my ($cmd) = @_;
397 PTDEVDEBUG && _d('Wait until not running:', $cmd);
398
399=== modified file 'lib/bash/log_warn_die.sh'
400--- lib/bash/log_warn_die.sh 2012-08-13 15:23:18 +0000
401+++ lib/bash/log_warn_die.sh 2013-01-24 19:10:25 +0000
402@@ -28,23 +28,32 @@
403 PTDEBUG="${PTDEBUG:-""}"
404 EXIT_STATUS=0
405
406+ts() {
407+ TS=$(date +%F-%T | tr ':-' '_')
408+ echo "$TS $*"
409+}
410+
411+info() {
412+ [ ${OPT_VERBOSE:-3} -ge 3 ] && ts "$*"
413+}
414+
415 log() {
416- TS=$(date +%F-%T | tr ':-' '_');
417- echo "$TS $*"
418+ [ ${OPT_VERBOSE:-3} -ge 2 ] && ts "$*"
419 }
420
421 warn() {
422- log "$*" >&2
423+ [ ${OPT_VERBOSE:-3} -ge 1 ] && ts "$*" >&2
424 EXIT_STATUS=1
425 }
426
427 die() {
428- warn "$*"
429+ ts "$*" >&2
430+ EXIT_STATUS=1
431 exit 1
432 }
433
434 _d () {
435- [ "$PTDEBUG" ] && echo "# $PTFUNCNAME: $(log "$*")" >&2
436+ [ "$PTDEBUG" ] && echo "# $PTFUNCNAME: $(ts "$*")" >&2
437 }
438
439 # ###########################################################################
440
441=== modified file 't/lib/bash/log_warn_die.sh'
442--- t/lib/bash/log_warn_die.sh 2012-06-11 20:51:43 +0000
443+++ t/lib/bash/log_warn_die.sh 2013-01-24 19:10:25 +0000
444@@ -1,17 +1,15 @@
445 #!/usr/bin/env bash
446
447-plan 6
448-
449 source "$LIB_DIR/log_warn_die.sh"
450
451-log "Hello world!" > $TEST_PT_TMPDIR/log
452+log "Hello world A!" > $TEST_PT_TMPDIR/log
453 cmd_ok \
454- "grep -q 'Hello world!' $TEST_PT_TMPDIR/log" \
455+ "grep -q 'Hello world A!' $TEST_PT_TMPDIR/log" \
456 "log msg"
457
458-log "Hello" "world!" > $TEST_PT_TMPDIR/log
459+log "Hello" "world B!" > $TEST_PT_TMPDIR/log
460 cmd_ok \
461- "grep -q 'Hello world!' $TEST_PT_TMPDIR/log" \
462+ "grep -q 'Hello world B!' $TEST_PT_TMPDIR/log" \
463 "log msg msg"
464
465 is \
466@@ -19,14 +17,14 @@
467 "0" \
468 "Exit status 0"
469
470-warn "Hello world!" 2> $TEST_PT_TMPDIR/log
471+warn "Hello world C!" 2> $TEST_PT_TMPDIR/log
472 cmd_ok \
473- "grep -q 'Hello world!' $TEST_PT_TMPDIR/log" \
474+ "grep -q 'Hello world C!' $TEST_PT_TMPDIR/log" \
475 "warn msg"
476
477-warn "Hello" "world!" 2> $TEST_PT_TMPDIR/log
478+warn "Hello" "world D!" 2> $TEST_PT_TMPDIR/log
479 cmd_ok \
480- "grep -q 'Hello world!' $TEST_PT_TMPDIR/log" \
481+ "grep -q 'Hello world D!' $TEST_PT_TMPDIR/log" \
482 "warn msg msg"
483
484 is \
485@@ -34,6 +32,81 @@
486 "1" \
487 "Exit status 1"
488
489+OPT_VERBOSE=1
490+
491+info "Hello world 1!" > $TEST_PT_TMPDIR/log
492+file_is_empty \
493+ $TEST_PT_TMPDIR/log \
494+ "verbose=1 info"
495+
496+log "Hello world 2!" > $TEST_PT_TMPDIR/log
497+file_is_empty \
498+ $TEST_PT_TMPDIR/log \
499+ "verbose=1 log"
500+
501+warn "Hello world 3!" > $TEST_PT_TMPDIR/log 2>&1
502+file_contains \
503+ $TEST_PT_TMPDIR/log \
504+ "Hello world 3!" \
505+ "verbose=1 warn"
506+
507+OPT_VERBOSE=2
508+
509+info "Hello world 4!" > $TEST_PT_TMPDIR/log
510+file_is_empty \
511+ $TEST_PT_TMPDIR/log \
512+ "verbose=2 info"
513+
514+log "Hello world 5!" > $TEST_PT_TMPDIR/log
515+file_contains \
516+ $TEST_PT_TMPDIR/log \
517+ "Hello world 5!" \
518+ "verbose=2 log"
519+
520+warn "Hello world 6!" > $TEST_PT_TMPDIR/log 2>&1
521+file_contains \
522+ $TEST_PT_TMPDIR/log \
523+ "Hello world 6!" \
524+ "verbose=2 warn"
525+
526+OPT_VERBOSE=3
527+
528+info "Hello world 7!" > $TEST_PT_TMPDIR/log
529+file_contains \
530+ $TEST_PT_TMPDIR/log \
531+ "Hello world 7!" \
532+ "verbose=3 info"
533+
534+log "Hello world 8!" > $TEST_PT_TMPDIR/log
535+file_contains \
536+ $TEST_PT_TMPDIR/log \
537+ "Hello world 8!" \
538+ "verbose=3 log"
539+
540+warn "Hello world 9!" > $TEST_PT_TMPDIR/log 2>&1
541+file_contains \
542+ $TEST_PT_TMPDIR/log \
543+ "Hello world 9!" \
544+ "verbose=3 warn"
545+
546+OPT_VERBOSE=0
547+
548+info "Hello world 10!" > $TEST_PT_TMPDIR/log
549+file_is_empty \
550+ $TEST_PT_TMPDIR/log \
551+ "verbose=0 info"
552+
553+log "Hello world 11!" > $TEST_PT_TMPDIR/log
554+file_is_empty \
555+ $TEST_PT_TMPDIR/log \
556+ "verbose=0 log"
557+
558+warn "Hello world 12!" > $TEST_PT_TMPDIR/log 2>&1
559+file_is_empty \
560+ $TEST_PT_TMPDIR/log \
561+ "verbose=0 warn"
562+
563 # ###########################################################################
564 # Done
565 # ###########################################################################
566+done_testing
567
568=== added file 't/pt-stalk/option_sanity.t'
569--- t/pt-stalk/option_sanity.t 1970-01-01 00:00:00 +0000
570+++ t/pt-stalk/option_sanity.t 2013-01-24 19:10:25 +0000
571@@ -0,0 +1,24 @@
572+#!/usr/bin/env perl
573+
574+BEGIN {
575+ die "The PERCONA_TOOLKIT_BRANCH environment variable is not set.\n"
576+ unless $ENV{PERCONA_TOOLKIT_BRANCH} && -d $ENV{PERCONA_TOOLKIT_BRANCH};
577+ unshift @INC, "$ENV{PERCONA_TOOLKIT_BRANCH}/lib";
578+};
579+
580+use strict;
581+use warnings FATAL => 'all';
582+use English qw(-no_match_vars);
583+use Test::More;
584+
585+use PerconaTest;
586+
587+my $output = `$trunk/bin/pt-stalk --help`;
588+
589+like(
590+ $output,
591+ qr/^\s+--verbose\s+2/m,
592+ "Default --verbose=2"
593+);
594+
595+done_testing;
596
597=== modified file 't/pt-stalk/pt-stalk.t'
598--- t/pt-stalk/pt-stalk.t 2012-10-23 23:51:59 +0000
599+++ t/pt-stalk/pt-stalk.t 2013-01-24 19:10:25 +0000
600@@ -28,11 +28,28 @@
601 my $pid_file = "/tmp/pt-stalk.pid.$PID";
602 my $log_file = "/tmp/pt-stalk.log.$PID";
603 my $dest = "/tmp/pt-stalk.collect.$PID";
604+my $int_file = "/tmp/pt-stalk-after-interval-sleep";
605 my $pid;
606
607-diag(`rm $pid_file 2>/dev/null`);
608-diag(`rm $log_file 2>/dev/null`);
609-diag(`rm -rf $dest 2>/dev/null`);
610+sub cleanup {
611+ diag(`rm $pid_file $log_file $int_file 2>/dev/null`);
612+ diag(`rm -rf $dest 2>/dev/null`);
613+}
614+
615+sub wait_n_cycles {
616+ my ($n) = @_;
617+ PerconaTest::wait_until(
618+ sub {
619+ return 0 unless -f "$dest/after_interval_sleep";
620+ my $n_cycles = `wc -l "$dest/after_interval_sleep" | awk '{print \$1}'`;
621+ $n_cycles ||= '';
622+ chomp($n_cycles);
623+ return ($n_cycles || 0) >= $n;
624+ },
625+ 1.5,
626+ 15
627+ );
628+}
629
630 # ###########################################################################
631 # Test that it won't run if can't connect to MySQL.
632@@ -56,11 +73,14 @@
633 # ###########################################################################
634 # Test that it runs and dies normally.
635 # ###########################################################################
636-diag(`rm $pid_file 2>/dev/null`);
637-diag(`rm $log_file 2>/dev/null`);
638-diag(`rm -rf $dest 2>/dev/null`);
639-
640-$retval = system("$trunk/bin/pt-stalk --daemonize --pid $pid_file --log $log_file --dest $dest -- --defaults-file=$cnf");
641+
642+cleanup();
643+
644+# As of v2.1.9 when --verbose was added, non-matching checks are not
645+# printed by default. So we use the --plugin to tell us when the tool
646+# has completed a cycle.
647+
648+$retval = system("$trunk/bin/pt-stalk --daemonize --pid $pid_file --log $log_file --dest $dest --plugin $trunk/t/pt-stalk/samples/plugin002.sh -- --defaults-file=$cnf");
649
650 is(
651 $retval >> 8,
652@@ -94,21 +114,16 @@
653 "pt-stalk is running"
654 );
655
656-PerconaTest::wait_for_sh("grep -q 'Check results' $log_file >/dev/null");
657+wait_n_cycles(2);
658+PerconaTest::kill_program(pid_file => $pid_file);
659+
660 $output = `cat $log_file 2>/dev/null`;
661-like(
662+unlike(
663 $output,
664 qr/Check results: Threads_running=\d+, matched=no, cycles_true=0/,
665- "Check results logged"
666+ "Non-matching results not logged because --verbose=2"
667 ) or diag(`cat $log_file 2>/dev/null`, `cat $dest/*-output 2>/dev/null`);
668
669-$retval = system("kill $pid 2>/dev/null");
670-is(
671- $retval >> 0,
672- 0,
673- "Killed pt-stalk"
674-);
675-
676 PerconaTest::wait_until(sub { !-f $pid_file });
677
678 ok(
679@@ -123,12 +138,56 @@
680 "Caught signal logged"
681 ) or diag(`cat $log_file 2>/dev/null`, `cat $dest/*-output 2>/dev/null`);
682
683+# #############################################################################
684+# --verbose 3 (non-matching results)
685+# #############################################################################
686+
687+cleanup();
688+
689+$retval = system("$trunk/bin/pt-stalk --daemonize --pid $pid_file --log $log_file --dest $dest --verbose 3 -- --defaults-file=$cnf");
690+
691+PerconaTest::wait_for_files($pid_file, $log_file);
692+PerconaTest::wait_for_sh("grep -q 'Check results' $log_file >/dev/null");
693+PerconaTest::kill_program(pid_file => $pid_file);
694+
695+$output = `cat $log_file 2>/dev/null`;
696+like(
697+ $output,
698+ qr/Check results: Threads_running=\d+, matched=no, cycles_true=0/,
699+ "Matching results logged with --verbose 3"
700+) or diag(`cat $log_file 2>/dev/null`, `cat $dest/*-output 2>/dev/null`);
701+
702+# #############################################################################
703+# --verbose 1 (just errors and warnings)
704+# #############################################################################
705+
706+cleanup();
707+
708+$retval = system("$trunk/bin/pt-stalk --daemonize --pid $pid_file --log $log_file --dest $dest --verbose 1 --plugin $trunk/t/pt-stalk/samples/plugin002.sh -- --defaults-file=$cnf");
709+
710+PerconaTest::wait_for_files($pid_file, $log_file);
711+wait_n_cycles(2);
712+PerconaTest::kill_program(pid_file => $pid_file);
713+
714+$output = `cat $log_file 2>/dev/null`;
715+
716+like(
717+ $output,
718+ qr/Caught signal, exiting/,
719+ "Warning logged (--verbose 1)"
720+);
721+
722+unlike(
723+ $output,
724+ qr/Start|Collect|Check/i,
725+ "No run info log (--verbose 1)"
726+);
727+
728 # ###########################################################################
729 # Test collect.
730 # ###########################################################################
731-diag(`rm $pid_file 2>/dev/null`);
732-diag(`rm $log_file 2>/dev/null`);
733-diag(`rm $dest/* 2>/dev/null`);
734+
735+cleanup();
736
737 # We'll have to watch Uptime since it's the only status var that's going
738 # to be predictable.
739@@ -180,10 +239,10 @@
740 "Trigger file logs how pt-stalk was ran"
741 );
742
743-chomp($output = `cat $log_file 2>/dev/null | grep 'Collector PID'`);
744+chomp($output = `cat $log_file 2>/dev/null | grep 'Collect [0-9] PID'`);
745 like(
746 $output,
747- qr/Collector PID \d+/,
748+ qr/Collect 1 PID \d+/,
749 "Collector PID logged"
750 )
751 or diag(
752@@ -195,9 +254,8 @@
753 # ###########################################################################
754 # Triggered but --no-collect.
755 # ###########################################################################
756-diag(`rm $pid_file 2>/dev/null`);
757-diag(`rm $log_file 2>/dev/null`);
758-diag(`rm $dest/* 2>/dev/null`);
759+
760+cleanup();
761
762 (undef, $uptime) = $dbh->selectrow_array("SHOW STATUS LIKE 'Uptime'");
763 $threshold = $uptime + 2;
764@@ -209,7 +267,7 @@
765 $output = `cat $log_file 2>/dev/null`;
766 like(
767 $output,
768- qr/Collect triggered/,
769+ qr/Collect 1 triggered/,
770 "Collect triggered"
771 );
772
773@@ -227,6 +285,8 @@
774 # --config
775 # #############################################################################
776
777+cleanup();
778+
779 diag(`cp $ENV{HOME}/.pt-stalk.conf $ENV{HOME}/.pt-stalk.conf.original 2>/dev/null`);
780 diag(`cp $trunk/t/pt-stalk/samples/config001.conf $ENV{HOME}/.pt-stalk.conf`);
781
782@@ -254,9 +314,8 @@
783 # #############################################################################
784 # Don't stalk, just collect.
785 # #############################################################################
786-diag(`rm $pid_file 2>/dev/null`);
787-diag(`rm $log_file 2>/dev/null`);
788-diag(`rm $dest/* 2>/dev/null`);
789+
790+cleanup();
791
792 $retval = system("$trunk/bin/pt-stalk --no-stalk --run-time 2 --dest $dest --prefix nostalk --pid $pid_file -- --defaults-file=$cnf >$log_file 2>&1");
793
794@@ -343,9 +402,7 @@
795 # #############################################################################
796 # Done.
797 # #############################################################################
798-diag(`rm $pid_file 2>/dev/null`);
799-diag(`rm $log_file 2>/dev/null`);
800+cleanup();
801 diag(`rm -rf $dest 2>/dev/null`);
802 ok($sb->ok(), "Sandbox servers") or BAIL_OUT(__FILE__ . " broke the sandbox");
803-
804 done_testing;
805
806=== added file 't/pt-stalk/samples/plugin002.sh'
807--- t/pt-stalk/samples/plugin002.sh 1970-01-01 00:00:00 +0000
808+++ t/pt-stalk/samples/plugin002.sh 2013-01-24 19:10:25 +0000
809@@ -0,0 +1,5 @@
810+#!/bin/sh
811+
812+after_interval_sleep() {
813+ date >> "$OPT_DEST/after_interval_sleep"
814+}
815
816=== modified file 'util/test-bash-functions'
817--- util/test-bash-functions 2012-08-22 20:50:11 +0000
818+++ util/test-bash-functions 2013-01-24 19:10:25 +0000
819@@ -100,6 +100,7 @@
820 result() {
821 local result=$1
822 local test_name=${2:-""}
823+ testno=$((testno + 1))
824 if [ $result -eq 0 ]; then
825 echo "ok $testno - $TEST_FILE $test_name"
826 else
827@@ -110,7 +111,6 @@
828 cat $TEST_PT_TMPDIR/failed_result | sed -e 's/^/# /' -e '30q' >&2
829 fi
830 fi
831- testno=$((testno + 1))
832 return $result
833 }
834
835@@ -121,19 +121,21 @@
836 fi
837 }
838
839+done_testing() {
840+ echo "1..$testno"
841+}
842+
843 #
844 # The following subs are for the test files to call.
845 #
846
847 pass() {
848 local reason="${1:-""}"
849-
850 result 0 "$reason"
851 }
852
853 fail() {
854 local reason="${1:-""}"
855-
856 result 1 "$reason"
857 }
858
859@@ -177,6 +179,42 @@
860 result $? "$test_name"
861 }
862
863+file_is_empty() {
864+ local file=$1
865+ local test_name=${2:-""}
866+ test_command="-s $file"
867+ if [ ! -f "$file" ]; then
868+ echo "$file does not exist" > $TEST_PT_TMPDIR/failed_result
869+ result 1 "$test_name"
870+ fi
871+ if [ -s "$file" ]; then
872+ echo "$file is not empty:" > $TEST_PT_TMPDIR/failed_result
873+ cat "$file" >> $TEST_PT_TMPDIR/failed_result
874+ result 1 "$test_name"
875+ else
876+ result 0 "$test_name"
877+ fi
878+}
879+
880+file_contains() {
881+ local file="$1"
882+ local pat="$2"
883+ local test_name=${3:-""}
884+ test_command="grep -q '$pat' '$file'"
885+ if [ ! -f "$file" ]; then
886+ echo "$file does not exist" > $TEST_PT_TMPDIR/failed_result
887+ result 1 "$test_name"
888+ fi
889+ grep -q "$pat" $file
890+ if [ $? -ne 0 ]; then
891+ echo "$file does not contain '$pat':" > $TEST_PT_TMPDIR/failed_result
892+ cat "$file" >> $TEST_PT_TMPDIR/failed_result
893+ result 1 "$test_name"
894+ else
895+ result 0 "$test_name"
896+ fi
897+}
898+
899 cmd_ok() {
900 local test_command=$1
901 local test_name=${2:-""}
902@@ -226,7 +264,7 @@
903 # Script starts here
904 # ############################################################################
905
906-testno=1
907+testno=0
908 failed_tests=0
909
910 if [ $# -eq 0 ]; then

Subscribers

People subscribed via source and target branches