Skip to content

Commit

Permalink
Update helper.rb TestSkips, add HAS_UNIX_SOCKET to puma.rb [changelog…
Browse files Browse the repository at this point in the history
… skip] (puma#2576)

* Update helper.rb TestSkips, add HAS_UNIX_SOCKET to puma.rb

* Tests - skip updates

* test_integration_cluster.rb - fix some test warnings & RuboCop
  • Loading branch information
MSP-Greg committed Mar 15, 2021
1 parent 41280fd commit 51ea1b4
Show file tree
Hide file tree
Showing 17 changed files with 106 additions and 97 deletions.
2 changes: 2 additions & 0 deletions lib/puma.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ module Puma
# not in minissl.rb
HAS_SSL = const_defined?(:MiniSSL, false) && MiniSSL.const_defined?(:Engine, false)

HAS_UNIX_SOCKET = Object.const_defined? :UNIXSocket

if HAS_SSL
require 'puma/minissl'
else
Expand Down
47 changes: 28 additions & 19 deletions test/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,20 +91,21 @@ def run

module TestSkips

# usage: skip NO_FORK_MSG unless HAS_FORK
# windows >= 2.6 fork is not defined, < 2.6 fork raises NotImplementedError
HAS_FORK = ::Process.respond_to? :fork
NO_FORK_MSG = "Kernel.fork isn't available on #{RUBY_ENGINE} on #{RUBY_PLATFORM}"

# socket is required by puma
# usage: skip UNIX_SKT_MSG unless UNIX_SKT_EXIST
UNIX_SKT_EXIST = Object.const_defined? :UNIXSocket
UNIX_SKT_MSG = "UnixSockets aren't available on the #{RUBY_PLATFORM} platform"

MSG_FORK = "Kernel.fork isn't available on #{RUBY_ENGINE} on #{RUBY_PLATFORM}"
MSG_UNIX = "UNIXSockets aren't available on the #{RUBY_PLATFORM} platform"
MSG_AUNIX = "Abstract UNIXSockets aren't available on the #{RUBY_PLATFORM} platform"

SIGNAL_LIST = Signal.list.keys.map(&:to_sym) - (Puma.windows? ? [:INT, :TERM] : [])

JRUBY_HEAD = Puma::IS_JRUBY && RUBY_DESCRIPTION =~ /SNAPSHOT/

DARWIN = RUBY_PLATFORM.include? 'darwin'

TRUFFLE = RUBY_ENGINE == 'truffleruby'

# usage: skip_unless_signal_exist? :USR2
def skip_unless_signal_exist?(sig, bt: caller)
signal = sig.to_s.sub(/\ASIG/, '').to_sym
Expand All @@ -113,18 +114,22 @@ def skip_unless_signal_exist?(sig, bt: caller)
end
end

# called with one or more params, like skip_on :jruby, :windows
# called with one or more params, like skip_if :jruby, :windows
# optional suffix kwarg is appended to the skip message
# optional suffix bt should generally not used
def skip_on(*engs, suffix: '', bt: caller)
def skip_if(*engs, suffix: '', bt: caller)
engs.each do |eng|
skip_msg = case eng
when :darwin then "Skipped on darwin#{suffix}" if RUBY_PLATFORM[/darwin/]
when :jruby then "Skipped on JRuby#{suffix}" if Puma.jruby?
when :truffleruby then "Skipped on TruffleRuby#{suffix}" if RUBY_ENGINE == "truffleruby"
when :windows then "Skipped on Windows#{suffix}" if Puma.windows?
when :ci then "Skipped on ENV['CI']#{suffix}" if ENV["CI"]
when :no_bundler then "Skipped w/o Bundler#{suffix}" if !defined?(Bundler)
when :darwin then "Skipped if darwin#{suffix}" if Puma::IS_OSX
when :jruby then "Skipped if JRuby#{suffix}" if Puma::IS_JRUBY
when :truffleruby then "Skipped if TruffleRuby#{suffix}" if TRUFFLE
when :windows then "Skipped if Windows#{suffix}" if Puma::IS_WINDOWS
when :ci then "Skipped if ENV['CI']#{suffix}" if ENV['CI']
when :no_bundler then "Skipped w/o Bundler#{suffix}" if !defined?(Bundler)
when :ssl then "Skipped if SSL is supported" if Puma::HAS_SSL
when :fork then "Skipped if Kernel.fork exists" if HAS_FORK
when :unix then "Skipped if UNIXSocket exists" if Puma::HAS_UNIX_SOCKET
when :aunix then "Skipped if abstract UNIXSocket" if Puma.abstract_unix_socket?
else false
end
skip skip_msg, bt if skip_msg
Expand All @@ -134,10 +139,14 @@ def skip_on(*engs, suffix: '', bt: caller)
# called with only one param
def skip_unless(eng, bt: caller)
skip_msg = case eng
when :darwin then "Skip unless darwin" unless RUBY_PLATFORM[/darwin/]
when :jruby then "Skip unless JRuby" unless Puma.jruby?
when :windows then "Skip unless Windows" unless Puma.windows?
when :mri then "Skip unless MRI" unless Puma.mri?
when :darwin then "Skip unless darwin" unless Puma::IS_OSX
when :jruby then "Skip unless JRuby" unless Puma::IS_JRUBY
when :windows then "Skip unless Windows" unless Puma::IS_WINDOWS
when :mri then "Skip unless MRI" unless Puma::IS_MRI
when :ssl then "Skip unless SSL is supported" unless Puma::HAS_SSL
when :fork then MSG_FORK unless HAS_FORK
when :unix then MSG_UNIX unless Puma::HAS_UNIX_SOCKET
when :aunix then MSG_AUNIX unless Puma.abstract_unix_socket?
else false
end
skip skip_msg, bt if skip_msg
Expand Down
4 changes: 2 additions & 2 deletions test/helpers/integration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,10 @@ def cli_pumactl(argv, unix: false)

def hot_restart_does_not_drop_connections(num_threads: 1, total_requests: 500)
skipped = true
skip_on :jruby, suffix: <<-MSG
skip_if :jruby, suffix: <<-MSG
- file descriptors are not preserved on exec on JRuby; connection reset errors are expected during restarts
MSG
skip_on :truffleruby, suffix: ' - Undiagnosed failures on TruffleRuby'
skip_if :truffleruby, suffix: ' - Undiagnosed failures on TruffleRuby'
skip "Undiagnosed failures on Ruby 2.2" if RUBY_VERSION < '2.3'

args = "-w #{workers} -t 0:5 -q test/rackup/hello_with_delay.ru"
Expand Down
48 changes: 24 additions & 24 deletions test/test_binder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,14 @@ def test_connected_ports
end

def test_localhost_addresses_dont_alter_listeners_for_ssl_addresses
skip 'No ssl support' unless ::Puma::HAS_SSL
skip_unless :ssl
@binder.parse ["ssl://localhost:0?#{ssl_query}"], @events

assert_empty @binder.listeners
end

def test_home_alters_listeners_for_ssl_addresses
skip 'No ssl support' unless ::Puma::HAS_SSL
skip_unless :ssl
port = UniquePort.call
@binder.parse ["ssl://127.0.0.1:#{port}?#{ssl_query}"], @events

Expand All @@ -127,7 +127,7 @@ def test_correct_zero_port
end

def test_correct_zero_port_ssl
skip 'No ssl support' unless ::Puma::HAS_SSL
skip_unless :ssl

ssl_regex = %r!ssl://127.0.0.1:(\d+)!

Expand All @@ -148,7 +148,7 @@ def test_logs_all_localhost_bindings
end

def test_logs_all_localhost_bindings_ssl
skip 'No ssl support' unless ::Puma::HAS_SSL
skip_unless :ssl

@binder.parse ["ssl://localhost:0?#{ssl_query}"], @events

Expand All @@ -163,7 +163,7 @@ def test_allows_both_ssl_and_tcp
end

def test_allows_both_unix_and_tcp
skip_on :jruby # Undiagnosed thread race. TODO fix
skip_if :jruby # Undiagnosed thread race. TODO fix
assert_parsing_logs_uri [:unix, :tcp]
end

Expand All @@ -172,7 +172,7 @@ def test_allows_both_tcp_and_unix
end

def test_pre_existing_unix
skip UNIX_SKT_MSG unless UNIX_SKT_EXIST
skip_unless :unix

unix_path = tmp_path('.sock')
File.open(unix_path, mode: 'wb') { |f| f.puts 'pre existing' }
Expand All @@ -193,43 +193,43 @@ def test_pre_existing_unix
end

def test_binder_parses_tlsv1_disabled
skip 'No ssl support' unless ::Puma::HAS_SSL
skip_unless :ssl
@binder.parse ["ssl://0.0.0.0:0?#{ssl_query}&no_tlsv1=true"], @events

assert ssl_context_for_binder.no_tlsv1
end

def test_binder_parses_tlsv1_enabled
skip 'No ssl support' unless ::Puma::HAS_SSL
skip_unless :ssl
@binder.parse ["ssl://0.0.0.0:0?#{ssl_query}&no_tlsv1=false"], @events

refute ssl_context_for_binder.no_tlsv1
end

def test_binder_parses_tlsv1_tlsv1_1_unspecified_defaults_to_enabled
skip 'No ssl support' unless ::Puma::HAS_SSL
skip_unless :ssl
@binder.parse ["ssl://0.0.0.0:0?#{ssl_query}"], @events

refute ssl_context_for_binder.no_tlsv1
refute ssl_context_for_binder.no_tlsv1_1
end

def test_binder_parses_tlsv1_1_disabled
skip 'No ssl support' unless ::Puma::HAS_SSL
skip_unless :ssl
@binder.parse ["ssl://0.0.0.0:0?#{ssl_query}&no_tlsv1_1=true"], @events

assert ssl_context_for_binder.no_tlsv1_1
end

def test_binder_parses_tlsv1_1_enabled
skip 'No ssl support' unless ::Puma::HAS_SSL
skip_unless :ssl
@binder.parse ["ssl://0.0.0.0:0?#{ssl_query}&no_tlsv1_1=false"], @events

refute ssl_context_for_binder.no_tlsv1_1
end

def test_env_contains_protoenv
skip 'No ssl support' unless ::Puma::HAS_SSL
skip_unless :ssl
@binder.parse ["ssl://localhost:0?#{ssl_query}"], @events

env_hash = @binder.envs[@binder.ios.first]
Expand All @@ -240,7 +240,7 @@ def test_env_contains_protoenv
end

def test_env_contains_stderr
skip 'No ssl support' unless ::Puma::HAS_SSL
skip_unless :ssl
@binder.parse ["ssl://localhost:0?#{ssl_query}"], @events

env_hash = @binder.envs[@binder.ios.first]
Expand Down Expand Up @@ -303,7 +303,7 @@ def bomb.close; raise "Boom!"; end # the bomb has been planted
end

def test_listeners_file_unlink_if_unix_listener
skip UNIX_SKT_MSG unless UNIX_SKT_EXIST
skip_unless :unix

unix_path = tmp_path('.sock')
@binder.parse ["unix://#{unix_path}"], @events
Expand All @@ -327,24 +327,24 @@ def test_import_from_env_listen_inherit
# because the check that we do in the code only works if you support UNIX sockets.
# This is OK, because systemd obviously only works on Linux.
def test_socket_activation_tcp
skip UNIX_SKT_MSG unless UNIX_SKT_EXIST
skip_unless :unix
url = "127.0.0.1"
port = UniquePort.call
sock = Addrinfo.tcp(url, port).listen
assert_activates_sockets(url: url, port: port, sock: sock)
end

def test_socket_activation_tcp_ipv6
skip UNIX_SKT_MSG unless UNIX_SKT_EXIST
skip_unless :unix
url = "::"
port = UniquePort.call
sock = Addrinfo.tcp(url, port).listen
assert_activates_sockets(url: url, port: port, sock: sock)
end

def test_socket_activation_unix
skip_on :jruby # Failing with what I think is a JRuby bug
skip UNIX_SKT_MSG unless UNIX_SKT_EXIST
skip_if :jruby # Failing with what I think is a JRuby bug
skip_unless :unix

state_path = tmp_path('.state')
sock = Addrinfo.unix(state_path).listen
Expand Down Expand Up @@ -400,8 +400,8 @@ def @binder.socket_activation_fd(int); @sock_fd; end
end

def assert_parsing_logs_uri(order = [:unix, :tcp])
skip UNIX_SKT_MSG if order.include?(:unix) && !UNIX_SKT_EXIST
skip 'No ssl support' unless ::Puma::HAS_SSL
skip MSG_UNIX if order.include?(:unix) && !UNIX_SKT_EXIST
skip_unless :ssl

unix_path = tmp_path('.sock')
prepared_paths = {
Expand Down Expand Up @@ -429,7 +429,7 @@ def assert_parsing_logs_uri(order = [:unix, :tcp])

class TestBinderJRuby < TestBinderBase
def test_binder_parses_jruby_ssl_options
skip 'No ssl support' unless ::Puma::HAS_SSL
skip_unless :ssl

keystore = File.expand_path "../../examples/puma/keystore.jks", __FILE__
ssl_cipher_list = "TLS_DHE_RSA_WITH_AES_128_CBC_SHA,TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256"
Expand All @@ -443,7 +443,7 @@ def test_binder_parses_jruby_ssl_options

class TestBinderMRI < TestBinderBase
def test_binder_parses_ssl_cipher_filter
skip 'No ssl support' unless ::Puma::HAS_SSL
skip_unless :ssl

ssl_cipher_filter = "AES@STRENGTH"

Expand All @@ -453,7 +453,7 @@ def test_binder_parses_ssl_cipher_filter
end

def test_binder_parses_ssl_verification_flags_one
skip 'No ssl support' unless ::Puma::HAS_SSL
skip_unless :ssl

input = "&verification_flags=TRUSTED_FIRST"

Expand All @@ -463,7 +463,7 @@ def test_binder_parses_ssl_verification_flags_one
end

def test_binder_parses_ssl_verification_flags_multiple
skip 'No ssl support' unless ::Puma::HAS_SSL
skip_unless :ssl

input = "&verification_flags=TRUSTED_FIRST,NO_CHECK_TIME"

Expand Down
22 changes: 11 additions & 11 deletions test/test_cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def test_control_for_tcp
end

def test_control_for_ssl
skip 'No ssl support' unless ::Puma::HAS_SSL
skip_unless :ssl

require "net/http"
control_port = UniquePort.call
Expand Down Expand Up @@ -105,8 +105,8 @@ def test_control_for_ssl
end

def test_control_clustered
skip NO_FORK_MSG unless HAS_FORK
skip UNIX_SKT_MSG unless UNIX_SKT_EXIST
skip_unless :fork
skip_unless :unix
url = "unix://#{@tmp_path}"

cli = Puma::CLI.new ["-b", "unix://#{@tmp_path2}",
Expand Down Expand Up @@ -156,7 +156,7 @@ def test_control_clustered
end

def test_control
skip UNIX_SKT_MSG unless UNIX_SKT_EXIST
skip_unless :unix
url = "unix://#{@tmp_path}"

cli = Puma::CLI.new ["-b", "unix://#{@tmp_path2}",
Expand All @@ -183,7 +183,7 @@ def test_control
end

def test_control_stop
skip UNIX_SKT_MSG unless UNIX_SKT_EXIST
skip_unless :unix
url = "unix://#{@tmp_path}"

cli = Puma::CLI.new ["-b", "unix://#{@tmp_path2}",
Expand Down Expand Up @@ -248,7 +248,7 @@ def test_control_requests_count
end

def test_control_thread_backtraces
skip UNIX_SKT_MSG unless UNIX_SKT_EXIST
skip_unless :unix
url = "unix://#{@tmp_path}"

cli = Puma::CLI.new ["-b", "unix://#{@tmp_path2}",
Expand Down Expand Up @@ -330,7 +330,7 @@ def test_control_gc_stats_tcp
end

def test_control_gc_stats_unix
skip UNIX_SKT_MSG unless UNIX_SKT_EXIST
skip_unless :unix

uri = "unix://#{@tmp_path2}"
cntl = "unix://#{@tmp_path}"
Expand All @@ -339,7 +339,7 @@ def test_control_gc_stats_unix
end

def test_tmp_control
skip_on :jruby, suffix: " - Unknown issue"
skip_if :jruby, suffix: " - Unknown issue"

cli = Puma::CLI.new ["--state", @tmp_path, "--control-url", "auto"]
cli.launcher.write_state
Expand All @@ -356,7 +356,7 @@ def test_tmp_control
end

def test_state_file_callback_filtering
skip NO_FORK_MSG unless HAS_FORK
skip_unless :fork
cli = Puma::CLI.new [ "--config", "test/config/state_file_testing_config.rb",
"--state", @tmp_path ]
cli.launcher.write_state
Expand All @@ -373,7 +373,7 @@ def test_log_formatter_default_single
end

def test_log_formatter_default_clustered
skip NO_FORK_MSG unless HAS_FORK
skip_unless :fork

cli = Puma::CLI.new [ "-w 2" ]
assert_instance_of Puma::Events::PidFormatter, cli.launcher.events.formatter
Expand All @@ -386,7 +386,7 @@ def test_log_formatter_custom_single
end

def test_log_formatter_custom_clustered
skip NO_FORK_MSG unless HAS_FORK
skip_unless :fork

cli = Puma::CLI.new [ "--config", "test/config/custom_log_formatter.rb", "-w 2" ]
assert_instance_of Proc, cli.launcher.events.formatter
Expand Down

0 comments on commit 51ea1b4

Please sign in to comment.