debian-mirror-gitlab/spec/models/concerns/prometheus_adapter_spec.rb

198 lines
5.4 KiB
Ruby
Raw Permalink Normal View History

2019-07-07 11:18:12 +05:30
# frozen_string_literal: true
2018-03-27 19:54:05 +05:30
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe PrometheusAdapter, :use_clean_rails_memory_store_caching do
2018-03-27 19:54:05 +05:30
include PrometheusHelpers
include ReactiveCachingHelpers
2023-07-09 08:55:56 +05:30
let_it_be(:project) { create(:project, :with_prometheus_integration) }
2021-09-30 23:02:18 +05:30
let(:integration) { project.prometheus_integration }
2018-03-27 19:54:05 +05:30
2019-07-07 11:18:12 +05:30
let(:described_class) do
Class.new do
include PrometheusAdapter
end
end
2018-03-27 19:54:05 +05:30
let(:environment_query) { Gitlab::Prometheus::Queries::EnvironmentQuery }
describe '#query' do
2020-04-22 19:07:51 +05:30
describe 'validate_query' do
let(:environment) { build_stubbed(:environment, slug: 'env-slug') }
let(:validation_query) { Gitlab::Prometheus::Queries::ValidateQuery.name }
let(:query) { 'avg(response)' }
let(:validation_respone) { { data: { valid: true } } }
around do |example|
2020-11-24 15:15:51 +05:30
freeze_time { example.run }
2020-04-22 19:07:51 +05:30
end
context 'with valid data' do
2021-09-30 23:02:18 +05:30
subject { integration.query(:validate, query) }
2020-04-22 19:07:51 +05:30
before do
2021-09-30 23:02:18 +05:30
stub_reactive_cache(integration, validation_respone, validation_query, query)
2020-04-22 19:07:51 +05:30
end
it 'returns query data' do
is_expected.to eq(query: { valid: true })
end
end
end
2018-03-27 19:54:05 +05:30
describe 'environment' do
let(:environment) { build_stubbed(:environment, slug: 'env-slug') }
around do |example|
2020-11-24 15:15:51 +05:30
freeze_time { example.run }
2018-03-27 19:54:05 +05:30
end
context 'with valid data' do
2021-09-30 23:02:18 +05:30
subject { integration.query(:environment, environment) }
2018-03-27 19:54:05 +05:30
before do
2021-09-30 23:02:18 +05:30
stub_reactive_cache(integration, prometheus_data, environment_query, environment.id)
2018-03-27 19:54:05 +05:30
end
it 'returns reactive data' do
is_expected.to eq(prometheus_metrics_data)
end
end
end
describe 'matched_metrics' do
let(:matched_metrics_query) { Gitlab::Prometheus::Queries::MatchedMetricQuery }
2019-10-12 21:52:04 +05:30
let(:prometheus_client) { double(:prometheus_client, label_values: nil) }
2018-03-27 19:54:05 +05:30
context 'with valid data' do
2021-09-30 23:02:18 +05:30
subject { integration.query(:matched_metrics) }
2018-03-27 19:54:05 +05:30
before do
2021-09-30 23:02:18 +05:30
allow(integration).to receive(:prometheus_client).and_return(prometheus_client)
synchronous_reactive_cache(integration)
2018-03-27 19:54:05 +05:30
end
it 'returns reactive data' do
expect(subject[:success]).to be_truthy
expect(subject[:data]).to eq([])
end
end
end
describe 'deployment' do
let(:deployment) { build_stubbed(:deployment) }
let(:deployment_query) { Gitlab::Prometheus::Queries::DeploymentQuery }
around do |example|
2020-11-24 15:15:51 +05:30
freeze_time { example.run }
2018-03-27 19:54:05 +05:30
end
context 'with valid data' do
2021-09-30 23:02:18 +05:30
subject { integration.query(:deployment, deployment) }
2018-03-27 19:54:05 +05:30
before do
2021-09-30 23:02:18 +05:30
stub_reactive_cache(integration, prometheus_data, deployment_query, deployment.id)
2018-03-27 19:54:05 +05:30
end
it 'returns reactive data' do
expect(subject).to eq(prometheus_metrics_data)
end
end
end
2019-07-07 11:18:12 +05:30
describe 'additional_metrics' do
let(:additional_metrics_environment_query) { Gitlab::Prometheus::Queries::AdditionalMetricsEnvironmentQuery }
let(:environment) { build_stubbed(:environment, slug: 'env-slug') }
let(:time_window) { [1552642245.067, 1552642095.831] }
around do |example|
2020-11-24 15:15:51 +05:30
freeze_time { example.run }
2019-07-07 11:18:12 +05:30
end
context 'with valid data' do
2021-09-30 23:02:18 +05:30
subject { integration.query(:additional_metrics_environment, environment, *time_window) }
2019-07-07 11:18:12 +05:30
before do
2021-09-30 23:02:18 +05:30
stub_reactive_cache(integration, prometheus_data, additional_metrics_environment_query, environment.id, *time_window)
2019-07-07 11:18:12 +05:30
end
it 'returns reactive data' do
expect(subject).to eq(prometheus_data)
end
end
end
2018-03-27 19:54:05 +05:30
end
describe '#calculate_reactive_cache' do
let(:environment) { create(:environment, slug: 'env-slug') }
2020-03-13 15:44:24 +05:30
2018-03-27 19:54:05 +05:30
before do
2021-09-30 23:02:18 +05:30
integration.manual_configuration = true
integration.active = true
2018-03-27 19:54:05 +05:30
end
subject do
2021-09-30 23:02:18 +05:30
integration.calculate_reactive_cache(environment_query.name, environment.id)
2018-03-27 19:54:05 +05:30
end
around do |example|
2020-11-24 15:15:51 +05:30
freeze_time { example.run }
2018-03-27 19:54:05 +05:30
end
2021-09-30 23:02:18 +05:30
context 'when integration is inactive' do
2018-03-27 19:54:05 +05:30
before do
2021-09-30 23:02:18 +05:30
integration.active = false
2018-03-27 19:54:05 +05:30
end
it { is_expected.to be_nil }
end
context 'when Prometheus responds with valid data' do
before do
stub_all_prometheus_requests(environment.slug)
end
it { expect(subject.to_json).to eq(prometheus_data.to_json) }
end
[404, 500].each do |status|
context "when Prometheus responds with #{status}" do
before do
stub_all_prometheus_requests(environment.slug, status: status, body: "QUERY FAILED!")
end
it { is_expected.to eq(success: false, result: %(#{status} - "QUERY FAILED!")) }
end
end
2021-12-11 22:18:48 +05:30
context "when client raises Gitlab::PrometheusClient::ConnectionError" do
before do
stub_any_prometheus_request.to_raise(Gitlab::PrometheusClient::ConnectionError)
end
it { is_expected.to include(success: false, result: kind_of(String)) }
end
2018-03-27 19:54:05 +05:30
end
2019-07-07 11:18:12 +05:30
describe '#build_query_args' do
2021-09-30 23:02:18 +05:30
subject { integration.build_query_args(*args) }
2019-07-07 11:18:12 +05:30
context 'when active record models are included' do
let(:args) { [double(:environment, id: 12)] }
it 'serializes by id' do
is_expected.to eq [12]
end
end
context 'when args are safe for serialization' do
let(:args) { ['stringy arg', 5, 6.0, :symbolic_arg] }
it 'does nothing' do
is_expected.to eq args
end
end
end
2018-03-27 19:54:05 +05:30
end