Commit 72003273 authored by Pawel Chojnacki's avatar Pawel Chojnacki

Cleanup PrometheusService tests

parent e308bb0c
...@@ -76,10 +76,6 @@ class PrometheusService < MonitoringService ...@@ -76,10 +76,6 @@ class PrometheusService < MonitoringService
{ success: false, result: err } { success: false, result: err }
end end
def with_reactive_cache(cl, *args)
yield calculate_reactive_cache(cl, *args)
end
def environment_metrics(environment) def environment_metrics(environment)
with_reactive_cache(Gitlab::Prometheus::Queries::EnvironmentQuery.name, environment.id, &method(:rename_data_to_metrics)) with_reactive_cache(Gitlab::Prometheus::Queries::EnvironmentQuery.name, environment.id, &method(:rename_data_to_metrics))
end end
......
...@@ -2,20 +2,22 @@ module Gitlab ...@@ -2,20 +2,22 @@ module Gitlab
module Prometheus module Prometheus
module Queries module Queries
class EnvironmentQuery < BaseQuery class EnvironmentQuery < BaseQuery
def query def query(environment_id)
environment_slug = environment.slug ::Environment.find_by(id: environment_id).try do |environment|
timeframe_start = 8.hours.ago.to_f environment_slug = environment.slug
timeframe_end = Time.now.to_f timeframe_start = 8.hours.ago.to_f
timeframe_end = Time.now.to_f
memory_query = raw_memory_usage_query(environment_slug) memory_query = raw_memory_usage_query(environment_slug)
cpu_query = raw_cpu_usage_query(environment_slug) cpu_query = raw_cpu_usage_query(environment_slug)
{ {
memory_values: client_query_range(memory_query, start: timeframe_start, stop: timeframe_end), memory_values: client_query_range(memory_query, start: timeframe_start, stop: timeframe_end),
memory_current: client_query(memory_query, time: timeframe_end), memory_current: client_query(memory_query, time: timeframe_end),
cpu_values: client_query_range(cpu_query, start: timeframe_start, stop: timeframe_end), cpu_values: client_query_range(cpu_query, start: timeframe_start, stop: timeframe_end),
cpu_current: client_query(cpu_query, time: timeframe_end) cpu_current: client_query(cpu_query, time: timeframe_end)
} }
end
end end
end end
end end
......
...@@ -8,24 +8,22 @@ describe PrometheusService, :use_clean_rails_memory_store_caching do ...@@ -8,24 +8,22 @@ describe PrometheusService, :use_clean_rails_memory_store_caching do
let(:service) { project.prometheus_service } let(:service) { project.prometheus_service }
let(:environment_query) { Gitlab::Prometheus::Queries::EnvironmentQuery } let(:environment_query) { Gitlab::Prometheus::Queries::EnvironmentQuery }
subject { project.prometheus_service }
describe "Associations" do describe "Associations" do
it { is_expected.to belong_to :project } it { is_expected.to belong_to :project }
end end
describe 'Validations' do describe 'Validations' do
context 'when service is active' do context 'when manual_configuration is enabled' do
before do before do
subject.active = true subject.manual_configuration = true
end end
it { is_expected.to validate_presence_of(:api_url) } it { is_expected.to validate_presence_of(:api_url) }
end end
context 'when service is inactive' do context 'when manual configuration is disabled' do
before do before do
subject.active = false subject.manual_configuration = false
end end
it { is_expected.not_to validate_presence_of(:api_url) } it { is_expected.not_to validate_presence_of(:api_url) }
...@@ -33,12 +31,17 @@ describe PrometheusService, :use_clean_rails_memory_store_caching do ...@@ -33,12 +31,17 @@ describe PrometheusService, :use_clean_rails_memory_store_caching do
end end
describe '#test' do describe '#test' do
before do
service.manual_configuration = true
end
let!(:req_stub) { stub_prometheus_request(prometheus_query_url('1'), body: prometheus_value_body('vector')) } let!(:req_stub) { stub_prometheus_request(prometheus_query_url('1'), body: prometheus_value_body('vector')) }
context 'success' do context 'success' do
it 'reads the discovery endpoint' do it 'reads the discovery endpoint' do
expect(service.test[:result]).to eq('Checked API endpoint')
expect(service.test[:success]).to be_truthy expect(service.test[:success]).to be_truthy
expect(req_stub).to have_been_requested expect(req_stub).to have_been_requested.twice
end end
end end
...@@ -85,7 +88,7 @@ describe PrometheusService, :use_clean_rails_memory_store_caching do ...@@ -85,7 +88,7 @@ describe PrometheusService, :use_clean_rails_memory_store_caching do
let(:fake_deployment_time) { 10 } let(:fake_deployment_time) { 10 }
before do before do
stub_reactive_cache(service, prometheus_data, deployment_query, deployment.id) stub_reactive_cache(service, prometheus_data, deployment_query, deployment.environment.id, deployment.id)
end end
it 'returns reactive data' do it 'returns reactive data' do
...@@ -98,13 +101,17 @@ describe PrometheusService, :use_clean_rails_memory_store_caching do ...@@ -98,13 +101,17 @@ describe PrometheusService, :use_clean_rails_memory_store_caching do
describe '#calculate_reactive_cache' do describe '#calculate_reactive_cache' do
let(:environment) { create(:environment, slug: 'env-slug') } let(:environment) { create(:environment, slug: 'env-slug') }
before do
around do |example| service.manual_configuration = true
Timecop.freeze { example.run } service.active = true
end end
subject do subject do
service.calculate_reactive_cache(environment_query.to_s, environment.id) service.calculate_reactive_cache(environment_query.name, environment.id)
end
around do |example|
Timecop.freeze { example.run }
end end
context 'when service is inactive' do context 'when service is inactive' do
...@@ -157,7 +164,7 @@ describe PrometheusService, :use_clean_rails_memory_store_caching do ...@@ -157,7 +164,7 @@ describe PrometheusService, :use_clean_rails_memory_store_caching do
let(:proxy_client) { double('proxy_client') } let(:proxy_client) { double('proxy_client') }
before do before do
subject.manual_configuration = false service.manual_configuration = false
end end
context 'with cluster for all environments with prometheus installed' do context 'with cluster for all environments with prometheus installed' do
...@@ -165,10 +172,10 @@ describe PrometheusService, :use_clean_rails_memory_store_caching do ...@@ -165,10 +172,10 @@ describe PrometheusService, :use_clean_rails_memory_store_caching do
context 'without environment supplied' do context 'without environment supplied' do
it 'returns client handling all environments' do it 'returns client handling all environments' do
expect(subject).to receive(:client_from_cluster).with(cluster_for_all).and_return(proxy_client).twice expect(service).to receive(:client_from_cluster).with(cluster_for_all).and_return(proxy_client).twice
expect(subject.client).to be_instance_of(Gitlab::PrometheusClient) expect(service.client).to be_instance_of(Gitlab::PrometheusClient)
expect(subject.client.rest_client).to eq(proxy_client) expect(service.client.rest_client).to eq(proxy_client)
end end
end end
...@@ -176,10 +183,10 @@ describe PrometheusService, :use_clean_rails_memory_store_caching do ...@@ -176,10 +183,10 @@ describe PrometheusService, :use_clean_rails_memory_store_caching do
let!(:environment) { create(:environment, project: project, name: 'dev') } let!(:environment) { create(:environment, project: project, name: 'dev') }
it 'returns dev cluster client' do it 'returns dev cluster client' do
expect(subject).to receive(:client_from_cluster).with(cluster_for_dev).and_return(proxy_client).twice expect(service).to receive(:client_from_cluster).with(cluster_for_dev).and_return(proxy_client).twice
expect(subject.client(environment.id)).to be_instance_of(Gitlab::PrometheusClient) expect(service.client(environment.id)).to be_instance_of(Gitlab::PrometheusClient)
expect(subject.client(environment.id).rest_client).to eq(proxy_client) expect(service.client(environment.id).rest_client).to eq(proxy_client)
end end
end end
...@@ -187,10 +194,10 @@ describe PrometheusService, :use_clean_rails_memory_store_caching do ...@@ -187,10 +194,10 @@ describe PrometheusService, :use_clean_rails_memory_store_caching do
let!(:environment) { create(:environment, project: project, name: 'prod') } let!(:environment) { create(:environment, project: project, name: 'prod') }
it 'returns dev cluster client' do it 'returns dev cluster client' do
expect(subject).to receive(:client_from_cluster).with(cluster_for_all).and_return(proxy_client).twice expect(service).to receive(:client_from_cluster).with(cluster_for_all).and_return(proxy_client).twice
expect(subject.client(environment.id)).to be_instance_of(Gitlab::PrometheusClient) expect(service.client(environment.id)).to be_instance_of(Gitlab::PrometheusClient)
expect(subject.client(environment.id).rest_client).to eq(proxy_client) expect(service.client(environment.id).rest_client).to eq(proxy_client)
end end
end end
end end
...@@ -198,7 +205,7 @@ describe PrometheusService, :use_clean_rails_memory_store_caching do ...@@ -198,7 +205,7 @@ describe PrometheusService, :use_clean_rails_memory_store_caching do
context 'with cluster for all environments without prometheus installed' do context 'with cluster for all environments without prometheus installed' do
context 'without environment supplied' do context 'without environment supplied' do
it 'raises PrometheusError because cluster was not found' do it 'raises PrometheusError because cluster was not found' do
expect{subject.client}.to raise_error(Gitlab::PrometheusError, /couldn't find cluster with Prometheus installed/) expect{service.client}.to raise_error(Gitlab::PrometheusError, /couldn't find cluster with Prometheus installed/)
end end
end end
...@@ -206,10 +213,10 @@ describe PrometheusService, :use_clean_rails_memory_store_caching do ...@@ -206,10 +213,10 @@ describe PrometheusService, :use_clean_rails_memory_store_caching do
let!(:environment) { create(:environment, project: project, name: 'dev') } let!(:environment) { create(:environment, project: project, name: 'dev') }
it 'returns dev cluster client' do it 'returns dev cluster client' do
expect(subject).to receive(:client_from_cluster).with(cluster_for_dev).and_return(proxy_client).twice expect(service).to receive(:client_from_cluster).with(cluster_for_dev).and_return(proxy_client).twice
expect(subject.client(environment.id)).to be_instance_of(Gitlab::PrometheusClient) expect(service.client(environment.id)).to be_instance_of(Gitlab::PrometheusClient)
expect(subject.client(environment.id).rest_client).to eq(proxy_client) expect(service.client(environment.id).rest_client).to eq(proxy_client)
end end
end end
...@@ -217,7 +224,7 @@ describe PrometheusService, :use_clean_rails_memory_store_caching do ...@@ -217,7 +224,7 @@ describe PrometheusService, :use_clean_rails_memory_store_caching do
let!(:environment) { create(:environment, project: project, name: 'prod') } let!(:environment) { create(:environment, project: project, name: 'prod') }
it 'raises PrometheusError because cluster was not found' do it 'raises PrometheusError because cluster was not found' do
expect{subject.client}.to raise_error(Gitlab::PrometheusError, /couldn't find cluster with Prometheus installed/) expect{service.client}.to raise_error(Gitlab::PrometheusError, /couldn't find cluster with Prometheus installed/)
end end
end end
end end
...@@ -225,14 +232,12 @@ describe PrometheusService, :use_clean_rails_memory_store_caching do ...@@ -225,14 +232,12 @@ describe PrometheusService, :use_clean_rails_memory_store_caching do
end end
describe '#prometheus_installed?' do describe '#prometheus_installed?' do
subject { project.prometheus_service }
context 'clusters with installed prometheus' do context 'clusters with installed prometheus' do
let!(:cluster) { create(:cluster, projects: [project]) } let!(:cluster) { create(:cluster, projects: [project]) }
let!(:prometheus) { create(:clusters_applications_prometheus, :installed, cluster: cluster) } let!(:prometheus) { create(:clusters_applications_prometheus, :installed, cluster: cluster) }
it 'returns true' do it 'returns true' do
expect(subject.prometheus_installed?).to be(true) expect(service.prometheus_installed?).to be(true)
end end
end end
...@@ -241,7 +246,7 @@ describe PrometheusService, :use_clean_rails_memory_store_caching do ...@@ -241,7 +246,7 @@ describe PrometheusService, :use_clean_rails_memory_store_caching do
let!(:prometheus) { create(:clusters_applications_prometheus, cluster: cluster) } let!(:prometheus) { create(:clusters_applications_prometheus, cluster: cluster) }
it 'returns false' do it 'returns false' do
expect(subject.prometheus_installed?).to be(false) expect(service.prometheus_installed?).to be(false)
end end
end end
...@@ -249,13 +254,13 @@ describe PrometheusService, :use_clean_rails_memory_store_caching do ...@@ -249,13 +254,13 @@ describe PrometheusService, :use_clean_rails_memory_store_caching do
let(:cluster) { create(:cluster, projects: [project]) } let(:cluster) { create(:cluster, projects: [project]) }
it 'returns false' do it 'returns false' do
expect(subject.prometheus_installed?).to be(false) expect(service.prometheus_installed?).to be(false)
end end
end end
context 'no clusters' do context 'no clusters' do
it 'returns false' do it 'returns false' do
expect(subject.prometheus_installed?).to be(false) expect(service.prometheus_installed?).to be(false)
end end
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment