Commit d6e00f53 authored by Kamil Trzcinski's avatar Kamil Trzcinski

Improve specs and add missing cases that were not supported

parent 2b8292cd
...@@ -2,20 +2,28 @@ module EnvironmentHelper ...@@ -2,20 +2,28 @@ module EnvironmentHelper
def environment_for_build(project, build) def environment_for_build(project, build)
return unless build.environment return unless build.environment
environment_name = ExpandVariables.expand(build.environment, build.variables) project.environments.find_by(name: build.expanded_environment_name)
project.environments.find_by(name: environment_name)
end end
def environment_link_for_build(project, build) def environment_link_for_build(project, build)
environment = environment_for_build(project, build) environment = environment_for_build(project, build)
return unless environment if environment
link_to environment.name, namespace_project_environment_path(project.namespace, project, environment)
link_to environment.name, namespace_project_environment_path(project.namespace, project, environment) else
content_tag :span, build.expanded_environment_name
end
end end
def deployment_link(project, deployment) def deployment_link(deployment)
return unless deployment return unless deployment
link_to "##{deployment.id}", [deployment.project.namespace.becomes(Namespace), deployment.project, deployment.deployable] link_to "##{deployment.id}", [deployment.project.namespace.becomes(Namespace), deployment.project, deployment.deployable]
end end
def last_deployment_link_for_environment_build(project, build)
environment = environment_for_build(project, build)
return unless environment
deployment_link(environment.last_deployment)
end
end end
...@@ -127,8 +127,24 @@ module Ci ...@@ -127,8 +127,24 @@ module Ci
!self.pipeline.statuses.latest.include?(self) !self.pipeline.statuses.latest.include?(self)
end end
def deployable? def expanded_environment_name
self.environment.present? ExpandVariables.expand(environment, variables) if environment
end
def starts_environment?
self.environment.present? && self.environment_action == 'start'
end
def stops_environment?
self.environment.present? && self.environment_action == 'stop'
end
def environment_action
self.options.fetch(:environment, {}).fetch(:action, 'start')
end
def outdated_deployment?
success? && !last_deployment.try(:last?)
end end
def last_deployment def last_deployment
......
...@@ -26,26 +26,29 @@ ...@@ -26,26 +26,29 @@
= link_to namespace_project_runners_path(@build.project.namespace, @build.project) do = link_to namespace_project_runners_path(@build.project.namespace, @build.project) do
Runners page Runners page
- if @build.deployable? - if @build.environment.present? && @build.starts_environment?
.prepend-top-default .prepend-top-default
.environment-information .environment-information
- if @build.status == 'success' && (!@build.last_deployment.try(:last?)) - if @build.outdated_deployment?
= ci_icon_for_status('success_with_warnings') = ci_icon_for_status('success_with_warnings')
- else - else
= ci_icon_for_status(@build.status) = ci_icon_for_status(@build.status)
- last_deployment = @build.last_deployment - environment = environment_for_build(@build.project, @build)
- if @build.complete? - if @build.success? && @build.last_deployment.present?
- if @build.success? - if @build.last_deployment.last?
- if last_deployment.try(:last?) This build is the most recent deployment to #{environment_link_for_build(@build.project, @build)}.
This build is the most recent deployment to #{environment_link_for_build(@build.project, @build)}.
- else
This build is an out-of-date deployment to #{environment_link_for_build(@build.project, @build)}. View the most recent deployment #{deployment_link(@project, last_deployment)}.
- else - else
The deployment of this build to #{environment_link_for_build(@build.project, @build)} did not complete. This build is an out-of-date deployment to #{environment_link_for_build(@build.project, @build)}.
- if environment.last_deployment
View the most recent deployment #{deployment_link(environment.last_deployment)}.
- elsif @build.complete? && !@build.success?
The deployment of this build to #{environment_link_for_build(@build.project, @build)} did not complete.
- else - else
This build is creating a deployment to #{environment_link_for_build(@build.project, @build)} and will overwrite the | This build is creating a deployment to #{environment_link_for_build(@build.project, @build)}
= link_to "latest deployment.", deployment_link(@project, last_deployment) | - if environment.last_deployment
and will overwrite the
= link_to 'latest deployment', deployment_link(environment.last_deployment)
.prepend-top-default .prepend-top-default
- if @build.erased? - if @build.erased?
......
...@@ -4,7 +4,7 @@ class BuildSuccessWorker ...@@ -4,7 +4,7 @@ class BuildSuccessWorker
def perform(build_id) def perform(build_id)
Ci::Build.find_by(id: build_id).try do |build| Ci::Build.find_by(id: build_id).try do |build|
create_deployment(build) if build.deployable? create_deployment(build) if build.environment.present?
end end
end end
......
...@@ -1052,4 +1052,100 @@ describe Ci::Build, models: true do ...@@ -1052,4 +1052,100 @@ describe Ci::Build, models: true do
end end
end end
end end
describe '#starts_environment?' do
subject { build.starts_environment? }
context 'when environment is defined' do
before do
build.update(environment: 'review')
end
it { is_expected.to be_truthy }
end
context 'when environment is not defined' do
before do
build.update(environment: nil)
end
it { is_expected.to be_falsey }
end
end
describe '#stops_environment?' do
subject { build.stops_environment? }
context 'when environment is defined' do
before do
build.update(environment: 'review')
end
context 'no action is defined' do
it { is_expected.to be_falsey }
end
context 'and stop action is defined' do
before do
build.update(options: { environment: { action: 'stop' } } )
end
it { is_expected.to be_truthy }
end
end
context 'when environment is not defined' do
before do
build.update(environment: nil)
end
it { is_expected.to be_falsey }
end
end
describe '#last_deployment' do
subject { build.last_deployment }
context 'when multiple deployments is created returns latest one' do
let!(:deployment1) { create(:deployment, deployable: build) }
let!(:deployment2) { create(:deployment, deployable: build) }
it { is_expected.to eq(deployment2) }
end
end
describe '#outdated_deployment?' do
subject { build.outdated_deployment? }
context 'when build succeeded' do
let(:build) { create(:ci_build, :success) }
let!(:deployment) { create(:deployment, deployable: build) }
context 'current deployment is latest' do
it { is_expected.to be_falsey }
end
context 'current deployment is not latest on environment' do
let!(:deployment2) { create(:deployment, environment: deployment.environment) }
it { is_expected.to be_truthy }
end
end
context 'when build failed' do
let(:build) { create(:ci_build, :failed) }
it { is_expected.to be_falsey }
end
end
describe '#expanded_environment_name' do
subject { build.expanded_environment_name }
context 'when environment uses variables' do
let(:build) { create(:ci_build, ref: 'master', environment: 'review/$CI_BUILD_REF_NAME') }
it { is_expected.to eq('review/master') }
end
end
end end
...@@ -4,6 +4,14 @@ describe Ci::Build, models: true do ...@@ -4,6 +4,14 @@ describe Ci::Build, models: true do
let(:build) { create(:ci_build) } let(:build) { create(:ci_build) }
let(:test_trace) { 'This is a test' } let(:test_trace) { 'This is a test' }
describe 'ss' do
it { is_expected.to belong_to(:runner) }
it { is_expected.to belong_to(:trigger_request) }
it { is_expected.to belong_to(:erased_by) }
it { is_expected.to have_many(:deployments) }
end
describe '#trace' do describe '#trace' do
it 'obfuscates project runners token' do it 'obfuscates project runners token' do
allow(build).to receive(:raw_trace).and_return("Test: #{build.project.runners_token}") allow(build).to receive(:raw_trace).and_return("Test: #{build.project.runners_token}")
......
...@@ -54,7 +54,7 @@ describe 'projects/builds/show', :view do ...@@ -54,7 +54,7 @@ describe 'projects/builds/show', :view do
it 'shows deployment message' do it 'shows deployment message' do
expected_text = 'This build is an out-of-date deployment ' \ expected_text = 'This build is an out-of-date deployment ' \
"to staging. View the most recent deployment ##{first_deployment.id}" "to staging.\nView the most recent deployment ##{second_deployment.id}."
render render
expect(rendered).to have_css('.environment-information', text: expected_text) expect(rendered).to have_css('.environment-information', text: expected_text)
......
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