debian-mirror-gitlab/spec/controllers/projects/environments/prometheus_api_controller_spec.rb

76 lines
2.2 KiB
Ruby
Raw Normal View History

2019-07-07 11:18:12 +05:30
# frozen_string_literal: true
require 'spec_helper'
2020-06-23 00:09:42 +05:30
RSpec.describe Projects::Environments::PrometheusApiController do
2019-12-21 20:55:43 +05:30
let_it_be(:user) { create(:user) }
2020-07-28 23:09:34 +05:30
let_it_be(:project) { create(:project) }
let_it_be(:proxyable) { create(:environment, project: project) }
2019-07-07 11:18:12 +05:30
before do
project.add_reporter(user)
sign_in(user)
end
2020-07-28 23:09:34 +05:30
describe 'GET #prometheus_proxy' do
it_behaves_like 'metrics dashboard prometheus api proxy' do
let(:proxyable_params) do
{
id: proxyable.id.to_s,
namespace_id: project.namespace.full_path,
project_id: project.name
}
2019-07-07 11:18:12 +05:30
end
2020-07-28 23:09:34 +05:30
context 'with variables' do
2019-07-07 11:18:12 +05:30
let(:prometheus_body) { '{"status":"success"}' }
2020-07-28 23:09:34 +05:30
let(:pod_name) { "pod1" }
2019-07-07 11:18:12 +05:30
2020-07-28 23:09:34 +05:30
before do
expected_params[:query] = %{up{pod_name="#{pod_name}"}}
expected_params[:variables] = { 'pod_name' => pod_name }
2019-07-07 11:18:12 +05:30
end
2019-07-31 22:56:46 +05:30
2020-07-28 23:09:34 +05:30
it 'replaces variables with values' do
get :prometheus_proxy, params: prometheus_proxy_params.merge(
query: 'up{pod_name="{{pod_name}}"}', variables: { 'pod_name' => pod_name }
)
2019-07-31 22:56:46 +05:30
2020-07-28 23:09:34 +05:30
expect(response).to have_gitlab_http_status(:success)
expect(Prometheus::ProxyService).to have_received(:new)
.with(proxyable, 'GET', 'query', expected_params)
2019-07-31 22:56:46 +05:30
end
2020-03-13 15:44:24 +05:30
2020-07-28 23:09:34 +05:30
context 'with invalid variables' do
let(:params_with_invalid_variables) do
prometheus_proxy_params.merge(
query: 'up{pod_name="{{pod_name}}"}', variables: ['a']
2020-03-13 15:44:24 +05:30
)
2019-07-07 11:18:12 +05:30
end
2020-07-28 23:09:34 +05:30
it 'returns 400' do
get :prometheus_proxy, params: params_with_invalid_variables
2019-07-07 11:18:12 +05:30
expect(response).to have_gitlab_http_status(:bad_request)
2020-07-28 23:09:34 +05:30
expect(Prometheus::ProxyService).not_to receive(:new)
2019-07-07 11:18:12 +05:30
end
end
end
context 'with anonymous user' do
2020-07-28 23:09:34 +05:30
let(:prometheus_body) { nil }
2019-07-07 11:18:12 +05:30
before do
sign_out(user)
end
it 'redirects to signin page' do
2020-07-28 23:09:34 +05:30
get :prometheus_proxy, params: prometheus_proxy_params
2019-07-07 11:18:12 +05:30
expect(response).to redirect_to(new_user_session_path)
end
end
end
end
end