2015-08-26 01:42:46 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-08-02 16:43:51 +00:00
|
|
|
describe Ci::Pipeline, :mailer do
|
2017-02-13 12:07:34 +00:00
|
|
|
let(:user) { create(:user) }
|
2017-08-23 08:45:46 +00:00
|
|
|
set(:project) { create(:project) }
|
2017-02-13 12:07:34 +00:00
|
|
|
|
|
|
|
let(:pipeline) do
|
|
|
|
create(:ci_empty_pipeline, status: :created, project: project)
|
|
|
|
end
|
2015-08-26 01:42:46 +00:00
|
|
|
|
2015-12-04 11:55:23 +00:00
|
|
|
it { is_expected.to belong_to(:project) }
|
2016-07-15 13:42:29 +00:00
|
|
|
it { is_expected.to belong_to(:user) }
|
2017-04-05 18:21:31 +00:00
|
|
|
it { is_expected.to belong_to(:auto_canceled_by) }
|
2017-05-07 22:35:56 +00:00
|
|
|
it { is_expected.to belong_to(:pipeline_schedule) }
|
2016-07-15 13:42:29 +00:00
|
|
|
|
2016-03-10 10:06:33 +00:00
|
|
|
it { is_expected.to have_many(:statuses) }
|
2015-10-06 10:01:16 +00:00
|
|
|
it { is_expected.to have_many(:trigger_requests) }
|
2017-07-26 09:31:09 +00:00
|
|
|
it { is_expected.to have_many(:variables) }
|
2015-09-10 13:52:52 +00:00
|
|
|
it { is_expected.to have_many(:builds) }
|
2017-04-05 18:21:31 +00:00
|
|
|
it { is_expected.to have_many(:auto_canceled_pipelines) }
|
2017-04-06 13:53:02 +00:00
|
|
|
it { is_expected.to have_many(:auto_canceled_jobs) }
|
2016-07-15 13:42:29 +00:00
|
|
|
|
2017-05-24 13:13:51 +00:00
|
|
|
it { is_expected.to validate_presence_of(:sha) }
|
|
|
|
it { is_expected.to validate_presence_of(:status) }
|
2015-08-26 01:42:46 +00:00
|
|
|
|
2015-09-10 13:52:52 +00:00
|
|
|
it { is_expected.to respond_to :git_author_name }
|
|
|
|
it { is_expected.to respond_to :git_author_email }
|
|
|
|
it { is_expected.to respond_to :short_sha }
|
2017-09-18 12:29:43 +00:00
|
|
|
it { is_expected.to delegate_method(:full_path).to(:project).with_prefix }
|
2015-08-26 01:42:46 +00:00
|
|
|
|
2017-05-24 13:13:51 +00:00
|
|
|
describe '#source' do
|
|
|
|
context 'when creating new pipeline' do
|
|
|
|
let(:pipeline) do
|
|
|
|
build(:ci_empty_pipeline, status: :created, project: project, source: nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "prevents from creating an object" do
|
|
|
|
expect(pipeline).not_to be_valid
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when updating existing pipeline' do
|
|
|
|
before do
|
|
|
|
pipeline.update_attribute(:source, nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "object is valid" do
|
|
|
|
expect(pipeline).to be_valid
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-03-06 12:01:10 +00:00
|
|
|
describe '#block' do
|
|
|
|
it 'changes pipeline status to manual' do
|
|
|
|
expect(pipeline.block).to be true
|
|
|
|
expect(pipeline.reload).to be_manual
|
|
|
|
expect(pipeline.reload).to be_blocked
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-11 22:12:31 +00:00
|
|
|
describe '#valid_commit_sha' do
|
2015-08-26 01:42:46 +00:00
|
|
|
context 'commit.sha can not start with 00000000' do
|
|
|
|
before do
|
2016-06-03 14:22:26 +00:00
|
|
|
pipeline.sha = '0' * 40
|
|
|
|
pipeline.valid_commit_sha
|
2015-08-26 01:42:46 +00:00
|
|
|
end
|
|
|
|
|
2016-06-03 14:22:26 +00:00
|
|
|
it('commit errors should not be empty') { expect(pipeline.errors).not_to be_empty }
|
2015-08-26 01:42:46 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-11 22:12:31 +00:00
|
|
|
describe '#short_sha' do
|
2016-06-03 14:22:26 +00:00
|
|
|
subject { pipeline.short_sha }
|
2015-08-26 01:42:46 +00:00
|
|
|
|
2015-09-10 13:52:52 +00:00
|
|
|
it 'has 8 items' do
|
|
|
|
expect(subject.size).to eq(8)
|
|
|
|
end
|
2016-06-03 14:22:26 +00:00
|
|
|
it { expect(pipeline.sha).to start_with(subject) }
|
2015-08-26 01:42:46 +00:00
|
|
|
end
|
|
|
|
|
2016-07-11 22:12:31 +00:00
|
|
|
describe '#retried' do
|
2016-06-03 14:22:26 +00:00
|
|
|
subject { pipeline.retried }
|
2015-10-06 10:01:16 +00:00
|
|
|
|
|
|
|
before do
|
2017-04-16 11:14:39 +00:00
|
|
|
@build1 = create(:ci_build, pipeline: pipeline, name: 'deploy', retried: true)
|
|
|
|
@build2 = create(:ci_build, pipeline: pipeline, name: 'deploy')
|
2015-10-06 10:01:16 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns old builds' do
|
2016-06-03 14:22:26 +00:00
|
|
|
is_expected.to contain_exactly(@build1)
|
2015-10-06 10:01:16 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-08-26 01:42:46 +00:00
|
|
|
describe "coverage" do
|
2017-08-02 19:55:11 +00:00
|
|
|
let(:project) { create(:project, build_coverage_regex: "/.*/") }
|
2017-04-16 11:14:39 +00:00
|
|
|
let(:pipeline) { create(:ci_empty_pipeline, project: project) }
|
2015-08-26 01:42:46 +00:00
|
|
|
|
|
|
|
it "calculates average when there are two builds with coverage" do
|
2017-04-16 11:14:39 +00:00
|
|
|
create(:ci_build, name: "rspec", coverage: 30, pipeline: pipeline)
|
|
|
|
create(:ci_build, name: "rubocop", coverage: 40, pipeline: pipeline)
|
2016-06-03 14:22:26 +00:00
|
|
|
expect(pipeline.coverage).to eq("35.00")
|
2015-08-26 01:42:46 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "calculates average when there are two builds with coverage and one with nil" do
|
2017-04-16 11:14:39 +00:00
|
|
|
create(:ci_build, name: "rspec", coverage: 30, pipeline: pipeline)
|
|
|
|
create(:ci_build, name: "rubocop", coverage: 40, pipeline: pipeline)
|
|
|
|
create(:ci_build, pipeline: pipeline)
|
2016-06-03 14:22:26 +00:00
|
|
|
expect(pipeline.coverage).to eq("35.00")
|
2015-08-26 01:42:46 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "calculates average when there are two builds with coverage and one is retried" do
|
2017-04-16 11:14:39 +00:00
|
|
|
create(:ci_build, name: "rspec", coverage: 30, pipeline: pipeline)
|
|
|
|
create(:ci_build, name: "rubocop", coverage: 30, pipeline: pipeline, retried: true)
|
|
|
|
create(:ci_build, name: "rubocop", coverage: 40, pipeline: pipeline)
|
2016-06-03 14:22:26 +00:00
|
|
|
expect(pipeline.coverage).to eq("35.00")
|
2015-08-26 01:42:46 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "calculates average when there is one build without coverage" do
|
2017-12-14 00:13:44 +00:00
|
|
|
FactoryBot.create(:ci_build, pipeline: pipeline)
|
2016-06-03 14:22:26 +00:00
|
|
|
expect(pipeline.coverage).to be_nil
|
2015-08-26 01:42:46 +00:00
|
|
|
end
|
|
|
|
end
|
2016-04-16 20:43:40 +00:00
|
|
|
|
|
|
|
describe '#retryable?' do
|
2016-06-03 14:22:26 +00:00
|
|
|
subject { pipeline.retryable? }
|
2016-04-16 20:43:40 +00:00
|
|
|
|
|
|
|
context 'no failed builds' do
|
|
|
|
before do
|
2016-10-17 07:45:39 +00:00
|
|
|
create_build('rspec', 'success')
|
2016-04-16 20:43:40 +00:00
|
|
|
end
|
|
|
|
|
2016-10-17 07:45:39 +00:00
|
|
|
it 'is not retryable' do
|
2016-04-16 20:43:40 +00:00
|
|
|
is_expected.to be_falsey
|
|
|
|
end
|
2016-10-17 07:45:39 +00:00
|
|
|
|
|
|
|
context 'one canceled job' do
|
|
|
|
before do
|
|
|
|
create_build('rubocop', 'canceled')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'is retryable' do
|
|
|
|
is_expected.to be_truthy
|
|
|
|
end
|
|
|
|
end
|
2016-04-16 20:43:40 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'with failed builds' do
|
|
|
|
before do
|
2016-10-17 07:45:39 +00:00
|
|
|
create_build('rspec', 'running')
|
|
|
|
create_build('rubocop', 'failed')
|
2016-04-16 20:43:40 +00:00
|
|
|
end
|
|
|
|
|
2016-10-17 07:45:39 +00:00
|
|
|
it 'is retryable' do
|
2016-04-16 20:43:40 +00:00
|
|
|
is_expected.to be_truthy
|
|
|
|
end
|
|
|
|
end
|
2016-10-17 07:45:39 +00:00
|
|
|
|
|
|
|
def create_build(name, status)
|
|
|
|
create(:ci_build, name: name, status: status, pipeline: pipeline)
|
|
|
|
end
|
2016-04-16 20:43:40 +00:00
|
|
|
end
|
|
|
|
|
2017-08-23 08:45:46 +00:00
|
|
|
describe '#predefined_variables' do
|
|
|
|
subject { pipeline.predefined_variables }
|
|
|
|
|
|
|
|
it { is_expected.to be_an(Array) }
|
|
|
|
|
|
|
|
it 'includes the defined keys' do
|
|
|
|
keys = subject.map { |v| v[:key] }
|
|
|
|
|
|
|
|
expect(keys).to include('CI_PIPELINE_ID', 'CI_CONFIG_PATH', 'CI_PIPELINE_SOURCE')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-06 13:32:56 +00:00
|
|
|
describe '#auto_canceled?' do
|
|
|
|
subject { pipeline.auto_canceled? }
|
|
|
|
|
|
|
|
context 'when it is canceled' do
|
|
|
|
before do
|
|
|
|
pipeline.cancel
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when there is auto_canceled_by' do
|
|
|
|
before do
|
|
|
|
pipeline.update(auto_canceled_by: create(:ci_empty_pipeline))
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'is auto canceled' do
|
|
|
|
is_expected.to be_truthy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when there is no auto_canceled_by' do
|
|
|
|
it 'is not auto canceled' do
|
|
|
|
is_expected.to be_falsey
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when it is retried and canceled manually' do
|
|
|
|
before do
|
|
|
|
pipeline.enqueue
|
|
|
|
pipeline.cancel
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'is not auto canceled' do
|
|
|
|
is_expected.to be_falsey
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-01-18 10:25:36 +00:00
|
|
|
describe 'pipeline stages' do
|
2016-04-16 20:43:40 +00:00
|
|
|
before do
|
2017-01-18 10:25:36 +00:00
|
|
|
create(:commit_status, pipeline: pipeline,
|
|
|
|
stage: 'build',
|
|
|
|
name: 'linux',
|
|
|
|
stage_idx: 0,
|
|
|
|
status: 'success')
|
|
|
|
|
|
|
|
create(:commit_status, pipeline: pipeline,
|
|
|
|
stage: 'build',
|
|
|
|
name: 'mac',
|
|
|
|
stage_idx: 0,
|
|
|
|
status: 'failed')
|
|
|
|
|
|
|
|
create(:commit_status, pipeline: pipeline,
|
|
|
|
stage: 'deploy',
|
|
|
|
name: 'staging',
|
|
|
|
stage_idx: 2,
|
|
|
|
status: 'running')
|
|
|
|
|
|
|
|
create(:commit_status, pipeline: pipeline,
|
|
|
|
stage: 'test',
|
|
|
|
name: 'rspec',
|
|
|
|
stage_idx: 1,
|
|
|
|
status: 'success')
|
|
|
|
end
|
|
|
|
|
2017-05-31 13:25:36 +00:00
|
|
|
describe '#stage_seeds' do
|
|
|
|
let(:pipeline) do
|
2017-09-28 10:34:00 +00:00
|
|
|
build(:ci_pipeline, config: { rspec: { script: 'rake' } })
|
2017-05-31 13:25:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns preseeded stage seeds object' do
|
2017-06-02 10:16:11 +00:00
|
|
|
expect(pipeline.stage_seeds).to all(be_a Gitlab::Ci::Stage::Seed)
|
|
|
|
expect(pipeline.stage_seeds.count).to eq 1
|
2017-05-31 13:25:36 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-28 10:34:00 +00:00
|
|
|
describe '#seeds_size' do
|
|
|
|
let(:pipeline) { build(:ci_pipeline_with_one_job) }
|
|
|
|
|
|
|
|
it 'returns number of jobs in stage seeds' do
|
|
|
|
expect(pipeline.seeds_size).to eq 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-01 09:55:18 +00:00
|
|
|
describe '#legacy_stages' do
|
|
|
|
subject { pipeline.legacy_stages }
|
2017-01-18 10:25:36 +00:00
|
|
|
|
|
|
|
context 'stages list' do
|
|
|
|
it 'returns ordered list of stages' do
|
|
|
|
expect(subject.map(&:name)).to eq(%w[build test deploy])
|
|
|
|
end
|
2016-12-05 16:52:50 +00:00
|
|
|
end
|
|
|
|
|
2017-01-18 10:25:36 +00:00
|
|
|
context 'stages with statuses' do
|
|
|
|
let(:statuses) do
|
|
|
|
subject.map { |stage| [stage.name, stage.status] }
|
|
|
|
end
|
2016-12-05 16:52:50 +00:00
|
|
|
|
2017-01-18 10:25:36 +00:00
|
|
|
it 'returns list of stages with correct statuses' do
|
2017-02-22 17:46:57 +00:00
|
|
|
expect(statuses).to eq([%w(build failed),
|
|
|
|
%w(test success),
|
|
|
|
%w(deploy running)])
|
2017-01-18 10:25:36 +00:00
|
|
|
end
|
2016-12-06 13:49:37 +00:00
|
|
|
|
2017-04-16 11:14:39 +00:00
|
|
|
context 'when commit status is retried' do
|
2017-01-18 10:25:36 +00:00
|
|
|
before do
|
|
|
|
create(:commit_status, pipeline: pipeline,
|
|
|
|
stage: 'build',
|
|
|
|
name: 'mac',
|
|
|
|
stage_idx: 0,
|
|
|
|
status: 'success')
|
2017-04-16 11:14:39 +00:00
|
|
|
|
|
|
|
pipeline.process!
|
2017-01-18 10:25:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'ignores the previous state' do
|
2017-02-22 17:46:57 +00:00
|
|
|
expect(statuses).to eq([%w(build success),
|
|
|
|
%w(test success),
|
|
|
|
%w(deploy running)])
|
2017-01-18 10:25:36 +00:00
|
|
|
end
|
2016-12-05 16:52:50 +00:00
|
|
|
end
|
|
|
|
end
|
2017-03-08 09:58:41 +00:00
|
|
|
|
|
|
|
context 'when there is a stage with warnings' do
|
|
|
|
before do
|
|
|
|
create(:commit_status, pipeline: pipeline,
|
|
|
|
stage: 'deploy',
|
|
|
|
name: 'prod:2',
|
|
|
|
stage_idx: 2,
|
|
|
|
status: 'failed',
|
|
|
|
allow_failure: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'populates stage with correct number of warnings' do
|
2017-06-01 09:55:18 +00:00
|
|
|
deploy_stage = pipeline.legacy_stages.third
|
2017-03-08 09:58:41 +00:00
|
|
|
|
|
|
|
expect(deploy_stage).not_to receive(:statuses)
|
|
|
|
expect(deploy_stage).to have_warnings
|
|
|
|
end
|
|
|
|
end
|
2017-01-18 10:25:36 +00:00
|
|
|
end
|
2016-12-05 16:52:50 +00:00
|
|
|
|
2017-01-18 10:25:36 +00:00
|
|
|
describe '#stages_count' do
|
|
|
|
it 'returns a valid number of stages' do
|
|
|
|
expect(pipeline.stages_count).to eq(3)
|
2016-12-05 16:52:50 +00:00
|
|
|
end
|
2017-01-18 10:25:36 +00:00
|
|
|
end
|
2016-12-05 16:52:50 +00:00
|
|
|
|
2017-06-01 09:55:18 +00:00
|
|
|
describe '#stages_names' do
|
2017-01-18 10:25:36 +00:00
|
|
|
it 'returns a valid names of stages' do
|
2017-06-01 09:55:18 +00:00
|
|
|
expect(pipeline.stages_names).to eq(%w(build test deploy))
|
2016-12-05 16:52:50 +00:00
|
|
|
end
|
2016-04-16 20:43:40 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-01 09:55:18 +00:00
|
|
|
describe '#legacy_stage' do
|
|
|
|
subject { pipeline.legacy_stage('test') }
|
2016-12-20 10:00:56 +00:00
|
|
|
|
|
|
|
context 'with status in stage' do
|
2016-12-20 10:26:24 +00:00
|
|
|
before do
|
|
|
|
create(:commit_status, pipeline: pipeline, stage: 'test')
|
2016-12-20 10:00:56 +00:00
|
|
|
end
|
2016-12-20 10:26:24 +00:00
|
|
|
|
2017-06-01 10:00:37 +00:00
|
|
|
it { expect(subject).to be_a Ci::LegacyStage }
|
2016-12-20 19:07:34 +00:00
|
|
|
it { expect(subject.name).to eq 'test' }
|
|
|
|
it { expect(subject.statuses).not_to be_empty }
|
2016-12-20 10:00:56 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'without status in stage' do
|
2016-12-20 10:26:24 +00:00
|
|
|
before do
|
|
|
|
create(:commit_status, pipeline: pipeline, stage: 'build')
|
|
|
|
end
|
2016-12-20 10:00:56 +00:00
|
|
|
|
|
|
|
it 'return stage object' do
|
|
|
|
is_expected.to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-12 09:53:27 +00:00
|
|
|
describe 'state machine' do
|
2016-08-11 20:54:25 +00:00
|
|
|
let(:current) { Time.now.change(usec: 0) }
|
2017-04-21 17:45:14 +00:00
|
|
|
let(:build) { create_build('build1', queued_at: 0) }
|
|
|
|
let(:build_b) { create_build('build2', queued_at: 0) }
|
|
|
|
let(:build_c) { create_build('build3', queued_at: 0) }
|
2016-04-16 20:43:40 +00:00
|
|
|
|
2016-08-12 09:53:27 +00:00
|
|
|
describe '#duration' do
|
2017-04-21 17:45:14 +00:00
|
|
|
context 'when multiple builds are finished' do
|
|
|
|
before do
|
|
|
|
travel_to(current + 30) do
|
|
|
|
build.run!
|
|
|
|
build.success!
|
|
|
|
build_b.run!
|
|
|
|
build_c.run!
|
|
|
|
end
|
2016-08-29 18:54:07 +00:00
|
|
|
|
2017-04-21 17:45:14 +00:00
|
|
|
travel_to(current + 40) do
|
|
|
|
build_b.drop!
|
|
|
|
end
|
|
|
|
|
|
|
|
travel_to(current + 70) do
|
|
|
|
build_c.success!
|
|
|
|
end
|
2016-08-19 08:57:25 +00:00
|
|
|
end
|
2016-08-15 13:32:36 +00:00
|
|
|
|
2017-04-21 17:45:14 +00:00
|
|
|
it 'matches sum of builds duration' do
|
|
|
|
pipeline.reload
|
|
|
|
|
|
|
|
expect(pipeline.duration).to eq(40)
|
2016-08-19 08:57:25 +00:00
|
|
|
end
|
2016-08-12 09:53:27 +00:00
|
|
|
end
|
|
|
|
|
2017-04-21 17:45:14 +00:00
|
|
|
context 'when pipeline becomes blocked' do
|
|
|
|
let!(:build) { create_build('build:1') }
|
2017-04-21 17:49:18 +00:00
|
|
|
let!(:action) { create_build('manual:action', :manual) }
|
2016-09-02 11:15:42 +00:00
|
|
|
|
2017-04-21 17:45:14 +00:00
|
|
|
before do
|
|
|
|
travel_to(current + 1.minute) do
|
|
|
|
build.run!
|
|
|
|
end
|
|
|
|
|
|
|
|
travel_to(current + 5.minutes) do
|
|
|
|
build.success!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'recalculates pipeline duration' do
|
|
|
|
pipeline.reload
|
|
|
|
|
|
|
|
expect(pipeline).to be_manual
|
|
|
|
expect(pipeline.duration).to eq 4.minutes
|
|
|
|
end
|
2016-08-12 09:53:27 +00:00
|
|
|
end
|
2016-04-16 20:43:40 +00:00
|
|
|
end
|
|
|
|
|
2016-08-12 09:53:27 +00:00
|
|
|
describe '#started_at' do
|
|
|
|
it 'updates on transitioning to running' do
|
|
|
|
build.run
|
2016-04-16 20:43:40 +00:00
|
|
|
|
2016-08-12 09:53:27 +00:00
|
|
|
expect(pipeline.reload.started_at).not_to be_nil
|
|
|
|
end
|
|
|
|
|
2016-08-12 11:57:58 +00:00
|
|
|
it 'does not update on transitioning to success' do
|
2016-08-12 09:53:27 +00:00
|
|
|
build.success
|
|
|
|
|
|
|
|
expect(pipeline.reload.started_at).to be_nil
|
2016-04-16 20:43:40 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-12 09:53:27 +00:00
|
|
|
describe '#finished_at' do
|
|
|
|
it 'updates on transitioning to success' do
|
|
|
|
build.success
|
2016-04-16 20:43:40 +00:00
|
|
|
|
2016-08-12 09:53:27 +00:00
|
|
|
expect(pipeline.reload.finished_at).not_to be_nil
|
2016-04-16 20:43:40 +00:00
|
|
|
end
|
|
|
|
|
2016-08-12 11:57:58 +00:00
|
|
|
it 'does not update on transitioning to running' do
|
2016-08-12 09:53:27 +00:00
|
|
|
build.run
|
|
|
|
|
|
|
|
expect(pipeline.reload.finished_at).to be_nil
|
2016-04-16 20:43:40 +00:00
|
|
|
end
|
|
|
|
end
|
2016-09-19 07:26:25 +00:00
|
|
|
|
2016-10-17 07:55:47 +00:00
|
|
|
describe 'merge request metrics' do
|
2017-01-26 22:44:58 +00:00
|
|
|
let(:project) { create(:project, :repository) }
|
2017-12-14 00:13:44 +00:00
|
|
|
let(:pipeline) { FactoryBot.create(:ci_empty_pipeline, status: 'created', project: project, ref: 'master', sha: project.repository.commit('master').id) }
|
2016-09-19 07:26:25 +00:00
|
|
|
let!(:merge_request) { create(:merge_request, source_project: project, source_branch: pipeline.ref) }
|
|
|
|
|
2016-10-17 07:55:47 +00:00
|
|
|
before do
|
|
|
|
expect(PipelineMetricsWorker).to receive(:perform_async).with(pipeline.id)
|
|
|
|
end
|
2016-09-19 07:26:25 +00:00
|
|
|
|
2016-10-17 07:55:47 +00:00
|
|
|
context 'when transitioning to running' do
|
|
|
|
it 'schedules metrics workers' do
|
|
|
|
pipeline.run
|
2016-09-19 07:26:25 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when transitioning to success' do
|
2016-10-17 07:55:47 +00:00
|
|
|
it 'schedules metrics workers' do
|
|
|
|
pipeline.succeed
|
2016-09-19 07:26:25 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-09-20 09:18:13 +00:00
|
|
|
|
2017-04-07 08:56:35 +00:00
|
|
|
describe 'pipeline caching' do
|
2017-04-18 19:26:56 +00:00
|
|
|
it 'performs ExpirePipelinesCacheWorker' do
|
|
|
|
expect(ExpirePipelineCacheWorker).to receive(:perform_async).with(pipeline.id)
|
2017-04-04 15:38:16 +00:00
|
|
|
|
|
|
|
pipeline.cancel
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-21 17:49:18 +00:00
|
|
|
def create_build(name, *traits, queued_at: current, started_from: 0, **opts)
|
|
|
|
create(:ci_build, *traits,
|
2016-08-29 18:54:07 +00:00
|
|
|
name: name,
|
|
|
|
pipeline: pipeline,
|
2016-09-02 11:15:42 +00:00
|
|
|
queued_at: queued_at,
|
2017-04-21 17:45:14 +00:00
|
|
|
started_at: queued_at + started_from,
|
|
|
|
**opts)
|
2016-08-29 18:54:07 +00:00
|
|
|
end
|
2016-04-16 20:43:40 +00:00
|
|
|
end
|
2016-04-18 11:35:43 +00:00
|
|
|
|
|
|
|
describe '#branch?' do
|
2016-06-03 14:22:26 +00:00
|
|
|
subject { pipeline.branch? }
|
2016-04-18 11:35:43 +00:00
|
|
|
|
|
|
|
context 'is not a tag' do
|
|
|
|
before do
|
2016-06-03 14:22:26 +00:00
|
|
|
pipeline.tag = false
|
2016-04-18 11:35:43 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'return true when tag is set to false' do
|
|
|
|
is_expected.to be_truthy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'is not a tag' do
|
|
|
|
before do
|
2016-06-03 14:22:26 +00:00
|
|
|
pipeline.tag = true
|
2016-04-18 11:35:43 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'return false when tag is set to true' do
|
|
|
|
is_expected.to be_falsey
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-07-16 23:48:51 +00:00
|
|
|
|
2016-08-10 10:40:10 +00:00
|
|
|
context 'with non-empty project' do
|
2017-01-26 22:44:58 +00:00
|
|
|
let(:project) { create(:project, :repository) }
|
2016-08-11 10:04:52 +00:00
|
|
|
|
|
|
|
let(:pipeline) do
|
|
|
|
create(:ci_pipeline,
|
|
|
|
project: project,
|
|
|
|
ref: project.default_branch,
|
|
|
|
sha: project.commit.sha)
|
|
|
|
end
|
2016-08-10 10:40:10 +00:00
|
|
|
|
|
|
|
describe '#latest?' do
|
|
|
|
context 'with latest sha' do
|
|
|
|
it 'returns true' do
|
|
|
|
expect(pipeline).to be_latest
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with not latest sha' do
|
|
|
|
before do
|
|
|
|
pipeline.update(
|
|
|
|
sha: project.commit("#{project.default_branch}~1").sha)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false' do
|
|
|
|
expect(pipeline).not_to be_latest
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-16 23:48:51 +00:00
|
|
|
describe '#manual_actions' do
|
|
|
|
subject { pipeline.manual_actions }
|
|
|
|
|
|
|
|
it 'when none defined' do
|
|
|
|
is_expected.to be_empty
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when action defined' do
|
|
|
|
let!(:manual) { create(:ci_build, :manual, pipeline: pipeline, name: 'deploy') }
|
|
|
|
|
|
|
|
it 'returns one action' do
|
|
|
|
is_expected.to contain_exactly(manual)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'there are multiple of the same name' do
|
|
|
|
let!(:manual2) { create(:ci_build, :manual, pipeline: pipeline, name: 'deploy') }
|
|
|
|
|
2017-04-16 11:14:39 +00:00
|
|
|
before do
|
|
|
|
manual.update(retried: true)
|
|
|
|
end
|
|
|
|
|
2016-07-16 23:48:51 +00:00
|
|
|
it 'returns latest one' do
|
|
|
|
is_expected.to contain_exactly(manual2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-07-12 17:05:48 +00:00
|
|
|
|
2017-09-01 10:58:22 +00:00
|
|
|
describe '#has_kubernetes_active?' do
|
2017-09-01 11:03:43 +00:00
|
|
|
context 'when kubernetes is active' do
|
2017-11-27 14:19:16 +00:00
|
|
|
shared_examples 'same behavior between KubernetesService and Platform::Kubernetes' do
|
2017-11-27 12:48:54 +00:00
|
|
|
it 'returns true' do
|
|
|
|
expect(pipeline).to have_kubernetes_active
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user configured kubernetes from Integration > Kubernetes' do
|
|
|
|
let(:project) { create(:kubernetes_project) }
|
|
|
|
|
2017-11-27 14:19:16 +00:00
|
|
|
it_behaves_like 'same behavior between KubernetesService and Platform::Kubernetes'
|
2017-11-27 12:48:54 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user configured kubernetes from CI/CD > Clusters' do
|
|
|
|
let!(:cluster) { create(:cluster, :project, :provided_by_gcp) }
|
|
|
|
let(:project) { cluster.project }
|
2017-08-26 10:45:36 +00:00
|
|
|
|
2017-11-27 14:19:16 +00:00
|
|
|
it_behaves_like 'same behavior between KubernetesService and Platform::Kubernetes'
|
2017-08-26 10:45:36 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-01 11:03:43 +00:00
|
|
|
context 'when kubernetes is not active' do
|
2017-08-26 10:45:36 +00:00
|
|
|
it 'returns false' do
|
2017-09-01 10:58:22 +00:00
|
|
|
expect(pipeline).not_to have_kubernetes_active
|
2017-08-26 10:45:36 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-05 13:23:09 +00:00
|
|
|
describe '#has_stage_seeds?' do
|
2017-06-02 10:16:11 +00:00
|
|
|
context 'when pipeline has stage seeds' do
|
2017-06-05 13:23:09 +00:00
|
|
|
subject { build(:ci_pipeline_with_one_job) }
|
2017-05-30 11:27:46 +00:00
|
|
|
|
2017-06-02 10:16:11 +00:00
|
|
|
it { is_expected.to have_stage_seeds }
|
2017-05-30 11:27:46 +00:00
|
|
|
end
|
|
|
|
|
2017-06-02 10:16:11 +00:00
|
|
|
context 'when pipeline does not have stage seeds' do
|
2017-05-30 11:27:46 +00:00
|
|
|
subject { create(:ci_pipeline_without_jobs) }
|
|
|
|
|
2017-06-02 10:16:11 +00:00
|
|
|
it { is_expected.not_to have_stage_seeds }
|
2017-05-30 11:27:46 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-14 14:58:05 +00:00
|
|
|
describe '#has_warnings?' do
|
|
|
|
subject { pipeline.has_warnings? }
|
2016-07-12 17:05:48 +00:00
|
|
|
|
|
|
|
context 'build which is allowed to fail fails' do
|
|
|
|
before do
|
2016-07-14 14:58:05 +00:00
|
|
|
create :ci_build, :success, pipeline: pipeline, name: 'rspec'
|
|
|
|
create :ci_build, :allowed_to_fail, :failed, pipeline: pipeline, name: 'rubocop'
|
2016-07-12 17:05:48 +00:00
|
|
|
end
|
2016-08-01 15:00:44 +00:00
|
|
|
|
2016-07-12 17:05:48 +00:00
|
|
|
it 'returns true' do
|
|
|
|
is_expected.to be_truthy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'build which is allowed to fail succeeds' do
|
|
|
|
before do
|
2016-07-14 14:58:05 +00:00
|
|
|
create :ci_build, :success, pipeline: pipeline, name: 'rspec'
|
|
|
|
create :ci_build, :allowed_to_fail, :success, pipeline: pipeline, name: 'rubocop'
|
2016-07-12 17:05:48 +00:00
|
|
|
end
|
2016-08-01 15:00:44 +00:00
|
|
|
|
2016-07-12 17:05:48 +00:00
|
|
|
it 'returns false' do
|
|
|
|
is_expected.to be_falsey
|
|
|
|
end
|
|
|
|
end
|
2016-07-14 14:58:05 +00:00
|
|
|
|
|
|
|
context 'build is retried and succeeds' do
|
|
|
|
before do
|
|
|
|
create :ci_build, :success, pipeline: pipeline, name: 'rubocop'
|
|
|
|
create :ci_build, :failed, pipeline: pipeline, name: 'rspec'
|
|
|
|
create :ci_build, :success, pipeline: pipeline, name: 'rspec'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false' do
|
|
|
|
is_expected.to be_falsey
|
|
|
|
end
|
|
|
|
end
|
2016-07-12 17:05:48 +00:00
|
|
|
end
|
2016-08-11 16:37:36 +00:00
|
|
|
|
2016-12-15 10:14:48 +00:00
|
|
|
shared_context 'with some outdated pipelines' do
|
2016-12-14 13:00:06 +00:00
|
|
|
before do
|
2017-11-10 19:57:11 +00:00
|
|
|
create_pipeline(:canceled, 'ref', 'A', project)
|
|
|
|
create_pipeline(:success, 'ref', 'A', project)
|
|
|
|
create_pipeline(:failed, 'ref', 'B', project)
|
|
|
|
create_pipeline(:skipped, 'feature', 'C', project)
|
2016-12-14 13:00:06 +00:00
|
|
|
end
|
|
|
|
|
2017-11-10 19:57:11 +00:00
|
|
|
def create_pipeline(status, ref, sha, project)
|
|
|
|
create(
|
|
|
|
:ci_empty_pipeline,
|
|
|
|
status: status,
|
|
|
|
ref: ref,
|
|
|
|
sha: sha,
|
|
|
|
project: project
|
|
|
|
)
|
2016-12-14 13:00:06 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-11-10 19:57:11 +00:00
|
|
|
describe '.newest_first' do
|
2016-12-15 10:14:48 +00:00
|
|
|
include_context 'with some outdated pipelines'
|
2016-12-14 13:00:06 +00:00
|
|
|
|
2017-11-10 19:57:11 +00:00
|
|
|
it 'returns the pipelines from new to old' do
|
|
|
|
expect(described_class.newest_first.pluck(:status))
|
|
|
|
.to eq(%w[skipped failed success canceled])
|
2016-12-14 13:00:06 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.latest_status' do
|
2016-12-15 10:14:48 +00:00
|
|
|
include_context 'with some outdated pipelines'
|
2016-12-14 13:00:06 +00:00
|
|
|
|
|
|
|
context 'when no ref is specified' do
|
2017-11-10 19:57:11 +00:00
|
|
|
it 'returns the status of the latest pipeline' do
|
|
|
|
expect(described_class.latest_status).to eq('skipped')
|
2016-12-14 13:00:06 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when ref is specified' do
|
2017-11-10 19:57:11 +00:00
|
|
|
it 'returns the status of the latest pipeline for the given ref' do
|
|
|
|
expect(described_class.latest_status('ref')).to eq('failed')
|
2016-12-14 13:00:06 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-26 06:33:08 +00:00
|
|
|
describe '.latest_successful_for' do
|
|
|
|
include_context 'with some outdated pipelines'
|
|
|
|
|
|
|
|
let!(:latest_successful_pipeline) do
|
2017-11-10 19:57:11 +00:00
|
|
|
create_pipeline(:success, 'ref', 'D', project)
|
2016-12-26 06:33:08 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the latest successful pipeline' do
|
2017-06-21 13:48:12 +00:00
|
|
|
expect(described_class.latest_successful_for('ref'))
|
|
|
|
.to eq(latest_successful_pipeline)
|
2016-12-26 06:33:08 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-03-13 16:00:07 +00:00
|
|
|
describe '.latest_successful_for_refs' do
|
|
|
|
include_context 'with some outdated pipelines'
|
|
|
|
|
2017-11-10 19:57:11 +00:00
|
|
|
let!(:latest_successful_pipeline1) do
|
|
|
|
create_pipeline(:success, 'ref1', 'D', project)
|
|
|
|
end
|
|
|
|
|
|
|
|
let!(:latest_successful_pipeline2) do
|
|
|
|
create_pipeline(:success, 'ref2', 'D', project)
|
|
|
|
end
|
2017-03-13 16:00:07 +00:00
|
|
|
|
|
|
|
it 'returns the latest successful pipeline for both refs' do
|
|
|
|
refs = %w(ref1 ref2 ref3)
|
|
|
|
|
|
|
|
expect(described_class.latest_successful_for_refs(refs)).to eq({ 'ref1' => latest_successful_pipeline1, 'ref2' => latest_successful_pipeline2 })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-11-10 19:57:11 +00:00
|
|
|
describe '.latest_status_per_commit' do
|
|
|
|
let(:project) { create(:project) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
pairs = [
|
|
|
|
%w[success ref1 123],
|
|
|
|
%w[manual master 123],
|
|
|
|
%w[failed ref 456]
|
|
|
|
]
|
|
|
|
|
|
|
|
pairs.each do |(status, ref, sha)|
|
|
|
|
create(
|
|
|
|
:ci_empty_pipeline,
|
|
|
|
status: status,
|
|
|
|
ref: ref,
|
|
|
|
sha: sha,
|
|
|
|
project: project
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'without a ref' do
|
|
|
|
it 'returns a Hash containing the latest status per commit for all refs' do
|
|
|
|
expect(described_class.latest_status_per_commit(%w[123 456]))
|
|
|
|
.to eq({ '123' => 'manual', '456' => 'failed' })
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'only includes the status of the given commit SHAs' do
|
|
|
|
expect(described_class.latest_status_per_commit(%w[123]))
|
|
|
|
.to eq({ '123' => 'manual' })
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when there are two pipelines for a ref and SHA' do
|
|
|
|
it 'returns the status of the latest pipeline' do
|
|
|
|
create(
|
|
|
|
:ci_empty_pipeline,
|
|
|
|
status: 'failed',
|
|
|
|
ref: 'master',
|
|
|
|
sha: '123',
|
|
|
|
project: project
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(described_class.latest_status_per_commit(%w[123]))
|
|
|
|
.to eq({ '123' => 'failed' })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a ref' do
|
|
|
|
it 'only includes the pipelines for the given ref' do
|
|
|
|
expect(described_class.latest_status_per_commit(%w[123 456], 'master'))
|
|
|
|
.to eq({ '123' => 'manual' })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-19 17:56:27 +00:00
|
|
|
describe '.internal_sources' do
|
|
|
|
subject { described_class.internal_sources }
|
|
|
|
|
|
|
|
it { is_expected.to be_an(Array) }
|
|
|
|
end
|
|
|
|
|
2016-08-11 16:37:36 +00:00
|
|
|
describe '#status' do
|
2017-02-09 10:11:42 +00:00
|
|
|
let(:build) do
|
|
|
|
create(:ci_build, :created, pipeline: pipeline, name: 'test')
|
|
|
|
end
|
2016-08-11 16:37:36 +00:00
|
|
|
|
|
|
|
subject { pipeline.reload.status }
|
|
|
|
|
|
|
|
context 'on queuing' do
|
2016-08-12 11:57:58 +00:00
|
|
|
before do
|
|
|
|
build.enqueue
|
|
|
|
end
|
2016-08-11 16:37:36 +00:00
|
|
|
|
|
|
|
it { is_expected.to eq('pending') }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'on run' do
|
|
|
|
before do
|
2016-08-12 11:57:58 +00:00
|
|
|
build.enqueue
|
2016-08-11 16:37:36 +00:00
|
|
|
build.run
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to eq('running') }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'on drop' do
|
|
|
|
before do
|
|
|
|
build.drop
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to eq('failed') }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'on success' do
|
|
|
|
before do
|
|
|
|
build.success
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to eq('success') }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'on cancel' do
|
|
|
|
before do
|
|
|
|
build.cancel
|
|
|
|
end
|
|
|
|
|
2017-02-09 10:11:42 +00:00
|
|
|
context 'when build is pending' do
|
|
|
|
let(:build) do
|
|
|
|
create(:ci_build, :pending, pipeline: pipeline)
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to eq('canceled') }
|
|
|
|
end
|
2016-08-11 16:37:36 +00:00
|
|
|
end
|
2016-08-12 11:57:58 +00:00
|
|
|
|
|
|
|
context 'on failure and build retry' do
|
|
|
|
before do
|
2017-07-26 09:44:49 +00:00
|
|
|
stub_not_protect_default_branch
|
|
|
|
|
2016-08-12 11:57:58 +00:00
|
|
|
build.drop
|
2017-07-18 14:32:34 +00:00
|
|
|
project.add_developer(user)
|
2017-02-13 12:07:34 +00:00
|
|
|
|
|
|
|
Ci::Build.retry(build, user)
|
2016-08-12 11:57:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# We are changing a state: created > failed > running
|
|
|
|
# Instead of: created > failed > pending
|
|
|
|
# Since the pipeline already run, so it should not be pending anymore
|
|
|
|
|
|
|
|
it { is_expected.to eq('running') }
|
|
|
|
end
|
2016-08-11 16:37:36 +00:00
|
|
|
end
|
2016-08-11 16:37:36 +00:00
|
|
|
|
2017-07-05 12:04:53 +00:00
|
|
|
describe '#ci_yaml_file_path' do
|
|
|
|
subject { pipeline.ci_yaml_file_path }
|
|
|
|
|
|
|
|
it 'returns the path from project' do
|
2017-07-05 12:11:01 +00:00
|
|
|
allow(pipeline.project).to receive(:ci_config_path) { 'custom/path' }
|
2017-07-05 12:04:53 +00:00
|
|
|
|
|
|
|
is_expected.to eq('custom/path')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns default when custom path is nil' do
|
2017-07-05 12:11:01 +00:00
|
|
|
allow(pipeline.project).to receive(:ci_config_path) { nil }
|
2017-07-05 12:04:53 +00:00
|
|
|
|
|
|
|
is_expected.to eq('.gitlab-ci.yml')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns default when custom path is empty' do
|
2017-07-05 12:11:01 +00:00
|
|
|
allow(pipeline.project).to receive(:ci_config_path) { '' }
|
2017-07-05 12:04:53 +00:00
|
|
|
|
|
|
|
is_expected.to eq('.gitlab-ci.yml')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-06 19:00:34 +00:00
|
|
|
describe '#set_config_source' do
|
2017-11-30 14:11:33 +00:00
|
|
|
context 'when pipelines does not contain needed data' do
|
|
|
|
it 'defines source to be unknown' do
|
|
|
|
pipeline.set_config_source
|
2017-06-28 10:29:14 +00:00
|
|
|
|
2017-11-30 14:11:33 +00:00
|
|
|
expect(pipeline).to be_unknown_source
|
2017-08-24 11:01:33 +00:00
|
|
|
end
|
2017-11-30 14:11:33 +00:00
|
|
|
end
|
2017-09-06 07:26:47 +00:00
|
|
|
|
2017-11-30 14:11:33 +00:00
|
|
|
context 'when pipeline contains all needed data' do
|
|
|
|
let(:pipeline) do
|
|
|
|
create(:ci_pipeline, project: project,
|
|
|
|
sha: '1234',
|
|
|
|
ref: 'master',
|
|
|
|
source: :push)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the repository has a config file' do
|
|
|
|
before do
|
|
|
|
allow(project.repository).to receive(:gitlab_ci_yml_for)
|
|
|
|
.and_return('config')
|
2017-09-06 19:00:34 +00:00
|
|
|
end
|
2017-08-24 11:01:33 +00:00
|
|
|
|
2017-11-30 14:11:33 +00:00
|
|
|
it 'defines source to be from repository' do
|
|
|
|
pipeline.set_config_source
|
2017-09-06 19:00:34 +00:00
|
|
|
|
2017-11-30 14:11:33 +00:00
|
|
|
expect(pipeline).to be_repository_source
|
|
|
|
end
|
2017-09-06 19:00:34 +00:00
|
|
|
|
2017-11-30 14:11:33 +00:00
|
|
|
context 'when loading an object' do
|
|
|
|
let(:new_pipeline) { Ci::Pipeline.find(pipeline.id) }
|
2017-09-06 19:00:34 +00:00
|
|
|
|
2017-11-30 14:11:33 +00:00
|
|
|
it 'does not redefine the source' do
|
|
|
|
# force to overwrite the source
|
|
|
|
pipeline.unknown_source!
|
2017-09-06 19:00:34 +00:00
|
|
|
|
2017-11-30 14:11:33 +00:00
|
|
|
expect(new_pipeline).to be_unknown_source
|
2017-09-06 19:00:34 +00:00
|
|
|
end
|
2017-08-31 11:47:29 +00:00
|
|
|
end
|
2017-11-30 14:11:33 +00:00
|
|
|
end
|
2017-08-24 11:01:33 +00:00
|
|
|
|
2017-11-30 14:11:33 +00:00
|
|
|
context 'when the repository does not have a config file' do
|
|
|
|
let(:implied_yml) { Gitlab::Template::GitlabCiYmlTemplate.find('Auto-DevOps').content }
|
2017-08-31 11:47:29 +00:00
|
|
|
|
2017-11-30 14:11:33 +00:00
|
|
|
context 'auto devops enabled' do
|
|
|
|
before do
|
|
|
|
stub_application_setting(auto_devops_enabled: true)
|
|
|
|
allow(project).to receive(:ci_config_path) { 'custom' }
|
|
|
|
end
|
2017-09-06 19:00:34 +00:00
|
|
|
|
2017-11-30 14:11:33 +00:00
|
|
|
it 'defines source to be auto devops' do
|
|
|
|
pipeline.set_config_source
|
2017-09-06 19:00:34 +00:00
|
|
|
|
2017-11-30 14:11:33 +00:00
|
|
|
expect(pipeline).to be_auto_devops_source
|
2017-09-06 19:00:34 +00:00
|
|
|
end
|
2017-08-31 11:47:29 +00:00
|
|
|
end
|
2017-09-06 07:26:47 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-08-31 11:47:29 +00:00
|
|
|
|
2017-09-06 07:26:47 +00:00
|
|
|
describe '#ci_yaml_file' do
|
|
|
|
let(:implied_yml) { Gitlab::Template::GitlabCiYmlTemplate.find('Auto-DevOps').content }
|
2017-08-31 11:47:29 +00:00
|
|
|
|
2017-09-06 07:26:47 +00:00
|
|
|
context 'the source is unknown' do
|
2017-09-06 16:57:07 +00:00
|
|
|
before do
|
|
|
|
pipeline.unknown_source!
|
|
|
|
end
|
2017-09-06 07:26:47 +00:00
|
|
|
|
|
|
|
it 'returns the configuration if found' do
|
|
|
|
allow(pipeline.project.repository).to receive(:gitlab_ci_yml_for)
|
|
|
|
.and_return('config')
|
|
|
|
|
|
|
|
expect(pipeline.ci_yaml_file).to be_a(String)
|
|
|
|
expect(pipeline.ci_yaml_file).not_to eq(implied_yml)
|
|
|
|
expect(pipeline.yaml_errors).to be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets yaml errors if not found' do
|
|
|
|
expect(pipeline.ci_yaml_file).to be_nil
|
|
|
|
expect(pipeline.yaml_errors)
|
|
|
|
.to start_with('Failed to load CI/CD config file')
|
2017-08-24 11:01:33 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-06 07:26:47 +00:00
|
|
|
context 'the source is the repository' do
|
2017-09-06 16:57:07 +00:00
|
|
|
before do
|
|
|
|
pipeline.repository_source!
|
|
|
|
end
|
2017-08-24 11:01:33 +00:00
|
|
|
|
2017-09-06 07:26:47 +00:00
|
|
|
it 'returns the configuration if found' do
|
|
|
|
allow(pipeline.project.repository).to receive(:gitlab_ci_yml_for)
|
|
|
|
.and_return('config')
|
|
|
|
|
|
|
|
expect(pipeline.ci_yaml_file).to be_a(String)
|
|
|
|
expect(pipeline.ci_yaml_file).not_to eq(implied_yml)
|
|
|
|
expect(pipeline.yaml_errors).to be_nil
|
2017-08-24 11:01:33 +00:00
|
|
|
end
|
2017-09-06 07:26:47 +00:00
|
|
|
end
|
2017-08-24 11:01:33 +00:00
|
|
|
|
2017-09-06 07:26:47 +00:00
|
|
|
context 'when the source is auto_devops_source' do
|
2017-09-06 16:57:07 +00:00
|
|
|
before do
|
2017-09-06 19:00:34 +00:00
|
|
|
stub_application_setting(auto_devops_enabled: true)
|
2017-09-06 16:57:07 +00:00
|
|
|
pipeline.auto_devops_source!
|
|
|
|
end
|
2017-08-24 11:01:33 +00:00
|
|
|
|
2017-09-06 07:26:47 +00:00
|
|
|
it 'finds the implied config' do
|
|
|
|
expect(pipeline.ci_yaml_file).to eq(implied_yml)
|
|
|
|
expect(pipeline.yaml_errors).to be_nil
|
2017-08-24 11:01:33 +00:00
|
|
|
end
|
2017-06-28 07:53:12 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-05 12:23:58 +00:00
|
|
|
describe '#detailed_status' do
|
2016-12-12 14:14:31 +00:00
|
|
|
subject { pipeline.detailed_status(user) }
|
|
|
|
|
2016-12-05 12:23:58 +00:00
|
|
|
context 'when pipeline is created' do
|
|
|
|
let(:pipeline) { create(:ci_pipeline, status: :created) }
|
|
|
|
|
|
|
|
it 'returns detailed status for created pipeline' do
|
2016-12-12 14:14:31 +00:00
|
|
|
expect(subject.text).to eq 'created'
|
2016-12-05 12:23:58 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when pipeline is pending' do
|
|
|
|
let(:pipeline) { create(:ci_pipeline, status: :pending) }
|
|
|
|
|
|
|
|
it 'returns detailed status for pending pipeline' do
|
2016-12-12 14:14:31 +00:00
|
|
|
expect(subject.text).to eq 'pending'
|
2016-12-05 12:23:58 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when pipeline is running' do
|
|
|
|
let(:pipeline) { create(:ci_pipeline, status: :running) }
|
|
|
|
|
|
|
|
it 'returns detailed status for running pipeline' do
|
2016-12-12 14:14:31 +00:00
|
|
|
expect(subject.text).to eq 'running'
|
2016-12-05 12:23:58 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when pipeline is successful' do
|
|
|
|
let(:pipeline) { create(:ci_pipeline, status: :success) }
|
|
|
|
|
|
|
|
it 'returns detailed status for successful pipeline' do
|
2016-12-12 14:14:31 +00:00
|
|
|
expect(subject.text).to eq 'passed'
|
2016-12-05 12:23:58 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when pipeline is failed' do
|
|
|
|
let(:pipeline) { create(:ci_pipeline, status: :failed) }
|
|
|
|
|
|
|
|
it 'returns detailed status for failed pipeline' do
|
2016-12-12 14:14:31 +00:00
|
|
|
expect(subject.text).to eq 'failed'
|
2016-12-05 12:23:58 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when pipeline is canceled' do
|
|
|
|
let(:pipeline) { create(:ci_pipeline, status: :canceled) }
|
|
|
|
|
|
|
|
it 'returns detailed status for canceled pipeline' do
|
2016-12-12 14:14:31 +00:00
|
|
|
expect(subject.text).to eq 'canceled'
|
2016-12-05 12:23:58 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when pipeline is skipped' do
|
|
|
|
let(:pipeline) { create(:ci_pipeline, status: :skipped) }
|
|
|
|
|
|
|
|
it 'returns detailed status for skipped pipeline' do
|
2016-12-12 14:14:31 +00:00
|
|
|
expect(subject.text).to eq 'skipped'
|
2016-12-05 12:23:58 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-03-06 13:08:55 +00:00
|
|
|
context 'when pipeline is blocked' do
|
|
|
|
let(:pipeline) { create(:ci_pipeline, status: :manual) }
|
|
|
|
|
|
|
|
it 'returns detailed status for blocked pipeline' do
|
2017-03-08 10:35:57 +00:00
|
|
|
expect(subject.text).to eq 'blocked'
|
2017-03-06 13:08:55 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-05 12:23:58 +00:00
|
|
|
context 'when pipeline is successful but with warnings' do
|
|
|
|
let(:pipeline) { create(:ci_pipeline, status: :success) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
create(:ci_build, :allowed_to_fail, :failed, pipeline: pipeline)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'retruns detailed status for successful pipeline with warnings' do
|
2016-12-12 14:14:31 +00:00
|
|
|
expect(subject.label).to eq 'passed with warnings'
|
2016-12-05 12:23:58 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-11-16 16:57:50 +00:00
|
|
|
describe '#cancelable?' do
|
2016-11-21 17:21:15 +00:00
|
|
|
%i[created running pending].each do |status0|
|
|
|
|
context "when there is a build #{status0}" do
|
2016-11-16 16:57:50 +00:00
|
|
|
before do
|
2016-11-21 17:21:15 +00:00
|
|
|
create(:ci_build, status0, pipeline: pipeline)
|
2016-11-16 16:57:50 +00:00
|
|
|
end
|
|
|
|
|
2016-11-21 09:59:57 +00:00
|
|
|
it 'is cancelable' do
|
|
|
|
expect(pipeline.cancelable?).to be_truthy
|
|
|
|
end
|
2016-11-16 16:57:50 +00:00
|
|
|
end
|
|
|
|
|
2016-11-21 17:21:15 +00:00
|
|
|
context "when there is an external job #{status0}" do
|
2016-11-16 16:57:50 +00:00
|
|
|
before do
|
2016-11-21 17:21:15 +00:00
|
|
|
create(:generic_commit_status, status0, pipeline: pipeline)
|
2016-11-16 16:57:50 +00:00
|
|
|
end
|
|
|
|
|
2016-11-21 09:59:57 +00:00
|
|
|
it 'is cancelable' do
|
|
|
|
expect(pipeline.cancelable?).to be_truthy
|
|
|
|
end
|
2016-11-16 16:57:50 +00:00
|
|
|
end
|
2016-11-18 15:27:06 +00:00
|
|
|
|
2016-11-21 17:21:15 +00:00
|
|
|
%i[success failed canceled].each do |status1|
|
2016-11-22 11:07:12 +00:00
|
|
|
context "when there are generic_commit_status jobs for #{status0} and #{status1}" do
|
|
|
|
before do
|
|
|
|
create(:generic_commit_status, status0, pipeline: pipeline)
|
|
|
|
create(:generic_commit_status, status1, pipeline: pipeline)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'is cancelable' do
|
|
|
|
expect(pipeline.cancelable?).to be_truthy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when there are generic_commit_status and ci_build jobs for #{status0} and #{status1}" do
|
|
|
|
before do
|
|
|
|
create(:generic_commit_status, status0, pipeline: pipeline)
|
|
|
|
create(:ci_build, status1, pipeline: pipeline)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'is cancelable' do
|
|
|
|
expect(pipeline.cancelable?).to be_truthy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when there are ci_build jobs for #{status0} and #{status1}" do
|
|
|
|
before do
|
|
|
|
create(:ci_build, status0, pipeline: pipeline)
|
|
|
|
create(:ci_build, status1, pipeline: pipeline)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'is cancelable' do
|
|
|
|
expect(pipeline.cancelable?).to be_truthy
|
2016-11-21 09:59:57 +00:00
|
|
|
end
|
2016-11-18 15:27:06 +00:00
|
|
|
end
|
|
|
|
end
|
2016-11-16 16:57:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
%i[success failed canceled].each do |status|
|
|
|
|
context "when there is a build #{status}" do
|
|
|
|
before do
|
|
|
|
create(:ci_build, status, pipeline: pipeline)
|
|
|
|
end
|
|
|
|
|
2016-11-21 09:59:57 +00:00
|
|
|
it 'is not cancelable' do
|
|
|
|
expect(pipeline.cancelable?).to be_falsey
|
|
|
|
end
|
2016-11-16 16:57:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context "when there is an external job #{status}" do
|
|
|
|
before do
|
|
|
|
create(:generic_commit_status, status, pipeline: pipeline)
|
|
|
|
end
|
|
|
|
|
2016-11-21 09:59:57 +00:00
|
|
|
it 'is not cancelable' do
|
|
|
|
expect(pipeline.cancelable?).to be_falsey
|
|
|
|
end
|
2016-11-16 16:57:50 +00:00
|
|
|
end
|
|
|
|
end
|
2017-05-19 18:42:05 +00:00
|
|
|
|
|
|
|
context 'when there is a manual action present in the pipeline' do
|
|
|
|
before do
|
|
|
|
create(:ci_build, :manual, pipeline: pipeline)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'is not cancelable' do
|
|
|
|
expect(pipeline).not_to be_cancelable
|
|
|
|
end
|
|
|
|
end
|
2016-11-16 16:57:50 +00:00
|
|
|
end
|
|
|
|
|
2016-11-18 17:17:40 +00:00
|
|
|
describe '#cancel_running' do
|
2016-11-19 13:04:11 +00:00
|
|
|
let(:latest_status) { pipeline.statuses.pluck(:status) }
|
|
|
|
|
2017-02-09 13:14:25 +00:00
|
|
|
context 'when there is a running external job and a regular job' do
|
2016-11-18 17:17:40 +00:00
|
|
|
before do
|
2016-11-19 13:04:11 +00:00
|
|
|
create(:ci_build, :running, pipeline: pipeline)
|
2016-11-18 17:17:40 +00:00
|
|
|
create(:generic_commit_status, :running, pipeline: pipeline)
|
|
|
|
|
|
|
|
pipeline.cancel_running
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'cancels both jobs' do
|
2016-11-19 13:04:11 +00:00
|
|
|
expect(latest_status).to contain_exactly('canceled', 'canceled')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-02-09 13:14:25 +00:00
|
|
|
context 'when jobs are in different stages' do
|
2016-11-19 13:04:11 +00:00
|
|
|
before do
|
|
|
|
create(:ci_build, :running, stage_idx: 0, pipeline: pipeline)
|
|
|
|
create(:ci_build, :running, stage_idx: 1, pipeline: pipeline)
|
|
|
|
|
|
|
|
pipeline.cancel_running
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'cancels both jobs' do
|
|
|
|
expect(latest_status).to contain_exactly('canceled', 'canceled')
|
|
|
|
end
|
|
|
|
end
|
2017-02-09 13:14:25 +00:00
|
|
|
|
|
|
|
context 'when there are created builds present in the pipeline' do
|
|
|
|
before do
|
|
|
|
create(:ci_build, :running, stage_idx: 0, pipeline: pipeline)
|
|
|
|
create(:ci_build, :created, stage_idx: 1, pipeline: pipeline)
|
|
|
|
|
|
|
|
pipeline.cancel_running
|
|
|
|
end
|
|
|
|
|
2017-02-14 12:29:58 +00:00
|
|
|
it 'cancels created builds' do
|
2017-02-22 17:46:57 +00:00
|
|
|
expect(latest_status).to eq %w(canceled canceled)
|
2017-02-09 13:14:25 +00:00
|
|
|
end
|
|
|
|
end
|
2016-11-19 13:04:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#retry_failed' do
|
|
|
|
let(:latest_status) { pipeline.statuses.latest.pluck(:status) }
|
|
|
|
|
2017-02-13 12:07:34 +00:00
|
|
|
before do
|
2017-07-26 09:44:49 +00:00
|
|
|
stub_not_protect_default_branch
|
|
|
|
|
2017-07-18 14:32:34 +00:00
|
|
|
project.add_developer(user)
|
2017-02-13 12:07:34 +00:00
|
|
|
end
|
|
|
|
|
2016-11-19 13:04:11 +00:00
|
|
|
context 'when there is a failed build and failed external status' do
|
|
|
|
before do
|
|
|
|
create(:ci_build, :failed, name: 'build', pipeline: pipeline)
|
|
|
|
create(:generic_commit_status, :failed, name: 'jenkins', pipeline: pipeline)
|
|
|
|
|
2017-02-13 12:07:34 +00:00
|
|
|
pipeline.retry_failed(user)
|
2016-11-19 13:04:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'retries only build' do
|
|
|
|
expect(latest_status).to contain_exactly('pending', 'failed')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when builds are in different stages' do
|
|
|
|
before do
|
|
|
|
create(:ci_build, :failed, name: 'build', stage_idx: 0, pipeline: pipeline)
|
|
|
|
create(:ci_build, :failed, name: 'jenkins', stage_idx: 1, pipeline: pipeline)
|
|
|
|
|
2017-02-13 12:07:34 +00:00
|
|
|
pipeline.retry_failed(user)
|
2016-11-19 13:04:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'retries both builds' do
|
2017-02-14 12:29:58 +00:00
|
|
|
expect(latest_status).to contain_exactly('pending', 'created')
|
2016-11-19 13:04:11 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when there are canceled and failed' do
|
|
|
|
before do
|
|
|
|
create(:ci_build, :failed, name: 'build', stage_idx: 0, pipeline: pipeline)
|
|
|
|
create(:ci_build, :canceled, name: 'jenkins', stage_idx: 1, pipeline: pipeline)
|
|
|
|
|
2017-02-13 12:07:34 +00:00
|
|
|
pipeline.retry_failed(user)
|
2016-11-19 13:04:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'retries both builds' do
|
2017-02-14 12:29:58 +00:00
|
|
|
expect(latest_status).to contain_exactly('pending', 'created')
|
2016-11-18 17:17:40 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-02 10:06:31 +00:00
|
|
|
describe '#execute_hooks' do
|
2016-09-13 08:14:20 +00:00
|
|
|
let!(:build_a) { create_build('a', 0) }
|
2017-12-06 12:24:11 +00:00
|
|
|
let!(:build_b) { create_build('b', 0) }
|
2016-08-11 16:37:50 +00:00
|
|
|
|
2016-08-02 10:06:31 +00:00
|
|
|
let!(:hook) do
|
|
|
|
create(:project_hook, project: project, pipeline_events: enabled)
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
2017-04-27 10:08:57 +00:00
|
|
|
WebHookWorker.drain
|
2016-08-02 10:06:31 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'with pipeline hooks enabled' do
|
|
|
|
let(:enabled) { true }
|
|
|
|
|
2016-08-11 16:37:50 +00:00
|
|
|
before do
|
|
|
|
WebMock.stub_request(:post, hook.url)
|
|
|
|
end
|
|
|
|
|
2016-08-03 15:39:14 +00:00
|
|
|
context 'with multiple builds' do
|
2016-08-11 16:37:50 +00:00
|
|
|
context 'when build is queued' do
|
|
|
|
before do
|
2016-08-12 15:34:43 +00:00
|
|
|
build_a.enqueue
|
|
|
|
build_b.enqueue
|
2016-08-11 16:37:50 +00:00
|
|
|
end
|
2016-08-03 15:39:14 +00:00
|
|
|
|
2016-09-13 08:22:02 +00:00
|
|
|
it 'receives a pending event once' do
|
2016-08-12 07:29:49 +00:00
|
|
|
expect(WebMock).to have_requested_pipeline_hook('pending').once
|
2016-08-11 16:37:50 +00:00
|
|
|
end
|
|
|
|
end
|
2016-08-11 15:23:35 +00:00
|
|
|
|
2016-08-11 16:37:50 +00:00
|
|
|
context 'when build is run' do
|
|
|
|
before do
|
2016-08-12 15:34:43 +00:00
|
|
|
build_a.enqueue
|
2016-08-11 16:37:50 +00:00
|
|
|
build_a.run
|
2016-08-12 15:34:43 +00:00
|
|
|
build_b.enqueue
|
2016-08-11 16:37:50 +00:00
|
|
|
build_b.run
|
|
|
|
end
|
2016-08-11 15:23:35 +00:00
|
|
|
|
2016-09-13 08:22:02 +00:00
|
|
|
it 'receives a running event once' do
|
2016-08-12 07:29:49 +00:00
|
|
|
expect(WebMock).to have_requested_pipeline_hook('running').once
|
2016-08-11 16:37:50 +00:00
|
|
|
end
|
2016-08-03 15:39:14 +00:00
|
|
|
end
|
|
|
|
|
2016-08-11 16:37:50 +00:00
|
|
|
context 'when all builds succeed' do
|
|
|
|
before do
|
|
|
|
build_a.success
|
2016-10-26 09:34:40 +00:00
|
|
|
|
|
|
|
# We have to reload build_b as this is in next stage and it gets triggered by PipelineProcessWorker
|
|
|
|
build_b.reload.success
|
2016-08-11 16:37:50 +00:00
|
|
|
end
|
|
|
|
|
2016-09-13 08:22:02 +00:00
|
|
|
it 'receives a success event once' do
|
2016-08-12 07:29:49 +00:00
|
|
|
expect(WebMock).to have_requested_pipeline_hook('success').once
|
2016-08-11 16:37:50 +00:00
|
|
|
end
|
2016-08-04 16:44:17 +00:00
|
|
|
end
|
|
|
|
|
2016-09-13 08:14:20 +00:00
|
|
|
context 'when stage one failed' do
|
2017-12-06 12:24:11 +00:00
|
|
|
let!(:build_b) { create_build('b', 1) }
|
|
|
|
|
2016-09-13 08:14:20 +00:00
|
|
|
before do
|
|
|
|
build_a.drop
|
|
|
|
end
|
|
|
|
|
2016-09-13 08:22:02 +00:00
|
|
|
it 'receives a failed event once' do
|
2016-09-13 08:14:20 +00:00
|
|
|
expect(WebMock).to have_requested_pipeline_hook('failed').once
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-12 07:29:49 +00:00
|
|
|
def have_requested_pipeline_hook(status)
|
2016-08-11 16:37:50 +00:00
|
|
|
have_requested(:post, hook.url).with do |req|
|
2016-08-11 15:23:35 +00:00
|
|
|
json_body = JSON.parse(req.body)
|
|
|
|
json_body['object_attributes']['status'] == status &&
|
|
|
|
json_body['builds'].length == 2
|
|
|
|
end
|
2016-08-04 16:44:17 +00:00
|
|
|
end
|
2016-08-03 15:39:14 +00:00
|
|
|
end
|
2016-08-02 10:06:31 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'with pipeline hooks disabled' do
|
|
|
|
let(:enabled) { false }
|
|
|
|
|
2016-08-11 16:37:50 +00:00
|
|
|
before do
|
2016-08-12 15:34:43 +00:00
|
|
|
build_a.enqueue
|
|
|
|
build_b.enqueue
|
2016-08-11 16:37:50 +00:00
|
|
|
end
|
|
|
|
|
2016-08-02 10:06:31 +00:00
|
|
|
it 'did not execute pipeline_hook after touched' do
|
|
|
|
expect(WebMock).not_to have_requested(:post, hook.url)
|
|
|
|
end
|
|
|
|
end
|
2016-08-11 16:37:50 +00:00
|
|
|
|
2016-09-13 08:14:20 +00:00
|
|
|
def create_build(name, stage_idx)
|
|
|
|
create(:ci_build,
|
|
|
|
:created,
|
|
|
|
pipeline: pipeline,
|
|
|
|
name: name,
|
|
|
|
stage_idx: stage_idx)
|
2016-08-11 16:37:50 +00:00
|
|
|
end
|
2016-08-02 10:06:31 +00:00
|
|
|
end
|
2016-09-19 07:26:25 +00:00
|
|
|
|
|
|
|
describe "#merge_requests" do
|
2017-08-02 19:55:11 +00:00
|
|
|
let(:project) { create(:project) }
|
2017-04-21 15:39:19 +00:00
|
|
|
let(:pipeline) { create(:ci_empty_pipeline, status: 'created', project: project, ref: 'master', sha: 'a288a022a53a5a944fae87bcec6efc87b7061808') }
|
2016-09-19 07:26:25 +00:00
|
|
|
|
|
|
|
it "returns merge requests whose `diff_head_sha` matches the pipeline's SHA" do
|
2017-04-21 15:39:19 +00:00
|
|
|
allow_any_instance_of(MergeRequest).to receive(:diff_head_sha) { 'a288a022a53a5a944fae87bcec6efc87b7061808' }
|
2017-03-28 20:04:14 +00:00
|
|
|
merge_request = create(:merge_request, source_project: project, head_pipeline: pipeline, source_branch: pipeline.ref)
|
2016-09-19 07:26:25 +00:00
|
|
|
|
|
|
|
expect(pipeline.merge_requests).to eq([merge_request])
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't return merge requests whose source branch doesn't match the pipeline's ref" do
|
|
|
|
create(:merge_request, source_project: project, source_branch: 'feature', target_branch: 'master')
|
|
|
|
|
|
|
|
expect(pipeline.merge_requests).to be_empty
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't return merge requests whose `diff_head_sha` doesn't match the pipeline's SHA" do
|
|
|
|
create(:merge_request, source_project: project, source_branch: pipeline.ref)
|
|
|
|
allow_any_instance_of(MergeRequest).to receive(:diff_head_sha) { '97de212e80737a608d939f648d959671fb0a0142b' }
|
|
|
|
|
|
|
|
expect(pipeline.merge_requests).to be_empty
|
|
|
|
end
|
|
|
|
end
|
2016-09-14 13:57:04 +00:00
|
|
|
|
2017-04-20 06:44:01 +00:00
|
|
|
describe "#all_merge_requests" do
|
2017-08-02 19:55:11 +00:00
|
|
|
let(:project) { create(:project) }
|
2017-04-24 08:18:37 +00:00
|
|
|
let(:pipeline) { create(:ci_empty_pipeline, status: 'created', project: project, ref: 'master') }
|
2017-04-20 06:44:01 +00:00
|
|
|
|
2017-04-24 08:18:37 +00:00
|
|
|
it "returns all merge requests having the same source branch" do
|
2017-04-20 06:44:01 +00:00
|
|
|
merge_request = create(:merge_request, source_project: project, source_branch: pipeline.ref)
|
|
|
|
|
|
|
|
expect(pipeline.all_merge_requests).to eq([merge_request])
|
|
|
|
end
|
|
|
|
|
2017-04-24 08:18:37 +00:00
|
|
|
it "doesn't return merge requests having a different source branch" do
|
2017-04-20 06:44:01 +00:00
|
|
|
create(:merge_request, source_project: project, source_branch: 'feature', target_branch: 'master')
|
|
|
|
|
|
|
|
expect(pipeline.all_merge_requests).to be_empty
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-27 12:15:14 +00:00
|
|
|
describe '#stuck?' do
|
|
|
|
before do
|
|
|
|
create(:ci_build, :pending, pipeline: pipeline)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when pipeline is stuck' do
|
|
|
|
it 'is stuck' do
|
|
|
|
expect(pipeline).to be_stuck
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when pipeline is not stuck' do
|
2017-06-14 18:18:56 +00:00
|
|
|
before do
|
|
|
|
create(:ci_runner, :shared, :online)
|
|
|
|
end
|
2016-12-27 12:15:14 +00:00
|
|
|
|
|
|
|
it 'is not stuck' do
|
|
|
|
expect(pipeline).not_to be_stuck
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#has_yaml_errors?' do
|
|
|
|
context 'when pipeline has errors' do
|
|
|
|
let(:pipeline) do
|
|
|
|
create(:ci_pipeline, config: { rspec: nil })
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'contains yaml errors' do
|
|
|
|
expect(pipeline).to have_yaml_errors
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when pipeline does not have errors' do
|
|
|
|
let(:pipeline) do
|
|
|
|
create(:ci_pipeline, config: { rspec: { script: 'rake test' } })
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not containyaml errors' do
|
|
|
|
expect(pipeline).not_to have_yaml_errors
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-14 13:57:04 +00:00
|
|
|
describe 'notifications when pipeline success or failed' do
|
2017-01-26 22:44:58 +00:00
|
|
|
let(:project) { create(:project, :repository) }
|
2016-09-26 11:04:02 +00:00
|
|
|
|
|
|
|
let(:pipeline) do
|
|
|
|
create(:ci_pipeline,
|
|
|
|
project: project,
|
|
|
|
sha: project.commit('master').sha,
|
|
|
|
user: create(:user))
|
|
|
|
end
|
|
|
|
|
2016-09-14 13:57:04 +00:00
|
|
|
before do
|
2017-12-22 08:18:28 +00:00
|
|
|
project.add_developer(pipeline.user)
|
2016-10-21 17:10:47 +00:00
|
|
|
|
2017-06-21 13:48:12 +00:00
|
|
|
pipeline.user.global_notification_setting
|
|
|
|
.update(level: 'custom', failed_pipeline: true, success_pipeline: true)
|
2017-03-30 11:29:52 +00:00
|
|
|
|
2016-09-28 09:22:06 +00:00
|
|
|
perform_enqueued_jobs do
|
|
|
|
pipeline.enqueue
|
|
|
|
pipeline.run
|
|
|
|
end
|
2016-09-14 13:57:04 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples 'sending a notification' do
|
|
|
|
it 'sends an email' do
|
2016-10-18 12:02:35 +00:00
|
|
|
should_only_email(pipeline.user, kind: :bcc)
|
2016-09-14 13:57:04 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples 'not sending any notification' do
|
|
|
|
it 'does not send any email' do
|
2016-10-17 10:03:53 +00:00
|
|
|
should_not_email_anyone
|
2016-09-14 13:57:04 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with success pipeline' do
|
|
|
|
before do
|
|
|
|
perform_enqueued_jobs do
|
2016-09-14 14:12:31 +00:00
|
|
|
pipeline.succeed
|
2016-09-14 13:57:04 +00:00
|
|
|
end
|
|
|
|
end
|
2016-09-14 14:12:31 +00:00
|
|
|
|
|
|
|
it_behaves_like 'sending a notification'
|
2016-09-14 13:57:04 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'with failed pipeline' do
|
|
|
|
before do
|
|
|
|
perform_enqueued_jobs do
|
2016-11-22 05:41:39 +00:00
|
|
|
create(:ci_build, :failed, pipeline: pipeline)
|
|
|
|
create(:generic_commit_status, :failed, pipeline: pipeline)
|
2016-11-21 12:29:34 +00:00
|
|
|
|
2016-11-22 05:41:39 +00:00
|
|
|
pipeline.drop
|
2016-09-14 13:57:04 +00:00
|
|
|
end
|
|
|
|
end
|
2016-09-14 14:12:31 +00:00
|
|
|
|
|
|
|
it_behaves_like 'sending a notification'
|
2016-09-14 13:57:04 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'with skipped pipeline' do
|
|
|
|
before do
|
|
|
|
perform_enqueued_jobs do
|
|
|
|
pipeline.skip
|
|
|
|
end
|
|
|
|
end
|
2016-09-14 14:12:31 +00:00
|
|
|
|
|
|
|
it_behaves_like 'not sending any notification'
|
2016-09-14 13:57:04 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'with cancelled pipeline' do
|
|
|
|
before do
|
|
|
|
perform_enqueued_jobs do
|
|
|
|
pipeline.cancel
|
|
|
|
end
|
|
|
|
end
|
2016-09-14 14:12:31 +00:00
|
|
|
|
|
|
|
it_behaves_like 'not sending any notification'
|
2016-09-14 13:57:04 +00:00
|
|
|
end
|
|
|
|
end
|
2017-09-12 16:13:07 +00:00
|
|
|
|
|
|
|
describe '#latest_builds_with_artifacts' do
|
|
|
|
let!(:pipeline) { create(:ci_pipeline, :success) }
|
|
|
|
|
|
|
|
let!(:build) do
|
|
|
|
create(:ci_build, :success, :artifacts, pipeline: pipeline)
|
|
|
|
end
|
|
|
|
|
2017-11-21 16:32:12 +00:00
|
|
|
it 'returns an Array' do
|
|
|
|
expect(pipeline.latest_builds_with_artifacts).to be_an_instance_of(Array)
|
|
|
|
end
|
|
|
|
|
2017-09-12 16:13:07 +00:00
|
|
|
it 'returns the latest builds' do
|
|
|
|
expect(pipeline.latest_builds_with_artifacts).to eq([build])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'memoizes the returned relation' do
|
|
|
|
query_count = ActiveRecord::QueryRecorder
|
|
|
|
.new { 2.times { pipeline.latest_builds_with_artifacts.to_a } }
|
|
|
|
.count
|
|
|
|
|
|
|
|
expect(query_count).to eq(1)
|
|
|
|
end
|
|
|
|
end
|
2017-12-18 09:13:46 +00:00
|
|
|
|
|
|
|
describe '#total_size' do
|
|
|
|
let!(:build_job1) { create(:ci_build, pipeline: pipeline, stage_idx: 0) }
|
|
|
|
let!(:build_job2) { create(:ci_build, pipeline: pipeline, stage_idx: 0) }
|
|
|
|
let!(:test_job_failed_and_retried) { create(:ci_build, :failed, :retried, pipeline: pipeline, stage_idx: 1) }
|
|
|
|
let!(:second_test_job) { create(:ci_build, pipeline: pipeline, stage_idx: 1) }
|
|
|
|
let!(:deploy_job) { create(:ci_build, pipeline: pipeline, stage_idx: 2) }
|
|
|
|
|
|
|
|
it 'returns all jobs (including failed and retried)' do
|
|
|
|
expect(pipeline.total_size).to eq(5)
|
|
|
|
end
|
|
|
|
end
|
2015-08-26 01:42:46 +00:00
|
|
|
end
|