/
test_pumactl.rb
263 lines (208 loc) · 7.54 KB
/
test_pumactl.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
require_relative "helper"
require_relative "helpers/config_file"
require_relative "helpers/ssl"
require 'pathname'
require 'puma/control_cli'
class TestPumaControlCli < TestConfigFileBase
include SSLHelper
def setup
# use a pipe to get info across thread boundary
@wait, @ready = IO.pipe
end
def wait_booted
line = @wait.gets until line =~ /Use Ctrl-C to stop/
end
def teardown
@wait.close
@ready.close
end
def with_config_file(path_to_config, port)
path = Pathname.new(path_to_config)
Dir.mktmpdir do |tmp_dir|
Dir.chdir(tmp_dir) do
FileUtils.mkdir_p(path.dirname)
File.open(path, "w") { |f| f << "port #{port}" }
yield
end
end
end
def test_blank_command
assert_system_exit_with_cli_output [], "Available commands: #{Puma::ControlCLI::CMD_PATH_SIG_MAP.keys.join(", ")}"
end
def test_invalid_command
assert_system_exit_with_cli_output ['an-invalid-command'], 'Invalid command: an-invalid-command'
end
def test_config_file
control_cli = Puma::ControlCLI.new ["--config-file", "test/config/state_file_testing_config.rb", "halt"]
assert_equal "t3-pid", control_cli.instance_variable_get("@pidfile")
end
def test_app_env_without_environment
with_env('APP_ENV' => 'test') do
control_cli = Puma::ControlCLI.new ['halt']
assert_equal 'test', control_cli.instance_variable_get('@environment')
end
end
def test_rack_env_without_environment
with_env("RACK_ENV" => "test") do
control_cli = Puma::ControlCLI.new ["halt"]
assert_equal "test", control_cli.instance_variable_get("@environment")
end
end
def test_app_env_precedence
with_env('APP_ENV' => nil, 'RACK_ENV' => nil, 'RAILS_ENV' => 'production') do
control_cli = Puma::ControlCLI.new ['halt']
assert_equal 'production', control_cli.instance_variable_get('@environment')
end
with_env('APP_ENV' => nil, 'RACK_ENV' => 'test', 'RAILS_ENV' => 'production') do
control_cli = Puma::ControlCLI.new ['halt']
assert_equal 'test', control_cli.instance_variable_get('@environment')
end
with_env('APP_ENV' => 'development', 'RACK_ENV' => 'test', 'RAILS_ENV' => 'production') do
control_cli = Puma::ControlCLI.new ['halt']
assert_equal 'development', control_cli.instance_variable_get('@environment')
control_cli = Puma::ControlCLI.new ['-e', 'test', 'halt']
assert_equal 'test', control_cli.instance_variable_get('@environment')
end
end
def test_environment_without_app_env
with_env('APP_ENV' => nil, 'RACK_ENV' => nil, 'RAILS_ENV' => nil) do
control_cli = Puma::ControlCLI.new ['halt']
assert_nil control_cli.instance_variable_get('@environment')
control_cli = Puma::ControlCLI.new ['-e', 'test', 'halt']
assert_equal 'test', control_cli.instance_variable_get('@environment')
end
end
def test_environment_without_rack_env
with_env("RACK_ENV" => nil, 'RAILS_ENV' => nil) do
control_cli = Puma::ControlCLI.new ["halt"]
assert_nil control_cli.instance_variable_get("@environment")
control_cli = Puma::ControlCLI.new ["-e", "test", "halt"]
assert_equal "test", control_cli.instance_variable_get("@environment")
end
end
def test_environment_with_rack_env
with_env("RACK_ENV" => "production") do
control_cli = Puma::ControlCLI.new ["halt"]
assert_equal "production", control_cli.instance_variable_get("@environment")
control_cli = Puma::ControlCLI.new ["-e", "test", "halt"]
assert_equal "test", control_cli.instance_variable_get("@environment")
end
end
def test_environment_specific_config_file_exist
port = 6002
puma_config_file = "config/puma.rb"
production_config_file = "config/puma/production.rb"
with_env("RACK_ENV" => nil) do
with_config_file(puma_config_file, port) do
control_cli = Puma::ControlCLI.new ["-e", "production", "halt"]
assert_equal puma_config_file, control_cli.instance_variable_get("@config_file")
end
with_config_file(production_config_file, port) do
control_cli = Puma::ControlCLI.new ["-e", "production", "halt"]
assert_equal production_config_file, control_cli.instance_variable_get("@config_file")
end
end
end
def test_default_config_file_exist
port = 6001
puma_config_file = "config/puma.rb"
development_config_file = "config/puma/development.rb"
with_env("RACK_ENV" => nil, 'RAILS_ENV' => nil) do
with_config_file(puma_config_file, port) do
control_cli = Puma::ControlCLI.new ["halt"]
assert_equal puma_config_file, control_cli.instance_variable_get("@config_file")
end
with_config_file(development_config_file, port) do
control_cli = Puma::ControlCLI.new ["halt"]
assert_equal development_config_file, control_cli.instance_variable_get("@config_file")
end
end
end
def test_control_no_token
opts = [
"--config-file", "test/config/control_no_token.rb",
"start"
]
control_cli = Puma::ControlCLI.new opts, @ready, @ready
assert_equal 'none', control_cli.instance_variable_get("@control_auth_token")
end
def test_control_url_and_status
host = "127.0.0.1"
port = UniquePort.call
url = "tcp://#{host}:#{port}/"
opts = [
"--control-url", url,
"--control-token", "ctrl",
"--config-file", "test/config/app.rb",
]
control_cli = Puma::ControlCLI.new (opts + ["start"]), @ready, @ready
t = Thread.new do
control_cli.run
end
wait_booted
s = TCPSocket.new host, 9292
s << "GET / HTTP/1.0\r\n\r\n"
body = s.read
assert_match "200 OK", body
assert_match "embedded app", body
assert_command_cli_output opts + ["status"], "Puma is started"
assert_command_cli_output opts + ["stop"], "Command stop sent success"
assert_kind_of Thread, t.join, "server didn't stop"
end
def test_control_ssl
skip_unless :ssl
host = "127.0.0.1"
port = UniquePort.call
url = "ssl://#{host}:#{port}?#{ssl_query}"
opts = [
"--control-url", url,
"--control-token", "ctrl",
"--config-file", "test/config/app.rb",
]
control_cli = Puma::ControlCLI.new (opts + ["start"]), @ready, @ready
t = Thread.new do
control_cli.run
end
wait_booted
assert_command_cli_output opts + ["status"], "Puma is started"
assert_command_cli_output opts + ["stop"], "Command stop sent success"
assert_kind_of Thread, t.join, "server didn't stop"
end
def test_control_aunix
skip_unless :aunix
url = "unix://@test_control_aunix.unix"
opts = [
"--control-url", url,
"--control-token", "ctrl",
"--config-file", "test/config/app.rb",
]
control_cli = Puma::ControlCLI.new (opts + ["start"]), @ready, @ready
t = Thread.new do
control_cli.run
end
wait_booted
assert_command_cli_output opts + ["status"], "Puma is started"
assert_command_cli_output opts + ["stop"], "Command stop sent success"
assert_kind_of Thread, t.join, "server didn't stop"
end
private
def assert_command_cli_output(options, expected_out)
cmd = Puma::ControlCLI.new(options)
out, _ = capture_subprocess_io do
begin
cmd.run
rescue SystemExit
end
end
assert_match expected_out, out
end
def assert_system_exit_with_cli_output(options, expected_out)
out, _ = capture_subprocess_io do
response = assert_raises(SystemExit) do
Puma::ControlCLI.new(options).run
end
assert_equal(response.status, 1)
end
assert_match expected_out, out
end
end