File: execute_command.t

package info (click to toggle)
percona-toolkit 3.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 68,916 kB
  • sloc: perl: 241,287; sql: 22,868; sh: 19,746; javascript: 6,799; makefile: 353; awk: 38; python: 30; sed: 1
file content (123 lines) | stat: -rw-r--r-- 3,399 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env perl

BEGIN {
   die "The PERCONA_TOOLKIT_BRANCH environment variable is not set.\n"
      unless $ENV{PERCONA_TOOLKIT_BRANCH} && -d $ENV{PERCONA_TOOLKIT_BRANCH};
   unshift @INC, "$ENV{PERCONA_TOOLKIT_BRANCH}/lib";
};

use strict;
use warnings FATAL => 'all';
use English qw(-no_match_vars);
use Test::More tests => 9;

use PerconaTest;
use Sandbox;
require "$trunk/bin/pt-kill";

my $dp = new DSNParser(opts=>$dsn_opts);
my $sb = new Sandbox(basedir => '/tmp', DSNParser => $dp);
my $master_dbh = $sb->get_dbh_for('master');

my $output;
my $cnf ='/tmp/12345/my.sandbox.cnf';
my $cmd = "$trunk/bin/pt-kill -F $cnf -h 127.1";
my $out = "/tmp/mk-kill-test.txt";

# #############################################################################
# Test --execute-command action.
# #############################################################################
diag(`rm $out 2>/dev/null`);

$output = `$cmd --test-matching $trunk/t/lib/samples/pl/recset001.txt --match-command Query --execute-command 'echo hello > $out'`;
is(
   $output, 
   '',
   'No output without --print'
);

chomp($output = `cat $out 2>/dev/null`),
is(
   $output,
   'hello',
   '--execute-command'
);

diag(`rm $out 2>/dev/null`);

SKIP: {
   skip 'Cannot connect to sandbox master', 2 unless $master_dbh;
   $master_dbh->do("CREATE DATABASE IF NOT EXISTS pt_kill_zombie_test");

   system "/tmp/12345/use -e 'select sleep(2)' >/dev/null 2>&1 &";

   $output = `$cmd --match-info 'select sleep' --run-time 2 --interval 1 --print --execute-command 'echo batty > $out'`;

   like(
      $output,
      qr/KILL .+ select sleep\(2\)/,
      '--print with --execute-command'
   );

   chomp($output = `cat $out 2>/dev/null`);
   is(
      $output,
      'batty',
      '--execute-command (online)'
   );

   # Let our select sleep(2) go away before other tests are ran.
   sleep 1;
   diag(`rm $out 2>/dev/null`);

   # Don't make zombies (https://bugs.launchpad.net/percona-toolkit/+bug/919819)
   $master_dbh->do("USE pt_kill_zombie_test");

   my $sentinel = "/tmp/pt-kill-test.$PID.stop";
   my $pid_file = "/tmp/pt-kill-test.$PID.pid";
   my $log_file = "/tmp/pt-kill-test.$PID.log";
   diag(`rm $sentinel 2>/dev/null`);
   diag(`rm $pid_file 2>/dev/null`);
   diag(`rm $log_file 2>/dev/null`);

   `$cmd --daemonize --match-db pt_kill_zombie_test --interval 1 --print --execute-command 'echo zombie > $out' --verbose --pid $pid_file --log $log_file --sentinel $sentinel`;
   PerconaTest::wait_for_files($pid_file, $log_file, $out);
   $output = `grep Executed $log_file`;
   like(
      $output,
      qr/Executed echo zombie/,
      "Executed zombie command"
   );

   $output = `ps x | grep Z | grep -v grep`;
   is(
      $output,
      "",
      "No zombies"
   );

   diag(`touch $sentinel`);
   sleep 1;
   ok(
      !-f $pid_file,
      "pt-kill stopped"
   );
   $output = `ps x | grep Z | grep -v grep`;
   is(
      $output,
      "",
      "No zombies"
   );

   diag(`rm $sentinel 2>/dev/null`);
   diag(`rm $pid_file 2>/dev/null`);
   diag(`rm $log_file 2>/dev/null`);
}

# #############################################################################
# Done.
# #############################################################################
diag(`rm $out 2>/dev/null`);
$sb->wipe_clean($master_dbh) if $master_dbh;
ok($sb->ok(), "Sandbox servers") or BAIL_OUT(__FILE__ . " broke the sandbox");
exit;