2019-09-30 21:07:59 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe DeploymentMetrics do
|
2019-09-30 21:07:59 +05:30
|
|
|
describe '#has_metrics?' do
|
|
|
|
subject { described_class.new(deployment.project, deployment).has_metrics? }
|
|
|
|
|
|
|
|
context 'when deployment is failed' do
|
|
|
|
let(:deployment) { create(:deployment, :failed) }
|
|
|
|
|
|
|
|
it { is_expected.to be_falsy }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when deployment is success' do
|
|
|
|
let(:deployment) { create(:deployment, :success) }
|
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
context 'without a monitoring integration' do
|
2019-09-30 21:07:59 +05:30
|
|
|
it { is_expected.to be_falsy }
|
|
|
|
end
|
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
context 'with a Prometheus integration' do
|
|
|
|
let(:prometheus_integration) { instance_double(::Integrations::Prometheus, can_query?: true, configured?: true) }
|
2019-09-30 21:07:59 +05:30
|
|
|
|
|
|
|
before do
|
2021-09-30 23:02:18 +05:30
|
|
|
allow(deployment.project).to receive(:find_or_initialize_integration).with('prometheus').and_return prometheus_integration
|
2019-09-30 21:07:59 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to be_truthy }
|
|
|
|
end
|
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
context 'with a Prometheus integration that cannot query' do
|
|
|
|
let(:prometheus_integration) { instance_double(::Integrations::Prometheus, configured?: true, can_query?: false) }
|
2020-03-13 15:44:24 +05:30
|
|
|
|
|
|
|
before do
|
2021-09-30 23:02:18 +05:30
|
|
|
allow(deployment.project).to receive(:find_or_initialize_integration).with('prometheus').and_return prometheus_integration
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to be_falsy }
|
|
|
|
end
|
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
context 'with a Prometheus integration that is not configured' do
|
|
|
|
let(:prometheus_integration) { instance_double(::Integrations::Prometheus, configured?: false, can_query?: false) }
|
2019-09-30 21:07:59 +05:30
|
|
|
|
|
|
|
before do
|
2021-09-30 23:02:18 +05:30
|
|
|
allow(deployment.project).to receive(:find_or_initialize_integration).with('prometheus').and_return prometheus_integration
|
2019-09-30 21:07:59 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to be_falsy }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a cluster Prometheus' do
|
|
|
|
let(:deployment) { create(:deployment, :success, :on_cluster) }
|
2021-09-04 01:27:46 +05:30
|
|
|
let!(:prometheus) { create(:clusters_integrations_prometheus, cluster: deployment.cluster) }
|
2019-09-30 21:07:59 +05:30
|
|
|
|
|
|
|
before do
|
2021-09-04 01:27:46 +05:30
|
|
|
expect(deployment.cluster.integration_prometheus).to receive(:configured?).and_return(true)
|
2019-09-30 21:07:59 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to be_truthy }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#metrics' do
|
|
|
|
let(:deployment) { create(:deployment, :success) }
|
2021-09-30 23:02:18 +05:30
|
|
|
let(:prometheus_adapter) { instance_double(::Integrations::Prometheus, can_query?: true, configured?: true) }
|
2019-09-30 21:07:59 +05:30
|
|
|
let(:deployment_metrics) { described_class.new(deployment.project, deployment) }
|
|
|
|
|
|
|
|
subject { deployment_metrics.metrics }
|
|
|
|
|
|
|
|
context 'metrics are disabled' do
|
|
|
|
it { is_expected.to eq({}) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'metrics are enabled' do
|
|
|
|
let(:simple_metrics) do
|
|
|
|
{
|
|
|
|
success: true,
|
|
|
|
metrics: {},
|
|
|
|
last_update: 42
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(deployment_metrics).to receive(:prometheus_adapter).and_return(prometheus_adapter)
|
|
|
|
expect(prometheus_adapter).to receive(:query).with(:deployment, deployment).and_return(simple_metrics)
|
|
|
|
end
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
it { is_expected.to eq(simple_metrics.merge({ deployment_time: deployment.finished_at.to_i })) }
|
2019-09-30 21:07:59 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#additional_metrics' do
|
|
|
|
let(:project) { create(:project, :repository) }
|
|
|
|
let(:deployment) { create(:deployment, :succeed, project: project) }
|
|
|
|
let(:deployment_metrics) { described_class.new(deployment.project, deployment) }
|
|
|
|
|
|
|
|
subject { deployment_metrics.additional_metrics }
|
|
|
|
|
|
|
|
context 'metrics are disabled' do
|
|
|
|
it { is_expected.to eq({}) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'metrics are enabled' do
|
|
|
|
let(:simple_metrics) do
|
|
|
|
{
|
|
|
|
success: true,
|
|
|
|
metrics: {},
|
|
|
|
last_update: 42
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
let(:prometheus_adapter) { instance_double('prometheus_adapter', can_query?: true, configured?: true) }
|
2019-09-30 21:07:59 +05:30
|
|
|
|
|
|
|
before do
|
|
|
|
allow(deployment_metrics).to receive(:prometheus_adapter).and_return(prometheus_adapter)
|
|
|
|
expect(prometheus_adapter).to receive(:query).with(:additional_metrics_deployment, deployment).and_return(simple_metrics)
|
|
|
|
end
|
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
it { is_expected.to eq(simple_metrics.merge({ deployment_time: deployment.finished_at.to_i })) }
|
2019-09-30 21:07:59 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|