diff options
Diffstat (limited to 'spec/lib/gitlab/ci/config')
-rw-r--r-- | spec/lib/gitlab/ci/config/entry/cache_spec.rb | 10 | ||||
-rw-r--r-- | spec/lib/gitlab/ci/config/entry/commands_spec.rb | 8 | ||||
-rw-r--r-- | spec/lib/gitlab/ci/config/entry/job_spec.rb | 10 | ||||
-rw-r--r-- | spec/lib/gitlab/ci/config/entry/processable_spec.rb | 29 | ||||
-rw-r--r-- | spec/lib/gitlab/ci/config/external/mapper_spec.rb | 14 | ||||
-rw-r--r-- | spec/lib/gitlab/ci/config/yaml/tags/reference_spec.rb | 121 | ||||
-rw-r--r-- | spec/lib/gitlab/ci/config/yaml/tags/resolver_spec.rb | 123 |
7 files changed, 280 insertions, 35 deletions
diff --git a/spec/lib/gitlab/ci/config/entry/cache_spec.rb b/spec/lib/gitlab/ci/config/entry/cache_spec.rb index 80427eaa6ee..247f4b63910 100644 --- a/spec/lib/gitlab/ci/config/entry/cache_spec.rb +++ b/spec/lib/gitlab/ci/config/entry/cache_spec.rb @@ -3,6 +3,8 @@ require 'spec_helper' RSpec.describe Gitlab::Ci::Config::Entry::Cache do + using RSpec::Parameterized::TableSyntax + subject(:entry) { described_class.new(config) } describe 'validations' do @@ -56,8 +58,6 @@ RSpec.describe Gitlab::Ci::Config::Entry::Cache do end context 'with `policy`' do - using RSpec::Parameterized::TableSyntax - where(:policy, :result) do 'pull-push' | 'pull-push' 'push' | 'push' @@ -77,8 +77,6 @@ RSpec.describe Gitlab::Ci::Config::Entry::Cache do end context 'with `when`' do - using RSpec::Parameterized::TableSyntax - where(:when_config, :result) do 'on_success' | 'on_success' 'on_failure' | 'on_failure' @@ -109,8 +107,6 @@ RSpec.describe Gitlab::Ci::Config::Entry::Cache do end context 'with `policy`' do - using RSpec::Parameterized::TableSyntax - where(:policy, :valid) do 'pull-push' | true 'push' | true @@ -126,8 +122,6 @@ RSpec.describe Gitlab::Ci::Config::Entry::Cache do end context 'with `when`' do - using RSpec::Parameterized::TableSyntax - where(:when_config, :valid) do 'on_success' | true 'on_failure' | true diff --git a/spec/lib/gitlab/ci/config/entry/commands_spec.rb b/spec/lib/gitlab/ci/config/entry/commands_spec.rb index 439799fe973..1b8dfae692a 100644 --- a/spec/lib/gitlab/ci/config/entry/commands_spec.rb +++ b/spec/lib/gitlab/ci/config/entry/commands_spec.rb @@ -87,18 +87,20 @@ RSpec.describe Gitlab::Ci::Config::Entry::Commands do describe '#errors' do it 'saves errors' do expect(entry.errors) - .to include 'commands config should be a string or an array containing strings and arrays of strings' + .to include 'commands config should be a string or a nested array of strings up to 10 levels deep' end end end context 'when entry value is multi-level nested array' do - let(:config) { [['ls', ['echo 1']], 'pwd'] } + let(:config) do + ['ls 0', ['ls 1', ['ls 2', ['ls 3', ['ls 4', ['ls 5', ['ls 6', ['ls 7', ['ls 8', ['ls 9', ['ls 10']]]]]]]]]]] + end describe '#errors' do it 'saves errors' do expect(entry.errors) - .to include 'commands config should be a string or an array containing strings and arrays of strings' + .to include 'commands config should be a string or a nested array of strings up to 10 levels deep' end end diff --git a/spec/lib/gitlab/ci/config/entry/job_spec.rb b/spec/lib/gitlab/ci/config/entry/job_spec.rb index 7834a1a94f2..a3b5f32b9f9 100644 --- a/spec/lib/gitlab/ci/config/entry/job_spec.rb +++ b/spec/lib/gitlab/ci/config/entry/job_spec.rb @@ -763,16 +763,6 @@ RSpec.describe Gitlab::Ci::Config::Entry::Job do it 'returns allow_failure_criteria' do expect(entry.value[:allow_failure_criteria]).to match(exit_codes: [42]) end - - context 'with ci_allow_failure_with_exit_codes disabled' do - before do - stub_feature_flags(ci_allow_failure_with_exit_codes: false) - end - - it 'does not return allow_failure_criteria' do - expect(entry.value.key?(:allow_failure_criteria)).to be_falsey - end - end end end end diff --git a/spec/lib/gitlab/ci/config/entry/processable_spec.rb b/spec/lib/gitlab/ci/config/entry/processable_spec.rb index aadf94365c6..04e80450263 100644 --- a/spec/lib/gitlab/ci/config/entry/processable_spec.rb +++ b/spec/lib/gitlab/ci/config/entry/processable_spec.rb @@ -73,6 +73,15 @@ RSpec.describe Gitlab::Ci::Config::Entry::Processable do end end + context 'when resource_group key is not a string' do + let(:config) { { resource_group: 123 } } + + it 'returns error about wrong value type' do + expect(entry).not_to be_valid + expect(entry.errors).to include "job resource group should be a string" + end + end + context 'when it uses both "when:" and "rules:"' do let(:config) do { @@ -340,6 +349,26 @@ RSpec.describe Gitlab::Ci::Config::Entry::Processable do end end + context 'with resource group' do + using RSpec::Parameterized::TableSyntax + + where(:resource_group, :result) do + 'iOS' | 'iOS' + 'review/$CI_COMMIT_REF_NAME' | 'review/$CI_COMMIT_REF_NAME' + nil | nil + end + + with_them do + let(:config) { { script: 'ls', resource_group: resource_group }.compact } + + it do + entry.compose!(deps) + + expect(entry.resource_group).to eq(result) + end + end + end + context 'with inheritance' do context 'of variables' do let(:config) do diff --git a/spec/lib/gitlab/ci/config/external/mapper_spec.rb b/spec/lib/gitlab/ci/config/external/mapper_spec.rb index 4fdaaca8316..99f546ceb37 100644 --- a/spec/lib/gitlab/ci/config/external/mapper_spec.rb +++ b/spec/lib/gitlab/ci/config/external/mapper_spec.rb @@ -323,20 +323,6 @@ RSpec.describe Gitlab::Ci::Config::External::Mapper do expect { subject }.to raise_error(described_class::AmbigiousSpecificationError) end end - - context 'when feature flag is turned off' do - let(:values) do - { include: full_local_file_path } - end - - before do - stub_feature_flags(variables_in_include_section_ci: false) - end - - it 'does not expand the variables' do - expect(subject[0].location).to eq('$CI_PROJECT_PATH' + local_file) - end - end end end end diff --git a/spec/lib/gitlab/ci/config/yaml/tags/reference_spec.rb b/spec/lib/gitlab/ci/config/yaml/tags/reference_spec.rb new file mode 100644 index 00000000000..c68dccd3455 --- /dev/null +++ b/spec/lib/gitlab/ci/config/yaml/tags/reference_spec.rb @@ -0,0 +1,121 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::Ci::Config::Yaml::Tags::Reference do + let(:config) do + Gitlab::Ci::Config::Yaml.load!(yaml) + end + + describe '.tag' do + it 'implements the tag method' do + expect(described_class.tag).to eq('!reference') + end + end + + describe '#resolve' do + subject { Gitlab::Ci::Config::Yaml::Tags::Resolver.new(config).to_hash } + + context 'with circular references' do + let(:yaml) do + <<~YML + a: !reference [b] + b: !reference [a] + YML + end + + it 'raises CircularReferenceError' do + expect { subject }.to raise_error Gitlab::Ci::Config::Yaml::Tags::TagError, '!reference ["b"] is part of a circular chain' + end + end + + context 'with nested circular references' do + let(:yaml) do + <<~YML + a: !reference [b, c] + b: { c: !reference [d, e, f] } + d: { e: { f: !reference [a] } } + YML + end + + it 'raises CircularReferenceError' do + expect { subject }.to raise_error Gitlab::Ci::Config::Yaml::Tags::TagError, '!reference ["b", "c"] is part of a circular chain' + end + end + + context 'with missing references' do + let(:yaml) { 'a: !reference [b]' } + + it 'raises MissingReferenceError' do + expect { subject }.to raise_error Gitlab::Ci::Config::Yaml::Tags::TagError, '!reference ["b"] could not be found' + end + end + + context 'with invalid references' do + using RSpec::Parameterized::TableSyntax + + where(:yaml, :error_message) do + 'a: !reference' | '!reference [] is not valid' + 'a: !reference str' | '!reference "str" is not valid' + 'a: !reference 1' | '!reference "1" is not valid' + 'a: !reference [1]' | '!reference [1] is not valid' + 'a: !reference { b: c }' | '!reference {"b"=>"c"} is not valid' + end + + with_them do + it 'raises an error' do + expect { subject }.to raise_error Gitlab::Ci::Config::Yaml::Tags::TagError, error_message + end + end + end + + context 'with arrays' do + let(:yaml) do + <<~YML + a: { b: [1, 2] } + c: { d: { e: [3, 4] } } + f: { g: [ !reference [a, b], 5, !reference [c, d, e]] } + YML + end + + it { is_expected.to match(a_hash_including({ f: { g: [[1, 2], 5, [3, 4]] } })) } + end + + context 'with hashes' do + context 'when referencing an entire hash' do + let(:yaml) do + <<~YML + a: { b: { c: 'c', d: 'd' } } + e: { f: !reference [a, b] } + YML + end + + it { is_expected.to match(a_hash_including({ e: { f: { c: 'c', d: 'd' } } })) } + end + + context 'when referencing only a hash value' do + let(:yaml) do + <<~YML + a: { b: { c: 'c', d: 'd' } } + e: { f: { g: !reference [a, b, c], h: 'h' } } + i: !reference [e, f] + YML + end + + it { is_expected.to match(a_hash_including({ i: { g: 'c', h: 'h' } })) } + end + + context 'when referencing a value before its definition' do + let(:yaml) do + <<~YML + a: { b: !reference [c, d] } + g: { h: { i: 'i', j: 1 } } + c: { d: { e: !reference [g, h, j], f: 'f' } } + YML + end + + it { is_expected.to match(a_hash_including({ a: { b: { e: 1, f: 'f' } } })) } + end + end + end +end diff --git a/spec/lib/gitlab/ci/config/yaml/tags/resolver_spec.rb b/spec/lib/gitlab/ci/config/yaml/tags/resolver_spec.rb new file mode 100644 index 00000000000..594242c33cc --- /dev/null +++ b/spec/lib/gitlab/ci/config/yaml/tags/resolver_spec.rb @@ -0,0 +1,123 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::Ci::Config::Yaml::Tags::Resolver do + let(:config) do + Gitlab::Ci::Config::Yaml.load!(yaml) + end + + describe '#to_hash' do + subject { described_class.new(config).to_hash } + + context 'when referencing deeply nested arrays' do + let(:yaml_templates) do + <<~YML + .job-1: + script: + - echo doing step 1 of job 1 + - echo doing step 2 of job 1 + + .job-2: + script: + - echo doing step 1 of job 2 + - !reference [.job-1, script] + - echo doing step 2 of job 2 + + .job-3: + script: + - echo doing step 1 of job 3 + - !reference [.job-2, script] + - echo doing step 2 of job 3 + YML + end + + let(:job_yaml) do + <<~YML + test: + script: + - echo preparing to test + - !reference [.job-3, script] + - echo test finished + YML + end + + shared_examples 'expands references' do + it 'expands the references' do + is_expected.to match({ + '.job-1': { + script: [ + 'echo doing step 1 of job 1', + 'echo doing step 2 of job 1' + ] + }, + '.job-2': { + script: [ + 'echo doing step 1 of job 2', + [ + 'echo doing step 1 of job 1', + 'echo doing step 2 of job 1' + ], + 'echo doing step 2 of job 2' + ] + }, + '.job-3': { + script: [ + 'echo doing step 1 of job 3', + [ + 'echo doing step 1 of job 2', + [ + 'echo doing step 1 of job 1', + 'echo doing step 2 of job 1' + ], + 'echo doing step 2 of job 2' + ], + 'echo doing step 2 of job 3' + ] + }, + test: { + script: [ + 'echo preparing to test', + [ + 'echo doing step 1 of job 3', + [ + 'echo doing step 1 of job 2', + [ + 'echo doing step 1 of job 1', + 'echo doing step 2 of job 1' + ], + 'echo doing step 2 of job 2' + ], + 'echo doing step 2 of job 3' + ], + 'echo test finished' + ] + } + }) + end + end + + context 'when templates are defined before the job' do + let(:yaml) do + <<~YML + #{yaml_templates} + #{job_yaml} + YML + end + + it_behaves_like 'expands references' + end + + context 'when templates are defined after the job' do + let(:yaml) do + <<~YML + #{job_yaml} + #{yaml_templates} + YML + end + + it_behaves_like 'expands references' + end + end + end +end |