2019-12-26 22:10:19 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe Gitlab::Ci::Build::Rules do
|
2022-01-26 12:08:38 +05:30
|
|
|
let_it_be(:pipeline) { create(:ci_pipeline) }
|
|
|
|
let_it_be(:ci_build) { build(:ci_build, pipeline: pipeline) }
|
2019-12-04 20:38:33 +05:30
|
|
|
|
|
|
|
let(:seed) do
|
|
|
|
double('build seed',
|
|
|
|
to_resource: ci_build,
|
2022-01-26 12:08:38 +05:30
|
|
|
variables_hash: ci_build.scoped_variables.to_hash
|
2019-12-04 20:38:33 +05:30
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2019-12-26 22:10:19 +05:30
|
|
|
let(:rules) { described_class.new(rule_list, default_when: 'on_success') }
|
2019-12-04 20:38:33 +05:30
|
|
|
|
|
|
|
describe '.new' do
|
|
|
|
let(:rules_ivar) { rules.instance_variable_get :@rule_list }
|
|
|
|
let(:default_when) { rules.instance_variable_get :@default_when }
|
|
|
|
|
|
|
|
context 'with no rules' do
|
|
|
|
let(:rule_list) { [] }
|
|
|
|
|
|
|
|
it 'sets @rule_list to an empty array' do
|
|
|
|
expect(rules_ivar).to eq([])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets @default_when to "on_success"' do
|
|
|
|
expect(default_when).to eq('on_success')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with one rule' do
|
|
|
|
let(:rule_list) { [{ if: '$VAR == null', when: 'always' }] }
|
|
|
|
|
|
|
|
it 'sets @rule_list to an array of a single rule' do
|
|
|
|
expect(rules_ivar).to be_an(Array)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets @default_when to "on_success"' do
|
|
|
|
expect(default_when).to eq('on_success')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with multiple rules' do
|
|
|
|
let(:rule_list) do
|
|
|
|
[
|
|
|
|
{ if: '$VAR == null', when: 'always' },
|
|
|
|
{ if: '$VAR == null', when: 'always' }
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets @rule_list to an array of a single rule' do
|
|
|
|
expect(rules_ivar).to be_an(Array)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets @default_when to "on_success"' do
|
|
|
|
expect(default_when).to eq('on_success')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a specified default when:' do
|
|
|
|
let(:rule_list) { [{ if: '$VAR == null', when: 'always' }] }
|
2019-12-26 22:10:19 +05:30
|
|
|
let(:rules) { described_class.new(rule_list, default_when: 'manual') }
|
2019-12-04 20:38:33 +05:30
|
|
|
|
|
|
|
it 'sets @rule_list to an array of a single rule' do
|
|
|
|
expect(rules_ivar).to be_an(Array)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets @default_when to "manual"' do
|
|
|
|
expect(default_when).to eq('manual')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#evaluate' do
|
|
|
|
subject { rules.evaluate(pipeline, seed) }
|
|
|
|
|
|
|
|
context 'with nil rules' do
|
|
|
|
let(:rule_list) { nil }
|
|
|
|
|
|
|
|
it { is_expected.to eq(described_class::Result.new('on_success')) }
|
|
|
|
|
|
|
|
context 'and when:manual set as the default' do
|
2019-12-26 22:10:19 +05:30
|
|
|
let(:rules) { described_class.new(rule_list, default_when: 'manual') }
|
2019-12-04 20:38:33 +05:30
|
|
|
|
|
|
|
it { is_expected.to eq(described_class::Result.new('manual')) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with no rules' do
|
|
|
|
let(:rule_list) { [] }
|
|
|
|
|
|
|
|
it { is_expected.to eq(described_class::Result.new('never')) }
|
|
|
|
|
|
|
|
context 'and when:manual set as the default' do
|
2019-12-26 22:10:19 +05:30
|
|
|
let(:rules) { described_class.new(rule_list, default_when: 'manual') }
|
2019-12-04 20:38:33 +05:30
|
|
|
|
|
|
|
it { is_expected.to eq(described_class::Result.new('never')) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with one rule without any clauses' do
|
2020-03-13 15:44:24 +05:30
|
|
|
let(:rule_list) { [{ when: 'manual', allow_failure: true }] }
|
2019-12-04 20:38:33 +05:30
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
it { is_expected.to eq(described_class::Result.new('manual', nil, true, nil)) }
|
2019-12-04 20:38:33 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'with one matching rule' do
|
|
|
|
let(:rule_list) { [{ if: '$VAR == null', when: 'always' }] }
|
|
|
|
|
|
|
|
it { is_expected.to eq(described_class::Result.new('always')) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with two matching rules' do
|
|
|
|
let(:rule_list) do
|
|
|
|
[
|
|
|
|
{ if: '$VAR == null', when: 'delayed', start_in: '1 day' },
|
|
|
|
{ if: '$VAR == null', when: 'always' }
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the value of the first matched rule in the list' do
|
|
|
|
expect(subject).to eq(described_class::Result.new('delayed', '1 day'))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a non-matching and matching rule' do
|
|
|
|
let(:rule_list) do
|
|
|
|
[
|
|
|
|
{ if: '$VAR =! null', when: 'delayed', start_in: '1 day' },
|
|
|
|
{ if: '$VAR == null', when: 'always' }
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to eq(described_class::Result.new('always')) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a matching and non-matching rule' do
|
|
|
|
let(:rule_list) do
|
|
|
|
[
|
|
|
|
{ if: '$VAR == null', when: 'delayed', start_in: '1 day' },
|
|
|
|
{ if: '$VAR != null', when: 'always' }
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to eq(described_class::Result.new('delayed', '1 day')) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with non-matching rules' do
|
|
|
|
let(:rule_list) do
|
|
|
|
[
|
|
|
|
{ if: '$VAR != null', when: 'delayed', start_in: '1 day' },
|
|
|
|
{ if: '$VAR != null', when: 'always' }
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to eq(described_class::Result.new('never')) }
|
|
|
|
|
|
|
|
context 'and when:manual set as the default' do
|
2019-12-26 22:10:19 +05:30
|
|
|
let(:rules) { described_class.new(rule_list, default_when: 'manual') }
|
2019-12-04 20:38:33 +05:30
|
|
|
|
|
|
|
it 'does not return the default when:' do
|
|
|
|
expect(subject).to eq(described_class::Result.new('never'))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-03-13 15:44:24 +05:30
|
|
|
|
|
|
|
context 'with only allow_failure' do
|
|
|
|
context 'with matching rule' do
|
|
|
|
let(:rule_list) { [{ if: '$VAR == null', allow_failure: true }] }
|
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
it { is_expected.to eq(described_class::Result.new('on_success', nil, true, nil)) }
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'with non-matching rule' do
|
|
|
|
let(:rule_list) { [{ if: '$VAR != null', allow_failure: true }] }
|
|
|
|
|
|
|
|
it { is_expected.to eq(described_class::Result.new('never')) }
|
|
|
|
end
|
|
|
|
end
|
2021-02-22 17:27:13 +05:30
|
|
|
|
2023-07-09 08:55:56 +05:30
|
|
|
context 'with needs' do
|
|
|
|
context 'when single needs is specified' do
|
|
|
|
let(:rule_list) do
|
|
|
|
[{ if: '$VAR == null', needs: [{ name: 'test', artifacts: true, optional: false }] }]
|
|
|
|
end
|
|
|
|
|
|
|
|
it {
|
|
|
|
is_expected.to eq(described_class::Result.new('on_success', nil, nil, nil,
|
|
|
|
[{ name: 'test', artifacts: true, optional: false }], nil))
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when multiple needs are specified' do
|
|
|
|
let(:rule_list) do
|
|
|
|
[{ if: '$VAR == null',
|
|
|
|
needs: [{ name: 'test', artifacts: true, optional: false },
|
|
|
|
{ name: 'rspec', artifacts: true, optional: false }] }]
|
|
|
|
end
|
|
|
|
|
|
|
|
it {
|
|
|
|
is_expected.to eq(described_class::Result.new('on_success', nil, nil, nil,
|
|
|
|
[{ name: 'test', artifacts: true, optional: false },
|
|
|
|
{ name: 'rspec', artifacts: true, optional: false }], nil))
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when there are no needs specified' do
|
|
|
|
let(:rule_list) { [{ if: '$VAR == null' }] }
|
|
|
|
|
|
|
|
it { is_expected.to eq(described_class::Result.new('on_success', nil, nil, nil, nil, nil)) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when need is specified with additional attibutes' do
|
|
|
|
let(:rule_list) do
|
|
|
|
[{ if: '$VAR == null', needs: [{
|
|
|
|
artifacts: true,
|
|
|
|
name: 'test',
|
|
|
|
optional: false,
|
|
|
|
when: 'never'
|
|
|
|
}] }]
|
|
|
|
end
|
|
|
|
|
|
|
|
it {
|
|
|
|
is_expected.to eq(
|
|
|
|
described_class::Result.new('on_success', nil, nil, nil,
|
|
|
|
[{ artifacts: true, name: 'test', optional: false, when: 'never' }], nil))
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when feature flag is disabled' do
|
|
|
|
before do
|
|
|
|
stub_feature_flags(introduce_rules_with_needs: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with needs' do
|
|
|
|
context 'when single needs is specified' do
|
|
|
|
let(:rule_list) do
|
|
|
|
[{ if: '$VAR == null', needs: [{ name: 'test', artifacts: true, optional: false }] }]
|
|
|
|
end
|
|
|
|
|
|
|
|
it {
|
|
|
|
is_expected.to eq(described_class::Result.new('on_success', nil, nil, nil, nil, nil))
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when multiple needs are specified' do
|
|
|
|
let(:rule_list) do
|
|
|
|
[{ if: '$VAR == null',
|
|
|
|
needs: [{ name: 'test', artifacts: true, optional: false },
|
|
|
|
{ name: 'rspec', artifacts: true, optional: false }] }]
|
|
|
|
end
|
|
|
|
|
|
|
|
it {
|
|
|
|
is_expected.to eq(described_class::Result.new('on_success', nil, nil, nil, nil, nil))
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when there are no needs specified' do
|
|
|
|
let(:rule_list) { [{ if: '$VAR == null' }] }
|
|
|
|
|
|
|
|
it { is_expected.to eq(described_class::Result.new('on_success', nil, nil, nil, nil, nil)) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when need is specified with additional attibutes' do
|
|
|
|
let(:rule_list) do
|
|
|
|
[{ if: '$VAR == null', needs: [{
|
|
|
|
artifacts: true,
|
|
|
|
name: 'test',
|
|
|
|
optional: false,
|
|
|
|
when: 'never'
|
|
|
|
}] }]
|
|
|
|
end
|
|
|
|
|
|
|
|
it {
|
|
|
|
is_expected.to eq(
|
|
|
|
described_class::Result.new('on_success', nil, nil, nil, nil, nil))
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
context 'with variables' do
|
|
|
|
context 'with matching rule' do
|
|
|
|
let(:rule_list) { [{ if: '$VAR == null', variables: { MY_VAR: 'my var' } }] }
|
|
|
|
|
|
|
|
it { is_expected.to eq(described_class::Result.new('on_success', nil, nil, { MY_VAR: 'my var' })) }
|
|
|
|
end
|
|
|
|
end
|
2022-07-16 23:28:13 +05:30
|
|
|
|
|
|
|
context 'with a regexp variable matching rule' do
|
|
|
|
let(:rule_list) { [{ if: '"abcde" =~ $pattern' }] }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(ci_build).to receive(:scoped_variables).and_return(
|
|
|
|
Gitlab::Ci::Variables::Collection.new
|
|
|
|
.append(key: 'pattern', value: '/^ab.*/', public: true)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to eq(described_class::Result.new('on_success')) }
|
|
|
|
end
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
describe 'Gitlab::Ci::Build::Rules::Result' do
|
|
|
|
let(:when_value) { 'on_success' }
|
|
|
|
let(:start_in) { nil }
|
|
|
|
let(:allow_failure) { nil }
|
2021-02-22 17:27:13 +05:30
|
|
|
let(:variables) { nil }
|
2023-07-09 08:55:56 +05:30
|
|
|
let(:needs) { nil }
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
subject(:result) do
|
2023-07-09 08:55:56 +05:30
|
|
|
Gitlab::Ci::Build::Rules::Result.new(when_value, start_in, allow_failure, variables, needs)
|
2021-02-22 17:27:13 +05:30
|
|
|
end
|
2020-03-13 15:44:24 +05:30
|
|
|
|
|
|
|
describe '#build_attributes' do
|
2021-02-22 17:27:13 +05:30
|
|
|
subject(:build_attributes) do
|
2021-03-11 19:13:27 +05:30
|
|
|
result.build_attributes
|
2021-02-22 17:27:13 +05:30
|
|
|
end
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
it 'compacts nil values' do
|
2021-02-22 17:27:13 +05:30
|
|
|
is_expected.to eq(options: {}, when: 'on_success')
|
|
|
|
end
|
2023-07-09 08:55:56 +05:30
|
|
|
|
|
|
|
context 'scheduling_type' do
|
|
|
|
context 'when rules have needs' do
|
|
|
|
context 'single need' do
|
|
|
|
let(:needs) do
|
|
|
|
{ job: [{ name: 'test' }] }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'saves needs' do
|
|
|
|
expect(subject[:needs_attributes]).to eq([{ name: "test" }])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds schedule type to the build_attributes' do
|
|
|
|
expect(subject[:scheduling_type]).to eq(:dag)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'multiple needs' do
|
|
|
|
let(:needs) do
|
|
|
|
{ job: [{ name: 'test' }, { name: 'test_2', artifacts: true, optional: false }] }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'saves needs' do
|
|
|
|
expect(subject[:needs_attributes]).to match_array([{ name: "test" },
|
|
|
|
{ name: 'test_2', artifacts: true, optional: false }])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds schedule type to the build_attributes' do
|
|
|
|
expect(subject[:scheduling_type]).to eq(:dag)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when rules do not have needs' do
|
|
|
|
it 'does not add schedule type to the build_attributes' do
|
|
|
|
expect(subject.key?(:scheduling_type)).to be_falsy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
describe '#pass?' do
|
|
|
|
context "'when' is 'never'" do
|
|
|
|
let!(:when_value) { 'never' }
|
|
|
|
|
|
|
|
it 'returns false' do
|
2021-02-22 17:27:13 +05:30
|
|
|
expect(result.pass?).to eq(false)
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "'when' is 'on_success'" do
|
|
|
|
let!(:when_value) { 'on_success' }
|
|
|
|
|
|
|
|
it 'returns true' do
|
2021-02-22 17:27:13 +05:30
|
|
|
expect(result.pass?).to eq(true)
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-12-04 20:38:33 +05:30
|
|
|
end
|
|
|
|
end
|