2016-02-16 16:31:37 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe RepositoryImportWorker do
|
2017-08-02 19:55:11 +00:00
|
|
|
let(:project) { create(:project, :import_scheduled) }
|
2016-02-16 16:31:37 +00:00
|
|
|
|
|
|
|
subject { described_class.new }
|
|
|
|
|
|
|
|
describe '#perform' do
|
2017-10-10 09:09:49 +00:00
|
|
|
context 'when worker was reset without cleanup' do
|
|
|
|
let(:jid) { '12345678' }
|
|
|
|
let(:started_project) { create(:project, :import_started, import_jid: jid) }
|
|
|
|
|
|
|
|
it 'imports the project successfully' do
|
|
|
|
allow(subject).to receive(:jid).and_return(jid)
|
|
|
|
|
|
|
|
expect_any_instance_of(Projects::ImportService).to receive(:execute)
|
|
|
|
.and_return({ status: :ok })
|
|
|
|
|
|
|
|
expect_any_instance_of(Repository).to receive(:expire_emptiness_caches)
|
|
|
|
expect_any_instance_of(Project).to receive(:import_finish)
|
|
|
|
|
|
|
|
subject.perform(project.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-05-19 02:16:36 +00:00
|
|
|
context 'when the import was successful' do
|
|
|
|
it 'imports a project' do
|
2017-06-21 13:48:12 +00:00
|
|
|
expect_any_instance_of(Projects::ImportService).to receive(:execute)
|
|
|
|
.and_return({ status: :ok })
|
2016-02-16 16:31:37 +00:00
|
|
|
|
2016-05-19 02:16:36 +00:00
|
|
|
expect_any_instance_of(Repository).to receive(:expire_emptiness_caches)
|
|
|
|
expect_any_instance_of(Project).to receive(:import_finish)
|
2016-02-16 16:31:37 +00:00
|
|
|
|
2016-05-19 02:16:36 +00:00
|
|
|
subject.perform(project.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the import has failed' do
|
|
|
|
it 'hide the credentials that were used in the import URL' do
|
2017-02-22 22:39:43 +00:00
|
|
|
error = %q{remote: Not Found fatal: repository 'https://user:pass@test.com/root/repoC.git/' not found }
|
2016-05-19 02:16:36 +00:00
|
|
|
|
2017-08-17 09:00:31 +00:00
|
|
|
project.update_attributes(import_jid: '123')
|
2017-06-01 14:27:35 +00:00
|
|
|
expect_any_instance_of(Projects::ImportService).to receive(:execute).and_return({ status: :error, message: error })
|
2016-05-19 02:16:36 +00:00
|
|
|
|
2017-06-01 14:27:35 +00:00
|
|
|
expect do
|
|
|
|
subject.perform(project.id)
|
|
|
|
end.to raise_error(RepositoryImportWorker::ImportError, error)
|
2017-03-24 11:08:34 +00:00
|
|
|
expect(project.reload.import_jid).not_to be_nil
|
2016-05-19 02:16:36 +00:00
|
|
|
end
|
2016-02-16 16:31:37 +00:00
|
|
|
end
|
2017-06-01 14:27:35 +00:00
|
|
|
|
|
|
|
context 'with unexpected error' do
|
|
|
|
it 'marks import as failed' do
|
|
|
|
allow_any_instance_of(Projects::ImportService).to receive(:execute).and_raise(RuntimeError)
|
|
|
|
|
|
|
|
expect do
|
|
|
|
subject.perform(project.id)
|
|
|
|
end.to raise_error(RepositoryImportWorker::ImportError)
|
|
|
|
expect(project.reload.import_status).to eq('failed')
|
|
|
|
end
|
|
|
|
end
|
2017-10-13 16:50:36 +00:00
|
|
|
|
|
|
|
context 'when using an asynchronous importer' do
|
|
|
|
it 'does not mark the import process as finished' do
|
|
|
|
service = double(:service)
|
|
|
|
|
|
|
|
allow(Projects::ImportService)
|
|
|
|
.to receive(:new)
|
|
|
|
.and_return(service)
|
|
|
|
|
|
|
|
allow(service)
|
|
|
|
.to receive(:execute)
|
|
|
|
.and_return(true)
|
|
|
|
|
|
|
|
allow(service)
|
|
|
|
.to receive(:async?)
|
|
|
|
.and_return(true)
|
|
|
|
|
|
|
|
expect_any_instance_of(Project)
|
|
|
|
.not_to receive(:import_finish)
|
|
|
|
|
|
|
|
subject.perform(project.id)
|
|
|
|
end
|
|
|
|
end
|
2016-02-16 16:31:37 +00:00
|
|
|
end
|
|
|
|
end
|