-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
test_cli.rb
417 lines (304 loc) · 12.1 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
require_relative "helper"
require_relative "helpers/ssl" if ::Puma::HAS_SSL
require_relative "helpers/tmp_path"
require_relative "helpers/test_puma/puma_socket"
require "puma/cli"
require "json"
require "psych"
class TestCLI < Minitest::Test
include SSLHelper if ::Puma::HAS_SSL
include TmpPath
include TestPuma::PumaSocket
def setup
@environment = 'production'
@tmp_path = tmp_path('puma-test')
@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
@log_writer = Puma::LogWriter.strings
@events = Puma::Events.new
@events.on_booted { @ready << "!" }
@puma_version_pattern = "\\d+.\\d+.\\d+(\\.[a-z\\d]+)?"
end
def wait_booted
@wait.sysread 1
rescue Errno::EAGAIN
sleep 0.001
retry
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
end
def test_control_for_tcp
control_port = UniquePort.call
url = "tcp://127.0.0.1:#{control_port}/"
cli = Puma::CLI.new ["-b", "tcp://127.0.0.1:0",
"--control-url", url,
"--control-token", "",
"test/rackup/hello.ru"], @log_writer, @events
t = Thread.new { cli.run }
wait_booted
body = send_http_read_resp_body "GET /stats HTTP/1.0\r\n\r\n", port: control_port
assert_equal Puma.stats_hash, JSON.parse(Puma.stats, symbolize_names: true)
dmt = Puma::Configuration::DEFAULTS[:max_threads]
expected_stats = /\{"started_at":"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z","backlog":0,"running":0,"pool_capacity":#{dmt},"max_threads":#{dmt},"requests_count":0,"versions":\{"puma":"#{@puma_version_pattern}","ruby":\{"engine":"\w+","version":"\d+.\d+.\d+","patchlevel":-?\d+\}\}\}/
assert_match(expected_stats, body)
ensure
cli.launcher.stop
t.join
end
def test_control_for_ssl
skip_unless :ssl
require "net/http"
control_port = UniquePort.call
control_host = "127.0.0.1"
control_url = "ssl://#{control_host}:#{control_port}?#{ssl_query}"
token = "token"
cli = Puma::CLI.new ["-b", "tcp://127.0.0.1:0",
"--control-url", control_url,
"--control-token", token,
"test/rackup/hello.ru"], @log_writer, @events
t = Thread.new { cli.run }
wait_booted
body = send_http_read_resp_body "GET /stats?token=#{token} HTTP/1.0\r\n\r\n",
port: control_port, ctx: new_ctx
dmt = Puma::Configuration::DEFAULTS[:max_threads]
expected_stats = /{"started_at":"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z","backlog":0,"running":0,"pool_capacity":#{dmt},"max_threads":#{dmt}/
assert_match(expected_stats, body)
ensure
# always called, even if skipped
cli&.launcher&.stop
t&.join
end
def test_control_clustered
skip_unless :fork
skip_unless :unix
url = "unix://#{@tmp_path}"
cli = Puma::CLI.new ["-b", "unix://#{@tmp_path2}",
"-t", "2:2",
"-w", "2",
"--control-url", url,
"--control-token", "",
"test/rackup/hello.ru"], @log_writer, @events
# without this, Minitest.after_run will trigger on this test ?
$debugging_hold = true
t = Thread.new { cli.run }
wait_booted
body = send_http_read_resp_body "GET /stats HTTP/1.0\r\n\r\n", path: @tmp_path
status = JSON.parse(body)
assert_equal 2, status["workers"]
body = send_http_read_resp_body "GET /stats HTTP/1.0\r\n\r\n", path: @tmp_path
expected_stats = /\{"started_at":"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z","workers":2,"phase":0,"booted_workers":2,"old_workers":0,"worker_status":\[\{"started_at":"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z","pid":\d+,"index":0,"phase":0,"booted":true,"last_checkin":"[^"]+","last_status":\{"backlog":0,"running":2,"pool_capacity":2,"max_threads":2,"requests_count":0\}\},\{"started_at":"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z","pid":\d+,"index":1,"phase":0,"booted":true,"last_checkin":"[^"]+","last_status":\{"backlog":0,"running":2,"pool_capacity":2,"max_threads":2,"requests_count":0\}\}\],"versions":\{"puma":"#{@puma_version_pattern}","ruby":\{"engine":"\w+","version":"\d+.\d+.\d+","patchlevel":-?\d+\}\}\}/
assert_match(expected_stats, body)
ensure
if UNIX_SKT_EXIST && HAS_FORK
cli.launcher.stop
t.join
done = nil
until done
@log_writer.stdout.rewind
log = @log_writer.stdout.readlines.join ''
done = log[/ - Goodbye!/]
end
$debugging_hold = false
end
end
def test_control
skip_unless :unix
url = "unix://#{@tmp_path}"
cli = Puma::CLI.new ["-b", "unix://#{@tmp_path2}",
"--control-url", url,
"--control-token", "",
"test/rackup/hello.ru"], @log_writer, @events
t = Thread.new { cli.run }
wait_booted
body = send_http_read_resp_body "GET /stats HTTP/1.0\r\n\r\n", path: @tmp_path
dmt = Puma::Configuration::DEFAULTS[:max_threads]
expected_stats = /{"started_at":"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z","backlog":0,"running":0,"pool_capacity":#{dmt},"max_threads":#{dmt},"requests_count":0,"versions":\{"puma":"#{@puma_version_pattern}","ruby":\{"engine":"\w+","version":"\d+.\d+.\d+","patchlevel":-?\d+\}\}\}/
assert_match(expected_stats, body)
ensure
if UNIX_SKT_EXIST
cli.launcher.stop
t.join
end
end
def test_control_stop
skip_unless :unix
url = "unix://#{@tmp_path}"
cli = Puma::CLI.new ["-b", "unix://#{@tmp_path2}",
"--control-url", url,
"--control-token", "",
"test/rackup/hello.ru"], @log_writer, @events
t = Thread.new { cli.run }
wait_booted
body = send_http_read_resp_body "GET /stop HTTP/1.0\r\n\r\n", path: @tmp_path
assert_equal '{ "status": "ok" }', body
ensure
t.join if UNIX_SKT_EXIST
end
def test_control_requests_count
@bind_port = UniquePort.call
control_port = UniquePort.call
url = "tcp://127.0.0.1:#{control_port}/"
cli = Puma::CLI.new ["-b", "tcp://127.0.0.1:#{@bind_port}",
"--control-url", url,
"--control-token", "",
"test/rackup/hello.ru"], @log_writer, @events
t = Thread.new { cli.run }
wait_booted
body = send_http_read_resp_body "GET /stats HTTP/1.0\r\n\r\n", port: control_port
assert_equal 0, JSON.parse(body)['requests_count']
# send real requests to server
3.times { send_http_read_resp_body GET_10 }
body = send_http_read_resp_body "GET /stats HTTP/1.0\r\n\r\n", port: control_port
assert_equal 3, JSON.parse(body)['requests_count']
ensure
cli.launcher.stop
t.join
end
def test_control_thread_backtraces
skip_unless :unix
url = "unix://#{@tmp_path}"
cli = Puma::CLI.new ["-b", "unix://#{@tmp_path2}",
"--control-url", url,
"--control-token", "",
"test/rackup/hello.ru"], @log_writer, @events
t = Thread.new { cli.run }
wait_booted
if TRUFFLE
Thread.pass
sleep 0.2
end
# All thread backtraces may be very large, just get a chunk
socket = send_http "GET /thread-backtraces HTTP/1.0\r\n\r\n", path: @tmp_path
socket.wait_readable 3
body = socket.sysread 32_768
assert_match %r{Thread: TID-}, body
ensure
cli.launcher.stop if cli
t.join if UNIX_SKT_EXIST
end
def test_tmp_control
skip_if :jruby, suffix: " - Unknown issue"
cli = Puma::CLI.new ["--state", @tmp_path, "--control-url", "auto"]
cli.launcher.write_state
opts = cli.launcher.instance_variable_get(:@options)
data = Psych.load_file @tmp_path
Puma::StateFile::ALLOWED_FIELDS.each do |key|
val =
case key
when 'pid' then Process.pid
when 'running_from' then File.expand_path('.') # same as Launcher
else opts[key.to_sym]
end
assert_equal val, data[key]
end
assert_equal (Puma::StateFile::ALLOWED_FIELDS & data.keys).sort, data.keys.sort
url = data["control_url"]
assert_operator url, :start_with?, "unix://", "'#{url}' is not a URL"
end
def test_state_file_callback_filtering
skip_unless :fork
cli = Puma::CLI.new [ "--config", "test/config/state_file_testing_config.rb",
"--state", @tmp_path ]
cli.launcher.write_state
data = Psych.load_file @tmp_path
assert_equal (Puma::StateFile::ALLOWED_FIELDS & data.keys).sort, data.keys.sort
end
def test_log_formatter_default_single
cli = Puma::CLI.new [ ]
assert_instance_of Puma::LogWriter::DefaultFormatter, cli.launcher.log_writer.formatter
end
def test_log_formatter_default_clustered
skip_unless :fork
cli = Puma::CLI.new [ "-w 2" ]
assert_instance_of Puma::LogWriter::PidFormatter, cli.launcher.log_writer.formatter
end
def test_log_formatter_custom_single
cli = Puma::CLI.new [ "--config", "test/config/custom_log_formatter.rb" ]
assert_instance_of Proc, cli.launcher.log_writer.formatter
assert_match(/^\[.*\] \[.*\] .*: test$/, cli.launcher.log_writer.format('test'))
end
def test_log_formatter_custom_clustered
skip_unless :fork
cli = Puma::CLI.new [ "--config", "test/config/custom_log_formatter.rb", "-w 2" ]
assert_instance_of Proc, cli.launcher.log_writer.formatter
assert_match(/^\[.*\] \[.*\] .*: test$/, cli.launcher.log_writer.format('test'))
end
def test_state
url = "tcp://127.0.0.1:#{UniquePort.call}"
cli = Puma::CLI.new ["--state", @tmp_path, "--control-url", url]
cli.launcher.write_state
data = Psych.load_file @tmp_path
assert_equal Process.pid, data["pid"]
assert_equal url, data["control_url"]
end
def test_load_path
Puma::CLI.new ["--include", 'foo/bar']
assert_equal 'foo/bar', $LOAD_PATH[0]
$LOAD_PATH.shift
Puma::CLI.new ["--include", 'foo/bar:baz/qux']
assert_equal 'foo/bar', $LOAD_PATH[0]
$LOAD_PATH.shift
assert_equal 'baz/qux', $LOAD_PATH[0]
$LOAD_PATH.shift
end
def test_extra_runtime_dependencies
cli = Puma::CLI.new ['--extra-runtime-dependencies', 'a,b']
extra_dependencies = cli.instance_variable_get(:@conf)
.instance_variable_get(:@options)[:extra_runtime_dependencies]
assert_equal %w[a b], extra_dependencies
end
def test_environment_app_env
ENV['RACK_ENV'] = @environment
ENV['RAILS_ENV'] = @environment
ENV['APP_ENV'] = 'test'
cli = Puma::CLI.new []
assert_equal 'test', cli.instance_variable_get(:@conf).environment
ensure
ENV.delete 'APP_ENV'
ENV.delete 'RAILS_ENV'
end
def test_environment_rack_env
ENV['RACK_ENV'] = @environment
cli = Puma::CLI.new []
assert_equal @environment, cli.instance_variable_get(:@conf).environment
end
def test_environment_rails_env
ENV.delete 'RACK_ENV'
ENV['RAILS_ENV'] = @environment
cli = Puma::CLI.new []
assert_equal @environment, cli.instance_variable_get(:@conf).environment
ensure
ENV.delete 'RAILS_ENV'
end
def test_silent
cli = Puma::CLI.new ['--silent']
log_writer = cli.instance_variable_get(:@log_writer)
assert_equal log_writer.class, Puma::LogWriter.null.class
assert_equal log_writer.stdout.class, Puma::NullIO
assert_equal log_writer.stderr, $stderr
end
def test_plugins
assert_empty Puma::Plugins.instance_variable_get(:@plugins)
cli = Puma::CLI.new ['--plugin', 'tmp_restart', '--plugin', 'systemd']
assert Puma::Plugins.find("tmp_restart")
assert Puma::Plugins.find("systemd")
end
def test_config_does_not_preload_app_with_workers
skip_unless :fork
cli = Puma::CLI.new ['-w 0']
config = Puma.cli_config
assert_equal false, config.options[:preload_app]
end
def test_config_preloads_app_with_workers
skip_unless :fork
cli = Puma::CLI.new ['-w 2']
config = Puma.cli_config
assert_equal true, config.options[:preload_app]
end
end