2017-03-07 11:57:42 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-07-10 10:24:02 -04:00
|
|
|
describe PrometheusService, :use_clean_rails_memory_store_caching do
|
2017-03-07 11:57:42 -05:00
|
|
|
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
|
|
|
|
|
|
|
|
describe 'Validations' do
|
2018-01-04 09:04:14 -05:00
|
|
|
context 'when manual_configuration is enabled' do
|
2017-06-14 14:18:56 -04:00
|
|
|
before do
|
2018-01-04 09:04:14 -05:00
|
|
|
subject.manual_configuration = true
|
2017-06-14 14:18:56 -04:00
|
|
|
end
|
2017-03-07 11:57:42 -05:00
|
|
|
|
|
|
|
it { is_expected.to validate_presence_of(:api_url) }
|
|
|
|
end
|
|
|
|
|
2018-01-04 09:04:14 -05:00
|
|
|
context 'when manual configuration is disabled' do
|
2017-06-14 14:18:56 -04:00
|
|
|
before do
|
2018-01-04 09:04:14 -05:00
|
|
|
subject.manual_configuration = false
|
2017-06-14 14:18:56 -04:00
|
|
|
end
|
2017-03-07 11:57:42 -05:00
|
|
|
|
|
|
|
it { is_expected.not_to validate_presence_of(:api_url) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#test' do
|
2018-01-04 09:04:14 -05:00
|
|
|
before do
|
|
|
|
service.manual_configuration = true
|
|
|
|
end
|
|
|
|
|
2017-03-07 11:57:42 -05:00
|
|
|
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-01-04 09:04:14 -05:00
|
|
|
expect(service.test[:result]).to eq('Checked API endpoint')
|
2017-03-07 11:57:42 -05:00
|
|
|
expect(service.test[:success]).to be_truthy
|
2018-01-04 09:04:14 -05:00
|
|
|
expect(req_stub).to have_been_requested.twice
|
2017-03-07 11:57:42 -05:00
|
|
|
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-02-23 15:33:33 -05:00
|
|
|
describe '#prometheus_client' do
|
2018-01-04 07:56:07 -05:00
|
|
|
context 'manual configuration is enabled' do
|
|
|
|
let(:api_url) { 'http://some_url' }
|
2018-02-23 15:33:33 -05:00
|
|
|
|
2018-01-04 07:56:07 -05:00
|
|
|
before do
|
2018-02-23 15:33:33 -05:00
|
|
|
subject.active = true
|
2018-01-04 07:56:07 -05:00
|
|
|
subject.manual_configuration = true
|
|
|
|
subject.api_url = api_url
|
|
|
|
end
|
|
|
|
|
2018-02-23 15:33:33 -05:00
|
|
|
it 'returns rest client from api_url' do
|
|
|
|
expect(subject.prometheus_client.url).to eq(api_url)
|
2018-01-04 07:56:07 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'manual configuration is disabled' do
|
2018-02-23 15:33:33 -05:00
|
|
|
let(:api_url) { 'http://some_url' }
|
2018-01-04 07:56:07 -05:00
|
|
|
|
|
|
|
before do
|
2018-02-23 15:33:33 -05:00
|
|
|
subject.manual_configuration = false
|
|
|
|
subject.api_url = api_url
|
2018-01-04 07:56:07 -05:00
|
|
|
end
|
|
|
|
|
2018-02-23 15:33:33 -05:00
|
|
|
it 'no client provided' do
|
|
|
|
expect(subject.prometheus_client).to be_nil
|
2018-01-04 07:56:07 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#prometheus_installed?' do
|
|
|
|
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-01-04 09:04:14 -05:00
|
|
|
expect(service.prometheus_installed?).to be(true)
|
2018-01-04 07:56:07 -05:00
|
|
|
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-01-04 09:04:14 -05:00
|
|
|
expect(service.prometheus_installed?).to be(false)
|
2018-01-04 07:56:07 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'clusters without prometheus' do
|
|
|
|
let(:cluster) { create(:cluster, projects: [project]) }
|
|
|
|
|
|
|
|
it 'returns false' do
|
2018-01-04 09:04:14 -05:00
|
|
|
expect(service.prometheus_installed?).to be(false)
|
2018-01-04 07:56:07 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'no clusters' do
|
|
|
|
it 'returns false' do
|
2018-01-04 09:04:14 -05:00
|
|
|
expect(service.prometheus_installed?).to be(false)
|
2018-01-04 07:56:07 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-01-04 09:46:32 -05:00
|
|
|
|
2018-02-23 19:06:08 -05:00
|
|
|
describe '#synchronize_service_state before_save callback' do
|
2018-01-04 09:46:32 -05:00
|
|
|
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
|
|
|
|
allow(service).to receive(:prometheus_installed?).and_return(true)
|
|
|
|
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-03-07 11:57:42 -05:00
|
|
|
end
|