2019-03-30 07:15:48 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-09-06 05:16:26 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 15:08:50 +00:00
|
|
|
RSpec.describe HashedStorage::ProjectMigrateWorker, :clean_gitlab_redis_shared_state do
|
2018-06-28 18:51:36 +00:00
|
|
|
include ExclusiveLeaseHelpers
|
|
|
|
|
2019-12-06 03:08:02 +00:00
|
|
|
let(:migration_service) { ::Projects::HashedStorage::MigrationService }
|
|
|
|
let(:lease_timeout) { described_class::LEASE_TIMEOUT }
|
2018-06-28 18:51:36 +00:00
|
|
|
|
2019-12-06 03:08:02 +00:00
|
|
|
describe '#perform' do
|
2018-06-28 18:51:36 +00:00
|
|
|
it 'skips when project no longer exists' do
|
2019-12-06 03:08:02 +00:00
|
|
|
stub_exclusive_lease(lease_key(-1), 'uuid', timeout: lease_timeout)
|
|
|
|
|
2019-01-17 01:53:50 +00:00
|
|
|
expect(migration_service).not_to receive(:new)
|
2018-06-28 18:51:36 +00:00
|
|
|
|
|
|
|
subject.perform(-1)
|
|
|
|
end
|
2017-09-06 05:16:26 +00:00
|
|
|
|
2018-06-28 18:51:36 +00:00
|
|
|
it 'skips when project is pending delete' do
|
|
|
|
pending_delete_project = create(:project, :empty_repo, pending_delete: true)
|
2019-12-06 03:08:02 +00:00
|
|
|
stub_exclusive_lease(lease_key(pending_delete_project.id), 'uuid', timeout: lease_timeout)
|
2017-11-22 15:29:03 +00:00
|
|
|
|
2019-01-17 01:53:50 +00:00
|
|
|
expect(migration_service).not_to receive(:new)
|
2017-09-06 05:16:26 +00:00
|
|
|
|
2018-06-28 18:51:36 +00:00
|
|
|
subject.perform(pending_delete_project.id)
|
|
|
|
end
|
2017-11-22 05:35:53 +00:00
|
|
|
|
2019-12-06 03:08:02 +00:00
|
|
|
it 'skips when project is already migrated' do
|
|
|
|
migrated_project = create(:project, :empty_repo)
|
|
|
|
stub_exclusive_lease(lease_key(migrated_project.id), 'uuid', timeout: lease_timeout)
|
|
|
|
|
|
|
|
expect(migration_service).not_to receive(:new)
|
|
|
|
|
|
|
|
subject.perform(migrated_project.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with exclusive lease available' do
|
|
|
|
it 'delegates migration to service class' do
|
|
|
|
project = create(:project, :empty_repo, :legacy_storage)
|
|
|
|
stub_exclusive_lease(lease_key(project.id), 'uuid', timeout: lease_timeout)
|
2017-11-22 05:35:53 +00:00
|
|
|
|
2019-12-06 03:08:02 +00:00
|
|
|
service_spy = spy
|
2017-09-06 05:16:26 +00:00
|
|
|
|
2019-12-06 03:08:02 +00:00
|
|
|
allow(migration_service)
|
|
|
|
.to receive(:new).with(project, project.full_path, logger: subject.logger)
|
|
|
|
.and_return(service_spy)
|
2017-09-06 05:16:26 +00:00
|
|
|
|
2019-12-06 03:08:02 +00:00
|
|
|
subject.perform(project.id)
|
2017-11-22 05:35:53 +00:00
|
|
|
|
2019-12-06 03:08:02 +00:00
|
|
|
expect(service_spy).to have_received(:execute)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'delegates migration to service class with correct path in a partially migrated project' do
|
|
|
|
project = create(:project, :empty_repo, storage_version: 1)
|
|
|
|
stub_exclusive_lease(lease_key(project.id), 'uuid', timeout: lease_timeout)
|
|
|
|
|
|
|
|
service_spy = spy
|
|
|
|
|
|
|
|
allow(migration_service)
|
|
|
|
.to receive(:new).with(project, project.full_path, logger: subject.logger)
|
|
|
|
.and_return(service_spy)
|
|
|
|
|
|
|
|
subject.perform(project.id)
|
|
|
|
|
|
|
|
expect(service_spy).to have_received(:execute)
|
|
|
|
end
|
2017-09-06 05:16:26 +00:00
|
|
|
end
|
|
|
|
|
2019-12-06 03:08:02 +00:00
|
|
|
context 'with exclusive lease taken' do
|
|
|
|
it 'skips when it cant acquire the exclusive lease' do
|
|
|
|
project = create(:project, :empty_repo, :legacy_storage)
|
|
|
|
stub_exclusive_lease_taken(lease_key(project.id), timeout: lease_timeout)
|
2017-11-22 05:35:53 +00:00
|
|
|
|
2019-12-06 03:08:02 +00:00
|
|
|
expect(migration_service).not_to receive(:new)
|
2017-09-06 05:16:26 +00:00
|
|
|
|
2019-12-06 03:08:02 +00:00
|
|
|
subject.perform(project.id)
|
|
|
|
end
|
2017-09-06 05:16:26 +00:00
|
|
|
end
|
|
|
|
end
|
2019-12-06 03:08:02 +00:00
|
|
|
|
|
|
|
def lease_key(key)
|
|
|
|
"project_migrate_hashed_storage_worker:#{key}"
|
|
|
|
end
|
2017-09-06 05:16:26 +00:00
|
|
|
end
|