/
test_config.rb
508 lines (395 loc) · 13.3 KB
/
test_config.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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
# frozen_string_literal: true
require_relative "helper"
require_relative "helpers/config_file"
require "puma/configuration"
require 'puma/events'
class TestConfigFile < TestConfigFileBase
parallelize_me!
def test_default_max_threads
max_threads = 16
max_threads = 5 if RUBY_ENGINE.nil? || RUBY_ENGINE == 'ruby'
assert_equal max_threads, Puma::Configuration.new.default_max_threads
end
def test_app_from_rackup
conf = Puma::Configuration.new do |c|
c.rackup "test/rackup/hello-bind.ru"
end
conf.load
# suppress deprecation warning of Rack (>= 2.2.0)
# > Parsing options from the first comment line is deprecated!\n
assert_output(nil, nil) do
conf.app
end
assert_equal ["tcp://127.0.0.1:9292"], conf.options[:binds]
end
def test_app_from_app_DSL
conf = Puma::Configuration.new do |c|
c.load "test/config/app.rb"
end
conf.load
app = conf.app
assert_equal [200, {}, ["embedded app"]], app.call({})
end
def test_ssl_configuration_from_DSL
skip_unless :ssl
conf = Puma::Configuration.new do |config|
config.load "test/config/ssl_config.rb"
end
conf.load
bind_configuration = conf.options.file_options[:binds].first
app = conf.app
assert bind_configuration =~ %r{ca=.*ca.crt}
assert bind_configuration =~ /verify_mode=peer/
assert_equal [200, {}, ["embedded app"]], app.call({})
end
def test_ssl_bind
skip_if :jruby
skip_unless :ssl
conf = Puma::Configuration.new do |c|
c.ssl_bind "0.0.0.0", "9292", {
cert: "/path/to/cert",
key: "/path/to/key",
verify_mode: "the_verify_mode",
}
end
conf.load
ssl_binding = "ssl://0.0.0.0:9292?cert=/path/to/cert&key=/path/to/key&verify_mode=the_verify_mode"
assert_equal [ssl_binding], conf.options[:binds]
end
def test_ssl_bind_jruby
skip_unless :jruby
skip_unless :ssl
cipher_list = "TLS_DHE_RSA_WITH_AES_128_CBC_SHA,TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256"
conf = Puma::Configuration.new do |c|
c.ssl_bind "0.0.0.0", "9292", {
keystore: "/path/to/keystore",
keystore_pass: "password",
ssl_cipher_list: cipher_list,
verify_mode: "the_verify_mode"
}
end
conf.load
ssl_binding = "ssl://0.0.0.0:9292?keystore=/path/to/keystore" \
"&keystore-pass=password&ssl_cipher_list=#{cipher_list}" \
"&verify_mode=the_verify_mode"
assert_equal [ssl_binding], conf.options[:binds]
end
def test_ssl_bind_no_tlsv1_1
skip_if :jruby
skip_unless :ssl
conf = Puma::Configuration.new do |c|
c.ssl_bind "0.0.0.0", "9292", {
cert: "/path/to/cert",
key: "/path/to/key",
verify_mode: "the_verify_mode",
no_tlsv1_1: true
}
end
conf.load
ssl_binding = "ssl://0.0.0.0:9292?cert=/path/to/cert&key=/path/to/key&verify_mode=the_verify_mode&no_tlsv1_1=true"
assert_equal [ssl_binding], conf.options[:binds]
end
def test_ssl_bind_with_cipher_filter
skip_if :jruby
skip_unless :ssl
cipher_filter = "!aNULL:AES+SHA"
conf = Puma::Configuration.new do |c|
c.ssl_bind "0.0.0.0", "9292", {
cert: "cert",
key: "key",
ssl_cipher_filter: cipher_filter,
}
end
conf.load
ssl_binding = conf.options[:binds].first
assert ssl_binding.include?("&ssl_cipher_filter=#{cipher_filter}")
end
def test_ssl_bind_with_verification_flags
skip_if :jruby
skip_unless :ssl
conf = Puma::Configuration.new do |c|
c.ssl_bind "0.0.0.0", "9292", {
cert: "cert",
key: "key",
verification_flags: ["TRUSTED_FIRST", "NO_CHECK_TIME"]
}
end
conf.load
ssl_binding = conf.options[:binds].first
assert ssl_binding.include?("&verification_flags=TRUSTED_FIRST,NO_CHECK_TIME")
end
def test_ssl_bind_with_ca
skip_unless :ssl
conf = Puma::Configuration.new do |c|
c.ssl_bind "0.0.0.0", "9292", {
cert: "/path/to/cert",
ca: "/path/to/ca",
key: "/path/to/key",
verify_mode: :peer,
}
end
conf.load
ssl_binding = conf.options[:binds].first
assert_match "ca=/path/to/ca", ssl_binding
assert_match "verify_mode=peer", ssl_binding
end
def test_lowlevel_error_handler_DSL
conf = Puma::Configuration.new do |c|
c.load "test/config/app.rb"
end
conf.load
app = conf.options[:lowlevel_error_handler]
assert_equal [200, {}, ["error page"]], app.call({})
end
def test_allow_users_to_override_default_options
conf = Puma::Configuration.new(restart_cmd: 'bin/rails server')
assert_equal 'bin/rails server', conf.options[:restart_cmd]
end
def test_overwrite_options
conf = Puma::Configuration.new do |c|
c.workers 3
end
conf.load
assert_equal conf.options[:workers], 3
conf.options[:workers] += 1
assert_equal conf.options[:workers], 4
end
def test_explicit_config_files
conf = Puma::Configuration.new(config_files: ['test/config/settings.rb']) do |c|
end
conf.load
assert_match(/:3000$/, conf.options[:binds].first)
end
def test_parameters_overwrite_files
conf = Puma::Configuration.new(config_files: ['test/config/settings.rb']) do |c|
c.port 3030
end
conf.load
assert_match(/:3030$/, conf.options[:binds].first)
assert_equal 3, conf.options[:min_threads]
assert_equal 5, conf.options[:max_threads]
end
def test_config_files_default
conf = Puma::Configuration.new do
end
assert_equal [nil], conf.config_files
end
def test_config_files_with_dash
conf = Puma::Configuration.new(config_files: ['-']) do
end
assert_equal [], conf.config_files
end
def test_config_files_with_existing_path
conf = Puma::Configuration.new(config_files: ['test/config/settings.rb']) do
end
assert_equal ['test/config/settings.rb'], conf.config_files
end
def test_config_files_with_non_existing_path
conf = Puma::Configuration.new(config_files: ['test/config/typo/settings.rb']) do
end
assert_equal ['test/config/typo/settings.rb'], conf.config_files
end
def test_config_files_with_integer_convert
conf = Puma::Configuration.new(config_files: ['test/config/with_integer_convert.rb']) do
end
conf.load
assert_equal 6, conf.options[:persistent_timeout]
assert_equal 3, conf.options[:first_data_timeout]
assert_equal 2, conf.options[:workers]
assert_equal 4, conf.options[:min_threads]
assert_equal 8, conf.options[:max_threads]
assert_equal 90, conf.options[:worker_timeout]
assert_equal 120, conf.options[:worker_boot_timeout]
assert_equal 150, conf.options[:worker_shutdown_timeout]
end
def test_config_files_with_float_convert
conf = Puma::Configuration.new(config_files: ['test/config/with_float_convert.rb']) do
end
conf.load
assert_equal Float::INFINITY, conf.options[:max_fast_inline]
end
def test_config_files_with_symbol_convert
conf = Puma::Configuration.new(config_files: ['test/config/with_symbol_convert.rb']) do
end
conf.load
assert_equal :ruby, conf.options[:io_selector_backend]
end
def test_config_raise_exception_on_sigterm
conf = Puma::Configuration.new do |c|
c.raise_exception_on_sigterm false
end
conf.load
assert_equal conf.options[:raise_exception_on_sigterm], false
conf.options[:raise_exception_on_sigterm] = true
assert_equal conf.options[:raise_exception_on_sigterm], true
end
def test_run_hooks_on_restart_hook
assert_run_hooks :on_restart
end
def test_run_hooks_before_worker_fork
assert_run_hooks :before_worker_fork, configured_with: :on_worker_fork
end
def test_run_hooks_after_worker_fork
assert_run_hooks :after_worker_fork
end
def test_run_hooks_before_worker_boot
assert_run_hooks :before_worker_boot, configured_with: :on_worker_boot
end
def test_run_hooks_before_worker_shutdown
assert_run_hooks :before_worker_shutdown, configured_with: :on_worker_shutdown
end
def test_run_hooks_before_fork
assert_run_hooks :before_fork
end
def test_run_hooks_and_exception
conf = Puma::Configuration.new do |c|
c.on_restart do |a|
raise RuntimeError, 'Error from hook'
end
end
conf.load
events = Puma::Events.strings
conf.run_hooks :on_restart, 'ARG', events
expected = /WARNING hook on_restart failed with exception \(RuntimeError\) Error from hook/
assert_match expected, events.stdout.string
end
def test_config_does_not_load_workers_by_default
assert_equal 0, Puma::Configuration.new.options.default_options[:workers]
end
def test_final_options_returns_merged_options
conf = Puma::Configuration.new({ min_threads: 1, max_threads: 2 }, { min_threads: 2 })
assert_equal 1, conf.final_options[:min_threads]
assert_equal 2, conf.final_options[:max_threads]
end
def test_silence_single_worker_warning_default
conf = Puma::Configuration.new
conf.load
assert_equal false, conf.options[:silence_single_worker_warning]
end
def test_silence_single_worker_warning_overwrite
conf = Puma::Configuration.new do |c|
c.silence_single_worker_warning
end
conf.load
assert_equal true, conf.options[:silence_single_worker_warning]
end
private
def assert_run_hooks(hook_name, options = {})
configured_with = options[:configured_with] || hook_name
# test single, not an array
messages = []
conf = Puma::Configuration.new
conf.options[hook_name] = -> (a) {
messages << "#{hook_name} is called with #{a}"
}
conf.run_hooks hook_name, 'ARG', Puma::Events.strings
assert_equal messages, ["#{hook_name} is called with ARG"]
# test multiple
messages = []
conf = Puma::Configuration.new do |c|
c.send(configured_with) do |a|
messages << "#{hook_name} is called with #{a} one time"
end
c.send(configured_with) do |a|
messages << "#{hook_name} is called with #{a} a second time"
end
end
conf.load
conf.run_hooks hook_name, 'ARG', Puma::Events.strings
assert_equal messages, ["#{hook_name} is called with ARG one time", "#{hook_name} is called with ARG a second time"]
end
end
# Thread unsafe modification of ENV
class TestEnvModifificationConfig < TestConfigFileBase
def test_double_bind_port
port = (rand(10_000) + 30_000).to_s
with_env("PORT" => port) do
conf = Puma::Configuration.new do |user_config, file_config, default_config|
user_config.bind "tcp://#{Puma::Configuration::DefaultTCPHost}:#{port}"
file_config.load "test/config/app.rb"
end
conf.load
assert_equal ["tcp://0.0.0.0:#{port}"], conf.options[:binds]
end
end
end
class TestConfigEnvVariables < TestConfigFileBase
def test_config_loads_correct_min_threads
assert_equal 0, Puma::Configuration.new.options.default_options[:min_threads]
with_env("MIN_THREADS" => "7") do
conf = Puma::Configuration.new
assert_equal 7, conf.options.default_options[:min_threads]
end
with_env("PUMA_MIN_THREADS" => "8") do
conf = Puma::Configuration.new
assert_equal 8, conf.options.default_options[:min_threads]
end
end
def test_config_loads_correct_max_threads
conf = Puma::Configuration.new
assert_equal conf.default_max_threads, conf.options.default_options[:max_threads]
with_env("MAX_THREADS" => "7") do
conf = Puma::Configuration.new
assert_equal 7, conf.options.default_options[:max_threads]
end
with_env("PUMA_MAX_THREADS" => "8") do
conf = Puma::Configuration.new
assert_equal 8, conf.options.default_options[:max_threads]
end
end
def test_config_loads_workers_from_env
with_env("WEB_CONCURRENCY" => "9") do
conf = Puma::Configuration.new
assert_equal 9, conf.options.default_options[:workers]
end
end
def test_config_does_not_preload_app_if_not_using_workers
with_env("WEB_CONCURRENCY" => "0") do
conf = Puma::Configuration.new
assert_equal false, conf.options.default_options[:preload_app]
end
end
def test_config_preloads_app_if_using_workers
with_env("WEB_CONCURRENCY" => "2") do
preload = Puma.forkable?
conf = Puma::Configuration.new
assert_equal preload, conf.options.default_options[:preload_app]
end
end
end
class TestConfigFileWithFakeEnv < TestConfigFileBase
def setup
FileUtils.mkpath("config/puma")
File.write("config/puma/fake-env.rb", "")
end
def test_config_files_with_app_env
with_env('APP_ENV' => 'fake-env') do
conf = Puma::Configuration.new do
end
assert_equal ['config/puma/fake-env.rb'], conf.config_files
end
end
def test_config_files_with_rack_env
with_env('RACK_ENV' => 'fake-env') do
conf = Puma::Configuration.new do
end
assert_equal ['config/puma/fake-env.rb'], conf.config_files
end
end
def test_config_files_with_rails_env
with_env('RAILS_ENV' => 'fake-env', 'RACK_ENV' => nil) do
conf = Puma::Configuration.new do
end
assert_equal ['config/puma/fake-env.rb'], conf.config_files
end
end
def test_config_files_with_specified_environment
conf = Puma::Configuration.new do
end
conf.options[:environment] = 'fake-env'
assert_equal ['config/puma/fake-env.rb'], conf.config_files
end
def teardown
FileUtils.rm_r("config/puma")
end
end