comparison lib/Test/Nginx.pm @ 1790:ba625d5a02e4

Tests: updated try_run() to rely on nginx "-e". The "-e" command line option introduced in nginx 1.19.5 is now used to print error line on startup failures with TEST_NGINX_VERBOSE set. This change replaces a previous approach (a1874249496d) compatible with pre-1.19.5 nginx versions that used to redirect stderr to file. Hence, "-e" compatibility is removed. As a side effect, this fixes temporary directory removal on win32 left on startup failures because the "stderr" file was kept open.
author Sergey Kandaurov <pluknet@nginx.com>
date Mon, 19 Sep 2022 13:26:32 +0400
parents ba6e24e38f03
children cdcd75657e52
comparison
equal deleted inserted replaced
1789:c67b390cb9ef 1790:ba625d5a02e4
46 'nginx-test-XXXXXXXXXX', 46 'nginx-test-XXXXXXXXXX',
47 TMPDIR => 1 47 TMPDIR => 1
48 ) 48 )
49 or die "Can't create temp directory: $!\n"; 49 or die "Can't create temp directory: $!\n";
50 $self->{_testdir} =~ s!\\!/!g if $^O eq 'MSWin32'; 50 $self->{_testdir} =~ s!\\!/!g if $^O eq 'MSWin32';
51 mkdir "$self->{_testdir}/logs"
52 or die "Can't create logs directory: $!\n";
53 51
54 Test::More::BAIL_OUT("no $NGINX binary found") 52 Test::More::BAIL_OUT("no $NGINX binary found")
55 unless -x $NGINX; 53 unless -x $NGINX;
56 54
57 return $self; 55 return $self;
289 287
290 sub try_run($$) { 288 sub try_run($$) {
291 my ($self, $message) = @_; 289 my ($self, $message) = @_;
292 290
293 eval { 291 eval {
294 open OLDERR, ">&", \*STDERR; 292 open OLDERR, ">&", \*STDERR; close STDERR;
295 open NEWERR, ">", $self->{_testdir} . '/stderr'
296 or die "Can't open stderr: $!";
297 close STDERR;
298 open STDERR, ">&", \*NEWERR;
299 close NEWERR;
300
301 $self->run(); 293 $self->run();
302
303 close STDERR;
304 open STDERR, ">&", \*OLDERR; 294 open STDERR, ">&", \*OLDERR;
305 }; 295 };
306 296
307 return $self unless $@; 297 return $self unless $@;
308 298
309 if ($ENV{TEST_NGINX_VERBOSE}) { 299 if ($ENV{TEST_NGINX_VERBOSE}) {
310 open F, '<', $self->{_testdir} . '/stderr' 300 open F, '<', $self->{_testdir} . '/error.log'
311 or die "Can't open stderr: $!"; 301 or die "Can't open error.log: $!";
312 log_core($_) while (<F>); 302 log_core($_) while (<F>);
313 close F; 303 close F;
314 } 304 }
315 305
316 Test::More::plan(skip_all => $message); 306 Test::More::plan(skip_all => $message);
348 338
349 if ($pid == 0) { 339 if ($pid == 0) {
350 my @globals = $self->{_test_globals} ? 340 my @globals = $self->{_test_globals} ?
351 () : ('-g', "pid $testdir/nginx.pid; " 341 () : ('-g', "pid $testdir/nginx.pid; "
352 . "error_log $testdir/error.log debug;"); 342 . "error_log $testdir/error.log debug;");
353 my @error = $self->has_version('1.19.5') ?
354 ('-e', 'error.log') : ();
355 exec($NGINX, '-p', "$testdir/", '-c', 'nginx.conf', 343 exec($NGINX, '-p', "$testdir/", '-c', 'nginx.conf',
356 @error, @globals) 344 '-e', 'error.log', @globals)
357 or die "Unable to exec(): $!\n"; 345 or die "Unable to exec(): $!\n";
358 } 346 }
359 347
360 # wait for nginx to start 348 # wait for nginx to start
361 349
423 my $testdir = $self->{_testdir}; 411 my $testdir = $self->{_testdir};
424 412
425 my @globals = $self->{_test_globals} ? 413 my @globals = $self->{_test_globals} ?
426 () : ('-g', "pid $testdir/nginx.pid; " 414 () : ('-g', "pid $testdir/nginx.pid; "
427 . "error_log $testdir/error.log debug;"); 415 . "error_log $testdir/error.log debug;");
428 my @error = $self->has_version('1.19.5') ?
429 ('-e', 'error.log') : ();
430 my $command = "$NGINX -T -p $testdir/ -c nginx.conf " 416 my $command = "$NGINX -T -p $testdir/ -c nginx.conf "
431 . join(' ', @error, @globals); 417 . "-e error.log " . join(' ', @globals);
432 418
433 return qx/$command 2>&1/; 419 return qx/$command 2>&1/;
434 } 420 }
435 421
436 sub waitforfile($;$) { 422 sub waitforfile($;$) {
479 if ($^O eq 'MSWin32') { 465 if ($^O eq 'MSWin32') {
480 my $testdir = $self->{_testdir}; 466 my $testdir = $self->{_testdir};
481 my @globals = $self->{_test_globals} ? 467 my @globals = $self->{_test_globals} ?
482 () : ('-g', "pid $testdir/nginx.pid; " 468 () : ('-g', "pid $testdir/nginx.pid; "
483 . "error_log $testdir/error.log debug;"); 469 . "error_log $testdir/error.log debug;");
484 my @error = $self->has_version('1.19.5') ?
485 ('-e', 'error.log') : ();
486 system($NGINX, '-p', $testdir, '-c', "nginx.conf", 470 system($NGINX, '-p', $testdir, '-c', "nginx.conf",
487 '-s', 'reload', @error, @globals) == 0 471 '-s', 'reload', '-e', 'error.log', @globals) == 0
488 or die "system() failed: $?\n"; 472 or die "system() failed: $?\n";
489 473
490 } else { 474 } else {
491 kill 'HUP', $pid; 475 kill 'HUP', $pid;
492 } 476 }
504 if ($^O eq 'MSWin32') { 488 if ($^O eq 'MSWin32') {
505 my $testdir = $self->{_testdir}; 489 my $testdir = $self->{_testdir};
506 my @globals = $self->{_test_globals} ? 490 my @globals = $self->{_test_globals} ?
507 () : ('-g', "pid $testdir/nginx.pid; " 491 () : ('-g', "pid $testdir/nginx.pid; "
508 . "error_log $testdir/error.log debug;"); 492 . "error_log $testdir/error.log debug;");
509 my @error = $self->has_version('1.19.5') ?
510 ('-e', 'error.log') : ();
511 system($NGINX, '-p', $testdir, '-c', "nginx.conf", 493 system($NGINX, '-p', $testdir, '-c', "nginx.conf",
512 '-s', 'quit', @error, @globals) == 0 494 '-s', 'quit', '-e', 'error.log', @globals) == 0
513 or die "system() failed: $?\n"; 495 or die "system() failed: $?\n";
514 496
515 } else { 497 } else {
516 kill 'QUIT', $pid; 498 kill 'QUIT', $pid;
517 } 499 }
528 if ($^O eq 'MSWin32') { 510 if ($^O eq 'MSWin32') {
529 my $testdir = $self->{_testdir}; 511 my $testdir = $self->{_testdir};
530 my @globals = $self->{_test_globals} ? 512 my @globals = $self->{_test_globals} ?
531 () : ('-g', "pid $testdir/nginx.pid; " 513 () : ('-g', "pid $testdir/nginx.pid; "
532 . "error_log $testdir/error.log debug;"); 514 . "error_log $testdir/error.log debug;");
533 my @error = $self->has_version('1.19.5') ?
534 ('-e', 'error.log') : ();
535 system($NGINX, '-p', $testdir, '-c', "nginx.conf", 515 system($NGINX, '-p', $testdir, '-c', "nginx.conf",
536 '-s', 'stop', @error, @globals) == 0 516 '-s', 'stop', '-e', 'error.log', @globals) == 0
537 or die "system() failed: $?\n"; 517 or die "system() failed: $?\n";
538 518
539 } else { 519 } else {
540 kill 'TERM', $pid; 520 kill 'TERM', $pid;
541 } 521 }