debian-mirror-gitlab/spec/presenters/ci/build_runner_presenter_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

403 lines
14 KiB
Ruby
Raw Normal View History

2019-12-26 22:10:19 +05:30
# frozen_string_literal: true
2018-11-18 11:00:15 +05:30
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe Ci::BuildRunnerPresenter do
2018-11-18 11:00:15 +05:30
let(:presenter) { described_class.new(build) }
let(:archive) { { paths: ['sample.txt'] } }
let(:archive_expectation) do
{
artifact_type: :archive,
artifact_format: :zip,
paths: archive[:paths],
untracked: archive[:untracked]
}
end
describe '#artifacts' do
context "when option contains archive-type artifacts" do
2021-03-11 19:13:27 +05:30
let(:build) { create(:ci_build, options: { artifacts: archive }) }
2018-11-18 11:00:15 +05:30
it 'presents correct hash' do
expect(presenter.artifacts.first).to include(archive_expectation)
end
context "when untracked is specified" do
let(:archive) { { untracked: true } }
it 'presents correct hash' do
expect(presenter.artifacts.first).to include(archive_expectation)
end
end
context "when untracked and paths are missing" do
let(:archive) { { when: 'always' } }
it 'does not present hash' do
expect(presenter.artifacts).to be_empty
end
end
2020-05-24 23:13:21 +05:30
context 'when artifacts exclude is defined' do
let(:build) do
create(:ci_build, options: { artifacts: { paths: %w[abc], exclude: %w[cde] } })
end
2021-09-30 23:02:18 +05:30
it 'includes the list of excluded paths' do
expect(presenter.artifacts.first).to include(
artifact_type: :archive,
artifact_format: :zip,
paths: %w[abc],
exclude: %w[cde]
)
2020-05-24 23:13:21 +05:30
end
end
context 'when artifacts exclude is not defined' do
let(:build) do
create(:ci_build, options: { artifacts: { paths: %w[abc] } })
end
it 'does not include an empty list of excluded paths' do
expect(presenter.artifacts.first).not_to have_key(:exclude)
end
end
2018-11-18 11:00:15 +05:30
end
2018-12-05 23:21:45 +05:30
context "with reports" do
Ci::JobArtifact::DEFAULT_FILE_NAMES.each do |file_type, filename|
2018-12-13 13:39:08 +05:30
context file_type.to_s do
let(:report) { { "#{file_type}": [filename] } }
2021-04-29 21:17:54 +05:30
let(:build) { create(:ci_build, options: { artifacts: { reports: report } }) }
2018-12-05 23:21:45 +05:30
2018-12-13 13:39:08 +05:30
let(:report_expectation) do
{
name: filename,
artifact_type: :"#{file_type}",
artifact_format: Ci::JobArtifact::TYPE_AND_FORMAT_PAIRS.fetch(file_type),
paths: [filename],
when: 'always'
2022-06-21 17:19:12 +05:30
}.compact
2018-12-13 13:39:08 +05:30
end
2018-11-18 11:00:15 +05:30
2018-12-13 13:39:08 +05:30
it 'presents correct hash' do
2022-06-21 17:19:12 +05:30
expect(presenter.artifacts).to contain_exactly(report_expectation)
2018-12-13 13:39:08 +05:30
end
2018-12-05 23:21:45 +05:30
end
2018-11-18 11:00:15 +05:30
end
end
2022-06-21 17:19:12 +05:30
context 'when a specific coverage_report type is given' do
let(:coverage_format) { :cobertura }
let(:filename) { 'cobertura-coverage.xml' }
let(:coverage_report) { { path: filename, coverage_format: coverage_format } }
let(:report) { { coverage_report: coverage_report } }
let(:build) { create(:ci_build, options: { artifacts: { reports: report } }) }
let(:expected_coverage_report) do
{
name: filename,
artifact_type: coverage_format,
artifact_format: Ci::JobArtifact::TYPE_AND_FORMAT_PAIRS.fetch(coverage_format),
paths: [filename],
when: 'always'
}
end
it 'presents the coverage report hash with the coverage format' do
expect(presenter.artifacts).to contain_exactly(expected_coverage_report)
end
end
context 'when a specific coverage_report type is given with another report type' do
let(:coverage_format) { :cobertura }
let(:coverage_filename) { 'cobertura-coverage.xml' }
let(:coverage_report) { { path: coverage_filename, coverage_format: coverage_format } }
let(:ds_filename) { 'gl-dependency-scanning-report.json' }
let(:report) { { coverage_report: coverage_report, dependency_scanning: [ds_filename] } }
let(:build) { create(:ci_build, options: { artifacts: { reports: report } }) }
let(:expected_coverage_report) do
{
name: coverage_filename,
artifact_type: coverage_format,
artifact_format: Ci::JobArtifact::TYPE_AND_FORMAT_PAIRS.fetch(coverage_format),
paths: [coverage_filename],
when: 'always'
}
end
let(:expected_ds_report) do
{
name: ds_filename,
artifact_type: :dependency_scanning,
artifact_format: Ci::JobArtifact::TYPE_AND_FORMAT_PAIRS.fetch(:dependency_scanning),
paths: [ds_filename],
when: 'always'
}
end
it 'presents both reports' do
expect(presenter.artifacts).to contain_exactly(expected_coverage_report, expected_ds_report)
end
end
2018-11-18 11:00:15 +05:30
context "when option has both archive and reports specification" do
2018-12-05 23:21:45 +05:30
let(:report) { { junit: ['junit.xml'] } }
2021-04-29 21:17:54 +05:30
let(:build) { create(:ci_build, options: { script: 'echo', artifacts: { **archive, reports: report } }) }
2018-12-05 23:21:45 +05:30
let(:report_expectation) do
{
name: 'junit.xml',
artifact_type: :junit,
artifact_format: :gzip,
paths: ['junit.xml'],
when: 'always'
}
end
2018-11-18 11:00:15 +05:30
it 'presents correct hash' do
expect(presenter.artifacts.first).to include(archive_expectation)
2018-12-05 23:21:45 +05:30
expect(presenter.artifacts.second).to include(report_expectation)
2018-11-18 11:00:15 +05:30
end
context "when archive specifies 'expire_in' keyword" do
let(:archive) { { paths: ['sample.txt'], expire_in: '3 mins 4 sec' } }
it 'inherits expire_in from archive' do
expect(presenter.artifacts.first).to include({ **archive_expectation, expire_in: '3 mins 4 sec' })
2018-12-05 23:21:45 +05:30
expect(presenter.artifacts.second).to include({ **report_expectation, expire_in: '3 mins 4 sec' })
2018-11-18 11:00:15 +05:30
end
end
end
context "when option has no artifact keywords" do
let(:build) { create(:ci_build, :no_options) }
it 'does not present hash' do
expect(presenter.artifacts).to be_nil
end
end
end
2019-07-07 11:18:12 +05:30
describe '#ref_type' do
subject { presenter.ref_type }
let(:build) { create(:ci_build, tag: tag) }
let(:tag) { true }
it 'returns the correct ref type' do
is_expected.to eq('tag')
end
context 'when tag is false' do
let(:tag) { false }
it 'returns the correct ref type' do
is_expected.to eq('branch')
end
end
end
describe '#git_depth' do
let(:build) { create(:ci_build) }
2019-09-04 21:01:54 +05:30
subject(:git_depth) { presenter.git_depth }
2019-07-07 11:18:12 +05:30
context 'when GIT_DEPTH variable is specified' do
before do
create(:ci_pipeline_variable, key: 'GIT_DEPTH', value: 1, pipeline: build.pipeline)
end
2019-09-04 21:01:54 +05:30
it 'returns its value' do
expect(git_depth).to eq(1)
end
end
it 'defaults to git depth setting for the project' do
expect(git_depth).to eq(build.project.ci_default_git_depth)
end
2019-07-07 11:18:12 +05:30
end
describe '#refspecs' do
subject { presenter.refspecs }
let(:build) { create(:ci_build) }
2020-05-24 23:13:21 +05:30
let(:pipeline) { build.pipeline }
2019-07-07 11:18:12 +05:30
it 'returns the correct refspecs' do
2020-05-24 23:13:21 +05:30
is_expected.to contain_exactly("+refs/heads/#{build.ref}:refs/remotes/origin/#{build.ref}",
2022-05-07 20:08:51 +05:30
"+refs/pipelines/#{pipeline.id}:refs/pipelines/#{pipeline.id}")
2021-03-08 18:12:59 +05:30
end
2019-09-04 21:01:54 +05:30
context 'when ref is tag' do
let(:build) { create(:ci_build, :tag) }
2019-07-07 11:18:12 +05:30
it 'returns the correct refspecs' do
2020-05-24 23:13:21 +05:30
is_expected.to contain_exactly("+refs/tags/#{build.ref}:refs/tags/#{build.ref}",
2022-05-07 20:08:51 +05:30
"+refs/pipelines/#{pipeline.id}:refs/pipelines/#{pipeline.id}")
2019-07-07 11:18:12 +05:30
end
2019-09-04 21:01:54 +05:30
context 'when GIT_DEPTH is zero' do
before do
create(:ci_pipeline_variable, key: 'GIT_DEPTH', value: 0, pipeline: build.pipeline)
end
2019-07-07 11:18:12 +05:30
it 'returns the correct refspecs' do
2019-09-04 21:01:54 +05:30
is_expected.to contain_exactly('+refs/tags/*:refs/tags/*',
2020-05-24 23:13:21 +05:30
'+refs/heads/*:refs/remotes/origin/*',
2022-05-07 20:08:51 +05:30
"+refs/pipelines/#{pipeline.id}:refs/pipelines/#{pipeline.id}")
2019-07-07 11:18:12 +05:30
end
end
end
context 'when pipeline is detached merge request pipeline' do
let(:merge_request) { create(:merge_request, :with_detached_merge_request_pipeline) }
let(:pipeline) { merge_request.all_pipelines.first }
let(:build) { create(:ci_build, ref: pipeline.ref, pipeline: pipeline) }
2020-05-24 23:13:21 +05:30
before do
2021-04-29 21:17:54 +05:30
pipeline.persistent_ref.create # rubocop:disable Rails/SaveBang
2020-05-24 23:13:21 +05:30
end
2020-03-13 15:44:24 +05:30
2020-05-24 23:13:21 +05:30
it 'returns the correct refspecs' do
is_expected
2022-05-07 20:08:51 +05:30
.to contain_exactly("+refs/pipelines/#{pipeline.id}:refs/pipelines/#{pipeline.id}")
2019-07-07 11:18:12 +05:30
end
2020-05-24 23:13:21 +05:30
context 'when GIT_DEPTH is zero' do
2020-03-13 15:44:24 +05:30
before do
2020-05-24 23:13:21 +05:30
create(:ci_pipeline_variable, key: 'GIT_DEPTH', value: 0, pipeline: build.pipeline)
2020-03-13 15:44:24 +05:30
end
2019-07-07 11:18:12 +05:30
it 'returns the correct refspecs' do
2020-03-13 15:44:24 +05:30
is_expected
2022-05-07 20:08:51 +05:30
.to contain_exactly("+refs/pipelines/#{pipeline.id}:refs/pipelines/#{pipeline.id}",
2020-05-24 23:13:21 +05:30
'+refs/heads/*:refs/remotes/origin/*',
'+refs/tags/*:refs/tags/*')
2020-03-13 15:44:24 +05:30
end
2020-05-24 23:13:21 +05:30
end
2020-03-13 15:44:24 +05:30
2020-05-24 23:13:21 +05:30
context 'when pipeline is legacy detached merge request pipeline' do
let(:merge_request) { create(:merge_request, :with_legacy_detached_merge_request_pipeline) }
2020-03-13 15:44:24 +05:30
2020-05-24 23:13:21 +05:30
it 'returns the correct refspecs' do
2022-05-07 20:08:51 +05:30
is_expected.to contain_exactly("+refs/pipelines/#{pipeline.id}:refs/pipelines/#{pipeline.id}",
2021-03-11 19:13:27 +05:30
"+refs/heads/#{build.ref}:refs/remotes/origin/#{build.ref}")
2019-07-07 11:18:12 +05:30
end
end
end
2019-12-21 20:55:43 +05:30
context 'when persistent pipeline ref exists' do
let(:project) { create(:project, :repository) }
let(:sha) { project.repository.commit.sha }
let(:pipeline) { create(:ci_pipeline, sha: sha, project: project) }
let(:build) { create(:ci_build, pipeline: pipeline) }
before do
2021-04-29 21:17:54 +05:30
pipeline.persistent_ref.create # rubocop:disable Rails/SaveBang
2019-12-21 20:55:43 +05:30
end
it 'exposes the persistent pipeline ref' do
is_expected
2022-05-07 20:08:51 +05:30
.to contain_exactly("+refs/pipelines/#{pipeline.id}:refs/pipelines/#{pipeline.id}",
2019-12-21 20:55:43 +05:30
"+refs/heads/#{build.ref}:refs/remotes/origin/#{build.ref}")
end
end
2019-07-07 11:18:12 +05:30
end
2021-04-17 20:07:23 +05:30
2021-04-29 21:17:54 +05:30
describe '#runner_variables' do
2022-08-27 11:52:29 +05:30
subject(:runner_variables) { presenter.runner_variables }
2021-04-17 20:07:23 +05:30
2021-12-11 22:18:48 +05:30
let_it_be(:project) { create(:project, :repository) }
2021-04-29 21:17:54 +05:30
2022-08-27 11:52:29 +05:30
let(:sha) { project.repository.commit.sha }
let(:pipeline) { create(:ci_pipeline, sha: sha, project: project) }
let(:build) { create(:ci_build, pipeline: pipeline) }
it 'returns an array' do
is_expected.to be_an_instance_of(Array)
end
it 'returns the expected variables' do
is_expected.to eq(presenter.variables.to_runner_variables)
end
context 'when there are variables to expand' do
before_all do
create(:ci_variable, project: project,
key: 'regular_var',
value: 'value 1')
create(:ci_variable, project: project,
key: 'file_var',
value: 'value 2',
variable_type: :file)
create(:ci_variable, project: project,
key: 'var_with_variables',
value: 'value 3 and $regular_var and $file_var and $undefined_var')
2021-04-29 21:17:54 +05:30
end
2022-08-27 11:52:29 +05:30
it 'returns variables with expanded' do
expect(runner_variables).to include(
{ key: 'regular_var', value: 'value 1',
public: false, masked: false },
{ key: 'file_var', value: 'value 2',
public: false, masked: false, file: true },
{ key: 'var_with_variables', value: 'value 3 and value 1 and $file_var and $undefined_var',
public: false, masked: false }
)
2021-04-29 21:17:54 +05:30
end
2022-11-25 23:54:43 +05:30
it 'logs file_variable_is_referenced_in_another_variable' do
expect(Gitlab::AppJsonLogger).to receive(:info).with(
event: 'file_variable_is_referenced_in_another_variable',
project_id: project.id
).once
runner_variables
end
2022-08-27 11:52:29 +05:30
context 'when the FF ci_stop_expanding_file_vars_for_runners is disabled' do
before do
stub_feature_flags(ci_stop_expanding_file_vars_for_runners: false)
end
2021-04-29 21:17:54 +05:30
2022-08-27 11:52:29 +05:30
it 'returns variables with expanded' do
expect(runner_variables).to include(
{ key: 'regular_var', value: 'value 1',
public: false, masked: false },
{ key: 'file_var', value: 'value 2',
public: false, masked: false, file: true },
{ key: 'var_with_variables', value: 'value 3 and value 1 and value 2 and $undefined_var',
public: false, masked: false }
)
end
end
end
2021-04-17 20:07:23 +05:30
end
2021-04-29 21:17:54 +05:30
describe '#runner_variables subset' do
subject { presenter.runner_variables.select { |v| %w[A B C].include?(v.fetch(:key)) } }
2021-04-17 20:07:23 +05:30
let(:build) { create(:ci_build) }
2021-04-29 21:17:54 +05:30
context 'with references in pipeline variables' do
before do
create(:ci_pipeline_variable, key: 'A', value: 'refA-$B', pipeline: build.pipeline)
create(:ci_pipeline_variable, key: 'B', value: 'refB-$C-$D', pipeline: build.pipeline)
create(:ci_pipeline_variable, key: 'C', value: 'value', pipeline: build.pipeline)
end
2021-12-11 22:18:48 +05:30
it 'returns expanded and sorted variables' do
is_expected.to eq [
2022-11-25 23:54:43 +05:30
{ key: 'C', value: 'value', public: false, masked: false },
{ key: 'B', value: 'refB-value-$D', public: false, masked: false },
{ key: 'A', value: 'refA-refB-value-$D', public: false, masked: false }
]
2021-04-29 21:17:54 +05:30
end
2021-04-17 20:07:23 +05:30
end
end
2018-11-18 11:00:15 +05:30
end