1
0
Fork 0
mirror of https://github.com/puma/puma.git synced 2022-11-09 13:48:40 -05:00
puma--puma/test/test_cli.rb

190 lines
4.4 KiB
Ruby
Raw Normal View History

require "rbconfig"
2011-11-22 00:15:40 -05:00
require 'test/unit'
require 'puma/cli'
require 'tempfile'
class TestCLI < Test::Unit::TestCase
def setup
@environment = 'production'
@tmp_file = Tempfile.new("puma-test")
@tmp_path = @tmp_file.path
@tmp_file.close!
@tmp_path2 = "#{@tmp_path}2"
File.unlink @tmp_path if File.exist? @tmp_path
File.unlink @tmp_path2 if File.exist? @tmp_path2
@wait, @ready = IO.pipe
@events = Events.strings
@events.on_booted { @ready << "!" }
end
def wait_booted
@wait.sysread 1
end
def teardown
File.unlink @tmp_path if File.exist? @tmp_path
File.unlink @tmp_path2 if File.exist? @tmp_path2
@wait.close
@ready.close
2011-11-22 00:15:40 -05:00
end
def test_pid_file
cli = Puma::CLI.new ["--pidfile", @tmp_path]
2015-03-13 21:23:09 -04:00
cli.send(:parse_options)
2011-11-22 00:15:40 -05:00
cli.write_pid
assert_equal File.read(@tmp_path).strip.to_i, Process.pid
end
2012-06-21 12:01:18 -04:00
def test_control_for_tcp
url = "tcp://127.0.0.1:9877/"
cli = Puma::CLI.new ["-b", "tcp://127.0.0.1:9876",
"--control", url,
"--control-token", "",
"test/lobster.ru"], @events
2015-03-13 21:23:09 -04:00
cli.send(:parse_options)
2012-06-21 12:01:18 -04:00
thread_exception = nil
t = Thread.new do
begin
cli.run
rescue Exception => e
thread_exception = e
end
2015-03-13 19:08:07 -04:00
end
2012-06-21 12:01:18 -04:00
wait_booted
2012-06-21 12:01:18 -04:00
s = TCPSocket.new "127.0.0.1", 9877
s << "GET /stats HTTP/1.0\r\n\r\n"
body = s.read
assert_equal '{ "backlog": 0, "running": 0 }', body.split("\r\n").last
cli.stop
t.join
assert_equal nil, thread_exception
end
unless defined?(JRUBY_VERSION) || RbConfig::CONFIG["host_os"] =~ /mingw|mswin/
def test_control
url = "unix://#{@tmp_path}"
cli = Puma::CLI.new ["-b", "unix://#{@tmp_path2}",
"--control", url,
"--control-token", "",
"test/lobster.ru"], @events
2015-03-13 21:23:09 -04:00
cli.send(:parse_options)
t = Thread.new { cli.run }
wait_booted
s = UNIXSocket.new @tmp_path
s << "GET /stats HTTP/1.0\r\n\r\n"
body = s.read
assert_equal '{ "backlog": 0, "running": 0 }', body.split("\r\n").last
cli.stop
t.join
end
def test_control_stop
url = "unix://#{@tmp_path}"
cli = Puma::CLI.new ["-b", "unix://#{@tmp_path2}",
"--control", url,
"--control-token", "",
"test/lobster.ru"], @events
2015-03-13 21:23:09 -04:00
cli.send(:parse_options)
t = Thread.new { cli.run }
wait_booted
s = UNIXSocket.new @tmp_path
s << "GET /stop HTTP/1.0\r\n\r\n"
body = s.read
assert_equal '{ "status": "ok" }', body.split("\r\n").last
t.join
end
def test_tmp_control
url = "tcp://127.0.0.1:8232"
cli = Puma::CLI.new ["--state", @tmp_path, "--control", "auto"]
2015-03-13 21:23:09 -04:00
cli.send(:parse_options)
cli.write_state
data = YAML.load File.read(@tmp_path)
assert_equal Process.pid, data["pid"]
url = data["config"].options[:control_url]
m = %r!unix://(.*)!.match(url)
assert m, "'#{url}' is not a URL"
end
def test_state_file_callback_filtering
cli = Puma::CLI.new [ "--config", "test/config/state_file_testing_config.rb",
"--state", @tmp_path ]
cli.send( :parse_options )
cli.write_state
data = nil
assert_nothing_raised do
data = YAML.load_file( @tmp_path )
end
keys_not_stripped = data.keys & Puma::CLI::KEYS_NOT_TO_PERSIST_IN_STATE
assert_empty keys_not_stripped
end
2016-01-28 00:08:04 -05:00
end # JRUBY or Windows
def test_state
url = "tcp://127.0.0.1:8232"
cli = Puma::CLI.new ["--state", @tmp_path, "--control", url]
cli.send(:parse_options)
cli.write_state
data = YAML.load File.read(@tmp_path)
assert_equal Process.pid, data["pid"]
assert_equal url, data["config"].options[:control_url]
end
def test_load_path
cli = Puma::CLI.new ["--include", 'foo/bar']
2015-03-13 21:23:09 -04:00
cli.send(:parse_options)
assert_equal 'foo/bar', $LOAD_PATH[0]
$LOAD_PATH.shift
cli = Puma::CLI.new ["--include", 'foo/bar:baz/qux']
2015-03-13 21:23:09 -04:00
cli.send(:parse_options)
assert_equal 'foo/bar', $LOAD_PATH[0]
$LOAD_PATH.shift
assert_equal 'baz/qux', $LOAD_PATH[0]
$LOAD_PATH.shift
end
def test_environment
cli = Puma::CLI.new ["--environment", @environment]
2015-03-13 21:23:09 -04:00
cli.send(:parse_options)
2015-03-13 19:08:07 -04:00
cli.send(:set_rack_environment)
2015-03-13 19:08:07 -04:00
assert_equal ENV['RACK_ENV'], @environment
end
2011-11-22 00:15:40 -05:00
end