2017-05-12 19:16:55 +00:00
|
|
|
require_relative "helper"
|
2019-10-18 05:49:05 +00:00
|
|
|
require_relative "helpers/config_file"
|
2019-11-02 18:51:46 +00:00
|
|
|
require_relative "helpers/ssl"
|
2017-03-10 16:02:34 +00:00
|
|
|
|
2019-10-29 21:42:35 +00:00
|
|
|
require 'pathname'
|
2017-03-10 16:02:34 +00:00
|
|
|
require 'puma/control_cli'
|
|
|
|
|
2019-10-18 05:49:05 +00:00
|
|
|
class TestPumaControlCli < TestConfigFileBase
|
2019-11-02 18:51:46 +00:00
|
|
|
include SSLHelper
|
|
|
|
|
2018-05-09 18:38:12 +00:00
|
|
|
def setup
|
|
|
|
# use a pipe to get info across thread boundary
|
|
|
|
@wait, @ready = IO.pipe
|
|
|
|
end
|
|
|
|
|
|
|
|
def wait_booted
|
2022-09-30 06:06:32 +00:00
|
|
|
line = @wait.gets until line&.include?('Use Ctrl-C to stop')
|
2018-05-09 18:38:12 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
|
|
|
@wait.close
|
2022-06-02 18:10:03 +00:00
|
|
|
@ready.close unless @ready.closed?
|
2018-05-09 18:38:12 +00:00
|
|
|
end
|
|
|
|
|
2019-10-18 05:49:05 +00:00
|
|
|
def with_config_file(path_to_config, port)
|
|
|
|
path = Pathname.new(path_to_config)
|
|
|
|
Dir.mktmpdir do |tmp_dir|
|
|
|
|
Dir.chdir(tmp_dir) do
|
|
|
|
FileUtils.mkdir_p(path.dirname)
|
|
|
|
File.open(path, "w") { |f| f << "port #{port}" }
|
|
|
|
yield
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-06 07:15:05 +00:00
|
|
|
def test_blank_command
|
2020-10-28 13:09:37 +00:00
|
|
|
assert_system_exit_with_cli_output [], "Available commands: #{Puma::ControlCLI::CMD_PATH_SIG_MAP.keys.join(", ")}"
|
2020-05-06 07:15:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_invalid_command
|
|
|
|
assert_system_exit_with_cli_output ['an-invalid-command'], 'Invalid command: an-invalid-command'
|
|
|
|
end
|
|
|
|
|
2017-03-10 16:02:34 +00:00
|
|
|
def test_config_file
|
2017-03-10 16:30:27 +00:00
|
|
|
control_cli = Puma::ControlCLI.new ["--config-file", "test/config/state_file_testing_config.rb", "halt"]
|
2022-09-30 06:06:32 +00:00
|
|
|
assert_equal "t3-pid", control_cli.instance_variable_get(:@pidfile)
|
2021-12-14 15:06:36 +00:00
|
|
|
File.unlink "t3-pid" if File.file? "t3-pid"
|
2017-03-10 16:02:34 +00:00
|
|
|
end
|
2018-05-09 18:38:12 +00:00
|
|
|
|
2021-09-17 23:21:58 +00:00
|
|
|
def test_app_env_without_environment
|
|
|
|
with_env('APP_ENV' => 'test') do
|
|
|
|
control_cli = Puma::ControlCLI.new ['halt']
|
2022-09-30 06:06:32 +00:00
|
|
|
assert_equal 'test', control_cli.instance_variable_get(:@environment)
|
2021-09-17 23:21:58 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-18 05:49:05 +00:00
|
|
|
def test_rack_env_without_environment
|
|
|
|
with_env("RACK_ENV" => "test") do
|
|
|
|
control_cli = Puma::ControlCLI.new ["halt"]
|
2022-09-30 06:06:32 +00:00
|
|
|
assert_equal "test", control_cli.instance_variable_get(:@environment)
|
2019-10-18 05:49:05 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-17 23:21:58 +00:00
|
|
|
def test_app_env_precedence
|
|
|
|
with_env('APP_ENV' => nil, 'RACK_ENV' => nil, 'RAILS_ENV' => 'production') do
|
|
|
|
control_cli = Puma::ControlCLI.new ['halt']
|
2022-09-30 06:06:32 +00:00
|
|
|
assert_equal 'production', control_cli.instance_variable_get(:@environment)
|
2021-09-17 23:21:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
with_env('APP_ENV' => nil, 'RACK_ENV' => 'test', 'RAILS_ENV' => 'production') do
|
|
|
|
control_cli = Puma::ControlCLI.new ['halt']
|
2022-09-30 06:06:32 +00:00
|
|
|
assert_equal 'test', control_cli.instance_variable_get(:@environment)
|
2021-09-17 23:21:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
with_env('APP_ENV' => 'development', 'RACK_ENV' => 'test', 'RAILS_ENV' => 'production') do
|
|
|
|
control_cli = Puma::ControlCLI.new ['halt']
|
2022-09-30 06:06:32 +00:00
|
|
|
assert_equal 'development', control_cli.instance_variable_get(:@environment)
|
2021-09-17 23:21:58 +00:00
|
|
|
|
|
|
|
control_cli = Puma::ControlCLI.new ['-e', 'test', 'halt']
|
2022-09-30 06:06:32 +00:00
|
|
|
assert_equal 'test', control_cli.instance_variable_get(:@environment)
|
2021-09-17 23:21:58 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_environment_without_app_env
|
|
|
|
with_env('APP_ENV' => nil, 'RACK_ENV' => nil, 'RAILS_ENV' => nil) do
|
|
|
|
control_cli = Puma::ControlCLI.new ['halt']
|
2022-09-30 06:06:32 +00:00
|
|
|
assert_nil control_cli.instance_variable_get(:@environment)
|
2021-09-17 23:21:58 +00:00
|
|
|
|
|
|
|
control_cli = Puma::ControlCLI.new ['-e', 'test', 'halt']
|
2022-09-30 06:06:32 +00:00
|
|
|
assert_equal 'test', control_cli.instance_variable_get(:@environment)
|
2021-09-17 23:21:58 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-18 05:49:05 +00:00
|
|
|
def test_environment_without_rack_env
|
2019-11-11 05:10:02 +00:00
|
|
|
with_env("RACK_ENV" => nil, 'RAILS_ENV' => nil) do
|
2019-10-18 05:49:05 +00:00
|
|
|
control_cli = Puma::ControlCLI.new ["halt"]
|
2022-09-30 06:06:32 +00:00
|
|
|
assert_nil control_cli.instance_variable_get(:@environment)
|
2019-10-18 05:49:05 +00:00
|
|
|
|
|
|
|
control_cli = Puma::ControlCLI.new ["-e", "test", "halt"]
|
2022-09-30 06:06:32 +00:00
|
|
|
assert_equal "test", control_cli.instance_variable_get(:@environment)
|
2019-10-18 05:49:05 +00:00
|
|
|
end
|
2019-08-10 18:34:35 +00:00
|
|
|
end
|
|
|
|
|
2019-10-18 05:49:05 +00:00
|
|
|
def test_environment_with_rack_env
|
|
|
|
with_env("RACK_ENV" => "production") do
|
|
|
|
control_cli = Puma::ControlCLI.new ["halt"]
|
2022-09-30 06:06:32 +00:00
|
|
|
assert_equal "production", control_cli.instance_variable_get(:@environment)
|
2019-10-18 05:49:05 +00:00
|
|
|
|
|
|
|
control_cli = Puma::ControlCLI.new ["-e", "test", "halt"]
|
2022-09-30 06:06:32 +00:00
|
|
|
assert_equal "test", control_cli.instance_variable_get(:@environment)
|
2019-10-18 05:49:05 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_environment_specific_config_file_exist
|
|
|
|
port = 6002
|
|
|
|
puma_config_file = "config/puma.rb"
|
|
|
|
production_config_file = "config/puma/production.rb"
|
|
|
|
|
|
|
|
with_env("RACK_ENV" => nil) do
|
|
|
|
with_config_file(puma_config_file, port) do
|
|
|
|
control_cli = Puma::ControlCLI.new ["-e", "production", "halt"]
|
2022-09-30 06:06:32 +00:00
|
|
|
assert_equal puma_config_file, control_cli.instance_variable_get(:@config_file)
|
2019-10-18 05:49:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
with_config_file(production_config_file, port) do
|
|
|
|
control_cli = Puma::ControlCLI.new ["-e", "production", "halt"]
|
2022-09-30 06:06:32 +00:00
|
|
|
assert_equal production_config_file, control_cli.instance_variable_get(:@config_file)
|
2019-10-18 05:49:05 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_default_config_file_exist
|
2019-08-10 18:34:35 +00:00
|
|
|
port = 6001
|
2019-10-18 05:49:05 +00:00
|
|
|
puma_config_file = "config/puma.rb"
|
|
|
|
development_config_file = "config/puma/development.rb"
|
|
|
|
|
2019-11-11 05:10:02 +00:00
|
|
|
with_env("RACK_ENV" => nil, 'RAILS_ENV' => nil) do
|
2019-10-18 05:49:05 +00:00
|
|
|
with_config_file(puma_config_file, port) do
|
2019-08-10 18:34:35 +00:00
|
|
|
control_cli = Puma::ControlCLI.new ["halt"]
|
2022-09-30 06:06:32 +00:00
|
|
|
assert_equal puma_config_file, control_cli.instance_variable_get(:@config_file)
|
2019-08-10 18:34:35 +00:00
|
|
|
end
|
2019-10-18 05:49:05 +00:00
|
|
|
|
|
|
|
with_config_file(development_config_file, port) do
|
2019-08-10 18:34:35 +00:00
|
|
|
control_cli = Puma::ControlCLI.new ["halt"]
|
2022-09-30 06:06:32 +00:00
|
|
|
assert_equal development_config_file, control_cli.instance_variable_get(:@config_file)
|
2019-08-10 18:34:35 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-05-28 13:37:34 +00:00
|
|
|
def test_control_no_token
|
|
|
|
opts = [
|
|
|
|
"--config-file", "test/config/control_no_token.rb",
|
|
|
|
"start"
|
|
|
|
]
|
|
|
|
|
|
|
|
control_cli = Puma::ControlCLI.new opts, @ready, @ready
|
2022-09-30 06:06:32 +00:00
|
|
|
assert_equal 'none', control_cli.instance_variable_get(:@control_auth_token)
|
2019-05-28 13:37:34 +00:00
|
|
|
end
|
|
|
|
|
2019-06-24 00:36:06 +00:00
|
|
|
def test_control_url_and_status
|
2018-05-09 18:38:12 +00:00
|
|
|
host = "127.0.0.1"
|
2020-03-14 13:32:00 +00:00
|
|
|
port = UniquePort.call
|
2018-05-09 18:38:12 +00:00
|
|
|
url = "tcp://#{host}:#{port}/"
|
|
|
|
|
|
|
|
opts = [
|
|
|
|
"--control-url", url,
|
|
|
|
"--control-token", "ctrl",
|
|
|
|
"--config-file", "test/config/app.rb",
|
|
|
|
]
|
|
|
|
|
|
|
|
control_cli = Puma::ControlCLI.new (opts + ["start"]), @ready, @ready
|
|
|
|
t = Thread.new do
|
|
|
|
control_cli.run
|
|
|
|
end
|
|
|
|
|
|
|
|
wait_booted
|
|
|
|
|
|
|
|
s = TCPSocket.new host, 9292
|
|
|
|
s << "GET / HTTP/1.0\r\n\r\n"
|
|
|
|
body = s.read
|
|
|
|
assert_match "200 OK", body
|
|
|
|
assert_match "embedded app", body
|
|
|
|
|
2019-11-02 18:51:46 +00:00
|
|
|
assert_command_cli_output opts + ["status"], "Puma is started"
|
|
|
|
assert_command_cli_output opts + ["stop"], "Command stop sent success"
|
2018-05-09 18:38:12 +00:00
|
|
|
|
2019-11-02 18:51:46 +00:00
|
|
|
assert_kind_of Thread, t.join, "server didn't stop"
|
|
|
|
end
|
|
|
|
|
2022-06-02 18:10:03 +00:00
|
|
|
# This checks that a 'signal only' command is sent
|
|
|
|
# they are defined by the `Puma::ControlCLI::NO_REQ_COMMANDS` array
|
|
|
|
# test is skipped unless NO_REQ_COMMANDS is defined
|
|
|
|
def test_control_url_with_signal_only_cmd
|
|
|
|
skip_if :windows
|
|
|
|
skip unless defined? Puma::ControlCLI::NO_REQ_COMMANDS
|
|
|
|
host = "127.0.0.1"
|
|
|
|
port = UniquePort.call
|
|
|
|
url = "tcp://#{host}:#{port}/"
|
|
|
|
|
|
|
|
opts = [
|
|
|
|
"--control-url", url,
|
|
|
|
"--control-token", "ctrl",
|
|
|
|
"--config-file", "test/config/app.rb",
|
|
|
|
"--pid", "1234"
|
|
|
|
]
|
|
|
|
cmd = Puma::ControlCLI::NO_REQ_COMMANDS.first
|
2022-09-30 06:06:32 +00:00
|
|
|
log = +''
|
2022-06-02 18:10:03 +00:00
|
|
|
control_cli = Puma::ControlCLI.new (opts + [cmd]), @ready, @ready
|
|
|
|
|
|
|
|
def control_cli.send_signal
|
|
|
|
message "send_signal #{@command}\n"
|
|
|
|
end
|
|
|
|
def control_cli.send_request
|
|
|
|
message "send_request #{@command}\n"
|
|
|
|
end
|
|
|
|
|
|
|
|
control_cli.run
|
|
|
|
@ready.close
|
|
|
|
|
|
|
|
log = @wait.read
|
|
|
|
|
|
|
|
assert_includes log, "send_signal #{cmd}"
|
|
|
|
refute_includes log, 'send_request'
|
|
|
|
end
|
|
|
|
|
2019-11-02 18:51:46 +00:00
|
|
|
def test_control_ssl
|
2021-03-15 14:10:43 +00:00
|
|
|
skip_unless :ssl
|
2020-07-06 01:57:35 +00:00
|
|
|
|
2019-11-02 18:51:46 +00:00
|
|
|
host = "127.0.0.1"
|
2020-03-14 13:32:00 +00:00
|
|
|
port = UniquePort.call
|
2019-11-02 18:51:46 +00:00
|
|
|
url = "ssl://#{host}:#{port}?#{ssl_query}"
|
|
|
|
|
|
|
|
opts = [
|
2021-04-24 23:24:44 +00:00
|
|
|
"--control-url", url,
|
|
|
|
"--control-token", "ctrl",
|
|
|
|
"--config-file", "test/config/app.rb",
|
|
|
|
]
|
|
|
|
|
|
|
|
control_cli = Puma::ControlCLI.new (opts + ["start"]), @ready, @ready
|
|
|
|
t = Thread.new do
|
|
|
|
control_cli.run
|
|
|
|
end
|
|
|
|
|
|
|
|
wait_booted
|
|
|
|
|
|
|
|
assert_command_cli_output opts + ["status"], "Puma is started"
|
|
|
|
assert_command_cli_output opts + ["stop"], "Command stop sent success"
|
|
|
|
|
|
|
|
assert_kind_of Thread, t.join, "server didn't stop"
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_control_aunix
|
|
|
|
skip_unless :aunix
|
|
|
|
|
|
|
|
url = "unix://@test_control_aunix.unix"
|
|
|
|
|
|
|
|
opts = [
|
2019-11-02 18:51:46 +00:00
|
|
|
"--control-url", url,
|
|
|
|
"--control-token", "ctrl",
|
|
|
|
"--config-file", "test/config/app.rb",
|
|
|
|
]
|
|
|
|
|
|
|
|
control_cli = Puma::ControlCLI.new (opts + ["start"]), @ready, @ready
|
|
|
|
t = Thread.new do
|
|
|
|
control_cli.run
|
2019-06-24 00:36:06 +00:00
|
|
|
end
|
2019-11-02 18:51:46 +00:00
|
|
|
|
|
|
|
wait_booted
|
|
|
|
|
|
|
|
assert_command_cli_output opts + ["status"], "Puma is started"
|
|
|
|
assert_command_cli_output opts + ["stop"], "Command stop sent success"
|
2018-05-09 18:38:12 +00:00
|
|
|
|
2019-06-24 00:36:06 +00:00
|
|
|
assert_kind_of Thread, t.join, "server didn't stop"
|
2018-05-09 18:38:12 +00:00
|
|
|
end
|
2019-11-02 18:51:46 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def assert_command_cli_output(options, expected_out)
|
|
|
|
cmd = Puma::ControlCLI.new(options)
|
|
|
|
out, _ = capture_subprocess_io do
|
2020-08-07 10:52:31 +00:00
|
|
|
begin
|
|
|
|
cmd.run
|
2020-10-22 19:54:57 +00:00
|
|
|
rescue SystemExit
|
2020-08-07 10:52:31 +00:00
|
|
|
end
|
2019-11-02 18:51:46 +00:00
|
|
|
end
|
|
|
|
assert_match expected_out, out
|
|
|
|
end
|
2020-05-06 07:15:05 +00:00
|
|
|
|
|
|
|
def assert_system_exit_with_cli_output(options, expected_out)
|
|
|
|
out, _ = capture_subprocess_io do
|
|
|
|
response = assert_raises(SystemExit) do
|
|
|
|
Puma::ControlCLI.new(options).run
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal(response.status, 1)
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_match expected_out, out
|
|
|
|
end
|
2017-03-10 16:02:34 +00:00
|
|
|
end
|