2019-03-30 03:15:48 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-05-07 18:35:56 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe PipelineScheduleWorker do
|
2019-04-27 23:27:08 -04:00
|
|
|
include ExclusiveLeaseHelpers
|
|
|
|
|
2017-05-07 18:35:56 -04:00
|
|
|
subject { described_class.new.perform }
|
|
|
|
|
|
|
|
set(:project) { create(:project, :repository) }
|
|
|
|
set(:user) { create(:user) }
|
|
|
|
|
|
|
|
let!(:pipeline_schedule) do
|
|
|
|
create(:ci_pipeline_schedule, :nightly, project: project, owner: user)
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
2018-12-04 10:14:24 -05:00
|
|
|
stub_application_setting(auto_devops_enabled: false)
|
2017-05-07 18:35:56 -04:00
|
|
|
stub_ci_pipeline_to_return_yaml_file
|
|
|
|
|
2017-05-10 04:04:25 -04:00
|
|
|
pipeline_schedule.update_column(:next_run_at, 1.day.ago)
|
|
|
|
end
|
2017-05-07 18:35:56 -04:00
|
|
|
|
2017-05-10 04:04:25 -04:00
|
|
|
context 'when the schedule is runnable by the user' do
|
2017-05-07 18:35:56 -04:00
|
|
|
before do
|
2018-07-11 10:36:08 -04:00
|
|
|
project.add_maintainer(user)
|
2017-05-07 18:35:56 -04:00
|
|
|
end
|
|
|
|
|
2017-05-10 04:04:25 -04:00
|
|
|
context 'when there is a scheduled pipeline within next_run_at' do
|
2017-11-20 04:29:59 -05:00
|
|
|
shared_examples 'successful scheduling' do
|
|
|
|
it 'creates a new pipeline' do
|
2018-12-05 09:39:15 -05:00
|
|
|
expect { subject }.to change { project.ci_pipelines.count }.by(1)
|
2017-11-22 12:55:10 -05:00
|
|
|
expect(Ci::Pipeline.last).to be_schedule
|
2017-11-20 04:29:59 -05:00
|
|
|
|
|
|
|
pipeline_schedule.reload
|
|
|
|
expect(pipeline_schedule.next_run_at).to be > Time.now
|
2018-12-05 09:39:15 -05:00
|
|
|
expect(pipeline_schedule).to eq(project.ci_pipelines.last.pipeline_schedule)
|
2017-11-20 04:29:59 -05:00
|
|
|
expect(pipeline_schedule).to be_active
|
|
|
|
end
|
2017-05-10 04:04:25 -04:00
|
|
|
end
|
2017-05-07 18:35:56 -04:00
|
|
|
|
2017-11-20 04:29:59 -05:00
|
|
|
it_behaves_like 'successful scheduling'
|
2017-05-10 04:04:25 -04:00
|
|
|
|
2019-04-27 23:27:08 -04:00
|
|
|
context 'when exclusive lease has already been taken by the other instance' do
|
|
|
|
before do
|
|
|
|
stub_exclusive_lease_taken(described_class::EXCLUSIVE_LOCK_KEY, timeout: described_class::LOCK_TIMEOUT)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises an error and does not start creating pipelines' do
|
|
|
|
expect { subject }.to raise_error(Gitlab::ExclusiveLeaseHelpers::FailedToObtainLockError)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-11-20 04:29:59 -05:00
|
|
|
context 'when the latest commit contains [ci skip]' do
|
|
|
|
before do
|
|
|
|
allow_any_instance_of(Ci::Pipeline)
|
|
|
|
.to receive(:git_commit_message)
|
|
|
|
.and_return('some commit [ci skip]')
|
|
|
|
end
|
2017-05-10 04:04:25 -04:00
|
|
|
|
2017-11-20 04:29:59 -05:00
|
|
|
it_behaves_like 'successful scheduling'
|
2017-05-10 04:04:25 -04:00
|
|
|
end
|
2017-05-07 18:35:56 -04:00
|
|
|
end
|
|
|
|
|
2017-11-20 04:29:59 -05:00
|
|
|
context 'when the schedule is deactivated' do
|
2017-05-10 04:04:25 -04:00
|
|
|
before do
|
|
|
|
pipeline_schedule.deactivate!
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not creates a new pipeline' do
|
2018-12-05 09:39:15 -05:00
|
|
|
expect { subject }.not_to change { project.ci_pipelines.count }
|
2017-05-10 04:04:25 -04:00
|
|
|
end
|
2017-05-07 18:35:56 -04:00
|
|
|
end
|
2018-11-30 02:32:30 -05:00
|
|
|
|
|
|
|
context 'when gitlab-ci.yml is corrupted' do
|
|
|
|
before do
|
|
|
|
stub_ci_pipeline_yaml_file(YAML.dump(rspec: { variables: 'rspec' } ))
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates a failed pipeline with the reason' do
|
2018-12-05 09:39:15 -05:00
|
|
|
expect { subject }.to change { project.ci_pipelines.count }.by(1)
|
2018-11-30 02:32:30 -05:00
|
|
|
expect(Ci::Pipeline.last).to be_config_error
|
|
|
|
expect(Ci::Pipeline.last.yaml_errors).not_to be_nil
|
|
|
|
end
|
|
|
|
end
|
2017-05-07 18:35:56 -04:00
|
|
|
end
|
|
|
|
|
2017-05-10 04:04:25 -04:00
|
|
|
context 'when the schedule is not runnable by the user' do
|
2018-11-30 02:32:30 -05:00
|
|
|
before do
|
|
|
|
expect(Gitlab::Sentry)
|
|
|
|
.to receive(:track_exception)
|
|
|
|
.with(Ci::CreatePipelineService::CreateError,
|
|
|
|
issue_url: 'https://gitlab.com/gitlab-org/gitlab-ce/issues/41231',
|
|
|
|
extra: { schedule_id: pipeline_schedule.id } ).once
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not deactivate the schedule' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(pipeline_schedule.reload.active).to be_truthy
|
|
|
|
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
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'logging a pipeline error' do
|
|
|
|
expect(Rails.logger)
|
|
|
|
.to receive(:error)
|
|
|
|
.with(a_string_matching("Insufficient permissions to create a new pipeline"))
|
|
|
|
.and_call_original
|
|
|
|
|
2017-05-10 04:04:25 -04:00
|
|
|
subject
|
2018-11-30 02:32:30 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not create a pipeline' do
|
2018-12-05 09:39:15 -05:00
|
|
|
expect { subject }.not_to change { project.ci_pipelines.count }
|
2018-11-30 02:32:30 -05:00
|
|
|
end
|
2017-05-10 04:04:25 -04:00
|
|
|
|
2018-11-30 02:32:30 -05:00
|
|
|
it 'does not raise an exception' do
|
|
|
|
expect { subject }.not_to raise_error
|
2017-05-07 18:35:56 -04:00
|
|
|
end
|
2018-11-30 02:32:30 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when .gitlab-ci.yml is missing in the project' do
|
|
|
|
before do
|
|
|
|
stub_ci_pipeline_yaml_file(nil)
|
|
|
|
project.add_maintainer(user)
|
2017-05-07 18:35:56 -04:00
|
|
|
|
2018-11-30 02:32:30 -05:00
|
|
|
expect(Gitlab::Sentry)
|
|
|
|
.to receive(:track_exception)
|
|
|
|
.with(Ci::CreatePipelineService::CreateError,
|
|
|
|
issue_url: 'https://gitlab.com/gitlab-org/gitlab-ce/issues/41231',
|
|
|
|
extra: { schedule_id: pipeline_schedule.id } ).once
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'logging a pipeline error' do
|
|
|
|
expect(Rails.logger)
|
|
|
|
.to receive(:error)
|
|
|
|
.with(a_string_matching("Missing .gitlab-ci.yml file"))
|
|
|
|
.and_call_original
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not create a pipeline' do
|
2018-12-05 09:39:15 -05:00
|
|
|
expect { subject }.not_to change { project.ci_pipelines.count }
|
2017-05-07 18:35:56 -04:00
|
|
|
end
|
2018-11-30 02:32:30 -05:00
|
|
|
|
|
|
|
it 'does not raise an exception' do
|
|
|
|
expect { subject }.not_to raise_error
|
|
|
|
end
|
2017-05-07 18:35:56 -04:00
|
|
|
end
|
|
|
|
end
|