2018-02-28 07:08:42 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Gitlab::Ci::Build::Policy::Variables do
|
2018-03-23 08:44:12 -04:00
|
|
|
set(:project) { create(:project) }
|
|
|
|
|
2018-03-23 09:44:30 -04:00
|
|
|
let(:pipeline) do
|
2018-03-23 08:44:12 -04:00
|
|
|
build(:ci_empty_pipeline, project: project, ref: 'master')
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:ci_build) do
|
2018-03-23 09:44:30 -04:00
|
|
|
build(:ci_build, pipeline: pipeline, project: project, ref: 'master')
|
2018-03-23 08:44:12 -04:00
|
|
|
end
|
2018-02-28 07:08:42 -05:00
|
|
|
|
2018-03-23 09:44:30 -04:00
|
|
|
let(:seed) { double('build seed', to_resource: ci_build) }
|
|
|
|
|
2018-02-28 07:08:42 -05:00
|
|
|
before do
|
2018-03-23 09:44:30 -04:00
|
|
|
pipeline.variables.build(key: 'CI_PROJECT_NAME', value: '')
|
2018-02-28 07:08:42 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#satisfied_by?' do
|
|
|
|
it 'is satisfied by a defined and existing variable' do
|
|
|
|
policy = described_class.new(['$CI_PROJECT_ID', '$UNDEFINED'])
|
|
|
|
|
2018-03-23 09:44:30 -04:00
|
|
|
expect(policy).to be_satisfied_by(pipeline, seed)
|
2018-02-28 07:08:42 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'is not satisfied by an overriden empty variable' do
|
|
|
|
policy = described_class.new(['$CI_PROJECT_NAME'])
|
|
|
|
|
2018-03-23 09:44:30 -04:00
|
|
|
expect(policy).not_to be_satisfied_by(pipeline, seed)
|
2018-02-28 07:08:42 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'is satisfied by a truthy pipeline expression' do
|
2018-03-23 09:44:30 -04:00
|
|
|
policy = described_class.new([%($CI_PIPELINE_SOURCE == "#{pipeline.source}")])
|
2018-02-28 07:08:42 -05:00
|
|
|
|
2018-03-23 09:44:30 -04:00
|
|
|
expect(policy).to be_satisfied_by(pipeline, seed)
|
2018-02-28 07:08:42 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'is not satisfied by a falsy pipeline expression' do
|
|
|
|
policy = described_class.new([%($CI_PIPELINE_SOURCE == "invalid source")])
|
|
|
|
|
2018-03-23 09:44:30 -04:00
|
|
|
expect(policy).not_to be_satisfied_by(pipeline, seed)
|
2018-02-28 07:08:42 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'is satisfied by a truthy expression using undefined variable' do
|
|
|
|
policy = described_class.new(['$UNDEFINED', '$UNDEFINED == null'])
|
|
|
|
|
2018-03-23 09:44:30 -04:00
|
|
|
expect(policy).to be_satisfied_by(pipeline, seed)
|
2018-02-28 07:08:42 -05:00
|
|
|
end
|
2018-03-23 08:44:12 -04:00
|
|
|
|
2018-03-27 07:36:30 -04:00
|
|
|
it 'allows to evaluate regular secret variables' do
|
2018-03-27 08:34:55 -04:00
|
|
|
create(:ci_variable, project: project, key: 'SECRET', value: 'my secret')
|
2018-03-27 07:36:30 -04:00
|
|
|
|
2018-03-27 08:34:55 -04:00
|
|
|
policy = described_class.new(["$SECRET == 'my secret'"])
|
2018-03-27 07:36:30 -04:00
|
|
|
|
|
|
|
expect(policy).to be_satisfied_by(pipeline, seed)
|
|
|
|
end
|
|
|
|
|
2018-03-23 08:44:12 -04:00
|
|
|
it 'does not persist neither pipeline nor build' do
|
2018-03-23 09:44:30 -04:00
|
|
|
described_class.new('$VAR').satisfied_by?(pipeline, seed)
|
2018-03-23 08:44:12 -04:00
|
|
|
|
2018-03-23 09:44:30 -04:00
|
|
|
expect(pipeline).not_to be_persisted
|
|
|
|
expect(seed.to_resource).not_to be_persisted
|
2018-03-23 08:44:12 -04:00
|
|
|
end
|
2018-02-28 07:08:42 -05:00
|
|
|
end
|
|
|
|
end
|