~percona-toolkit-dev/percona-toolkit/pqd-json-with-histograms

« back to all changes in this revision

Viewing changes to t/lib/Daemon.t

Merge fix-pt-archiver-dry-run-bug-1199589 and make 2.2.4 release dates in Changelog and release_notes.pod match.

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
# Test that it daemonizes, creates a PID file, and removes that PID file.
32
32
# ############################################################################
33
33
 
34
 
my $ret_val = system("$cmd 5 --daemonize --pid $pid_file >/dev/null 2>&1");
 
34
my $ret_val = system("$cmd 3 --daemonize --pid $pid_file --log $log_file");
35
35
die 'Cannot test Daemon.pm because t/daemonizes.pl is not working'
36
36
   unless $ret_val == 0;
37
37
 
38
 
PerconaTest::wait_for_files($pid_file);
 
38
PerconaTest::wait_for_files($pid_file, $log_file)
 
39
   or die "$cmd did not create $pid_file and $log_file";
39
40
 
40
 
my $output = `ps wx | grep '$cmd 5' | grep -v grep`;
 
41
my ($pid, $output) = PerconaTest::get_cmd_pid("$cmd 3");
41
42
 
42
43
like(
43
44
   $output,
50
51
   'Creates PID file'
51
52
);
52
53
 
53
 
my ($pid) = $output =~ /^\s*(\d+)\s+/;
54
54
$output = slurp_file($pid_file);
55
55
chomp($output) if $output;
56
56
 
60
60
   'PID file has correct PID'
61
61
);
62
62
 
63
 
# Wait until the process goes away
64
 
PerconaTest::wait_until(sub { !kill(0, $pid) });
65
 
ok(
66
 
   ! -f $pid_file,
67
 
   'Removes PID file upon exit'
68
 
);
 
63
SKIP: {
 
64
   skip "Previous tests failed", 1 unless $pid;
 
65
 
 
66
   # Wait until the process goes away
 
67
   PerconaTest::wait_until(sub { !kill(0, $pid) });
 
68
   ok(
 
69
      ! -f $pid_file,
 
70
      'Removes PID file upon exit'
 
71
   );
 
72
}
69
73
 
70
74
# ############################################################################
71
75
# Check that STDOUT can be redirected
132
136
# ##########################################################################
133
137
rm_tmp_files();
134
138
SKIP: {
135
 
   skip 'No /proc', 2 unless -d '/proc';
136
 
   skip 'No fd in /proc', 2 unless -l "/proc/$PID/0" || -l "/proc/$PID/fd/0";
 
139
   skip 'No /proc', 1 unless -d '/proc';
 
140
   skip 'No fd in /proc', 1 unless -l "/proc/$PID/0" || -l "/proc/$PID/fd/0";
137
141
 
138
 
   system("$cmd 5 --daemonize --pid $pid_file --log $log_file");
139
 
   PerconaTest::wait_for_files($pid_file);
140
 
   chomp($pid = slurp_file($pid_file));
 
142
   system("$cmd 15 --daemonize --pid $pid_file --log $log_file");
 
143
   PerconaTest::wait_for_files($pid_file, $log_file)
 
144
      or die "$cmd did not create $pid_file and $log_file";
 
145
   my $pid = PerconaTest::get_cmd_pid("$cmd 15")
 
146
      or die "Cannot get PID of $cmd 15\n" . `ps xww`;
141
147
   my $proc_fd_0 = -l "/proc/$pid/0"    ? "/proc/$pid/0"
142
148
                 : -l "/proc/$pid/fd/0" ? "/proc/$pid/fd/0"
143
149
                 : die "Cannot find fd 0 symlink in /proc/$pid";
150
156
      'Reopens STDIN to /dev/null'
151
157
   );
152
158
 
153
 
   SKIP: {
154
 
      skip "-t is not reliable", 1;
155
 
      rm_tmp_files();
156
 
      system("echo foo | $cmd 5 --daemonize --pid $pid_file --log $log_file");
157
 
      PerconaTest::wait_for_files($pid_file, $log_file);
158
 
      chomp($pid = slurp_file($pid_file));
159
 
      $proc_fd_0 = -l "/proc/$pid/0"    ? "/proc/$pid/0"
160
 
               : -l "/proc/$pid/fd/0" ? "/proc/$pid/fd/0"
161
 
               : die "Cannot find fd 0 symlink in /proc/$pid";
162
 
      PTDEVDEBUG && PerconaTest::_d('pid_file', $pid_file,
163
 
         'pid', $pid, 'proc_fd_0', $proc_fd_0, `ls -l $proc_fd_0`);
164
 
      $stdin = readlink $proc_fd_0;
165
 
      like(
166
 
         $stdin,
167
 
         qr/pipe/,
168
 
         'Does not reopen STDIN to /dev/null when piped',
169
 
      );
170
 
      rm_tmp_files();
171
 
   }
172
 
};
 
159
   PerconaTest::kill_program(pid => $pid);
 
160
 
 
161
#   SKIP: {
 
162
#      skip "-t is not reliable", 1;
 
163
#      rm_tmp_files();
 
164
#      system("echo foo | $cmd 5 --daemonize --pid $pid_file --log $log_file");
 
165
#      PerconaTest::wait_for_files($pid_file, $log_file);
 
166
#      chomp($pid = slurp_file($pid_file));
 
167
#      $proc_fd_0 = -l "/proc/$pid/0"    ? "/proc/$pid/0"
 
168
#               : -l "/proc/$pid/fd/0" ? "/proc/$pid/fd/0"
 
169
#               : die "Cannot find fd 0 symlink in /proc/$pid";
 
170
#      PTDEVDEBUG && PerconaTest::_d('pid_file', $pid_file,
 
171
#         'pid', $pid, 'proc_fd_0', $proc_fd_0, `ls -l $proc_fd_0`);
 
172
#      $stdin = readlink $proc_fd_0;
 
173
#      like(
 
174
#         $stdin,
 
175
#         qr/pipe/,
 
176
#         'Does not reopen STDIN to /dev/null when piped',
 
177
#      );
 
178
#      rm_tmp_files();
 
179
#   }
 
180
}
173
181
 
174
182
# ##########################################################################
175
183
# Issue 419: Daemon should check wether process with pid obtained from
176
184
# pid-file is still running
177
185
# ##########################################################################
178
186
rm_tmp_files();
179
 
system("$cmd 5 --daemonize --pid $pid_file >/dev/null 2>&1");
 
187
system("$cmd 7 --daemonize --pid $pid_file >/dev/null 2>&1");
180
188
PerconaTest::wait_for_files($pid_file);
181
 
chomp($pid = slurp_file($pid_file));
 
189
$pid = PerconaTest::get_cmd_pid("$cmd 7")
 
190
   or die "Cannot get PID of $cmd 7";
182
191
kill 9, $pid;
183
192
sleep 0.25;
184
 
$output = `ps wx | grep '^[ ]*$pid' | grep -v grep`;
 
193
(undef, $output) = PerconaTest::get_cmd_pid("$cmd 7");
185
194
unlike(
186
195
   $output,
187
196
   qr/daemonize/,
194
203
 
195
204
my (undef, $tempfile) = tempfile();
196
205
 
197
 
system("$cmd 5 --daemonize --log $log_file --pid $pid_file > $tempfile 2>&1");
 
206
system("$cmd 6 --daemonize --log $log_file --pid $pid_file > $tempfile 2>&1");
198
207
PerconaTest::wait_for_files($log_file, $pid_file, $tempfile);
199
 
 
200
 
$output = `ps wx | grep '$cmd 5' | grep -v grep`;
201
 
chomp(my $new_pid = slurp_file($pid_file));
 
208
my $new_pid;
 
209
($new_pid, $output) = PerconaTest::get_cmd_pid("$cmd 6");
202
210
 
203
211
like(
204
212
   $output,
212
220
   'Says that old PID is not running (issue 419)'
213
221
);
214
222
 
215
 
ok(
216
 
   $pid != $new_pid,
 
223
cmp_ok(
 
224
   $pid,
 
225
  '!=',
 
226
   $new_pid,
217
227
   'Overwrites PID file with new PID (issue 419)'
218
228
);
219
229
 
220
 
PerconaTest::wait_until(sub { !-e $pid_file });
 
230
PerconaTest::wait_until(sub { !-f $pid_file });
221
231
ok(
222
232
   !-f $pid_file,
223
233
   'Re-used PID file still removed (issue 419)'
229
239
# Check that it actually checks the running process.
230
240
# ############################################################################
231
241
rm_tmp_files();
232
 
system("$cmd 20 --daemonize --log $log_file --pid $pid_file");
 
242
system("$cmd 10 --daemonize --log $log_file --pid $pid_file");
233
243
PerconaTest::wait_for_files($pid_file, $log_file);
234
244
chomp($pid = slurp_file($pid_file));
235
 
$output = `$cmd 0 --daemonize --pid $pid_file 2>&1`;
 
245
$output = `$cmd 1 --daemonize --pid $pid_file 2>&1`;
236
246
like(
237
247
   $output,
238
248
   qr/PID file $pid_file exists and PID $pid is running/,
239
249
   'Says that PID is running (issue 419)'
240
250
);
241
251
 
242
 
if ( $pid ) {
243
 
   kill 9, $pid;
244
 
}
245
 
 
246
 
sleep 0.25;
 
252
PerconaTest::kill_program(pid => $pid);
247
253
rm_tmp_files();
248
254
 
249
255
# #############################################################################