2019-09-30 05:06:31 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-11-04 08:23:06 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 02:09:01 -04:00
|
|
|
RSpec.describe EnvironmentEntity do
|
2021-01-05 13:10:25 -05:00
|
|
|
include KubernetesHelpers
|
2020-03-11 11:09:37 -04:00
|
|
|
include Gitlab::Routing.url_helpers
|
|
|
|
|
2021-01-05 13:10:25 -05:00
|
|
|
let(:request) { double('request', current_user: user, project: project) }
|
2016-11-04 08:23:06 -04:00
|
|
|
let(:entity) do
|
2020-12-03 16:09:35 -05:00
|
|
|
described_class.new(environment, request: request)
|
2016-11-04 08:23:06 -04:00
|
|
|
end
|
|
|
|
|
2020-05-18 02:08:14 -04:00
|
|
|
let_it_be(:user) { create(:user) }
|
2020-12-03 16:09:35 -05:00
|
|
|
let_it_be(:project) { create(:project, :repository) }
|
|
|
|
let_it_be(:environment, refind: true) { create(:environment, project: project) }
|
|
|
|
|
|
|
|
before_all do
|
|
|
|
project.add_developer(user)
|
|
|
|
end
|
2020-05-18 02:08:14 -04:00
|
|
|
|
|
|
|
before do
|
2020-12-03 16:09:35 -05:00
|
|
|
allow(request).to receive(:current_user).and_return(user)
|
|
|
|
allow(request).to receive(:project).and_return(project)
|
2020-05-18 02:08:14 -04:00
|
|
|
end
|
2019-12-17 13:07:48 -05:00
|
|
|
|
2016-11-04 08:23:06 -04:00
|
|
|
subject { entity.as_json }
|
|
|
|
|
|
|
|
it 'exposes latest deployment' do
|
|
|
|
expect(subject).to include(:last_deployment)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'exposes core elements of environment' do
|
2020-11-08 22:09:03 -05:00
|
|
|
expect(subject).to include(:id, :global_id, :name, :state, :environment_path)
|
2016-11-04 08:23:06 -04:00
|
|
|
end
|
2017-03-31 05:20:11 -04:00
|
|
|
|
2017-08-21 11:46:45 -04:00
|
|
|
it 'exposes folder path' do
|
|
|
|
expect(subject).to include(:folder_path)
|
|
|
|
end
|
|
|
|
|
2020-12-03 16:09:35 -05:00
|
|
|
context 'when there is a successful deployment' do
|
|
|
|
let!(:pipeline) { create(:ci_pipeline, :success, project: project) }
|
|
|
|
let!(:deployable) { create(:ci_build, :success, project: project, pipeline: pipeline) }
|
|
|
|
let!(:deployment) { create(:deployment, :success, project: project, environment: environment, deployable: deployable) }
|
|
|
|
|
|
|
|
it 'exposes it as the latest deployment' do
|
|
|
|
expect(subject[:last_deployment][:sha]).to eq(deployment.sha)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not expose it as an upcoming deployment' do
|
|
|
|
expect(subject[:upcoming_deployment]).to be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the deployment pipeline has the other manual job' do
|
|
|
|
let!(:manual_job) { create(:ci_build, :manual, name: 'stop-review', project: project, pipeline: pipeline) }
|
|
|
|
|
|
|
|
it 'exposes the manual job in the latest deployment' do
|
|
|
|
expect(subject[:last_deployment][:manual_actions].first[:name])
|
|
|
|
.to eq(manual_job.name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when there is a running deployment' do
|
|
|
|
let!(:pipeline) { create(:ci_pipeline, :running, project: project) }
|
|
|
|
let!(:deployable) { create(:ci_build, :running, project: project, pipeline: pipeline) }
|
|
|
|
let!(:deployment) { create(:deployment, :running, project: project, environment: environment, deployable: deployable) }
|
|
|
|
|
|
|
|
it 'does not expose it as the latest deployment' do
|
|
|
|
expect(subject[:last_deployment]).to be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'exposes it as an upcoming deployment' do
|
|
|
|
expect(subject[:upcoming_deployment][:sha]).to eq(deployment.sha)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the deployment pipeline has the other manual job' do
|
|
|
|
let!(:manual_job) { create(:ci_build, :manual, name: 'stop-review', project: project, pipeline: pipeline) }
|
|
|
|
|
|
|
|
it 'does not expose the manual job in the latest deployment' do
|
|
|
|
expect(subject[:upcoming_deployment][:manual_actions]).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-03-31 05:20:11 -04:00
|
|
|
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
|
2018-12-20 04:39:09 -05:00
|
|
|
|
|
|
|
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-04-05 04:43:27 -04:00
|
|
|
it 'includes cluster_type' do
|
2018-12-20 04:39:09 -05:00
|
|
|
expect(subject).to include(:cluster_type)
|
|
|
|
expect(subject[:cluster_type]).to eq('project_type')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-12-10 02:53:40 -05:00
|
|
|
|
|
|
|
context 'with auto_stop_in' do
|
2020-05-18 02:08:14 -04:00
|
|
|
let(:environment) { create(:environment, :will_auto_stop, project: project) }
|
2019-12-10 02:53:40 -05:00
|
|
|
|
|
|
|
it 'exposes auto stop related information' do
|
2020-05-18 02:08:14 -04:00
|
|
|
project.add_maintainer(user)
|
|
|
|
|
2019-12-10 02:53:40 -05:00
|
|
|
expect(subject).to include(:cancel_auto_stop_path, :auto_stop_at)
|
|
|
|
end
|
|
|
|
end
|
2020-03-11 11:09:37 -04:00
|
|
|
|
|
|
|
context 'pod_logs' do
|
2020-08-04 11:09:27 -04:00
|
|
|
context 'with reporter access' do
|
2020-05-18 02:08:14 -04:00
|
|
|
before do
|
2020-08-04 11:09:27 -04:00
|
|
|
project.add_reporter(user)
|
2020-05-18 02:08:14 -04:00
|
|
|
end
|
2020-03-11 11:09:37 -04:00
|
|
|
|
2020-05-18 02:08:14 -04:00
|
|
|
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-03-11 11:09:37 -04:00
|
|
|
end
|
|
|
|
|
2020-08-04 11:09:27 -04:00
|
|
|
context 'with developer access' do
|
2020-05-18 02:08:14 -04:00
|
|
|
before do
|
2020-08-04 11:09:27 -04:00
|
|
|
project.add_developer(user)
|
2020-05-18 02:08:14 -04:00
|
|
|
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-03-11 11:09:37 -04:00
|
|
|
|
2020-05-18 02:08:14 -04:00
|
|
|
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-03-11 11:09:37 -04:00
|
|
|
end
|
|
|
|
end
|
2021-01-05 13:10:25 -05:00
|
|
|
|
|
|
|
context 'with deployment service ready' do
|
|
|
|
before do
|
|
|
|
allow(environment).to receive(:has_terminals?).and_return(true)
|
|
|
|
allow(environment).to receive(:rollout_status).and_return(kube_deployment_rollout_status)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'exposes rollout_status' do
|
|
|
|
expect(subject).to include(:rollout_status)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with deployment service not ready' do
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
|
|
|
it 'does not expose rollout_status' do
|
|
|
|
expect(subject).not_to include(:rollout_status)
|
|
|
|
end
|
|
|
|
end
|
2016-11-04 08:23:06 -04:00
|
|
|
end
|