-
Notifications
You must be signed in to change notification settings - Fork 367
Expand file tree
/
Copy pathapp_packager_spec.rb
More file actions
286 lines (222 loc) · 9.91 KB
/
app_packager_spec.rb
File metadata and controls
286 lines (222 loc) · 9.91 KB
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
require 'spec_helper'
require 'cloud_controller/app_packager'
RSpec.describe AppPackager do
subject(:app_packager) { AppPackager.new(input_zip, logger:) }
around do |example|
Dir.mktmpdir('app_packager_spec') do |tmpdir|
@tmpdir = tmpdir
example.call
end
end
let(:logger) { instance_double(Steno::Logger, error: nil) }
describe '#size' do
let(:input_zip) { File.join(Paths::FIXTURES, 'good.zip') }
let(:size_of_good_zip) { 17 }
it 'returns the sum of each file size' do
expect(app_packager.size).to eq(size_of_good_zip)
end
end
describe '#unzip' do
let(:input_zip) { File.join(Paths::FIXTURES, 'good.zip') }
it 'unzips the file given' do
app_packager.unzip(@tmpdir)
expect(Dir["#{@tmpdir}/**/*"].size).to eq 4
expect(Dir["#{@tmpdir}/*"].size).to eq 3
expect(Dir["#{@tmpdir}/subdir/*"].size).to eq 1
end
context 'when the zip contains broken symlinks' do
let(:input_zip) { File.join(Paths::FIXTURES, 'app_packager_zips', 'broken-file-symlink.zip') }
it 'successfully unzips' do
expect do
app_packager.unzip(@tmpdir)
end.not_to raise_error
end
end
context 'when the zip destination does not exist' do
it 'raises an exception' do
expect do
app_packager.unzip(File.join(@tmpdir, 'blahblah'))
end.to raise_exception(CloudController::Errors::ApiError, /destination does not exist/i)
end
end
context 'when the zip is empty' do
let(:input_zip) { File.join(Paths::FIXTURES, 'empty.zip') }
it 'raises an exception' do
expect do
app_packager.unzip(@tmpdir)
end.to raise_exception(CloudController::Errors::ApiError, /Invalid zip archive/)
end
end
describe 'relative paths' do
context 'when the relative path does NOT leave the root directory' do
let(:input_zip) { File.join(Paths::FIXTURES, 'good_relative_paths.zip') }
it 'unzips the archive, ignoring ".."' do
app_packager.unzip(@tmpdir)
expect(File.exist?("#{@tmpdir}/bar/cat")).to be true
end
end
context 'when the relative path does leave the root directory' do
let(:input_zip) { File.join(Paths::FIXTURES, 'bad_relative_paths.zip') }
it 'unzips the archive, ignoring ".."' do
app_packager.unzip(@tmpdir)
expect(File.exist?("#{@tmpdir}/fakezip.zip")).to be true
end
end
end
context 'when there is an error unzipping' do
before do
allow(Open3).to receive(:capture3).and_return(['', multiline_error_message, double(success?: false)])
end
context 'end-of-central-directory signature not found' do
let(:multiline_error_message) do
<<~EOF
[archive]
End-of-central-directory signature not found. Either this file is not
a zipfile, or it constitutes one disk of a multi-part archive. In the
latter case the central directory and zipfile comment will be found on
the last disk(s) of this archive.
unzip: cannot find zipfile directory in one of archive or
archive.zip, and cannot find archive.ZIP, period.
EOF
end
it 'raises an exception' do
expect do
app_packager.unzip(@tmpdir)
end.to raise_error(CloudController::Errors::ApiError, 'The app upload is invalid: Invalid zip archive (end-of-central-directory signature not found).')
end
end
context 'end-of-central-directory signature not found' do
let(:multiline_error_message) do
<<~EOF
warning [archive]: zipfile is empty
EOF
end
it 'raises an exception' do
expect do
app_packager.unzip(@tmpdir)
end.to raise_error(CloudController::Errors::ApiError, 'The app upload is invalid: Invalid zip archive (zipfile is empty).')
end
end
context 'mismatching "local" filename' do
let(:multiline_error_message) do
<<~EOF
a_is_++: mismatching "local" filename (a_is_α),
continuing with "central" filename version
b_is_++: mismatching "local" filename (b_is_β),
continuing with "central" filename version
EOF
end
it 'raises an exception' do
expect do
app_packager.unzip(@tmpdir)
end.to raise_error(CloudController::Errors::ApiError, 'The app upload is invalid: Invalid zip archive (mismatching local filename).')
end
end
end
end
describe '#append_dir_contents' do
let(:input_zip) { File.join(@tmpdir, 'good.zip') }
let(:additional_files_path) { File.join(Paths::FIXTURES, 'fake_package') }
before { FileUtils.cp(File.join(Paths::FIXTURES, 'good.zip'), input_zip) }
it 'adds the files to the zip' do
app_packager.append_dir_contents(additional_files_path)
output = `zipinfo #{input_zip}`
expect(output).not_to include './'
expect(output).not_to include 'fake_package'
expect(output).to match(/^l.+coming_from_inside$/)
expect(output).to include 'here.txt'
expect(output).to include 'subdir/'
expect(output).to include 'subdir/there.txt'
expect(output).to include 'bye'
expect(output).to include 'hi'
expect(output).to include 'subdir/'
expect(output).to include 'subdir/greetings'
expect(output).to include '7 files'
end
context 'when there are no additional files' do
let(:additional_files_path) { File.join(@tmpdir, 'empty') }
it 'results in the existing zip' do
Dir.mkdir(additional_files_path)
output = `zipinfo #{input_zip}`
expect(output).to include 'bye'
expect(output).to include 'hi'
expect(output).to include 'subdir/'
expect(output).to include 'subdir/greeting'
expect(output).to include '4 files'
app_packager.append_dir_contents(additional_files_path)
output = `zipinfo #{input_zip}`
expect(output).to include 'bye'
expect(output).to include 'hi'
expect(output).to include 'subdir/'
expect(output).to include 'subdir/greeting'
expect(output).to include '4 files'
end
end
context 'when there is an error zipping' do
it 'raises an exception' do
allow(Open3).to receive(:capture3).and_return(['output', 'error', double(success?: false)])
expect do
app_packager.append_dir_contents(additional_files_path)
end.to raise_error(CloudController::Errors::ApiError, /The app package is invalid: Error appending additional resources to package/)
end
end
end
describe '#fix_subdir_permissions' do
context 'when the zip has directories without the directory attribute or execute permission (it was created on windows)' do
let(:input_zip) { File.join(@tmpdir, 'bad_directory_permissions.zip') }
before { FileUtils.cp(File.join(Paths::FIXTURES, 'app_packager_zips', 'bad_directory_permissions.zip'), input_zip) }
it 'deletes all directories from the archive' do
app_packager.fix_subdir_permissions(@tmpdir, "#{@tmpdir}/application_contents")
has_dirs = Zip::File.open(input_zip) do |in_zip|
in_zip.entries.any?(&:directory?)
end
expect(has_dirs).to be_falsey
end
end
context 'when the zip has directories with special characters' do
let(:input_zip) { File.join(@tmpdir, 'special_character_names.zip') }
before { FileUtils.cp(File.join(Paths::FIXTURES, 'app_packager_zips', 'special_character_names.zip'), input_zip) }
it 'successfully removes and re-adds them' do
app_packager.fix_subdir_permissions(@tmpdir, "#{@tmpdir}/application_contents")
expect(`zipinfo #{input_zip}`).to match %r{special_character_names/&&hello::\?\?/}
end
end
context 'when there are many directories' do
let(:input_zip) { File.join(@tmpdir, 'many_dirs.zip') }
before { FileUtils.cp(File.join(Paths::FIXTURES, 'app_packager_zips', 'many_dirs.zip'), input_zip) }
it 'fixes the directory permissions and batches the directory deletes so it does not exceed the max command length' do
allow(Open3).to receive(:capture3).and_call_original
batch_size = 10
stub_const('AppPackager::DIRECTORY_DELETE_BATCH_SIZE', batch_size)
app_packager.fix_subdir_permissions(@tmpdir, "#{@tmpdir}/application_contents")
output = `zipinfo #{input_zip}`
21.times do |i|
expect(output).to include("folder_#{i}/")
expect(output).to include("folder_#{i}/empty_file")
end
number_of_batches = (21.0 / batch_size).ceil
expect(number_of_batches).to eq(3)
expect(Open3).to have_received(:capture3).exactly(number_of_batches + 1).times
end
end
context 'when there is an error deleting directories' do
let(:input_zip) { File.join(@tmpdir, 'bad_directory_permissions.zip') }
before { FileUtils.cp(File.join(Paths::FIXTURES, 'app_packager_zips', 'bad_directory_permissions.zip'), input_zip) }
it 'raises an exception' do
allow(Open3).to receive(:capture3).and_return(['output', 'error', double(success?: false)])
expect do
app_packager.fix_subdir_permissions(@tmpdir, "#{@tmpdir}/application_contents")
end.to raise_error(CloudController::Errors::ApiError, /The app package is invalid: Error removing zip directories./)
end
end
context 'when there is a zip error' do
let(:input_zip) { 'garbage' }
it 'raises an exception' do
allow(Open3).to receive(:capture3).and_return(['output', 'error', double(success?: false)])
expect do
app_packager.fix_subdir_permissions(@tmpdir, "#{@tmpdir}/application_contents")
end.to raise_error(CloudController::Errors::ApiError, /The app upload is invalid: Invalid zip archive./)
end
end
end
end