2019-03-30 03:23:56 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-08-29 03:56:52 -04:00
|
|
|
require 'spec_helper'
|
2018-05-02 09:35:04 -04:00
|
|
|
|
2020-06-24 14:09:03 -04:00
|
|
|
RSpec.describe ProjectImportState, type: :model do
|
2020-04-21 11:21:10 -04:00
|
|
|
let_it_be(:correlation_id) { 'cid' }
|
|
|
|
let_it_be(:import_state, refind: true) { create(:import_state, correlation_id_value: correlation_id) }
|
|
|
|
|
|
|
|
subject { import_state }
|
2018-05-02 09:35:04 -04:00
|
|
|
|
|
|
|
describe 'associations' do
|
|
|
|
it { is_expected.to belong_to(:project) }
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'validations' do
|
|
|
|
it { is_expected.to validate_presence_of(:project) }
|
|
|
|
end
|
2018-11-27 04:41:27 -05:00
|
|
|
|
|
|
|
describe 'Project import job' do
|
2020-04-08 14:09:16 -04:00
|
|
|
let_it_be(:import_state) { create(:import_state, import_url: generate(:url)) }
|
|
|
|
let_it_be(:project) { import_state.project }
|
2018-11-27 04:41:27 -05:00
|
|
|
|
|
|
|
before do
|
|
|
|
allow_any_instance_of(Gitlab::GitalyClient::RepositoryService).to receive(:import_repository)
|
2022-04-01 23:09:24 -04:00
|
|
|
.with(project.import_url, http_authorization_header: '', mirror: false).and_return(true)
|
2018-11-27 04:41:27 -05:00
|
|
|
|
|
|
|
# Works around https://github.com/rspec/rspec-mocks/issues/910
|
|
|
|
allow(Project).to receive(:find).with(project.id).and_return(project)
|
2020-03-11 20:09:34 -04:00
|
|
|
expect(project).to receive(:after_import).and_call_original
|
2018-11-27 04:41:27 -05:00
|
|
|
end
|
|
|
|
|
2019-10-23 05:06:03 -04:00
|
|
|
it 'imports a project', :sidekiq_might_not_need_inline do
|
2018-11-27 04:41:27 -05:00
|
|
|
expect(RepositoryImportWorker).to receive(:perform_async).and_call_original
|
|
|
|
|
2020-04-08 14:09:16 -04:00
|
|
|
expect { import_state.schedule }.to change { import_state.status }.from('none').to('finished')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'records job and correlation IDs', :sidekiq_might_not_need_inline do
|
2020-04-21 11:21:10 -04:00
|
|
|
allow(Labkit::Correlation::CorrelationId).to receive(:current_or_new_id).and_return(correlation_id)
|
2020-04-08 14:09:16 -04:00
|
|
|
|
|
|
|
import_state.schedule
|
|
|
|
|
|
|
|
expect(import_state.jid).to be_an_instance_of(String)
|
2020-04-21 11:21:10 -04:00
|
|
|
expect(import_state.correlation_id).to eq(correlation_id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#relation_hard_failures' do
|
|
|
|
let_it_be(:failures) { create_list(:import_failure, 2, :hard_failure, project: import_state.project, correlation_id_value: correlation_id) }
|
|
|
|
|
|
|
|
it 'returns hard relation failures related to this import' do
|
|
|
|
expect(subject.relation_hard_failures(limit: 100)).to match_array(failures)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'limits returned collection to given maximum' do
|
|
|
|
expect(subject.relation_hard_failures(limit: 1).size).to eq(1)
|
2018-11-27 04:41:27 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-24 11:08:04 -04:00
|
|
|
describe '#mark_as_failed' do
|
|
|
|
let(:error_message) { 'some message' }
|
|
|
|
|
|
|
|
it 'logs error when update column fails' do
|
|
|
|
allow(import_state).to receive(:update_column).and_raise(ActiveRecord::ActiveRecordError)
|
|
|
|
|
2020-05-25 11:07:58 -04:00
|
|
|
expect_next_instance_of(Gitlab::Import::Logger) do |logger|
|
|
|
|
expect(logger).to receive(:error).with(
|
2022-05-02 11:10:10 -04:00
|
|
|
{
|
|
|
|
error: 'ActiveRecord::ActiveRecordError',
|
|
|
|
message: 'Error setting import status to failed',
|
|
|
|
original_error: error_message
|
|
|
|
}
|
2020-05-25 11:07:58 -04:00
|
|
|
)
|
|
|
|
end
|
2020-05-24 11:08:04 -04:00
|
|
|
|
2020-05-25 11:07:58 -04:00
|
|
|
import_state.mark_as_failed(error_message)
|
2020-05-24 11:08:04 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'updates last_error with error message' do
|
|
|
|
import_state.mark_as_failed(error_message)
|
|
|
|
|
|
|
|
expect(import_state.last_error).to eq(error_message)
|
|
|
|
end
|
2022-02-09 07:12:04 -05:00
|
|
|
|
|
|
|
it 'removes project import data' do
|
|
|
|
import_data = ProjectImportData.new(data: { 'test' => 'some data' })
|
|
|
|
project = create(:project, import_data: import_data)
|
|
|
|
import_state = create(:import_state, :started, project: project)
|
|
|
|
|
|
|
|
expect do
|
|
|
|
import_state.mark_as_failed(error_message)
|
|
|
|
end.to change { project.reload.import_data }.from(import_data).to(nil)
|
|
|
|
end
|
2020-05-24 11:08:04 -04:00
|
|
|
end
|
|
|
|
|
2018-11-27 04:41:27 -05:00
|
|
|
describe '#human_status_name' do
|
|
|
|
context 'when import_state exists' do
|
|
|
|
it 'returns the humanized status name' do
|
|
|
|
import_state = build(:import_state, :started)
|
|
|
|
|
|
|
|
expect(import_state.human_status_name).to eq("started")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-03-31 14:08:39 -04:00
|
|
|
describe '#expire_etag_cache' do
|
|
|
|
context 'when project import type has realtime changes endpoint' do
|
|
|
|
before do
|
|
|
|
import_state.project.import_type = 'github'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'expires revelant etag cache' do
|
|
|
|
expect_next_instance_of(Gitlab::EtagCaching::Store) do |instance|
|
|
|
|
expect(instance).to receive(:touch).with(Gitlab::Routing.url_helpers.realtime_changes_import_github_path(format: :json))
|
|
|
|
end
|
|
|
|
|
|
|
|
subject.expire_etag_cache
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when project import type does not have realtime changes endpoint' do
|
|
|
|
before do
|
|
|
|
import_state.project.import_type = 'jira'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not touch etag caches' do
|
|
|
|
expect(Gitlab::EtagCaching::Store).not_to receive(:new)
|
|
|
|
|
|
|
|
subject.expire_etag_cache
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-27 04:41:27 -05:00
|
|
|
describe 'import state transitions' do
|
|
|
|
context 'state transition: [:started] => [:finished]' do
|
|
|
|
it 'resets last_error' do
|
|
|
|
error_message = 'Some error'
|
|
|
|
import_state = create(:import_state, :started, last_error: error_message)
|
|
|
|
|
|
|
|
expect { import_state.finish }.to change { import_state.last_error }.from(error_message).to(nil)
|
|
|
|
end
|
|
|
|
|
2022-04-22 08:08:38 -04:00
|
|
|
it 'enqueues housekeeping when an import of a fresh project is completed' do
|
2018-11-27 04:41:27 -05:00
|
|
|
project = create(:project_empty_repo, :import_started, import_type: :github)
|
|
|
|
|
2022-04-22 08:08:38 -04:00
|
|
|
expect(Projects::AfterImportWorker).to receive(:perform_async).with(project.id)
|
2018-11-27 04:41:27 -05:00
|
|
|
|
2022-04-22 08:08:38 -04:00
|
|
|
project.import_state.finish
|
2018-11-27 04:41:27 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not perform housekeeping when project repository does not exist' do
|
|
|
|
project = create(:project, :import_started, import_type: :github)
|
|
|
|
|
2022-04-22 08:08:38 -04:00
|
|
|
expect(Projects::AfterImportWorker).not_to receive(:perform_async)
|
2018-11-27 04:41:27 -05:00
|
|
|
|
2022-04-22 08:08:38 -04:00
|
|
|
project.import_state.finish
|
2018-11-27 04:41:27 -05:00
|
|
|
end
|
|
|
|
|
2022-04-22 08:08:38 -04:00
|
|
|
it 'does not qneueue housekeeping when project does not have a valid import type' do
|
2018-11-27 04:41:27 -05:00
|
|
|
project = create(:project, :import_started, import_type: nil)
|
|
|
|
|
2022-04-22 08:08:38 -04:00
|
|
|
expect(Projects::AfterImportWorker).not_to receive(:perform_async)
|
2018-11-27 04:41:27 -05:00
|
|
|
|
2022-04-22 08:08:38 -04:00
|
|
|
project.import_state.finish
|
2018-11-27 04:41:27 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-11-21 19:06:08 -05:00
|
|
|
describe 'clearing `jid` after finish', :clean_gitlab_redis_cache do
|
2018-11-27 04:41:27 -05:00
|
|
|
context 'without an JID' do
|
|
|
|
it 'does nothing' do
|
2019-11-21 19:06:08 -05:00
|
|
|
import_state = create(:import_state, :started)
|
2018-11-27 04:41:27 -05:00
|
|
|
|
|
|
|
expect(Gitlab::SidekiqStatus)
|
|
|
|
.not_to receive(:unset)
|
|
|
|
|
2019-11-21 19:06:08 -05:00
|
|
|
import_state.finish!
|
2018-11-27 04:41:27 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with an JID' do
|
|
|
|
it 'unsets the JID' do
|
2019-11-21 19:06:08 -05:00
|
|
|
import_state = create(:import_state, :started, jid: '123')
|
2018-11-27 04:41:27 -05:00
|
|
|
|
|
|
|
expect(Gitlab::SidekiqStatus)
|
|
|
|
.to receive(:unset)
|
|
|
|
.with('123')
|
|
|
|
.and_call_original
|
|
|
|
|
2019-11-21 19:06:08 -05:00
|
|
|
import_state.finish!
|
2018-11-27 04:41:27 -05:00
|
|
|
|
|
|
|
expect(import_state.jid).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-03-31 14:08:39 -04:00
|
|
|
|
|
|
|
describe 'callbacks' do
|
|
|
|
context 'after_commit :expire_etag_cache' do
|
|
|
|
before do
|
|
|
|
import_state.project.import_type = 'github'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'expires etag cache' do
|
|
|
|
expect_next_instance_of(Gitlab::EtagCaching::Store) do |instance|
|
|
|
|
expect(instance).to receive(:touch).with(Gitlab::Routing.url_helpers.realtime_changes_import_github_path(format: :json))
|
|
|
|
end
|
|
|
|
|
|
|
|
subject.save!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-05-02 09:35:04 -04:00
|
|
|
end
|