Welcome to mirror list, hosted at ThFree Co, Russian Federation.

files_spec.rb « targets « backup « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d4acd13c4cbc762a17664acfa78993a358d1922f (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
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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Backup::Targets::Files, feature_category: :backup_restore do
  let(:progress) { StringIO.new }
  let!(:project) { create(:project) }
  let(:backup_options) { Backup::Options.new }
  let(:backup_basepath) { Pathname(Gitlab.config.backup.path) }

  let(:status_0) { instance_double(Process::Status, success?: true, exitstatus: 0) }
  let(:status_1) { instance_double(Process::Status, success?: false, exitstatus: 1) }
  let(:status_2) { instance_double(Process::Status, success?: false, exitstatus: 2) }

  before do
    allow(progress).to receive(:puts)
    allow(progress).to receive(:print)
    allow(FileUtils).to receive(:mkdir_p).and_return(true)
    allow(FileUtils).to receive(:mv).and_return(true)
    allow(File).to receive(:exist?).and_return(true)
    allow(File).to receive(:realpath).with("/var/gitlab-registry").and_return("/var/gitlab-registry")
    allow(File).to receive(:realpath).with("/var/gitlab-registry/..").and_return("/var")
    allow(File).to receive(:realpath).with("/var/gitlab-pages").and_return("/var/gitlab-pages")
    allow(File).to receive(:realpath).with("/var/gitlab-pages/..").and_return("/var")

    allow_next_instance_of(described_class) do |instance|
      allow(instance).to receive(:progress).and_return(progress)
    end
  end

  RSpec::Matchers.define :eq_statuslist do |expected|
    match do |actual|
      actual.map(&:exitstatus) == expected.map(&:exitstatus)
    end

    description do
      'be an Array of Process::Status with equal exitstatus against expected'
    end

    failure_message do |actual|
      "expected #{actual} exitstatuses list to be equal #{expected} exitstatuses list"
    end
  end

  describe '#restore' do
    subject(:files) { described_class.new(progress, '/var/gitlab-registry', options: backup_options) }

    let(:timestamp) { Time.utc(2017, 3, 22) }

    around do |example|
      travel_to(timestamp) { example.run }
    end

    describe 'folders with permission' do
      before do
        allow(files).to receive(:run_pipeline!).and_return([[true, true], ''])
        allow(files).to receive(:backup_existing_files).and_return(true)
        allow(files).to receive(:pipeline_succeeded?).and_return(true)
        found_files = %w[/var/gitlab-registry/. /var/gitlab-registry/.. /var/gitlab-registry/sample1]
        allow(Dir).to receive(:glob).with("/var/gitlab-registry/*", File::FNM_DOTMATCH).and_return(found_files)
      end

      it 'moves all necessary files' do
        allow(files).to receive(:backup_existing_files).and_call_original

        tmp_dir = backup_basepath.join('tmp', "registry.#{Time.now.to_i}")
        expect(FileUtils).to receive(:mv).with(['/var/gitlab-registry/sample1'], tmp_dir)

        files.restore('registry.tar.gz', 'backup_id')
      end

      it 'raises no errors' do
        expect { files.restore('registry.tar.gz', 'backup_id') }.not_to raise_error
      end

      it 'calls tar command with unlink' do
        expect(files).to receive(:tar).and_return('blabla-tar')

        expect(files).to receive(:run_pipeline!).with(
          ['gzip -cd', %w[blabla-tar --unlink-first --recursive-unlink -C /var/gitlab-registry -xf -]],
          any_args)
        expect(files).to receive(:pipeline_succeeded?).and_return(true)

        files.restore('registry.tar.gz', 'backup_id')
      end

      it 'raises an error on failure' do
        expect(files).to receive(:pipeline_succeeded?).and_return(false)

        expect { files.restore('registry.tar.gz', 'backup_id') }.to raise_error(/Restore operation failed:/)
      end
    end

    describe 'folders without permissions' do
      before do
        allow(FileUtils).to receive(:mv).and_raise(Errno::EACCES)
        allow(files).to receive(:run_pipeline!).and_return([[true, true], ''])
        allow(files).to receive(:pipeline_succeeded?).and_return(true)
      end

      it 'shows error message' do
        expect(files).to receive(:access_denied_error).with("/var/gitlab-registry")

        files.restore('registry.tar.gz', 'backup_id')
      end
    end

    describe 'folders that are a mountpoint' do
      before do
        allow(FileUtils).to receive(:mv).and_raise(Errno::EBUSY)
        allow(files).to receive(:run_pipeline!).and_return([[true, true], ''])
        allow(files).to receive(:pipeline_succeeded?).and_return(true)
      end

      it 'shows error message' do
        expect(files).to receive(:resource_busy_error).with("/var/gitlab-registry")
                             .and_call_original

        expect { files.restore('registry.tar.gz', 'backup_id') }.to raise_error(/is a mountpoint/)
      end
    end

    describe 'with DECOMPRESS_CMD' do
      before do
        stub_env('DECOMPRESS_CMD', 'tee')
        allow(files).to receive(:pipeline_succeeded?).and_return(true)
      end

      it 'passes through tee instead of gzip' do
        expect(files).to receive(:run_pipeline!).with(['tee', anything], any_args).and_return([[true, true], ''])

        expect do
          files.restore('registry.tar.gz', 'backup_id')
        end.to output(/Using custom DECOMPRESS_CMD 'tee'/).to_stdout
      end
    end
  end

  describe '#dump' do
    subject(:files) do
      described_class.new(progress, '/var/gitlab-pages', excludes: ['@pages.tmp'], options: backup_options)
    end

    before do
      allow(files).to receive(:run_pipeline!).and_return([[true, true], ''])
      allow(files).to receive(:pipeline_succeeded?).and_return(true)
    end

    it 'raises no errors' do
      expect { files.dump('registry.tar.gz', 'backup_id') }.not_to raise_error
    end

    it 'excludes tmp dirs from archive' do
      expect(files).to receive(:tar).and_return('blabla-tar')

      expect(files).to receive(:run_pipeline!).with(
        [%w[blabla-tar --exclude=lost+found --exclude=./@pages.tmp -C /var/gitlab-pages -cf - .], 'gzip -c -1'],
        any_args)
      files.dump('registry.tar.gz', 'backup_id')
    end

    it 'raises an error on failure' do
      allow(files).to receive(:run_pipeline!).and_return([[true, true], ''])
      expect(files).to receive(:pipeline_succeeded?).and_return(false)

      expect do
        files.dump('registry.tar.gz', 'backup_id')
      end.to raise_error(/Failed to create compressed file/)
    end

    describe 'with STRATEGY=copy' do
      before do
        stub_env('STRATEGY', 'copy')
        allow(files).to receive(:backup_basepath).and_return(Pathname('/var/gitlab-backup'))
        allow(File).to receive(:realpath).with('/var/gitlab-backup').and_return('/var/gitlab-backup')
      end

      it 'excludes tmp dirs from rsync' do
        cmd_args = %w[rsync -a --delete --exclude=lost+found --exclude=/gitlab-pages/@pages.tmp
          /var/gitlab-pages /var/gitlab-backup]
        expect(Gitlab::Popen).to receive(:popen).with(cmd_args).and_return(['', 0])

        files.dump('registry.tar.gz', 'backup_id')
      end

      it 'retries if rsync fails due to vanishing files' do
        cmd_args = %w[rsync -a --delete --exclude=lost+found --exclude=/gitlab-pages/@pages.tmp
          /var/gitlab-pages /var/gitlab-backup]
        expect(Gitlab::Popen).to receive(:popen).with(cmd_args).and_return(['rsync failed', 24], ['', 0])

        expect do
          files.dump('registry.tar.gz', 'backup_id')
        end.to output(/files vanished during rsync, retrying/).to_stdout
      end

      it 'raises an error and outputs an error message if rsync failed' do
        cmd_args = %w[rsync -a --delete --exclude=lost+found --exclude=/gitlab-pages/@pages.tmp
          /var/gitlab-pages /var/gitlab-backup]
        allow(Gitlab::Popen).to receive(:popen).with(cmd_args).and_return(['rsync failed', 1])

        expect do
          files.dump('registry.tar.gz', 'backup_id')
        end.to output(/rsync failed/).to_stdout
           .and raise_error(/Failed to create compressed file/)
      end
    end

    describe 'with COMPRESS_CMD' do
      before do
        stub_env('COMPRESS_CMD', 'tee')
      end

      it 'passes through tee instead of gzip' do
        expect(files).to receive(:run_pipeline!).with([anything, 'tee'], any_args)
        expect do
          files.dump('registry.tar.gz', 'backup_id')
        end.to output(/Using custom COMPRESS_CMD 'tee'/).to_stdout
      end
    end

    context 'when GZIP_RSYNCABLE is "yes"' do
      before do
        stub_env('GZIP_RSYNCABLE', 'yes')
      end

      it 'gzips the files with rsyncable option' do
        expect(files).to receive(:run_pipeline!).with([anything, 'gzip --rsyncable -c -1'], any_args)
        files.dump('registry.tar.gz', 'backup_id')
      end
    end

    context 'when GZIP_RSYNCABLE is not set' do
      it 'gzips the files without the rsyncable option' do
        expect(files).to receive(:run_pipeline!).with([anything, 'gzip -c -1'], any_args)
        files.dump('registry.tar.gz', 'backup_id')
      end
    end
  end

  describe '#exclude_dirs' do
    subject(:files) do
      described_class.new(progress, '/var/gitlab-pages', excludes: ['@pages.tmp'], options: backup_options)
    end

    it 'prepends a leading dot slash to tar excludes' do
      expect(files.exclude_dirs(:tar)).to eq(%w[--exclude=lost+found --exclude=./@pages.tmp])
    end

    it 'prepends a leading slash and app_files_dir basename to rsync excludes' do
      expect(files.exclude_dirs(:rsync)).to eq(%w[--exclude=lost+found --exclude=/gitlab-pages/@pages.tmp])
    end
  end

  describe '#run_pipeline!' do
    subject(:files) do
      described_class.new(progress, '/var/gitlab-registry', options: backup_options)
    end

    it 'executes an Open3.pipeline for cmd_list' do
      expect(Open3).to receive(:pipeline).with(%w[whew command], %w[another cmd], any_args)

      files.run_pipeline!([%w[whew command], %w[another cmd]])
    end

    it 'returns an empty output on success pipeline' do
      expect(files.run_pipeline!(%w[true true])[1]).to eq('')
    end

    it 'returns the stderr for failed pipeline' do
      expect(
        files.run_pipeline!(['echo OMG: failed command present 1>&2; false', 'true'])[1]
      ).to match(/OMG: failed/)
    end

    it 'returns the success status list on success pipeline' do
      expect(
        files.run_pipeline!(%w[true true])[0]
      ).to eq_statuslist([status_0, status_0])
    end

    it 'returns the failed status in status list for failed commands in pipeline' do
      expect(files.run_pipeline!(%w[false true true])[0]).to eq_statuslist([status_1, status_0, status_0])
      expect(files.run_pipeline!(%w[true false true])[0]).to eq_statuslist([status_0, status_1, status_0])
      expect(files.run_pipeline!(%w[false false true])[0]).to eq_statuslist([status_1, status_1, status_0])
      expect(files.run_pipeline!(%w[false true false])[0]).to eq_statuslist([status_1, status_0, status_1])
      expect(files.run_pipeline!(%w[false false false])[0]).to eq_statuslist([status_1, status_1, status_1])
    end
  end

  describe '#pipeline_succeeded?' do
    subject(:files) do
      described_class.new(progress, '/var/gitlab-registry', options: backup_options)
    end

    it 'returns true if both tar and gzip succeeeded' do
      expect(
        files.pipeline_succeeded?(tar_status: status_0, compress_status: status_0, output: 'any_output')
      ).to be_truthy
    end

    it 'returns false if gzip failed' do
      expect(
        files.pipeline_succeeded?(tar_status: status_1, compress_status: status_1, output: 'any_output')
      ).to be_falsey
    end

    context 'if gzip succeeded and tar failed non-critically' do
      before do
        allow(files).to receive(:tar_ignore_non_success?).and_return(true)
      end

      it 'returns true' do
        expect(
          files.pipeline_succeeded?(tar_status: status_1, compress_status: status_0, output: 'any_output')
        ).to be_truthy
      end
    end

    context 'if gzip succeeded and tar failed in other cases' do
      before do
        allow(files).to receive(:tar_ignore_non_success?).and_return(false)
      end

      it 'returns false' do
        expect(
          files.pipeline_succeeded?(tar_status: status_1, compress_status: status_0, output: 'any_output')
        ).to be_falsey
      end
    end
  end

  describe '#tar_ignore_non_success?' do
    subject(:files) do
      described_class.new(progress, '/var/gitlab-registry', options: backup_options)
    end

    context 'if `tar` command exits with 1 exitstatus' do
      it 'returns true' do
        expect(
          files.tar_ignore_non_success?(1, 'any_output')
        ).to be_truthy
      end

      it 'outputs a warning' do
        expect do
          files.tar_ignore_non_success?(1, 'any_output')
        end.to output(/Ignoring tar exit status 1/).to_stdout
      end
    end

    context 'if `tar` command exits with 2 exitstatus with non-critical warning' do
      before do
        allow(files).to receive(:noncritical_warning?).and_return(true)
      end

      it 'returns true' do
        expect(
          files.tar_ignore_non_success?(2, 'any_output')
        ).to be_truthy
      end

      it 'outputs a warning' do
        expect do
          files.tar_ignore_non_success?(2, 'any_output')
        end.to output(/Ignoring non-success exit status/).to_stdout
      end
    end

    context 'if `tar` command exits with any other unlisted error' do
      before do
        allow(files).to receive(:noncritical_warning?).and_return(false)
      end

      it 'returns false' do
        expect(
          files.tar_ignore_non_success?(2, 'any_output')
        ).to be_falsey
      end
    end
  end

  describe '#noncritical_warning?' do
    subject(:files) do
      described_class.new(progress, '/var/gitlab-registry', options: backup_options)
    end

    it 'returns true if given text matches noncritical warnings list' do
      expect(
        files.noncritical_warning?('tar: .: Cannot mkdir: No such file or directory')
      ).to be_truthy

      expect(
        files.noncritical_warning?('gtar: .: Cannot mkdir: No such file or directory')
      ).to be_truthy
    end

    it 'returns false otherwize' do
      expect(
        files.noncritical_warning?('unknown message')
      ).to be_falsey
    end
  end
end