/
test_cli.rb
171 lines (128 loc) · 3.91 KB
/
test_cli.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
require "rbconfig"
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
end
def teardown
File.unlink @tmp_path if File.exist? @tmp_path
File.unlink @tmp_path2 if File.exist? @tmp_path2
end
def test_pid_file
cli = Puma::CLI.new ["--pidfile", @tmp_path]
cli.parse_options
cli.write_pid
assert_equal File.read(@tmp_path).strip.to_i, Process.pid
cli.stop
assert !File.exist?(@tmp_path), "Pid file shouldn't exist anymore"
end
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
unless defined?(JRUBY_VERSION) || RbConfig::CONFIG["host_os"] =~ /mingw|mswin/
def test_control
url = "unix://#{@tmp_path}"
sin = StringIO.new
sout = StringIO.new
cli = Puma::CLI.new ["-b", "unix://#{@tmp_path2}",
"--control", url,
"--control-token", "",
"test/lobster.ru"], sin, sout
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
def test_control_stop
url = "unix://#{@tmp_path}"
sin = StringIO.new
sout = StringIO.new
cli = Puma::CLI.new ["-b", "unix://#{@tmp_path2}",
"--control", url,
"--control-token", "",
"test/lobster.ru"], sin, sout
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
def test_tmp_control
url = "tcp://127.0.0.1:8232"
cli = Puma::CLI.new ["--state", @tmp_path, "--control", "auto"]
cli.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
end # JRUBY or Windows
def test_state
url = "tcp://127.0.0.1:8232"
cli = Puma::CLI.new ["--state", @tmp_path, "--control", url]
cli.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']
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
def test_environment
cli = Puma::CLI.new ["--environment", @environment]
cli.parse_options
cli.set_rack_environment
assert_equal ENV['RACK_ENV'], @environment
end
end