2019-10-12 21:52:04 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2023-03-17 16:20:25 +05:30
|
|
|
RSpec.describe Gitlab::Ci::Pipeline::Chain::Populate, feature_category: :continuous_integration do
|
2020-04-08 14:13:33 +05:30
|
|
|
let_it_be(:project) { create(:project, :repository) }
|
|
|
|
let_it_be(:user) { create(:user) }
|
2018-05-09 12:01:36 +05:30
|
|
|
|
|
|
|
let(:pipeline) do
|
2019-12-26 22:10:19 +05:30
|
|
|
build(:ci_pipeline, project: project, ref: 'master', user: user)
|
2018-05-09 12:01:36 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
let(:command) do
|
|
|
|
Gitlab::Ci::Pipeline::Chain::Command.new(
|
|
|
|
project: project,
|
|
|
|
current_user: user,
|
2019-01-03 12:48:30 +05:30
|
|
|
origin_ref: 'master',
|
2018-05-09 12:01:36 +05:30
|
|
|
seeds_block: nil)
|
|
|
|
end
|
|
|
|
|
2019-12-26 22:10:19 +05:30
|
|
|
let(:dependencies) do
|
|
|
|
[
|
|
|
|
Gitlab::Ci::Pipeline::Chain::Config::Content.new(pipeline, command),
|
|
|
|
Gitlab::Ci::Pipeline::Chain::Config::Process.new(pipeline, command),
|
2021-04-29 21:17:54 +05:30
|
|
|
Gitlab::Ci::Pipeline::Chain::EvaluateWorkflowRules.new(pipeline, command),
|
2021-01-29 00:20:46 +05:30
|
|
|
Gitlab::Ci::Pipeline::Chain::SeedBlock.new(pipeline, command),
|
2019-12-26 22:10:19 +05:30
|
|
|
Gitlab::Ci::Pipeline::Chain::Seed.new(pipeline, command)
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
let(:step) { described_class.new(pipeline, command) }
|
|
|
|
|
2019-12-26 22:10:19 +05:30
|
|
|
let(:config) do
|
|
|
|
{ rspec: { script: 'rspec' } }
|
|
|
|
end
|
|
|
|
|
|
|
|
def run_chain
|
|
|
|
dependencies.map(&:perform!)
|
|
|
|
step.perform!
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
stub_ci_pipeline_yaml_file(YAML.dump(config))
|
|
|
|
end
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
context 'when pipeline doesn not have seeds block' do
|
|
|
|
before do
|
2019-12-26 22:10:19 +05:30
|
|
|
run_chain
|
2018-05-09 12:01:36 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not persist the pipeline' do
|
|
|
|
expect(pipeline).not_to be_persisted
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not break the chain' do
|
|
|
|
expect(step.break?).to be false
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'populates pipeline with stages' do
|
|
|
|
expect(pipeline.stages).to be_one
|
|
|
|
expect(pipeline.stages.first).not_to be_persisted
|
2019-10-12 21:52:04 +05:30
|
|
|
expect(pipeline.stages.first.statuses).to be_one
|
|
|
|
expect(pipeline.stages.first.statuses.first).not_to be_persisted
|
2018-05-09 12:01:36 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'correctly assigns user' do
|
|
|
|
expect(pipeline.builds).to all(have_attributes(user: user))
|
|
|
|
end
|
2018-11-08 19:23:39 +05:30
|
|
|
|
|
|
|
it 'has pipeline iid' do
|
|
|
|
expect(pipeline.iid).to be > 0
|
|
|
|
end
|
2018-05-09 12:01:36 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'when pipeline is empty' do
|
|
|
|
let(:config) do
|
|
|
|
{ rspec: {
|
|
|
|
script: 'ls',
|
|
|
|
only: ['something']
|
|
|
|
} }
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
2019-12-26 22:10:19 +05:30
|
|
|
run_chain
|
2018-05-09 12:01:36 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'breaks the chain' do
|
|
|
|
expect(step.break?).to be true
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'appends an error about missing stages' do
|
|
|
|
expect(pipeline.errors.to_a)
|
2023-03-17 16:20:25 +05:30
|
|
|
.to include 'Pipeline will not run for the selected trigger. ' \
|
|
|
|
'The rules configuration prevented any jobs from being added to the pipeline.'
|
2018-05-09 12:01:36 +05:30
|
|
|
end
|
2018-11-08 19:23:39 +05:30
|
|
|
|
|
|
|
it 'wastes pipeline iid' do
|
|
|
|
expect(InternalId.ci_pipelines.where(project_id: project.id).last.last_value).to be > 0
|
|
|
|
end
|
2021-04-29 21:17:54 +05:30
|
|
|
|
|
|
|
it 'increments the error metric' do
|
|
|
|
counter = Gitlab::Metrics.counter(:gitlab_ci_pipeline_failure_reasons, 'desc')
|
|
|
|
expect { run_chain }.to change { counter.get(reason: 'unknown_failure') }.by(1)
|
|
|
|
end
|
2018-05-09 12:01:36 +05:30
|
|
|
end
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
describe 'pipeline protect' do
|
|
|
|
context 'when ref is protected' do
|
|
|
|
before do
|
|
|
|
allow(project).to receive(:protected_for?).with('master').and_return(true)
|
|
|
|
allow(project).to receive(:protected_for?).with('refs/heads/master').and_return(true)
|
2019-12-26 22:10:19 +05:30
|
|
|
|
|
|
|
dependencies.map(&:perform!)
|
2019-03-02 22:35:43 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not protect the pipeline' do
|
2019-12-26 22:10:19 +05:30
|
|
|
run_chain
|
2019-03-02 22:35:43 +05:30
|
|
|
|
|
|
|
expect(pipeline.protected).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when ref is not protected' do
|
|
|
|
it 'does not protect the pipeline' do
|
2019-12-26 22:10:19 +05:30
|
|
|
run_chain
|
2019-03-02 22:35:43 +05:30
|
|
|
|
|
|
|
expect(pipeline.protected).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
context 'when pipeline has validation errors' do
|
|
|
|
let(:pipeline) do
|
|
|
|
build(:ci_pipeline, project: project, ref: nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
2019-12-26 22:10:19 +05:30
|
|
|
run_chain
|
2018-05-09 12:01:36 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'breaks the chain' do
|
|
|
|
expect(step.break?).to be true
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'appends validation error' do
|
|
|
|
expect(pipeline.errors.to_a)
|
|
|
|
.to include 'Failed to build the pipeline!'
|
|
|
|
end
|
2018-11-08 19:23:39 +05:30
|
|
|
|
|
|
|
it 'wastes pipeline iid' do
|
|
|
|
expect(InternalId.ci_pipelines.where(project_id: project.id).last.last_value).to be > 0
|
|
|
|
end
|
2018-05-09 12:01:36 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'when there is a seed blocks present' do
|
|
|
|
let(:command) do
|
|
|
|
Gitlab::Ci::Pipeline::Chain::Command.new(
|
|
|
|
project: project,
|
|
|
|
current_user: user,
|
2019-01-03 12:48:30 +05:30
|
|
|
origin_ref: 'master',
|
2018-05-09 12:01:36 +05:30
|
|
|
seeds_block: seeds_block)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when seeds block builds some resources' do
|
|
|
|
let(:seeds_block) do
|
|
|
|
->(pipeline) { pipeline.variables.build(key: 'VAR', value: '123') }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'populates pipeline with resources described in the seeds block' do
|
2019-12-26 22:10:19 +05:30
|
|
|
run_chain
|
2018-05-09 12:01:36 +05:30
|
|
|
|
|
|
|
expect(pipeline).not_to be_persisted
|
|
|
|
expect(pipeline.variables).not_to be_empty
|
|
|
|
expect(pipeline.variables.first).not_to be_persisted
|
|
|
|
expect(pipeline.variables.first.key).to eq 'VAR'
|
|
|
|
expect(pipeline.variables.first.value).to eq '123'
|
|
|
|
end
|
2018-11-08 19:23:39 +05:30
|
|
|
|
|
|
|
it 'has pipeline iid' do
|
2019-12-26 22:10:19 +05:30
|
|
|
run_chain
|
2018-11-08 19:23:39 +05:30
|
|
|
|
|
|
|
expect(pipeline.iid).to be > 0
|
|
|
|
end
|
2018-05-09 12:01:36 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'when seeds block tries to persist some resources' do
|
|
|
|
let(:seeds_block) do
|
|
|
|
->(pipeline) { pipeline.variables.create!(key: 'VAR', value: '123') }
|
|
|
|
end
|
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
it 'raises error' do
|
|
|
|
expect { run_chain }.to raise_error(ActiveRecord::RecordNotSaved,
|
|
|
|
'You cannot call create unless the parent is saved')
|
2018-11-08 19:23:39 +05:30
|
|
|
end
|
2018-05-09 12:01:36 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when pipeline gets persisted during the process' do
|
2021-01-29 00:20:46 +05:30
|
|
|
before do
|
|
|
|
dependencies.each(&:perform!)
|
|
|
|
pipeline.save!
|
|
|
|
end
|
2018-05-09 12:01:36 +05:30
|
|
|
|
|
|
|
it 'raises error' do
|
2021-01-29 00:20:46 +05:30
|
|
|
expect { step.perform! }.to raise_error(described_class::PopulateError)
|
2018-05-09 12:01:36 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
context 'when variables policy is specified' do
|
|
|
|
shared_examples_for 'a correct pipeline' do
|
|
|
|
it 'populates pipeline according to used policies' do
|
2019-12-26 22:10:19 +05:30
|
|
|
run_chain
|
2018-05-09 12:01:36 +05:30
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
expect(pipeline.stages.size).to eq 1
|
2019-10-12 21:52:04 +05:30
|
|
|
expect(pipeline.stages.first.statuses.size).to eq 1
|
|
|
|
expect(pipeline.stages.first.statuses.first.name).to eq 'rspec'
|
2018-11-08 19:23:39 +05:30
|
|
|
end
|
2018-05-09 12:01:36 +05:30
|
|
|
end
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
context 'when using only/except build policies' do
|
|
|
|
let(:config) do
|
|
|
|
{ rspec: { script: 'rspec', stage: 'test', only: ['master'] },
|
|
|
|
prod: { script: 'cap prod', stage: 'deploy', only: ['tags'] } }
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'a correct pipeline'
|
|
|
|
|
|
|
|
context 'when variables expression is specified' do
|
|
|
|
context 'when pipeline iid is the subject' do
|
|
|
|
let(:config) do
|
|
|
|
{ rspec: { script: 'rspec', only: { variables: ["$CI_PIPELINE_IID == '1'"] } },
|
|
|
|
prod: { script: 'cap prod', only: { variables: ["$CI_PIPELINE_IID == '1000'"] } } }
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'a correct pipeline'
|
|
|
|
end
|
|
|
|
end
|
2018-05-09 12:01:36 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|