diff options
author | Markus Doits <markus.doits@stellenticket.de> | 2018-10-19 17:47:18 +0300 |
---|---|---|
committer | Markus Doits <markus.doits@stellenticket.de> | 2018-11-07 15:04:43 +0300 |
commit | 9818bb561889579ed1031e90b7879ec1c3f0e25a (patch) | |
tree | 445a085e087644243d8e67f1bb22d04c63abcf57 /spec | |
parent | ff3484a0b331456b36f82b0d6cda2afd56da566e (diff) |
refactoring after latest feedback
Diffstat (limited to 'spec')
-rw-r--r-- | spec/lib/gitlab/ci/config/entry/retry_spec.rb | 169 | ||||
-rw-r--r-- | spec/models/ci/build_spec.rb | 50 |
2 files changed, 120 insertions, 99 deletions
diff --git a/spec/lib/gitlab/ci/config/entry/retry_spec.rb b/spec/lib/gitlab/ci/config/entry/retry_spec.rb index cc5dfb56655..4feddafb580 100644 --- a/spec/lib/gitlab/ci/config/entry/retry_spec.rb +++ b/spec/lib/gitlab/ci/config/entry/retry_spec.rb @@ -3,72 +3,125 @@ require 'spec_helper' describe Gitlab::Ci::Config::Entry::Retry do let(:entry) { described_class.new(config) } - describe 'validation' do - context 'when retry value is correct' do - context 'when it is a numeric' do - let(:config) { 2 } + shared_context 'when retry value is a numeric', :numeric do + let(:config) { max } + let(:max) {} + end - it 'is valid' do - expect(entry).to be_valid - end + shared_context 'when retry value is a hash', :hash do + let(:config) { { max: max, when: public_send(:when) }.compact } + let(:when) {} + let(:max) {} + end + + describe '#value' do + subject(:value) { entry.value } + + context 'when retry value is a numeric', :numeric do + let(:max) { 2 } + + it 'is returned as a hash with max key' do + expect(value).to eq(max: 2) end + end - context 'when it is a hash without when' do - let(:config) { { max: 2 } } + context 'when retry value is a hash', :hash do + context 'and `when` is a string' do + let(:when) { 'unknown_failure' } - it 'is valid' do - expect(entry).to be_valid + it 'returns when wrapped in an array' do + expect(value).to eq(when: ['unknown_failure']) end end - context 'when it is a hash with string when' do - let(:config) { { max: 2, when: 'unknown_failure' } } + context 'and `when` is an array' do + let(:when) { %w[unknown_failure runner_system_failure] } - it 'is valid' do - expect(entry).to be_valid + it 'returns when as it was passed' do + expect(value).to eq(when: %w[unknown_failure runner_system_failure]) end end + end + end - context 'when it is a hash with string when always' do - let(:config) { { max: 2, when: 'always' } } + describe 'validation' do + context 'when retry value is correct' do + context 'when it is a numeric', :numeric do + let(:max) { 2 } it 'is valid' do expect(entry).to be_valid end end - context 'when it is a hash with array when' do - let(:config) { { max: 2, when: %w[unknown_failure runner_system_failure] } } + context 'when it is a hash', :hash do + context 'with max' do + let(:max) { 2 } - it 'is valid' do - expect(entry).to be_valid + it 'is valid' do + expect(entry).to be_valid + end + end + + context 'with string when' do + let(:when) { 'unknown_failure' } + + it 'is valid' do + expect(entry).to be_valid + end + end + + context 'with string when always' do + let(:when) { 'always' } + + it 'is valid' do + expect(entry).to be_valid + end end - end - # Those values are documented at `doc/ci/yaml/README.md`. If any of - # those values gets invalid, documentation must be updated. To make - # sure this is catched, check explicitly that all of the documented - # values are valid. If they are not it means the documentation and this - # array must be updated. - RETRY_WHEN_IN_DOCUMENTATION = %w[ - always - unknown_failure - script_failure - api_failure - stuck_or_timeout_failure - runner_system_failure - missing_dependency_failure - runner_unsupported - ].freeze - - RETRY_WHEN_IN_DOCUMENTATION.each do |reason| - context "when it is a hash with value from documentation `#{reason}`" do - let(:config) { { max: 2, when: reason } } + context 'with array when' do + let(:when) { %w[unknown_failure runner_system_failure] } it 'is valid' do expect(entry).to be_valid end end + + # Those values are documented at `doc/ci/yaml/README.md`. If any of + # those values gets invalid, documentation must be updated. To make + # sure this is catched, check explicitly that all of the documented + # values are valid. If they are not it means the documentation and this + # array must be updated. + RETRY_WHEN_IN_DOCUMENTATION = %w[ + always + unknown_failure + script_failure + api_failure + stuck_or_timeout_failure + runner_system_failure + missing_dependency_failure + runner_unsupported + ].freeze + + RETRY_WHEN_IN_DOCUMENTATION.each do |reason| + context "with when from documentation `#{reason}`" do + let(:when) { reason } + + it 'is valid' do + expect(entry).to be_valid + end + end + end + + CommitStatus.failure_reasons.each_key do |reason| + context "with when from CommitStatus.failure_reasons `#{reason}`" do + let(:when) { reason } + + it 'is valid' do + expect(entry).to be_valid + end + end + end end end @@ -82,9 +135,9 @@ describe Gitlab::Ci::Config::Entry::Retry do end end - context 'not defined as a hash' do + context 'when it is a numeric', :numeric do context 'when it is lower than zero' do - let(:config) { -1 } + let(:max) { -1 } it 'returns error about value too low' do expect(entry).not_to be_valid @@ -94,7 +147,7 @@ describe Gitlab::Ci::Config::Entry::Retry do end context 'when it is not an integer' do - let(:config) { 1.5 } + let(:max) { 1.5 } it 'returns error about wrong value' do expect(entry).not_to be_valid @@ -103,7 +156,7 @@ describe Gitlab::Ci::Config::Entry::Retry do end context 'when the value is too high' do - let(:config) { 10 } + let(:max) { 10 } it 'returns error about value too high' do expect(entry).not_to be_valid @@ -112,7 +165,7 @@ describe Gitlab::Ci::Config::Entry::Retry do end end - context 'defined as a hash' do + context 'when it is a hash', :hash do context 'with unknown keys' do let(:config) { { max: 2, unknown_key: :something, one_more: :key } } @@ -123,8 +176,8 @@ describe Gitlab::Ci::Config::Entry::Retry do end end - context 'when max is lower than zero' do - let(:config) { { max: -1 } } + context 'with max lower than zero' do + let(:max) { -1 } it 'returns error about value too low' do expect(entry).not_to be_valid @@ -133,8 +186,8 @@ describe Gitlab::Ci::Config::Entry::Retry do end end - context 'when max is not an integer' do - let(:config) { { max: 1.5 } } + context 'with max not an integer' do + let(:max) { 1.5 } it 'returns error about wrong value' do expect(entry).not_to be_valid @@ -142,8 +195,8 @@ describe Gitlab::Ci::Config::Entry::Retry do end end - context 'when max is too high' do - let(:config) { { max: 10 } } + context 'iwth max too high' do + let(:max) { 10 } it 'returns error about value too high' do expect(entry).not_to be_valid @@ -151,8 +204,8 @@ describe Gitlab::Ci::Config::Entry::Retry do end end - context 'when when has the wrong format' do - let(:config) { { when: true } } + context 'with when in wrong format' do + let(:when) { true } it 'returns error about the wrong format' do expect(entry).not_to be_valid @@ -160,8 +213,8 @@ describe Gitlab::Ci::Config::Entry::Retry do end end - context 'when when is a string and unknown' do - let(:config) { { when: 'unknown_reason' } } + context 'with an unknown when string' do + let(:when) { 'unknown_reason' } it 'returns error about the wrong format' do expect(entry).not_to be_valid @@ -169,8 +222,8 @@ describe Gitlab::Ci::Config::Entry::Retry do end end - context 'when when is an array and includes unknown failures' do - let(:config) { { when: %w[unknown_reason runner_system_failure] } } + context 'with an unknown failure reason in a when array' do + let(:when) { %w[unknown_reason runner_system_failure] } it 'returns error about the wrong format' do expect(entry).not_to be_valid diff --git a/spec/models/ci/build_spec.rb b/spec/models/ci/build_spec.rb index 2b4923926db..8a7b06ad21c 100644 --- a/spec/models/ci/build_spec.rb +++ b/spec/models/ci/build_spec.rb @@ -1472,15 +1472,7 @@ describe Ci::Build do end describe '#retries_max' do - context 'when max retries value is defined as an integer' do - subject { create(:ci_build, options: { retry: 1 }) } - - it 'returns the number of configured max retries' do - expect(subject.retries_max).to eq 1 - end - end - - context 'when retries value is defined as a hash' do + context 'with retries max config option' do subject { create(:ci_build, options: { retry: { max: 1 } }) } it 'returns the number of configured max retries' do @@ -1488,15 +1480,7 @@ describe Ci::Build do end end - context 'when retries value is defined as a hash without max key' do - subject { create(:ci_build, options: { retry: { something: :else } }) } - - it 'returns zero' do - expect(subject.retries_max).to eq 0 - end - end - - context 'when max retries value is not defined' do + context 'without retries max config option' do subject { create(:ci_build) } it 'returns zero' do @@ -1514,34 +1498,18 @@ describe Ci::Build do end describe '#retry_when' do - context 'when value is defined without an array' do - subject { create(:ci_build, options: { retry: { when: 'something' } }) } - - it 'returns the configured value inside an array' do - expect(subject.retry_when).to eq ['something'] - end - end - - context 'when value is defined as an array' do - subject { create(:ci_build, options: { retry: { when: %w[something more] } }) } + context 'with retries when config option' do + subject { create(:ci_build, options: { retry: { when: ['some_reason'] } }) } - it 'returns the configured value' do - expect(subject.retry_when).to eq %w[something more] + it 'returns the configured when' do + expect(subject.retry_when).to eq ['some_reason'] end end - context 'when value is not defined' do + context 'without retries when config option' do subject { create(:ci_build) } - it 'returns `always`' do - expect(subject.retry_when).to eq ['always'] - end - end - - context 'when retry is only defined as an integer' do - subject { create(:ci_build, options: { retry: 1 }) } - - it 'returns `always`' do + it 'returns always array' do expect(subject.retry_when).to eq ['always'] end end @@ -3001,7 +2969,7 @@ describe Ci::Build do end context 'when build is configured to be retried' do - subject { create(:ci_build, :running, options: { retry: 3 }, project: project, user: user) } + subject { create(:ci_build, :running, options: { retry: { max: 3 } }, project: project, user: user) } it 'retries build and assigns the same user to it' do expect(described_class).to receive(:retry) |