debian-mirror-gitlab/spec/serializers/environment_entity_spec.rb

203 lines
6.2 KiB
Ruby
Raw Normal View History

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
2021-03-08 18:12:59 +05:30
include KubernetesHelpers
2020-04-08 14:13:33 +05:30
include Gitlab::Routing.url_helpers
2021-03-08 18:12:59 +05:30
let(:request) { double('request', current_user: user, project: project) }
2017-08-17 22:00:37 +05:30
let(:entity) do
2021-02-22 17:27:13 +05:30
described_class.new(environment, request: request)
2017-08-17 22:00:37 +05:30
end
2020-05-24 23:13:21 +05:30
let_it_be(:user) { create(:user) }
2021-02-22 17:27:13 +05:30
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-24 23:13:21 +05:30
before do
2021-02-22 17:27:13 +05:30
allow(request).to receive(:current_user).and_return(user)
allow(request).to receive(:project).and_return(project)
2020-05-24 23:13:21 +05:30
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
2022-05-07 20:08:51 +05:30
expect(subject).to include(:id, :global_id, :name, :state, :environment_path, :tier)
2017-08-17 22:00:37 +05:30
end
2017-09-10 17:25:29 +05:30
it 'exposes folder path' do
expect(subject).to include(:folder_path)
end
2021-02-22 17:27:13 +05:30
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-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
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
2022-07-16 23:28:13 +05:30
context 'with feature flag disabled' do
before do
stub_feature_flags(monitor_logging: false)
end
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
end
2020-04-08 14:13:33 +05:30
end
end
2021-03-08 18:12:59 +05:30
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
2017-08-17 22:00:37 +05:30
end