2012-01-08 14:57:55 -05:00
|
|
|
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
|
2012-07-05 16:38:06 -04:00
|
|
|
@environment = 'production'
|
2011-12-05 14:15:44 -05:00
|
|
|
@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
|
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
|
|
|
File.unlink @tmp_path if File.exist? @tmp_path
|
|
|
|
File.unlink @tmp_path2 if File.exist? @tmp_path2
|
2011-11-22 00:15:40 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_pid_file
|
2011-12-05 14:15:44 -05:00
|
|
|
cli = Puma::CLI.new ["--pidfile", @tmp_path]
|
2011-11-22 00:15:40 -05:00
|
|
|
cli.parse_options
|
|
|
|
cli.write_pid
|
|
|
|
|
2011-12-05 14:15:44 -05:00
|
|
|
assert_equal File.read(@tmp_path).strip.to_i, Process.pid
|
2012-07-19 14:11:23 -04:00
|
|
|
|
|
|
|
cli.stop
|
|
|
|
assert !File.exist?(@tmp_path), "Pid file shouldn't exist anymore"
|
2011-12-05 14:15:44 -05:00
|
|
|
end
|
|
|
|
|
2012-06-21 12:01:18 -04:00
|
|
|
def test_control_for_tcp
|
|
|
|
url = "tcp://127.0.0.1:9877/"
|
|
|
|
sin = StringIO.new
|
|
|
|
sout = StringIO.new
|
|
|
|
cli = Puma::CLI.new ["-b", "tcp://127.0.0.1:9876",
|
|
|
|
"--control", url,
|
|
|
|
"--control-token", "",
|
|
|
|
"test/lobster.ru"], sin, sout
|
|
|
|
cli.parse_options
|
|
|
|
|
|
|
|
thread_exception = nil
|
|
|
|
t = Thread.new do
|
|
|
|
begin
|
|
|
|
cli.run
|
|
|
|
rescue Exception => e
|
|
|
|
thread_exception = e
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
sleep 1
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2012-01-08 14:57:55 -05:00
|
|
|
unless defined?(JRUBY_VERSION) || RbConfig::CONFIG["host_os"] =~ /mingw|mswin/
|
2011-12-06 17:56:38 -05:00
|
|
|
def test_control
|
2011-12-05 14:15:44 -05:00
|
|
|
url = "unix://#{@tmp_path}"
|
|
|
|
|
|
|
|
sin = StringIO.new
|
|
|
|
sout = StringIO.new
|
|
|
|
|
2011-12-07 13:46:36 -05:00
|
|
|
cli = Puma::CLI.new ["-b", "unix://#{@tmp_path2}",
|
|
|
|
"--control", url,
|
|
|
|
"--control-token", "",
|
|
|
|
"test/lobster.ru"], sin, sout
|
2011-12-05 14:15:44 -05:00
|
|
|
cli.parse_options
|
|
|
|
|
|
|
|
t = Thread.new { cli.run }
|
|
|
|
|
|
|
|
sleep 1
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2011-12-06 17:56:38 -05:00
|
|
|
def test_control_stop
|
2011-12-05 14:15:44 -05:00
|
|
|
url = "unix://#{@tmp_path}"
|
|
|
|
|
|
|
|
sin = StringIO.new
|
|
|
|
sout = StringIO.new
|
|
|
|
|
2011-12-07 13:46:36 -05:00
|
|
|
cli = Puma::CLI.new ["-b", "unix://#{@tmp_path2}",
|
|
|
|
"--control", url,
|
|
|
|
"--control-token", "",
|
|
|
|
"test/lobster.ru"], sin, sout
|
2011-12-05 14:15:44 -05:00
|
|
|
cli.parse_options
|
|
|
|
|
|
|
|
t = Thread.new { cli.run }
|
|
|
|
|
|
|
|
sleep 1
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2011-12-06 17:56:38 -05:00
|
|
|
def test_tmp_control
|
2011-12-05 14:32:18 -05:00
|
|
|
url = "tcp://127.0.0.1:8232"
|
2011-12-06 17:56:38 -05:00
|
|
|
cli = Puma::CLI.new ["--state", @tmp_path, "--control", "auto"]
|
2011-12-05 14:32:18 -05:00
|
|
|
cli.parse_options
|
|
|
|
cli.write_state
|
|
|
|
|
2012-01-08 15:46:10 -05:00
|
|
|
data = YAML.load File.read(@tmp_path)
|
2011-12-05 14:32:18 -05:00
|
|
|
|
|
|
|
assert_equal Process.pid, data["pid"]
|
|
|
|
|
2011-12-06 17:56:38 -05:00
|
|
|
url = data["config"].options[:control_url]
|
2011-12-05 14:32:18 -05:00
|
|
|
|
|
|
|
m = %r!unix://(.*)!.match(url)
|
|
|
|
|
|
|
|
assert m, "'#{url}' is not a URL"
|
|
|
|
end
|
2012-01-08 14:57:55 -05:00
|
|
|
end # JRUBY or Windows
|
2011-12-05 14:32:18 -05:00
|
|
|
|
2011-12-05 14:15:44 -05:00
|
|
|
def test_state
|
|
|
|
url = "tcp://127.0.0.1:8232"
|
2011-12-06 17:56:38 -05:00
|
|
|
cli = Puma::CLI.new ["--state", @tmp_path, "--control", url]
|
2011-12-05 14:15:44 -05:00
|
|
|
cli.parse_options
|
|
|
|
cli.write_state
|
|
|
|
|
2012-01-08 15:46:10 -05:00
|
|
|
data = YAML.load File.read(@tmp_path)
|
2011-12-05 14:15:44 -05:00
|
|
|
|
|
|
|
assert_equal Process.pid, data["pid"]
|
2011-12-06 17:56:38 -05:00
|
|
|
assert_equal url, data["config"].options[:control_url]
|
2011-11-22 00:15:40 -05:00
|
|
|
end
|
2012-04-30 19:44:03 -04:00
|
|
|
|
|
|
|
def test_load_path
|
|
|
|
cli = Puma::CLI.new ["--include", 'foo/bar']
|
|
|
|
cli.parse_options
|
|
|
|
|
|
|
|
assert_equal 'foo/bar', $LOAD_PATH[0]
|
|
|
|
$LOAD_PATH.shift
|
|
|
|
|
|
|
|
cli = Puma::CLI.new ["--include", 'foo/bar:baz/qux']
|
|
|
|
cli.parse_options
|
|
|
|
|
|
|
|
assert_equal 'foo/bar', $LOAD_PATH[0]
|
|
|
|
$LOAD_PATH.shift
|
|
|
|
assert_equal 'baz/qux', $LOAD_PATH[0]
|
|
|
|
$LOAD_PATH.shift
|
|
|
|
end
|
2012-07-05 16:38:06 -04:00
|
|
|
|
|
|
|
def test_environment
|
|
|
|
cli = Puma::CLI.new ["--environment", @environment]
|
|
|
|
cli.parse_options
|
|
|
|
cli.set_rack_environment
|
|
|
|
|
|
|
|
assert_equal ENV['RACK_ENV'], @environment
|
|
|
|
end
|
2011-11-22 00:15:40 -05:00
|
|
|
end
|