debian-mirror-gitlab/spec/models/project_services/prometheus_service_spec.rb

239 lines
7.2 KiB
Ruby
Raw Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
require 'spec_helper'
2017-09-10 17:25:29 +05:30
describe PrometheusService, :use_clean_rails_memory_store_caching do
2017-08-17 22:00:37 +05:30
include PrometheusHelpers
include ReactiveCachingHelpers
let(:project) { create(:prometheus_project) }
let(:service) { project.prometheus_service }
describe "Associations" do
it { is_expected.to belong_to :project }
end
2018-11-29 20:51:05 +05:30
context 'redirects' do
it 'does not follow redirects' do
redirect_to = 'https://redirected.example.com'
redirect_req_stub = stub_prometheus_request(prometheus_query_url('1'), status: 302, headers: { location: redirect_to })
redirected_req_stub = stub_prometheus_request(redirect_to, body: { 'status': 'success' })
result = service.test
# result = { success: false, result: error }
expect(result[:success]).to be_falsy
expect(result[:result]).to be_instance_of(Gitlab::PrometheusClient::Error)
expect(redirect_req_stub).to have_been_requested
expect(redirected_req_stub).not_to have_been_requested
end
end
2017-08-17 22:00:37 +05:30
describe 'Validations' do
2018-03-17 18:26:18 +05:30
context 'when manual_configuration is enabled' do
2017-09-10 17:25:29 +05:30
before do
2019-03-13 22:55:13 +05:30
service.manual_configuration = true
2017-09-10 17:25:29 +05:30
end
2017-08-17 22:00:37 +05:30
2019-03-13 22:55:13 +05:30
it 'validates presence of api_url' do
expect(service).to validate_presence_of(:api_url)
end
2017-08-17 22:00:37 +05:30
end
2018-03-17 18:26:18 +05:30
context 'when manual configuration is disabled' do
2017-09-10 17:25:29 +05:30
before do
2019-03-13 22:55:13 +05:30
service.manual_configuration = false
end
it 'does not validate presence of api_url' do
expect(service).not_to validate_presence_of(:api_url)
2017-09-10 17:25:29 +05:30
end
2019-03-13 22:55:13 +05:30
end
context 'when the api_url domain points to localhost or local network' do
let(:domain) { Addressable::URI.parse(service.api_url).hostname }
2017-08-17 22:00:37 +05:30
2019-03-13 22:55:13 +05:30
it 'cannot query' do
expect(service.can_query?).to be true
aggregate_failures do
['127.0.0.1', '192.168.2.3'].each do |url|
allow(Addrinfo).to receive(:getaddrinfo).with(domain, any_args).and_return([Addrinfo.tcp(url, 80)])
expect(service.can_query?).to be false
end
end
end
2017-08-17 22:00:37 +05:30
end
end
describe '#test' do
2018-03-17 18:26:18 +05:30
before do
service.manual_configuration = true
end
2017-08-17 22:00:37 +05:30
let!(:req_stub) { stub_prometheus_request(prometheus_query_url('1'), body: prometheus_value_body('vector')) }
context 'success' do
it 'reads the discovery endpoint' do
2018-03-17 18:26:18 +05:30
expect(service.test[:result]).to eq('Checked API endpoint')
2017-08-17 22:00:37 +05:30
expect(service.test[:success]).to be_truthy
2018-03-17 18:26:18 +05:30
expect(req_stub).to have_been_requested.twice
2017-08-17 22:00:37 +05:30
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
2018-03-27 19:54:05 +05:30
describe '#prometheus_client' do
2019-03-13 22:55:13 +05:30
let(:api_url) { 'http://some_url' }
before do
service.active = true
service.api_url = api_url
service.manual_configuration = manual_configuration
end
2018-03-17 18:26:18 +05:30
context 'manual configuration is enabled' do
2019-03-13 22:55:13 +05:30
let(:manual_configuration) { true }
2018-03-27 19:54:05 +05:30
2019-03-13 22:55:13 +05:30
it 'returns rest client from api_url' do
expect(service.prometheus_client.url).to eq(api_url)
2018-03-17 18:26:18 +05:30
end
2019-03-13 22:55:13 +05:30
it 'calls valid?' do
allow(service).to receive(:valid?).and_call_original
expect(service.prometheus_client).not_to be_nil
expect(service).to have_received(:valid?)
2018-03-17 18:26:18 +05:30
end
end
context 'manual configuration is disabled' do
2019-03-13 22:55:13 +05:30
let(:manual_configuration) { false }
2018-03-17 18:26:18 +05:30
2018-03-27 19:54:05 +05:30
it 'no client provided' do
2019-03-13 22:55:13 +05:30
expect(service.prometheus_client).to be_nil
2018-03-17 18:26:18 +05:30
end
end
end
2018-12-13 13:39:08 +05:30
describe '#prometheus_available?' do
2018-03-17 18:26:18 +05:30
context 'clusters with installed prometheus' do
let!(:cluster) { create(:cluster, projects: [project]) }
let!(:prometheus) { create(:clusters_applications_prometheus, :installed, cluster: cluster) }
it 'returns true' do
2018-12-13 13:39:08 +05:30
expect(service.prometheus_available?).to be(true)
end
end
context 'clusters with updated prometheus' do
let!(:cluster) { create(:cluster, projects: [project]) }
let!(:prometheus) { create(:clusters_applications_prometheus, :updated, cluster: cluster) }
it 'returns true' do
expect(service.prometheus_available?).to be(true)
2018-03-17 18:26:18 +05:30
end
end
context 'clusters without prometheus installed' do
let(:cluster) { create(:cluster, projects: [project]) }
let!(:prometheus) { create(:clusters_applications_prometheus, cluster: cluster) }
it 'returns false' do
2018-12-13 13:39:08 +05:30
expect(service.prometheus_available?).to be(false)
2018-03-17 18:26:18 +05:30
end
end
context 'clusters without prometheus' do
let(:cluster) { create(:cluster, projects: [project]) }
it 'returns false' do
2018-12-13 13:39:08 +05:30
expect(service.prometheus_available?).to be(false)
2018-03-17 18:26:18 +05:30
end
end
context 'no clusters' do
it 'returns false' do
2018-12-13 13:39:08 +05:30
expect(service.prometheus_available?).to be(false)
2018-03-17 18:26:18 +05:30
end
end
end
2018-03-27 19:54:05 +05:30
describe '#synchronize_service_state before_save callback' do
2018-03-17 18:26:18 +05:30
context 'no clusters with prometheus are installed' do
context 'when service is inactive' do
before do
service.active = false
end
it 'activates service when manual_configuration is enabled' do
expect { service.update!(manual_configuration: true) }.to change { service.active }.from(false).to(true)
end
it 'keeps service inactive when manual_configuration is disabled' do
expect { service.update!(manual_configuration: false) }.not_to change { service.active }.from(false)
end
end
context 'when service is active' do
before do
service.active = true
end
it 'keeps the service active when manual_configuration is enabled' do
expect { service.update!(manual_configuration: true) }.not_to change { service.active }.from(true)
end
it 'inactivates the service when manual_configuration is disabled' do
expect { service.update!(manual_configuration: false) }.to change { service.active }.from(true).to(false)
end
end
end
context 'with prometheus installed in the cluster' do
before do
2018-12-13 13:39:08 +05:30
allow(service).to receive(:prometheus_available?).and_return(true)
2018-03-17 18:26:18 +05:30
end
context 'when service is inactive' do
before do
service.active = false
end
it 'activates service when manual_configuration is enabled' do
expect { service.update!(manual_configuration: true) }.to change { service.active }.from(false).to(true)
end
it 'activates service when manual_configuration is disabled' do
expect { service.update!(manual_configuration: false) }.to change { service.active }.from(false).to(true)
end
end
context 'when service is active' do
before do
service.active = true
end
it 'keeps service active when manual_configuration is enabled' do
expect { service.update!(manual_configuration: true) }.not_to change { service.active }.from(true)
end
it 'keeps service active when manual_configuration is disabled' do
expect { service.update!(manual_configuration: false) }.not_to change { service.active }.from(true)
end
end
end
end
2017-08-17 22:00:37 +05:30
end