2019-10-15 09:06:09 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-09-06 05:16:26 +00:00
|
|
|
require 'rake_helper'
|
|
|
|
|
2019-03-11 17:15:44 +00:00
|
|
|
describe 'rake gitlab:storage:*', :sidekiq do
|
2017-09-06 05:16:26 +00:00
|
|
|
before do
|
|
|
|
Rake.application.rake_require 'tasks/gitlab/storage'
|
|
|
|
|
|
|
|
stub_warn_user_is_not_gitlab
|
|
|
|
end
|
|
|
|
|
2018-05-29 00:53:21 +00:00
|
|
|
shared_examples "rake listing entities" do |entity_name, storage_type|
|
|
|
|
context 'limiting to 2' do
|
|
|
|
before do
|
|
|
|
stub_env('LIMIT' => 2)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "lists 2 out of 3 #{storage_type.downcase} #{entity_name}" do
|
|
|
|
create_collection
|
|
|
|
|
|
|
|
expect { run_rake_task(task) }.to output(/Found 3 #{entity_name} using #{storage_type} Storage.*Displaying first 2 #{entity_name}/m).to_stdout
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "without any #{storage_type.downcase} #{entity_name.singularize}" do
|
|
|
|
it 'displays message for empty results' do
|
|
|
|
expect { run_rake_task(task) }.to output(/Found 0 #{entity_name} using #{storage_type} Storage/).to_stdout
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples "rake entities summary" do |entity_name, storage_type|
|
|
|
|
context "with existing 3 #{storage_type.downcase} #{entity_name}" do
|
|
|
|
it "reports 3 #{storage_type.downcase} #{entity_name}" do
|
|
|
|
create_collection
|
|
|
|
|
|
|
|
expect { run_rake_task(task) }.to output(/Found 3 #{entity_name} using #{storage_type} Storage/).to_stdout
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "without any #{storage_type.downcase} #{entity_name.singularize}" do
|
|
|
|
it 'displays message for empty results' do
|
|
|
|
expect { run_rake_task(task) }.to output(/Found 0 #{entity_name} using #{storage_type} Storage/).to_stdout
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-11 17:15:44 +00:00
|
|
|
shared_examples "make sure database is writable" do
|
2018-12-19 21:10:00 +00:00
|
|
|
context 'read-only database' do
|
|
|
|
it 'does nothing' do
|
|
|
|
expect(Gitlab::Database).to receive(:read_only?).and_return(true)
|
|
|
|
|
|
|
|
expect(Project).not_to receive(:with_unmigrated_storage)
|
|
|
|
|
2019-06-24 18:31:38 +00:00
|
|
|
expect { run_rake_task(task) }.to abort_execution.with_message(/This task requires database write access. Exiting./)
|
2018-12-19 21:10:00 +00:00
|
|
|
end
|
|
|
|
end
|
2019-03-11 17:15:44 +00:00
|
|
|
end
|
2018-12-19 21:10:00 +00:00
|
|
|
|
2019-03-11 17:15:44 +00:00
|
|
|
shared_examples "handles custom BATCH env var" do |worker_klass|
|
|
|
|
context 'in batches of 1' do
|
|
|
|
before do
|
|
|
|
stub_env('BATCH' => 1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "enqueues one #{worker_klass} per project" do
|
|
|
|
projects.each do |project|
|
|
|
|
expect(worker_klass).to receive(:perform_async).with(project.id, project.id)
|
|
|
|
end
|
2017-09-06 05:16:26 +00:00
|
|
|
|
2018-06-07 15:40:44 +00:00
|
|
|
run_rake_task(task)
|
2017-09-06 05:16:26 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-11 17:15:44 +00:00
|
|
|
context 'in batches of 2' do
|
|
|
|
before do
|
|
|
|
stub_env('BATCH' => 2)
|
|
|
|
end
|
2017-09-06 05:16:26 +00:00
|
|
|
|
2019-03-11 17:15:44 +00:00
|
|
|
it "enqueues one #{worker_klass} per 2 projects" do
|
|
|
|
projects.map(&:id).sort.each_slice(2) do |first, last|
|
|
|
|
last ||= first
|
|
|
|
expect(worker_klass).to receive(:perform_async).with(first, last)
|
2017-09-06 05:16:26 +00:00
|
|
|
end
|
|
|
|
|
2019-03-11 17:15:44 +00:00
|
|
|
run_rake_task(task)
|
2017-09-06 05:16:26 +00:00
|
|
|
end
|
2019-03-11 17:15:44 +00:00
|
|
|
end
|
|
|
|
end
|
2017-09-06 05:16:26 +00:00
|
|
|
|
2019-03-11 17:15:44 +00:00
|
|
|
describe 'gitlab:storage:migrate_to_hashed' do
|
|
|
|
let(:task) { 'gitlab:storage:migrate_to_hashed' }
|
2017-09-06 05:16:26 +00:00
|
|
|
|
2019-03-13 14:54:30 +00:00
|
|
|
context 'with rollback already scheduled', :redis do
|
2019-03-11 17:15:44 +00:00
|
|
|
it 'does nothing' do
|
2019-03-13 14:54:30 +00:00
|
|
|
Sidekiq::Testing.disable! do
|
2019-03-11 17:15:44 +00:00
|
|
|
::HashedStorage::RollbackerWorker.perform_async(1, 5)
|
|
|
|
|
|
|
|
expect(Project).not_to receive(:with_unmigrated_storage)
|
2017-09-06 05:16:26 +00:00
|
|
|
|
2019-06-24 18:31:38 +00:00
|
|
|
expect { run_rake_task(task) }.to abort_execution.with_message(/There is already a rollback operation in progress/)
|
2017-09-06 05:16:26 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-06-07 15:40:44 +00:00
|
|
|
|
2019-03-11 17:15:44 +00:00
|
|
|
context 'with 0 legacy projects' do
|
|
|
|
it 'does nothing' do
|
|
|
|
expect(::HashedStorage::MigratorWorker).not_to receive(:perform_async)
|
|
|
|
|
2019-06-24 18:31:38 +00:00
|
|
|
expect { run_rake_task(task) }.to abort_execution.with_message('There are no projects requiring storage migration. Nothing to do!')
|
2019-03-11 17:15:44 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with 3 legacy projects' do
|
|
|
|
let(:projects) { create_list(:project, 3, :legacy_storage) }
|
|
|
|
|
2019-06-24 18:31:38 +00:00
|
|
|
it 'enqueues migrations and count projects correctly' do
|
|
|
|
projects.map(&:id).sort.tap do |ids|
|
|
|
|
stub_env('ID_FROM', ids[0])
|
|
|
|
stub_env('ID_TO', ids[1])
|
|
|
|
end
|
|
|
|
|
|
|
|
expect { run_rake_task(task) }.to output(/Enqueuing migration of 2 projects in batches/).to_stdout
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'handles custom BATCH env var', ::HashedStorage::MigratorWorker
|
2019-03-11 17:15:44 +00:00
|
|
|
end
|
|
|
|
|
2018-06-07 15:40:44 +00:00
|
|
|
context 'with same id in range' do
|
|
|
|
it 'displays message when project cant be found' do
|
|
|
|
stub_env('ID_FROM', 99999)
|
|
|
|
stub_env('ID_TO', 99999)
|
|
|
|
|
2019-06-24 18:31:38 +00:00
|
|
|
expect { run_rake_task(task) }.to abort_execution.with_message(/There are no projects requiring storage migration with ID=99999/)
|
2018-06-07 15:40:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'displays a message when project exists but its already migrated' do
|
|
|
|
project = create(:project)
|
|
|
|
stub_env('ID_FROM', project.id)
|
|
|
|
stub_env('ID_TO', project.id)
|
|
|
|
|
2019-06-24 18:31:38 +00:00
|
|
|
expect { run_rake_task(task) }.to abort_execution.with_message(/There are no projects requiring storage migration with ID=#{project.id}/)
|
2018-06-07 15:40:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'enqueues migration when project can be found' do
|
|
|
|
project = create(:project, :legacy_storage)
|
|
|
|
stub_env('ID_FROM', project.id)
|
|
|
|
stub_env('ID_TO', project.id)
|
|
|
|
|
|
|
|
expect { run_rake_task(task) }.to output(/Enqueueing storage migration .* \(ID=#{project.id}\)/).to_stdout
|
|
|
|
end
|
|
|
|
end
|
2017-09-06 05:16:26 +00:00
|
|
|
end
|
2018-05-29 00:53:21 +00:00
|
|
|
|
2019-03-11 17:15:44 +00:00
|
|
|
describe 'gitlab:storage:rollback_to_legacy' do
|
|
|
|
let(:task) { 'gitlab:storage:rollback_to_legacy' }
|
|
|
|
|
|
|
|
it_behaves_like 'make sure database is writable'
|
|
|
|
|
2019-03-13 14:54:30 +00:00
|
|
|
context 'with migration already scheduled', :redis do
|
2019-03-11 17:15:44 +00:00
|
|
|
it 'does nothing' do
|
2019-03-13 14:54:30 +00:00
|
|
|
Sidekiq::Testing.disable! do
|
2019-03-11 17:15:44 +00:00
|
|
|
::HashedStorage::MigratorWorker.perform_async(1, 5)
|
|
|
|
|
|
|
|
expect(Project).not_to receive(:with_unmigrated_storage)
|
|
|
|
|
2019-06-24 18:31:38 +00:00
|
|
|
expect { run_rake_task(task) }.to abort_execution.with_message(/There is already a migration operation in progress/)
|
2019-03-11 17:15:44 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with 0 hashed projects' do
|
|
|
|
it 'does nothing' do
|
|
|
|
expect(::HashedStorage::RollbackerWorker).not_to receive(:perform_async)
|
|
|
|
|
2019-06-24 18:31:38 +00:00
|
|
|
expect { run_rake_task(task) }.to abort_execution.with_message('There are no projects that can have storage rolledback. Nothing to do!')
|
2019-03-11 17:15:44 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with 3 hashed projects' do
|
|
|
|
let(:projects) { create_list(:project, 3) }
|
|
|
|
|
2019-06-24 18:31:38 +00:00
|
|
|
it 'enqueues migrations and count projects correctly' do
|
|
|
|
projects.map(&:id).sort.tap do |ids|
|
|
|
|
stub_env('ID_FROM', ids[0])
|
|
|
|
stub_env('ID_TO', ids[1])
|
|
|
|
end
|
|
|
|
|
|
|
|
expect { run_rake_task(task) }.to output(/Enqueuing rollback of 2 projects in batches/).to_stdout
|
|
|
|
end
|
|
|
|
|
2019-03-11 17:15:44 +00:00
|
|
|
it_behaves_like "handles custom BATCH env var", ::HashedStorage::RollbackerWorker
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-29 00:53:21 +00:00
|
|
|
describe 'gitlab:storage:legacy_projects' do
|
|
|
|
it_behaves_like 'rake entities summary', 'projects', 'Legacy' do
|
|
|
|
let(:task) { 'gitlab:storage:legacy_projects' }
|
2018-06-07 15:40:44 +00:00
|
|
|
let(:create_collection) { create_list(:project, 3, :legacy_storage) }
|
2018-05-29 00:53:21 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'gitlab:storage:list_legacy_projects' do
|
|
|
|
it_behaves_like 'rake listing entities', 'projects', 'Legacy' do
|
|
|
|
let(:task) { 'gitlab:storage:list_legacy_projects' }
|
2018-06-07 15:40:44 +00:00
|
|
|
let(:create_collection) { create_list(:project, 3, :legacy_storage) }
|
2018-05-29 00:53:21 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'gitlab:storage:hashed_projects' do
|
|
|
|
it_behaves_like 'rake entities summary', 'projects', 'Hashed' do
|
|
|
|
let(:task) { 'gitlab:storage:hashed_projects' }
|
|
|
|
let(:create_collection) { create_list(:project, 3, storage_version: 1) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'gitlab:storage:list_hashed_projects' do
|
|
|
|
it_behaves_like 'rake listing entities', 'projects', 'Hashed' do
|
|
|
|
let(:task) { 'gitlab:storage:list_hashed_projects' }
|
|
|
|
let(:create_collection) { create_list(:project, 3, storage_version: 1) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'gitlab:storage:legacy_attachments' do
|
|
|
|
it_behaves_like 'rake entities summary', 'attachments', 'Legacy' do
|
|
|
|
let(:task) { 'gitlab:storage:legacy_attachments' }
|
|
|
|
let(:project) { create(:project, storage_version: 1) }
|
|
|
|
let(:create_collection) { create_list(:upload, 3, model: project) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'gitlab:storage:list_legacy_attachments' do
|
|
|
|
it_behaves_like 'rake listing entities', 'attachments', 'Legacy' do
|
|
|
|
let(:task) { 'gitlab:storage:list_legacy_attachments' }
|
|
|
|
let(:project) { create(:project, storage_version: 1) }
|
|
|
|
let(:create_collection) { create_list(:upload, 3, model: project) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'gitlab:storage:hashed_attachments' do
|
|
|
|
it_behaves_like 'rake entities summary', 'attachments', 'Hashed' do
|
|
|
|
let(:task) { 'gitlab:storage:hashed_attachments' }
|
2018-06-07 15:40:44 +00:00
|
|
|
let(:project) { create(:project) }
|
2018-05-29 00:53:21 +00:00
|
|
|
let(:create_collection) { create_list(:upload, 3, model: project) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'gitlab:storage:list_hashed_attachments' do
|
|
|
|
it_behaves_like 'rake listing entities', 'attachments', 'Hashed' do
|
|
|
|
let(:task) { 'gitlab:storage:list_hashed_attachments' }
|
2018-06-07 15:40:44 +00:00
|
|
|
let(:project) { create(:project) }
|
2018-05-29 00:53:21 +00:00
|
|
|
let(:create_collection) { create_list(:upload, 3, model: project) }
|
|
|
|
end
|
|
|
|
end
|
2017-09-06 05:16:26 +00:00
|
|
|
end
|