diff options
Diffstat (limited to 'spec/workers')
8 files changed, 135 insertions, 23 deletions
diff --git a/spec/workers/gitlab/jira_import/import_issue_worker_spec.rb b/spec/workers/gitlab/jira_import/import_issue_worker_spec.rb new file mode 100644 index 00000000000..c09492efcae --- /dev/null +++ b/spec/workers/gitlab/jira_import/import_issue_worker_spec.rb @@ -0,0 +1,44 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Gitlab::JiraImport::ImportIssueWorker do + let_it_be(:user) { create(:user) } + let_it_be(:project) { create(:project) } + + describe 'modules' do + it { expect(described_class).to include_module(ApplicationWorker) } + it { expect(described_class).to include_module(Gitlab::NotifyUponDeath) } + it { expect(described_class).to include_module(Gitlab::JiraImport::QueueOptions) } + it { expect(described_class).to include_module(Gitlab::Import::DatabaseHelpers) } + end + + subject { described_class.new } + + describe '#perform', :clean_gitlab_redis_cache do + let(:issue_attrs) { build(:issue, project_id: project.id).as_json.compact } + + context 'when any exception raised while inserting to DB' do + before do + allow(subject).to receive(:insert_and_return_id).and_raise(StandardError) + expect(Gitlab::JobWaiter).to receive(:notify) + + subject.perform(project.id, 123, issue_attrs, 'some-key') + end + + it 'record a failed to import issue' do + expect(Gitlab::Cache::Import::Caching.read(Gitlab::JiraImport.failed_issues_counter_cache_key(project.id)).to_i).to eq(1) + end + end + + context 'when record is successfully inserted' do + before do + subject.perform(project.id, 123, issue_attrs, 'some-key') + end + + it 'does not record import failure' do + expect(Gitlab::Cache::Import::Caching.read(Gitlab::JiraImport.failed_issues_counter_cache_key(project.id)).to_i).to eq(0) + end + end + end +end diff --git a/spec/workers/gitlab/jira_import/stage/finish_import_worker_spec.rb b/spec/workers/gitlab/jira_import/stage/finish_import_worker_spec.rb index fa0c7d83851..00505226212 100644 --- a/spec/workers/gitlab/jira_import/stage/finish_import_worker_spec.rb +++ b/spec/workers/gitlab/jira_import/stage/finish_import_worker_spec.rb @@ -3,8 +3,8 @@ require 'spec_helper' describe Gitlab::JiraImport::Stage::FinishImportWorker do - let(:project) { create(:project) } - let(:worker) { described_class.new } + let_it_be(:project) { create(:project) } + let_it_be(:worker) { described_class.new } describe 'modules' do it_behaves_like 'include import workers modules' @@ -46,7 +46,7 @@ describe Gitlab::JiraImport::Stage::FinishImportWorker do it 'changes import state to finished' do worker.perform(project.id) - expect(project.reload.import_state.status).to eq "finished" + expect(project.reload.import_state.status).to eq("finished") end it 'removes force-import flag' do diff --git a/spec/workers/gitlab/jira_import/stage/import_attachments_worker_spec.rb b/spec/workers/gitlab/jira_import/stage/import_attachments_worker_spec.rb index fa2f3501973..513925507a1 100644 --- a/spec/workers/gitlab/jira_import/stage/import_attachments_worker_spec.rb +++ b/spec/workers/gitlab/jira_import/stage/import_attachments_worker_spec.rb @@ -3,8 +3,7 @@ require 'spec_helper' describe Gitlab::JiraImport::Stage::ImportAttachmentsWorker do - let(:project) { create(:project) } - let(:worker) { described_class.new } + let_it_be(:project) { create(:project) } describe 'modules' do it_behaves_like 'include import workers modules' diff --git a/spec/workers/gitlab/jira_import/stage/import_issues_worker_spec.rb b/spec/workers/gitlab/jira_import/stage/import_issues_worker_spec.rb index b43519a3e5d..dca748a6ebc 100644 --- a/spec/workers/gitlab/jira_import/stage/import_issues_worker_spec.rb +++ b/spec/workers/gitlab/jira_import/stage/import_issues_worker_spec.rb @@ -3,8 +3,8 @@ require 'spec_helper' describe Gitlab::JiraImport::Stage::ImportIssuesWorker do - let(:project) { create(:project) } - let(:worker) { described_class.new } + let_it_be(:user) { create(:user) } + let_it_be(:project) { create(:project) } describe 'modules' do it_behaves_like 'include import workers modules' @@ -30,10 +30,49 @@ describe Gitlab::JiraImport::Stage::ImportIssuesWorker do it_behaves_like 'exit import not started' end - context 'when import started' do + context 'when import started', :clean_gitlab_redis_cache do + let(:jira_import_data) do + data = JiraImportData.new + data << JiraImportData::JiraProjectDetails.new('XX', Time.now.strftime('%Y-%m-%d %H:%M:%S'), { user_id: user.id, name: user.name }) + data + end + let(:project) { create(:project, import_data: jira_import_data) } + let!(:jira_service) { create(:jira_service, project: project) } let!(:import_state) { create(:import_state, status: :started, project: project) } - it_behaves_like 'advance to next stage', :attachments + before do + allow_next_instance_of(Gitlab::JiraImport::IssuesImporter) do |instance| + allow(instance).to receive(:fetch_issues).and_return([]) + end + end + + context 'when start_at is nil' do + it_behaves_like 'advance to next stage', :attachments + end + + context 'when start_at is zero' do + before do + allow(Gitlab::Cache::Import::Caching).to receive(:read).and_return(0) + end + + it_behaves_like 'advance to next stage', :issues + end + + context 'when start_at is greater than zero' do + before do + allow(Gitlab::Cache::Import::Caching).to receive(:read).and_return(25) + end + + it_behaves_like 'advance to next stage', :issues + end + + context 'when start_at is below zero' do + before do + allow(Gitlab::Cache::Import::Caching).to receive(:read).and_return(-1) + end + + it_behaves_like 'advance to next stage', :attachments + end end end end diff --git a/spec/workers/gitlab/jira_import/stage/import_labels_worker_spec.rb b/spec/workers/gitlab/jira_import/stage/import_labels_worker_spec.rb index 827efb85a17..2b156e0f489 100644 --- a/spec/workers/gitlab/jira_import/stage/import_labels_worker_spec.rb +++ b/spec/workers/gitlab/jira_import/stage/import_labels_worker_spec.rb @@ -3,8 +3,7 @@ require 'spec_helper' describe Gitlab::JiraImport::Stage::ImportLabelsWorker do - let(:project) { create(:project) } - let(:worker) { described_class.new } + let_it_be(:project) { create(:project) } describe 'modules' do it_behaves_like 'include import workers modules' diff --git a/spec/workers/gitlab/jira_import/stage/import_notes_worker_spec.rb b/spec/workers/gitlab/jira_import/stage/import_notes_worker_spec.rb index bd6b36613cc..7d1c29614e4 100644 --- a/spec/workers/gitlab/jira_import/stage/import_notes_worker_spec.rb +++ b/spec/workers/gitlab/jira_import/stage/import_notes_worker_spec.rb @@ -3,8 +3,7 @@ require 'spec_helper' describe Gitlab::JiraImport::Stage::ImportNotesWorker do - let(:project) { create(:project) } - let(:worker) { described_class.new } + let_it_be(:project) { create(:project) } describe 'modules' do it_behaves_like 'include import workers modules' diff --git a/spec/workers/gitlab/jira_import/stage/start_import_worker_spec.rb b/spec/workers/gitlab/jira_import/stage/start_import_worker_spec.rb index cc70277384d..d5e10a950bb 100644 --- a/spec/workers/gitlab/jira_import/stage/start_import_worker_spec.rb +++ b/spec/workers/gitlab/jira_import/stage/start_import_worker_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe Gitlab::JiraImport::Stage::StartImportWorker do - let(:project) { create(:project) } + let(:project) { create(:project, import_type: 'jira') } let(:worker) { described_class.new } let(:jid) { '12345678' } @@ -24,13 +24,19 @@ describe Gitlab::JiraImport::Stage::StartImportWorker do end end - context 'when feature flag not enabled' do + context 'when feature flag enabled' do + let(:symbol_keys_project) do + { key: 'AA', scheduled_at: 2.days.ago.strftime('%Y-%m-%d %H:%M:%S'), scheduled_by: { 'user_id' => 1, 'name' => 'tester1' } } + end + let(:import_data) { JiraImportData.new( data: { 'jira' => { JiraImportData::FORCE_IMPORT_KEY => true, projects: [symbol_keys_project] } }) } + before do stub_feature_flags(jira_issue_import: true) end - context 'when import is not scheudled' do - let!(:import_state) { create(:import_state, project: project, status: :none, jid: jid) } + context 'when import is not scheduled' do + let(:project) { create(:project, import_type: 'jira') } + let(:import_state) { create(:import_state, project: project, status: :none, jid: jid) } it 'exits because import not started' do expect(Gitlab::JiraImport::Stage::ImportLabelsWorker).not_to receive(:perform_async) @@ -40,17 +46,32 @@ describe Gitlab::JiraImport::Stage::StartImportWorker do end context 'when import is scheduled' do - let!(:import_state) { create(:import_state, project: project, status: :scheduled, jid: jid) } + let(:import_state) { create(:import_state, status: :scheduled, jid: jid) } + let(:project) { create(:project, import_type: 'jira', import_state: import_state) } - it 'advances to importing labels' do - expect(Gitlab::JiraImport::Stage::ImportLabelsWorker).to receive(:perform_async) + context 'when this is a mirror sync in a jira imported project' do + it 'exits early' do + expect(Gitlab::Import::SetAsyncJid).not_to receive(:set_jid) + expect(Gitlab::JiraImport::Stage::ImportLabelsWorker).not_to receive(:perform_async) - worker.perform(project.id) + worker.perform(project.id) + end + end + + context 'when scheduled import is a hard triggered jira import and not a mirror' do + let!(:project) { create(:project, import_type: 'jira', import_data: import_data, import_state: import_state) } + + it 'advances to importing labels' do + expect(Gitlab::JiraImport::Stage::ImportLabelsWorker).to receive(:perform_async) + + worker.perform(project.id) + end end end context 'when import is started' do - let!(:import_state) { create(:import_state, project: project, status: :started, jid: jid) } + let!(:import_state) { create(:import_state, status: :started, jid: jid) } + let!(:project) { create(:project, import_type: 'jira', import_data: import_data, import_state: import_state) } context 'when this is the same worker that stated import' do it 'advances to importing labels' do @@ -72,7 +93,8 @@ describe Gitlab::JiraImport::Stage::StartImportWorker do end context 'when import is finished' do - let!(:import_state) { create(:import_state, project: project, status: :finished, jid: jid) } + let!(:import_state) { create(:import_state, status: :finished, jid: jid) } + let!(:project) { create(:project, import_type: 'jira', import_data: import_data, import_state: import_state) } it 'advances to importing labels' do allow(worker).to receive(:jid).and_return(jid) diff --git a/spec/workers/reactive_caching_worker_spec.rb b/spec/workers/reactive_caching_worker_spec.rb index 6c74c4ea072..c39a97b4eee 100644 --- a/spec/workers/reactive_caching_worker_spec.rb +++ b/spec/workers/reactive_caching_worker_spec.rb @@ -28,4 +28,14 @@ describe ReactiveCachingWorker do end end end + + describe 'worker context' do + it 'sets the related class on the job' do + described_class.perform_async('Environment', 1, 'other', 'argument') + + scheduled_job = described_class.jobs.first + + expect(scheduled_job).to include('meta.related_class' => 'Environment') + end + end end |