/
runtime_installer_spec.rb
354 lines (306 loc) · 11 KB
/
runtime_installer_spec.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
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
require 'phusion_passenger/standalone/runtime_installer'
require 'tmpdir'
require 'fileutils'
require 'stringio'
module PhusionPassenger
module Standalone
describe RuntimeInstaller do
before :each do
@temp_dir = Dir.mktmpdir
Dir.mkdir("#{@temp_dir}/support")
Dir.mkdir("#{@temp_dir}/nginx")
@logs = StringIO.new
PhusionPassenger.stub(:installed_from_release_package?).and_return(true)
end
after :each do
FileUtils.remove_entry_secure(@temp_dir)
end
let(:binaries_url_root) { "http://somewhere" }
let(:version) { PhusionPassenger::VERSION_STRING }
let(:nginx_version) { PhusionPassenger::PREFERRED_NGINX_VERSION }
let(:cxx_compat_id) { PlatformInfo.cxx_binary_compatibility_id }
let(:support_binaries_url) { "#{binaries_url_root}/#{version}/support-#{cxx_compat_id}.tar.gz" }
let(:nginx_binary_url) { "#{binaries_url_root}/#{version}/nginx-#{nginx_version}-#{cxx_compat_id}.tar.gz" }
let(:nginx_source_url) { "http://nginx.org/download/nginx-#{nginx_version}.tar.gz" }
def create_installer(options = {})
options = {
:binaries_url_root => binaries_url_root,
:stdout => @logs,
:stderr => @logs
}.merge(options)
@installer = RuntimeInstaller.new(options)
end
def create_tarball(filename, contents = nil)
Dir.mktmpdir("tarball-", @temp_dir) do |tarball_dir|
Dir.chdir(tarball_dir) do
if block_given?
yield
else
contents.each do |content_name|
File.open(content_name, "w").close
end
end
sh "tar", "-czf", filename, "."
end
end
end
def create_dummy_support_binaries
Dir.mkdir("agents")
["PassengerWatchdog", "PassengerHelperAgent", "PassengerLoggingAgent"].each do |exe|
File.open("agents/#{exe}", "w") do |f|
f.puts "#!/bin/bash"
f.puts "echo PASS"
end
File.chmod(0755, "agents/#{exe}")
end
end
def create_dummy_nginx_binary
File.open("nginx", "w") do |f|
f.puts "#!/bin/bash"
f.puts "echo nginx version: 1.0.0"
end
File.chmod(0755, "nginx")
end
def create_file(filename)
File.open(filename, "w").close
end
def sh(*command)
if !system(*command)
raise "Command failed: #{command.join(' ')}"
end
end
def test_download_nginx_binary
create_installer(:targets => [:nginx],
:nginx_dir => "#{@temp_dir}/nginx",
:lib_dir => PhusionPassenger.lib_dir)
@installer.should_receive(:download).
and_return do |url, output, options|
url.should == nginx_binary_url
options[:use_cache].should be_true
create_tarball(output) do
create_dummy_nginx_binary
end
true
end
@installer.should_receive(:check_for_download_tool)
@installer.should_not_receive(:check_depdendencies)
@installer.should_not_receive(:compile_support_binaries)
@installer.should_not_receive(:download_and_extract_nginx_sources)
@installer.should_not_receive(:compile_nginx)
@installer.run
File.exist?("#{@temp_dir}/nginx/nginx").should be_true
end
def test_building_nginx_binary
create_installer(:targets => [:nginx],
:nginx_dir => "#{@temp_dir}/nginx",
:lib_dir => PhusionPassenger.lib_dir)
@installer.should_receive(:download).twice.and_return do |url, output|
if url == nginx_binary_url
false
elsif url == nginx_source_url
create_tarball(output) do
Dir.mkdir("nginx-#{nginx_version}")
File.open("nginx-#{nginx_version}/configure", "w") do |f|
f.puts %Q{echo "$@" > '#{@temp_dir}/configure.txt'}
end
File.chmod(0700, "nginx-#{nginx_version}/configure")
File.open("nginx-#{nginx_version}/Makefile", "w") do |f|
f.puts("all:")
f.puts(" mkdir objs")
f.puts(" echo ok > objs/nginx")
end
end
true
else
raise "Unexpected download URL: #{url}"
end
end
@installer.should_receive(:check_for_download_tool)
@installer.should_receive(:check_dependencies).and_return(true)
@installer.should_not_receive(:compile_support_binaries)
@installer.should_receive(:strip_binary).
with(an_instance_of(String)).
and_return(true)
@installer.run
File.read("#{@temp_dir}/nginx/nginx").should == "ok\n"
File.read("#{@temp_dir}/configure.txt").should include(
"--add-module=#{PhusionPassenger.nginx_module_source_dir}")
end
context "when originally packaged" do
before :each do
PhusionPassenger.stub(:originally_packaged?).and_return(true)
PhusionPassenger.stub(:natively_packaged?).and_return(false)
end
it "downloads the support binaries from the Internet if :support_binaries is specified as target" do
create_installer(:targets => [:support_binaries],
:support_dir => "#{@temp_dir}/support")
@installer.should_receive(:download).
and_return do |url, output, options|
url.should == "#{binaries_url_root}/#{version}/support-#{cxx_compat_id}.tar.gz"
options[:use_cache].should be_true
create_tarball(output) do
create_dummy_support_binaries
end
true
end
@installer.should_receive(:check_for_download_tool)
@installer.should_not_receive(:check_depdendencies)
@installer.should_not_receive(:compile_support_binaries)
@installer.should_not_receive(:download_and_extract_nginx_sources)
@installer.should_not_receive(:compile_nginx)
@installer.run
File.exist?("#{@temp_dir}/support/agents/PassengerWatchdog").should be_true
end
it "downloads the Nginx binary from the Internet if :nginx is specified as target" do
test_download_nginx_binary
end
it "downloads everything if :support_binaries and :nginx are both specified as target" do
create_installer(:targets => [:support_binaries, :nginx],
:support_dir => "#{@temp_dir}/support",
:nginx_dir => "#{@temp_dir}/nginx",
:lib_dir => PhusionPassenger.lib_dir)
@installer.should_receive(:download).
twice.
and_return do |url, output, options|
if url == support_binaries_url
create_tarball(output) do
create_dummy_support_binaries
end
elsif url == nginx_binary_url
create_tarball(output) do
create_dummy_nginx_binary
end
else
raise "Unexpected download URL: #{url}"
end
options[:use_cache].should be_true
true
end
@installer.should_receive(:check_for_download_tool)
@installer.should_not_receive(:check_depdendencies)
@installer.should_not_receive(:compile_support_binaries)
@installer.should_not_receive(:download_and_extract_nginx_sources)
@installer.should_not_receive(:compile_nginx)
@installer.run
File.exist?("#{@temp_dir}/support/agents/PassengerWatchdog").should be_true
File.exist?("#{@temp_dir}/nginx/nginx").should be_true
end
it "builds the support binaries if it cannot be downloaded" do
create_installer(:targets => [:support_binaries],
:support_dir => "#{@temp_dir}/support")
nginx_libs = COMMON_LIBRARY.
only(*NGINX_LIBS_SELECTOR).
set_output_dir("#{@temp_dir}/support/libpassenger_common").
link_objects
built_files = nil
@installer.should_receive(:run_rake_task!).with(
"nginx_without_native_support CACHING=false OUTPUT_DIR='#{@temp_dir}/support'").
and_return do
FileUtils.mkdir_p("#{@temp_dir}/agents")
create_file("#{@temp_dir}/agents/PassengerWatchdog")
nginx_libs.each do |object_filename|
dir = File.dirname(object_filename)
FileUtils.mkdir_p(dir)
create_file(object_filename)
end
built_files = `find '#{@temp_dir}/support'`
end
@installer.should_receive(:check_for_download_tool)
@installer.should_receive(:download).and_return(false)
@installer.should_receive(:check_dependencies).and_return(true)
@installer.should_not_receive(:download_and_extract_nginx_sources)
@installer.should_not_receive(:compile_nginx)
@installer.run
`find '#{@temp_dir}/support'`.should == built_files
end
it "builds the Nginx binary if it cannot be downloaded" do
test_building_nginx_binary
end
it "aborts if the support binaries cannot be built" do
create_installer(:targets => [:support_binaries],
:support_dir => "#{@temp_dir}/support")
@installer.should_receive(:run_rake_task!).with(
"nginx_without_native_support CACHING=false OUTPUT_DIR='#{@temp_dir}/support'").
and_raise(RuntimeError, "Rake failed")
@installer.should_receive(:check_for_download_tool)
@installer.should_receive(:download).and_return(false)
@installer.should_receive(:check_dependencies).and_return(true)
@installer.should_not_receive(:download_and_extract_nginx_sources)
@installer.should_not_receive(:compile_nginx)
lambda { @installer.run }.should raise_error("Rake failed")
end
end
context "when natively packaged" do
before :each do
PhusionPassenger.stub(:source_root).and_return("/locations.ini")
PhusionPassenger.stub(:originally_packaged?).and_return(false)
PhusionPassenger.stub(:natively_packaged?).and_return(true)
end
it "refuses to accept :support_binaries as target" do
block = lambda do
create_installer(:targets => [:support_binaries],
:support_dir => "#{@temp_dir}/support")
end
block.should raise_error(ArgumentError, /You cannot specify :support_binaries/)
end
it "downloads the Nginx binary from the Internet if :nginx is specified as target" do
test_download_nginx_binary
end
it "builds the Nginx binary if it cannot be downloaded" do
test_building_nginx_binary
end
end
it "aborts if the Nginx source tarball cannot be extracted" do
create_installer(:targets => [:nginx],
:nginx_dir => "#{@temp_dir}/nginx",
:lib_dir => PhusionPassenger.lib_dir)
@installer.should_receive(:download).twice.and_return do |url, output, options|
if url == nginx_binary_url
false
elsif url == nginx_source_url
File.open(output, "w") do |f|
f.write("garbage")
end
true
else
raise "Unexpected download URL: #{url}"
end
end
@installer.should_receive(:check_for_download_tool)
@installer.should_receive(:check_dependencies).and_return(true)
@installer.should_not_receive(:compile_support_binaries)
lambda { @installer.run }.should raise_error(SystemExit)
@logs.string.should =~ %r{Unable to download or extract Nginx source tarball}
end
it "aborts if the Nginx binary cannot be built" do
create_installer(:targets => [:nginx],
:nginx_dir => "#{@temp_dir}/nginx",
:lib_dir => PhusionPassenger.lib_dir)
@installer.should_receive(:download).twice.and_return do |url, output, options|
if url == nginx_binary_url
false
elsif url == nginx_source_url
create_tarball(output) do
Dir.mkdir("nginx-#{nginx_version}")
File.open("nginx-#{nginx_version}/configure", "w") do |f|
f.puts("#!/bin/bash")
f.puts("echo error")
f.puts("exit 1")
end
File.chmod(0700, "nginx-#{nginx_version}/configure")
end
true
else
raise "Unexpected download URL: #{url}"
end
end
@installer.should_receive(:check_for_download_tool)
@installer.should_receive(:check_dependencies).and_return(true)
@installer.should_not_receive(:compile_support_binaries)
lambda { @installer.run }.should raise_error(SystemExit)
@logs.string.should =~ %r{command failed:.*./configure}
end
end
end # module Standalone
end # module PhusionPassenger