2019-04-02 02:26:09 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Projects::Environments::PrometheusApiController do
|
2019-10-01 20:06:26 -04:00
|
|
|
let_it_be(:project) { create(:project) }
|
|
|
|
let_it_be(:environment) { create(:environment, project: project) }
|
|
|
|
let_it_be(:user) { create(:user) }
|
2019-04-02 02:26:09 -04:00
|
|
|
|
|
|
|
before do
|
|
|
|
project.add_reporter(user)
|
|
|
|
sign_in(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET #proxy' do
|
|
|
|
let(:prometheus_proxy_service) { instance_double(Prometheus::ProxyService) }
|
2019-04-15 11:31:59 -04:00
|
|
|
|
2019-04-03 10:00:22 -04:00
|
|
|
let(:expected_params) do
|
|
|
|
ActionController::Parameters.new(
|
2019-04-04 09:46:45 -04:00
|
|
|
environment_params(
|
|
|
|
proxy_path: 'query',
|
|
|
|
controller: 'projects/environments/prometheus_api',
|
|
|
|
action: 'proxy'
|
|
|
|
)
|
2019-04-03 10:00:22 -04:00
|
|
|
).permit!
|
|
|
|
end
|
2019-04-02 02:26:09 -04:00
|
|
|
|
2019-04-02 12:38:49 -04:00
|
|
|
context 'with valid requests' do
|
|
|
|
before do
|
|
|
|
allow(Prometheus::ProxyService).to receive(:new)
|
2019-04-03 10:00:22 -04:00
|
|
|
.with(environment, 'GET', 'query', expected_params)
|
2019-04-02 12:38:49 -04:00
|
|
|
.and_return(prometheus_proxy_service)
|
2019-04-02 02:26:09 -04:00
|
|
|
|
2019-04-02 12:38:49 -04:00
|
|
|
allow(prometheus_proxy_service).to receive(:execute)
|
|
|
|
.and_return(service_result)
|
|
|
|
end
|
2019-04-02 02:26:09 -04:00
|
|
|
|
2019-04-02 12:38:49 -04:00
|
|
|
context 'with success result' do
|
|
|
|
let(:service_result) { { status: :success, body: prometheus_body } }
|
|
|
|
let(:prometheus_body) { '{"status":"success"}' }
|
|
|
|
let(:prometheus_json_body) { JSON.parse(prometheus_body) }
|
2019-04-02 02:26:09 -04:00
|
|
|
|
2019-04-02 12:38:49 -04:00
|
|
|
it 'returns prometheus response' do
|
|
|
|
get :proxy, params: environment_params
|
2019-04-02 02:26:09 -04:00
|
|
|
|
2019-04-03 10:00:22 -04:00
|
|
|
expect(Prometheus::ProxyService).to have_received(:new)
|
|
|
|
.with(environment, 'GET', 'query', expected_params)
|
2019-04-02 12:38:49 -04:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
expect(json_response).to eq(prometheus_json_body)
|
|
|
|
end
|
2019-04-15 11:31:59 -04:00
|
|
|
|
|
|
|
context 'with format string' do
|
|
|
|
before do
|
|
|
|
expected_params[:query] = %{up{environment="#{environment.slug}"}}
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'replaces variables with values' do
|
|
|
|
get :proxy, params: environment_params.merge(query: 'up{environment="%{ci_environment_slug}"}')
|
|
|
|
|
|
|
|
expect(Prometheus::ProxyService).to have_received(:new)
|
|
|
|
.with(environment, 'GET', 'query', expected_params)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with nil query' do
|
|
|
|
let(:params_without_query) do
|
2019-12-08 22:07:57 -05:00
|
|
|
environment_params.except(:query)
|
2019-04-15 11:31:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
expected_params.delete(:query)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not raise error' do
|
|
|
|
get :proxy, params: params_without_query
|
|
|
|
|
|
|
|
expect(Prometheus::ProxyService).to have_received(:new)
|
|
|
|
.with(environment, 'GET', 'query', expected_params)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-12-18 04:07:38 -05:00
|
|
|
|
|
|
|
context 'with variables' do
|
|
|
|
let(:pod_name) { "pod1" }
|
|
|
|
|
|
|
|
before do
|
|
|
|
expected_params[:query] = %{up{pod_name="#{pod_name}"}}
|
|
|
|
expected_params[:variables] = ['pod_name', pod_name]
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'replaces variables with values' do
|
|
|
|
get :proxy, params: environment_params.merge(
|
|
|
|
query: 'up{pod_name="{{pod_name}}"}', variables: ['pod_name', pod_name]
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:success)
|
|
|
|
expect(Prometheus::ProxyService).to have_received(:new)
|
|
|
|
.with(environment, 'GET', 'query', expected_params)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with invalid variables' do
|
|
|
|
let(:params_with_invalid_variables) do
|
|
|
|
environment_params.merge(
|
|
|
|
query: 'up{pod_name="{{pod_name}}"}', variables: ['a']
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns 400' do
|
|
|
|
get :proxy, params: params_with_invalid_variables
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:bad_request)
|
|
|
|
expect(Prometheus::ProxyService).not_to receive(:new)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-04-02 02:26:09 -04:00
|
|
|
end
|
|
|
|
|
2019-04-02 12:38:49 -04:00
|
|
|
context 'with nil result' do
|
|
|
|
let(:service_result) { nil }
|
2019-04-02 02:26:09 -04:00
|
|
|
|
2019-06-05 03:12:10 -04:00
|
|
|
it 'returns 204 no_content' do
|
2019-04-02 12:38:49 -04:00
|
|
|
get :proxy, params: environment_params
|
2019-04-02 02:26:09 -04:00
|
|
|
|
2019-12-12 07:07:33 -05:00
|
|
|
expect(json_response['status']).to eq(_('processing'))
|
|
|
|
expect(json_response['message']).to eq(_('Not ready yet. Try again later.'))
|
2019-06-05 03:12:10 -04:00
|
|
|
expect(response).to have_gitlab_http_status(:no_content)
|
2019-04-02 12:38:49 -04:00
|
|
|
end
|
2019-04-02 02:26:09 -04:00
|
|
|
end
|
|
|
|
|
2019-04-02 12:38:49 -04:00
|
|
|
context 'with 404 result' do
|
|
|
|
let(:service_result) { { http_status: 404, status: :success, body: '{"body": "value"}' } }
|
2019-04-02 02:26:09 -04:00
|
|
|
|
2019-04-02 12:38:49 -04:00
|
|
|
it 'returns body' do
|
|
|
|
get :proxy, params: environment_params
|
2019-04-02 02:26:09 -04:00
|
|
|
|
2019-04-02 12:38:49 -04:00
|
|
|
expect(response).to have_gitlab_http_status(:not_found)
|
|
|
|
expect(json_response['body']).to eq('value')
|
|
|
|
end
|
2019-04-02 02:26:09 -04:00
|
|
|
end
|
|
|
|
|
2019-04-02 12:38:49 -04:00
|
|
|
context 'with error result' do
|
|
|
|
context 'with http_status' do
|
|
|
|
let(:service_result) do
|
|
|
|
{ http_status: :service_unavailable, status: :error, message: 'error message' }
|
|
|
|
end
|
2019-04-02 02:26:09 -04:00
|
|
|
|
2019-04-02 12:38:49 -04:00
|
|
|
it 'sets the http response status code' do
|
|
|
|
get :proxy, params: environment_params
|
2019-04-02 02:26:09 -04:00
|
|
|
|
2019-04-02 12:38:49 -04:00
|
|
|
expect(response).to have_gitlab_http_status(:service_unavailable)
|
|
|
|
expect(json_response['status']).to eq('error')
|
|
|
|
expect(json_response['message']).to eq('error message')
|
|
|
|
end
|
2019-04-02 02:26:09 -04:00
|
|
|
end
|
|
|
|
|
2019-04-02 12:38:49 -04:00
|
|
|
context 'without http_status' do
|
|
|
|
let(:service_result) { { status: :error, message: 'error message' } }
|
2019-04-02 02:26:09 -04:00
|
|
|
|
2019-04-02 12:38:49 -04:00
|
|
|
it 'returns bad_request' do
|
|
|
|
get :proxy, params: environment_params
|
2019-04-02 02:26:09 -04:00
|
|
|
|
2019-04-02 12:38:49 -04:00
|
|
|
expect(response).to have_gitlab_http_status(:bad_request)
|
|
|
|
expect(json_response['status']).to eq('error')
|
|
|
|
expect(json_response['message']).to eq('error message')
|
|
|
|
end
|
2019-04-02 02:26:09 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-02 12:38:49 -04:00
|
|
|
context 'with inappropriate requests' do
|
|
|
|
context 'with anonymous user' do
|
|
|
|
before do
|
|
|
|
sign_out(user)
|
|
|
|
end
|
2019-04-02 02:26:09 -04:00
|
|
|
|
2019-04-02 12:38:49 -04:00
|
|
|
it 'redirects to signin page' do
|
|
|
|
get :proxy, params: environment_params
|
2019-04-02 02:26:09 -04:00
|
|
|
|
2019-04-02 12:38:49 -04:00
|
|
|
expect(response).to redirect_to(new_user_session_path)
|
|
|
|
end
|
2019-04-02 02:26:09 -04:00
|
|
|
end
|
|
|
|
|
2019-04-02 12:38:49 -04:00
|
|
|
context 'without correct permissions' do
|
|
|
|
before do
|
|
|
|
project.team.truncate
|
|
|
|
end
|
2019-04-02 02:26:09 -04:00
|
|
|
|
2019-04-02 12:38:49 -04:00
|
|
|
it 'returns 404' do
|
|
|
|
get :proxy, params: environment_params
|
2019-04-02 02:26:09 -04:00
|
|
|
|
2019-04-02 12:38:49 -04:00
|
|
|
expect(response).to have_gitlab_http_status(:not_found)
|
|
|
|
end
|
2019-04-02 02:26:09 -04:00
|
|
|
end
|
|
|
|
end
|
2019-04-03 04:02:45 -04:00
|
|
|
|
|
|
|
context 'with invalid environment id' do
|
|
|
|
let(:other_environment) { create(:environment) }
|
|
|
|
|
|
|
|
it 'returns 404' do
|
|
|
|
get :proxy, params: environment_params(id: other_environment.id)
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:not_found)
|
|
|
|
end
|
|
|
|
end
|
2019-04-02 02:26:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def environment_params(params = {})
|
|
|
|
{
|
2019-04-04 09:46:45 -04:00
|
|
|
id: environment.id.to_s,
|
2018-11-21 08:13:54 -05:00
|
|
|
namespace_id: project.namespace.full_path,
|
2019-04-04 09:46:45 -04:00
|
|
|
project_id: project.name,
|
2019-04-02 02:26:09 -04:00
|
|
|
proxy_path: 'query',
|
|
|
|
query: '1'
|
2019-04-03 02:42:12 -04:00
|
|
|
}.merge(params)
|
2019-04-02 02:26:09 -04:00
|
|
|
end
|
|
|
|
end
|