From f43c28ea10ca8d520b55f2fbb20710dd66fc4fb5 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Thu, 7 Sep 2023 22:55:09 +0000 Subject: tests: port some bad config tests to Perl 5 We can fold some tests into one test to save on Perl startup time (but Ruby startup time is a lost cause). --- t/lib.perl | 12 +++++---- t/reload-bad-config.t | 58 ++++++++++++++++++++++++++++++++++++++++++++ t/t0001-reload-bad-config.sh | 53 ---------------------------------------- t/t0002-config-conflict.sh | 49 ------------------------------------- 4 files changed, 65 insertions(+), 107 deletions(-) create mode 100644 t/reload-bad-config.t delete mode 100755 t/t0001-reload-bad-config.sh delete mode 100755 t/t0002-config-conflict.sh diff --git a/t/lib.perl b/t/lib.perl index fe3404b..7de9e42 100644 --- a/t/lib.perl +++ b/t/lib.perl @@ -9,17 +9,19 @@ use Test::More; use IO::Socket::INET; use POSIX qw(dup2 _exit setpgid :signal_h SEEK_SET F_SETFD); use File::Temp 0.19 (); # 0.19 for ->newdir -our ($tmpdir, $errfh); -our @EXPORT = qw(unicorn slurp tcp_server tcp_start unicorn $tmpdir $errfh +our ($tmpdir, $errfh, $err_log); +our @EXPORT = qw(unicorn slurp tcp_server tcp_start unicorn + $tmpdir $errfh $err_log SEEK_SET tcp_host_port which spawn check_stderr unix_start slurp_hdr); my ($base) = ($0 =~ m!\b([^/]+)\.[^\.]+\z!); $tmpdir = File::Temp->newdir("unicorn-$base-XXXX", TMPDIR => 1); -open($errfh, '>>', "$tmpdir/err.log"); -END { diag slurp("$tmpdir/err.log") if $tmpdir }; +$err_log = "$tmpdir/err.log"; +open($errfh, '>>', $err_log); +END { diag slurp($err_log) if $tmpdir }; sub check_stderr () { - my @log = slurp("$tmpdir/err.log"); + my @log = slurp($err_log); diag("@log") if $ENV{V}; my @err = grep(!/NameError.*Unicorn::Waiter/, grep(/error/i, @log)); @err = grep(!/failed to set accept_filter=/, @err); diff --git a/t/reload-bad-config.t b/t/reload-bad-config.t new file mode 100644 index 0000000..c7055c7 --- /dev/null +++ b/t/reload-bad-config.t @@ -0,0 +1,58 @@ +#!perl -w +# Copyright (C) unicorn hackers +# License: GPL-3.0+ +use v5.14; BEGIN { require './t/lib.perl' }; +use autodie; +my $srv = tcp_server(); +my $host_port = tcp_host_port($srv); +my $ru = "$tmpdir/config.ru"; +my $u_conf = "$tmpdir/u.conf.rb"; + +open my $fh, '>', $ru; +print $fh <<'EOM'; +use Rack::ContentLength +use Rack::ContentType, 'text/plain' +config = ru = "hello world\n" # check for config variable conflicts, too +run lambda { |env| [ 200, {}, [ ru.to_s ] ] } +EOM +close $fh; + +open $fh, '>', $u_conf; +print $fh < $srv }); +my $c = tcp_start($srv, 'GET / HTTP/1.0'); +my ($status, $hdr) = slurp_hdr($c); +my $bdy = do { local $/; <$c> }; +like($status, qr!\AHTTP/1\.[01] 200\b!, 'status line valid at start'); +is($bdy, "hello world\n", 'body matches expected'); + +open $fh, '>>', $ru; +say $fh '....this better be a syntax error in any version of ruby...'; +close $fh; + +$ar->do_kill('HUP'); # reload +my @l; +for (1..1000) { + @l = grep(/(?:done|error) reloading/, slurp($err_log)) and + last; + select undef, undef, undef, 0.011; +} +diag slurp($err_log) if $ENV{V}; +ok(grep(/error reloading/, @l), 'got error reloading'); +open $fh, '>', $err_log; +close $fh; + +$c = tcp_start($srv, 'GET / HTTP/1.0'); +($status, $hdr) = slurp_hdr($c); +$bdy = do { local $/; <$c> }; +like($status, qr!\AHTTP/1\.[01] 200\b!, 'status line valid afte reload'); +is($bdy, "hello world\n", 'body matches expected after reload'); + +check_stderr; +undef $tmpdir; # quiet t/lib.perl END{} +done_testing; diff --git a/t/t0001-reload-bad-config.sh b/t/t0001-reload-bad-config.sh deleted file mode 100755 index 55bb355..0000000 --- a/t/t0001-reload-bad-config.sh +++ /dev/null @@ -1,53 +0,0 @@ -#!/bin/sh -. ./test-lib.sh -t_plan 7 "reload config.ru error with preload_app true" - -t_begin "setup and start" && { - unicorn_setup - rtmpfiles ru - - cat > $ru <<\EOF -use Rack::ContentLength -use Rack::ContentType, "text/plain" -x = { "hello" => "world" } -run lambda { |env| [ 200, {}, [ x.inspect << "\n" ] ] } -EOF - echo 'preload_app true' >> $unicorn_config - unicorn -D -c $unicorn_config $ru - unicorn_wait_start -} - -t_begin "hit with curl" && { - out=$(curl -sSf http://$listen/) - test x"$out" = x'{"hello"=>"world"}' -} - -t_begin "introduce syntax error in rackup file" && { - echo '...' >> $ru -} - -t_begin "reload signal succeeds" && { - kill -HUP $unicorn_pid - while ! egrep '(done|error) reloading' $r_err >/dev/null - do - sleep 1 - done - - grep 'error reloading' $r_err >/dev/null - > $r_err -} - -t_begin "hit with curl" && { - out=$(curl -sSf http://$listen/) - test x"$out" = x'{"hello"=>"world"}' -} - -t_begin "killing succeeds" && { - kill $unicorn_pid -} - -t_begin "check stderr" && { - check_stderr -} - -t_done diff --git a/t/t0002-config-conflict.sh b/t/t0002-config-conflict.sh deleted file mode 100755 index d7b2181..0000000 --- a/t/t0002-config-conflict.sh +++ /dev/null @@ -1,49 +0,0 @@ -#!/bin/sh -. ./test-lib.sh -t_plan 6 "config variables conflict with preload_app" - -t_begin "setup and start" && { - unicorn_setup - rtmpfiles ru rutmp - - cat > $ru <<\EOF -use Rack::ContentLength -use Rack::ContentType, "text/plain" -config = ru = { "hello" => "world" } -run lambda { |env| [ 200, {}, [ ru.inspect << "\n" ] ] } -EOF - echo 'preload_app true' >> $unicorn_config - unicorn -D -c $unicorn_config $ru - unicorn_wait_start -} - -t_begin "hit with curl" && { - out=$(curl -sSf http://$listen/) - test x"$out" = x'{"hello"=>"world"}' -} - -t_begin "modify rackup file" && { - sed -e 's/world/WORLD/' < $ru > $rutmp - mv $rutmp $ru -} - -t_begin "reload signal succeeds" && { - kill -HUP $unicorn_pid - while ! egrep '(done|error) reloading' < $r_err >/dev/null - do - sleep 1 - done - - grep 'done reloading' $r_err >/dev/null -} - -t_begin "hit with curl" && { - out=$(curl -sSf http://$listen/) - test x"$out" = x'{"hello"=>"WORLD"}' -} - -t_begin "killing succeeds" && { - kill $unicorn_pid -} - -t_done -- cgit v1.2.3-24-ge0c7