Commit b5335654 authored by Pawel Chojnacki's avatar Pawel Chojnacki

Check if prometheus_adapter is properly called

parent 5ceec83a
...@@ -21,9 +21,10 @@ module Projects ...@@ -21,9 +21,10 @@ module Projects
private private
def prometheus_adapter def prometheus_adapter
@prometheus_adapter ||= Prometheus::AdapterService.new(project).prometheus_adapter @prometheus_adapter ||= ::Prometheus::AdapterService.new(project).prometheus_adapter
end end
def require_prometheus_metrics! def require_prometheus_metrics!
render_404 unless prometheus_adapter.can_query? render_404 unless prometheus_adapter.can_query?
end end
......
...@@ -7,13 +7,15 @@ describe Projects::Prometheus::MetricsController do ...@@ -7,13 +7,15 @@ describe Projects::Prometheus::MetricsController do
let(:prometheus_adapter) { double('prometheus_adapter', can_query?: true) } let(:prometheus_adapter) { double('prometheus_adapter', can_query?: true) }
before do before do
allow(controller).to receive(:prometheus_adapter).and_return(prometheus_adapter)
project.add_master(user) project.add_master(user)
sign_in(user) sign_in(user)
end end
describe 'GET #active_common' do describe 'GET #active_common' do
before do
allow(controller).to receive(:prometheus_adapter).and_return(prometheus_adapter)
end
context 'when prometheus metrics are enabled' do context 'when prometheus metrics are enabled' do
context 'when data is not present' do context 'when data is not present' do
before do before do
...@@ -52,6 +54,18 @@ describe Projects::Prometheus::MetricsController do ...@@ -52,6 +54,18 @@ describe Projects::Prometheus::MetricsController do
end end
end end
describe '#prometheus_adapter' do
before do
allow(controller).to receive(:project).and_return(project)
end
it 'calls prometheus adapter service' do
expect_any_instance_of(::Prometheus::AdapterService).to receive(:prometheus_adapter)
subject.__send__(:prometheus_adapter)
end
end
def project_params(opts = {}) def project_params(opts = {})
opts.reverse_merge(namespace_id: project.namespace, project_id: project) opts.reverse_merge(namespace_id: project.namespace, project_id: project)
end end
......
...@@ -623,36 +623,10 @@ describe Environment do ...@@ -623,36 +623,10 @@ describe Environment do
end end
describe '#prometheus_adapter' do describe '#prometheus_adapter' do
let(:cluster) { create(:cluster, :provided_by_user, environment_scope: '*', projects: [project]) } it 'calls prometheus adapter service' do
expect_any_instance_of(Prometheus::AdapterService).to receive(:prometheus_adapter)
context 'prometheus service can execute queries' do subject.prometheus_adapter
let(:prometheus_service) { double(:prometheus_service, can_query?: true) }
before do
allow(environment.project).to receive(:find_or_initialize_service).with('prometheus').and_return prometheus_service
end
it 'return prometheus service as prometheus adapter' do
expect(environment.prometheus_adapter).to eq(prometheus_service)
end
end
context "prometheus service can't execute queries" do
let(:prometheus_service) { double(:prometheus_service, can_query?: false) }
context 'with cluster with prometheus installed' do
let!(:prometheus) { create(:clusters_applications_prometheus, :installed, cluster: cluster) }
it 'returns application handling all environments' do
expect(environment.prometheus_adapter).to eq(prometheus)
end
end
context 'with cluster without prometheus installed' do
it 'returns nil' do
expect(environment.prometheus_adapter).to be_nil
end
end
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