diff options
Diffstat (limited to 'spec/lib/gitlab/ci/config/entry')
-rw-r--r-- | spec/lib/gitlab/ci/config/entry/bridge_spec.rb | 3 | ||||
-rw-r--r-- | spec/lib/gitlab/ci/config/entry/include/rules/rule_spec.rb | 90 | ||||
-rw-r--r-- | spec/lib/gitlab/ci/config/entry/include/rules_spec.rb | 98 | ||||
-rw-r--r-- | spec/lib/gitlab/ci/config/entry/include_spec.rb | 40 | ||||
-rw-r--r-- | spec/lib/gitlab/ci/config/entry/inherit/variables_spec.rb | 15 | ||||
-rw-r--r-- | spec/lib/gitlab/ci/config/entry/job_spec.rb | 15 | ||||
-rw-r--r-- | spec/lib/gitlab/ci/config/entry/jobs_spec.rb | 2 | ||||
-rw-r--r-- | spec/lib/gitlab/ci/config/entry/processable_spec.rb | 71 | ||||
-rw-r--r-- | spec/lib/gitlab/ci/config/entry/root_spec.rb | 5 | ||||
-rw-r--r-- | spec/lib/gitlab/ci/config/entry/rules_spec.rb | 72 |
10 files changed, 282 insertions, 129 deletions
diff --git a/spec/lib/gitlab/ci/config/entry/bridge_spec.rb b/spec/lib/gitlab/ci/config/entry/bridge_spec.rb index d294eca7f15..6c9c8fa5df5 100644 --- a/spec/lib/gitlab/ci/config/entry/bridge_spec.rb +++ b/spec/lib/gitlab/ci/config/entry/bridge_spec.rb @@ -106,7 +106,6 @@ RSpec.describe Gitlab::Ci::Config::Entry::Bridge do ignore: false, stage: 'test', only: { refs: %w[branches tags] }, - variables: {}, job_variables: {}, root_variables_inheritance: true, scheduling_type: :stage) @@ -131,7 +130,6 @@ RSpec.describe Gitlab::Ci::Config::Entry::Bridge do ignore: false, stage: 'test', only: { refs: %w[branches tags] }, - variables: {}, job_variables: {}, root_variables_inheritance: true, scheduling_type: :stage) @@ -287,7 +285,6 @@ RSpec.describe Gitlab::Ci::Config::Entry::Bridge do only: { refs: %w[branches tags] }, parallel: { matrix: [{ 'PROVIDER' => ['aws'], 'STACK' => %w(monitoring app1) }, { 'PROVIDER' => ['gcp'], 'STACK' => %w(data) }] }, - variables: {}, job_variables: {}, root_variables_inheritance: true, scheduling_type: :stage diff --git a/spec/lib/gitlab/ci/config/entry/include/rules/rule_spec.rb b/spec/lib/gitlab/ci/config/entry/include/rules/rule_spec.rb new file mode 100644 index 00000000000..b99048e2c18 --- /dev/null +++ b/spec/lib/gitlab/ci/config/entry/include/rules/rule_spec.rb @@ -0,0 +1,90 @@ +# frozen_string_literal: true + +require 'fast_spec_helper' + +RSpec.describe Gitlab::Ci::Config::Entry::Include::Rules::Rule do + let(:factory) do + Gitlab::Config::Entry::Factory.new(described_class) + .value(config) + end + + subject(:entry) { factory.create! } + + describe '.new' do + shared_examples 'an invalid config' do |error_message| + it { is_expected.not_to be_valid } + + it 'has errors' do + expect(entry.errors).to include(error_message) + end + end + + context 'when specifying an if: clause' do + let(:config) { { if: '$THIS || $THAT' } } + + it { is_expected.to be_valid } + end + + context 'using a list of multiple expressions' do + let(:config) { { if: ['$MY_VAR == "this"', '$YOUR_VAR == "that"'] } } + + it_behaves_like 'an invalid config', /invalid expression syntax/ + end + + context 'when specifying an invalid if: clause expression' do + let(:config) { { if: ['$MY_VAR =='] } } + + it_behaves_like 'an invalid config', /invalid expression syntax/ + end + + context 'when specifying an if: clause expression with an invalid token' do + let(:config) { { if: ['$MY_VAR == 123'] } } + + it_behaves_like 'an invalid config', /invalid expression syntax/ + end + + context 'when using invalid regex in an if: clause' do + let(:config) { { if: ['$MY_VAR =~ /some ( thing/'] } } + + it_behaves_like 'an invalid config', /invalid expression syntax/ + end + + context 'when using an if: clause with lookahead regex character "?"' do + let(:config) { { if: '$CI_COMMIT_REF =~ /^(?!master).+/' } } + + context 'when allow_unsafe_ruby_regexp is disabled' do + it_behaves_like 'an invalid config', /invalid expression syntax/ + end + end + + context 'when specifying unknown policy' do + let(:config) { { invalid: :something } } + + it_behaves_like 'an invalid config', /unknown keys: invalid/ + end + + context 'when clause is empty' do + let(:config) { {} } + + it_behaves_like 'an invalid config', /can't be blank/ + end + + context 'when policy strategy does not match' do + let(:config) { 'string strategy' } + + it_behaves_like 'an invalid config', /should be a hash/ + end + end + + describe '#value' do + subject(:value) { entry.value } + + context 'when specifying an if: clause' do + let(:config) { { if: '$THIS || $THAT' } } + + it 'returns the config' do + expect(subject).to eq(if: '$THIS || $THAT') + end + end + end +end diff --git a/spec/lib/gitlab/ci/config/entry/include/rules_spec.rb b/spec/lib/gitlab/ci/config/entry/include/rules_spec.rb new file mode 100644 index 00000000000..c255d6e9dd6 --- /dev/null +++ b/spec/lib/gitlab/ci/config/entry/include/rules_spec.rb @@ -0,0 +1,98 @@ +# frozen_string_literal: true + +require 'fast_spec_helper' + +RSpec.describe Gitlab::Ci::Config::Entry::Include::Rules do + let(:factory) do + Gitlab::Config::Entry::Factory.new(described_class) + .value(config) + end + + subject(:entry) { factory.create! } + + describe '.new' do + shared_examples 'a valid config' do + it { is_expected.to be_valid } + + context 'when composed' do + before do + entry.compose! + end + + it { is_expected.to be_valid } + end + end + + shared_examples 'an invalid config' do |error_message| + it { is_expected.not_to be_valid } + + it 'has errors' do + expect(entry.errors).to include(error_message) + end + end + + context 'with an "if"' do + let(:config) do + [{ if: '$THIS == "that"' }] + end + + it_behaves_like 'a valid config' + end + + context 'with a "changes"' do + let(:config) do + [{ changes: ['filename.txt'] }] + end + + context 'when composed' do + before do + entry.compose! + end + + it_behaves_like 'an invalid config', /contains unknown keys: changes/ + end + end + + context 'with a list of two rules' do + let(:config) do + [ + { if: '$THIS == "that"' }, + { if: '$SKIP' } + ] + end + + it_behaves_like 'a valid config' + end + + context 'without an array' do + let(:config) do + { if: '$SKIP' } + end + + it_behaves_like 'an invalid config', /should be a array/ + end + end + + describe '#value' do + subject(:value) { entry.value } + + context 'with an "if"' do + let(:config) do + [{ if: '$THIS == "that"' }] + end + + it { is_expected.to eq(config) } + end + + context 'with a list of two rules' do + let(:config) do + [ + { if: '$THIS == "that"' }, + { if: '$SKIP' } + ] + end + + it { is_expected.to eq(config) } + end + end +end diff --git a/spec/lib/gitlab/ci/config/entry/include_spec.rb b/spec/lib/gitlab/ci/config/entry/include_spec.rb index 59f0b0e7a48..275cdcddeb0 100644 --- a/spec/lib/gitlab/ci/config/entry/include_spec.rb +++ b/spec/lib/gitlab/ci/config/entry/include_spec.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require 'spec_helper' +require 'fast_spec_helper' RSpec.describe ::Gitlab::Ci::Config::Entry::Include do subject(:include_entry) { described_class.new(config) } @@ -86,6 +86,22 @@ RSpec.describe ::Gitlab::Ci::Config::Entry::Include do end end end + + context 'when using with "rules"' do + let(:config) { { local: 'test.yml', rules: [{ if: '$VARIABLE' }] } } + + it { is_expected.to be_valid } + + context 'when rules is not an array of hashes' do + let(:config) { { local: 'test.yml', rules: ['$VARIABLE'] } } + + it { is_expected.not_to be_valid } + + it 'has specific error' do + expect(include_entry.errors).to include('include rules should be an array of hashes') + end + end + end end context 'when value is something else' do @@ -94,4 +110,26 @@ RSpec.describe ::Gitlab::Ci::Config::Entry::Include do it { is_expected.not_to be_valid } end end + + describe '#value' do + subject(:value) { include_entry.value } + + context 'when config is a string' do + let(:config) { 'test.yml' } + + it { is_expected.to eq('test.yml') } + end + + context 'when config is a hash' do + let(:config) { { local: 'test.yml' } } + + it { is_expected.to eq(local: 'test.yml') } + end + + context 'when config has "rules"' do + let(:config) { { local: 'test.yml', rules: [{ if: '$VARIABLE' }] } } + + it { is_expected.to eq(local: 'test.yml', rules: [{ if: '$VARIABLE' }]) } + end + end end diff --git a/spec/lib/gitlab/ci/config/entry/inherit/variables_spec.rb b/spec/lib/gitlab/ci/config/entry/inherit/variables_spec.rb index b1a8fbcdbe0..bdb4d25c142 100644 --- a/spec/lib/gitlab/ci/config/entry/inherit/variables_spec.rb +++ b/spec/lib/gitlab/ci/config/entry/inherit/variables_spec.rb @@ -24,19 +24,4 @@ RSpec.describe ::Gitlab::Ci::Config::Entry::Inherit::Variables do end end end - - describe '#inherit?' do - where(:config, :inherit) do - true | true - false | false - %w[A] | true - %w[B] | false - end - - with_them do - it do - expect(subject.inherit?('A')).to eq(inherit) - end - end - 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 1d23ab0c2c7..5b47d3a3922 100644 --- a/spec/lib/gitlab/ci/config/entry/job_spec.rb +++ b/spec/lib/gitlab/ci/config/entry/job_spec.rb @@ -434,20 +434,6 @@ RSpec.describe Gitlab::Ci::Config::Entry::Job do expect(entry.errors).to include 'job dependencies the another-job should be part of needs' end end - - context 'when stage: is missing' do - let(:config) do - { - script: 'echo', - needs: ['build-job'] - } - end - - it 'returns error about invalid data' do - expect(entry).not_to be_valid - expect(entry.errors).to include 'job config missing required keys: stage' - end - end end context 'when timeout value is not correct' do @@ -626,7 +612,6 @@ RSpec.describe Gitlab::Ci::Config::Entry::Job do ignore: false, after_script: %w[cleanup], only: { refs: %w[branches tags] }, - variables: {}, job_variables: {}, root_variables_inheritance: true, scheduling_type: :stage) diff --git a/spec/lib/gitlab/ci/config/entry/jobs_spec.rb b/spec/lib/gitlab/ci/config/entry/jobs_spec.rb index cb73044b62b..9a2a67389fc 100644 --- a/spec/lib/gitlab/ci/config/entry/jobs_spec.rb +++ b/spec/lib/gitlab/ci/config/entry/jobs_spec.rb @@ -99,7 +99,6 @@ RSpec.describe Gitlab::Ci::Config::Entry::Jobs do only: { refs: %w[branches tags] }, stage: 'test', trigger: { project: 'my/project' }, - variables: {}, job_variables: {}, root_variables_inheritance: true, scheduling_type: :stage @@ -110,7 +109,6 @@ RSpec.describe Gitlab::Ci::Config::Entry::Jobs do only: { refs: %w[branches tags] }, script: ['something'], stage: 'test', - variables: {}, job_variables: {}, root_variables_inheritance: true, scheduling_type: :stage diff --git a/spec/lib/gitlab/ci/config/entry/processable_spec.rb b/spec/lib/gitlab/ci/config/entry/processable_spec.rb index f98a6a869d6..b872f6644a2 100644 --- a/spec/lib/gitlab/ci/config/entry/processable_spec.rb +++ b/spec/lib/gitlab/ci/config/entry/processable_spec.rb @@ -362,76 +362,6 @@ RSpec.describe Gitlab::Ci::Config::Entry::Processable do end context 'with inheritance' do - context 'of variables' do - let(:config) do - { variables: { A: 'job', B: 'job' } } - end - - before do - entry.compose!(deps) - end - - context 'with only job variables' do - it 'does return defined variables' do - expect(entry.value).to include( - variables: { 'A' => 'job', 'B' => 'job' }, - job_variables: { 'A' => 'job', 'B' => 'job' }, - root_variables_inheritance: true - ) - end - end - - context 'when root yaml variables are used' do - let(:variables) do - Gitlab::Ci::Config::Entry::Variables.new( - { A: 'root', C: 'root', D: 'root' } - ).value - end - - it 'does return job and root variables' do - expect(entry.value).to include( - variables: { 'A' => 'job', 'B' => 'job', 'C' => 'root', 'D' => 'root' }, - job_variables: { 'A' => 'job', 'B' => 'job' }, - root_variables_inheritance: true - ) - end - - context 'when inherit of defaults is disabled' do - let(:config) do - { - variables: { A: 'job', B: 'job' }, - inherit: { variables: false } - } - end - - it 'does return job and root variables' do - expect(entry.value).to include( - variables: { 'A' => 'job', 'B' => 'job' }, - job_variables: { 'A' => 'job', 'B' => 'job' }, - root_variables_inheritance: false - ) - end - end - - context 'when inherit of only specific variable is enabled' do - let(:config) do - { - variables: { A: 'job', B: 'job' }, - inherit: { variables: ['D'] } - } - end - - it 'does return job and root variables' do - expect(entry.value).to include( - variables: { 'A' => 'job', 'B' => 'job', 'D' => 'root' }, - job_variables: { 'A' => 'job', 'B' => 'job' }, - root_variables_inheritance: ['D'] - ) - end - end - end - end - context 'of default:tags' do using RSpec::Parameterized::TableSyntax @@ -493,7 +423,6 @@ RSpec.describe Gitlab::Ci::Config::Entry::Processable do name: :rspec, stage: 'test', only: { refs: %w[branches tags] }, - variables: {}, job_variables: {}, root_variables_inheritance: true ) diff --git a/spec/lib/gitlab/ci/config/entry/root_spec.rb b/spec/lib/gitlab/ci/config/entry/root_spec.rb index 31e3545e8d8..d862fbf5b78 100644 --- a/spec/lib/gitlab/ci/config/entry/root_spec.rb +++ b/spec/lib/gitlab/ci/config/entry/root_spec.rb @@ -132,7 +132,6 @@ 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', 'VAR2' => 'val 2' }, job_variables: {}, root_variables_inheritance: true, ignore: false, @@ -148,7 +147,6 @@ 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', 'VAR2' => 'val 2' }, job_variables: {}, root_variables_inheritance: true, ignore: false, @@ -166,7 +164,6 @@ 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', 'VAR2' => 'val 2' }, job_variables: { 'VAR' => 'job' }, root_variables_inheritance: true, after_script: [], @@ -214,7 +211,6 @@ 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' }, job_variables: {}, root_variables_inheritance: true, ignore: false, @@ -228,7 +224,6 @@ 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' => 'job' }, job_variables: { 'VAR' => 'job' }, root_variables_inheritance: true, ignore: false, diff --git a/spec/lib/gitlab/ci/config/entry/rules_spec.rb b/spec/lib/gitlab/ci/config/entry/rules_spec.rb index 7d26365e7b3..91252378541 100644 --- a/spec/lib/gitlab/ci/config/entry/rules_spec.rb +++ b/spec/lib/gitlab/ci/config/entry/rules_spec.rb @@ -17,6 +17,10 @@ RSpec.describe Gitlab::Ci::Config::Entry::Rules do describe '.new' do subject { entry } + before do + subject.compose! + end + context 'with a list of rule rule' do let(:config) do [{ if: '$THIS == "that"', when: 'never' }] @@ -24,14 +28,6 @@ RSpec.describe Gitlab::Ci::Config::Entry::Rules do it { is_expected.to be_a(described_class) } it { is_expected.to be_valid } - - context 'when composed' do - before do - subject.compose! - end - - it { is_expected.to be_valid } - end end context 'with a list of two rules' do @@ -42,21 +38,34 @@ RSpec.describe Gitlab::Ci::Config::Entry::Rules do ] end - it { is_expected.to be_a(described_class) } it { is_expected.to be_valid } + end - context 'when composed' do - before do - subject.compose! - end + context 'with a single rule object' do + let(:config) do + { if: '$SKIP', when: 'never' } + end - it { is_expected.to be_valid } + it { is_expected.not_to be_valid } + end + + context 'with nested rules' do + let(:config) do + [ + { if: '$THIS == "that"', when: 'always' }, + [{ if: '$SKIP', when: 'never' }] + ] end + + it { is_expected.to be_valid } end - context 'with a single rule object' do + context 'with rules nested more than one level' do let(:config) do - { if: '$SKIP', when: 'never' } + [ + { if: '$THIS == "that"', when: 'always' }, + [{ if: '$SKIP', when: 'never' }, [{ if: '$THIS == "other"', when: 'aways' }]] + ] end it { is_expected.not_to be_valid } @@ -90,7 +99,36 @@ RSpec.describe Gitlab::Ci::Config::Entry::Rules do { if: '$SKIP', when: 'never' } end - it { is_expected.to eq(config) } + it { is_expected.to eq([config]) } + end + + context 'with nested rules' do + let(:first_rule) { { if: '$THIS == "that"', when: 'always' } } + let(:second_rule) { { if: '$SKIP', when: 'never' } } + + let(:config) do + [ + first_rule, + [second_rule] + ] + end + + it { is_expected.to contain_exactly(first_rule, second_rule) } + end + + context 'with rules nested more than one level' do + let(:first_rule) { { if: '$THIS == "that"', when: 'always' } } + let(:second_rule) { { if: '$SKIP', when: 'never' } } + let(:third_rule) { { if: '$THIS == "other"', when: 'aways' } } + + let(:config) do + [ + first_rule, + [second_rule, [third_rule]] + ] + end + + it { is_expected.to contain_exactly(first_rule, second_rule, third_rule) } end end |