diff options
Diffstat (limited to 'test/exec/test_exec.rb')
-rw-r--r-- | test/exec/test_exec.rb | 127 |
1 files changed, 29 insertions, 98 deletions
diff --git a/test/exec/test_exec.rb b/test/exec/test_exec.rb index 8a6b43e..807f724 100644 --- a/test/exec/test_exec.rb +++ b/test/exec/test_exec.rb @@ -1,6 +1,6 @@ # -*- encoding: binary -*- - -# Copyright (c) 2009 Eric Wong +# frozen_string_literal: false +# Don't add to this file, new tests are in Perl 5. See t/README FLOCK_PATH = File.expand_path(__FILE__) require './test/test_helper' @@ -25,28 +25,29 @@ class ExecTest < Test::Unit::TestCase HI = <<-EOS use Rack::ContentLength -run proc { |env| [ 200, { 'Content-Type' => 'text/plain' }, [ "HI\\n" ] ] } +run proc { |env| [ 200, { 'content-type' => 'text/plain' }, [ "HI\\n" ] ] } EOS SHOW_RACK_ENV = <<-EOS use Rack::ContentLength run proc { |env| - [ 200, { 'Content-Type' => 'text/plain' }, [ ENV['RACK_ENV'] ] ] + [ 200, { 'content-type' => 'text/plain' }, [ ENV['RACK_ENV'] ] ] } EOS HELLO = <<-EOS class Hello def call(env) - [ 200, { 'Content-Type' => 'text/plain' }, [ "HI\\n" ] ] + [ 200, { 'content-type' => 'text/plain' }, [ "HI\\n" ] ] end end EOS COMMON_TMP = Tempfile.new('unicorn_tmp') unless defined?(COMMON_TMP) + HEAVY_WORKERS = 2 HEAVY_CFG = <<-EOS -worker_processes 4 +worker_processes #{HEAVY_WORKERS} timeout 30 logger Logger.new('#{COMMON_TMP.path}') before_fork do |server, worker| @@ -61,9 +62,9 @@ run lambda { |env| a = ::File.stat(pwd) b = ::File.stat(Dir.pwd) if (a.ino == b.ino && a.dev == b.dev) - [ 200, { 'Content-Type' => 'text/plain' }, [ pwd ] ] + [ 200, { 'content-type' => 'text/plain' }, [ pwd ] ] else - [ 404, { 'Content-Type' => 'text/plain' }, [] ] + [ 404, { 'content-type' => 'text/plain' }, [] ] end } EOS @@ -96,59 +97,6 @@ run lambda { |env| end end - def test_sd_listen_fds_emulation - # [ruby-core:69895] [Bug #11336] fixed by r51576 - return if RUBY_VERSION.to_f < 2.3 - - File.open("config.ru", "wb") { |fp| fp.write(HI) } - sock = TCPServer.new(@addr, @port) - - [ %W(-l #@addr:#@port), nil ].each do |l| - sock.setsockopt(:SOL_SOCKET, :SO_KEEPALIVE, 0) - - pid = xfork do - redirect_test_io do - # pretend to be systemd - ENV['LISTEN_PID'] = "#$$" - ENV['LISTEN_FDS'] = '1' - - # 3 = SD_LISTEN_FDS_START - args = [ $unicorn_bin ] - args.concat(l) if l - args << { 3 => sock } - exec(*args) - end - end - res = hit(["http://#@addr:#@port/"]) - assert_equal [ "HI\n" ], res - assert_shutdown(pid) - assert sock.getsockopt(:SOL_SOCKET, :SO_KEEPALIVE).bool, - 'unicorn should always set SO_KEEPALIVE on inherited sockets' - end - ensure - sock.close if sock - end - - def test_inherit_listener_unspecified - File.open("config.ru", "wb") { |fp| fp.write(HI) } - sock = TCPServer.new(@addr, @port) - sock.setsockopt(:SOL_SOCKET, :SO_KEEPALIVE, 0) - - pid = xfork do - redirect_test_io do - ENV['UNICORN_FD'] = sock.fileno.to_s - exec($unicorn_bin, sock.fileno => sock.fileno) - end - end - res = hit(["http://#@addr:#@port/"]) - assert_equal [ "HI\n" ], res - assert_shutdown(pid) - assert sock.getsockopt(:SOL_SOCKET, :SO_KEEPALIVE).bool, - 'unicorn should always set SO_KEEPALIVE on inherited sockets' - ensure - sock.close if sock - end - def test_working_directory_rel_path_config_file other = Tempfile.new('unicorn.wd') File.unlink(other.path) @@ -254,7 +202,13 @@ after_fork do |server, worker| end EOF pid = xfork { redirect_test_io { exec($unicorn_bin, "-c#{tmp.path}") } } - File.open("#{other.path}/fifo", "rb").close + begin + fifo = File.open("#{other.path}/fifo", "rb") + rescue Errno::EINTR + # OpenBSD raises Errno::EINTR when opening + return if RUBY_PLATFORM =~ /openbsd/ + end + fifo.close assert ! File.exist?("stderr_log_here") assert ! File.exist?("stdout_log_here") @@ -291,16 +245,6 @@ EOF end end - def test_basic - File.open("config.ru", "wb") { |fp| fp.syswrite(HI) } - pid = fork do - redirect_test_io { exec($unicorn_bin, "-l", "#{@addr}:#{@port}") } - end - results = retry_hit(["http://#{@addr}:#{@port}/"]) - assert_equal String, results[0].class - assert_shutdown(pid) - end - def test_rack_env_unset File.open("config.ru", "wb") { |fp| fp.syswrite(SHOW_RACK_ENV) } pid = fork { redirect_test_io { exec($unicorn_bin, "-l#@addr:#@port") } } @@ -556,7 +500,7 @@ EOF def test_unicorn_config_per_worker_listen port2 = unused_port pid_spit = 'use Rack::ContentLength;' \ - 'run proc { |e| [ 200, {"Content-Type"=>"text/plain"}, ["#$$\\n"] ] }' + 'run proc { |e| [ 200, {"content-type"=>"text/plain"}, ["#$$\\n"] ] }' File.open("config.ru", "wb") { |fp| fp.syswrite(pid_spit) } tmp = Tempfile.new('test.socket') File.unlink(tmp.path) @@ -573,7 +517,7 @@ EOF assert_equal String, results[0].class worker_pid = results[0].to_i assert_not_equal pid, worker_pid - s = UNIXSocket.new(tmp.path) + s = unix_socket(tmp.path) s.syswrite("GET / HTTP/1.0\r\n\r\n") results = '' loop { results << s.sysread(4096) } rescue nil @@ -606,6 +550,7 @@ EOF def test_weird_config_settings File.open("config.ru", "wb") { |fp| fp.syswrite(HI) } ucfg = Tempfile.new('unicorn_test_config') + proc_total = HEAVY_WORKERS + 1 # + 1 for master ucfg.syswrite(HEAVY_CFG) pid = xfork do redirect_test_io do @@ -616,9 +561,9 @@ EOF results = retry_hit(["http://#{@addr}:#{@port}/"]) assert_equal String, results[0].class wait_master_ready(COMMON_TMP.path) - wait_workers_ready(COMMON_TMP.path, 4) + wait_workers_ready(COMMON_TMP.path, HEAVY_WORKERS) bf = File.readlines(COMMON_TMP.path).grep(/\bbefore_fork: worker=/) - assert_equal 4, bf.size + assert_equal HEAVY_WORKERS, bf.size rotate = Tempfile.new('unicorn_rotate') File.rename(COMMON_TMP.path, rotate.path) @@ -630,20 +575,20 @@ EOF tries = DEFAULT_TRIES log = File.readlines(rotate.path) while (tries -= 1) > 0 && - log.grep(/reopening logs\.\.\./).size < 5 + log.grep(/reopening logs\.\.\./).size < proc_total sleep DEFAULT_RES log = File.readlines(rotate.path) end - assert_equal 5, log.grep(/reopening logs\.\.\./).size + assert_equal proc_total, log.grep(/reopening logs\.\.\./).size assert_equal 0, log.grep(/done reopening logs/).size tries = DEFAULT_TRIES log = File.readlines(COMMON_TMP.path) - while (tries -= 1) > 0 && log.grep(/done reopening logs/).size < 5 + while (tries -= 1) > 0 && log.grep(/done reopening logs/).size < proc_total sleep DEFAULT_RES log = File.readlines(COMMON_TMP.path) end - assert_equal 5, log.grep(/done reopening logs/).size + assert_equal proc_total, log.grep(/done reopening logs/).size assert_equal 0, log.grep(/reopening logs\.\.\./).size Process.kill(:QUIT, pid) @@ -663,20 +608,6 @@ EOF assert_shutdown(pid) end - def test_config_ru_alt_path - config_path = "#{@tmpdir}/foo.ru" - File.open(config_path, "wb") { |fp| fp.syswrite(HI) } - pid = fork do - redirect_test_io do - Dir.chdir("/") - exec($unicorn_bin, "-l#{@addr}:#{@port}", config_path) - end - end - results = retry_hit(["http://#{@addr}:#{@port}/"]) - assert_equal String, results[0].class - assert_shutdown(pid) - end - def test_load_module libdir = "#{@tmpdir}/lib" FileUtils.mkpath([ libdir ]) @@ -730,7 +661,7 @@ EOF wait_for_file(sock_path) assert File.socket?(sock_path) - sock = UNIXSocket.new(sock_path) + sock = unix_socket(sock_path) sock.syswrite("GET / HTTP/1.0\r\n\r\n") results = sock.sysread(4096) @@ -740,7 +671,7 @@ EOF wait_for_file(sock_path) assert File.socket?(sock_path) - sock = UNIXSocket.new(sock_path) + sock = unix_socket(sock_path) sock.syswrite("GET / HTTP/1.0\r\n\r\n") results = sock.sysread(4096) @@ -775,7 +706,7 @@ EOF assert_equal pid, File.read(pid_file).to_i assert File.socket?(sock_path), "socket created" - sock = UNIXSocket.new(sock_path) + sock = unix_socket(sock_path) sock.syswrite("GET / HTTP/1.0\r\n\r\n") results = sock.sysread(4096) @@ -801,7 +732,7 @@ EOF wait_for_file(new_sock_path) assert File.socket?(new_sock_path), "socket exists" @sockets.each do |path| - sock = UNIXSocket.new(path) + sock = unix_socket(path) sock.syswrite("GET / HTTP/1.0\r\n\r\n") results = sock.sysread(4096) assert_equal String, results.class |