2019-07-07 11:18:12 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe RunPipelineScheduleWorker do
|
2018-03-17 18:26:18 +05:30
|
|
|
describe '#perform' do
|
2020-04-08 14:13:33 +05:30
|
|
|
let_it_be(:project) { create(:project) }
|
|
|
|
let_it_be(:user) { create(:user) }
|
|
|
|
let_it_be(:pipeline_schedule) { create(:ci_pipeline_schedule, :nightly, project: project ) }
|
2018-03-17 18:26:18 +05:30
|
|
|
let(:worker) { described_class.new }
|
|
|
|
|
|
|
|
context 'when a project not found' do
|
|
|
|
it 'does not call the Service' do
|
|
|
|
expect(Ci::CreatePipelineService).not_to receive(:new)
|
|
|
|
expect(worker).not_to receive(:run_pipeline_schedule)
|
|
|
|
|
|
|
|
worker.perform(100000, user.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when a user not found' do
|
|
|
|
it 'does not call the Service' do
|
|
|
|
expect(Ci::CreatePipelineService).not_to receive(:new)
|
|
|
|
expect(worker).not_to receive(:run_pipeline_schedule)
|
|
|
|
|
|
|
|
worker.perform(pipeline_schedule.id, 10000)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when everything is ok' do
|
|
|
|
let(:create_pipeline_service) { instance_double(Ci::CreatePipelineService) }
|
|
|
|
|
|
|
|
it 'calls the Service' do
|
|
|
|
expect(Ci::CreatePipelineService).to receive(:new).with(project, user, ref: pipeline_schedule.ref).and_return(create_pipeline_service)
|
2019-09-04 21:01:54 +05:30
|
|
|
expect(create_pipeline_service).to receive(:execute!).with(:schedule, ignore_skip_ci: true, save_on_errors: false, schedule: pipeline_schedule)
|
|
|
|
|
|
|
|
worker.perform(pipeline_schedule.id, user.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when database statement timeout happens' do
|
|
|
|
before do
|
|
|
|
allow(Ci::CreatePipelineService).to receive(:new) { raise ActiveRecord::StatementInvalid }
|
|
|
|
|
2020-01-01 13:55:28 +05:30
|
|
|
expect(Gitlab::ErrorTracking)
|
|
|
|
.to receive(:track_and_raise_for_dev_exception)
|
2019-09-04 21:01:54 +05:30
|
|
|
.with(ActiveRecord::StatementInvalid,
|
2019-12-04 20:38:33 +05:30
|
|
|
issue_url: 'https://gitlab.com/gitlab-org/gitlab-foss/issues/41231',
|
2020-01-01 13:55:28 +05:30
|
|
|
schedule_id: pipeline_schedule.id).once
|
2019-09-04 21:01:54 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'increments Prometheus counter' do
|
|
|
|
expect(Gitlab::Metrics)
|
|
|
|
.to receive(:counter)
|
|
|
|
.with(:pipeline_schedule_creation_failed_total, "Counter of failed attempts of pipeline schedule creation")
|
|
|
|
.and_call_original
|
|
|
|
|
|
|
|
worker.perform(pipeline_schedule.id, user.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'logging a pipeline error' do
|
|
|
|
expect(Rails.logger)
|
|
|
|
.to receive(:error)
|
|
|
|
.with(a_string_matching('ActiveRecord::StatementInvalid'))
|
|
|
|
.and_call_original
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
worker.perform(pipeline_schedule.id, user.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|