mirror of
https://github.com/puma/puma.git
synced 2022-11-09 13:48:40 -05:00
Merge pull request #2306 from alexeevit/feature/use-tempfile-for-state-sock-files
Use Tempfile for sockets and state files in tests
This commit is contained in:
commit
71c6cd265b
8 changed files with 82 additions and 47 deletions
|
@ -2,10 +2,12 @@
|
||||||
|
|
||||||
require "puma/control_cli"
|
require "puma/control_cli"
|
||||||
require "open3"
|
require "open3"
|
||||||
|
require_relative 'tmp_path'
|
||||||
|
|
||||||
# Only single mode tests go here. Cluster and pumactl tests
|
# Only single mode tests go here. Cluster and pumactl tests
|
||||||
# have their own files, use those instead
|
# have their own files, use those instead
|
||||||
class TestIntegration < Minitest::Test
|
class TestIntegration < Minitest::Test
|
||||||
|
include TmpPath
|
||||||
HOST = "127.0.0.1"
|
HOST = "127.0.0.1"
|
||||||
TOKEN = "xxyyzz"
|
TOKEN = "xxyyzz"
|
||||||
WORKERS = 2
|
WORKERS = 2
|
||||||
|
@ -15,7 +17,7 @@ class TestIntegration < Minitest::Test
|
||||||
|
|
||||||
def setup
|
def setup
|
||||||
@ios_to_close = []
|
@ios_to_close = []
|
||||||
@bind_path = "test/#{name}_server.sock"
|
@bind_path = tmp_path('.sock')
|
||||||
end
|
end
|
||||||
|
|
||||||
def teardown
|
def teardown
|
||||||
|
|
32
test/helpers/tmp_path.rb
Normal file
32
test/helpers/tmp_path.rb
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
module TmpPath
|
||||||
|
def capture_exceptions
|
||||||
|
super
|
||||||
|
ensure
|
||||||
|
clean_tmp_paths
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def tmp_path(extension=nil)
|
||||||
|
sock_file = Tempfile.new(['', extension])
|
||||||
|
path = sock_file.path
|
||||||
|
sock_file.close!
|
||||||
|
tmp_paths << path
|
||||||
|
path
|
||||||
|
end
|
||||||
|
|
||||||
|
def tmp_paths
|
||||||
|
@tmp_paths ||= []
|
||||||
|
end
|
||||||
|
|
||||||
|
def clean_tmp_paths
|
||||||
|
while path = tmp_paths.pop
|
||||||
|
delete_tmp_path(path)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def delete_tmp_path(path)
|
||||||
|
File.unlink(path)
|
||||||
|
rescue Errno::ENOENT
|
||||||
|
end
|
||||||
|
end
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
require_relative "helper"
|
require_relative "helper"
|
||||||
require_relative "helpers/ssl"
|
require_relative "helpers/ssl"
|
||||||
|
require_relative "helpers/tmp_path"
|
||||||
|
|
||||||
require "puma/binder"
|
require "puma/binder"
|
||||||
require "puma/puma_http11"
|
require "puma/puma_http11"
|
||||||
|
@ -10,6 +11,7 @@ require "puma/configuration"
|
||||||
|
|
||||||
class TestBinderBase < Minitest::Test
|
class TestBinderBase < Minitest::Test
|
||||||
include SSLHelper
|
include SSLHelper
|
||||||
|
include TmpPath
|
||||||
|
|
||||||
def setup
|
def setup
|
||||||
@events = Puma::Events.strings
|
@events = Puma::Events.strings
|
||||||
|
@ -123,8 +125,8 @@ class TestBinder < TestBinderBase
|
||||||
|
|
||||||
def test_pre_existing_unix
|
def test_pre_existing_unix
|
||||||
skip UNIX_SKT_MSG unless UNIX_SKT_EXIST
|
skip UNIX_SKT_MSG unless UNIX_SKT_EXIST
|
||||||
unix_path = "test/#{name}_server.sock"
|
|
||||||
|
|
||||||
|
unix_path = tmp_path('.sock')
|
||||||
File.open(unix_path, mode: 'wb') { |f| f.puts 'pre existing' }
|
File.open(unix_path, mode: 'wb') { |f| f.puts 'pre existing' }
|
||||||
@binder.parse ["unix://#{unix_path}"], @events
|
@binder.parse ["unix://#{unix_path}"], @events
|
||||||
|
|
||||||
|
@ -248,13 +250,12 @@ class TestBinder < TestBinderBase
|
||||||
def test_listeners_file_unlink_if_unix_listener
|
def test_listeners_file_unlink_if_unix_listener
|
||||||
skip UNIX_SKT_MSG unless UNIX_SKT_EXIST
|
skip UNIX_SKT_MSG unless UNIX_SKT_EXIST
|
||||||
|
|
||||||
@binder.parse ["unix://test/#{name}_server.sock"], @events
|
unix_path = tmp_path('.sock')
|
||||||
|
@binder.parse ["unix://#{unix_path}"], @events
|
||||||
assert File.socket?("test/#{name}_server.sock")
|
assert File.socket?(unix_path)
|
||||||
|
|
||||||
@binder.close_listeners
|
@binder.close_listeners
|
||||||
|
refute File.socket?(unix_path)
|
||||||
refute File.socket?("test/#{name}_server.sock")
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_import_from_env_listen_inherit
|
def test_import_from_env_listen_inherit
|
||||||
|
@ -289,11 +290,12 @@ class TestBinder < TestBinderBase
|
||||||
def test_socket_activation_unix
|
def test_socket_activation_unix
|
||||||
skip_on :jruby # Failing with what I think is a JRuby bug
|
skip_on :jruby # Failing with what I think is a JRuby bug
|
||||||
skip UNIX_SKT_MSG unless UNIX_SKT_EXIST
|
skip UNIX_SKT_MSG unless UNIX_SKT_EXIST
|
||||||
path = "test/unixserver.state"
|
|
||||||
sock = Addrinfo.unix(path).listen
|
state_path = tmp_path('.state')
|
||||||
assert_activates_sockets(path: path, sock: sock)
|
sock = Addrinfo.unix(state_path).listen
|
||||||
|
assert_activates_sockets(path: state_path, sock: sock)
|
||||||
ensure
|
ensure
|
||||||
File.unlink(path) rescue nil # JRuby race?
|
File.unlink(state_path) rescue nil # JRuby race?
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_rack_multithread_default_configuration
|
def test_rack_multithread_default_configuration
|
||||||
|
@ -345,10 +347,11 @@ class TestBinder < TestBinderBase
|
||||||
def assert_parsing_logs_uri(order = [:unix, :tcp])
|
def assert_parsing_logs_uri(order = [:unix, :tcp])
|
||||||
skip UNIX_SKT_MSG if order.include?(:unix) && !UNIX_SKT_EXIST
|
skip UNIX_SKT_MSG if order.include?(:unix) && !UNIX_SKT_EXIST
|
||||||
|
|
||||||
|
unix_path = tmp_path('.sock')
|
||||||
prepared_paths = {
|
prepared_paths = {
|
||||||
ssl: "ssl://127.0.0.1:#{UniquePort.call}?#{ssl_query}",
|
ssl: "ssl://127.0.0.1:#{UniquePort.call}?#{ssl_query}",
|
||||||
tcp: "tcp://127.0.0.1:#{UniquePort.call}",
|
tcp: "tcp://127.0.0.1:#{UniquePort.call}",
|
||||||
unix: "unix://test/#{name}_server.sock"
|
unix: "unix://#{unix_path}"
|
||||||
}
|
}
|
||||||
|
|
||||||
expected_logs = prepared_paths.dup.tap do |logs|
|
expected_logs = prepared_paths.dup.tap do |logs|
|
||||||
|
|
|
@ -1,18 +1,18 @@
|
||||||
require_relative "helper"
|
require_relative "helper"
|
||||||
require_relative "helpers/ssl"
|
require_relative "helpers/ssl"
|
||||||
|
require_relative "helpers/tmp_path"
|
||||||
|
|
||||||
require "puma/cli"
|
require "puma/cli"
|
||||||
require "json"
|
require "json"
|
||||||
|
|
||||||
class TestCLI < Minitest::Test
|
class TestCLI < Minitest::Test
|
||||||
include SSLHelper
|
include SSLHelper
|
||||||
|
include TmpPath
|
||||||
|
|
||||||
def setup
|
def setup
|
||||||
@environment = 'production'
|
@environment = 'production'
|
||||||
@tmp_file = Tempfile.new("puma-test")
|
|
||||||
@tmp_path = @tmp_file.path
|
|
||||||
@tmp_file.close!
|
|
||||||
|
|
||||||
|
@tmp_path = tmp_path('puma-test')
|
||||||
@tmp_path2 = "#{@tmp_path}2"
|
@tmp_path2 = "#{@tmp_path}2"
|
||||||
|
|
||||||
File.unlink @tmp_path if File.exist? @tmp_path
|
File.unlink @tmp_path if File.exist? @tmp_path
|
||||||
|
|
|
@ -2,13 +2,14 @@ require_relative "helper"
|
||||||
require_relative "helpers/integration"
|
require_relative "helpers/integration"
|
||||||
|
|
||||||
class TestIntegrationPumactl < TestIntegration
|
class TestIntegrationPumactl < TestIntegration
|
||||||
|
include TmpPath
|
||||||
parallelize_me!
|
parallelize_me!
|
||||||
|
|
||||||
def setup
|
def setup
|
||||||
super
|
super
|
||||||
|
|
||||||
@state_path = "test/#{name}_puma.state"
|
@state_path = tmp_path('.state')
|
||||||
@control_path = "test/#{name}_control.sock"
|
@control_path = tmp_path('.sock')
|
||||||
end
|
end
|
||||||
|
|
||||||
def teardown
|
def teardown
|
||||||
|
|
|
@ -1,9 +1,12 @@
|
||||||
require_relative "helper"
|
require_relative "helper"
|
||||||
|
require_relative "helpers/tmp_path"
|
||||||
|
|
||||||
require "puma/configuration"
|
require "puma/configuration"
|
||||||
require 'puma/events'
|
require 'puma/events'
|
||||||
|
|
||||||
class TestLauncher < Minitest::Test
|
class TestLauncher < Minitest::Test
|
||||||
|
include TmpPath
|
||||||
|
|
||||||
def test_dependencies_and_files_to_require_after_prune_is_correctly_built_for_no_extra_deps
|
def test_dependencies_and_files_to_require_after_prune_is_correctly_built_for_no_extra_deps
|
||||||
skip_on :no_bundler
|
skip_on :no_bundler
|
||||||
|
|
||||||
|
@ -64,70 +67,62 @@ class TestLauncher < Minitest::Test
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_pid_file
|
def test_pid_file
|
||||||
tmp_file = Tempfile.new("puma-test")
|
pid_path = tmp_path('.pid')
|
||||||
tmp_path = tmp_file.path
|
|
||||||
tmp_file.close!
|
|
||||||
|
|
||||||
conf = Puma::Configuration.new do |c|
|
conf = Puma::Configuration.new do |c|
|
||||||
c.pidfile tmp_path
|
c.pidfile pid_path
|
||||||
end
|
end
|
||||||
|
|
||||||
launcher(conf).write_state
|
launcher(conf).write_state
|
||||||
|
|
||||||
assert_equal File.read(tmp_path).strip.to_i, Process.pid
|
assert_equal File.read(pid_path).strip.to_i, Process.pid
|
||||||
|
|
||||||
File.unlink tmp_path
|
File.unlink pid_path
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_state_permission_0640
|
def test_state_permission_0640
|
||||||
tmp_file = Tempfile.new("puma-test")
|
state_path = tmp_path('.state')
|
||||||
tmp_path = tmp_file.path
|
state_permission = 0640
|
||||||
tmp_file.close!
|
|
||||||
tmp_permission = 0640
|
|
||||||
|
|
||||||
conf = Puma::Configuration.new do |c|
|
conf = Puma::Configuration.new do |c|
|
||||||
c.state_path tmp_path
|
c.state_path state_path
|
||||||
c.state_permission tmp_permission
|
c.state_permission state_permission
|
||||||
end
|
end
|
||||||
|
|
||||||
launcher(conf).write_state
|
launcher(conf).write_state
|
||||||
|
|
||||||
assert File.stat(tmp_path).mode.to_s(8)[-4..-1], tmp_permission
|
assert File.stat(state_path).mode.to_s(8)[-4..-1], state_permission
|
||||||
ensure
|
ensure
|
||||||
File.unlink tmp_path
|
File.unlink state_path
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_state_permission_nil
|
def test_state_permission_nil
|
||||||
tmp_file = Tempfile.new("puma-test")
|
state_path = tmp_path('.state')
|
||||||
tmp_path = tmp_file.path
|
|
||||||
tmp_file.close!
|
|
||||||
|
|
||||||
conf = Puma::Configuration.new do |c|
|
conf = Puma::Configuration.new do |c|
|
||||||
c.state_path tmp_path
|
c.state_path state_path
|
||||||
c.state_permission nil
|
c.state_permission nil
|
||||||
end
|
end
|
||||||
|
|
||||||
launcher(conf).write_state
|
launcher(conf).write_state
|
||||||
|
|
||||||
assert File.exist?(tmp_path)
|
assert File.exist?(state_path)
|
||||||
ensure
|
ensure
|
||||||
File.unlink tmp_path
|
File.unlink state_path
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_no_state_permission
|
def test_no_state_permission
|
||||||
tmp_file = Tempfile.new("puma-test")
|
state_path = tmp_path('.state')
|
||||||
tmp_path = tmp_file.path
|
|
||||||
tmp_file.close!
|
|
||||||
|
|
||||||
conf = Puma::Configuration.new do |c|
|
conf = Puma::Configuration.new do |c|
|
||||||
c.state_path tmp_path
|
c.state_path state_path
|
||||||
end
|
end
|
||||||
|
|
||||||
launcher(conf).write_state
|
launcher(conf).write_state
|
||||||
|
|
||||||
assert File.exist?(tmp_path)
|
assert File.exist?(state_path)
|
||||||
ensure
|
ensure
|
||||||
File.unlink tmp_path
|
File.unlink state_path
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_puma_stats
|
def test_puma_stats
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
require_relative "helper"
|
require_relative "helper"
|
||||||
|
require "net/http"
|
||||||
|
|
||||||
require "rack"
|
require "rack"
|
||||||
|
|
||||||
|
|
|
@ -1,17 +1,18 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
require_relative "helper"
|
require_relative "helper"
|
||||||
|
require_relative "helpers/tmp_path"
|
||||||
|
|
||||||
class TestPumaUnixSocket < Minitest::Test
|
class TestPumaUnixSocket < Minitest::Test
|
||||||
|
include TmpPath
|
||||||
|
|
||||||
App = lambda { |env| [200, {}, ["Works"]] }
|
App = lambda { |env| [200, {}, ["Works"]] }
|
||||||
|
|
||||||
PATH = "test/puma.sock"
|
|
||||||
|
|
||||||
def setup
|
def setup
|
||||||
return unless UNIX_SKT_EXIST
|
return unless UNIX_SKT_EXIST
|
||||||
|
@tmp_socket_path = tmp_path('.sock')
|
||||||
@server = Puma::Server.new App
|
@server = Puma::Server.new App
|
||||||
@server.add_unix_listener PATH
|
@server.add_unix_listener @tmp_socket_path
|
||||||
@server.run
|
@server.run
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -22,7 +23,7 @@ class TestPumaUnixSocket < Minitest::Test
|
||||||
|
|
||||||
def test_server
|
def test_server
|
||||||
skip UNIX_SKT_MSG unless UNIX_SKT_EXIST
|
skip UNIX_SKT_MSG unless UNIX_SKT_EXIST
|
||||||
sock = UNIXSocket.new PATH
|
sock = UNIXSocket.new @tmp_socket_path
|
||||||
|
|
||||||
sock << "GET / HTTP/1.0\r\nHost: blah.com\r\n\r\n"
|
sock << "GET / HTTP/1.0\r\nHost: blah.com\r\n\r\n"
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue