diff options
Diffstat (limited to 'spec/workers/gitlab')
11 files changed, 274 insertions, 117 deletions
diff --git a/spec/workers/gitlab/bitbucket_import/advance_stage_worker_spec.rb b/spec/workers/gitlab/bitbucket_import/advance_stage_worker_spec.rb new file mode 100644 index 00000000000..16e3a3dc481 --- /dev/null +++ b/spec/workers/gitlab/bitbucket_import/advance_stage_worker_spec.rb @@ -0,0 +1,115 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::BitbucketImport::AdvanceStageWorker, :clean_gitlab_redis_shared_state, feature_category: :importers do + let(:project) { create(:project) } + let(:import_state) { create(:import_state, project: project, jid: '123') } + let(:worker) { described_class.new } + + describe '#perform' do + context 'when the project no longer exists' do + it 'does not perform any work' do + expect(worker).not_to receive(:wait_for_jobs) + + worker.perform(-1, { '123' => 2 }, :finish) + end + end + + context 'when there are remaining jobs' do + before do + allow(worker) + .to receive(:find_import_state) + .and_return(import_state) + end + + it 'reschedules itself' do + expect(worker) + .to receive(:wait_for_jobs) + .with({ '123' => 2 }) + .and_return({ '123' => 1 }) + + expect(described_class) + .to receive(:perform_in) + .with(described_class::INTERVAL, project.id, { '123' => 1 }, :finish) + + worker.perform(project.id, { '123' => 2 }, :finish) + end + end + + context 'when there are no remaining jobs' do + before do + allow(worker) + .to receive(:find_import_state) + .and_return(import_state) + + allow(worker) + .to receive(:wait_for_jobs) + .with({ '123' => 2 }) + .and_return({}) + end + + it 'schedules the next stage' do + expect(import_state) + .to receive(:refresh_jid_expiration) + + expect(Gitlab::BitbucketImport::Stage::FinishImportWorker) + .to receive(:perform_async) + .with(project.id) + + worker.perform(project.id, { '123' => 2 }, :finish) + end + + it 'raises KeyError when the stage name is invalid' do + expect { worker.perform(project.id, { '123' => 2 }, :kittens) } + .to raise_error(KeyError) + end + end + end + + describe '#wait_for_jobs' do + it 'waits for jobs to complete and returns a new pair of keys to wait for' do + waiter1 = instance_double(Gitlab::JobWaiter, jobs_remaining: 1, key: '123') + waiter2 = instance_double(Gitlab::JobWaiter, jobs_remaining: 0, key: '456') + + expect(Gitlab::JobWaiter) + .to receive(:new) + .ordered + .with(2, '123') + .and_return(waiter1) + + expect(Gitlab::JobWaiter) + .to receive(:new) + .ordered + .with(1, '456') + .and_return(waiter2) + + expect(waiter1) + .to receive(:wait) + .with(described_class::BLOCKING_WAIT_TIME) + + expect(waiter2) + .to receive(:wait) + .with(described_class::BLOCKING_WAIT_TIME) + + new_waiters = worker.wait_for_jobs({ '123' => 2, '456' => 1 }) + + expect(new_waiters).to eq({ '123' => 1 }) + end + end + + describe '#find_import_state' do + it 'returns a ProjectImportState' do + import_state.update_column(:status, 'started') + + found = worker.find_import_state(project.id) + + expect(found).to be_an_instance_of(ProjectImportState) + expect(found.attributes.keys).to match_array(%w[id jid]) + end + + it 'returns nil if the project import is not running' do + expect(worker.find_import_state(project.id)).to be_nil + end + end +end diff --git a/spec/workers/gitlab/bitbucket_import/import_pull_request_worker_spec.rb b/spec/workers/gitlab/bitbucket_import/import_pull_request_worker_spec.rb new file mode 100644 index 00000000000..082499be515 --- /dev/null +++ b/spec/workers/gitlab/bitbucket_import/import_pull_request_worker_spec.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::BitbucketImport::ImportPullRequestWorker, feature_category: :importers do + subject(:worker) { described_class.new } + + it_behaves_like Gitlab::BitbucketImport::ObjectImporter +end diff --git a/spec/workers/gitlab/bitbucket_import/stage/finish_import_worker_spec.rb b/spec/workers/gitlab/bitbucket_import/stage/finish_import_worker_spec.rb new file mode 100644 index 00000000000..11baa58f1ab --- /dev/null +++ b/spec/workers/gitlab/bitbucket_import/stage/finish_import_worker_spec.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::BitbucketImport::Stage::FinishImportWorker, feature_category: :importers do + let_it_be(:project) { create(:project, :import_started) } + + subject(:worker) { described_class.new } + + it_behaves_like Gitlab::BitbucketImport::StageMethods + + it 'does not abort on failure' do + expect(worker.abort_on_failure).to be_falsey + end + + describe '#perform' do + it 'finalises the import process' do + expect_next_instance_of(Gitlab::Import::Metrics, :bitbucket_importer, project) do |metric| + expect(metric).to receive(:track_finished_import) + end + + worker.perform(project.id) + + expect(project.import_state.reload).to be_finished + end + end +end diff --git a/spec/workers/gitlab/bitbucket_import/stage/import_pull_requests_worker_spec.rb b/spec/workers/gitlab/bitbucket_import/stage/import_pull_requests_worker_spec.rb new file mode 100644 index 00000000000..8f425066160 --- /dev/null +++ b/spec/workers/gitlab/bitbucket_import/stage/import_pull_requests_worker_spec.rb @@ -0,0 +1,77 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::BitbucketImport::Stage::ImportPullRequestsWorker, feature_category: :importers do + let_it_be(:project) { create(:project, :import_started) } + + subject(:worker) { described_class.new } + + it_behaves_like Gitlab::BitbucketImport::StageMethods + + describe '#perform' do + context 'when the import succeeds' do + before do + allow_next_instance_of(Gitlab::BitbucketImport::Importers::PullRequestsImporter) do |importer| + allow(importer).to receive(:execute).and_return(Gitlab::JobWaiter.new(2, '123')) + end + end + + it 'schedules the next stage' do + expect(Gitlab::BitbucketImport::AdvanceStageWorker).to receive(:perform_async) + .with(project.id, { '123' => 2 }, :finish) + + worker.perform(project.id) + end + + it 'logs stage start and finish' do + expect(Gitlab::BitbucketImport::Logger) + .to receive(:info).with(hash_including(message: 'starting stage', project_id: project.id)) + expect(Gitlab::BitbucketImport::Logger) + .to receive(:info).with(hash_including(message: 'stage finished', project_id: project.id)) + + worker.perform(project.id) + end + end + + context 'when project does not exists' do + it 'does not call the importer' do + expect(Gitlab::BitbucketImport::Importers::PullRequestsImporter).not_to receive(:new) + + worker.perform(-1) + end + end + + context 'when project import state is not `started`' do + it 'does not call the importer' do + project = create(:project, :import_canceled) + + expect(Gitlab::BitbucketImport::Importers::PullRequestsImporter).not_to receive(:new) + + worker.perform(project.id) + end + end + + context 'when the importer fails' do + it 'does not schedule the next stage and raises error' do + exception = StandardError.new('Error') + + allow_next_instance_of(Gitlab::BitbucketImport::Importers::PullRequestsImporter) do |importer| + allow(importer).to receive(:execute).and_raise(exception) + end + + expect(Gitlab::Import::ImportFailureService) + .to receive(:track).with( + project_id: project.id, + exception: exception, + error_source: described_class.name, + fail_import: false + ).and_call_original + + expect { worker.perform(project.id) } + .to change { Gitlab::BitbucketImport::AdvanceStageWorker.jobs.size }.by(0) + .and raise_error(exception) + end + end + end +end diff --git a/spec/workers/gitlab/bitbucket_import/stage/import_repository_worker_spec.rb b/spec/workers/gitlab/bitbucket_import/stage/import_repository_worker_spec.rb new file mode 100644 index 00000000000..2234a49d66c --- /dev/null +++ b/spec/workers/gitlab/bitbucket_import/stage/import_repository_worker_spec.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::BitbucketImport::Stage::ImportRepositoryWorker, feature_category: :importers do + let_it_be(:project) { create(:project, :import_started) } + + let(:worker) { described_class.new } + + it_behaves_like Gitlab::BitbucketImport::StageMethods + + it 'executes the importer and enqueues ImportPullRequestsWorker' do + expect(Gitlab::BitbucketImport::Importers::RepositoryImporter).to receive_message_chain(:new, :execute) + .and_return(true) + + expect(Gitlab::BitbucketImport::Stage::ImportPullRequestsWorker).to receive(:perform_async).with(project.id) + .and_return(true).once + + worker.perform(project.id) + end +end diff --git a/spec/workers/gitlab/bitbucket_server_import/advance_stage_worker_spec.rb b/spec/workers/gitlab/bitbucket_server_import/advance_stage_worker_spec.rb new file mode 100644 index 00000000000..14e93440422 --- /dev/null +++ b/spec/workers/gitlab/bitbucket_server_import/advance_stage_worker_spec.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::BitbucketServerImport::AdvanceStageWorker, feature_category: :importers do + it_behaves_like Gitlab::Import::AdvanceStage, factory: :import_state +end diff --git a/spec/workers/gitlab/bitbucket_server_import/import_pull_request_worker_spec.rb b/spec/workers/gitlab/bitbucket_server_import/import_pull_request_worker_spec.rb index dd3235f846c..376078532cd 100644 --- a/spec/workers/gitlab/bitbucket_server_import/import_pull_request_worker_spec.rb +++ b/spec/workers/gitlab/bitbucket_server_import/import_pull_request_worker_spec.rb @@ -26,7 +26,7 @@ RSpec.describe Gitlab::BitbucketServerImport::ImportPullRequestWorker, feature_c end it 'notifies job waiter' do - expect(Gitlab::JobWaiter).to receive(:notify).with(job_waiter_key, 'jid') + expect(Gitlab::JobWaiter).to receive(:notify).with(job_waiter_key, 'jid', ttl: Gitlab::Import::JOB_WAITER_TTL) worker.perform(project.id, {}, job_waiter_key) end @@ -44,7 +44,7 @@ RSpec.describe Gitlab::BitbucketServerImport::ImportPullRequestWorker, feature_c context 'when project does not exists' do it 'does not call importer and notifies job waiter' do expect(importer_class).not_to receive(:new) - expect(Gitlab::JobWaiter).to receive(:notify).with(job_waiter_key, 'jid') + expect(Gitlab::JobWaiter).to receive(:notify).with(job_waiter_key, 'jid', ttl: Gitlab::Import::JOB_WAITER_TTL) worker.perform(-1, {}, job_waiter_key) end @@ -55,7 +55,7 @@ RSpec.describe Gitlab::BitbucketServerImport::ImportPullRequestWorker, feature_c project = create(:project, :import_canceled) expect(importer_class).not_to receive(:new) - expect(Gitlab::JobWaiter).to receive(:notify).with(job_waiter_key, 'jid') + expect(Gitlab::JobWaiter).to receive(:notify).with(job_waiter_key, 'jid', ttl: Gitlab::Import::JOB_WAITER_TTL) worker.perform(project.id, {}, job_waiter_key) end diff --git a/spec/workers/gitlab/github_gists_import/import_gist_worker_spec.rb b/spec/workers/gitlab/github_gists_import/import_gist_worker_spec.rb index 2e89263bcf3..dc715c3026b 100644 --- a/spec/workers/gitlab/github_gists_import/import_gist_worker_spec.rb +++ b/spec/workers/gitlab/github_gists_import/import_gist_worker_spec.rb @@ -68,7 +68,7 @@ RSpec.describe Gitlab::GithubGistsImport::ImportGistWorker, feature_category: :i .to receive(:info) .with(log_attributes.merge('message' => 'start importer')) expect(importer).to receive(:execute).and_return(importer_result) - expect(Gitlab::JobWaiter).to receive(:notify).with('some_key', subject.jid) + expect(Gitlab::JobWaiter).to receive(:notify).with('some_key', subject.jid, ttl: Gitlab::Import::JOB_WAITER_TTL) expect(Gitlab::GithubImport::Logger) .to receive(:info) .with(log_attributes.merge('message' => 'importer finished')) @@ -114,7 +114,9 @@ RSpec.describe Gitlab::GithubGistsImport::ImportGistWorker, feature_category: :i expect(Gitlab::GithubImport::Logger) .to receive(:error) .with(log_attributes.merge('message' => 'importer failed', 'error.message' => 'error_message')) - expect(Gitlab::JobWaiter).to receive(:notify).with('some_key', subject.jid) + expect(Gitlab::JobWaiter) + .to receive(:notify) + .with('some_key', subject.jid, ttl: Gitlab::Import::JOB_WAITER_TTL) subject.perform(user.id, gist_hash, 'some_key') @@ -189,7 +191,7 @@ RSpec.describe Gitlab::GithubGistsImport::ImportGistWorker, feature_category: :i it 'notifies the JobWaiter' do expect(Gitlab::JobWaiter) .to receive(:notify) - .with(job['args'].last, job['jid']) + .with(job['args'].last, job['jid'], ttl: Gitlab::Import::JOB_WAITER_TTL) sidekiq_retries_exhausted end diff --git a/spec/workers/gitlab/github_import/advance_stage_worker_spec.rb b/spec/workers/gitlab/github_import/advance_stage_worker_spec.rb index 121f30ea9d5..60c117a2a90 100644 --- a/spec/workers/gitlab/github_import/advance_stage_worker_spec.rb +++ b/spec/workers/gitlab/github_import/advance_stage_worker_spec.rb @@ -2,114 +2,6 @@ require 'spec_helper' -RSpec.describe Gitlab::GithubImport::AdvanceStageWorker, :clean_gitlab_redis_shared_state, feature_category: :importers do - let(:project) { create(:project) } - let(:import_state) { create(:import_state, project: project, jid: '123') } - let(:worker) { described_class.new } - - describe '#perform' do - context 'when the project no longer exists' do - it 'does not perform any work' do - expect(worker).not_to receive(:wait_for_jobs) - - worker.perform(-1, { '123' => 2 }, :finish) - end - end - - context 'when there are remaining jobs' do - before do - allow(worker) - .to receive(:find_import_state) - .and_return(import_state) - end - - it 'reschedules itself' do - expect(worker) - .to receive(:wait_for_jobs) - .with({ '123' => 2 }) - .and_return({ '123' => 1 }) - - expect(described_class) - .to receive(:perform_in) - .with(described_class::INTERVAL, project.id, { '123' => 1 }, :finish) - - worker.perform(project.id, { '123' => 2 }, :finish) - end - end - - context 'when there are no remaining jobs' do - before do - allow(worker) - .to receive(:find_import_state) - .and_return(import_state) - - allow(worker) - .to receive(:wait_for_jobs) - .with({ '123' => 2 }) - .and_return({}) - end - - it 'schedules the next stage' do - expect(import_state) - .to receive(:refresh_jid_expiration) - - expect(Gitlab::GithubImport::Stage::FinishImportWorker) - .to receive(:perform_async) - .with(project.id) - - worker.perform(project.id, { '123' => 2 }, :finish) - end - - it 'raises KeyError when the stage name is invalid' do - expect { worker.perform(project.id, { '123' => 2 }, :kittens) } - .to raise_error(KeyError) - end - end - end - - describe '#wait_for_jobs' do - it 'waits for jobs to complete and returns a new pair of keys to wait for' do - waiter1 = double(:waiter1, jobs_remaining: 1, key: '123') - waiter2 = double(:waiter2, jobs_remaining: 0, key: '456') - - expect(Gitlab::JobWaiter) - .to receive(:new) - .ordered - .with(2, '123') - .and_return(waiter1) - - expect(Gitlab::JobWaiter) - .to receive(:new) - .ordered - .with(1, '456') - .and_return(waiter2) - - expect(waiter1) - .to receive(:wait) - .with(described_class::BLOCKING_WAIT_TIME) - - expect(waiter2) - .to receive(:wait) - .with(described_class::BLOCKING_WAIT_TIME) - - new_waiters = worker.wait_for_jobs({ '123' => 2, '456' => 1 }) - - expect(new_waiters).to eq({ '123' => 1 }) - end - end - - describe '#find_import_state' do - it 'returns a ProjectImportState' do - import_state.update_column(:status, 'started') - - found = worker.find_import_state(project.id) - - expect(found).to be_an_instance_of(ProjectImportState) - expect(found.attributes.keys).to match_array(%w(id jid)) - end - - it 'returns nil if the project import is not running' do - expect(worker.find_import_state(project.id)).to be_nil - end - end +RSpec.describe Gitlab::GithubImport::AdvanceStageWorker, feature_category: :importers do + it_behaves_like Gitlab::Import::AdvanceStage, factory: :import_state end diff --git a/spec/workers/gitlab/jira_import/advance_stage_worker_spec.rb b/spec/workers/gitlab/jira_import/advance_stage_worker_spec.rb new file mode 100644 index 00000000000..d7c5d8aba4d --- /dev/null +++ b/spec/workers/gitlab/jira_import/advance_stage_worker_spec.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::JiraImport::AdvanceStageWorker, feature_category: :importers do + it_behaves_like Gitlab::Import::AdvanceStage, factory: :jira_import_state +end diff --git a/spec/workers/gitlab/jira_import/import_issue_worker_spec.rb b/spec/workers/gitlab/jira_import/import_issue_worker_spec.rb index 5209395923f..6dfab44b228 100644 --- a/spec/workers/gitlab/jira_import/import_issue_worker_spec.rb +++ b/spec/workers/gitlab/jira_import/import_issue_worker_spec.rb @@ -12,9 +12,9 @@ RSpec.describe Gitlab::JiraImport::ImportIssueWorker, feature_category: :importe 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) } + it { expect(described_class).to include_module(Gitlab::Import::NotifyUponDeath) } end subject { described_class.new } |