2020-07-02 17:09:14 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
RSpec.describe OperationsHelper do
|
|
|
|
include Gitlab::Routing
|
|
|
|
|
2020-07-14 20:09:23 -04:00
|
|
|
let_it_be(:user) { create(:user) }
|
|
|
|
let_it_be(:project, reload: true) { create(:project) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
helper.instance_variable_set(:@project, project)
|
|
|
|
allow(helper).to receive(:current_user) { user }
|
|
|
|
end
|
2020-07-02 17:09:14 -04:00
|
|
|
|
2020-07-14 20:09:23 -04:00
|
|
|
describe '#alerts_settings_data' do
|
2020-07-02 17:09:14 -04:00
|
|
|
subject { helper.alerts_settings_data }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(helper).to receive(:can?).with(user, :admin_operations, project) { true }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'initial service configuration' do
|
|
|
|
let_it_be(:alerts_service) { AlertsService.new(project: project) }
|
|
|
|
let_it_be(:prometheus_service) { PrometheusService.new(project: project) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(project).to receive(:find_or_initialize_service).with('alerts').and_return(alerts_service)
|
|
|
|
allow(project).to receive(:find_or_initialize_service).with('prometheus').and_return(prometheus_service)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the correct values' do
|
|
|
|
expect(subject).to eq(
|
|
|
|
'activated' => 'false',
|
|
|
|
'url' => alerts_service.url,
|
|
|
|
'authorization_key' => nil,
|
|
|
|
'form_path' => project_service_path(project, alerts_service),
|
2020-07-03 17:09:31 -04:00
|
|
|
'alerts_setup_url' => help_page_path('user/project/integrations/generic_alerts.md', anchor: 'setting-up-generic-alerts'),
|
2020-07-02 17:09:14 -04:00
|
|
|
'alerts_usage_url' => project_alert_management_index_path(project),
|
|
|
|
'prometheus_form_path' => project_service_path(project, prometheus_service),
|
|
|
|
'prometheus_reset_key_path' => reset_alerting_token_project_settings_operations_path(project),
|
|
|
|
'prometheus_authorization_key' => nil,
|
|
|
|
'prometheus_api_url' => nil,
|
|
|
|
'prometheus_activated' => 'false',
|
2020-07-10 08:09:15 -04:00
|
|
|
'prometheus_url' => notify_project_prometheus_alerts_url(project, format: :json),
|
|
|
|
'disabled' => 'false'
|
2020-07-02 17:09:14 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with external Prometheus configured' do
|
2020-07-08 05:09:17 -04:00
|
|
|
let_it_be(:prometheus_service, reload: true) do
|
|
|
|
create(:prometheus_service, project: project)
|
|
|
|
end
|
2020-07-02 17:09:14 -04:00
|
|
|
|
|
|
|
context 'with external Prometheus enabled' do
|
|
|
|
it 'returns the correct values' do
|
|
|
|
expect(subject).to include(
|
|
|
|
'prometheus_activated' => 'true',
|
|
|
|
'prometheus_api_url' => prometheus_service.api_url
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with external Prometheus disabled' do
|
2020-07-08 05:09:17 -04:00
|
|
|
shared_examples 'Prometheus is disabled' do
|
|
|
|
it 'returns the correct values' do
|
|
|
|
expect(subject).to include(
|
|
|
|
'prometheus_activated' => 'false',
|
|
|
|
'prometheus_api_url' => prometheus_service.api_url
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:cluster_managed) { false }
|
|
|
|
|
2020-07-02 17:09:14 -04:00
|
|
|
before do
|
2020-07-08 05:09:17 -04:00
|
|
|
allow(prometheus_service)
|
|
|
|
.to receive(:prometheus_available?)
|
|
|
|
.and_return(cluster_managed)
|
|
|
|
|
2020-07-02 17:09:14 -04:00
|
|
|
prometheus_service.update!(manual_configuration: false)
|
|
|
|
end
|
|
|
|
|
2020-07-08 05:09:17 -04:00
|
|
|
include_examples 'Prometheus is disabled'
|
|
|
|
|
|
|
|
context 'when cluster managed' do
|
|
|
|
let(:cluster_managed) { true }
|
|
|
|
|
|
|
|
include_examples 'Prometheus is disabled'
|
2020-07-02 17:09:14 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with project alert setting' do
|
|
|
|
let_it_be(:project_alerting_setting) { create(:project_alerting_setting, project: project) }
|
|
|
|
|
|
|
|
it 'returns the correct values' do
|
|
|
|
expect(subject).to include(
|
|
|
|
'prometheus_authorization_key' => project_alerting_setting.token,
|
|
|
|
'prometheus_api_url' => prometheus_service.api_url
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with generic alerts service configured' do
|
|
|
|
let_it_be(:alerts_service) { create(:alerts_service, project: project) }
|
|
|
|
|
|
|
|
context 'with generic alerts enabled' do
|
|
|
|
it 'returns the correct values' do
|
|
|
|
expect(subject).to include(
|
|
|
|
'activated' => 'true',
|
|
|
|
'authorization_key' => alerts_service.token,
|
|
|
|
'url' => alerts_service.url
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with generic alerts disabled' do
|
|
|
|
before do
|
|
|
|
alerts_service.update!(active: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the correct values' do
|
|
|
|
expect(subject).to include(
|
|
|
|
'activated' => 'false',
|
|
|
|
'authorization_key' => alerts_service.token
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-07-14 20:09:23 -04:00
|
|
|
|
|
|
|
describe '#operations_settings_data' do
|
|
|
|
let_it_be(:operations_settings) do
|
|
|
|
create(
|
|
|
|
:project_incident_management_setting,
|
|
|
|
project: project,
|
|
|
|
issue_template_key: 'template-key',
|
|
|
|
pagerduty_active: true
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
subject { helper.operations_settings_data }
|
|
|
|
|
|
|
|
it 'returns the correct set of data' do
|
|
|
|
is_expected.to eq(
|
|
|
|
operations_settings_endpoint: project_settings_operations_path(project),
|
|
|
|
templates: '[]',
|
|
|
|
create_issue: 'false',
|
|
|
|
issue_template_key: 'template-key',
|
|
|
|
send_email: 'false',
|
|
|
|
pagerduty_active: 'true',
|
|
|
|
pagerduty_token: operations_settings.pagerduty_token,
|
|
|
|
pagerduty_webhook_url: project_incidents_pagerduty_url(project, token: operations_settings.pagerduty_token),
|
|
|
|
pagerduty_reset_key_path: reset_pagerduty_token_project_settings_operations_path(project)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2020-07-02 17:09:14 -04:00
|
|
|
end
|