2021-10-27 15:23:28 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
RSpec.describe Ci::CreatePipelineService do
|
|
|
|
context 'include:' do
|
|
|
|
let_it_be(:project) { create(:project, :repository) }
|
2022-03-02 08:16:31 +05:30
|
|
|
let_it_be(:user) { project.first_owner }
|
2021-10-27 15:23:28 +05:30
|
|
|
|
2021-12-11 22:18:48 +05:30
|
|
|
let(:ref) { 'refs/heads/master' }
|
|
|
|
let(:variables_attributes) { [{ key: 'MYVAR', secret_value: 'hello' }] }
|
|
|
|
let(:source) { :push }
|
|
|
|
|
|
|
|
let(:service) { described_class.new(project, user, { ref: ref, variables_attributes: variables_attributes }) }
|
2021-10-27 15:23:28 +05:30
|
|
|
let(:pipeline) { service.execute(source).payload }
|
|
|
|
|
|
|
|
let(:file_location) { 'spec/fixtures/gitlab/ci/external_files/.gitlab-ci-template-1.yml' }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(project.repository)
|
|
|
|
.to receive(:blob_data_at).with(project.commit.id, '.gitlab-ci.yml')
|
|
|
|
.and_return(config)
|
|
|
|
|
|
|
|
allow(project.repository)
|
|
|
|
.to receive(:blob_data_at).with(project.commit.id, file_location)
|
|
|
|
.and_return(File.read(Rails.root.join(file_location)))
|
|
|
|
end
|
|
|
|
|
2021-12-11 22:18:48 +05:30
|
|
|
shared_examples 'not including the file' do
|
|
|
|
it 'does not include the job in the file' do
|
|
|
|
expect(pipeline).to be_created_successfully
|
|
|
|
expect(pipeline.processables.pluck(:name)).to contain_exactly('job')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples 'including the file' do
|
|
|
|
it 'includes the job in the file' do
|
|
|
|
expect(pipeline).to be_created_successfully
|
|
|
|
expect(pipeline.processables.pluck(:name)).to contain_exactly('job', 'rspec')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
context 'with a local file' do
|
|
|
|
let(:config) do
|
|
|
|
<<~EOY
|
|
|
|
include: #{file_location}
|
|
|
|
job:
|
|
|
|
script: exit 0
|
|
|
|
EOY
|
|
|
|
end
|
|
|
|
|
2021-12-11 22:18:48 +05:30
|
|
|
it_behaves_like 'including the file'
|
2021-10-27 15:23:28 +05:30
|
|
|
end
|
|
|
|
|
2021-12-11 22:18:48 +05:30
|
|
|
context 'with a local file with rules with a project variable' do
|
2021-10-27 15:23:28 +05:30
|
|
|
let(:config) do
|
|
|
|
<<~EOY
|
|
|
|
include:
|
|
|
|
- local: #{file_location}
|
|
|
|
rules:
|
|
|
|
- if: $CI_PROJECT_ID == "#{project_id}"
|
|
|
|
job:
|
|
|
|
script: exit 0
|
|
|
|
EOY
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the rules matches' do
|
|
|
|
let(:project_id) { project.id }
|
|
|
|
|
2021-12-11 22:18:48 +05:30
|
|
|
it_behaves_like 'including the file'
|
2021-10-27 15:23:28 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the rules does not match' do
|
|
|
|
let(:project_id) { non_existing_record_id }
|
|
|
|
|
2021-12-11 22:18:48 +05:30
|
|
|
it_behaves_like 'not including the file'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a local file with rules with a predefined pipeline variable' do
|
|
|
|
let(:config) do
|
|
|
|
<<~EOY
|
|
|
|
include:
|
|
|
|
- local: #{file_location}
|
|
|
|
rules:
|
|
|
|
- if: $CI_PIPELINE_SOURCE == "#{pipeline_source}"
|
|
|
|
job:
|
|
|
|
script: exit 0
|
|
|
|
EOY
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the rules matches' do
|
|
|
|
let(:pipeline_source) { 'push' }
|
|
|
|
|
|
|
|
it_behaves_like 'including the file'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the rules does not match' do
|
|
|
|
let(:pipeline_source) { 'web' }
|
|
|
|
|
|
|
|
it_behaves_like 'not including the file'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a local file with rules with a run pipeline variable' do
|
|
|
|
let(:config) do
|
|
|
|
<<~EOY
|
|
|
|
include:
|
|
|
|
- local: #{file_location}
|
|
|
|
rules:
|
|
|
|
- if: $MYVAR == "#{my_var}"
|
|
|
|
job:
|
|
|
|
script: exit 0
|
|
|
|
EOY
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the rules matches' do
|
|
|
|
let(:my_var) { 'hello' }
|
|
|
|
|
|
|
|
it_behaves_like 'including the file'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the rules does not match' do
|
|
|
|
let(:my_var) { 'mello' }
|
|
|
|
|
|
|
|
it_behaves_like 'not including the file'
|
2021-10-27 15:23:28 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|