diff options
Diffstat (limited to 'spec/lib/gitlab/ci/variables')
-rw-r--r-- | spec/lib/gitlab/ci/variables/builder/group_spec.rb | 11 | ||||
-rw-r--r-- | spec/lib/gitlab/ci/variables/builder/project_spec.rb | 11 | ||||
-rw-r--r-- | spec/lib/gitlab/ci/variables/builder/release_spec.rb | 69 | ||||
-rw-r--r-- | spec/lib/gitlab/ci/variables/builder_spec.rb | 44 | ||||
-rw-r--r-- | spec/lib/gitlab/ci/variables/collection/sort_spec.rb | 15 | ||||
-rw-r--r-- | spec/lib/gitlab/ci/variables/collection_spec.rb | 37 |
6 files changed, 162 insertions, 25 deletions
diff --git a/spec/lib/gitlab/ci/variables/builder/group_spec.rb b/spec/lib/gitlab/ci/variables/builder/group_spec.rb index 72487588cde..c3743ebd2d7 100644 --- a/spec/lib/gitlab/ci/variables/builder/group_spec.rb +++ b/spec/lib/gitlab/ci/variables/builder/group_spec.rb @@ -132,11 +132,12 @@ RSpec.describe Gitlab::Ci::Variables::Builder::Group do end it 'orders the variables from least to most matched' do - variables_collection = Gitlab::Ci::Variables::Collection.new([ - variable, - partially_matched_variable, - perfectly_matched_variable - ]).to_runner_variables + variables_collection = Gitlab::Ci::Variables::Collection.new( + [ + variable, + partially_matched_variable, + perfectly_matched_variable + ]).to_runner_variables expect(subject.to_runner_variables).to eq(variables_collection) end diff --git a/spec/lib/gitlab/ci/variables/builder/project_spec.rb b/spec/lib/gitlab/ci/variables/builder/project_spec.rb index b64b6ea98e2..c1cefc425f5 100644 --- a/spec/lib/gitlab/ci/variables/builder/project_spec.rb +++ b/spec/lib/gitlab/ci/variables/builder/project_spec.rb @@ -132,11 +132,12 @@ RSpec.describe Gitlab::Ci::Variables::Builder::Project do end it 'puts variables matching environment scope more in the end' do - variables_collection = Gitlab::Ci::Variables::Collection.new([ - variable, - partially_matched_variable, - perfectly_matched_variable - ]).to_runner_variables + variables_collection = Gitlab::Ci::Variables::Collection.new( + [ + variable, + partially_matched_variable, + perfectly_matched_variable + ]).to_runner_variables expect(subject.to_runner_variables).to eq(variables_collection) end diff --git a/spec/lib/gitlab/ci/variables/builder/release_spec.rb b/spec/lib/gitlab/ci/variables/builder/release_spec.rb new file mode 100644 index 00000000000..85b1659d07b --- /dev/null +++ b/spec/lib/gitlab/ci/variables/builder/release_spec.rb @@ -0,0 +1,69 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::Ci::Variables::Builder::Release do + let_it_be(:project) { create(:project, :public, :repository) } + let_it_be(:release) { create(:release, project: project) } + + let(:builder) { described_class.new(release) } + + describe '#variables' do + let(:description_variable) do + { + key: 'CI_RELEASE_DESCRIPTION', + value: release.description, + public: true, + masked: false, + raw: true + } + end + + subject do + builder.variables + end + + context 'when the release is present' do + let(:description_item) { item(description_variable) } + + it 'contains all the variables' do + is_expected.to contain_exactly(description_item) + end + + context 'for large description' do + before do + release.update_attribute(:description, "Test Description ..." * 5000) + end + + it 'truncates' do + expect(subject['CI_RELEASE_DESCRIPTION'].value.length).to eq(1024) + end + end + + context 'when description is nil' do + before do + release.update_attribute(:description, nil) + end + + it 'returns without error' do + builder = subject + + expect(builder).to match_array([]) + expect(builder.errors).to be_nil + end + end + end + + context 'when the release is not present' do + let(:release) { nil } + + it 'contains no variables' do + is_expected.to match_array([]) + end + end + end + + def item(variable) + ::Gitlab::Ci::Variables::Collection::Item.fabricate(variable) + end +end diff --git a/spec/lib/gitlab/ci/variables/builder_spec.rb b/spec/lib/gitlab/ci/variables/builder_spec.rb index 4833ccf9093..52ba85d2df1 100644 --- a/spec/lib/gitlab/ci/variables/builder_spec.rb +++ b/spec/lib/gitlab/ci/variables/builder_spec.rb @@ -10,6 +10,7 @@ RSpec.describe Gitlab::Ci::Variables::Builder, :clean_gitlab_redis_cache do let_it_be(:user) { create(:user) } let_it_be_with_reload(:job) do create(:ci_build, + name: 'rspec:test 1', pipeline: pipeline, user: user, yaml_variables: [{ key: 'YAML_VARIABLE', value: 'value' }] @@ -24,13 +25,15 @@ RSpec.describe Gitlab::Ci::Variables::Builder, :clean_gitlab_redis_cache do let(:predefined_variables) do [ { key: 'CI_JOB_NAME', - value: job.name }, + value: 'rspec:test 1' }, + { key: 'CI_JOB_NAME_SLUG', + value: 'rspec-test-1' }, { key: 'CI_JOB_STAGE', value: job.stage_name }, { key: 'CI_NODE_TOTAL', value: '1' }, { key: 'CI_BUILD_NAME', - value: job.name }, + value: 'rspec:test 1' }, { key: 'CI_BUILD_STAGE', value: job.stage_name }, { key: 'CI', @@ -171,6 +174,7 @@ RSpec.describe Gitlab::Ci::Variables::Builder, :clean_gitlab_redis_cache do allow(builder).to receive(:secret_project_variables) { [var('L', 12), var('M', 12)] } allow(pipeline).to receive(:variables) { [var('M', 13), var('N', 13)] } allow(pipeline).to receive(:pipeline_schedule) { double(job_variables: [var('N', 14), var('O', 14)]) } + allow(builder).to receive(:release_variables) { [var('P', 15), var('Q', 15)] } end it 'returns variables in order depending on resource hierarchy' do @@ -187,7 +191,8 @@ RSpec.describe Gitlab::Ci::Variables::Builder, :clean_gitlab_redis_cache do var('K', 11), var('L', 11), var('L', 12), var('M', 12), var('M', 13), var('N', 13), - var('N', 14), var('O', 14)]) + var('N', 14), var('O', 14), + var('P', 15), var('Q', 15)]) end it 'overrides duplicate keys depending on resource hierarchy' do @@ -199,7 +204,8 @@ RSpec.describe Gitlab::Ci::Variables::Builder, :clean_gitlab_redis_cache do 'I' => '9', 'J' => '10', 'K' => '11', 'L' => '12', 'M' => '13', 'N' => '14', - 'O' => '14') + 'O' => '14', 'P' => '15', + 'Q' => '15') end end @@ -216,6 +222,27 @@ RSpec.describe Gitlab::Ci::Variables::Builder, :clean_gitlab_redis_cache do .to include(a_hash_including(key: schedule_variable.key, value: schedule_variable.value)) end end + + context 'with release variables' do + let(:release_description_key) { 'CI_RELEASE_DESCRIPTION' } + + let_it_be(:tag) { project.repository.tags.first } + let_it_be(:pipeline) { create(:ci_pipeline, project: project, tag: true, ref: tag.name) } + let_it_be(:release) { create(:release, tag: tag.name, project: project) } + + it 'includes release variables' do + expect(subject.to_hash).to include(release_description_key => release.description) + end + + context 'when there is no release' do + let_it_be(:pipeline) { create(:ci_pipeline, project: project, tag: false, ref: 'master') } + let(:release) { nil } + + it 'does not include release variables' do + expect(subject.to_hash).not_to have_key(release_description_key) + end + end + end end describe '#user_variables' do @@ -261,10 +288,11 @@ RSpec.describe Gitlab::Ci::Variables::Builder, :clean_gitlab_redis_cache do end it 'includes #deployment_variables and merges the KUBECONFIG values', :aggregate_failures do - expect(builder).to receive(:deployment_variables).and_return([ - { key: 'KUBECONFIG', value: 'deployment-kubeconfig' }, - { key: 'OTHER', value: 'some value' } - ]) + expect(builder).to receive(:deployment_variables).and_return( + [ + { key: 'KUBECONFIG', value: 'deployment-kubeconfig' }, + { key: 'OTHER', value: 'some value' } + ]) expect(template).to receive(:merge_yaml).with('deployment-kubeconfig') expect(subject['KUBECONFIG'].value).to eq('example-kubeconfig') expect(subject['OTHER'].value).to eq('some value') diff --git a/spec/lib/gitlab/ci/variables/collection/sort_spec.rb b/spec/lib/gitlab/ci/variables/collection/sort_spec.rb index 57171e5be69..432225c53f0 100644 --- a/spec/lib/gitlab/ci/variables/collection/sort_spec.rb +++ b/spec/lib/gitlab/ci/variables/collection/sort_spec.rb @@ -192,13 +192,14 @@ RSpec.describe Gitlab::Ci::Variables::Collection::Sort do end it 'preserves relative order of overridden variables' do - is_expected.to eq([ - { 'TOP_LEVEL_GROUP_NAME' => 'top-level-group' }, - { 'SUBGROUP_VAR' => '$TOP_LEVEL_GROUP_NAME' }, - { 'SUB_GROUP_NAME' => 'vars-in-vars-subgroup' }, - { 'SUBGROUP_VAR' => '$SUB_GROUP_NAME' }, - { 'PROJECT_VAR' => '$SUBGROUP_VAR' } - ]) + is_expected.to eq( + [ + { 'TOP_LEVEL_GROUP_NAME' => 'top-level-group' }, + { 'SUBGROUP_VAR' => '$TOP_LEVEL_GROUP_NAME' }, + { 'SUB_GROUP_NAME' => 'vars-in-vars-subgroup' }, + { 'SUBGROUP_VAR' => '$SUB_GROUP_NAME' }, + { 'PROJECT_VAR' => '$SUBGROUP_VAR' } + ]) end end end diff --git a/spec/lib/gitlab/ci/variables/collection_spec.rb b/spec/lib/gitlab/ci/variables/collection_spec.rb index 8ac03301322..7d4a1eef70b 100644 --- a/spec/lib/gitlab/ci/variables/collection_spec.rb +++ b/spec/lib/gitlab/ci/variables/collection_spec.rb @@ -571,5 +571,42 @@ RSpec.describe Gitlab::Ci::Variables::Collection do end end end + + context 'with the file_variable_is_referenced_in_another_variable logging' do + let(:collection) do + Gitlab::Ci::Variables::Collection.new + .append(key: 'VAR1', value: 'test-1') + .append(key: 'VAR2', value: '$VAR1') + .append(key: 'VAR3', value: '$VAR1', raw: true) + .append(key: 'FILEVAR4', value: 'file-test-4', file: true) + .append(key: 'VAR5', value: '$FILEVAR4') + .append(key: 'VAR6', value: '$FILEVAR4', raw: true) + end + + subject(:sort_and_expand_all) { collection.sort_and_expand_all(project: project) } + + context 'when a project is not passed' do + let(:project) {} + + it 'does not log anything' do + expect(Gitlab::AppJsonLogger).not_to receive(:info) + + sort_and_expand_all + end + end + + context 'when a project is passed' do + let(:project) { create(:project) } + + it 'logs file_variable_is_referenced_in_another_variable once for VAR5' do + expect(Gitlab::AppJsonLogger).to receive(:info).with( + event: 'file_variable_is_referenced_in_another_variable', + project_id: project.id + ).once + + sort_and_expand_all + end + end + end end end |