Commit f7ef7bea authored by Grzegorz Bizon's avatar Grzegorz Bizon

Add environment factory review app trait

parent 04a8372c
module Ci module Ci
class StopEnvironmentService < BaseService class StopEnvironmentService < BaseService
def execute(ref) attr_reader :ref
@ref = ref
@commit = project.commit(ref)
return unless has_ref_sha_pair? def execute(branch_name)
@ref = branch_name
return unless has_ref_commit_pair?
return unless has_environments? return unless has_environments?
environments.each do |environment| environments.each do |environment|
...@@ -16,8 +17,12 @@ module Ci ...@@ -16,8 +17,12 @@ module Ci
private private
def has_ref_sha_pair? def has_ref_commit_pair?
@ref && @commit ref && commit
end
def commit
@commit ||= project.commit(ref)
end end
def has_environments? def has_environments?
...@@ -25,7 +30,7 @@ module Ci ...@@ -25,7 +30,7 @@ module Ci
end end
def environments def environments
@environments ||= project.environments_for(@ref, @commit) @environments ||= project.environments_for(ref, commit)
end end
end end
end end
...@@ -3,8 +3,9 @@ FactoryGirl.define do ...@@ -3,8 +3,9 @@ FactoryGirl.define do
sha '97de212e80737a608d939f648d959671fb0a0142' sha '97de212e80737a608d939f648d959671fb0a0142'
ref 'master' ref 'master'
tag false tag false
user
project nil project nil
deployable factory: :ci_build
environment factory: :environment environment factory: :environment
after(:build) do |deployment, evaluator| after(:build) do |deployment, evaluator|
......
...@@ -4,5 +4,28 @@ FactoryGirl.define do ...@@ -4,5 +4,28 @@ FactoryGirl.define do
project factory: :empty_project project factory: :empty_project
sequence(:external_url) { |n| "https://env#{n}.example.gitlab.com" } sequence(:external_url) { |n| "https://env#{n}.example.gitlab.com" }
trait :with_review_app do |environment|
project
# At this point `review app` is an ephemeral concept related to
# deployments being deployed for given environment. There is no
# first-class `review app` available so we need to create set of
# interconnected objects to simulate a review app.
#
after(:create) do |environment|
deployment = create(:deployment,
environment: environment,
project: environment.project,
sha: environment.project.commit.id)
teardown_build = create(:ci_build, :manual,
name: "#{deployment.environment.name}:teardown",
pipeline: deployment.deployable.pipeline)
deployment.update_column(:on_stop, teardown_build.name)
environment.update_attribute(:deployments, [deployment])
end
end
end end
end end
...@@ -8,21 +8,8 @@ describe Ci::StopEnvironmentService, services: true do ...@@ -8,21 +8,8 @@ describe Ci::StopEnvironmentService, services: true do
describe '#execute' do describe '#execute' do
context 'when environment exists' do context 'when environment exists' do
let(:environment) { create(:environment, project: project) }
let(:deployable) { create(:ci_build) }
let(:stop_build) do
create(:ci_build, :manual, name: 'environment/teardown',
pipeline: deployable.pipeline)
end
before do before do
create(:deployment, environment: environment, create(:environment, :with_review_app, project: project)
deployable: deployable,
on_stop: stop_build.name,
user: user,
project: project,
sha: project.commit.id)
end end
it 'stops environment' do it 'stops environment' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment