2019-12-21 20:55:43 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe EnvironmentEntity do
|
2020-04-08 14:13:33 +05:30
|
|
|
include Gitlab::Routing.url_helpers
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
let(:request) { double('request') }
|
2017-08-17 22:00:37 +05:30
|
|
|
let(:entity) do
|
2018-11-18 11:00:15 +05:30
|
|
|
described_class.new(environment, request: spy('request'))
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
let_it_be(:user) { create(:user) }
|
|
|
|
let_it_be(:project) { create(:project) }
|
|
|
|
let_it_be(:environment) { create(:environment, project: project) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(entity).to receive(:current_user).and_return(user)
|
|
|
|
end
|
2020-01-01 13:55:28 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
subject { entity.as_json }
|
|
|
|
|
|
|
|
it 'exposes latest deployment' do
|
|
|
|
expect(subject).to include(:last_deployment)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'exposes core elements of environment' do
|
|
|
|
expect(subject).to include(:id, :name, :state, :environment_path)
|
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
it 'exposes folder path' do
|
|
|
|
expect(subject).to include(:folder_path)
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
context 'metrics disabled' do
|
|
|
|
before do
|
|
|
|
allow(environment).to receive(:has_metrics?).and_return(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't expose metrics path" do
|
|
|
|
expect(subject).not_to include(:metrics_path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'metrics enabled' do
|
|
|
|
before do
|
|
|
|
allow(environment).to receive(:has_metrics?).and_return(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'exposes metrics path' do
|
|
|
|
expect(subject).to include(:metrics_path)
|
|
|
|
end
|
|
|
|
end
|
2019-02-15 15:39:39 +05:30
|
|
|
|
|
|
|
context 'with deployment platform' do
|
|
|
|
let(:project) { create(:project, :repository) }
|
|
|
|
let(:environment) { create(:environment, project: project) }
|
|
|
|
|
|
|
|
context 'when deployment platform is a cluster' do
|
|
|
|
before do
|
|
|
|
create(:cluster,
|
|
|
|
:provided_by_gcp,
|
|
|
|
:project,
|
|
|
|
environment_scope: '*',
|
|
|
|
projects: [project])
|
|
|
|
end
|
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
it 'includes cluster_type' do
|
2019-02-15 15:39:39 +05:30
|
|
|
expect(subject).to include(:cluster_type)
|
|
|
|
expect(subject[:cluster_type]).to eq('project_type')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-01-01 13:55:28 +05:30
|
|
|
|
|
|
|
context 'with auto_stop_in' do
|
2020-05-24 23:13:21 +05:30
|
|
|
let(:environment) { create(:environment, :will_auto_stop, project: project) }
|
2020-01-01 13:55:28 +05:30
|
|
|
|
|
|
|
it 'exposes auto stop related information' do
|
2020-05-24 23:13:21 +05:30
|
|
|
project.add_maintainer(user)
|
|
|
|
|
2020-01-01 13:55:28 +05:30
|
|
|
expect(subject).to include(:cancel_auto_stop_path, :auto_stop_at)
|
|
|
|
end
|
|
|
|
end
|
2020-04-08 14:13:33 +05:30
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
context 'with alert' do
|
|
|
|
let!(:environment) { create(:environment, project: project) }
|
|
|
|
let!(:prometheus_alert) { create(:prometheus_alert, project: project, environment: environment) }
|
|
|
|
let!(:alert) { create(:alert_management_alert, :triggered, :prometheus, project: project, environment: environment, prometheus_alert: prometheus_alert) }
|
|
|
|
|
|
|
|
it 'exposes active alert flag' do
|
|
|
|
project.add_maintainer(user)
|
|
|
|
|
|
|
|
expect(subject[:has_opened_alert]).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user does not have permission to read alert' do
|
|
|
|
it 'does not expose active alert flag' do
|
|
|
|
project.add_reporter(user)
|
|
|
|
|
|
|
|
expect(subject[:has_opened_alert]).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
context 'pod_logs' do
|
2020-10-24 23:57:45 +05:30
|
|
|
context 'with reporter access' do
|
2020-05-24 23:13:21 +05:30
|
|
|
before do
|
2020-10-24 23:57:45 +05:30
|
|
|
project.add_reporter(user)
|
2020-05-24 23:13:21 +05:30
|
|
|
end
|
2020-04-08 14:13:33 +05:30
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
it 'does not expose logs keys' do
|
|
|
|
expect(subject).not_to include(:logs_path)
|
|
|
|
expect(subject).not_to include(:logs_api_path)
|
|
|
|
expect(subject).not_to include(:enable_advanced_logs_querying)
|
|
|
|
end
|
2020-04-08 14:13:33 +05:30
|
|
|
end
|
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
context 'with developer access' do
|
2020-05-24 23:13:21 +05:30
|
|
|
before do
|
2020-10-24 23:57:45 +05:30
|
|
|
project.add_developer(user)
|
2020-05-24 23:13:21 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'exposes logs keys' do
|
|
|
|
expect(subject).to include(:logs_path)
|
|
|
|
expect(subject).to include(:logs_api_path)
|
|
|
|
expect(subject).to include(:enable_advanced_logs_querying)
|
|
|
|
end
|
2020-04-08 14:13:33 +05:30
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
it 'uses k8s api when ES is not available' do
|
|
|
|
expect(subject[:logs_api_path]).to eq(k8s_project_logs_path(project, environment_name: environment.name, format: :json))
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'uses ES api when ES is available' do
|
|
|
|
allow(environment).to receive(:elastic_stack_available?).and_return(true)
|
|
|
|
|
|
|
|
expect(subject[:logs_api_path]).to eq(elasticsearch_project_logs_path(project, environment_name: environment.name, format: :json))
|
|
|
|
end
|
2020-04-08 14:13:33 +05:30
|
|
|
end
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|