Commit 4fd0ea05 authored by Peter Leitzen's avatar Peter Leitzen

Merge branch 'sy-dashboard-url-bonus-specs' into 'master'

Move prometheus alert configuration into context

Closes #225330

See merge request gitlab-org/gitlab!35810
parents 933d6ed4 ddb6f269
......@@ -4,7 +4,7 @@ require 'spec_helper'
RSpec.describe AlertManagement::PrometheusAlertPresenter do
let_it_be(:project) { create(:project) }
let_it_be(:prometheus_payload) do
let_it_be(:payload) do
{
'annotations' => {
'title' => 'Alert title',
......@@ -15,8 +15,8 @@ RSpec.describe AlertManagement::PrometheusAlertPresenter do
'generatorURL' => 'http://8d467bd4607a:9090/graph?g0.expr=vector%281%29&g0.tab=1'
}
end
let_it_be(:alert) do
create(:alert_management_alert, :prometheus, project: project, payload: prometheus_payload)
let(:alert) do
create(:alert_management_alert, :prometheus, project: project, payload: payload)
end
subject(:presenter) { described_class.new(alert) }
......@@ -47,8 +47,22 @@ RSpec.describe AlertManagement::PrometheusAlertPresenter do
end
describe '#metrics_dashboard_url' do
it 'is not defined' do
expect(presenter.metrics_dashboard_url).to be_nil
subject { presenter.metrics_dashboard_url }
context 'for a non-prometheus alert' do
it { is_expected.to be_nil }
end
context 'for a self-managed prometheus alert' do
include_context 'self-managed prometheus alert attributes'
it { is_expected.to eq(dashboard_url_for_alert) }
end
context 'for a gitlab-managed prometheus alert' do
include_context 'gitlab-managed prometheus alert attributes'
it { is_expected.to eq(dashboard_url_for_alert) }
end
end
end
......@@ -20,38 +20,6 @@ RSpec.describe Projects::Prometheus::AlertPresenter do
end
end
shared_context 'self-managed prometheus alert with metrics data' do
let!(:environment) { create(:environment, project: project, name: 'production') }
let(:title) { 'title' }
let(:y_label) { 'y_label' }
let(:query) { 'avg(metric) > 1.0' }
let(:embed_content) do
{
panel_groups: [{
panels: [{
type: 'line-graph',
title: title,
y_label: y_label,
metrics: [{ query_range: query }]
}]
}]
}
end
before do
payload['startsAt'] = starts_at
payload['generatorURL'] = "http://host?g0.expr=#{CGI.escape(query)}"
payload['labels'] ||= {}
payload['labels']['gitlab_environment_name'] = 'production'
payload['annotations'] ||= {}
payload['annotations']['title'] = 'title'
payload['annotations']['gitlab_y_label'] = 'y_label'
end
end
describe '#project_full_path' do
subject { presenter.project_full_path }
......@@ -214,30 +182,30 @@ RSpec.describe Projects::Prometheus::AlertPresenter do
Timecop.freeze(starts_at) { example.run }
end
before do
payload.delete('startsAt')
end
it { is_expected.to eq(expected_markdown) }
end
context 'with a starting time available' do
before do
payload['startsAt'] = starts_at
end
it { is_expected.to eq(expected_markdown) }
end
end
context 'for gitlab-managed prometheus alerts' do
include_context 'gitlab alert'
include_context 'gitlab-managed prometheus alert attributes'
before do
payload['labels'] = { 'gitlab_alert_id' => metric_id }
let(:alert) do
create(:alerting_alert, project: project, metric_id: prometheus_metric_id, payload: payload)
end
it_behaves_like 'markdown with metrics embed'
end
context 'for alerts from a self-managed prometheus' do
include_context 'self-managed prometheus alert with metrics data'
include_context 'self-managed prometheus alert attributes'
it_behaves_like 'markdown with metrics embed'
......@@ -369,28 +337,6 @@ RSpec.describe Projects::Prometheus::AlertPresenter do
it { is_expected.to eq(expected_link) }
end
describe '#metrics_dashboard_url' do
let(:starts_at) { '2018-03-12T09:06:00Z' }
let(:expected_url) do
metrics_dashboard_project_prometheus_alert_url(
project,
metric_id,
environment_id: gitlab_alert.environment_id,
embedded: true,
end: '2018-03-12T09:36:00Z',
start: '2018-03-12T08:36:00Z'
)
end
subject { presenter.metrics_dashboard_url }
before do
payload['startsAt'] = starts_at
end
it { is_expected.to eq(expected_url) }
end
end
context 'without gitlab alert' do
......@@ -431,33 +377,29 @@ RSpec.describe Projects::Prometheus::AlertPresenter do
it { is_expected.to eq(expected_link) }
end
end
describe '#metrics_dashboard_url' do
subject { presenter.metrics_dashboard_url }
describe '#metrics_dashboard_url' do
subject { presenter.metrics_dashboard_url }
context 'for a non-prometheus alert' do
it { is_expected.to be_nil }
end
end
context 'with self-managed prometheus alert with metrics data' do
include_context 'self-managed prometheus alert with metrics data'
context 'for a self-managed prometheus alert' do
include_context 'self-managed prometheus alert attributes'
describe '#metrics_dashboard_url' do
let(:starts_at) { '2018-03-12T09:06:00Z' }
let(:expected_url) do
metrics_dashboard_project_environment_url(
project,
environment,
embed_json: embed_content.to_json,
embedded: true,
end: '2018-03-12T09:36:00Z',
start: '2018-03-12T08:36:00Z'
)
end
let(:prometheus_payload) { payload }
it { is_expected.to eq(dashboard_url_for_alert) }
end
context 'for a gitlab-managed prometheus alert' do
include_context 'gitlab-managed prometheus alert attributes'
subject { presenter.metrics_dashboard_url }
let(:prometheus_payload) { payload }
it { is_expected.to eq(expected_url) }
it { is_expected.to eq(dashboard_url_for_alert) }
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'getting Alert Management Alert Assignees' do
include GraphqlHelpers
let_it_be(:project) { create(:project) }
let_it_be(:current_user) { create(:user) }
let(:fields) do
<<~QUERY
nodes {
iid
metricsDashboardUrl
}
QUERY
end
let(:graphql_query) do
graphql_query_for(
'project',
{ 'fullPath' => project.full_path },
query_graphql_field('alertManagementAlerts', {}, fields)
)
end
let(:alerts) { graphql_data.dig('project', 'alertManagementAlerts', 'nodes') }
let(:first_alert) { alerts.first }
before do
project.add_developer(current_user)
end
context 'with self-managed prometheus payload' do
include_context 'self-managed prometheus alert attributes'
before do
create(:alert_management_alert, :prometheus, project: project, payload: payload)
end
it 'includes the correct metrics dashboard url' do
post_graphql(graphql_query, current_user: current_user)
expect(first_alert).to include('metricsDashboardUrl' => dashboard_url_for_alert)
end
end
context 'with gitlab-managed prometheus payload' do
include_context 'gitlab-managed prometheus alert attributes'
before do
create(:alert_management_alert, :prometheus, project: project, payload: payload, prometheus_alert: prometheus_alert)
end
it 'includes the correct metrics dashboard url' do
post_graphql(graphql_query, current_user: current_user)
expect(first_alert).to include('metricsDashboardUrl' => dashboard_url_for_alert)
end
end
end
......@@ -136,28 +136,6 @@ RSpec.describe 'getting Alert Management Alerts' do
it { expect(alerts.size).to eq(0) }
end
end
context 'with prometheus payload' do
let_it_be(:gitlab_alert) { create(:prometheus_alert, project: project) }
let_it_be(:metric_id) { gitlab_alert.prometheus_metric_id }
let_it_be(:prometheus_payload) { { 'labels' => { 'gitlab_alert_id' => metric_id }, 'startsAt' => '2018-03-12T09:06:00Z' } }
let_it_be(:self_managed_alert) { create(:alert_management_alert, :prometheus, project: project, payload: prometheus_payload) }
let(:expected_url) do
Gitlab::Routing.url_helpers.metrics_dashboard_project_prometheus_alert_url(
project,
metric_id,
environment_id: gitlab_alert.environment_id,
start: '2018-03-12T08:36:00Z',
end: '2018-03-12T09:36:00Z',
embedded: true
)
end
it 'includes a metrics dashboard url' do
expect(first_alert).to include('metricsDashboardUrl' => expected_url)
end
end
end
end
end
# frozen_string_literal: true
# These contexts expect a `project` to be defined.
# It is expected that these contexts are used to create an
# alert.
RSpec.shared_context 'self-managed prometheus alert attributes' do
let_it_be(:environment) { create(:environment, project: project, name: 'production') }
let(:starts_at) { '2018-03-12T09:06:00Z' }
let(:title) { 'title' }
let(:y_label) { 'y_label' }
let(:query) { 'avg(metric) > 1.0' }
let(:embed_content) do
{
panel_groups: [{
panels: [{
type: 'line-graph',
title: title,
y_label: y_label,
metrics: [{ query_range: query }]
}]
}]
}.to_json
end
let(:payload) do
{
'startsAt' => starts_at,
'generatorURL' => "http://host?g0.expr=#{CGI.escape(query)}",
'labels' => {
'gitlab_environment_name' => 'production'
},
'annotations' => {
'title' => title,
'gitlab_y_label' => y_label
}
}
end
let(:dashboard_url_for_alert) do
Gitlab::Routing.url_helpers.metrics_dashboard_project_environment_url(
project,
environment,
embed_json: embed_content,
embedded: true,
end: '2018-03-12T09:36:00Z',
start: '2018-03-12T08:36:00Z'
)
end
end
RSpec.shared_context 'gitlab-managed prometheus alert attributes' do
let_it_be(:prometheus_alert) { create(:prometheus_alert, project: project) }
let(:prometheus_metric_id) { prometheus_alert.prometheus_metric_id }
let(:payload) do
{
'startsAt' => '2018-03-12T09:06:00Z',
'labels' => {
'gitlab_alert_id' => prometheus_metric_id
}
}
end
let(:dashboard_url_for_alert) do
Gitlab::Routing.url_helpers.metrics_dashboard_project_prometheus_alert_url(
project,
prometheus_metric_id,
environment_id: prometheus_alert.environment_id,
embedded: true,
end: '2018-03-12T09:36:00Z',
start: '2018-03-12T08:36:00Z'
)
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment