2019-12-21 20:55:43 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe Gitlab::Ci::Pipeline::Seed::Environment do
|
2019-12-21 20:55:43 +05:30
|
|
|
let_it_be(:project) { create(:project) }
|
|
|
|
let(:job) { build(:ci_build, project: project) }
|
|
|
|
let(:seed) { described_class.new(job) }
|
|
|
|
let(:attributes) { {} }
|
|
|
|
|
|
|
|
before do
|
|
|
|
job.assign_attributes(**attributes)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#to_resource' do
|
|
|
|
subject { seed.to_resource }
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
shared_examples_for 'returning a correct environment' do
|
2019-12-21 20:55:43 +05:30
|
|
|
it 'returns a persisted environment object' do
|
2020-04-08 14:13:33 +05:30
|
|
|
expect { subject }.to change { Environment.count }.by(1)
|
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
expect(subject).to be_a(Environment)
|
|
|
|
expect(subject).to be_persisted
|
|
|
|
expect(subject.project).to eq(project)
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(subject.name).to eq(expected_environment_name)
|
2019-12-21 20:55:43 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'when environment has already existed' do
|
2020-04-08 14:13:33 +05:30
|
|
|
let!(:environment) { create(:environment, project: project, name: expected_environment_name) }
|
2019-12-21 20:55:43 +05:30
|
|
|
|
|
|
|
it 'returns the existing environment object' do
|
2020-04-08 14:13:33 +05:30
|
|
|
expect { subject }.not_to change { Environment.count }
|
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
expect(subject).to be_persisted
|
|
|
|
expect(subject).to eq(environment)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-04-08 14:13:33 +05:30
|
|
|
|
|
|
|
context 'when job has environment attribute' do
|
|
|
|
let(:environment_name) { 'production' }
|
|
|
|
let(:expected_environment_name) { 'production' }
|
|
|
|
|
|
|
|
let(:attributes) do
|
|
|
|
{
|
|
|
|
environment: environment_name,
|
|
|
|
options: { environment: { name: environment_name } }
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'returning a correct environment'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when job starts a review app' do
|
|
|
|
let(:environment_name) { 'review/$CI_COMMIT_REF_NAME' }
|
|
|
|
let(:expected_environment_name) { "review/#{job.ref}" }
|
|
|
|
|
|
|
|
let(:attributes) do
|
|
|
|
{
|
|
|
|
environment: environment_name,
|
|
|
|
options: { environment: { name: environment_name } }
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'returning a correct environment'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when job stops a review app' do
|
|
|
|
let(:environment_name) { 'review/$CI_COMMIT_REF_NAME' }
|
|
|
|
let(:expected_environment_name) { "review/#{job.ref}" }
|
|
|
|
|
|
|
|
let(:attributes) do
|
|
|
|
{
|
|
|
|
environment: environment_name,
|
|
|
|
options: { environment: { name: environment_name, action: 'stop' } }
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'returning a correct environment'
|
|
|
|
end
|
2019-12-21 20:55:43 +05:30
|
|
|
end
|
|
|
|
end
|