~percona-toolkit-dev/percona-toolkit/pt-stalk-sleep-collect-option

« back to all changes in this revision

Viewing changes to t/lib/Daemon.t

  • Committer: Daniel Nichter
  • Date: 2013-06-19 21:23:55 UTC
  • mfrom: (582.1.5 release-2.2.3)
  • Revision ID: daniel@percona.com-20130619212355-nf6bmx23j3b76afe
Tags: 2.2.3
Merge release-2.2.3.

Show diffs side-by-side

added added

removed removed

Lines of Context:
9
9
use strict;
10
10
use warnings FATAL => 'all';
11
11
use English qw(-no_match_vars);
 
12
 
12
13
use Test::More;
13
14
use Time::HiRes qw(sleep);
14
 
use File::Temp qw( tempfile );
 
15
use File::Temp qw(tempfile);
 
16
 
15
17
use Daemon;
16
 
use OptionParser;
17
18
use PerconaTest;
18
 
#plan skip_all => "Hm";
 
19
 
19
20
use constant PTDEVDEBUG => $ENV{PTDEVDEBUG} || 0;
20
21
 
21
 
my $o = new OptionParser(file => "$trunk/t/lib/samples/daemonizes.pl");
22
 
my $d = new Daemon(o=>$o);
23
 
 
24
 
my $pid_file = '/tmp/daemonizes.pl.pid';
25
 
my $log_file = '/tmp/daemonizes.output'; 
 
22
my $cmd      = "$trunk/t/lib/samples/daemonizes.pl";
 
23
my $pid_file = "/tmp/pt-daemon-test.pid.$PID";
 
24
my $log_file = "/tmp/pt-daemon-test.log.$PID";
26
25
sub rm_tmp_files() {
27
 
   -e $pid_file && (unlink $pid_file || die "Error removing $pid_file");
28
 
   -e $log_file && (unlink $log_file || die "Error removing $log_file");
 
26
   -f $pid_file && (unlink $pid_file || die "Error removing $pid_file");
 
27
   -f $log_file && (unlink $log_file || die "Error removing $log_file");
29
28
}
30
29
 
31
30
# ############################################################################
32
31
# Test that it daemonizes, creates a PID file, and removes that PID file.
33
32
# ############################################################################
34
 
rm_tmp_files();
35
33
 
36
 
my $cmd     = "$trunk/t/lib/samples/daemonizes.pl";
37
34
my $ret_val = system("$cmd 5 --daemonize --pid $pid_file >/dev/null 2>&1");
38
35
die 'Cannot test Daemon.pm because t/daemonizes.pl is not working'
39
36
   unless $ret_val == 0;
41
38
PerconaTest::wait_for_files($pid_file);
42
39
 
43
40
my $output = `ps wx | grep '$cmd 5' | grep -v grep`;
44
 
like($output, qr/$cmd/, 'Daemonizes');
45
 
ok(-f $pid_file, 'Creates PID file');
46
 
 
47
 
my ($pid) = $output =~ /\s*(\d+)\s+/;
 
41
 
 
42
like(
 
43
   $output,
 
44
   qr/$cmd/,
 
45
   'Daemonizes'
 
46
);
 
47
 
 
48
ok(
 
49
   -f $pid_file,
 
50
   'Creates PID file'
 
51
);
 
52
 
 
53
my ($pid) = $output =~ /^\s*(\d+)\s+/;
48
54
$output = slurp_file($pid_file);
49
 
is($output, $pid, 'PID file has correct PID');
 
55
chomp($output) if $output;
 
56
 
 
57
is(
 
58
   $output,
 
59
   $pid,
 
60
   'PID file has correct PID'
 
61
);
50
62
 
51
63
# Wait until the process goes away
52
64
PerconaTest::wait_until(sub { !kill(0, $pid) });
53
 
ok(! -f $pid_file, 'Removes PID file upon exit');
 
65
ok(
 
66
   ! -f $pid_file,
 
67
   'Removes PID file upon exit'
 
68
);
54
69
 
55
70
# ############################################################################
56
71
# Check that STDOUT can be redirected
59
74
 
60
75
system("$cmd 0 --daemonize --log $log_file");
61
76
PerconaTest::wait_for_files($log_file);
62
 
ok(-f $log_file, 'Log file exists');
 
77
 
 
78
ok(
 
79
   -f $log_file,
 
80
   'Log file exists'
 
81
);
63
82
 
64
83
$output = slurp_file($log_file);
65
 
like($output, qr/STDOUT\nSTDERR\n/, 'STDOUT and STDERR went to log file');
 
84
 
 
85
like(
 
86
   $output,
 
87
   qr/STDOUT\nSTDERR\n/,
 
88
   'STDOUT and STDERR went to log file'
 
89
);
66
90
 
67
91
my $log_size = -s $log_file;
68
92
PTDEVDEBUG && PerconaTest::_d('log size', $log_size);
71
95
system("$cmd 0 --daemonize --log $log_file");
72
96
PerconaTest::wait_until(sub { -s $log_file > $log_size });
73
97
$output = slurp_file($log_file);
 
98
 
74
99
like(
75
100
   $output,
76
101
   qr/STDOUT\nSTDERR\nSTDOUT\nSTDERR\n/,
82
107
# ##########################################################################
83
108
rm_tmp_files();
84
109
diag(`touch $pid_file`);
 
110
 
85
111
ok(
86
112
   -f  $pid_file,
87
113
   'PID file already exists'
90
116
$output = `$cmd 2 --daemonize --pid $pid_file 2>&1`;
91
117
like(
92
118
   $output,
93
 
   qr{The PID file $pid_file already exists},
 
119
   qr{PID file $pid_file exists},
94
120
   'Dies if PID file already exists'
95
121
);
96
122
 
182
208
 
183
209
like(
184
210
   slurp_file($tempfile),
185
 
   qr/$pid, is not running/,
 
211
   qr/Overwriting PID file $pid_file because PID $pid is not running/,
186
212
   'Says that old PID is not running (issue 419)'
187
213
);
188
214
 
209
235
$output = `$cmd 0 --daemonize --pid $pid_file 2>&1`;
210
236
like(
211
237
   $output,
212
 
   qr/$pid, is running/,
 
238
   qr/PID file $pid_file exists and PID $pid is running/,
213
239
   'Says that PID is running (issue 419)'
214
240
);
215
241
 
216
 
kill SIGKILL => $pid
217
 
   if $pid;
 
242
if ( $pid ) {
 
243
   kill 9, $pid;
 
244
}
218
245
 
219
 
sleep 1;
 
246
sleep 0.25;
220
247
rm_tmp_files();
221
248
 
222
249
# #############################################################################
223
250
# Test auto-PID file removal without having to daemonize (for issue 391).
224
251
# #############################################################################
 
252
my $pid_file2 = "/tmp/pt-daemon-test.pid2.$PID";
225
253
{
226
 
   @ARGV = qw(--pid /tmp/d2.pid);
227
 
   $o->get_specs("$trunk/t/lib/samples/daemonizes.pl");
228
 
   $o->get_opts();
229
 
   my $d2 = new Daemon(o=>$o);
230
 
   $d2->make_PID_file();
 
254
   my $d2 = Daemon->new(
 
255
      pid_file => $pid_file2,
 
256
   );
 
257
   $d2->run();
231
258
   ok(
232
 
      -f '/tmp/d2.pid',
 
259
      -f $pid_file2,
233
260
      'PID file for non-daemon exists'
234
261
   );
235
262
}
236
263
# Since $d2 was locally scoped, it should have been destoryed by now.
237
264
# This should have caused the PID file to be automatically removed.
238
265
ok(
239
 
   !-f '/tmpo/d2.pid',
 
266
   !-f $pid_file2,
240
267
   'PID file auto-removed for non-daemon'
241
268
);
242
269
 
243
270
# We should still die if the PID file already exists,
244
271
# even if we're not a daemon.
245
272
{
246
 
   `touch /tmp/d2.pid`;
247
 
   @ARGV = qw(--pid /tmp/d2.pid);
248
 
   $o->get_opts();
 
273
   diag(`touch $pid_file2`);
249
274
   eval {
250
 
      my $d2 = new Daemon(o=>$o);  # should die here actually
251
 
      $d2->make_PID_file();
 
275
      my $d2 = Daemon->new(
 
276
         pid_file => $pid_file2,
 
277
      );
 
278
      $d2->run();
252
279
   };
253
280
   like(
254
281
      $EVAL_ERROR,
255
 
      qr{PID file /tmp/d2.pid already exists},
 
282
      qr/PID file $pid_file2 exists/,
256
283
      'Dies if PID file already exists for non-daemon'
257
284
   );
258
285
 
259
 
   diag(`rm -rf /tmp/d2.pid >/dev/null`);
 
286
   unlink $pid_file2 if -f $pid_file2;
260
287
}
261
288
 
262
289
# #############################################################################