debian-mirror-gitlab/spec/lib/gitlab/ci/build/policy/variables_spec.rb

139 lines
4.1 KiB
Ruby
Raw Normal View History

2019-10-12 21:52:04 +05:30
# frozen_string_literal: true
2018-05-09 12:01:36 +05:30
require 'spec_helper'
describe Gitlab::Ci::Build::Policy::Variables do
set(:project) { create(:project) }
let(:pipeline) do
build(:ci_empty_pipeline, project: project, ref: 'master', source: :push)
end
let(:ci_build) do
build(:ci_build, pipeline: pipeline, project: project, ref: 'master')
end
2019-12-04 20:38:33 +05:30
let(:seed) do
double('build seed',
to_resource: ci_build,
2019-12-26 22:10:19 +05:30
variables: ci_build.scoped_variables_hash
2019-12-04 20:38:33 +05:30
)
end
2018-05-09 12:01:36 +05:30
before do
pipeline.variables.build(key: 'CI_PROJECT_NAME', value: '')
2019-07-07 11:18:12 +05:30
pipeline.variables.build(key: 'MY_VARIABLE', value: 'my-var')
2018-05-09 12:01:36 +05:30
end
describe '#satisfied_by?' do
it 'is satisfied by at least one matching statement' do
policy = described_class.new(['$CI_PROJECT_ID', '$UNDEFINED'])
expect(policy).to be_satisfied_by(pipeline, seed)
end
2019-07-07 11:18:12 +05:30
it 'is satisfied by a matching pipeline variable' do
policy = described_class.new(['$MY_VARIABLE'])
expect(policy).to be_satisfied_by(pipeline, seed)
end
2018-12-13 13:39:08 +05:30
it 'is not satisfied by an overridden empty variable' do
2018-05-09 12:01:36 +05:30
policy = described_class.new(['$CI_PROJECT_NAME'])
expect(policy).not_to be_satisfied_by(pipeline, seed)
end
it 'is satisfied by a truthy pipeline expression' do
policy = described_class.new([%($CI_PIPELINE_SOURCE == "push")])
expect(policy).to be_satisfied_by(pipeline, seed)
end
it 'is not satisfied by a falsy pipeline expression' do
policy = described_class.new([%($CI_PIPELINE_SOURCE == "invalid source")])
expect(policy).not_to be_satisfied_by(pipeline, seed)
end
it 'is satisfied by a truthy expression using undefined variable' do
policy = described_class.new(['$UNDEFINED == null'])
expect(policy).to be_satisfied_by(pipeline, seed)
end
it 'is not satisfied by a falsy expression using undefined variable' do
policy = described_class.new(['$UNDEFINED'])
expect(policy).not_to be_satisfied_by(pipeline, seed)
end
2018-12-13 13:39:08 +05:30
it 'allows to evaluate regular CI variables' do
2018-05-09 12:01:36 +05:30
create(:ci_variable, project: project, key: 'SECRET', value: 'my secret')
policy = described_class.new(["$SECRET == 'my secret'"])
expect(policy).to be_satisfied_by(pipeline, seed)
end
it 'does not persist neither pipeline nor build' do
described_class.new('$VAR').satisfied_by?(pipeline, seed)
expect(pipeline).not_to be_persisted
expect(seed.to_resource).not_to be_persisted
end
2019-07-07 11:18:12 +05:30
context 'when a bridge job is used' do
let(:bridge) do
build(:ci_bridge, pipeline: pipeline, project: project, ref: 'master')
end
2019-12-04 20:38:33 +05:30
let(:seed) do
double('bridge seed',
to_resource: bridge,
2019-12-26 22:10:19 +05:30
variables: ci_build.scoped_variables_hash
2019-12-04 20:38:33 +05:30
)
end
2019-07-07 11:18:12 +05:30
it 'is satisfied by a matching expression for a bridge job' do
policy = described_class.new(['$MY_VARIABLE'])
expect(policy).to be_satisfied_by(pipeline, seed)
end
end
2019-10-12 21:52:04 +05:30
context 'when using project ci variables in environment scope' do
let(:ci_build) do
build(:ci_build, pipeline: pipeline,
project: project,
ref: 'master',
stage: 'review',
environment: 'test/$CI_JOB_STAGE/1')
end
before do
create(:ci_variable, project: project,
key: 'SCOPED_VARIABLE',
value: 'my-value-1')
create(:ci_variable, project: project,
key: 'SCOPED_VARIABLE',
value: 'my-value-2',
environment_scope: 'test/review/*')
end
it 'is satisfied by scoped variable match' do
policy = described_class.new(['$SCOPED_VARIABLE == "my-value-2"'])
expect(policy).to be_satisfied_by(pipeline, seed)
end
it 'is not satisfied when matching against overridden variable' do
policy = described_class.new(['$SCOPED_VARIABLE == "my-value-1"'])
expect(policy).not_to be_satisfied_by(pipeline, seed)
end
end
2018-05-09 12:01:36 +05:30
end
end