summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/import_export/command_line_util_spec.rb
blob: 59d9735704535e4472438f9bfff504a52a714e84 (plain)
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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::ImportExport::CommandLineUtil do
  include ExportFileHelper

  let(:path) { "#{Dir.tmpdir}/symlink_test" }
  let(:archive) { 'spec/fixtures/symlink_export.tar.gz' }
  let(:shared) { Gitlab::ImportExport::Shared.new(nil) }
  let(:tmpdir) { Dir.mktmpdir }

  subject do
    Class.new do
      include Gitlab::ImportExport::CommandLineUtil

      def initialize
        @shared = Gitlab::ImportExport::Shared.new(nil)
      end

      # Make the included methods public for testing
      public :download_or_copy_upload, :download
    end.new
  end

  before do
    FileUtils.mkdir_p(path)
    subject.untar_zxf(archive: archive, dir: path)
  end

  after do
    FileUtils.rm_rf(path)
    FileUtils.remove_entry(tmpdir)
  end

  it 'has the right mask for project.json' do
    expect(file_permissions("#{path}/project.json")).to eq(0755) # originally 777
  end

  it 'has the right mask for uploads' do
    expect(file_permissions("#{path}/uploads")).to eq(0755) # originally 555
  end

  describe '#download_or_copy_upload' do
    let(:upload) { instance_double(Upload, local?: local) }
    let(:uploader) { instance_double(ImportExportUploader, path: :path, url: :url, upload: upload) }
    let(:upload_path) { '/some/path' }

    context 'when the upload is local' do
      let(:local) { true }

      it 'copies the file' do
        expect(subject).to receive(:copy_files).with(:path, upload_path)

        subject.download_or_copy_upload(uploader, upload_path)
      end
    end

    context 'when the upload is remote' do
      let(:local) { false }

      it 'downloads the file' do
        expect(subject).to receive(:download).with(:url, upload_path, size_limit: nil)

        subject.download_or_copy_upload(uploader, upload_path)
      end
    end
  end

  describe '#download' do
    let(:content) { File.open('spec/fixtures/rails_sample.tif') }

    context 'a non-localhost uri' do
      before do
        stub_request(:get, url)
          .to_return(
            status: status,
            body: content
          )
      end

      let(:url) { 'https://gitlab.com/file' }

      context 'with ok status code' do
        let(:status) { HTTP::Status::OK }

        it 'gets the contents' do
          Tempfile.create('test') do |file|
            subject.download(url, file.path)
            expect(file.read).to eq(File.open('spec/fixtures/rails_sample.tif').read)
          end
        end

        it 'streams the contents via Gitlab::HTTP' do
          expect(Gitlab::HTTP).to receive(:get).with(url, hash_including(stream_body: true))

          Tempfile.create('test') do |file|
            subject.download(url, file.path)
          end
        end

        it 'does not get the content over the size_limit' do
          Tempfile.create('test') do |file|
            subject.download(url, file.path, size_limit: 300.kilobytes)
            expect(file.read).to eq('')
          end
        end

        it 'gets the content within the size_limit' do
          Tempfile.create('test') do |file|
            subject.download(url, file.path, size_limit: 400.kilobytes)
            expect(file.read).to eq(File.open('spec/fixtures/rails_sample.tif').read)
          end
        end
      end

      %w[MOVED_PERMANENTLY FOUND TEMPORARY_REDIRECT].each do |code|
        context "with a redirect status code #{code}" do
          let(:status) { HTTP::Status.const_get(code, false) }

          it 'logs the redirect' do
            expect(Gitlab::Import::Logger).to receive(:warn)

            Tempfile.create('test') do |file|
              subject.download(url, file.path)
            end
          end
        end
      end

      %w[ACCEPTED UNAUTHORIZED BAD_REQUEST].each do |code|
        context "with an invalid status code #{code}" do
          let(:status) { HTTP::Status.const_get(code, false) }

          it 'throws an error' do
            Tempfile.create('test') do |file|
              expect { subject.download(url, file.path) }.to raise_error(Gitlab::ImportExport::Error)
            end
          end
        end
      end
    end

    context 'a localhost uri' do
      include StubRequests

      let(:status) { HTTP::Status::OK }
      let(:url) { "#{host}/foo/bar" }
      let(:host) { 'http://localhost:8081' }

      before do
        # Note: the hostname gets changed to an ip address due to dns_rebind_protection
        stub_dns(url, ip_address: '127.0.0.1')
        stub_request(:get, 'http://127.0.0.1:8081/foo/bar')
          .to_return(
            status: status,
            body: content
          )
      end

      it 'throws a blocked url error' do
        Tempfile.create('test') do |file|
          expect { subject.download(url, file.path) }.to raise_error((Gitlab::HTTP::BlockedUrlError))
        end
      end

      context 'for object_storage uri' do
        let(:enabled_object_storage_setting) do
          {
            'enabled' => true,
            'object_store' =>
            {
              'enabled' => true,
              'connection' => {
                'endpoint' => host
              }
            }
          }
        end

        before do
          allow(Settings).to receive(:external_diffs).and_return(enabled_object_storage_setting)
        end

        it 'gets the content' do
          Tempfile.create('test') do |file|
            subject.download(url, file.path)
            expect(file.read).to eq(File.open('spec/fixtures/rails_sample.tif').read)
          end
        end
      end
    end
  end

  describe '#gzip' do
    it 'compresses specified file' do
      tempfile = Tempfile.new('test', path)
      filename = File.basename(tempfile.path)

      subject.gzip(dir: path, filename: filename)

      expect(File.exist?("#{tempfile.path}.gz")).to eq(true)
    end

    context 'when exception occurs' do
      it 'raises an exception' do
        expect { subject.gzip(dir: path, filename: 'test') }.to raise_error(Gitlab::ImportExport::Error)
      end
    end
  end

  describe '#gunzip' do
    it 'decompresses specified file' do
      filename = 'labels.ndjson.gz'
      gz_filepath = "spec/fixtures/bulk_imports/gz/#{filename}"
      FileUtils.copy_file(gz_filepath, File.join(tmpdir, filename))

      subject.gunzip(dir: tmpdir, filename: filename)

      expect(File.exist?(File.join(tmpdir, 'labels.ndjson'))).to eq(true)
    end

    context 'when exception occurs' do
      it 'raises an exception' do
        expect { subject.gunzip(dir: path, filename: 'test') }.to raise_error(Gitlab::ImportExport::Error)
      end
    end
  end

  describe '#tar_cf' do
    let(:archive_dir) { Dir.mktmpdir }

    after do
      FileUtils.remove_entry(archive_dir)
    end

    it 'archives a folder without compression' do
      archive_file = File.join(archive_dir, 'archive.tar')

      result = subject.tar_cf(archive: archive_file, dir: tmpdir)

      expect(result).to eq(true)
      expect(File.exist?(archive_file)).to eq(true)
    end

    context 'when something goes wrong' do
      it 'raises an error' do
        expect(Gitlab::Popen).to receive(:popen).and_return(['Error', 1])

        klass = Class.new do
          include Gitlab::ImportExport::CommandLineUtil
        end.new

        expect { klass.tar_cf(archive: 'test', dir: 'test') }.to raise_error(Gitlab::ImportExport::Error, 'command exited with error code 1: Error')
      end
    end
  end

  describe '#untar_xf' do
    let(:archive_dir) { Dir.mktmpdir }

    after do
      FileUtils.remove_entry(archive_dir)
    end

    it 'extracts archive without decompression' do
      filename = 'archive.tar.gz'
      archive_file = File.join(archive_dir, 'archive.tar')

      FileUtils.copy_file(archive, File.join(archive_dir, filename))
      subject.gunzip(dir: archive_dir, filename: filename)

      result = subject.untar_xf(archive: archive_file, dir: archive_dir)

      expect(result).to eq(true)
      expect(File.exist?(archive_file)).to eq(true)
      expect(File.exist?(File.join(archive_dir, 'project.json'))).to eq(true)
      expect(Dir.exist?(File.join(archive_dir, 'uploads'))).to eq(true)
    end

    context 'when something goes wrong' do
      before do
        expect(Gitlab::Popen).to receive(:popen).and_return(['Error', 1])
      end

      it 'raises an error' do
        klass = Class.new do
          include Gitlab::ImportExport::CommandLineUtil
        end.new

        expect { klass.untar_xf(archive: 'test', dir: 'test') }.to raise_error(Gitlab::ImportExport::Error, 'command exited with error code 1: Error')
      end

      it 'returns false and includes error status' do
        klass = Class.new do
          include Gitlab::ImportExport::CommandLineUtil

          attr_accessor :shared

          def initialize
            @shared = Gitlab::ImportExport::Shared.new(nil)
          end
        end.new

        expect(klass.tar_czf(archive: 'test', dir: 'test')).to eq(false)
        expect(klass.shared.errors).to eq(['command exited with error code 1: Error'])
      end
    end
  end
end