1
0
Fork 0
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:
Nate Berkopec 2020-09-08 12:49:04 -07:00 committed by GitHub
commit 71c6cd265b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 82 additions and 47 deletions

View file

@ -2,10 +2,12 @@
require "puma/control_cli"
require "open3"
require_relative 'tmp_path'
# Only single mode tests go here. Cluster and pumactl tests
# have their own files, use those instead
class TestIntegration < Minitest::Test
include TmpPath
HOST = "127.0.0.1"
TOKEN = "xxyyzz"
WORKERS = 2
@ -15,7 +17,7 @@ class TestIntegration < Minitest::Test
def setup
@ios_to_close = []
@bind_path = "test/#{name}_server.sock"
@bind_path = tmp_path('.sock')
end
def teardown

32
test/helpers/tmp_path.rb Normal file
View 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

View file

@ -2,6 +2,7 @@
require_relative "helper"
require_relative "helpers/ssl"
require_relative "helpers/tmp_path"
require "puma/binder"
require "puma/puma_http11"
@ -10,6 +11,7 @@ require "puma/configuration"
class TestBinderBase < Minitest::Test
include SSLHelper
include TmpPath
def setup
@events = Puma::Events.strings
@ -123,8 +125,8 @@ class TestBinder < TestBinderBase
def test_pre_existing_unix
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' }
@binder.parse ["unix://#{unix_path}"], @events
@ -248,13 +250,12 @@ class TestBinder < TestBinderBase
def test_listeners_file_unlink_if_unix_listener
skip UNIX_SKT_MSG unless UNIX_SKT_EXIST
@binder.parse ["unix://test/#{name}_server.sock"], @events
assert File.socket?("test/#{name}_server.sock")
unix_path = tmp_path('.sock')
@binder.parse ["unix://#{unix_path}"], @events
assert File.socket?(unix_path)
@binder.close_listeners
refute File.socket?("test/#{name}_server.sock")
refute File.socket?(unix_path)
end
def test_import_from_env_listen_inherit
@ -289,11 +290,12 @@ class TestBinder < TestBinderBase
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
path = "test/unixserver.state"
sock = Addrinfo.unix(path).listen
assert_activates_sockets(path: path, sock: sock)
state_path = tmp_path('.state')
sock = Addrinfo.unix(state_path).listen
assert_activates_sockets(path: state_path, sock: sock)
ensure
File.unlink(path) rescue nil # JRuby race?
File.unlink(state_path) rescue nil # JRuby race?
end
def test_rack_multithread_default_configuration
@ -345,10 +347,11 @@ class TestBinder < TestBinderBase
def assert_parsing_logs_uri(order = [:unix, :tcp])
skip UNIX_SKT_MSG if order.include?(:unix) && !UNIX_SKT_EXIST
unix_path = tmp_path('.sock')
prepared_paths = {
ssl: "ssl://127.0.0.1:#{UniquePort.call}?#{ssl_query}",
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|

View file

@ -1,18 +1,18 @@
require_relative "helper"
require_relative "helpers/ssl"
require_relative "helpers/tmp_path"
require "puma/cli"
require "json"
class TestCLI < Minitest::Test
include SSLHelper
include TmpPath
def setup
@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"
File.unlink @tmp_path if File.exist? @tmp_path

View file

@ -2,13 +2,14 @@ require_relative "helper"
require_relative "helpers/integration"
class TestIntegrationPumactl < TestIntegration
include TmpPath
parallelize_me!
def setup
super
@state_path = "test/#{name}_puma.state"
@control_path = "test/#{name}_control.sock"
@state_path = tmp_path('.state')
@control_path = tmp_path('.sock')
end
def teardown

View file

@ -1,9 +1,12 @@
require_relative "helper"
require_relative "helpers/tmp_path"
require "puma/configuration"
require 'puma/events'
class TestLauncher < Minitest::Test
include TmpPath
def test_dependencies_and_files_to_require_after_prune_is_correctly_built_for_no_extra_deps
skip_on :no_bundler
@ -64,70 +67,62 @@ class TestLauncher < Minitest::Test
end
def test_pid_file
tmp_file = Tempfile.new("puma-test")
tmp_path = tmp_file.path
tmp_file.close!
pid_path = tmp_path('.pid')
conf = Puma::Configuration.new do |c|
c.pidfile tmp_path
c.pidfile pid_path
end
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
def test_state_permission_0640
tmp_file = Tempfile.new("puma-test")
tmp_path = tmp_file.path
tmp_file.close!
tmp_permission = 0640
state_path = tmp_path('.state')
state_permission = 0640
conf = Puma::Configuration.new do |c|
c.state_path tmp_path
c.state_permission tmp_permission
c.state_path state_path
c.state_permission state_permission
end
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
File.unlink tmp_path
File.unlink state_path
end
def test_state_permission_nil
tmp_file = Tempfile.new("puma-test")
tmp_path = tmp_file.path
tmp_file.close!
state_path = tmp_path('.state')
conf = Puma::Configuration.new do |c|
c.state_path tmp_path
c.state_path state_path
c.state_permission nil
end
launcher(conf).write_state
assert File.exist?(tmp_path)
assert File.exist?(state_path)
ensure
File.unlink tmp_path
File.unlink state_path
end
def test_no_state_permission
tmp_file = Tempfile.new("puma-test")
tmp_path = tmp_file.path
tmp_file.close!
state_path = tmp_path('.state')
conf = Puma::Configuration.new do |c|
c.state_path tmp_path
c.state_path state_path
end
launcher(conf).write_state
assert File.exist?(tmp_path)
assert File.exist?(state_path)
ensure
File.unlink tmp_path
File.unlink state_path
end
def test_puma_stats

View file

@ -1,5 +1,6 @@
# frozen_string_literal: true
require_relative "helper"
require "net/http"
require "rack"

View file

@ -1,17 +1,18 @@
# frozen_string_literal: true
require_relative "helper"
require_relative "helpers/tmp_path"
class TestPumaUnixSocket < Minitest::Test
include TmpPath
App = lambda { |env| [200, {}, ["Works"]] }
PATH = "test/puma.sock"
def setup
return unless UNIX_SKT_EXIST
@tmp_socket_path = tmp_path('.sock')
@server = Puma::Server.new App
@server.add_unix_listener PATH
@server.add_unix_listener @tmp_socket_path
@server.run
end
@ -22,7 +23,7 @@ class TestPumaUnixSocket < Minitest::Test
def test_server
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"