diff options
Diffstat (limited to 'spec/lib/gitlab/ci/build')
6 files changed, 188 insertions, 13 deletions
diff --git a/spec/lib/gitlab/ci/build/artifacts/adapters/zip_stream_spec.rb b/spec/lib/gitlab/ci/build/artifacts/adapters/zip_stream_spec.rb new file mode 100644 index 00000000000..2c236ba3726 --- /dev/null +++ b/spec/lib/gitlab/ci/build/artifacts/adapters/zip_stream_spec.rb @@ -0,0 +1,86 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::Ci::Build::Artifacts::Adapters::ZipStream do + let(:file_name) { 'single_file.zip' } + let(:fixture_path) { "lib/gitlab/ci/build/artifacts/adapters/zip_stream/#{file_name}" } + let(:stream) { File.open(expand_fixture_path(fixture_path), 'rb') } + + describe '#initialize' do + it 'initializes when stream is passed' do + expect { described_class.new(stream) }.not_to raise_error + end + + context 'when stream is not passed' do + let(:stream) { nil } + + it 'raises an error' do + expect { described_class.new(stream) }.to raise_error(described_class::InvalidStreamError) + end + end + end + + describe '#each_blob' do + let(:adapter) { described_class.new(stream) } + + context 'when stream is a zip file' do + it 'iterates file content when zip file contains one file' do + expect { |b| adapter.each_blob(&b) } + .to yield_with_args("file 1 content\n") + end + + context 'when zip file contains multiple files' do + let(:file_name) { 'multiple_files.zip' } + + it 'iterates content of all files' do + expect { |b| adapter.each_blob(&b) } + .to yield_successive_args("file 1 content\n", "file 2 content\n") + end + end + + context 'when zip file includes files in a directory' do + let(:file_name) { 'with_directory.zip' } + + it 'iterates contents from files only' do + expect { |b| adapter.each_blob(&b) } + .to yield_successive_args("file 1 content\n", "file 2 content\n") + end + end + + context 'when zip contains a file which decompresses beyond the size limit' do + let(:file_name) { '200_mb_decompressed.zip' } + + it 'does not read the file' do + expect { |b| adapter.each_blob(&b) }.not_to yield_control + end + end + + context 'when the zip contains too many files' do + let(:file_name) { '100_files.zip' } + + it 'stops processing when the limit is reached' do + expect { |b| adapter.each_blob(&b) } + .to yield_control.exactly(described_class::MAX_FILES_PROCESSED).times + end + end + + context 'when stream is a zipbomb' do + let(:file_name) { 'zipbomb.zip' } + + it 'does not read the file' do + expect { |b| adapter.each_blob(&b) }.not_to yield_control + end + end + end + + context 'when stream is not a zip file' do + let(:stream) { File.open(expand_fixture_path('junit/junit.xml.gz'), 'rb') } + + it 'does not yield any data' do + expect { |b| adapter.each_blob(&b) }.not_to yield_control + expect { adapter.each_blob { |b| b } }.not_to raise_error + end + end + end +end diff --git a/spec/lib/gitlab/ci/build/artifacts/metadata/entry_spec.rb b/spec/lib/gitlab/ci/build/artifacts/metadata/entry_spec.rb index c8ace28108b..7b35c9ba483 100644 --- a/spec/lib/gitlab/ci/build/artifacts/metadata/entry_spec.rb +++ b/spec/lib/gitlab/ci/build/artifacts/metadata/entry_spec.rb @@ -67,6 +67,7 @@ RSpec.describe Gitlab::Ci::Build::Artifacts::Metadata::Entry do subject { |example| path(example).children } it { is_expected.to all(be_an_instance_of(described_class)) } + it do is_expected.to contain_exactly entry('path/dir_1/file_1'), entry('path/dir_1/file_b'), @@ -79,6 +80,7 @@ RSpec.describe Gitlab::Ci::Build::Artifacts::Metadata::Entry do it { is_expected.to all(be_file) } it { is_expected.to all(be_an_instance_of(described_class)) } + it do is_expected.to contain_exactly entry('path/dir_1/file_1'), entry('path/dir_1/file_b') @@ -99,6 +101,7 @@ RSpec.describe Gitlab::Ci::Build::Artifacts::Metadata::Entry do it { is_expected.to all(be_directory) } it { is_expected.to all(be_an_instance_of(described_class)) } + it do is_expected.to contain_exactly entry('path/dir_1/subdir/'), entry('path/') diff --git a/spec/lib/gitlab/ci/build/prerequisite/kubernetes_namespace_spec.rb b/spec/lib/gitlab/ci/build/prerequisite/kubernetes_namespace_spec.rb index 94c14cfa479..baabab73ea2 100644 --- a/spec/lib/gitlab/ci/build/prerequisite/kubernetes_namespace_spec.rb +++ b/spec/lib/gitlab/ci/build/prerequisite/kubernetes_namespace_spec.rb @@ -74,7 +74,7 @@ RSpec.describe Gitlab::Ci::Build::Prerequisite::KubernetesNamespace do end context 'kubernetes namespace does not exist' do - let(:namespace_builder) { double(execute: kubernetes_namespace)} + let(:namespace_builder) { double(execute: kubernetes_namespace) } before do allow(Clusters::KubernetesNamespaceFinder).to receive(:new) diff --git a/spec/lib/gitlab/ci/build/releaser_spec.rb b/spec/lib/gitlab/ci/build/releaser_spec.rb index 435f70e9ac5..ffa7073818a 100644 --- a/spec/lib/gitlab/ci/build/releaser_spec.rb +++ b/spec/lib/gitlab/ci/build/releaser_spec.rb @@ -13,6 +13,7 @@ RSpec.describe Gitlab::Ci::Build::Releaser do name: 'Release $CI_COMMIT_SHA', description: 'Created using the release-cli $EXTRA_DESCRIPTION', tag_name: 'release-$CI_COMMIT_SHA', + tag_message: 'Annotated tag message', ref: '$CI_COMMIT_SHA', milestones: %w[m1 m2 m3], released_at: '2020-07-15T08:00:00Z', @@ -27,7 +28,7 @@ RSpec.describe Gitlab::Ci::Build::Releaser do end it 'generates the script' do - expect(subject).to eq(['release-cli create --name "Release $CI_COMMIT_SHA" --description "Created using the release-cli $EXTRA_DESCRIPTION" --tag-name "release-$CI_COMMIT_SHA" --ref "$CI_COMMIT_SHA" --released-at "2020-07-15T08:00:00Z" --milestone "m1" --milestone "m2" --milestone "m3" --assets-link "{\"name\":\"asset1\",\"url\":\"https://example.com/assets/1\",\"link_type\":\"other\",\"filepath\":\"/pretty/asset/1\"}" --assets-link "{\"name\":\"asset2\",\"url\":\"https://example.com/assets/2\"}"']) + expect(subject).to eq(['release-cli create --name "Release $CI_COMMIT_SHA" --description "Created using the release-cli $EXTRA_DESCRIPTION" --tag-name "release-$CI_COMMIT_SHA" --tag-message "Annotated tag message" --ref "$CI_COMMIT_SHA" --released-at "2020-07-15T08:00:00Z" --milestone "m1" --milestone "m2" --milestone "m3" --assets-link "{\"name\":\"asset1\",\"url\":\"https://example.com/assets/1\",\"link_type\":\"other\",\"filepath\":\"/pretty/asset/1\"}" --assets-link "{\"name\":\"asset2\",\"url\":\"https://example.com/assets/2\"}"']) end end @@ -39,6 +40,7 @@ RSpec.describe Gitlab::Ci::Build::Releaser do :name | 'Release $CI_COMMIT_SHA' | 'release-cli create --name "Release $CI_COMMIT_SHA"' :description | 'Release-cli $EXTRA_DESCRIPTION' | 'release-cli create --description "Release-cli $EXTRA_DESCRIPTION"' :tag_name | 'release-$CI_COMMIT_SHA' | 'release-cli create --tag-name "release-$CI_COMMIT_SHA"' + :tag_message | 'Annotated tag message' | 'release-cli create --tag-message "Annotated tag message"' :ref | '$CI_COMMIT_SHA' | 'release-cli create --ref "$CI_COMMIT_SHA"' :milestones | %w[m1 m2 m3] | 'release-cli create --milestone "m1" --milestone "m2" --milestone "m3"' :released_at | '2020-07-15T08:00:00Z' | 'release-cli create --released-at "2020-07-15T08:00:00Z"' diff --git a/spec/lib/gitlab/ci/build/rules/rule/clause/changes_spec.rb b/spec/lib/gitlab/ci/build/rules/rule/clause/changes_spec.rb index 3892b88598a..234ba68d627 100644 --- a/spec/lib/gitlab/ci/build/rules/rule/clause/changes_spec.rb +++ b/spec/lib/gitlab/ci/build/rules/rule/clause/changes_spec.rb @@ -4,7 +4,9 @@ require 'spec_helper' RSpec.describe Gitlab::Ci::Build::Rules::Rule::Clause::Changes do describe '#satisfied_by?' do - subject { described_class.new(globs).satisfied_by?(pipeline, context) } + let(:context) { instance_double(Gitlab::Ci::Build::Context::Base) } + + subject(:satisfied_by) { described_class.new(globs).satisfied_by?(pipeline, context) } context 'a glob matching rule' do using RSpec::Parameterized::TableSyntax @@ -18,11 +20,9 @@ RSpec.describe Gitlab::Ci::Build::Rules::Rule::Clause::Changes do # rubocop:disable Layout/LineLength where(:case_name, :globs, :files, :satisfied) do - 'exact top-level match' | ['Dockerfile'] | { 'Dockerfile' => '', 'Gemfile' => '' } | true 'exact top-level match' | { paths: ['Dockerfile'] } | { 'Dockerfile' => '', 'Gemfile' => '' } | true 'exact top-level no match' | { paths: ['Dockerfile'] } | { 'Gemfile' => '' } | false 'pattern top-level match' | { paths: ['Docker*'] } | { 'Dockerfile' => '', 'Gemfile' => '' } | true - 'pattern top-level no match' | ['Docker*'] | { 'Gemfile' => '' } | false 'pattern top-level no match' | { paths: ['Docker*'] } | { 'Gemfile' => '' } | false 'exact nested match' | { paths: ['project/build.properties'] } | { 'project/build.properties' => '' } | true 'exact nested no match' | { paths: ['project/build.properties'] } | { 'project/README.md' => '' } | false @@ -92,5 +92,97 @@ RSpec.describe Gitlab::Ci::Build::Rules::Rule::Clause::Changes do it { is_expected.to be_truthy } end end + + context 'when using compare_to' do + let_it_be(:project) do + create(:project, :custom_repo, + files: { 'README.md' => 'readme' }) + end + + let_it_be(:user) { project.owner } + + before_all do + project.repository.add_branch(user, 'feature_1', 'master') + + project.repository.create_file( + user, 'file1.txt', 'file 1', message: 'Create file1.txt', branch_name: 'feature_1' + ) + project.repository.add_tag(user, 'tag_1', 'feature_1') + + project.repository.create_file( + user, 'file2.txt', 'file 2', message: 'Create file2.txt', branch_name: 'feature_1' + ) + project.repository.add_branch(user, 'feature_2', 'feature_1') + + project.repository.update_file( + user, 'file2.txt', 'file 2 updated', message: 'Update file2.txt', branch_name: 'feature_2' + ) + end + + context 'when compare_to is branch or tag' do + using RSpec::Parameterized::TableSyntax + + where(:pipeline_ref, :compare_to, :paths, :ff, :result) do + 'feature_1' | 'master' | ['file1.txt'] | true | true + 'feature_1' | 'master' | ['README.md'] | true | false + 'feature_1' | 'master' | ['xyz.md'] | true | false + 'feature_2' | 'master' | ['file1.txt'] | true | true + 'feature_2' | 'master' | ['file2.txt'] | true | true + 'feature_2' | 'feature_1' | ['file1.txt'] | true | false + 'feature_2' | 'feature_1' | ['file1.txt'] | false | true + 'feature_2' | 'feature_1' | ['file2.txt'] | true | true + 'feature_1' | 'tag_1' | ['file1.txt'] | true | false + 'feature_1' | 'tag_1' | ['file1.txt'] | false | true + 'feature_1' | 'tag_1' | ['file2.txt'] | true | true + 'feature_2' | 'tag_1' | ['file2.txt'] | true | true + end + + with_them do + let(:globs) { { paths: paths, compare_to: compare_to } } + + let(:pipeline) do + build(:ci_pipeline, project: project, ref: pipeline_ref, sha: project.commit(pipeline_ref).sha) + end + + before do + stub_feature_flags(ci_rules_changes_compare: ff) + end + + it { is_expected.to eq(result) } + end + end + + context 'when compare_to is a sha' do + let(:globs) { { paths: ['file2.txt'], compare_to: project.commit('tag_1').sha } } + + let(:pipeline) do + build(:ci_pipeline, project: project, ref: 'feature_2', sha: project.commit('feature_2').sha) + end + + it { is_expected.to be_truthy } + end + + context 'when compare_to is not a valid ref' do + let(:globs) { { paths: ['file1.txt'], compare_to: 'xyz' } } + + let(:pipeline) do + build(:ci_pipeline, project: project, ref: 'feature_2', sha: project.commit('feature_2').sha) + end + + it 'raises ParseError' do + expect { satisfied_by }.to raise_error( + ::Gitlab::Ci::Build::Rules::Rule::Clause::ParseError, 'rules:changes:compare_to is not a valid ref' + ) + end + + context 'when the FF ci_rules_changes_compare is disabled' do + before do + stub_feature_flags(ci_rules_changes_compare: false) + end + + it { is_expected.to be_truthy } + end + end + end end end diff --git a/spec/lib/gitlab/ci/build/rules/rule/clause/if_spec.rb b/spec/lib/gitlab/ci/build/rules/rule/clause/if_spec.rb index 81bce989833..31c7437cfe0 100644 --- a/spec/lib/gitlab/ci/build/rules/rule/clause/if_spec.rb +++ b/spec/lib/gitlab/ci/build/rules/rule/clause/if_spec.rb @@ -51,14 +51,6 @@ RSpec.describe Gitlab::Ci::Build::Rules::Rule::Clause::If do end it { is_expected.to eq(true) } - - context 'when the FF ci_fix_rules_if_comparison_with_regexp_variable is disabled' do - before do - stub_feature_flags(ci_fix_rules_if_comparison_with_regexp_variable: false) - end - - it { is_expected.to eq(false) } - end end context 'when comparison is false' do |