Commit a53b36e3 authored by Kamil Trzciński's avatar Kamil Trzciński

Improve PrometheusAlert EE validation

parent 2a209048
require 'spec_helper' require 'spec_helper'
describe PrometheusAlert do describe PrometheusAlert do
let(:metric) { create(:prometheus_metric) } set(:project) { build(:project) }
let(:metric) { build(:prometheus_metric) }
describe 'associations' do describe 'associations' do
it { is_expected.to belong_to(:project) } it { is_expected.to belong_to(:project) }
it { is_expected.to belong_to(:environment) } it { is_expected.to belong_to(:environment) }
end end
describe 'project validations' do
let(:environment) { build(:environment, project: project) }
let(:metric) { build(:prometheus_metric, project: project) }
subject do
build(:prometheus_alert, prometheus_metric: metric, environment: environment, project: project)
end
context 'when environment and metric belongs same project' do
it { is_expected.to be_valid }
end
context 'when environment belongs to different project' do
let(:environment) { build(:environment) }
it { is_expected.not_to be_valid }
end
context 'when metric belongs to different project' do
let(:metric) { build(:prometheus_metric) }
it { is_expected.not_to be_valid }
end
context 'when metric is common' do
let(:metric) { build(:prometheus_metric, :common) }
it { is_expected.to be_valid }
end
end
describe '#full_query' do describe '#full_query' do
it 'returns the concatenated query' do before do
subject.operator = "gt" subject.operator = "gt"
subject.threshold = 1 subject.threshold = 1
subject.prometheus_metric_id = metric.id subject.prometheus_metric = metric
end
it 'returns the concatenated query' do
expect(subject.full_query).to eq("#{metric.query} > 1.0") expect(subject.full_query).to eq("#{metric.query} > 1.0")
end end
end end
describe '#to_param' do describe '#to_param' do
it 'returns the params of the prometheus alert' do before do
subject.operator = "gt" subject.operator = "gt"
subject.threshold = 1 subject.threshold = 1
subject.prometheus_metric_id = metric.id subject.prometheus_metric = metric
end
alert_params = { it 'returns the params of the prometheus alert' do
expect(subject.to_param).to eq(
"alert" => metric.title, "alert" => metric.title,
"expr" => "#{metric.query} > 1.0", "expr" => "#{metric.query} > 1.0",
"for" => "5m", "for" => "5m",
"labels" => { "labels" => {
"gitlab" => "hook", "gitlab" => "hook",
"gitlab_alert_id" => metric.id "gitlab_alert_id" => metric.id
} })
}
expect(subject.to_param).to eq(alert_params)
end end
end end
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