2019-09-30 05:06:31 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-10-09 12:17:40 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 02:09:01 -04:00
|
|
|
RSpec.describe EnvironmentStatusEntity do
|
2018-10-09 12:17:40 -04:00
|
|
|
let(:user) { create(:user) }
|
2019-11-29 04:06:31 -05:00
|
|
|
let(:request) { double('request', project: project) }
|
2018-10-09 12:17:40 -04:00
|
|
|
|
2018-11-04 19:37:40 -05:00
|
|
|
let(:deployment) { create(:deployment, :succeed, :review_app) }
|
|
|
|
let(:environment) { deployment.environment }
|
2018-10-09 12:17:40 -04:00
|
|
|
let(:project) { deployment.project }
|
|
|
|
let(:merge_request) { create(:merge_request, :deployed_review_app, deployment: deployment) }
|
|
|
|
|
2019-07-01 04:27:58 -04:00
|
|
|
let(:environment_status) { EnvironmentStatus.new(project, environment, merge_request, merge_request.diff_head_sha) }
|
2018-10-09 12:17:40 -04:00
|
|
|
let(:entity) { described_class.new(environment_status, request: request) }
|
|
|
|
|
|
|
|
subject { entity.as_json }
|
|
|
|
|
|
|
|
before do
|
2018-11-06 08:08:33 -05:00
|
|
|
deployment.update(sha: merge_request.diff_head_sha)
|
2018-10-09 12:17:40 -04:00
|
|
|
allow(request).to receive(:current_user).and_return(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to include(:id) }
|
|
|
|
it { is_expected.to include(:name) }
|
|
|
|
it { is_expected.to include(:url) }
|
|
|
|
it { is_expected.to include(:external_url) }
|
|
|
|
it { is_expected.to include(:external_url_formatted) }
|
|
|
|
it { is_expected.to include(:deployed_at) }
|
|
|
|
it { is_expected.to include(:deployed_at_formatted) }
|
2019-11-29 04:06:31 -05:00
|
|
|
it { is_expected.to include(:details) }
|
2018-10-18 10:17:24 -04:00
|
|
|
it { is_expected.to include(:changes) }
|
2018-10-29 07:34:41 -04:00
|
|
|
it { is_expected.to include(:status) }
|
2018-10-09 12:17:40 -04:00
|
|
|
|
|
|
|
it { is_expected.not_to include(:stop_url) }
|
|
|
|
it { is_expected.not_to include(:metrics_url) }
|
|
|
|
it { is_expected.not_to include(:metrics_monitoring_url) }
|
|
|
|
|
|
|
|
context 'when the user is project maintainer' do
|
|
|
|
before do
|
|
|
|
project.add_maintainer(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to include(:stop_url) }
|
|
|
|
end
|
2018-11-14 06:11:27 -05:00
|
|
|
|
|
|
|
context 'when deployment has metrics' do
|
2019-12-20 07:07:40 -05:00
|
|
|
let(:prometheus_adapter) { double('prometheus_adapter', can_query?: true, configured?: true) }
|
2018-11-14 06:11:27 -05:00
|
|
|
|
|
|
|
let(:simple_metrics) do
|
|
|
|
{
|
|
|
|
success: true,
|
|
|
|
metrics: {},
|
|
|
|
last_update: 42
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
project.add_maintainer(user)
|
|
|
|
allow(deployment).to receive(:prometheus_adapter).and_return(prometheus_adapter)
|
|
|
|
allow(entity).to receive(:deployment).and_return(deployment)
|
2019-07-01 04:27:58 -04:00
|
|
|
|
|
|
|
expect_next_instance_of(DeploymentMetrics) do |deployment_metrics|
|
|
|
|
allow(deployment_metrics).to receive(:prometheus_adapter).and_return(prometheus_adapter)
|
|
|
|
|
|
|
|
allow(prometheus_adapter).to receive(:query)
|
|
|
|
.with(:deployment, deployment).and_return(simple_metrics)
|
|
|
|
end
|
2018-11-14 06:11:27 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when deployment succeeded' do
|
|
|
|
let(:deployment) { create(:deployment, :succeed, :review_app) }
|
|
|
|
|
|
|
|
it 'returns metrics url' do
|
|
|
|
expect(subject[:metrics_url])
|
2019-12-03 04:09:09 -05:00
|
|
|
.to eq("/#{project.full_path}/-/environments/#{environment.id}/deployments/#{deployment.iid}/metrics")
|
2018-11-14 06:11:27 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when deployment is running' do
|
|
|
|
let(:deployment) { create(:deployment, :running, :review_app) }
|
|
|
|
|
|
|
|
it 'does not return metrics url' do
|
|
|
|
expect(subject[:metrics_url]).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-10-09 12:17:40 -04:00
|
|
|
end
|