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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

97 lines
2.8 KiB
Ruby
Raw Permalink 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) }
2023-05-08 21:46:49 +05:30
let_it_be_with_reload(:project) { create(:project) }
2020-07-28 23:09:34 +05:30
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,
2023-07-09 08:55:56 +05:30
project_id: project.path
2020-07-28 23:09:34 +05:30
}
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
2023-05-08 21:46:49 +05:30
context 'with a public project' do
before do
project.update!(visibility_level: Gitlab::VisibilityLevel::PUBLIC)
project.project_feature.update!(metrics_dashboard_access_level: ProjectFeature::ENABLED)
end
context 'with guest user' do
let(:prometheus_body) { nil }
before do
project.add_guest(user)
end
it 'returns 404' do
get :prometheus_proxy, params: prometheus_proxy_params
expect(response).to have_gitlab_http_status(:not_found)
end
end
end
2019-07-07 11:18:12 +05:30
end
end
end