diff options
Diffstat (limited to 'spec/lib/gitlab/ci/config/entry')
-rw-r--r-- | spec/lib/gitlab/ci/config/entry/allow_failure_spec.rb | 92 | ||||
-rw-r--r-- | spec/lib/gitlab/ci/config/entry/bridge_spec.rb | 17 | ||||
-rw-r--r-- | spec/lib/gitlab/ci/config/entry/image_spec.rb | 2 | ||||
-rw-r--r-- | spec/lib/gitlab/ci/config/entry/job_spec.rb | 48 | ||||
-rw-r--r-- | spec/lib/gitlab/ci/config/entry/need_spec.rb | 39 | ||||
-rw-r--r-- | spec/lib/gitlab/ci/config/entry/needs_spec.rb | 23 | ||||
-rw-r--r-- | spec/lib/gitlab/ci/config/entry/processable_spec.rb | 2 | ||||
-rw-r--r-- | spec/lib/gitlab/ci/config/entry/root_spec.rb | 12 | ||||
-rw-r--r-- | spec/lib/gitlab/ci/config/entry/rules/rule_spec.rb | 16 | ||||
-rw-r--r-- | spec/lib/gitlab/ci/config/entry/service_spec.rb | 2 | ||||
-rw-r--r-- | spec/lib/gitlab/ci/config/entry/services_spec.rb | 2 | ||||
-rw-r--r-- | spec/lib/gitlab/ci/config/entry/variables_spec.rb | 54 |
12 files changed, 279 insertions, 30 deletions
diff --git a/spec/lib/gitlab/ci/config/entry/allow_failure_spec.rb b/spec/lib/gitlab/ci/config/entry/allow_failure_spec.rb new file mode 100644 index 00000000000..7aaad57f5cd --- /dev/null +++ b/spec/lib/gitlab/ci/config/entry/allow_failure_spec.rb @@ -0,0 +1,92 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::Ci::Config::Entry::AllowFailure do + let(:entry) { described_class.new(config.deep_dup) } + let(:expected_config) { config } + + describe 'validations' do + context 'when entry config value is valid' do + shared_examples 'valid entry' do + describe '#value' do + it 'returns key value' do + expect(entry.value).to eq(expected_config) + end + end + + describe '#valid?' do + it 'is valid' do + expect(entry).to be_valid + end + end + end + + context 'with boolean values' do + it_behaves_like 'valid entry' do + let(:config) { true } + end + + it_behaves_like 'valid entry' do + let(:config) { false } + end + end + + context 'with hash values' do + it_behaves_like 'valid entry' do + let(:config) { { exit_codes: 137 } } + let(:expected_config) { { exit_codes: [137] } } + end + + it_behaves_like 'valid entry' do + let(:config) { { exit_codes: [42, 137] } } + end + end + end + + context 'when entry value is not valid' do + shared_examples 'invalid entry' do + describe '#valid?' do + it { expect(entry).not_to be_valid } + it { expect(entry.errors).to include(error_message) } + end + end + + context 'when it has a wrong type' do + let(:config) { [1] } + let(:error_message) do + 'allow failure config should be a hash or a boolean value' + end + + it_behaves_like 'invalid entry' + end + + context 'with string exit codes' do + let(:config) { { exit_codes: 'string' } } + let(:error_message) do + 'allow failure exit codes should be an array of integers or an integer' + end + + it_behaves_like 'invalid entry' + end + + context 'with array of strings as exit codes' do + let(:config) { { exit_codes: ['string 1', 'string 2'] } } + let(:error_message) do + 'allow failure exit codes should be an array of integers or an integer' + end + + it_behaves_like 'invalid entry' + end + + context 'when it has an extra keys' do + let(:config) { { extra: true } } + let(:error_message) do + 'allow failure config contains unknown keys: extra' + end + + it_behaves_like 'invalid entry' + end + end + end +end diff --git a/spec/lib/gitlab/ci/config/entry/bridge_spec.rb b/spec/lib/gitlab/ci/config/entry/bridge_spec.rb index 8b2e0410474..b3b7901074a 100644 --- a/spec/lib/gitlab/ci/config/entry/bridge_spec.rb +++ b/spec/lib/gitlab/ci/config/entry/bridge_spec.rb @@ -227,6 +227,23 @@ RSpec.describe Gitlab::Ci::Config::Entry::Bridge do end end end + + context 'when bridge config contains exit_codes' do + let(:config) do + { script: 'rspec', allow_failure: { exit_codes: [42] } } + end + + describe '#valid?' do + it { is_expected.not_to be_valid } + end + + describe '#errors' do + it 'returns an error message' do + expect(subject.errors) + .to include(/allow failure should be a boolean value/) + end + end + end end describe '#manual_action?' do diff --git a/spec/lib/gitlab/ci/config/entry/image_spec.rb b/spec/lib/gitlab/ci/config/entry/image_spec.rb index c3d91057328..e810d65d560 100644 --- a/spec/lib/gitlab/ci/config/entry/image_spec.rb +++ b/spec/lib/gitlab/ci/config/entry/image_spec.rb @@ -81,7 +81,7 @@ RSpec.describe Gitlab::Ci::Config::Entry::Image do context 'when configuration has ports' do let(:ports) { [{ number: 80, protocol: 'http', name: 'foobar' }] } let(:config) { { name: 'ruby:2.7', entrypoint: %w(/bin/sh run), ports: ports } } - let(:entry) { described_class.new(config, { with_image_ports: image_ports }) } + let(:entry) { described_class.new(config, with_image_ports: image_ports) } let(:image_ports) { false } context 'when with_image_ports metadata is not enabled' do diff --git a/spec/lib/gitlab/ci/config/entry/job_spec.rb b/spec/lib/gitlab/ci/config/entry/job_spec.rb index e0e8bc93770..7834a1a94f2 100644 --- a/spec/lib/gitlab/ci/config/entry/job_spec.rb +++ b/spec/lib/gitlab/ci/config/entry/job_spec.rb @@ -670,6 +670,10 @@ RSpec.describe Gitlab::Ci::Config::Entry::Job do end describe '#ignored?' do + before do + entry.compose! + end + context 'when job is a manual action' do context 'when it is not specified if job is allowed to fail' do let(:config) do @@ -700,6 +704,16 @@ RSpec.describe Gitlab::Ci::Config::Entry::Job do expect(entry).not_to be_ignored end end + + context 'when job is dynamically allowed to fail' do + let(:config) do + { script: 'deploy', when: 'manual', allow_failure: { exit_codes: 42 } } + end + + it 'is not an ignored job' do + expect(entry).not_to be_ignored + end + end end context 'when job is not a manual action' do @@ -709,6 +723,10 @@ RSpec.describe Gitlab::Ci::Config::Entry::Job do it 'is not an ignored job' do expect(entry).not_to be_ignored end + + it 'does not return allow_failure' do + expect(entry.value.key?(:allow_failure_criteria)).to be_falsey + end end context 'when job is allowed to fail' do @@ -717,6 +735,10 @@ RSpec.describe Gitlab::Ci::Config::Entry::Job do it 'is an ignored job' do expect(entry).to be_ignored end + + it 'does not return allow_failure_criteria' do + expect(entry.value.key?(:allow_failure_criteria)).to be_falsey + end end context 'when job is not allowed to fail' do @@ -725,6 +747,32 @@ RSpec.describe Gitlab::Ci::Config::Entry::Job do it 'is not an ignored job' do expect(entry).not_to be_ignored end + + it 'does not return allow_failure_criteria' do + expect(entry.value.key?(:allow_failure_criteria)).to be_falsey + end + end + + context 'when job is dynamically allowed to fail' do + let(:config) { { script: 'deploy', allow_failure: { exit_codes: 42 } } } + + it 'is not an ignored job' do + expect(entry).not_to be_ignored + end + + 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/need_spec.rb b/spec/lib/gitlab/ci/config/entry/need_spec.rb index 5a826bf8282..983e95fae42 100644 --- a/spec/lib/gitlab/ci/config/entry/need_spec.rb +++ b/spec/lib/gitlab/ci/config/entry/need_spec.rb @@ -165,6 +165,45 @@ RSpec.describe ::Gitlab::Ci::Config::Entry::Need do end end + context 'with cross pipeline artifacts needs' do + context 'when pipeline is provided' do + context 'when job is provided' do + let(:config) { { job: 'job_name', pipeline: '$THE_PIPELINE_ID' } } + + it { is_expected.to be_valid } + + it 'sets artifacts:true by default' do + expect(need.value).to eq(job: 'job_name', pipeline: '$THE_PIPELINE_ID', artifacts: true) + end + + it 'sets the type as cross_dependency' do + expect(need.type).to eq(:cross_dependency) + end + end + + context 'when artifacts is provided' do + let(:config) { { job: 'job_name', pipeline: '$THE_PIPELINE_ID', artifacts: false } } + + it { is_expected.to be_valid } + + it 'returns the correct value' do + expect(need.value).to eq(job: 'job_name', pipeline: '$THE_PIPELINE_ID', artifacts: false) + end + end + end + + context 'when config contains not allowed keys' do + let(:config) { { job: 'job_name', pipeline: '$THE_PIPELINE_ID', something: 'else' } } + + it { is_expected.not_to be_valid } + + it 'returns an error' do + expect(need.errors) + .to contain_exactly('cross pipeline dependency config contains unknown keys: something') + end + end + end + context 'when need config is not a string or a hash' do let(:config) { :job_name } diff --git a/spec/lib/gitlab/ci/config/entry/needs_spec.rb b/spec/lib/gitlab/ci/config/entry/needs_spec.rb index f3b9d0c3c84..f11f2a56f5f 100644 --- a/spec/lib/gitlab/ci/config/entry/needs_spec.rb +++ b/spec/lib/gitlab/ci/config/entry/needs_spec.rb @@ -6,7 +6,7 @@ RSpec.describe ::Gitlab::Ci::Config::Entry::Needs do subject(:needs) { described_class.new(config) } before do - needs.metadata[:allowed_needs] = %i[job] + needs.metadata[:allowed_needs] = %i[job cross_dependency] end describe 'validations' do @@ -66,6 +66,27 @@ RSpec.describe ::Gitlab::Ci::Config::Entry::Needs do end end end + + context 'with too many cross pipeline dependencies' do + let(:limit) { described_class::NEEDS_CROSS_PIPELINE_DEPENDENCIES_LIMIT } + + let(:config) do + Array.new(limit.next) do |index| + { pipeline: "$UPSTREAM_PIPELINE_#{index}", job: 'job-1' } + end + end + + describe '#valid?' do + it { is_expected.not_to be_valid } + end + + describe '#errors' do + it 'returns error about incorrect type' do + expect(needs.errors).to contain_exactly( + "needs config must be less than or equal to #{limit}") + end + end + end end describe '.compose!' do diff --git a/spec/lib/gitlab/ci/config/entry/processable_spec.rb b/spec/lib/gitlab/ci/config/entry/processable_spec.rb index ac8dd2a3267..aadf94365c6 100644 --- a/spec/lib/gitlab/ci/config/entry/processable_spec.rb +++ b/spec/lib/gitlab/ci/config/entry/processable_spec.rb @@ -361,7 +361,7 @@ RSpec.describe Gitlab::Ci::Config::Entry::Processable do context 'when root yaml variables are used' do let(:variables) do Gitlab::Ci::Config::Entry::Variables.new( - A: 'root', C: 'root', D: 'root' + { A: 'root', C: 'root', D: 'root' } ).value end diff --git a/spec/lib/gitlab/ci/config/entry/root_spec.rb b/spec/lib/gitlab/ci/config/entry/root_spec.rb index 79716df6b60..54c7a5c3602 100644 --- a/spec/lib/gitlab/ci/config/entry/root_spec.rb +++ b/spec/lib/gitlab/ci/config/entry/root_spec.rb @@ -32,7 +32,7 @@ RSpec.describe Gitlab::Ci::Config::Entry::Root do image: 'ruby:2.7', default: {}, services: ['postgres:9.1', 'mysql:5.5'], - variables: { VAR: 'root' }, + variables: { VAR: 'root', VAR2: { value: 'val 2', description: 'this is var 2' } }, after_script: ['make clean'], stages: %w(build pages release), cache: { key: 'k', untracked: true, paths: ['public/'] }, @@ -80,6 +80,10 @@ RSpec.describe Gitlab::Ci::Config::Entry::Root do .to eq 'List of external YAML files to include.' end + it 'sets correct variables value' do + expect(root.variables_value).to eq('VAR' => 'root', 'VAR2' => 'val 2') + end + describe '#leaf?' do it 'is not leaf' do expect(root).not_to be_leaf @@ -128,7 +132,7 @@ RSpec.describe Gitlab::Ci::Config::Entry::Root do services: [{ name: 'postgres:9.1' }, { name: 'mysql:5.5' }], stage: 'test', cache: { key: 'k', untracked: true, paths: ['public/'], policy: 'pull-push', when: 'on_success' }, - variables: { 'VAR' => 'root' }, + variables: { 'VAR' => 'root', 'VAR2' => 'val 2' }, ignore: false, after_script: ['make clean'], only: { refs: %w[branches tags] }, @@ -142,7 +146,7 @@ RSpec.describe Gitlab::Ci::Config::Entry::Root do services: [{ name: 'postgres:9.1' }, { name: 'mysql:5.5' }], stage: 'test', cache: { key: 'k', untracked: true, paths: ['public/'], policy: 'pull-push', when: 'on_success' }, - variables: { 'VAR' => 'root' }, + variables: { 'VAR' => 'root', 'VAR2' => 'val 2' }, ignore: false, after_script: ['make clean'], only: { refs: %w[branches tags] }, @@ -158,7 +162,7 @@ RSpec.describe Gitlab::Ci::Config::Entry::Root do services: [{ name: "postgres:9.1" }, { name: "mysql:5.5" }], cache: { key: "k", untracked: true, paths: ["public/"], policy: "pull-push", when: 'on_success' }, only: { refs: %w(branches tags) }, - variables: { 'VAR' => 'job' }, + variables: { 'VAR' => 'job', 'VAR2' => 'val 2' }, after_script: [], ignore: false, scheduling_type: :stage } diff --git a/spec/lib/gitlab/ci/config/entry/rules/rule_spec.rb b/spec/lib/gitlab/ci/config/entry/rules/rule_spec.rb index 4a43e6c9a86..d1bd22e5573 100644 --- a/spec/lib/gitlab/ci/config/entry/rules/rule_spec.rb +++ b/spec/lib/gitlab/ci/config/entry/rules/rule_spec.rb @@ -339,6 +339,22 @@ RSpec.describe Gitlab::Ci::Config::Entry::Rules::Rule do end end end + + context 'with an invalid variables' do + let(:config) do + { if: '$THIS == "that"', variables: 'hello' } + end + + before do + subject.compose! + end + + it { is_expected.not_to be_valid } + + it 'returns an error about invalid variables:' do + expect(subject.errors).to include(/variables config should be a hash of key value pairs/) + end + end end context 'allow_failure: validation' do diff --git a/spec/lib/gitlab/ci/config/entry/service_spec.rb b/spec/lib/gitlab/ci/config/entry/service_spec.rb index ec137ef2ae4..2795cc9dddf 100644 --- a/spec/lib/gitlab/ci/config/entry/service_spec.rb +++ b/spec/lib/gitlab/ci/config/entry/service_spec.rb @@ -96,7 +96,7 @@ RSpec.describe Gitlab::Ci::Config::Entry::Service do { name: 'postgresql:9.5', alias: 'db', command: %w(cmd run), entrypoint: %w(/bin/sh run), ports: ports } end - let(:entry) { described_class.new(config, { with_image_ports: image_ports }) } + let(:entry) { described_class.new(config, with_image_ports: image_ports) } let(:image_ports) { false } context 'when with_image_ports metadata is not enabled' do diff --git a/spec/lib/gitlab/ci/config/entry/services_spec.rb b/spec/lib/gitlab/ci/config/entry/services_spec.rb index e4f8a348d21..85e7f297b03 100644 --- a/spec/lib/gitlab/ci/config/entry/services_spec.rb +++ b/spec/lib/gitlab/ci/config/entry/services_spec.rb @@ -38,7 +38,7 @@ RSpec.describe Gitlab::Ci::Config::Entry::Services do context 'when configuration has ports' do let(:ports) { [{ number: 80, protocol: 'http', name: 'foobar' }] } let(:config) { ['postgresql:9.5', { name: 'postgresql:9.1', alias: 'postgres_old', ports: ports }] } - let(:entry) { described_class.new(config, { with_image_ports: image_ports }) } + let(:entry) { described_class.new(config, with_image_ports: image_ports) } let(:image_ports) { false } context 'when with_image_ports metadata is not enabled' do diff --git a/spec/lib/gitlab/ci/config/entry/variables_spec.rb b/spec/lib/gitlab/ci/config/entry/variables_spec.rb index ac33f858f43..426a38e2ef7 100644 --- a/spec/lib/gitlab/ci/config/entry/variables_spec.rb +++ b/spec/lib/gitlab/ci/config/entry/variables_spec.rb @@ -3,7 +3,9 @@ require 'spec_helper' RSpec.describe Gitlab::Ci::Config::Entry::Variables do - subject { described_class.new(config) } + let(:metadata) { {} } + + subject { described_class.new(config, metadata) } shared_examples 'valid config' do describe '#value' do @@ -71,7 +73,13 @@ RSpec.describe Gitlab::Ci::Config::Entry::Variables do { 'VARIABLE_1' => 'value 1', 'VARIABLE_2' => 'value 2' } end - it_behaves_like 'valid config' + it_behaves_like 'invalid config' + + context 'when metadata has use_value_data' do + let(:metadata) { { use_value_data: true } } + + it_behaves_like 'valid config' + end end context 'when entry value is an array' do @@ -80,32 +88,36 @@ RSpec.describe Gitlab::Ci::Config::Entry::Variables do it_behaves_like 'invalid config' end - context 'when entry value has hash with other key-pairs' do - let(:config) do - { 'VARIABLE_1' => { value: 'value 1', hello: 'variable 1' }, - 'VARIABLE_2' => 'value 2' } - end + context 'when metadata has use_value_data' do + let(:metadata) { { use_value_data: true } } - it_behaves_like 'invalid config' - end + context 'when entry value has hash with other key-pairs' do + let(:config) do + { 'VARIABLE_1' => { value: 'value 1', hello: 'variable 1' }, + 'VARIABLE_2' => 'value 2' } + end - context 'when entry config value has hash with nil description' do - let(:config) do - { 'VARIABLE_1' => { value: 'value 1', description: nil } } + it_behaves_like 'invalid config' end - it_behaves_like 'invalid config' - end + context 'when entry config value has hash with nil description' do + let(:config) do + { 'VARIABLE_1' => { value: 'value 1', description: nil } } + end - context 'when entry config value has hash without description' do - let(:config) do - { 'VARIABLE_1' => { value: 'value 1' } } + it_behaves_like 'invalid config' end - let(:result) do - { 'VARIABLE_1' => 'value 1' } - end + context 'when entry config value has hash without description' do + let(:config) do + { 'VARIABLE_1' => { value: 'value 1' } } + end - it_behaves_like 'valid config' + let(:result) do + { 'VARIABLE_1' => 'value 1' } + end + + it_behaves_like 'valid config' + end end end |