diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2024-01-16 18:06:47 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2024-01-16 18:06:47 +0300 |
commit | cb8835f38a3e4c188e9a73adf45936e2a95f40ae (patch) | |
tree | 60c25b80fbcf5deb25b9bb00539564b8296858f6 /spec/lib | |
parent | 218585fc850159e0cf7fa4b609f0837cb5f29599 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/lib')
-rw-r--r-- | spec/lib/bitbucket/connection_spec.rb | 20 | ||||
-rw-r--r-- | spec/lib/gitlab/ci/config/interpolation/text_interpolator_spec.rb | 151 | ||||
-rw-r--r-- | spec/lib/gitlab/ci/config/yaml/documents_spec.rb | 71 | ||||
-rw-r--r-- | spec/lib/gitlab/config/loader/yaml_spec.rb | 6 |
4 files changed, 117 insertions, 131 deletions
diff --git a/spec/lib/bitbucket/connection_spec.rb b/spec/lib/bitbucket/connection_spec.rb index 6cf010f2eed..a5033f6465d 100644 --- a/spec/lib/bitbucket/connection_spec.rb +++ b/spec/lib/bitbucket/connection_spec.rb @@ -56,26 +56,6 @@ RSpec.describe Bitbucket::Connection, feature_category: :integrations do expect { connection.get('/users') }.to raise_error(Bitbucket::ExponentialBackoff::RateLimitError) end end - - context 'when the bitbucket_importer_exponential_backoff feature flag is disabled' do - before do - stub_feature_flags(bitbucket_importer_exponential_backoff: false) - end - - it 'does not run with exponential backoff' do - expect_next_instance_of(described_class) do |instance| - expect(instance).not_to receive(:retry_with_exponential_backoff).and_call_original - end - - expect_next_instance_of(OAuth2::AccessToken) do |instance| - expect(instance).to receive(:get).and_return(double(parsed: true)) - end - - connection = described_class.new({ token: token }) - - connection.get('/users') - end - end end describe '#expired?' do diff --git a/spec/lib/gitlab/ci/config/interpolation/text_interpolator_spec.rb b/spec/lib/gitlab/ci/config/interpolation/text_interpolator_spec.rb index 70858c0fff8..8655d3fb0b7 100644 --- a/spec/lib/gitlab/ci/config/interpolation/text_interpolator_spec.rb +++ b/spec/lib/gitlab/ci/config/interpolation/text_interpolator_spec.rb @@ -3,23 +3,14 @@ require 'spec_helper' RSpec.describe Gitlab::Ci::Config::Interpolation::TextInterpolator, feature_category: :pipeline_composition do - let(:result) { ::Gitlab::Ci::Config::Yaml::Result.new(config: [header, content]) } + let(:arguments) { { website: 'gitlab.com' } } + let(:content) { ::Gitlab::Config::Loader::Yaml.new("test: 'deploy $[[ inputs.website ]]'") } + let(:header) { ::Gitlab::Config::Loader::Yaml.new("spec:\n inputs:\n website: ") } + let(:documents) { ::Gitlab::Ci::Config::Yaml::Documents.new([header, content]) } - subject(:interpolator) { described_class.new(result, arguments, []) } + subject(:interpolator) { described_class.new(documents, arguments, []) } context 'when input data is valid' do - let(:header) do - { spec: { inputs: { website: nil } } } - end - - let(:content) do - "test: 'deploy $[[ inputs.website ]]'" - end - - let(:arguments) do - { website: 'gitlab.com' } - end - it 'correctly interpolates the config' do interpolator.interpolate! @@ -29,14 +20,24 @@ RSpec.describe Gitlab::Ci::Config::Interpolation::TextInterpolator, feature_cate end end - context 'when config has a syntax error' do - let(:result) { ::Gitlab::Ci::Config::Yaml::Result.new(error: 'Invalid configuration format') } + context 'when interpolation is not used' do + let(:arguments) { nil } + let(:content) { ::Gitlab::Config::Loader::Yaml.new("test: 'deploy production'") } + let(:documents) { ::Gitlab::Ci::Config::Yaml::Documents.new([content]) } - let(:arguments) do - { website: 'gitlab.com' } + it 'returns original content' do + interpolator.interpolate! + + expect(interpolator).not_to be_interpolated + expect(interpolator).to be_valid + expect(interpolator.to_result).to eq("test: 'deploy production'") end + end + + context 'when the header has an error while being parsed' do + let(:header) { ::Gitlab::Config::Loader::Yaml.new('_!@malformedyaml:&') } - it 'surfaces an error about invalid config' do + it 'surfaces the error' do interpolator.interpolate! expect(interpolator).not_to be_valid @@ -45,9 +46,7 @@ RSpec.describe Gitlab::Ci::Config::Interpolation::TextInterpolator, feature_cate end context 'when spec header is missing but inputs are specified' do - let(:header) { nil } - let(:content) { "test: 'echo'" } - let(:arguments) { { foo: 'bar' } } + let(:documents) { ::Gitlab::Ci::Config::Yaml::Documents.new([content]) } it 'surfaces an error about invalid inputs' do interpolator.interpolate! @@ -60,17 +59,7 @@ RSpec.describe Gitlab::Ci::Config::Interpolation::TextInterpolator, feature_cate end context 'when spec header is invalid' do - let(:header) do - { spec: { arguments: { website: nil } } } - end - - let(:content) do - "test: 'deploy $[[ inputs.website ]]'" - end - - let(:arguments) do - { website: 'gitlab.com' } - end + let(:header) { ::Gitlab::Config::Loader::Yaml.new("spec:\n arguments:\n website:") } it 'surfaces an error about invalid header' do interpolator.interpolate! @@ -81,17 +70,7 @@ RSpec.describe Gitlab::Ci::Config::Interpolation::TextInterpolator, feature_cate end context 'when provided interpolation argument is invalid' do - let(:header) do - { spec: { inputs: { website: nil } } } - end - - let(:content) do - "test: 'deploy $[[ inputs.website ]]'" - end - - let(:arguments) do - { website: ['gitlab.com'] } - end + let(:arguments) { { website: ['gitlab.com'] } } it 'returns an error about the invalid argument' do interpolator.interpolate! @@ -102,17 +81,7 @@ RSpec.describe Gitlab::Ci::Config::Interpolation::TextInterpolator, feature_cate end context 'when interpolation block is invalid' do - let(:header) do - { spec: { inputs: { website: nil } } } - end - - let(:content) do - "test: 'deploy $[[ inputs.abc ]]'" - end - - let(:arguments) do - { website: 'gitlab.com' } - end + let(:content) { ::Gitlab::Config::Loader::Yaml.new("test: 'deploy $[[ inputs.abc ]]'") } it 'returns an error about the invalid block' do interpolator.interpolate! @@ -123,16 +92,8 @@ RSpec.describe Gitlab::Ci::Config::Interpolation::TextInterpolator, feature_cate end context 'when multiple interpolation blocks are invalid' do - let(:header) do - { spec: { inputs: { website: nil } } } - end - let(:content) do - "test: 'deploy $[[ inputs.something.abc ]] $[[ inputs.cde ]] $[[ efg ]]'" - end - - let(:arguments) do - { website: 'gitlab.com' } + ::Gitlab::Config::Loader::Yaml.new("test: 'deploy $[[ inputs.something.abc ]] $[[ inputs.cde ]] $[[ efg ]]'") end it 'stops execution after the first invalid block' do @@ -145,16 +106,24 @@ RSpec.describe Gitlab::Ci::Config::Interpolation::TextInterpolator, feature_cate context 'when there are many invalid arguments' do let(:header) do - { spec: { inputs: { - allow_failure: { type: 'boolean' }, - image: nil, - parallel: { type: 'number' }, - website: nil - } } } + ::Gitlab::Config::Loader::Yaml.new( + <<~YAML + spec: + inputs: + allow_failure: + type: boolean + image: + parallel: + type: number + website: + YAML + ) end let(:content) do - "test: 'deploy $[[ inputs.website ]] $[[ inputs.parallel ]] $[[ inputs.allow_failure ]] $[[ inputs.image ]]'" + ::Gitlab::Config::Loader::Yaml.new( + "test: 'deploy $[[ inputs.website ]] $[[ inputs.parallel ]] $[[ inputs.allow_failure ]] $[[ inputs.image ]]'" + ) end let(:arguments) do @@ -178,44 +147,4 @@ RSpec.describe Gitlab::Ci::Config::Interpolation::TextInterpolator, feature_cate ) end end - - describe '#to_result' do - context 'when interpolation is not used' do - let(:result) do - ::Gitlab::Ci::Config::Yaml::Result.new(config: content) - end - - let(:content) do - "test: 'deploy production'" - end - - let(:arguments) { nil } - - it 'returns original content' do - interpolator.interpolate! - - expect(interpolator.to_result).to eq(content) - end - end - - context 'when interpolation is available' do - let(:header) do - { spec: { inputs: { website: nil } } } - end - - let(:content) do - "test: 'deploy $[[ inputs.website ]]'" - end - - let(:arguments) do - { website: 'gitlab.com' } - end - - it 'correctly interpolates content' do - interpolator.interpolate! - - expect(interpolator.to_result).to eq("test: 'deploy gitlab.com'") - end - end - end end diff --git a/spec/lib/gitlab/ci/config/yaml/documents_spec.rb b/spec/lib/gitlab/ci/config/yaml/documents_spec.rb new file mode 100644 index 00000000000..babdea6623b --- /dev/null +++ b/spec/lib/gitlab/ci/config/yaml/documents_spec.rb @@ -0,0 +1,71 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::Ci::Config::Yaml::Documents, feature_category: :pipeline_composition do + let(:documents) { described_class.new(yaml_documents) } + + describe '#valid?' do + context 'when there are no errors' do + let(:yaml_documents) { [::Gitlab::Config::Loader::Yaml.new('job:')] } + + it 'returns true' do + expect(documents).to be_valid + end + end + + context 'when there are errors' do + let(:yaml_documents) { [::Gitlab::Config::Loader::Yaml.new('_!@malformedyaml:&')] } + + it 'returns false' do + expect(documents).not_to be_valid + end + end + end + + describe '#header' do + context 'when there are at least 2 documents and the first document has a `spec` keyword' do + let(:yaml_documents) { [::Gitlab::Config::Loader::Yaml.new('spec:'), ::Gitlab::Config::Loader::Yaml.new('job:')] } + + it 'returns the header' do + expect(documents.header).to eq(spec: nil) + end + end + + context 'when there are fewer than 2 documents' do + let(:yaml_documents) { [::Gitlab::Config::Loader::Yaml.new('job:')] } + + it 'returns nil' do + expect(documents.header).to be_nil + end + end + + context 'when there are at least 2 documents and the first document does not have a `spec` keyword' do + let(:yaml_documents) do + [::Gitlab::Config::Loader::Yaml.new('job1:'), ::Gitlab::Config::Loader::Yaml.new('job2:')] + end + + it 'returns nil' do + expect(documents.header).to be_nil + end + end + end + + describe '#content' do + context 'when there is a header' do + let(:yaml_documents) { [::Gitlab::Config::Loader::Yaml.new('spec:'), ::Gitlab::Config::Loader::Yaml.new('job:')] } + + it 'returns the unparsed content of the last document' do + expect(documents.content).to eq('job:') + end + end + + context 'when there is no header' do + let(:yaml_documents) { [::Gitlab::Config::Loader::Yaml.new('job:')] } + + it 'returns the unparsed content of the first document' do + expect(documents.content).to eq('job:') + end + end + end +end diff --git a/spec/lib/gitlab/config/loader/yaml_spec.rb b/spec/lib/gitlab/config/loader/yaml_spec.rb index ec83fbc67b5..37e2293af1a 100644 --- a/spec/lib/gitlab/config/loader/yaml_spec.rb +++ b/spec/lib/gitlab/config/loader/yaml_spec.rb @@ -208,4 +208,10 @@ RSpec.describe Gitlab::Config::Loader::Yaml, feature_category: :pipeline_composi end end end + + describe '#raw' do + it 'returns the unparsed YAML' do + expect(loader.raw).to eq(yml) + end + end end |