debian-mirror-gitlab/spec/models/integrations/datadog_spec.rb

248 lines
8.4 KiB
Ruby
Raw Normal View History

2021-02-22 17:27:13 +05:30
# frozen_string_literal: true
require 'securerandom'
require 'spec_helper'
2021-06-08 01:23:25 +05:30
RSpec.describe Integrations::Datadog do
2021-02-22 17:27:13 +05:30
let_it_be(:project) { create(:project) }
let_it_be(:pipeline) { create(:ci_pipeline, project: project) }
2021-10-27 15:23:28 +05:30
let_it_be(:build) { create(:ci_build, pipeline: pipeline) }
let_it_be(:retried_build) { create(:ci_build, :retried, pipeline: pipeline) }
2021-02-22 17:27:13 +05:30
let(:active) { true }
let(:dd_site) { 'datadoghq.com' }
2021-11-11 11:23:49 +05:30
let(:default_url) { 'https://webhook-intake.datadoghq.com/api/v2/webhook' }
2021-03-11 19:13:27 +05:30
let(:api_url) { '' }
2021-02-22 17:27:13 +05:30
let(:api_key) { SecureRandom.hex(32) }
let(:dd_env) { 'ci' }
let(:dd_service) { 'awesome-gitlab' }
2022-04-04 11:22:00 +05:30
let(:dd_tags) { '' }
2021-02-22 17:27:13 +05:30
2021-09-30 23:02:18 +05:30
let(:expected_hook_url) { default_url + "?dd-api-key=#{api_key}&env=#{dd_env}&service=#{dd_service}" }
2022-10-02 17:18:49 +05:30
let(:hook_url) { default_url + "?dd-api-key={api_key}&env=#{dd_env}&service=#{dd_service}" }
2021-02-22 17:27:13 +05:30
let(:instance) do
described_class.new(
active: active,
project: project,
2021-03-11 19:13:27 +05:30
datadog_site: dd_site,
api_url: api_url,
api_key: api_key,
datadog_env: dd_env,
2022-04-04 11:22:00 +05:30
datadog_service: dd_service,
datadog_tags: dd_tags
2021-02-22 17:27:13 +05:30
)
end
let(:saved_instance) do
instance.save!
instance
end
let(:pipeline_data) { Gitlab::DataBuilder::Pipeline.build(pipeline) }
let(:build_data) { Gitlab::DataBuilder::Build.build(build) }
2022-03-02 08:16:31 +05:30
let(:archive_trace_data) do
create(:ci_job_artifact, :trace, job: build)
Gitlab::DataBuilder::ArchiveTrace.build(build)
end
2021-02-22 17:27:13 +05:30
2022-11-25 23:54:43 +05:30
it_behaves_like Integrations::ResetSecretFields do
let(:integration) { instance }
end
2021-09-30 23:02:18 +05:30
it_behaves_like Integrations::HasWebHook do
let(:integration) { instance }
2022-10-02 17:18:49 +05:30
let(:hook_url) { "#{described_class::URL_TEMPLATE % { datadog_domain: dd_site }}?dd-api-key={api_key}&env=#{dd_env}&service=#{dd_service}" }
2021-02-22 17:27:13 +05:30
end
describe 'validations' do
subject { instance }
context 'when service is active' do
let(:active) { true }
it { is_expected.to validate_presence_of(:api_key) }
it { is_expected.to allow_value(api_key).for(:api_key) }
it { is_expected.not_to allow_value('87dab2403c9d462 87aec4d9214edb1e').for(:api_key) }
it { is_expected.not_to allow_value('................................').for(:api_key) }
context 'when selecting site' do
let(:dd_site) { 'datadoghq.com' }
2021-03-11 19:13:27 +05:30
let(:api_url) { '' }
2021-02-22 17:27:13 +05:30
it { is_expected.to validate_presence_of(:datadog_site) }
it { is_expected.not_to validate_presence_of(:api_url) }
2023-01-13 00:05:48 +05:30
it { is_expected.to allow_value('data-dog-hq.com').for(:datadog_site) }
it { is_expected.to allow_value('dataDOG.com').for(:datadog_site) }
2021-02-22 17:27:13 +05:30
it { is_expected.not_to allow_value('datadog hq.com').for(:datadog_site) }
2023-01-13 00:05:48 +05:30
it { is_expected.not_to allow_value('-datadoghq.com').for(:datadog_site) }
it { is_expected.not_to allow_value('.datadoghq.com').for(:datadog_site) }
it { is_expected.not_to allow_value('datadoghq.com_').for(:datadog_site) }
it { is_expected.not_to allow_value('data-dog').for(:datadog_site) }
it { is_expected.not_to allow_value('datadoghq.com-').for(:datadog_site) }
it { is_expected.not_to allow_value('datadoghq.com.').for(:datadog_site) }
2021-02-22 17:27:13 +05:30
end
context 'with custom api_url' do
2021-03-11 19:13:27 +05:30
let(:dd_site) { '' }
2021-11-11 11:23:49 +05:30
let(:api_url) { 'https://webhook-intake.datad0g.com/api/v2/webhook' }
2021-02-22 17:27:13 +05:30
it { is_expected.not_to validate_presence_of(:datadog_site) }
it { is_expected.to validate_presence_of(:api_url) }
it { is_expected.to allow_value(api_url).for(:api_url) }
it { is_expected.not_to allow_value('example.com').for(:api_url) }
end
context 'when missing site and api_url' do
2021-03-11 19:13:27 +05:30
let(:dd_site) { '' }
let(:api_url) { '' }
2021-02-22 17:27:13 +05:30
it { is_expected.not_to be_valid }
it { is_expected.to validate_presence_of(:datadog_site) }
it { is_expected.to validate_presence_of(:api_url) }
end
2021-03-11 19:13:27 +05:30
context 'when providing both site and api_url' do
let(:dd_site) { 'datadoghq.com' }
let(:api_url) { default_url }
it { is_expected.not_to allow_value('datadog hq.com').for(:datadog_site) }
it { is_expected.not_to allow_value('example.com').for(:api_url) }
end
2022-04-04 11:22:00 +05:30
context 'with custom tags' do
it { is_expected.to allow_value('').for(:datadog_tags) }
it { is_expected.to allow_value('key:value').for(:datadog_tags) }
it { is_expected.to allow_value("key:value\nkey2:value2").for(:datadog_tags) }
it { is_expected.to allow_value("key:value\nkey2:value with spaces and 123?&$").for(:datadog_tags) }
it { is_expected.to allow_value("key:value\n\n\n\nkey2:value2\n").for(:datadog_tags) }
it { is_expected.not_to allow_value('value').for(:datadog_tags) }
it { is_expected.not_to allow_value('key:').for(:datadog_tags) }
it { is_expected.not_to allow_value('key: ').for(:datadog_tags) }
it { is_expected.not_to allow_value(':value').for(:datadog_tags) }
it { is_expected.not_to allow_value("key:value\nINVALID").for(:datadog_tags) }
end
2021-02-22 17:27:13 +05:30
end
2021-09-30 23:02:18 +05:30
context 'when integration is not active' do
2021-02-22 17:27:13 +05:30
let(:active) { false }
it { is_expected.to be_valid }
it { is_expected.not_to validate_presence_of(:api_key) }
end
end
2022-03-02 08:16:31 +05:30
describe '#help' do
subject { instance.help }
it { is_expected.to be_a(String) }
it { is_expected.not_to be_empty }
end
2021-02-22 17:27:13 +05:30
describe '#hook_url' do
subject { instance.hook_url }
context 'with standard site URL' do
2022-10-02 17:18:49 +05:30
it { is_expected.to eq(hook_url) }
2021-02-22 17:27:13 +05:30
end
context 'with custom URL' do
2021-11-11 11:23:49 +05:30
let(:api_url) { 'https://webhook-intake.datad0g.com/api/v2/webhook' }
2021-02-22 17:27:13 +05:30
2022-10-02 17:18:49 +05:30
it { is_expected.to eq(api_url + "?dd-api-key={api_key}&env=#{dd_env}&service=#{dd_service}") }
2021-02-22 17:27:13 +05:30
context 'blank' do
let(:api_url) { '' }
2022-10-02 17:18:49 +05:30
it { is_expected.to eq(hook_url) }
2021-02-22 17:27:13 +05:30
end
end
context 'without optional params' do
2021-03-11 19:13:27 +05:30
let(:dd_service) { '' }
let(:dd_env) { '' }
2022-04-04 11:22:00 +05:30
let(:dd_tags) { '' }
2021-02-22 17:27:13 +05:30
2022-10-02 17:18:49 +05:30
it { is_expected.to eq(default_url + "?dd-api-key={api_key}") }
2021-02-22 17:27:13 +05:30
end
2022-04-04 11:22:00 +05:30
context 'with custom tags' do
let(:dd_tags) { "key:value\nkey2:value, 2" }
let(:escaped_tags) { CGI.escape("key:value,\"key2:value, 2\"") }
2022-10-02 17:18:49 +05:30
it { is_expected.to eq(hook_url + "&tags=#{escaped_tags}") }
2022-04-04 11:22:00 +05:30
context 'and empty lines' do
let(:dd_tags) { "key:value\r\n\n\n\nkey2:value, 2\n" }
2022-10-02 17:18:49 +05:30
it { is_expected.to eq(hook_url + "&tags=#{escaped_tags}") }
2022-04-04 11:22:00 +05:30
end
end
2021-02-22 17:27:13 +05:30
end
describe '#test' do
2021-10-27 15:23:28 +05:30
subject(:result) { saved_instance.test(pipeline_data) }
2021-02-22 17:27:13 +05:30
2021-10-27 15:23:28 +05:30
let(:body) { 'OK' }
let(:status) { 200 }
before do
stub_request(:post, expected_hook_url).to_return(body: body, status: status)
end
context 'when request is successful with a HTTP 200 status' do
2021-02-22 17:27:13 +05:30
it { is_expected.to eq({ success: true, result: 'OK' }) }
end
2021-10-27 15:23:28 +05:30
context 'when request is successful with a HTTP 202 status' do
let(:status) { 202 }
it { is_expected.to eq({ success: true, result: 'OK' }) }
end
context 'when request fails with a HTTP 500 status' do
let(:status) { 500 }
let(:body) { 'CRASH!!!' }
2021-02-22 17:27:13 +05:30
it { is_expected.to eq({ success: false, result: 'CRASH!!!' }) }
end
end
describe '#execute' do
2021-10-27 15:23:28 +05:30
around do |example|
freeze_time { example.run }
end
2021-02-22 17:27:13 +05:30
before do
stub_request(:post, expected_hook_url)
saved_instance.execute(data)
end
context 'with pipeline data' do
let(:data) { pipeline_data }
2022-03-02 08:16:31 +05:30
let(:expected_headers) { { ::Gitlab::WebHooks::GITLAB_EVENT_HEADER => 'Pipeline Hook' } }
2021-10-27 15:23:28 +05:30
let(:expected_body) { data.with_retried_builds.to_json }
2021-02-22 17:27:13 +05:30
2021-10-27 15:23:28 +05:30
it { expect(a_request(:post, expected_hook_url).with(headers: expected_headers, body: expected_body)).to have_been_made }
2021-02-22 17:27:13 +05:30
end
context 'with job data' do
let(:data) { build_data }
2022-03-02 08:16:31 +05:30
let(:expected_headers) { { ::Gitlab::WebHooks::GITLAB_EVENT_HEADER => 'Job Hook' } }
let(:expected_body) { data.to_json }
it { expect(a_request(:post, expected_hook_url).with(headers: expected_headers, body: expected_body)).to have_been_made }
end
context 'with archive trace data' do
let(:data) { archive_trace_data }
let(:expected_headers) { { ::Gitlab::WebHooks::GITLAB_EVENT_HEADER => 'Archive Trace Hook' } }
2021-10-27 15:23:28 +05:30
let(:expected_body) { data.to_json }
2021-02-22 17:27:13 +05:30
2021-10-27 15:23:28 +05:30
it { expect(a_request(:post, expected_hook_url).with(headers: expected_headers, body: expected_body)).to have_been_made }
2021-02-22 17:27:13 +05:30
end
end
end