diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-03-12 09:09:55 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-03-12 09:09:55 +0000 |
commit | 18f7828977b74bf6e5153594a098ef90e773b3b7 (patch) | |
tree | 49cb1e16d5341d773807ee583357ae6eb167d61f /spec/workers | |
parent | 8191b1571c017378eac33b3ed296ad5216d0a410 (diff) | |
download | gitlab-ce-18f7828977b74bf6e5153594a098ef90e773b3b7.tar.gz |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/workers')
-rw-r--r-- | spec/workers/concerns/project_export_options_spec.rb | 41 | ||||
-rw-r--r-- | spec/workers/project_export_worker_spec.rb | 46 | ||||
-rw-r--r-- | spec/workers/stuck_export_jobs_worker_spec.rb | 75 |
3 files changed, 158 insertions, 4 deletions
diff --git a/spec/workers/concerns/project_export_options_spec.rb b/spec/workers/concerns/project_export_options_spec.rb new file mode 100644 index 00000000000..985afaaf11e --- /dev/null +++ b/spec/workers/concerns/project_export_options_spec.rb @@ -0,0 +1,41 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe ProjectExportOptions do + let(:project) { create(:project) } + let(:project_export_job) { create(:project_export_job, project: project, jid: '123', status: 1) } + let(:job) { { 'args' => [project.owner.id, project.id, nil, nil], 'jid' => '123' } } + let(:worker_class) do + Class.new do + include Sidekiq::Worker + include ProjectExportOptions + end + end + + it 'sets default retry limit' do + expect(worker_class.sidekiq_options['retry']).to eq(ProjectExportOptions::EXPORT_RETRY_COUNT) + end + + it 'sets default status expiration' do + expect(worker_class.sidekiq_options['status_expiration']).to eq(StuckExportJobsWorker::EXPORT_JOBS_EXPIRATION) + end + + describe '.sidekiq_retries_exhausted' do + it 'marks status as failed' do + expect { worker_class.sidekiq_retries_exhausted_block.call(job) }.to change { project_export_job.reload.status }.from(1).to(3) + end + + context 'when status update fails' do + before do + project_export_job.update(status: 2) + end + + it 'logs an error' do + expect(Sidekiq.logger).to receive(:error).with("Failed to set Job #{job['jid']} for project #{project.id} to failed state") + + worker_class.sidekiq_retries_exhausted_block.call(job) + end + end + end +end diff --git a/spec/workers/project_export_worker_spec.rb b/spec/workers/project_export_worker_spec.rb index 8065087796c..d0d52e0df2d 100644 --- a/spec/workers/project_export_worker_spec.rb +++ b/spec/workers/project_export_worker_spec.rb @@ -9,21 +9,59 @@ describe ProjectExportWorker do subject { described_class.new } describe '#perform' do + before do + allow_next_instance_of(described_class) do |job| + allow(job).to receive(:jid).and_return(SecureRandom.hex(8)) + end + end + context 'when it succeeds' do it 'calls the ExportService' do expect_any_instance_of(::Projects::ImportExport::ExportService).to receive(:execute) subject.perform(user.id, project.id, { 'klass' => 'Gitlab::ImportExport::AfterExportStrategies::DownloadNotificationStrategy' }) end + + context 'export job' do + before do + allow_any_instance_of(::Projects::ImportExport::ExportService).to receive(:execute) + end + + it 'creates an export job record for the project' do + expect { subject.perform(user.id, project.id, {}) }.to change { project.export_jobs.count }.from(0).to(1) + end + + it 'sets the export job status to started' do + expect_next_instance_of(ProjectExportJob) do |job| + expect(job).to receive(:start) + end + + subject.perform(user.id, project.id, {}) + end + + it 'sets the export job status to finished' do + expect_next_instance_of(ProjectExportJob) do |job| + expect(job).to receive(:finish) + end + + subject.perform(user.id, project.id, {}) + end + end end context 'when it fails' do - it 'raises an exception when params are invalid' do + it 'does not raise an exception when strategy is invalid' do expect_any_instance_of(::Projects::ImportExport::ExportService).not_to receive(:execute) - expect { subject.perform(1234, project.id, {}) }.to raise_exception(ActiveRecord::RecordNotFound) - expect { subject.perform(user.id, 1234, {}) }.to raise_exception(ActiveRecord::RecordNotFound) - expect { subject.perform(user.id, project.id, { 'klass' => 'Whatever' }) }.to raise_exception(Gitlab::ImportExport::AfterExportStrategyBuilder::StrategyNotFoundError) + expect { subject.perform(user.id, project.id, { 'klass' => 'Whatever' }) }.not_to raise_error + end + + it 'does not raise error when project cannot be found' do + expect { subject.perform(user.id, -234, {}) }.not_to raise_error + end + + it 'does not raise error when user cannot be found' do + expect { subject.perform(-863, project.id, {}) }.not_to raise_error end end end diff --git a/spec/workers/stuck_export_jobs_worker_spec.rb b/spec/workers/stuck_export_jobs_worker_spec.rb new file mode 100644 index 00000000000..fc5758fdadf --- /dev/null +++ b/spec/workers/stuck_export_jobs_worker_spec.rb @@ -0,0 +1,75 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe StuckExportJobsWorker do + let(:worker) { described_class.new } + + shared_examples 'project export job detection' do + context 'when the job has completed' do + context 'when the export status was already updated' do + before do + allow(Gitlab::SidekiqStatus).to receive(:completed_jids) do + project_export_job.start + project_export_job.finish + + [project_export_job.jid] + end + end + + it 'does not mark the export as failed' do + worker.perform + + expect(project_export_job.reload.finished?).to be true + end + end + + context 'when the export status was not updated' do + before do + allow(Gitlab::SidekiqStatus).to receive(:completed_jids) do + project_export_job.start + + [project_export_job.jid] + end + end + + it 'marks the project as failed' do + worker.perform + + expect(project_export_job.reload.failed?).to be true + end + end + + context 'when the job is not in queue and db record in queued state' do + before do + allow(Gitlab::SidekiqStatus).to receive(:completed_jids).and_return([project_export_job.jid]) + end + + it 'marks the project as failed' do + expect(project_export_job.queued?).to be true + + worker.perform + + expect(project_export_job.reload.failed?).to be true + end + end + end + + context 'when the job is running in Sidekiq' do + before do + allow(Gitlab::SidekiqStatus).to receive(:completed_jids).and_return([]) + end + + it 'does not mark the project export as failed' do + expect { worker.perform }.not_to change { project_export_job.reload.status } + end + end + end + + describe 'with started export status' do + it_behaves_like 'project export job detection' do + let(:project) { create(:project) } + let!(:project_export_job) { create(:project_export_job, project: project, jid: '123') } + end + end +end |