2017-03-07 16:57:42 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-07-10 14:24:02 +00:00
|
|
|
describe PrometheusService, :use_clean_rails_memory_store_caching do
|
2017-03-07 16:57:42 +00:00
|
|
|
include PrometheusHelpers
|
|
|
|
include ReactiveCachingHelpers
|
|
|
|
|
|
|
|
let(:project) { create(:prometheus_project) }
|
|
|
|
let(:service) { project.prometheus_service }
|
2017-04-26 20:09:03 +00:00
|
|
|
let(:environment_query) { Gitlab::Prometheus::Queries::EnvironmentQuery }
|
2017-03-07 16:57:42 +00:00
|
|
|
|
|
|
|
describe "Associations" do
|
|
|
|
it { is_expected.to belong_to :project }
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'Validations' do
|
|
|
|
context 'when service is active' do
|
2017-06-14 18:18:56 +00:00
|
|
|
before do
|
|
|
|
subject.active = true
|
|
|
|
end
|
2017-03-07 16:57:42 +00:00
|
|
|
|
|
|
|
it { is_expected.to validate_presence_of(:api_url) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when service is inactive' do
|
2017-06-14 18:18:56 +00:00
|
|
|
before do
|
|
|
|
subject.active = false
|
|
|
|
end
|
2017-03-07 16:57:42 +00:00
|
|
|
|
|
|
|
it { is_expected.not_to validate_presence_of(:api_url) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#test' do
|
|
|
|
let!(:req_stub) { stub_prometheus_request(prometheus_query_url('1'), body: prometheus_value_body('vector')) }
|
|
|
|
|
|
|
|
context 'success' do
|
|
|
|
it 'reads the discovery endpoint' do
|
|
|
|
expect(service.test[:success]).to be_truthy
|
|
|
|
expect(req_stub).to have_been_requested
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'failure' do
|
|
|
|
let!(:req_stub) { stub_prometheus_request(prometheus_query_url('1'), status: 404) }
|
|
|
|
|
|
|
|
it 'fails to read the discovery endpoint' do
|
|
|
|
expect(service.test[:success]).to be_falsy
|
|
|
|
expect(req_stub).to have_been_requested
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-26 20:09:03 +00:00
|
|
|
describe '#environment_metrics' do
|
2017-03-07 16:57:42 +00:00
|
|
|
let(:environment) { build_stubbed(:environment, slug: 'env-slug') }
|
|
|
|
|
|
|
|
around do |example|
|
|
|
|
Timecop.freeze { example.run }
|
|
|
|
end
|
|
|
|
|
2017-04-26 20:09:03 +00:00
|
|
|
context 'with valid data' do
|
|
|
|
subject { service.environment_metrics(environment) }
|
2017-05-09 04:15:34 +00:00
|
|
|
|
|
|
|
before do
|
2017-04-26 20:09:03 +00:00
|
|
|
stub_reactive_cache(service, prometheus_data, environment_query, environment.id)
|
2017-05-09 04:15:34 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns reactive data' do
|
2017-06-06 09:16:15 +00:00
|
|
|
is_expected.to eq(prometheus_metrics_data)
|
2017-05-09 04:15:34 +00:00
|
|
|
end
|
|
|
|
end
|
2017-04-26 20:09:03 +00:00
|
|
|
end
|
2017-05-09 04:15:34 +00:00
|
|
|
|
2017-04-26 20:09:03 +00:00
|
|
|
describe '#deployment_metrics' do
|
2017-06-22 19:19:55 +00:00
|
|
|
let(:deployment) { build_stubbed(:deployment) }
|
2017-04-26 20:09:03 +00:00
|
|
|
let(:deployment_query) { Gitlab::Prometheus::Queries::DeploymentQuery }
|
|
|
|
|
|
|
|
around do |example|
|
|
|
|
Timecop.freeze { example.run }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with valid data' do
|
|
|
|
subject { service.deployment_metrics(deployment) }
|
2017-06-22 19:19:55 +00:00
|
|
|
let(:fake_deployment_time) { 10 }
|
2017-05-09 04:15:34 +00:00
|
|
|
|
2017-03-07 16:57:42 +00:00
|
|
|
before do
|
2017-04-26 20:09:03 +00:00
|
|
|
stub_reactive_cache(service, prometheus_data, deployment_query, deployment.id)
|
2017-03-07 16:57:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns reactive data' do
|
2017-06-22 19:19:55 +00:00
|
|
|
expect(deployment).to receive(:created_at).and_return(fake_deployment_time)
|
|
|
|
|
|
|
|
expect(subject).to eq(prometheus_metrics_data.merge(deployment_time: fake_deployment_time))
|
2017-03-07 16:57:42 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#calculate_reactive_cache' do
|
2017-04-26 20:09:03 +00:00
|
|
|
let(:environment) { create(:environment, slug: 'env-slug') }
|
2017-03-07 16:57:42 +00:00
|
|
|
|
|
|
|
around do |example|
|
|
|
|
Timecop.freeze { example.run }
|
|
|
|
end
|
|
|
|
|
|
|
|
subject do
|
2017-04-26 20:09:03 +00:00
|
|
|
service.calculate_reactive_cache(environment_query.to_s, environment.id)
|
2017-03-07 16:57:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when service is inactive' do
|
|
|
|
before do
|
|
|
|
service.active = false
|
|
|
|
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) }
|
2017-06-06 09:16:15 +00:00
|
|
|
it { expect(subject.to_json).to eq(prometheus_data.to_json) }
|
2017-03-07 16:57:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
[404, 500].each do |status|
|
|
|
|
context "when Prometheus responds with #{status}" do
|
|
|
|
before do
|
2017-05-02 21:53:49 +00:00
|
|
|
stub_all_prometheus_requests(environment.slug, status: status, body: "QUERY FAILED!")
|
2017-03-07 16:57:42 +00:00
|
|
|
end
|
|
|
|
|
2017-05-02 21:53:49 +00:00
|
|
|
it { is_expected.to eq(success: false, result: %(#{status} - "QUERY FAILED!")) }
|
2017-03-07 16:57:42 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|