diff options
Diffstat (limited to 'spec/lib/gitlab/alert_management')
4 files changed, 131 insertions, 109 deletions
diff --git a/spec/lib/gitlab/alert_management/alert_params_spec.rb b/spec/lib/gitlab/alert_management/alert_params_spec.rb deleted file mode 100644 index c3171be5e29..00000000000 --- a/spec/lib/gitlab/alert_management/alert_params_spec.rb +++ /dev/null @@ -1,101 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -RSpec.describe Gitlab::AlertManagement::AlertParams do - let_it_be(:project) { create(:project, :repository, :private) } - - describe '.from_generic_alert' do - let(:started_at) { Time.current.change(usec: 0).rfc3339 } - let(:default_payload) do - { - 'title' => 'Alert title', - 'description' => 'Description', - 'monitoring_tool' => 'Monitoring tool name', - 'service' => 'Service', - 'hosts' => ['gitlab.com'], - 'start_time' => started_at, - 'some' => { 'extra' => { 'payload' => 'here' } } - } - end - - let(:payload) { default_payload } - - subject { described_class.from_generic_alert(project: project, payload: payload) } - - it 'returns Alert compatible parameters' do - is_expected.to eq( - project_id: project.id, - title: 'Alert title', - description: 'Description', - monitoring_tool: 'Monitoring tool name', - service: 'Service', - severity: 'critical', - hosts: ['gitlab.com'], - payload: payload, - started_at: started_at, - ended_at: nil, - fingerprint: nil, - environment: nil - ) - end - - context 'when severity given' do - let(:payload) { default_payload.merge(severity: 'low') } - - it 'returns Alert compatible parameters' do - expect(subject[:severity]).to eq('low') - end - end - - context 'when there are no hosts in the payload' do - let(:payload) { {} } - - it 'hosts param is an empty array' do - expect(subject[:hosts]).to be_empty - end - end - end - - describe '.from_prometheus_alert' do - let(:payload) do - { - 'status' => 'firing', - 'labels' => { - 'alertname' => 'GitalyFileServerDown', - 'channel' => 'gitaly', - 'pager' => 'pagerduty', - 'severity' => 's1' - }, - 'annotations' => { - 'description' => 'Alert description', - 'runbook' => 'troubleshooting/gitaly-down.md', - 'title' => 'Alert title' - }, - 'startsAt' => '2020-04-27T10:10:22.265949279Z', - 'endsAt' => '0001-01-01T00:00:00Z', - 'generatorURL' => 'http://8d467bd4607a:9090/graph?g0.expr=vector%281%29&g0.tab=1', - 'fingerprint' => 'b6ac4d42057c43c1' - } - end - - let(:parsed_alert) { Gitlab::Alerting::Alert.new(project: project, payload: payload) } - - subject { described_class.from_prometheus_alert(project: project, parsed_alert: parsed_alert) } - - it 'returns Alert-compatible params' do - is_expected.to eq( - project_id: project.id, - title: 'Alert title', - description: 'Alert description', - monitoring_tool: 'Prometheus', - payload: payload, - started_at: parsed_alert.starts_at, - ended_at: parsed_alert.ends_at, - fingerprint: parsed_alert.gitlab_fingerprint, - environment: parsed_alert.environment, - prometheus_alert: parsed_alert.gitlab_alert - ) - end - end -end diff --git a/spec/lib/gitlab/alert_management/alert_status_counts_spec.rb b/spec/lib/gitlab/alert_management/alert_status_counts_spec.rb index a2b8f0aa8d4..fceda763717 100644 --- a/spec/lib/gitlab/alert_management/alert_status_counts_spec.rb +++ b/spec/lib/gitlab/alert_management/alert_status_counts_spec.rb @@ -18,7 +18,7 @@ RSpec.describe Gitlab::AlertManagement::AlertStatusCounts do expect(counts.open).to eq(0) expect(counts.all).to eq(0) - AlertManagement::Alert::STATUSES.each_key do |status| + ::AlertManagement::Alert.status_names.each do |status| expect(counts.send(status)).to eq(0) end end @@ -39,7 +39,7 @@ RSpec.describe Gitlab::AlertManagement::AlertStatusCounts do end context 'when filtering params are included' do - let(:params) { { status: AlertManagement::Alert::STATUSES[:resolved] } } + let(:params) { { status: :resolved } } it 'returns the correct counts for each status' do expect(counts.open).to eq(0) diff --git a/spec/lib/gitlab/alert_management/payload/base_spec.rb b/spec/lib/gitlab/alert_management/payload/base_spec.rb index e0f63bad05d..0c26e94e596 100644 --- a/spec/lib/gitlab/alert_management/payload/base_spec.rb +++ b/spec/lib/gitlab/alert_management/payload/base_spec.rb @@ -120,14 +120,107 @@ RSpec.describe Gitlab::AlertManagement::Payload::Base do end describe '#alert_params' do - before do - allow(parsed_payload).to receive(:title).and_return('title') - allow(parsed_payload).to receive(:description).and_return('description') + subject { parsed_payload.alert_params } + + context 'with every key' do + let_it_be(:raw_payload) { { 'key' => 'value' } } + let_it_be(:stubs) do + { + description: 'description', + ends_at: Time.current, + environment: create(:environment, project: project), + gitlab_fingerprint: 'gitlab_fingerprint', + hosts: 'hosts', + monitoring_tool: 'monitoring_tool', + gitlab_alert: create(:prometheus_alert, project: project), + service: 'service', + severity: 'critical', + starts_at: Time.current, + title: 'title' + } + end + + let(:expected_result) do + { + description: stubs[:description], + ended_at: stubs[:ends_at], + environment: stubs[:environment], + fingerprint: stubs[:gitlab_fingerprint], + hosts: [stubs[:hosts]], + monitoring_tool: stubs[:monitoring_tool], + payload: raw_payload, + project_id: project.id, + prometheus_alert: stubs[:gitlab_alert], + service: stubs[:service], + severity: stubs[:severity], + started_at: stubs[:starts_at], + title: stubs[:title] + } + end + + before do + allow(parsed_payload).to receive_messages(stubs) + end + + it { is_expected.to eq(expected_result) } + + it 'can generate a valid new alert' do + expect(::AlertManagement::Alert.new(subject.except(:ended_at))).to be_valid + end end - subject { parsed_payload.alert_params } + context 'with too-long strings' do + let_it_be(:stubs) do + { + description: 'a' * (::AlertManagement::Alert::DESCRIPTION_MAX_LENGTH + 1), + hosts: 'b' * (::AlertManagement::Alert::HOSTS_MAX_LENGTH + 1), + monitoring_tool: 'c' * (::AlertManagement::Alert::TOOL_MAX_LENGTH + 1), + service: 'd' * (::AlertManagement::Alert::SERVICE_MAX_LENGTH + 1), + title: 'e' * (::AlertManagement::Alert::TITLE_MAX_LENGTH + 1) + } + end - it { is_expected.to eq({ description: 'description', project_id: project.id, title: 'title' }) } + before do + allow(parsed_payload).to receive_messages(stubs) + end + + it do + is_expected.to eq({ + description: stubs[:description].truncate(AlertManagement::Alert::DESCRIPTION_MAX_LENGTH), + hosts: ['b' * ::AlertManagement::Alert::HOSTS_MAX_LENGTH], + monitoring_tool: stubs[:monitoring_tool].truncate(::AlertManagement::Alert::TOOL_MAX_LENGTH), + service: stubs[:service].truncate(::AlertManagement::Alert::SERVICE_MAX_LENGTH), + project_id: project.id, + title: stubs[:title].truncate(::AlertManagement::Alert::TITLE_MAX_LENGTH) + }) + end + end + + context 'with too-long hosts array' do + let(:hosts) { %w(abc def ghij) } + let(:shortened_hosts) { %w(abc def ghi) } + + before do + stub_const('::AlertManagement::Alert::HOSTS_MAX_LENGTH', 9) + allow(parsed_payload).to receive(:hosts).and_return(hosts) + end + + it { is_expected.to eq(hosts: shortened_hosts, project_id: project.id) } + + context 'with host cut off between elements' do + let(:hosts) { %w(abcde fghij) } + let(:shortened_hosts) { %w(abcde fghi) } + + it { is_expected.to eq({ hosts: shortened_hosts, project_id: project.id }) } + end + + context 'with nested hosts' do + let(:hosts) { ['abc', ['de', 'f'], 'g', 'hij'] } # rubocop:disable Style/WordArray + let(:shortened_hosts) { %w(abc de f g hi) } + + it { is_expected.to eq({ hosts: shortened_hosts, project_id: project.id }) } + end + end end describe '#gitlab_fingerprint' do diff --git a/spec/lib/gitlab/alert_management/payload/generic_spec.rb b/spec/lib/gitlab/alert_management/payload/generic_spec.rb index 538a822503e..b7660462b0d 100644 --- a/spec/lib/gitlab/alert_management/payload/generic_spec.rb +++ b/spec/lib/gitlab/alert_management/payload/generic_spec.rb @@ -46,7 +46,7 @@ RSpec.describe Gitlab::AlertManagement::Payload::Generic do subject { parsed_payload.starts_at } around do |example| - Timecop.freeze(current_time) { example.run } + travel_to(current_time) { example.run } end context 'without start_time' do @@ -86,4 +86,34 @@ RSpec.describe Gitlab::AlertManagement::Payload::Generic do it_behaves_like 'parsable alert payload field', 'gitlab_environment_name' end + + describe '#description' do + subject { parsed_payload.description } + + it_behaves_like 'parsable alert payload field', 'description' + end + + describe '#ends_at' do + let(:current_time) { Time.current.change(usec: 0).utc } + + subject { parsed_payload.ends_at } + + around do |example| + travel_to(current_time) { example.run } + end + + context 'without end_time' do + it { is_expected.to be_nil } + end + + context "with end_time" do + let(:value) { 10.minutes.ago.change(usec: 0).utc } + + before do + raw_payload['end_time'] = value.to_s + end + + it { is_expected.to eq(value) } + end + end end |