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) }
|
|
|
|
|
|
|
|
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")
|
2018-03-30 02:08:30 -04:00
|
|
|
|
|
|
|
allow_any_instance_of(String).to receive(:color) do |string, _color|
|
|
|
|
string
|
|
|
|
end
|
|
|
|
|
|
|
|
allow_any_instance_of(described_class).to receive(:progress).and_return(progress)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#restore' do
|
2018-03-30 03:53:53 -04:00
|
|
|
subject { described_class.new('registry', '/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|
|
|
|
|
Timecop.freeze(timestamp) { example.run }
|
|
|
|
end
|
|
|
|
|
2018-03-30 07:11:51 -04:00
|
|
|
describe 'folders with permission' do
|
|
|
|
before do
|
|
|
|
allow(subject).to receive(:run_pipeline!).and_return(true)
|
|
|
|
allow(subject).to receive(:backup_existing_files).and_return(true)
|
|
|
|
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}"))
|
|
|
|
subject.restore
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises no errors' do
|
|
|
|
expect { subject.restore }.not_to raise_error
|
|
|
|
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)
|
2018-03-30 07:11:51 -04:00
|
|
|
subject.restore
|
|
|
|
end
|
|
|
|
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)
|
|
|
|
allow(subject).to receive(:run_pipeline!).and_return(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'shows error message' do
|
|
|
|
expect(subject).to receive(:access_denied_error).with("/var/gitlab-registry")
|
|
|
|
subject.restore
|
|
|
|
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)
|
|
|
|
allow(subject).to receive(:run_pipeline!).and_return(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'shows error message' do
|
|
|
|
expect(subject).to receive(:resource_busy_error).with("/var/gitlab-registry")
|
|
|
|
.and_call_original
|
|
|
|
|
|
|
|
expect { subject.restore }.to raise_error(/is a mountpoint/)
|
|
|
|
end
|
|
|
|
end
|
2018-03-30 02:08:30 -04:00
|
|
|
end
|
|
|
|
end
|