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

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'spec/tasks')
-rw-r--r--spec/tasks/gitlab/backup_rake_spec.rb79
-rw-r--r--spec/tasks/gitlab/db_rake_spec.rb131
-rw-r--r--spec/tasks/gitlab/dependency_proxy/migrate_rake_spec.rb58
-rw-r--r--spec/tasks/gitlab/info_rake_spec.rb39
4 files changed, 227 insertions, 80 deletions
diff --git a/spec/tasks/gitlab/backup_rake_spec.rb b/spec/tasks/gitlab/backup_rake_spec.rb
index c5e73aa3b45..e9aa8cbb991 100644
--- a/spec/tasks/gitlab/backup_rake_spec.rb
+++ b/spec/tasks/gitlab/backup_rake_spec.rb
@@ -4,7 +4,8 @@ require 'rake_helper'
RSpec.describe 'gitlab:app namespace rake task', :delete do
let(:enable_registry) { true }
- let(:backup_types) { %w{db repo uploads builds artifacts pages lfs terraform_state registry packages} }
+ let(:backup_tasks) { %w{db repo uploads builds artifacts pages lfs terraform_state registry packages} }
+ let(:backup_types) { %w{db repositories uploads builds artifacts pages lfs terraform_state registry packages} }
def tars_glob
Dir.glob(File.join(Gitlab.config.backup.path, '*_gitlab_backup.tar'))
@@ -48,7 +49,7 @@ RSpec.describe 'gitlab:app namespace rake task', :delete do
end
def reenable_backup_sub_tasks
- backup_types.each do |subtask|
+ backup_tasks.each do |subtask|
Rake::Task["gitlab:backup:#{subtask}:create"].reenable
end
end
@@ -72,8 +73,11 @@ RSpec.describe 'gitlab:app namespace rake task', :delete do
allow(YAML).to receive(:load_file)
.and_return({ gitlab_version: gitlab_version })
expect(Rake::Task['gitlab:db:drop_tables']).to receive(:invoke)
- backup_types.each do |subtask|
- expect(Rake::Task["gitlab:backup:#{subtask}:restore"]).to receive(:invoke)
+ expect_next_instance_of(::Backup::Manager) do |instance|
+ backup_types.each do |subtask|
+ expect(instance).to receive(:run_restore_task).with(subtask).ordered
+ end
+ expect(instance).not_to receive(:run_restore_task)
end
expect(Rake::Task['gitlab:shell:setup']).to receive(:invoke)
end
@@ -128,16 +132,14 @@ RSpec.describe 'gitlab:app namespace rake task', :delete do
.and_return({ gitlab_version: Gitlab::VERSION })
expect(Rake::Task['gitlab:db:drop_tables']).to receive(:invoke)
- expect(Rake::Task['gitlab:backup:db:restore']).to receive(:invoke)
- expect(Rake::Task['gitlab:backup:repo:restore']).to receive(:invoke)
- expect(Rake::Task['gitlab:backup:builds:restore']).to receive(:invoke)
- expect(Rake::Task['gitlab:backup:uploads:restore']).to receive(:invoke)
- expect(Rake::Task['gitlab:backup:artifacts:restore']).to receive(:invoke)
- expect(Rake::Task['gitlab:backup:pages:restore']).to receive(:invoke)
- expect(Rake::Task['gitlab:backup:lfs:restore']).to receive(:invoke)
- expect(Rake::Task['gitlab:backup:terraform_state:restore']).to receive(:invoke)
- expect(Rake::Task['gitlab:backup:registry:restore']).to receive(:invoke)
- expect(Rake::Task['gitlab:backup:packages:restore']).to receive(:invoke)
+
+ expect_next_instance_of(::Backup::Manager) do |instance|
+ backup_types.each do |subtask|
+ expect(instance).to receive(:run_restore_task).with(subtask).ordered
+ end
+ expect(instance).not_to receive(:run_restore_task)
+ end
+
expect(Rake::Task['gitlab:shell:setup']).to receive(:invoke)
end
@@ -198,7 +200,7 @@ RSpec.describe 'gitlab:app namespace rake task', :delete do
context 'specific backup tasks' do
it 'prints a progress message to stdout' do
- backup_types.each do |task|
+ backup_tasks.each do |task|
expect { run_rake_task("gitlab:backup:#{task}:create") }.to output(/Dumping /).to_stdout_from_any_process
end
end
@@ -206,7 +208,7 @@ RSpec.describe 'gitlab:app namespace rake task', :delete do
it 'logs the progress to log file' do
expect(Gitlab::BackupLogger).to receive(:info).with(message: "Dumping database ... ")
expect(Gitlab::BackupLogger).to receive(:info).with(message: "[SKIPPED]")
- expect(Gitlab::BackupLogger).to receive(:info).with(message: "Dumping repositories ...")
+ expect(Gitlab::BackupLogger).to receive(:info).with(message: "Dumping repositories ... ")
expect(Gitlab::BackupLogger).to receive(:info).with(message: "Dumping uploads ... ")
expect(Gitlab::BackupLogger).to receive(:info).with(message: "Dumping builds ... ")
expect(Gitlab::BackupLogger).to receive(:info).with(message: "Dumping artifacts ... ")
@@ -217,7 +219,7 @@ RSpec.describe 'gitlab:app namespace rake task', :delete do
expect(Gitlab::BackupLogger).to receive(:info).with(message: "Dumping packages ... ")
expect(Gitlab::BackupLogger).to receive(:info).with(message: "done").exactly(9).times
- backup_types.each do |task|
+ backup_tasks.each do |task|
run_rake_task("gitlab:backup:#{task}:create")
end
end
@@ -344,9 +346,9 @@ RSpec.describe 'gitlab:app namespace rake task', :delete do
shared_examples 'includes repositories in all repository storages' do
specify :aggregate_failures do
project_a = create(:project, :repository)
- project_snippet_a = create(:project_snippet, :repository, project: project_a, author: project_a.owner)
+ project_snippet_a = create(:project_snippet, :repository, project: project_a, author: project_a.first_owner)
project_b = create(:project, :repository, repository_storage: second_storage_name)
- project_snippet_b = create(:project_snippet, :repository, project: project_b, author: project_b.owner)
+ project_snippet_b = create(:project_snippet, :repository, project: project_b, author: project_b.first_owner)
project_snippet_b.snippet_repository.update!(shard: project_b.project_repository.shard)
create(:wiki_page, container: project_a)
create(:design, :with_file, issue: create(:issue, project: project_a))
@@ -414,11 +416,9 @@ RSpec.describe 'gitlab:app namespace rake task', :delete do
end
it 'has defaults' do
- expect_next_instance_of(::Backup::Repositories) do |instance|
- expect(instance).to receive(:dump)
- .with(max_concurrency: 1, max_storage_concurrency: 1)
- .and_call_original
- end
+ expect(::Backup::Repositories).to receive(:new)
+ .with(anything, strategy: anything, max_concurrency: 1, max_storage_concurrency: 1)
+ .and_call_original
expect { run_rake_task('gitlab:backup:create') }.to output.to_stdout_from_any_process
end
@@ -432,11 +432,9 @@ RSpec.describe 'gitlab:app namespace rake task', :delete do
stub_env('GITLAB_BACKUP_MAX_CONCURRENCY', 5)
stub_env('GITLAB_BACKUP_MAX_STORAGE_CONCURRENCY', 2)
- expect_next_instance_of(::Backup::Repositories) do |instance|
- expect(instance).to receive(:dump)
- .with(max_concurrency: 5, max_storage_concurrency: 2)
- .and_call_original
- end
+ expect(::Backup::Repositories).to receive(:new)
+ .with(anything, strategy: anything, max_concurrency: 5, max_storage_concurrency: 2)
+ .and_call_original
expect(::Backup::GitalyBackup).to receive(:new).with(anything, max_parallelism: 5, storage_parallelism: 2).and_call_original
expect { run_rake_task('gitlab:backup:create') }.to output.to_stdout_from_any_process
@@ -489,16 +487,12 @@ RSpec.describe 'gitlab:app namespace rake task', :delete do
.to receive(:invoke).and_return(true)
expect(Rake::Task['gitlab:db:drop_tables']).to receive :invoke
- expect(Rake::Task['gitlab:backup:db:restore']).to receive :invoke
- expect(Rake::Task['gitlab:backup:repo:restore']).not_to receive :invoke
- expect(Rake::Task['gitlab:backup:uploads:restore']).not_to receive :invoke
- expect(Rake::Task['gitlab:backup:builds:restore']).to receive :invoke
- expect(Rake::Task['gitlab:backup:artifacts:restore']).to receive :invoke
- expect(Rake::Task['gitlab:backup:pages:restore']).to receive :invoke
- expect(Rake::Task['gitlab:backup:lfs:restore']).to receive :invoke
- expect(Rake::Task['gitlab:backup:terraform_state:restore']).to receive :invoke
- expect(Rake::Task['gitlab:backup:registry:restore']).to receive :invoke
- expect(Rake::Task['gitlab:backup:packages:restore']).to receive :invoke
+ expect_next_instance_of(::Backup::Manager) do |instance|
+ (backup_types - %w{repositories uploads}).each do |subtask|
+ expect(instance).to receive(:run_restore_task).with(subtask).ordered
+ end
+ expect(instance).not_to receive(:run_restore_task)
+ end
expect(Rake::Task['gitlab:shell:setup']).to receive :invoke
expect { run_rake_task('gitlab:backup:restore') }.to output.to_stdout_from_any_process
end
@@ -538,8 +532,11 @@ RSpec.describe 'gitlab:app namespace rake task', :delete do
.to receive(:invoke).and_return(true)
expect(Rake::Task['gitlab:db:drop_tables']).to receive :invoke
- backup_types.each do |subtask|
- expect(Rake::Task["gitlab:backup:#{subtask}:restore"]).to receive :invoke
+ expect_next_instance_of(::Backup::Manager) do |instance|
+ backup_types.each do |subtask|
+ expect(instance).to receive(:run_restore_task).with(subtask).ordered
+ end
+ expect(instance).not_to receive(:run_restore_task)
end
expect(Rake::Task['gitlab:shell:setup']).to receive :invoke
expect { run_rake_task("gitlab:backup:restore") }.to output.to_stdout_from_any_process
diff --git a/spec/tasks/gitlab/db_rake_spec.rb b/spec/tasks/gitlab/db_rake_spec.rb
index 92c896b1ab0..c3fd8135ae0 100644
--- a/spec/tasks/gitlab/db_rake_spec.rb
+++ b/spec/tasks/gitlab/db_rake_spec.rb
@@ -20,6 +20,99 @@ RSpec.describe 'gitlab:db namespace rake task', :silence_stdout do
allow(Rake::Task['db:seed_fu']).to receive(:invoke).and_return(true)
end
+ describe 'mark_migration_complete' do
+ context 'with a single database' do
+ let(:main_model) { ActiveRecord::Base }
+
+ before do
+ skip_if_multiple_databases_are_setup
+ end
+
+ it 'marks the migration complete on the given database' do
+ expect(main_model.connection).to receive(:quote).and_call_original
+ expect(main_model.connection).to receive(:execute)
+ .with("INSERT INTO schema_migrations (version) VALUES ('123')")
+
+ run_rake_task('gitlab:db:mark_migration_complete', '[123]')
+ end
+ end
+
+ context 'with multiple databases' do
+ let(:main_model) { double(:model, connection: double(:connection)) }
+ let(:ci_model) { double(:model, connection: double(:connection)) }
+ let(:base_models) { { 'main' => main_model, 'ci' => ci_model } }
+
+ before do
+ skip_if_multiple_databases_not_setup
+
+ allow(Gitlab::Database).to receive(:database_base_models).and_return(base_models)
+ end
+
+ it 'marks the migration complete on each database' do
+ expect(main_model.connection).to receive(:quote).with('123').and_return("'123'")
+ expect(main_model.connection).to receive(:execute)
+ .with("INSERT INTO schema_migrations (version) VALUES ('123')")
+
+ expect(ci_model.connection).to receive(:quote).with('123').and_return("'123'")
+ expect(ci_model.connection).to receive(:execute)
+ .with("INSERT INTO schema_migrations (version) VALUES ('123')")
+
+ run_rake_task('gitlab:db:mark_migration_complete', '[123]')
+ end
+
+ context 'when the single database task is used' do
+ it 'marks the migration complete for the given database' do
+ expect(main_model.connection).to receive(:quote).with('123').and_return("'123'")
+ expect(main_model.connection).to receive(:execute)
+ .with("INSERT INTO schema_migrations (version) VALUES ('123')")
+
+ expect(ci_model.connection).not_to receive(:quote)
+ expect(ci_model.connection).not_to receive(:execute)
+
+ run_rake_task('gitlab:db:mark_migration_complete:main', '[123]')
+ end
+ end
+ end
+
+ context 'when the migration is already marked complete' do
+ let(:main_model) { double(:model, connection: double(:connection)) }
+ let(:base_models) { { 'main' => main_model } }
+
+ before do
+ allow(Gitlab::Database).to receive(:database_base_models).and_return(base_models)
+ end
+
+ it 'prints a warning message' do
+ allow(main_model.connection).to receive(:quote).with('123').and_return("'123'")
+
+ expect(main_model.connection).to receive(:execute)
+ .with("INSERT INTO schema_migrations (version) VALUES ('123')")
+ .and_raise(ActiveRecord::RecordNotUnique)
+
+ expect { run_rake_task('gitlab:db:mark_migration_complete', '[123]') }
+ .to output(/Migration version '123' is already marked complete on database main/).to_stdout
+ end
+ end
+
+ context 'when an invalid version is given' do
+ let(:main_model) { double(:model, connection: double(:connection)) }
+ let(:base_models) { { 'main' => main_model } }
+
+ before do
+ allow(Gitlab::Database).to receive(:database_base_models).and_return(base_models)
+ end
+
+ it 'prints an error and exits' do
+ expect(main_model).not_to receive(:quote)
+ expect(main_model.connection).not_to receive(:execute)
+
+ expect { run_rake_task('gitlab:db:mark_migration_complete', '[abc]') }
+ .to output(/Must give a version argument that is a non-zero integer/).to_stdout
+ .and raise_error(SystemExit) { |error| expect(error.status).to eq(1) }
+ end
+ end
+ end
+
describe 'configure' do
it 'invokes db:migrate when schema has already been loaded' do
allow(ActiveRecord::Base.connection).to receive(:tables).and_return(%w[table1 table2])
@@ -353,6 +446,44 @@ RSpec.describe 'gitlab:db namespace rake task', :silence_stdout do
end
end
+ describe 'gitlab:db:reset_as_non_superuser' do
+ let(:connection_pool) { instance_double(ActiveRecord::ConnectionAdapters::ConnectionPool ) }
+ let(:connection) { instance_double(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter) }
+ let(:configurations) { double(ActiveRecord::DatabaseConfigurations) }
+ let(:configuration) { instance_double(ActiveRecord::DatabaseConfigurations::HashConfig) }
+ let(:config_hash) { { username: 'foo' } }
+
+ it 'migrate as nonsuperuser check with default username' do
+ allow(Rake::Task['db:drop']).to receive(:invoke)
+ allow(Rake::Task['db:create']).to receive(:invoke)
+ allow(ActiveRecord::Base).to receive(:configurations).and_return(configurations)
+ allow(configurations).to receive(:configs_for).and_return([configuration])
+ allow(configuration).to receive(:configuration_hash).and_return(config_hash)
+ allow(ActiveRecord::Base).to receive(:establish_connection).and_return(connection_pool)
+
+ expect(config_hash).to receive(:merge).with({ username: 'gitlab' })
+ expect(Gitlab::Database).to receive(:check_for_non_superuser)
+ expect(Rake::Task['db:migrate']).to receive(:invoke)
+
+ run_rake_task('gitlab:db:reset_as_non_superuser')
+ end
+
+ it 'migrate as nonsuperuser check with specified username' do
+ allow(Rake::Task['db:drop']).to receive(:invoke)
+ allow(Rake::Task['db:create']).to receive(:invoke)
+ allow(ActiveRecord::Base).to receive(:configurations).and_return(configurations)
+ allow(configurations).to receive(:configs_for).and_return([configuration])
+ allow(configuration).to receive(:configuration_hash).and_return(config_hash)
+ allow(ActiveRecord::Base).to receive(:establish_connection).and_return(connection_pool)
+
+ expect(config_hash).to receive(:merge).with({ username: 'foo' })
+ expect(Gitlab::Database).to receive(:check_for_non_superuser)
+ expect(Rake::Task['db:migrate']).to receive(:invoke)
+
+ run_rake_task('gitlab:db:reset_as_non_superuser', '[foo]')
+ end
+ end
+
def run_rake_task(task_name, arguments = '')
Rake::Task[task_name].reenable
Rake.application.invoke_task("#{task_name}#{arguments}")
diff --git a/spec/tasks/gitlab/dependency_proxy/migrate_rake_spec.rb b/spec/tasks/gitlab/dependency_proxy/migrate_rake_spec.rb
new file mode 100644
index 00000000000..edd56f1667f
--- /dev/null
+++ b/spec/tasks/gitlab/dependency_proxy/migrate_rake_spec.rb
@@ -0,0 +1,58 @@
+# frozen_string_literal: true
+
+require 'rake_helper'
+
+RSpec.describe 'gitlab:dependency_proxy namespace rake task', :silence_stdout do
+ before :all do
+ Rake.application.rake_require 'tasks/gitlab/dependency_proxy/migrate'
+ end
+
+ describe 'migrate' do
+ let(:local) { ObjectStorage::Store::LOCAL }
+ let(:remote) { ObjectStorage::Store::REMOTE }
+ let!(:blob) { create(:dependency_proxy_blob) }
+ let!(:manifest) { create(:dependency_proxy_manifest) }
+
+ def dependency_proxy_migrate
+ run_rake_task('gitlab:dependency_proxy:migrate')
+ end
+
+ context 'object storage disabled' do
+ before do
+ stub_dependency_proxy_object_storage(enabled: false)
+ end
+
+ it "doesn't migrate files" do
+ expect { dependency_proxy_migrate }.to raise_error('Object store is disabled for dependency proxy feature')
+ end
+ end
+
+ context 'object storage enabled' do
+ before do
+ stub_dependency_proxy_object_storage
+ end
+
+ it 'migrates local file to object storage' do
+ expect { dependency_proxy_migrate }.to change { blob.reload.file_store }.from(local).to(remote)
+ .and change { manifest.reload.file_store }.from(local).to(remote)
+ end
+ end
+
+ context 'an error is raised while migrating' do
+ let(:blob_error) { 'Failed to transfer dependency proxy blob file' }
+ let(:manifest_error) { 'Failed to transfer dependency proxy manifest file' }
+ let!(:blob_non_existent) { create(:dependency_proxy_blob) }
+ let!(:manifest_non_existent) { create(:dependency_proxy_manifest) }
+
+ before do
+ stub_dependency_proxy_object_storage
+ blob_non_existent.file.file.delete
+ manifest_non_existent.file.file.delete
+ end
+
+ it 'fails to migrate a local file that does not exist' do
+ expect { dependency_proxy_migrate }.to output(include(blob_error, manifest_error)).to_stdout
+ end
+ end
+ end
+end
diff --git a/spec/tasks/gitlab/info_rake_spec.rb b/spec/tasks/gitlab/info_rake_spec.rb
deleted file mode 100644
index 19ed43723e2..00000000000
--- a/spec/tasks/gitlab/info_rake_spec.rb
+++ /dev/null
@@ -1,39 +0,0 @@
-# frozen_string_literal: true
-
-require 'rake_helper'
-
-RSpec.describe 'gitlab:env:info', :silence_stdout do
- before do
- Rake.application.rake_require 'tasks/gitlab/info'
-
- stub_warn_user_is_not_gitlab
- allow(Gitlab::Popen).to receive(:popen)
- end
-
- describe 'git version' do
- before do
- allow(Gitlab::Popen).to receive(:popen).with([Gitlab.config.git.bin_path, '--version'])
- .and_return(git_version)
- end
-
- context 'when git installed' do
- let(:git_version) { 'git version 2.10.0' }
-
- it 'prints git version' do
- run_rake_task('gitlab:env:info')
-
- expect($stdout.string).to match(/Git Version:(.*)2.10.0/)
- end
- end
-
- context 'when git not installed' do
- let(:git_version) { '' }
-
- it 'prints unknown' do
- run_rake_task('gitlab:env:info')
-
- expect($stdout.string).to match(/Git Version:(.*)unknown/)
- end
- end
- end
-end