2019-05-29 13:36:36 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 09:08:32 +00:00
|
|
|
RSpec.describe EnvironmentsHelper do
|
2020-02-18 15:08:51 +00:00
|
|
|
let_it_be(:user) { create(:user) }
|
|
|
|
let_it_be(:project, reload: true) { create(:project, :repository) }
|
|
|
|
let_it_be(:environment) { create(:environment, project: project) }
|
2019-05-29 13:36:36 +00:00
|
|
|
|
|
|
|
describe '#metrics_data' do
|
|
|
|
before do
|
|
|
|
# This is so that this spec also passes in EE.
|
|
|
|
allow(helper).to receive(:current_user).and_return(user)
|
|
|
|
allow(helper).to receive(:can?).and_return(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:metrics_data) { helper.metrics_data(project, environment) }
|
|
|
|
|
|
|
|
it 'returns data' do
|
|
|
|
expect(metrics_data).to include(
|
2020-08-21 12:10:22 +00:00
|
|
|
'settings_path' => edit_project_service_path(project, 'prometheus'),
|
|
|
|
'clusters_path' => project_clusters_path(project),
|
|
|
|
'metrics_dashboard_base_path' => environment_metrics_path(environment),
|
|
|
|
'current_environment_name' => environment.name,
|
|
|
|
'documentation_path' => help_page_path('administration/monitoring/prometheus/index.md'),
|
|
|
|
'add_dashboard_documentation_path' => help_page_path('operations/metrics/dashboards/index.md', anchor: 'add-a-new-dashboard-to-your-project'),
|
|
|
|
'empty_getting_started_svg_path' => match_asset_path('/assets/illustrations/monitoring/getting_started.svg'),
|
|
|
|
'empty_loading_svg_path' => match_asset_path('/assets/illustrations/monitoring/loading.svg'),
|
|
|
|
'empty_no_data_svg_path' => match_asset_path('/assets/illustrations/monitoring/no_data.svg'),
|
|
|
|
'empty_unable_to_connect_svg_path' => match_asset_path('/assets/illustrations/monitoring/unable_to_connect.svg'),
|
|
|
|
'metrics_endpoint' => additional_metrics_project_environment_path(project, environment, format: :json),
|
|
|
|
'deployments_endpoint' => project_environment_deployments_path(project, environment, format: :json),
|
|
|
|
'default_branch' => 'master',
|
|
|
|
'project_path' => project_path(project),
|
|
|
|
'tags_path' => project_tags_path(project),
|
|
|
|
'has_metrics' => "#{environment.has_metrics?}",
|
|
|
|
'external_dashboard_url' => nil,
|
|
|
|
'environment_state' => environment.state,
|
|
|
|
'custom_metrics_path' => project_prometheus_metrics_path(project),
|
|
|
|
'validate_query_path' => validate_query_project_prometheus_metrics_path(project),
|
|
|
|
'custom_metrics_available' => 'true',
|
|
|
|
'alerts_endpoint' => project_prometheus_alerts_path(project, environment_id: environment.id, format: :json),
|
|
|
|
'prometheus_alerts_available' => 'true',
|
|
|
|
'custom_dashboard_base_path' => Gitlab::Metrics::Dashboard::RepoDashboardFinder::DASHBOARD_ROOT,
|
|
|
|
'operations_settings_path' => project_settings_operations_path(project),
|
|
|
|
'can_access_operations_settings' => 'true',
|
2021-05-12 18:10:35 +00:00
|
|
|
'panel_preview_endpoint' => project_metrics_dashboards_builder_path(project, format: :json),
|
|
|
|
'has_managed_prometheus' => 'false'
|
2019-05-29 13:36:36 +00:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2020-07-09 09:09:27 +00:00
|
|
|
context 'without admin_operations permission' do
|
|
|
|
before do
|
|
|
|
allow(helper).to receive(:can?)
|
|
|
|
.with(user, :admin_operations, project)
|
|
|
|
.and_return(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
specify do
|
|
|
|
expect(metrics_data).to include(
|
2020-08-21 12:10:22 +00:00
|
|
|
'can_access_operations_settings' => 'false'
|
2020-07-09 09:09:27 +00:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-21 15:21:10 +00:00
|
|
|
context 'without read_prometheus_alerts permission' do
|
|
|
|
before do
|
|
|
|
allow(helper).to receive(:can?)
|
|
|
|
.with(user, :read_prometheus_alerts, project)
|
|
|
|
.and_return(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false' do
|
|
|
|
expect(metrics_data).to include(
|
2020-08-21 12:10:22 +00:00
|
|
|
'prometheus_alerts_available' => 'false'
|
2020-04-21 15:21:10 +00:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-05-29 13:36:36 +00:00
|
|
|
context 'with metrics_setting' do
|
|
|
|
before do
|
|
|
|
create(:project_metrics_setting, project: project, external_dashboard_url: 'http://gitlab.com')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds external_dashboard_url' do
|
2020-08-21 12:10:22 +00:00
|
|
|
expect(metrics_data['external_dashboard_url']).to eq('http://gitlab.com')
|
2019-05-29 13:36:36 +00:00
|
|
|
end
|
|
|
|
end
|
2020-03-31 12:08:09 +00:00
|
|
|
|
|
|
|
context 'when the environment is not available' do
|
|
|
|
before do
|
|
|
|
environment.stop
|
|
|
|
end
|
|
|
|
|
|
|
|
subject { metrics_data }
|
|
|
|
|
2020-08-21 12:10:22 +00:00
|
|
|
it { is_expected.to include('environment_state' => 'stopped') }
|
2020-03-31 12:08:09 +00:00
|
|
|
end
|
2020-07-15 09:09:34 +00:00
|
|
|
|
|
|
|
context 'when request is from project scoped metrics path' do
|
|
|
|
let(:request) { double('request', path: path) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(helper).to receive(:request).and_return(request)
|
|
|
|
end
|
|
|
|
|
|
|
|
context '/:namespace/:project/-/metrics' do
|
|
|
|
let(:path) { project_metrics_dashboard_path(project) }
|
|
|
|
|
2020-08-21 12:10:22 +00:00
|
|
|
it 'uses correct path for metrics_dashboard_base_path' do
|
|
|
|
expect(metrics_data['metrics_dashboard_base_path']).to eq(project_metrics_dashboard_path(project))
|
2020-07-15 09:09:34 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context '/:namespace/:project/-/metrics/some_custom_dashboard.yml' do
|
|
|
|
let(:path) { "#{project_metrics_dashboard_path(project)}/some_custom_dashboard.yml" }
|
|
|
|
|
2020-08-21 12:10:22 +00:00
|
|
|
it 'uses correct path for metrics_dashboard_base_path' do
|
|
|
|
expect(metrics_data['metrics_dashboard_base_path']).to eq(project_metrics_dashboard_path(project))
|
2020-07-15 09:09:34 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-05-12 18:10:35 +00:00
|
|
|
|
|
|
|
context 'has_managed_prometheus' do
|
2021-06-18 21:10:06 +00:00
|
|
|
context 'without prometheus integration' do
|
2021-05-12 18:10:35 +00:00
|
|
|
it "doesn't have managed prometheus" do
|
|
|
|
expect(metrics_data).to include(
|
|
|
|
'has_managed_prometheus' => 'false'
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-18 21:10:06 +00:00
|
|
|
context 'with prometheus integration' do
|
|
|
|
let_it_be(:prometheus_integration) { create(:prometheus_integration, project: project) }
|
2021-05-12 18:10:35 +00:00
|
|
|
|
2021-06-18 21:10:06 +00:00
|
|
|
context 'when manual prometheus integration is active' do
|
2021-05-12 18:10:35 +00:00
|
|
|
it "doesn't have managed prometheus" do
|
2021-06-18 21:10:06 +00:00
|
|
|
prometheus_integration.update!(manual_configuration: true)
|
2021-05-12 18:10:35 +00:00
|
|
|
|
|
|
|
expect(metrics_data).to include(
|
|
|
|
'has_managed_prometheus' => 'false'
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-18 21:10:06 +00:00
|
|
|
context 'when prometheus integration is inactive' do
|
2021-05-12 18:10:35 +00:00
|
|
|
it "doesn't have managed prometheus" do
|
2021-06-18 21:10:06 +00:00
|
|
|
prometheus_integration.update!(manual_configuration: false)
|
2021-05-12 18:10:35 +00:00
|
|
|
|
|
|
|
expect(metrics_data).to include(
|
|
|
|
'has_managed_prometheus' => 'false'
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when a cluster prometheus is available' do
|
|
|
|
let(:cluster) { create(:cluster, projects: [project]) }
|
|
|
|
|
|
|
|
it 'has managed prometheus' do
|
2021-06-03 12:10:18 +00:00
|
|
|
create(:clusters_integrations_prometheus, cluster: cluster)
|
2021-05-12 18:10:35 +00:00
|
|
|
|
|
|
|
expect(metrics_data).to include(
|
|
|
|
'has_managed_prometheus' => 'true'
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-05-29 13:36:36 +00:00
|
|
|
end
|
2020-04-07 12:09:34 +00:00
|
|
|
|
|
|
|
describe '#custom_metrics_available?' do
|
|
|
|
subject { helper.custom_metrics_available?(project) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
project.add_maintainer(user)
|
|
|
|
|
|
|
|
allow(helper).to receive(:current_user).and_return(user)
|
|
|
|
|
|
|
|
allow(helper).to receive(:can?)
|
|
|
|
.with(user, :admin_project, project)
|
|
|
|
.and_return(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns true' do
|
|
|
|
expect(subject).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
2020-07-14 15:09:05 +00:00
|
|
|
|
|
|
|
describe '#environment_logs_data' do
|
|
|
|
it 'returns logs data' do
|
|
|
|
expected_data = {
|
2020-08-21 12:10:22 +00:00
|
|
|
"environment_name": environment.name,
|
2021-05-13 15:10:20 +00:00
|
|
|
"environments_path": api_v4_projects_environments_path(id: project.id),
|
2020-08-21 12:10:22 +00:00
|
|
|
"environment_id": environment.id,
|
2021-06-08 21:10:05 +00:00
|
|
|
"cluster_applications_documentation_path" => help_page_path('user/clusters/integrations.md', anchor: 'elastic-stack-cluster-integration'),
|
2020-08-21 12:10:22 +00:00
|
|
|
"clusters_path": project_clusters_path(project, format: :json)
|
2020-07-14 15:09:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
expect(helper.environment_logs_data(project, environment)).to eq(expected_data)
|
|
|
|
end
|
|
|
|
end
|
2021-07-23 09:08:49 +00:00
|
|
|
|
|
|
|
describe '#environment_data' do
|
|
|
|
it 'returns the environment as JSON' do
|
|
|
|
expected_data = { id: environment.id,
|
|
|
|
name: environment.name,
|
|
|
|
external_url: environment.external_url }.to_json
|
|
|
|
expect(helper.environment_data(environment)).to eq(expected_data)
|
|
|
|
end
|
|
|
|
end
|
2019-05-29 13:36:36 +00:00
|
|
|
end
|