diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-08-19 09:08:42 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-08-19 09:08:42 +0000 |
commit | b76ae638462ab0f673e5915986070518dd3f9ad3 (patch) | |
tree | bdab0533383b52873be0ec0eb4d3c66598ff8b91 /spec/lib/backup | |
parent | 434373eabe7b4be9593d18a585fb763f1e5f1a6f (diff) | |
download | gitlab-ce-b76ae638462ab0f673e5915986070518dd3f9ad3.tar.gz |
Add latest changes from gitlab-org/gitlab@14-2-stable-eev14.2.0-rc42
Diffstat (limited to 'spec/lib/backup')
-rw-r--r-- | spec/lib/backup/database_backup_error_spec.rb | 30 | ||||
-rw-r--r-- | spec/lib/backup/file_backup_error_spec.rb | 35 | ||||
-rw-r--r-- | spec/lib/backup/gitaly_backup_spec.rb | 10 | ||||
-rw-r--r-- | spec/lib/backup/manager_spec.rb | 81 | ||||
-rw-r--r-- | spec/lib/backup/repository_backup_error_spec.rb | 42 |
5 files changed, 175 insertions, 23 deletions
diff --git a/spec/lib/backup/database_backup_error_spec.rb b/spec/lib/backup/database_backup_error_spec.rb new file mode 100644 index 00000000000..ef627900050 --- /dev/null +++ b/spec/lib/backup/database_backup_error_spec.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Backup::DatabaseBackupError do + let(:config) do + { + host: 'localhost', + port: 5432, + database: 'gitlabhq_test' + } + end + + let(:db_file_name) { File.join(Gitlab.config.backup.path, 'db', 'database.sql.gz') } + + subject { described_class.new(config, db_file_name) } + + it { is_expected.to respond_to :config } + it { is_expected.to respond_to :db_file_name } + + it 'expects exception message to include database file' do + expect(subject.message).to include("#{db_file_name}") + end + + it 'expects exception message to include database paths being back-up' do + expect(subject.message).to include("#{config[:host]}") + expect(subject.message).to include("#{config[:port]}") + expect(subject.message).to include("#{config[:database]}") + end +end diff --git a/spec/lib/backup/file_backup_error_spec.rb b/spec/lib/backup/file_backup_error_spec.rb new file mode 100644 index 00000000000..bb174bbe4a0 --- /dev/null +++ b/spec/lib/backup/file_backup_error_spec.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Backup::FileBackupError do + let_it_be(:lfs) { create(:lfs_object) } + let_it_be(:upload) { create(:upload) } + + let(:backup_tarball) { '/tmp/backup/uploads' } + + shared_examples 'includes backup path' do + it { is_expected.to respond_to :app_files_dir } + it { is_expected.to respond_to :backup_tarball } + + it 'expects exception message to include file backup path location' do + expect(subject.message).to include("#{subject.backup_tarball}") + end + + it 'expects exception message to include file being back-up' do + expect(subject.message).to include("#{subject.app_files_dir}") + end + end + + context 'with lfs file' do + subject { described_class.new(lfs, backup_tarball) } + + it_behaves_like 'includes backup path' + end + + context 'with uploads file' do + subject { described_class.new(upload, backup_tarball) } + + it_behaves_like 'includes backup path' + end +end diff --git a/spec/lib/backup/gitaly_backup_spec.rb b/spec/lib/backup/gitaly_backup_spec.rb index cdb35c0ce01..a48a1752eff 100644 --- a/spec/lib/backup/gitaly_backup_spec.rb +++ b/spec/lib/backup/gitaly_backup_spec.rb @@ -32,7 +32,7 @@ RSpec.describe Backup::GitalyBackup do project_snippet = create(:project_snippet, :repository, project: project) personal_snippet = create(:personal_snippet, :repository, author: project.owner) - expect(Process).to receive(:spawn).with(anything, 'create', '-path', anything, { in: anything, out: progress }).and_call_original + expect(Open3).to receive(:popen2).with(ENV, anything, 'create', '-path', anything).and_call_original subject.start(:create) subject.enqueue(project, Gitlab::GlRepository::PROJECT) @@ -53,7 +53,7 @@ RSpec.describe Backup::GitalyBackup do let(:parallel) { 3 } it 'passes parallel option through' do - expect(Process).to receive(:spawn).with(anything, 'create', '-path', anything, '-parallel', '3', { in: anything, out: progress }).and_call_original + expect(Open3).to receive(:popen2).with(ENV, anything, 'create', '-path', anything, '-parallel', '3').and_call_original subject.start(:create) subject.wait @@ -64,7 +64,7 @@ RSpec.describe Backup::GitalyBackup do let(:parallel_storage) { 3 } it 'passes parallel option through' do - expect(Process).to receive(:spawn).with(anything, 'create', '-path', anything, '-parallel-storage', '3', { in: anything, out: progress }).and_call_original + expect(Open3).to receive(:popen2).with(ENV, anything, 'create', '-path', anything, '-parallel-storage', '3').and_call_original subject.start(:create) subject.wait @@ -109,7 +109,7 @@ RSpec.describe Backup::GitalyBackup do copy_bundle_to_backup_path('personal_snippet_repo.bundle', personal_snippet.disk_path + '.bundle') copy_bundle_to_backup_path('project_snippet_repo.bundle', project_snippet.disk_path + '.bundle') - expect(Process).to receive(:spawn).with(anything, 'restore', '-path', anything, { in: anything, out: progress }).and_call_original + expect(Open3).to receive(:popen2).with(ENV, anything, 'restore', '-path', anything).and_call_original subject.start(:restore) subject.enqueue(project, Gitlab::GlRepository::PROJECT) @@ -132,7 +132,7 @@ RSpec.describe Backup::GitalyBackup do let(:parallel) { 3 } it 'does not pass parallel option through' do - expect(Process).to receive(:spawn).with(anything, 'restore', '-path', anything, { in: anything, out: progress }).and_call_original + expect(Open3).to receive(:popen2).with(ENV, anything, 'restore', '-path', anything).and_call_original subject.start(:restore) subject.wait diff --git a/spec/lib/backup/manager_spec.rb b/spec/lib/backup/manager_spec.rb index feaca6164eb..2cc1bf41d18 100644 --- a/spec/lib/backup/manager_spec.rb +++ b/spec/lib/backup/manager_spec.rb @@ -12,20 +12,13 @@ RSpec.describe Backup::Manager do before do allow(progress).to receive(:puts) allow(progress).to receive(:print) - - @old_progress = $progress # rubocop:disable Style/GlobalVars - $progress = progress # rubocop:disable Style/GlobalVars - end - - after do - $progress = @old_progress # rubocop:disable Style/GlobalVars end describe '#pack' do - let(:backup_contents) { ['backup_contents'] } + let(:expected_backup_contents) { %w(repositories db uploads.tar.gz builds.tar.gz artifacts.tar.gz pages.tar.gz lfs.tar.gz backup_information.yml) } + let(:tar_file) { '1546300800_2019_01_01_12.3_gitlab_backup.tar' } let(:tar_system_options) { { out: [tar_file, 'w', Gitlab.config.backup.archive_permissions] } } - let(:tar_cmdline) { ['tar', '-cf', '-', *backup_contents, tar_system_options] } - + let(:tar_cmdline) { ['tar', '-cf', '-', *expected_backup_contents, tar_system_options] } let(:backup_information) do { backup_created_at: Time.zone.parse('2019-01-01'), @@ -36,20 +29,20 @@ RSpec.describe Backup::Manager do before do allow(ActiveRecord::Base.connection).to receive(:reconnect!) allow(Kernel).to receive(:system).and_return(true) + allow(YAML).to receive(:load_file).and_return(backup_information) + + ::Backup::Manager::FOLDERS_TO_BACKUP.each do |folder| + allow(Dir).to receive(:exist?).with(File.join(Gitlab.config.backup.path, folder)).and_return(true) + end - allow(subject).to receive(:backup_contents).and_return(backup_contents) allow(subject).to receive(:backup_information).and_return(backup_information) allow(subject).to receive(:upload) end - context 'when BACKUP is not set' do - let(:tar_file) { '1546300800_2019_01_01_12.3_gitlab_backup.tar' } - - it 'uses the default tar file name' do - subject.pack + it 'executes tar' do + subject.pack - expect(Kernel).to have_received(:system).with(*tar_cmdline) - end + expect(Kernel).to have_received(:system).with(*tar_cmdline) end context 'when BACKUP is set' do @@ -62,6 +55,58 @@ RSpec.describe Backup::Manager do expect(Kernel).to have_received(:system).with(*tar_cmdline) end end + + context 'when skipped is set in backup_information.yml' do + let(:expected_backup_contents) { %w{db uploads.tar.gz builds.tar.gz artifacts.tar.gz pages.tar.gz lfs.tar.gz backup_information.yml} } + let(:backup_information) do + { + backup_created_at: Time.zone.parse('2019-01-01'), + gitlab_version: '12.3', + skipped: ['repositories'] + } + end + + it 'executes tar' do + subject.pack + + expect(Kernel).to have_received(:system).with(*tar_cmdline) + end + end + + context 'when a directory does not exist' do + let(:expected_backup_contents) { %w{db uploads.tar.gz builds.tar.gz artifacts.tar.gz pages.tar.gz lfs.tar.gz backup_information.yml} } + + before do + expect(Dir).to receive(:exist?).with(File.join(Gitlab.config.backup.path, 'repositories')).and_return(false) + end + + it 'executes tar' do + subject.pack + + expect(Kernel).to have_received(:system).with(*tar_cmdline) + end + end + end + + describe '#remove_tmp' do + let(:path) { File.join(Gitlab.config.backup.path, 'tmp') } + + before do + allow(FileUtils).to receive(:rm_rf).and_return(true) + end + + it 'removes backups/tmp dir' do + subject.remove_tmp + + expect(FileUtils).to have_received(:rm_rf).with(path) + end + + it 'prints running task with a done confirmation' do + subject.remove_tmp + + expect(progress).to have_received(:print).with('Deleting backups/tmp ... ') + expect(progress).to have_received(:puts).with('done') + end end describe '#remove_old' do diff --git a/spec/lib/backup/repository_backup_error_spec.rb b/spec/lib/backup/repository_backup_error_spec.rb new file mode 100644 index 00000000000..44c75c1cf77 --- /dev/null +++ b/spec/lib/backup/repository_backup_error_spec.rb @@ -0,0 +1,42 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Backup::RepositoryBackupError do + let_it_be(:snippet) { create(:snippet, content: 'foo', file_name: 'foo') } + let_it_be(:project) { create(:project, :repository) } + let_it_be(:wiki) { ProjectWiki.new(project, nil ) } + + let(:backup_repos_path) { '/tmp/backup/repositories' } + + shared_examples 'includes backup path' do + it { is_expected.to respond_to :container } + it { is_expected.to respond_to :backup_repos_path } + + it 'expects exception message to include repo backup path location' do + expect(subject.message).to include("#{subject.backup_repos_path}") + end + + it 'expects exception message to include container being back-up' do + expect(subject.message).to include("#{subject.container.disk_path}") + end + end + + context 'with snippet repository' do + subject { described_class.new(snippet, backup_repos_path) } + + it_behaves_like 'includes backup path' + end + + context 'with project repository' do + subject { described_class.new(project, backup_repos_path) } + + it_behaves_like 'includes backup path' + end + + context 'with wiki repository' do + subject { described_class.new(wiki, backup_repos_path) } + + it_behaves_like 'includes backup path' + end +end |