2019-07-25 01:21:37 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-30 02:08:30 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 05:08:32 -04:00
|
|
|
RSpec.describe Backup::Files do
|
2018-03-30 02:08:30 -04:00
|
|
|
let(:progress) { StringIO.new }
|
|
|
|
let!(:project) { create(:project) }
|
|
|
|
|
2020-10-29 05:08:38 -04:00
|
|
|
let(:status_0) { double('exit 0', success?: true, exitstatus: 0) }
|
|
|
|
let(:status_1) { double('exit 1', success?: false, exitstatus: 1) }
|
|
|
|
let(:status_2) { double('exit 2', success?: false, exitstatus: 2) }
|
|
|
|
|
2018-03-30 02:08:30 -04:00
|
|
|
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)
|
2018-03-30 07:11:51 -04:00
|
|
|
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")
|
2020-09-14 08:09:34 -04:00
|
|
|
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")
|
2018-03-30 02:08:30 -04:00
|
|
|
|
|
|
|
allow_any_instance_of(described_class).to receive(:progress).and_return(progress)
|
|
|
|
end
|
|
|
|
|
2020-10-29 05:08:38 -04:00
|
|
|
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
|
|
|
|
|
2018-03-30 02:08:30 -04:00
|
|
|
describe '#restore' do
|
2022-04-08 17:09:52 -04:00
|
|
|
subject { described_class.new(progress, '/var/gitlab-registry') }
|
2019-10-18 07:11:44 -04:00
|
|
|
|
2018-03-30 02:08:30 -04:00
|
|
|
let(:timestamp) { Time.utc(2017, 3, 22) }
|
|
|
|
|
|
|
|
around do |example|
|
2020-10-02 08:09:03 -04:00
|
|
|
travel_to(timestamp) { example.run }
|
2018-03-30 02:08:30 -04:00
|
|
|
end
|
|
|
|
|
2018-03-30 07:11:51 -04:00
|
|
|
describe 'folders with permission' do
|
|
|
|
before do
|
2020-10-29 05:08:38 -04:00
|
|
|
allow(subject).to receive(:run_pipeline!).and_return([[true, true], ''])
|
2018-03-30 07:11:51 -04:00
|
|
|
allow(subject).to receive(:backup_existing_files).and_return(true)
|
2020-10-29 05:08:38 -04:00
|
|
|
allow(subject).to receive(:pipeline_succeeded?).and_return(true)
|
2018-03-30 07:11:51 -04:00
|
|
|
allow(Dir).to receive(:glob).with("/var/gitlab-registry/*", File::FNM_DOTMATCH).and_return(["/var/gitlab-registry/.", "/var/gitlab-registry/..", "/var/gitlab-registry/sample1"])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'moves all necessary files' do
|
|
|
|
allow(subject).to receive(:backup_existing_files).and_call_original
|
|
|
|
expect(FileUtils).to receive(:mv).with(["/var/gitlab-registry/sample1"], File.join(Gitlab.config.backup.path, "tmp", "registry.#{Time.now.to_i}"))
|
2022-03-14 23:08:45 -04:00
|
|
|
subject.restore('registry.tar.gz')
|
2018-03-30 07:11:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises no errors' do
|
2022-03-14 23:08:45 -04:00
|
|
|
expect { subject.restore('registry.tar.gz') }.not_to raise_error
|
2018-03-30 07:11:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'calls tar command with unlink' do
|
2018-06-05 11:51:14 -04:00
|
|
|
expect(subject).to receive(:tar).and_return('blabla-tar')
|
|
|
|
|
|
|
|
expect(subject).to receive(:run_pipeline!).with([%w(gzip -cd), %w(blabla-tar --unlink-first --recursive-unlink -C /var/gitlab-registry -xf -)], any_args)
|
2020-10-29 05:08:38 -04:00
|
|
|
expect(subject).to receive(:pipeline_succeeded?).and_return(true)
|
2022-03-14 23:08:45 -04:00
|
|
|
subject.restore('registry.tar.gz')
|
2018-03-30 07:11:51 -04:00
|
|
|
end
|
2020-10-29 05:08:38 -04:00
|
|
|
|
|
|
|
it 'raises an error on failure' do
|
|
|
|
expect(subject).to receive(:pipeline_succeeded?).and_return(false)
|
|
|
|
|
2022-03-14 23:08:45 -04:00
|
|
|
expect { subject.restore('registry.tar.gz') }.to raise_error(/Restore operation failed:/)
|
2020-10-29 05:08:38 -04:00
|
|
|
end
|
2018-03-30 07:11:51 -04:00
|
|
|
end
|
|
|
|
|
2018-03-30 02:08:30 -04:00
|
|
|
describe 'folders without permissions' do
|
|
|
|
before do
|
|
|
|
allow(FileUtils).to receive(:mv).and_raise(Errno::EACCES)
|
2020-10-29 05:08:38 -04:00
|
|
|
allow(subject).to receive(:run_pipeline!).and_return([[true, true], ''])
|
|
|
|
allow(subject).to receive(:pipeline_succeeded?).and_return(true)
|
2018-03-30 02:08:30 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'shows error message' do
|
|
|
|
expect(subject).to receive(:access_denied_error).with("/var/gitlab-registry")
|
2022-03-14 23:08:45 -04:00
|
|
|
subject.restore('registry.tar.gz')
|
2018-03-30 02:08:30 -04:00
|
|
|
end
|
|
|
|
end
|
2018-04-23 05:40:55 -04:00
|
|
|
|
|
|
|
describe 'folders that are a mountpoint' do
|
|
|
|
before do
|
|
|
|
allow(FileUtils).to receive(:mv).and_raise(Errno::EBUSY)
|
2020-10-29 05:08:38 -04:00
|
|
|
allow(subject).to receive(:run_pipeline!).and_return([[true, true], ''])
|
|
|
|
allow(subject).to receive(:pipeline_succeeded?).and_return(true)
|
2018-04-23 05:40:55 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'shows error message' do
|
|
|
|
expect(subject).to receive(:resource_busy_error).with("/var/gitlab-registry")
|
|
|
|
.and_call_original
|
|
|
|
|
2022-03-14 23:08:45 -04:00
|
|
|
expect { subject.restore('registry.tar.gz') }.to raise_error(/is a mountpoint/)
|
2018-04-23 05:40:55 -04:00
|
|
|
end
|
|
|
|
end
|
2018-03-30 02:08:30 -04:00
|
|
|
end
|
2020-09-14 08:09:34 -04:00
|
|
|
|
|
|
|
describe '#dump' do
|
2022-04-08 17:09:52 -04:00
|
|
|
subject { described_class.new(progress, '/var/gitlab-pages', excludes: ['@pages.tmp']) }
|
2020-09-14 08:09:34 -04:00
|
|
|
|
|
|
|
before do
|
2020-10-29 05:08:38 -04:00
|
|
|
allow(subject).to receive(:run_pipeline!).and_return([[true, true], ''])
|
|
|
|
allow(subject).to receive(:pipeline_succeeded?).and_return(true)
|
2020-09-14 08:09:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises no errors' do
|
2022-03-31 08:08:17 -04:00
|
|
|
expect { subject.dump('registry.tar.gz', 'backup_id') }.not_to raise_error
|
2020-09-14 08:09:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'excludes tmp dirs from archive' do
|
|
|
|
expect(subject).to receive(:tar).and_return('blabla-tar')
|
|
|
|
|
|
|
|
expect(subject).to receive(:run_pipeline!).with([%w(blabla-tar --exclude=lost+found --exclude=./@pages.tmp -C /var/gitlab-pages -cf - .), 'gzip -c -1'], any_args)
|
2022-03-31 08:08:17 -04:00
|
|
|
subject.dump('registry.tar.gz', 'backup_id')
|
2020-09-14 08:09:34 -04:00
|
|
|
end
|
|
|
|
|
2020-10-29 05:08:38 -04:00
|
|
|
it 'raises an error on failure' do
|
|
|
|
allow(subject).to receive(:run_pipeline!).and_return([[true, true], ''])
|
|
|
|
expect(subject).to receive(:pipeline_succeeded?).and_return(false)
|
|
|
|
|
|
|
|
expect do
|
2022-03-31 08:08:17 -04:00
|
|
|
subject.dump('registry.tar.gz', 'backup_id')
|
2022-01-06 13:13:45 -05:00
|
|
|
end.to raise_error(/Failed to create compressed file/)
|
2020-10-29 05:08:38 -04:00
|
|
|
end
|
|
|
|
|
2020-09-14 08:09:34 -04:00
|
|
|
describe 'with STRATEGY=copy' do
|
|
|
|
before do
|
|
|
|
stub_env('STRATEGY', 'copy')
|
|
|
|
allow(Gitlab.config.backup).to receive(:path) { '/var/gitlab-backup' }
|
|
|
|
allow(File).to receive(:realpath).with("/var/gitlab-backup").and_return("/var/gitlab-backup")
|
2020-10-29 05:08:38 -04:00
|
|
|
end
|
2020-09-14 08:09:34 -04:00
|
|
|
|
2020-10-29 05:08:38 -04:00
|
|
|
it 'excludes tmp dirs from rsync' do
|
2020-12-13 19:10:01 -05:00
|
|
|
expect(Gitlab::Popen).to receive(:popen)
|
2021-01-27 07:09:01 -05:00
|
|
|
.with(%w(rsync -a --delete --exclude=lost+found --exclude=/gitlab-pages/@pages.tmp /var/gitlab-pages /var/gitlab-backup))
|
2020-12-13 19:10:01 -05:00
|
|
|
.and_return(['', 0])
|
2020-09-14 08:09:34 -04:00
|
|
|
|
2022-03-31 08:08:17 -04:00
|
|
|
subject.dump('registry.tar.gz', 'backup_id')
|
2020-09-14 08:09:34 -04:00
|
|
|
end
|
2020-10-29 05:08:38 -04:00
|
|
|
|
2020-12-13 19:10:01 -05:00
|
|
|
it 'retries if rsync fails due to vanishing files' do
|
|
|
|
expect(Gitlab::Popen).to receive(:popen)
|
2021-01-27 07:09:01 -05:00
|
|
|
.with(%w(rsync -a --delete --exclude=lost+found --exclude=/gitlab-pages/@pages.tmp /var/gitlab-pages /var/gitlab-backup))
|
2020-12-13 19:10:01 -05:00
|
|
|
.and_return(['rsync failed', 24], ['', 0])
|
|
|
|
|
|
|
|
expect do
|
2022-03-31 08:08:17 -04:00
|
|
|
subject.dump('registry.tar.gz', 'backup_id')
|
2020-12-13 19:10:01 -05:00
|
|
|
end.to output(/files vanished during rsync, retrying/).to_stdout
|
|
|
|
end
|
|
|
|
|
2020-10-29 05:08:38 -04:00
|
|
|
it 'raises an error and outputs an error message if rsync failed' do
|
2020-12-13 19:10:01 -05:00
|
|
|
allow(Gitlab::Popen).to receive(:popen)
|
2021-01-27 07:09:01 -05:00
|
|
|
.with(%w(rsync -a --delete --exclude=lost+found --exclude=/gitlab-pages/@pages.tmp /var/gitlab-pages /var/gitlab-backup))
|
2020-12-13 19:10:01 -05:00
|
|
|
.and_return(['rsync failed', 1])
|
2020-10-29 05:08:38 -04:00
|
|
|
|
|
|
|
expect do
|
2022-03-31 08:08:17 -04:00
|
|
|
subject.dump('registry.tar.gz', 'backup_id')
|
2020-10-29 05:08:38 -04:00
|
|
|
end.to output(/rsync failed/).to_stdout
|
2022-01-06 13:13:45 -05:00
|
|
|
.and raise_error(/Failed to create compressed file/)
|
2020-10-29 05:08:38 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#exclude_dirs' do
|
2022-04-08 17:09:52 -04:00
|
|
|
subject { described_class.new(progress, '/var/gitlab-pages', excludes: ['@pages.tmp']) }
|
2020-10-29 05:08:38 -04:00
|
|
|
|
|
|
|
it 'prepends a leading dot slash to tar excludes' do
|
|
|
|
expect(subject.exclude_dirs(:tar)).to eq(['--exclude=lost+found', '--exclude=./@pages.tmp'])
|
|
|
|
end
|
|
|
|
|
2021-01-27 07:09:01 -05:00
|
|
|
it 'prepends a leading slash and app_files_dir basename to rsync excludes' do
|
|
|
|
expect(subject.exclude_dirs(:rsync)).to eq(['--exclude=lost+found', '--exclude=/gitlab-pages/@pages.tmp'])
|
2020-10-29 05:08:38 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#run_pipeline!' do
|
2022-04-08 17:09:52 -04:00
|
|
|
subject { described_class.new(progress, '/var/gitlab-registry') }
|
2020-10-29 05:08:38 -04:00
|
|
|
|
|
|
|
it 'executes an Open3.pipeline for cmd_list' do
|
|
|
|
expect(Open3).to receive(:pipeline).with(%w[whew command], %w[another cmd], any_args)
|
|
|
|
|
|
|
|
subject.run_pipeline!([%w[whew command], %w[another cmd]])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns an empty output on success pipeline' do
|
|
|
|
expect(subject.run_pipeline!(%w[true true])[1]).to eq('')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the stderr for failed pipeline' do
|
|
|
|
expect(
|
|
|
|
subject.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(
|
|
|
|
subject.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(subject.run_pipeline!(%w[false true true])[0]).to eq_statuslist([status_1, status_0, status_0])
|
|
|
|
expect(subject.run_pipeline!(%w[true false true])[0]).to eq_statuslist([status_0, status_1, status_0])
|
|
|
|
expect(subject.run_pipeline!(%w[false false true])[0]).to eq_statuslist([status_1, status_1, status_0])
|
|
|
|
expect(subject.run_pipeline!(%w[false true false])[0]).to eq_statuslist([status_1, status_0, status_1])
|
|
|
|
expect(subject.run_pipeline!(%w[false false false])[0]).to eq_statuslist([status_1, status_1, status_1])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#pipeline_succeeded?' do
|
2022-04-08 17:09:52 -04:00
|
|
|
subject { described_class.new(progress, '/var/gitlab-registry') }
|
2020-10-29 05:08:38 -04:00
|
|
|
|
|
|
|
it 'returns true if both tar and gzip succeeeded' do
|
|
|
|
expect(
|
|
|
|
subject.pipeline_succeeded?(tar_status: status_0, gzip_status: status_0, output: 'any_output')
|
|
|
|
).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false if gzip failed' do
|
|
|
|
expect(
|
|
|
|
subject.pipeline_succeeded?(tar_status: status_1, gzip_status: status_1, output: 'any_output')
|
|
|
|
).to be_falsey
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'if gzip succeeded and tar failed non-critically' do
|
|
|
|
before do
|
|
|
|
allow(subject).to receive(:tar_ignore_non_success?).and_return(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns true' do
|
|
|
|
expect(
|
|
|
|
subject.pipeline_succeeded?(tar_status: status_1, gzip_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(subject).to receive(:tar_ignore_non_success?).and_return(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false' do
|
|
|
|
expect(
|
|
|
|
subject.pipeline_succeeded?(tar_status: status_1, gzip_status: status_0, output: 'any_output')
|
|
|
|
).to be_falsey
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#tar_ignore_non_success?' do
|
2022-04-08 17:09:52 -04:00
|
|
|
subject { described_class.new(progress, '/var/gitlab-registry') }
|
2020-10-29 05:08:38 -04:00
|
|
|
|
|
|
|
context 'if `tar` command exits with 1 exitstatus' do
|
|
|
|
it 'returns true' do
|
|
|
|
expect(
|
|
|
|
subject.tar_ignore_non_success?(1, 'any_output')
|
|
|
|
).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'outputs a warning' do
|
|
|
|
expect do
|
|
|
|
subject.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(subject).to receive(:noncritical_warning?).and_return(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns true' do
|
|
|
|
expect(
|
|
|
|
subject.tar_ignore_non_success?(2, 'any_output')
|
|
|
|
).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'outputs a warning' do
|
|
|
|
expect do
|
|
|
|
subject.tar_ignore_non_success?(2, 'any_output')
|
|
|
|
end.to output(/Ignoring non-success exit status/).to_stdout
|
|
|
|
end
|
2020-09-14 08:09:34 -04:00
|
|
|
end
|
|
|
|
|
2020-10-29 05:08:38 -04:00
|
|
|
context 'if `tar` command exits with any other unlisted error' do
|
|
|
|
before do
|
|
|
|
allow(subject).to receive(:noncritical_warning?).and_return(false)
|
2020-09-14 08:09:34 -04:00
|
|
|
end
|
|
|
|
|
2020-10-29 05:08:38 -04:00
|
|
|
it 'returns false' do
|
|
|
|
expect(
|
|
|
|
subject.tar_ignore_non_success?(2, 'any_output')
|
|
|
|
).to be_falsey
|
2020-09-14 08:09:34 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-10-29 05:08:38 -04:00
|
|
|
|
|
|
|
describe '#noncritical_warning?' do
|
2022-04-08 17:09:52 -04:00
|
|
|
subject { described_class.new(progress, '/var/gitlab-registry') }
|
2020-10-29 05:08:38 -04:00
|
|
|
|
|
|
|
it 'returns true if given text matches noncritical warnings list' do
|
|
|
|
expect(
|
|
|
|
subject.noncritical_warning?('tar: .: Cannot mkdir: No such file or directory')
|
|
|
|
).to be_truthy
|
|
|
|
|
|
|
|
expect(
|
|
|
|
subject.noncritical_warning?('gtar: .: Cannot mkdir: No such file or directory')
|
|
|
|
).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false otherwize' do
|
|
|
|
expect(
|
|
|
|
subject.noncritical_warning?('unknown message')
|
|
|
|
).to be_falsey
|
|
|
|
end
|
|
|
|
end
|
2018-03-30 02:08:30 -04:00
|
|
|
end
|