debian-mirror-gitlab/spec/lib/gitlab/ci/config/normalizer_spec.rb

249 lines
6.6 KiB
Ruby
Raw Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
require 'fast_spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe Gitlab::Ci::Config::Normalizer do
2018-12-13 13:39:08 +05:30
let(:job_name) { :rspec }
2021-10-27 15:23:28 +05:30
let(:job_config) { { script: 'rspec', parallel: parallel_config, name: 'rspec', job_variables: variables_config } }
2018-12-13 13:39:08 +05:30
let(:config) { { job_name => job_config } }
describe '.normalize_jobs' do
subject { described_class.new(config).normalize_jobs }
2020-10-24 23:57:45 +05:30
shared_examples 'parallel dependencies' do
2019-12-26 22:10:19 +05:30
context "when job has dependencies on parallelized jobs" do
2019-10-12 21:52:04 +05:30
let(:config) do
{
job_name => job_config,
2019-12-26 22:10:19 +05:30
other_job: { script: 'echo 1', dependencies: [job_name.to_s] }
2019-10-12 21:52:04 +05:30
}
end
2019-12-26 22:10:19 +05:30
it "parallelizes dependencies" do
expect(subject[:other_job][:dependencies]).to eq(expanded_job_names)
2019-10-12 21:52:04 +05:30
end
it "does not include original job name in #{context}" do
2019-12-26 22:10:19 +05:30
expect(subject[:other_job][:dependencies]).not_to include(job_name)
2019-10-12 21:52:04 +05:30
end
2018-12-13 13:39:08 +05:30
end
2019-12-26 22:10:19 +05:30
context "when there are dependencies which are both parallelized and not" do
2019-10-12 21:52:04 +05:30
let(:config) do
{
job_name => job_config,
other_job: { script: 'echo 1' },
2019-12-26 22:10:19 +05:30
final_job: { script: 'echo 1', dependencies: [job_name.to_s, "other_job"] }
2019-10-12 21:52:04 +05:30
}
end
2019-03-02 22:35:43 +05:30
2019-12-26 22:10:19 +05:30
it "parallelizes dependencies" do
2020-10-24 23:57:45 +05:30
expect(subject[:final_job][:dependencies]).to include(*expanded_job_names)
2019-12-26 22:10:19 +05:30
end
it "includes the regular job in dependencies" do
expect(subject[:final_job][:dependencies]).to include('other_job')
end
end
end
2020-10-24 23:57:45 +05:30
shared_examples 'parallel needs' do
2019-12-26 22:10:19 +05:30
let(:expanded_job_attributes) do
expanded_job_names.map do |job_name|
2020-01-01 13:55:28 +05:30
{ name: job_name, extra: :key }
2019-12-26 22:10:19 +05:30
end
end
2020-10-24 23:57:45 +05:30
context 'when job has needs on parallelized jobs' do
2019-12-26 22:10:19 +05:30
let(:config) do
{
job_name => job_config,
other_job: {
script: 'echo 1',
needs: {
job: [
2020-01-01 13:55:28 +05:30
{ name: job_name.to_s, extra: :key }
2019-12-26 22:10:19 +05:30
]
}
}
}
end
2020-10-24 23:57:45 +05:30
it 'parallelizes needs' do
2019-12-26 22:10:19 +05:30
expect(subject.dig(:other_job, :needs, :job)).to eq(expanded_job_attributes)
end
end
2020-10-24 23:57:45 +05:30
context 'when there are dependencies which are both parallelized and not' do
2019-12-26 22:10:19 +05:30
let(:config) do
{
job_name => job_config,
other_job: {
script: 'echo 1'
},
final_job: {
script: 'echo 1',
needs: {
job: [
2020-01-01 13:55:28 +05:30
{ name: job_name.to_s, extra: :key },
2020-10-24 23:57:45 +05:30
{ name: 'other_job', extra: :key }
2019-12-26 22:10:19 +05:30
]
}
}
}
end
2020-10-24 23:57:45 +05:30
it 'parallelizes dependencies' do
2019-12-26 22:10:19 +05:30
expect(subject.dig(:final_job, :needs, :job)).to include(*expanded_job_attributes)
2019-10-12 21:52:04 +05:30
end
2019-03-02 22:35:43 +05:30
2020-10-24 23:57:45 +05:30
it 'includes the regular job in dependencies' do
2020-01-01 13:55:28 +05:30
expect(subject.dig(:final_job, :needs, :job)).to include(name: 'other_job', extra: :key)
2019-10-12 21:52:04 +05:30
end
2019-03-02 22:35:43 +05:30
end
end
2020-10-24 23:57:45 +05:30
context 'with parallel config as integer' do
let(:variables_config) { {} }
let(:parallel_config) { 5 }
let(:expanded_job_names) do
[
'rspec 1/5',
'rspec 2/5',
'rspec 3/5',
'rspec 4/5',
'rspec 5/5'
]
end
it 'does not have original job' do
is_expected.not_to include(job_name)
end
it 'has parallelized jobs' do
is_expected.to include(*expanded_job_names.map(&:to_sym))
end
it 'sets job instance in options' do
expect(subject.values).to all(include(:instance))
end
it 'parallelizes jobs with original config' do
original_config = config[job_name]
.except(:name)
.deep_merge(parallel: { total: parallel_config })
configs = subject.values.map { |config| config.except(:name, :instance) }
expect(configs).to all(eq(original_config))
end
context 'when the job is not parallelized' do
let(:job_config) { { script: 'rspec', name: 'rspec' } }
it 'returns the same hash' do
is_expected.to eq(config)
end
end
context 'when there is a job with a slash in it' do
let(:job_name) { :"rspec 35/2" }
it 'properly parallelizes job names' do
job_names = [
:"rspec 35/2 1/5",
:"rspec 35/2 2/5",
:"rspec 35/2 3/5",
:"rspec 35/2 4/5",
:"rspec 35/2 5/5"
]
is_expected.to include(*job_names)
end
end
it_behaves_like 'parallel dependencies'
it_behaves_like 'parallel needs'
end
context 'with parallel matrix config' do
let(:variables_config) do
{
USER_VARIABLE: 'user value'
}
end
let(:parallel_config) do
{
matrix: [
{
2020-11-24 15:15:51 +05:30
VAR_1: ['A'],
VAR_2: %w[B C]
2020-10-24 23:57:45 +05:30
}
]
}
end
let(:expanded_job_names) do
[
2020-11-24 15:15:51 +05:30
'rspec: [A, B]',
'rspec: [A, C]'
2020-10-24 23:57:45 +05:30
]
end
it 'does not have original job' do
is_expected.not_to include(job_name)
end
it 'sets job instance in options' do
expect(subject.values).to all(include(:instance))
end
it 'sets job variables', :aggregate_failures do
expect(subject.values[0]).to match(
2021-10-27 15:23:28 +05:30
a_hash_including(job_variables: { VAR_1: 'A', VAR_2: 'B', USER_VARIABLE: 'user value' })
2020-10-24 23:57:45 +05:30
)
expect(subject.values[1]).to match(
2021-10-27 15:23:28 +05:30
a_hash_including(job_variables: { VAR_1: 'A', VAR_2: 'C', USER_VARIABLE: 'user value' })
2020-10-24 23:57:45 +05:30
)
end
it 'parallelizes jobs with original config' do
configs = subject.values.map do |config|
2021-10-27 15:23:28 +05:30
config.except(:name, :instance, :job_variables)
2020-10-24 23:57:45 +05:30
end
original_config = config[job_name]
2021-10-27 15:23:28 +05:30
.except(:name, :job_variables)
2020-10-24 23:57:45 +05:30
.deep_merge(parallel: { total: 2 })
expect(configs).to all(match(a_hash_including(original_config)))
end
2020-11-24 15:15:51 +05:30
it 'has parallelized jobs' do
is_expected.to include(*expanded_job_names.map(&:to_sym))
end
2020-10-24 23:57:45 +05:30
it_behaves_like 'parallel dependencies'
it_behaves_like 'parallel needs'
end
context 'when parallel config does not matches a factory' do
let(:variables_config) { {} }
let(:parallel_config) { }
it 'does not alter the job config' do
is_expected.to match(config)
end
end
2020-11-24 15:15:51 +05:30
context 'when jobs config is nil' do
let(:config) { nil }
it { is_expected.to eq({}) }
end
2018-12-13 13:39:08 +05:30
end
end