diff options
Diffstat (limited to 'spec/lib/backup')
-rw-r--r-- | spec/lib/backup/database_spec.rb | 41 | ||||
-rw-r--r-- | spec/lib/backup/gitaly_backup_spec.rb | 11 | ||||
-rw-r--r-- | spec/lib/backup/manager_spec.rb | 52 | ||||
-rw-r--r-- | spec/lib/backup/repositories_spec.rb | 10 |
4 files changed, 68 insertions, 46 deletions
diff --git a/spec/lib/backup/database_spec.rb b/spec/lib/backup/database_spec.rb index c70d47e4940..f0cee8ce36a 100644 --- a/spec/lib/backup/database_spec.rb +++ b/spec/lib/backup/database_spec.rb @@ -11,12 +11,17 @@ end RSpec.describe Backup::Database, feature_category: :backup_restore do let(:progress) { StringIO.new } let(:output) { progress.string } - let(:one_db_configured?) { Gitlab::Database.database_base_models.one? } - let(:database_models_for_backup) { Gitlab::Database.database_base_models_with_gitlab_shared } + let(:one_database_configured?) { base_models_for_backup.one? } let(:timeout_service) do instance_double(Gitlab::Database::TransactionTimeoutSettings, restore_timeouts: nil, disable_timeouts: nil) end + let(:base_models_for_backup) do + Gitlab::Database.database_base_models_with_gitlab_shared.select do |database_name| + Gitlab::Database.has_database?(database_name) + end + end + before(:all) do Rake::Task.define_task(:environment) Rake.application.rake_require 'active_record/railties/databases' @@ -33,7 +38,7 @@ RSpec.describe Backup::Database, feature_category: :backup_restore do subject { described_class.new(progress, force: force) } before do - database_models_for_backup.each do |database_name, base_model| + base_models_for_backup.each do |_, base_model| base_model.connection.rollback_transaction unless base_model.connection.open_transactions.zero? allow(base_model.connection).to receive(:execute).and_call_original end @@ -43,7 +48,7 @@ RSpec.describe Backup::Database, feature_category: :backup_restore do Dir.mktmpdir do |dir| subject.dump(dir, backup_id) - database_models_for_backup.each_key do |database_name| + base_models_for_backup.each_key do |database_name| filename = database_name == 'main' ? 'database.sql.gz' : "#{database_name}_database.sql.gz" expect(File.exist?(File.join(dir, filename))).to eq(true) end @@ -56,8 +61,8 @@ RSpec.describe Backup::Database, feature_category: :backup_restore do expect(base_model.connection).to receive(:begin_transaction).with( isolation: :repeatable_read ).and_call_original - expect(base_model.connection).to receive(:execute).with( - "SELECT pg_export_snapshot() as snapshot_id;" + expect(base_model.connection).to receive(:select_value).with( + "SELECT pg_export_snapshot()" ).and_call_original expect(base_model.connection).to receive(:rollback_transaction).and_call_original @@ -66,7 +71,7 @@ RSpec.describe Backup::Database, feature_category: :backup_restore do end it 'disables transaction time out' do - number_of_databases = Gitlab::Database.database_base_models_with_gitlab_shared.count + number_of_databases = base_models_for_backup.count expect(Gitlab::Database::TransactionTimeoutSettings) .to receive(:new).exactly(2 * number_of_databases).times.and_return(timeout_service) expect(timeout_service).to receive(:disable_timeouts).exactly(number_of_databases).times @@ -94,10 +99,10 @@ RSpec.describe Backup::Database, feature_category: :backup_restore do allow(Backup::Dump::Postgres).to receive(:new).and_return(dumper) allow(dumper).to receive(:dump).with(any_args).and_return(true) - database_models_for_backup.each do |database_name, base_model| - allow(base_model.connection).to receive(:execute).with( - "SELECT pg_export_snapshot() as snapshot_id;" - ).and_return(['snapshot_id' => snapshot_id]) + base_models_for_backup.each do |_, base_model| + allow(base_model.connection).to receive(:select_value).with( + "SELECT pg_export_snapshot()" + ).and_return(snapshot_id) end end @@ -134,7 +139,7 @@ RSpec.describe Backup::Database, feature_category: :backup_restore do it 'restores timeouts' do Dir.mktmpdir do |dir| - number_of_databases = Gitlab::Database.database_base_models_with_gitlab_shared.count + number_of_databases = base_models_for_backup.count expect(Gitlab::Database::TransactionTimeoutSettings) .to receive(:new).exactly(number_of_databases).times.and_return(timeout_service) expect(timeout_service).to receive(:restore_timeouts).exactly(number_of_databases).times @@ -165,7 +170,7 @@ RSpec.describe Backup::Database, feature_category: :backup_restore do it 'warns the user and waits' do expect(subject).to receive(:sleep) - if one_db_configured? + if one_database_configured? expect(Rake::Task['gitlab:db:drop_tables']).to receive(:invoke) else expect(Rake::Task['gitlab:db:drop_tables:main']).to receive(:invoke) @@ -183,7 +188,7 @@ RSpec.describe Backup::Database, feature_category: :backup_restore do context 'with an empty .gz file' do it 'returns successfully' do - if one_db_configured? + if one_database_configured? expect(Rake::Task['gitlab:db:drop_tables']).to receive(:invoke) else expect(Rake::Task['gitlab:db:drop_tables:main']).to receive(:invoke) @@ -203,7 +208,7 @@ RSpec.describe Backup::Database, feature_category: :backup_restore do end it 'raises a backup error' do - if one_db_configured? + if one_database_configured? expect(Rake::Task['gitlab:db:drop_tables']).to receive(:invoke) else expect(Rake::Task['gitlab:db:drop_tables:main']).to receive(:invoke) @@ -219,7 +224,7 @@ RSpec.describe Backup::Database, feature_category: :backup_restore do let(:cmd) { %W[#{Gem.ruby} -e $stderr.write("#{noise}#{visible_error}")] } it 'filters out noise from errors and has a post restore warning' do - if one_db_configured? + if one_database_configured? expect(Rake::Task['gitlab:db:drop_tables']).to receive(:invoke) else expect(Rake::Task['gitlab:db:drop_tables:main']).to receive(:invoke) @@ -246,7 +251,7 @@ RSpec.describe Backup::Database, feature_category: :backup_restore do end it 'overrides default config values' do - if one_db_configured? + if one_database_configured? expect(Rake::Task['gitlab:db:drop_tables']).to receive(:invoke) else expect(Rake::Task['gitlab:db:drop_tables:main']).to receive(:invoke) @@ -270,7 +275,7 @@ RSpec.describe Backup::Database, feature_category: :backup_restore do end it 'raises an error about missing source file' do - if one_db_configured? + if one_database_configured? expect(Rake::Task['gitlab:db:drop_tables']).not_to receive(:invoke) else expect(Rake::Task['gitlab:db:drop_tables:main']).not_to receive(:invoke) diff --git a/spec/lib/backup/gitaly_backup_spec.rb b/spec/lib/backup/gitaly_backup_spec.rb index ad0e5553fa1..172fc28dd3e 100644 --- a/spec/lib/backup/gitaly_backup_spec.rb +++ b/spec/lib/backup/gitaly_backup_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -RSpec.describe Backup::GitalyBackup do +RSpec.describe Backup::GitalyBackup, feature_category: :backup_restore do let(:max_parallelism) { nil } let(:storage_parallelism) { nil } let(:destination) { File.join(Gitlab.config.backup.path, 'repositories') } @@ -181,6 +181,15 @@ RSpec.describe Backup::GitalyBackup do expect(collect_commit_shas.call(project_snippet.repository)).to match_array(['6e44ba56a4748be361a841e759c20e421a1651a1']) end + it 'clears specified storages when remove_all_repositories is set' do + expect(Open3).to receive(:popen2).with(expected_env, anything, 'restore', '-path', anything, '-layout', 'pointer', '-remove-all-repositories', 'default').and_call_original + + copy_bundle_to_backup_path('project_repo.bundle', project.disk_path + '.bundle') + subject.start(:restore, destination, backup_id: backup_id, remove_all_repositories: %w[default]) + subject.enqueue(project, Gitlab::GlRepository::PROJECT) + subject.finish! + end + context 'parallel option set' do let(:max_parallelism) { 3 } diff --git a/spec/lib/backup/manager_spec.rb b/spec/lib/backup/manager_spec.rb index 02889c1535d..1733d21c23f 100644 --- a/spec/lib/backup/manager_spec.rb +++ b/spec/lib/backup/manager_spec.rb @@ -77,7 +77,9 @@ RSpec.describe Backup::Manager, feature_category: :backup_restore do end before do - allow(YAML).to receive(:load_file).with(File.join(Gitlab.config.backup.path, 'backup_information.yml')) + allow(YAML).to receive(:safe_load_file).with( + File.join(Gitlab.config.backup.path, 'backup_information.yml'), + permitted_classes: described_class::YAML_PERMITTED_CLASSES) .and_return(backup_information) end @@ -603,14 +605,16 @@ RSpec.describe Backup::Manager, feature_category: :backup_restore do end expect(Kernel).not_to have_received(:system).with(*pack_tar_cmdline) - expect(YAML.load_file(File.join(Gitlab.config.backup.path, 'backup_information.yml'))).to include( - backup_created_at: backup_time.localtime, - db_version: be_a(String), - gitlab_version: Gitlab::VERSION, - installation_type: Gitlab::INSTALLATION_TYPE, - skipped: 'tar', - tar_version: be_a(String) - ) + expect(YAML.safe_load_file( + File.join(Gitlab.config.backup.path, 'backup_information.yml'), + permitted_classes: described_class::YAML_PERMITTED_CLASSES)).to include( + backup_created_at: backup_time.localtime, + db_version: be_a(String), + gitlab_version: Gitlab::VERSION, + installation_type: Gitlab::INSTALLATION_TYPE, + skipped: 'tar', + tar_version: be_a(String) + ) expect(FileUtils).to have_received(:rm_rf).with(File.join(Gitlab.config.backup.path, 'tmp')) end end @@ -629,8 +633,10 @@ RSpec.describe Backup::Manager, feature_category: :backup_restore do end before do - allow(YAML).to receive(:load_file).and_call_original - allow(YAML).to receive(:load_file).with(File.join(Gitlab.config.backup.path, 'backup_information.yml')) + allow(YAML).to receive(:safe_load_file).and_call_original + allow(YAML).to receive(:safe_load_file).with( + File.join(Gitlab.config.backup.path, 'backup_information.yml'), + permitted_classes: described_class::YAML_PERMITTED_CLASSES) .and_return(backup_information) end @@ -658,8 +664,8 @@ RSpec.describe Backup::Manager, feature_category: :backup_restore do it 'prints the list of available backups' do expect { subject.create }.to raise_error SystemExit # rubocop:disable Rails/SaveBang - expect(progress).to have_received(:puts) - .with(a_string_matching('1451606400_2016_01_01_1.2.3\n 1451520000_2015_12_31')) + expect(progress).to have_received(:puts).with(a_string_matching('1451606400_2016_01_01_1.2.3')) + expect(progress).to have_received(:puts).with(a_string_matching('1451520000_2015_12_31')) end it 'fails the operation and prints an error' do @@ -892,12 +898,13 @@ RSpec.describe Backup::Manager, feature_category: :backup_restore do .with(a_string_matching('Non tarred backup found ')) expect(progress).to have_received(:puts) .with(a_string_matching("Backup #{backup_id} is done")) - expect(YAML.load_file(File.join(Gitlab.config.backup.path, 'backup_information.yml'))).to include( - backup_created_at: backup_time, - full_backup_id: full_backup_id, - gitlab_version: Gitlab::VERSION, - skipped: 'something,tar' - ) + expect(YAML.safe_load_file(File.join(Gitlab.config.backup.path, 'backup_information.yml'), + permitted_classes: described_class::YAML_PERMITTED_CLASSES)).to include( + backup_created_at: backup_time, + full_backup_id: full_backup_id, + gitlab_version: Gitlab::VERSION, + skipped: 'something,tar' + ) end context 'on version mismatch' do @@ -943,7 +950,8 @@ RSpec.describe Backup::Manager, feature_category: :backup_restore do allow(Gitlab::BackupLogger).to receive(:info) allow(task1).to receive(:restore).with(File.join(Gitlab.config.backup.path, 'task1.tar.gz')) allow(task2).to receive(:restore).with(File.join(Gitlab.config.backup.path, 'task2.tar.gz')) - allow(YAML).to receive(:load_file).with(File.join(Gitlab.config.backup.path, 'backup_information.yml')) + allow(YAML).to receive(:safe_load_file).with(File.join(Gitlab.config.backup.path, 'backup_information.yml'), + permitted_classes: described_class::YAML_PERMITTED_CLASSES) .and_return(backup_information) allow(Rake::Task['gitlab:shell:setup']).to receive(:invoke) allow(Rake::Task['cache:clear']).to receive(:invoke) @@ -973,8 +981,8 @@ RSpec.describe Backup::Manager, feature_category: :backup_restore do it 'prints the list of available backups' do expect { subject.restore }.to raise_error SystemExit - expect(progress).to have_received(:puts) - .with(a_string_matching('1451606400_2016_01_01_1.2.3\n 1451520000_2015_12_31')) + expect(progress).to have_received(:puts).with(a_string_matching('1451606400_2016_01_01_1.2.3')) + expect(progress).to have_received(:puts).with(a_string_matching('1451520000_2015_12_31')) end it 'fails the operation and prints an error' do diff --git a/spec/lib/backup/repositories_spec.rb b/spec/lib/backup/repositories_spec.rb index 8bcf1e46c33..c75f6c2ac89 100644 --- a/spec/lib/backup/repositories_spec.rb +++ b/spec/lib/backup/repositories_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -RSpec.describe Backup::Repositories do +RSpec.describe Backup::Repositories, feature_category: :backup_restore do let(:progress) { spy(:stdout) } let(:strategy) { spy(:strategy) } let(:storages) { [] } @@ -165,7 +165,7 @@ RSpec.describe Backup::Repositories do it 'calls enqueue for each repository type', :aggregate_failures do subject.restore(destination) - expect(strategy).to have_received(:start).with(:restore, destination) + expect(strategy).to have_received(:start).with(:restore, destination, remove_all_repositories: %w[default]) expect(strategy).to have_received(:enqueue).with(project, Gitlab::GlRepository::PROJECT) expect(strategy).to have_received(:enqueue).with(project, Gitlab::GlRepository::WIKI) expect(strategy).to have_received(:enqueue).with(project, Gitlab::GlRepository::DESIGN) @@ -246,7 +246,7 @@ RSpec.describe Backup::Repositories do subject.restore(destination) - expect(strategy).to have_received(:start).with(:restore, destination) + expect(strategy).to have_received(:start).with(:restore, destination, remove_all_repositories: %w[default]) expect(strategy).not_to have_received(:enqueue).with(excluded_project, Gitlab::GlRepository::PROJECT) expect(strategy).not_to have_received(:enqueue).with(excluded_project_snippet, Gitlab::GlRepository::SNIPPET) expect(strategy).not_to have_received(:enqueue).with(excluded_personal_snippet, Gitlab::GlRepository::SNIPPET) @@ -268,7 +268,7 @@ RSpec.describe Backup::Repositories do subject.restore(destination) - expect(strategy).to have_received(:start).with(:restore, destination) + expect(strategy).to have_received(:start).with(:restore, destination, remove_all_repositories: nil) expect(strategy).not_to have_received(:enqueue).with(excluded_project, Gitlab::GlRepository::PROJECT) expect(strategy).not_to have_received(:enqueue).with(excluded_project_snippet, Gitlab::GlRepository::SNIPPET) expect(strategy).not_to have_received(:enqueue).with(excluded_personal_snippet, Gitlab::GlRepository::SNIPPET) @@ -289,7 +289,7 @@ RSpec.describe Backup::Repositories do subject.restore(destination) - expect(strategy).to have_received(:start).with(:restore, destination) + expect(strategy).to have_received(:start).with(:restore, destination, remove_all_repositories: nil) expect(strategy).not_to have_received(:enqueue).with(excluded_project, Gitlab::GlRepository::PROJECT) expect(strategy).not_to have_received(:enqueue).with(excluded_project_snippet, Gitlab::GlRepository::SNIPPET) expect(strategy).not_to have_received(:enqueue).with(excluded_personal_snippet, Gitlab::GlRepository::SNIPPET) |