2019-04-15 06:17:05 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-05-19 11:03:10 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-03 14:08:28 -04:00
|
|
|
RSpec.describe MetricsController, :request_store do
|
2017-05-19 11:03:10 -04:00
|
|
|
include StubENV
|
|
|
|
|
2019-08-07 04:15:33 -04:00
|
|
|
let(:metrics_multiproc_dir) { @metrics_multiproc_dir }
|
2017-07-03 11:09:34 -04:00
|
|
|
let(:whitelisted_ip) { '127.0.0.1' }
|
2017-07-04 17:50:18 -04:00
|
|
|
let(:whitelisted_ip_range) { '10.0.0.0/24' }
|
|
|
|
let(:ip_in_whitelisted_range) { '10.0.0.1' }
|
|
|
|
let(:not_whitelisted_ip) { '10.0.1.1' }
|
2017-05-22 13:49:34 -04:00
|
|
|
|
2019-08-07 04:15:33 -04:00
|
|
|
around do |example|
|
|
|
|
Dir.mktmpdir do |path|
|
|
|
|
@metrics_multiproc_dir = path
|
|
|
|
example.run
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-19 11:03:10 -04:00
|
|
|
before do
|
|
|
|
stub_env('IN_MEMORY_APPLICATION_SETTINGS', 'false')
|
2017-07-19 04:54:39 -04:00
|
|
|
allow(Prometheus::Client.configuration).to receive(:multiprocess_files_dir).and_return(metrics_multiproc_dir)
|
2017-05-22 13:49:34 -04:00
|
|
|
allow(Gitlab::Metrics).to receive(:prometheus_metrics_enabled?).and_return(true)
|
2017-07-04 17:50:18 -04:00
|
|
|
allow(Settings.monitoring).to receive(:ip_whitelist).and_return([whitelisted_ip, whitelisted_ip_range])
|
2019-11-07 10:06:33 -05:00
|
|
|
allow_next_instance_of(MetricsService) do |instance|
|
|
|
|
allow(instance).to receive(:metrics_text).and_return("prometheus_counter 1")
|
|
|
|
end
|
2017-05-19 11:03:10 -04:00
|
|
|
end
|
|
|
|
|
2017-05-29 17:23:19 -04:00
|
|
|
describe '#index' do
|
2017-07-04 17:50:18 -04:00
|
|
|
shared_examples_for 'endpoint providing metrics' do
|
2018-07-12 13:37:51 -04:00
|
|
|
it 'returns prometheus metrics' do
|
2017-05-29 17:23:19 -04:00
|
|
|
get :index
|
2017-05-23 10:09:00 -04:00
|
|
|
|
2020-03-31 17:08:05 -04:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2018-07-12 13:37:51 -04:00
|
|
|
expect(response.body).to match(/^prometheus_counter 1$/)
|
2018-06-20 04:21:59 -04:00
|
|
|
end
|
|
|
|
|
2017-05-23 08:55:31 -04:00
|
|
|
context 'prometheus metrics are disabled' do
|
2017-05-23 09:42:36 -04:00
|
|
|
before do
|
|
|
|
allow(Gitlab::Metrics).to receive(:prometheus_metrics_enabled?).and_return(false)
|
|
|
|
end
|
2017-05-23 08:55:31 -04:00
|
|
|
|
|
|
|
it 'returns proper response' do
|
2017-05-29 17:23:19 -04:00
|
|
|
get :index
|
2017-05-23 10:09:00 -04:00
|
|
|
|
2020-03-31 17:08:05 -04:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2017-09-25 17:38:34 -04:00
|
|
|
expect(response.body).to eq("# Metrics are disabled, see: http://test.host/help/administration/monitoring/prometheus/gitlab_metrics#gitlab-prometheus-metrics\n")
|
2017-05-23 08:55:31 -04:00
|
|
|
end
|
|
|
|
end
|
2017-05-19 11:03:10 -04:00
|
|
|
end
|
|
|
|
|
2017-07-04 17:50:18 -04:00
|
|
|
context 'accessed from whitelisted ip' do
|
|
|
|
before do
|
2020-01-13 10:07:53 -05:00
|
|
|
allow(Gitlab::RequestContext.instance).to receive(:client_ip).and_return(whitelisted_ip)
|
2017-07-04 17:50:18 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'endpoint providing metrics'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'accessed from ip in whitelisted range' do
|
|
|
|
before do
|
2020-01-13 10:07:53 -05:00
|
|
|
allow(Gitlab::RequestContext.instance).to receive(:client_ip).and_return(ip_in_whitelisted_range)
|
2017-07-04 17:50:18 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'endpoint providing metrics'
|
|
|
|
end
|
|
|
|
|
2017-07-03 11:09:34 -04:00
|
|
|
context 'accessed from not whitelisted ip' do
|
|
|
|
before do
|
2020-01-13 10:07:53 -05:00
|
|
|
allow(Gitlab::RequestContext.instance).to receive(:client_ip).and_return(not_whitelisted_ip)
|
2017-07-03 11:09:34 -04:00
|
|
|
end
|
|
|
|
|
2018-07-12 13:37:51 -04:00
|
|
|
it 'returns the expected error response' do
|
2017-05-29 17:23:19 -04:00
|
|
|
get :index
|
2017-05-23 10:09:00 -04:00
|
|
|
|
2020-03-31 17:08:05 -04:00
|
|
|
expect(response).to have_gitlab_http_status(:not_found)
|
2017-05-19 11:03:10 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|