2020-04-22 19:07:51 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe Metrics::Dashboard::GitlabAlertEmbedService do
|
2020-04-22 19:07:51 +05:30
|
|
|
include MetricsDashboardHelpers
|
|
|
|
|
|
|
|
let_it_be(:alert) { create(:prometheus_alert) }
|
|
|
|
let_it_be(:project) { alert.project }
|
|
|
|
let_it_be(:user) { create(:user) }
|
|
|
|
let(:alert_id) { alert.id }
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
before_all do
|
2020-04-22 19:07:51 +05:30
|
|
|
project.add_maintainer(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.valid_params?' do
|
|
|
|
let(:valid_params) do
|
|
|
|
{
|
|
|
|
embedded: true,
|
|
|
|
prometheus_alert_id: alert_id
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
subject { described_class.valid_params?(params) }
|
|
|
|
|
|
|
|
let(:params) { valid_params }
|
|
|
|
|
|
|
|
it { is_expected.to be_truthy }
|
|
|
|
|
|
|
|
context 'missing embedded' do
|
|
|
|
let(:params) { valid_params.except(:embedded) }
|
|
|
|
|
|
|
|
it { is_expected.to be_falsey }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'not embedded' do
|
|
|
|
let(:params) { valid_params.merge(embedded: 'false') }
|
|
|
|
|
|
|
|
it { is_expected.to be_falsey }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'missing alert id' do
|
|
|
|
let(:params) { valid_params.except(:prometheus_alert_id) }
|
|
|
|
|
|
|
|
it { is_expected.to be_falsey }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'missing alert id' do
|
|
|
|
let(:params) { valid_params.merge(prometheus_alert_id: 'none') }
|
|
|
|
|
|
|
|
it { is_expected.to be_falsey }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#get_dashboard' do
|
|
|
|
let(:service_params) do
|
|
|
|
[
|
|
|
|
project,
|
|
|
|
user,
|
|
|
|
{
|
|
|
|
embedded: true,
|
|
|
|
prometheus_alert_id: alert_id
|
|
|
|
}
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:service_call) { described_class.new(*service_params).get_dashboard }
|
|
|
|
|
|
|
|
context 'when alerting is available' do
|
|
|
|
it_behaves_like 'valid embedded dashboard service response'
|
|
|
|
it_behaves_like 'raises error for users with insufficient permissions'
|
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
it 'generates an panel based on the alert' do
|
2020-04-22 19:07:51 +05:30
|
|
|
result = service_call
|
2020-10-24 23:57:45 +05:30
|
|
|
panel = result[:dashboard][:panel_groups][0][:panels][0]
|
|
|
|
metric = panel[:metrics].first
|
|
|
|
|
|
|
|
expect(panel[:metrics].length).to eq 1
|
|
|
|
expect(panel).to include(
|
|
|
|
title: alert.prometheus_metric.title,
|
|
|
|
y_label: alert.prometheus_metric.y_label,
|
|
|
|
type: 'area-chart'
|
|
|
|
)
|
|
|
|
expect(metric[:metric_id]).to eq alert.prometheus_metric_id
|
2020-04-22 19:07:51 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the metric does not exist' do
|
|
|
|
let(:alert_id) { -4 }
|
|
|
|
|
|
|
|
it_behaves_like 'misconfigured dashboard service response', :not_found
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not cache the unprocessed dashboard' do
|
2020-10-24 23:57:45 +05:30
|
|
|
# Fail spec if any method of Cache class is called.
|
|
|
|
stub_const('Gitlab::Metrics::Dashboard::Cache', double)
|
2020-04-22 19:07:51 +05:30
|
|
|
|
|
|
|
described_class.new(*service_params).get_dashboard
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|