2019-07-31 22:56:46 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
RSpec.describe Projects::EnvironmentsController do
|
2019-09-30 21:07:59 +05:30
|
|
|
include MetricsDashboardHelpers
|
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
let_it_be(:project) { create(:project) }
|
2020-01-01 13:55:28 +05:30
|
|
|
let_it_be(:maintainer) { create(:user, name: 'main-dos').tap { |u| project.add_maintainer(u) } }
|
|
|
|
let_it_be(:reporter) { create(:user, name: 'repo-dos').tap { |u| project.add_reporter(u) } }
|
|
|
|
let(:user) { maintainer }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2020-01-01 13:55:28 +05:30
|
|
|
let!(:environment) { create(:environment, name: 'production', project: project) }
|
2016-09-13 17:45:13 +05:30
|
|
|
|
|
|
|
before do
|
|
|
|
sign_in(user)
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
describe 'GET index' do
|
2017-09-10 17:25:29 +05:30
|
|
|
context 'when a request for the HTML is made' do
|
2017-08-17 22:00:37 +05:30
|
|
|
it 'responds with status code 200' do
|
2019-02-15 15:39:39 +05:30
|
|
|
get :index, params: environment_params
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2018-11-08 19:23:39 +05:30
|
|
|
|
|
|
|
it 'expires etag cache to force reload environments list' do
|
|
|
|
expect_any_instance_of(Gitlab::EtagCaching::Store)
|
|
|
|
.to receive(:touch).with(project_environments_path(project, format: :json))
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
get :index, params: environment_params
|
2018-11-08 19:23:39 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'when requesting JSON response for folders' do
|
|
|
|
before do
|
|
|
|
create(:environment, project: project,
|
|
|
|
name: 'staging/review-1',
|
|
|
|
state: :available)
|
|
|
|
|
|
|
|
create(:environment, project: project,
|
|
|
|
name: 'staging/review-2',
|
|
|
|
state: :available)
|
|
|
|
|
|
|
|
create(:environment, project: project,
|
|
|
|
name: 'staging/review-3',
|
|
|
|
state: :stopped)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:environments) { json_response['environments'] }
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
context 'with default parameters' do
|
|
|
|
before do
|
|
|
|
get :index, params: environment_params(format: :json)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'responds with a flat payload describing available environments' do
|
|
|
|
expect(environments.count).to eq 3
|
2019-07-07 11:18:12 +05:30
|
|
|
expect(environments.first).to include('name' => 'production', 'name_without_type' => 'production')
|
|
|
|
expect(environments.second).to include('name' => 'staging/review-1', 'name_without_type' => 'review-1')
|
|
|
|
expect(environments.third).to include('name' => 'staging/review-2', 'name_without_type' => 'review-2')
|
2019-03-02 22:35:43 +05:30
|
|
|
expect(json_response['available_count']).to eq 3
|
|
|
|
expect(json_response['stopped_count']).to eq 1
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets the polling interval header' do
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
expect(response.headers['Poll-Interval']).to eq("3000")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when a folder-based nested structure is requested' do
|
|
|
|
before do
|
|
|
|
get :index, params: environment_params(format: :json, nested: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'responds with a payload containing the latest environment for each folder' do
|
|
|
|
expect(environments.count).to eq 2
|
|
|
|
expect(environments.first['name']).to eq 'production'
|
|
|
|
expect(environments.second['name']).to eq 'staging'
|
|
|
|
expect(environments.second['size']).to eq 2
|
|
|
|
expect(environments.second['latest']['name']).to eq 'staging/review-2'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
context 'when requesting available environments scope' do
|
|
|
|
before do
|
2019-03-02 22:35:43 +05:30
|
|
|
get :index, params: environment_params(format: :json, nested: true, scope: :available)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'responds with a payload describing available environments' do
|
|
|
|
expect(environments.count).to eq 2
|
|
|
|
expect(environments.first['name']).to eq 'production'
|
|
|
|
expect(environments.second['name']).to eq 'staging'
|
|
|
|
expect(environments.second['size']).to eq 2
|
|
|
|
expect(environments.second['latest']['name']).to eq 'staging/review-2'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'contains values describing environment scopes sizes' do
|
|
|
|
expect(json_response['available_count']).to eq 3
|
|
|
|
expect(json_response['stopped_count']).to eq 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when requesting stopped environments scope' do
|
|
|
|
before do
|
2019-03-02 22:35:43 +05:30
|
|
|
get :index, params: environment_params(format: :json, nested: true, scope: :stopped)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'responds with a payload describing stopped environments' do
|
|
|
|
expect(environments.count).to eq 1
|
|
|
|
expect(environments.first['name']).to eq 'staging'
|
|
|
|
expect(environments.first['size']).to eq 1
|
|
|
|
expect(environments.first['latest']['name']).to eq 'staging/review-3'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'contains values describing environment scopes sizes' do
|
|
|
|
expect(json_response['available_count']).to eq 3
|
|
|
|
expect(json_response['stopped_count']).to eq 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET folder' do
|
|
|
|
before do
|
|
|
|
create(:environment, project: project,
|
|
|
|
name: 'staging-1.0/review',
|
|
|
|
state: :available)
|
2017-09-10 17:25:29 +05:30
|
|
|
create(:environment, project: project,
|
|
|
|
name: 'staging-1.0/zzz',
|
|
|
|
state: :available)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'when using default format' do
|
|
|
|
it 'responds with HTML' do
|
2019-02-15 15:39:39 +05:30
|
|
|
get :folder, params: {
|
|
|
|
namespace_id: project.namespace,
|
|
|
|
project_id: project,
|
|
|
|
id: 'staging-1.0'
|
|
|
|
}
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
expect(response).to be_ok
|
|
|
|
expect(response).to render_template 'folder'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when using JSON format' do
|
2017-09-10 17:25:29 +05:30
|
|
|
it 'sorts the subfolders lexicographically' do
|
2019-02-15 15:39:39 +05:30
|
|
|
get :folder, params: {
|
|
|
|
namespace_id: project.namespace,
|
|
|
|
project_id: project,
|
|
|
|
id: 'staging-1.0'
|
|
|
|
},
|
2017-08-17 22:00:37 +05:30
|
|
|
format: :json
|
|
|
|
|
|
|
|
expect(response).to be_ok
|
|
|
|
expect(response).not_to render_template 'folder'
|
|
|
|
expect(json_response['environments'][0])
|
2019-07-07 11:18:12 +05:30
|
|
|
.to include('name' => 'staging-1.0/review', 'name_without_type' => 'review')
|
2017-09-10 17:25:29 +05:30
|
|
|
expect(json_response['environments'][1])
|
2019-07-07 11:18:12 +05:30
|
|
|
.to include('name' => 'staging-1.0/zzz', 'name_without_type' => 'zzz')
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
describe 'GET show' do
|
|
|
|
context 'with valid id' do
|
|
|
|
it 'responds with a status code 200' do
|
2019-02-15 15:39:39 +05:30
|
|
|
get :show, params: environment_params
|
2016-09-13 17:45:13 +05:30
|
|
|
|
|
|
|
expect(response).to be_ok
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with invalid id' do
|
|
|
|
it 'responds with a status code 404' do
|
|
|
|
params = environment_params
|
2020-04-22 19:07:51 +05:30
|
|
|
params[:id] = non_existing_record_id
|
2019-02-15 15:39:39 +05:30
|
|
|
get :show, params: params
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:not_found)
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET edit' do
|
|
|
|
it 'responds with a status code 200' do
|
2019-02-15 15:39:39 +05:30
|
|
|
get :edit, params: environment_params
|
2016-09-13 17:45:13 +05:30
|
|
|
|
|
|
|
expect(response).to be_ok
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'PATCH #update' do
|
|
|
|
it 'responds with a 302' do
|
|
|
|
patch_params = environment_params.merge(environment: { external_url: 'https://git.gitlab.com' })
|
2019-02-15 15:39:39 +05:30
|
|
|
patch :update, params: patch_params
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:found)
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
describe 'PATCH #stop' do
|
|
|
|
context 'when env not available' do
|
|
|
|
it 'returns 404' do
|
|
|
|
allow_any_instance_of(Environment).to receive(:available?) { false }
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
patch :stop, params: environment_params(format: :json)
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:not_found)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when stop action' do
|
|
|
|
it 'returns action url' do
|
|
|
|
action = create(:ci_build, :manual)
|
|
|
|
|
|
|
|
allow_any_instance_of(Environment)
|
|
|
|
.to receive_messages(available?: true, stop_with_action!: action)
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
patch :stop, params: environment_params(format: :json)
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2017-08-17 22:00:37 +05:30
|
|
|
expect(json_response).to eq(
|
|
|
|
{ 'redirect_url' =>
|
2017-09-10 17:25:29 +05:30
|
|
|
project_job_url(project, action) })
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when no stop action' do
|
|
|
|
it 'returns env url' do
|
|
|
|
allow_any_instance_of(Environment)
|
|
|
|
.to receive_messages(available?: true, stop_with_action!: nil)
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
patch :stop, params: environment_params(format: :json)
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2017-08-17 22:00:37 +05:30
|
|
|
expect(json_response).to eq(
|
|
|
|
{ 'redirect_url' =>
|
2017-09-10 17:25:29 +05:30
|
|
|
project_environment_url(project, environment) })
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-01-01 13:55:28 +05:30
|
|
|
describe 'POST #cancel_auto_stop' do
|
|
|
|
subject { post :cancel_auto_stop, params: params }
|
|
|
|
|
|
|
|
let(:params) { environment_params }
|
|
|
|
|
|
|
|
context 'when environment is set as auto-stop' do
|
|
|
|
let(:environment) { create(:environment, :will_auto_stop, name: 'staging', project: project) }
|
|
|
|
|
|
|
|
it_behaves_like 'successful response for #cancel_auto_stop'
|
|
|
|
|
|
|
|
context 'when user is reporter' do
|
|
|
|
let(:user) { reporter }
|
|
|
|
|
|
|
|
it 'shows NOT Found' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:not_found)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when environment is not set as auto-stop' do
|
|
|
|
let(:environment) { create(:environment, name: 'staging', project: project) }
|
|
|
|
|
|
|
|
it_behaves_like 'failed response for #cancel_auto_stop' do
|
|
|
|
let(:message) { 'the environment is not set as auto stop' }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
describe 'GET #terminal' do
|
|
|
|
context 'with valid id' do
|
|
|
|
it 'responds with a status code 200' do
|
2019-02-15 15:39:39 +05:30
|
|
|
get :terminal, params: environment_params
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
it 'loads the terminals for the environment' do
|
2019-02-15 15:39:39 +05:30
|
|
|
# In EE we have to stub EE::Environment since it overwrites the
|
|
|
|
# "terminals" method.
|
2019-12-21 20:55:43 +05:30
|
|
|
expect_any_instance_of(Gitlab.ee? ? EE::Environment : Environment)
|
2019-02-15 15:39:39 +05:30
|
|
|
.to receive(:terminals)
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
get :terminal, params: environment_params
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with invalid id' do
|
|
|
|
it 'responds with a status code 404' do
|
2019-02-15 15:39:39 +05:30
|
|
|
get :terminal, params: environment_params(id: 666)
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:not_found)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET #terminal_websocket_authorize' do
|
|
|
|
context 'with valid workhorse signature' do
|
|
|
|
before do
|
|
|
|
allow(Gitlab::Workhorse).to receive(:verify_api_request!).and_return(nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'and valid id' do
|
|
|
|
it 'returns the first terminal for the environment' do
|
2019-02-15 15:39:39 +05:30
|
|
|
# In EE we have to stub EE::Environment since it overwrites the
|
|
|
|
# "terminals" method.
|
2019-12-21 20:55:43 +05:30
|
|
|
expect_any_instance_of(Gitlab.ee? ? EE::Environment : Environment)
|
2017-09-10 17:25:29 +05:30
|
|
|
.to receive(:terminals)
|
|
|
|
.and_return([:fake_terminal])
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
expect(Gitlab::Workhorse)
|
2019-07-07 11:18:12 +05:30
|
|
|
.to receive(:channel_websocket)
|
2017-09-10 17:25:29 +05:30
|
|
|
.with(:fake_terminal)
|
|
|
|
.and_return(workhorse: :response)
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
get :terminal_websocket_authorize, params: environment_params
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2017-08-17 22:00:37 +05:30
|
|
|
expect(response.headers["Content-Type"]).to eq(Gitlab::Workhorse::INTERNAL_API_CONTENT_TYPE)
|
|
|
|
expect(response.body).to eq('{"workhorse":"response"}')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'and invalid id' do
|
|
|
|
it 'returns 404' do
|
2019-02-15 15:39:39 +05:30
|
|
|
get :terminal_websocket_authorize, params: environment_params(id: 666)
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:not_found)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with invalid workhorse signature' do
|
|
|
|
it 'aborts with an exception' do
|
|
|
|
allow(Gitlab::Workhorse).to receive(:verify_api_request!).and_raise(JWT::DecodeError)
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
expect { get :terminal_websocket_authorize, params: environment_params }.to raise_error(JWT::DecodeError)
|
2017-08-17 22:00:37 +05:30
|
|
|
# controller tests don't set the response status correctly. It's enough
|
|
|
|
# to check that the action raised an exception
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
describe 'GET #metrics_redirect' do
|
2020-10-24 23:57:45 +05:30
|
|
|
it 'redirects to metrics dashboard page' do
|
2019-02-15 15:39:39 +05:30
|
|
|
get :metrics_redirect, params: { namespace_id: project.namespace, project_id: project }
|
2018-11-08 19:23:39 +05:30
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
expect(response).to redirect_to(project_metrics_dashboard_path(project))
|
2018-11-08 19:23:39 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
describe 'GET #metrics' do
|
|
|
|
before do
|
|
|
|
allow(controller).to receive(:environment).and_return(environment)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when environment has no metrics' do
|
2020-10-24 23:57:45 +05:30
|
|
|
it 'redirects to metrics dashboard page' do
|
2019-07-31 22:56:46 +05:30
|
|
|
expect(environment).not_to receive(:metrics)
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
get :metrics, params: environment_params
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
expect(response).to redirect_to(project_metrics_dashboard_path(project, environment: environment))
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'when requesting metrics as JSON' do
|
|
|
|
it 'returns a metrics JSON document' do
|
2019-07-31 22:56:46 +05:30
|
|
|
expect(environment).to receive(:metrics).and_return(nil)
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
get :metrics, params: environment_params(format: :json)
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:no_content)
|
2017-08-17 22:00:37 +05:30
|
|
|
expect(json_response).to eq({})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when environment has some metrics' do
|
|
|
|
before do
|
|
|
|
expect(environment).to receive(:metrics).and_return({
|
|
|
|
success: true,
|
|
|
|
metrics: {},
|
|
|
|
last_update: 42
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns a metrics JSON document' do
|
2019-02-15 15:39:39 +05:30
|
|
|
get :metrics, params: environment_params(format: :json)
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
expect(response).to be_ok
|
|
|
|
expect(json_response['success']).to be(true)
|
|
|
|
expect(json_response['metrics']).to eq({})
|
|
|
|
expect(json_response['last_update']).to eq(42)
|
|
|
|
end
|
|
|
|
end
|
2020-05-24 23:13:21 +05:30
|
|
|
|
|
|
|
context 'permissions' do
|
|
|
|
before do
|
|
|
|
allow(controller).to receive(:can?).and_return true
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'checks :metrics_dashboard ability' do
|
|
|
|
expect(controller).to receive(:can?).with(anything, :metrics_dashboard, anything)
|
|
|
|
|
|
|
|
get :metrics, params: environment_params
|
|
|
|
end
|
|
|
|
end
|
2020-06-23 00:09:42 +05:30
|
|
|
|
|
|
|
context 'with anonymous user and public dashboard visibility' do
|
|
|
|
let(:project) { create(:project, :public) }
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
it 'redirects to metrics dashboard page' do
|
2020-06-23 00:09:42 +05:30
|
|
|
project.project_feature.update!(metrics_dashboard_access_level: ProjectFeature::ENABLED)
|
|
|
|
|
|
|
|
get :metrics, params: environment_params
|
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
expect(response).to redirect_to(project_metrics_dashboard_path(project, environment: environment))
|
2020-06-23 00:09:42 +05:30
|
|
|
end
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
describe 'GET #additional_metrics' do
|
2019-09-04 21:01:54 +05:30
|
|
|
let(:window_params) { { start: '1554702993.5398998', end: '1554717396.996232' } }
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
before do
|
|
|
|
allow(controller).to receive(:environment).and_return(environment)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when environment has no metrics' do
|
|
|
|
before do
|
|
|
|
expect(environment).to receive(:additional_metrics).and_return(nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when requesting metrics as JSON' do
|
|
|
|
it 'returns a metrics JSON document' do
|
2019-09-04 21:01:54 +05:30
|
|
|
additional_metrics(window_params)
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:no_content)
|
2017-09-10 17:25:29 +05:30
|
|
|
expect(json_response).to eq({})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when environment has some metrics' do
|
|
|
|
before do
|
|
|
|
expect(environment)
|
|
|
|
.to receive(:additional_metrics)
|
|
|
|
.and_return({
|
|
|
|
success: true,
|
|
|
|
data: {},
|
|
|
|
last_update: 42
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns a metrics JSON document' do
|
2019-09-04 21:01:54 +05:30
|
|
|
additional_metrics(window_params)
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
expect(response).to be_ok
|
|
|
|
expect(json_response['success']).to be(true)
|
|
|
|
expect(json_response['data']).to eq({})
|
|
|
|
expect(json_response['last_update']).to eq(42)
|
|
|
|
end
|
2019-09-04 21:01:54 +05:30
|
|
|
end
|
2019-07-07 11:18:12 +05:30
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
context 'when time params are missing' do
|
|
|
|
it 'raises an error when window params are missing' do
|
|
|
|
expect { additional_metrics }
|
|
|
|
.to raise_error(ActionController::ParameterMissing)
|
2019-07-07 11:18:12 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when only one time param is provided' do
|
|
|
|
it 'raises an error when start is missing' do
|
2019-09-04 21:01:54 +05:30
|
|
|
expect { additional_metrics(end: '1552647300.651094') }
|
2019-07-07 11:18:12 +05:30
|
|
|
.to raise_error(ActionController::ParameterMissing)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises an error when end is missing' do
|
|
|
|
expect { additional_metrics(start: '1552647300.651094') }
|
|
|
|
.to raise_error(ActionController::ParameterMissing)
|
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
2020-05-24 23:13:21 +05:30
|
|
|
|
|
|
|
context 'permissions' do
|
|
|
|
before do
|
|
|
|
allow(controller).to receive(:can?).and_return true
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'checks :metrics_dashboard ability' do
|
|
|
|
expect(controller).to receive(:can?).with(anything, :metrics_dashboard, anything)
|
|
|
|
|
|
|
|
get :metrics, params: environment_params
|
|
|
|
end
|
|
|
|
end
|
2020-06-23 00:09:42 +05:30
|
|
|
|
|
|
|
context 'with anonymous user and public dashboard visibility' do
|
|
|
|
let(:project) { create(:project, :public) }
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
|
|
|
it 'does not fail' do
|
|
|
|
allow(environment)
|
|
|
|
.to receive(:additional_metrics)
|
|
|
|
.and_return({
|
|
|
|
success: true,
|
|
|
|
data: {},
|
|
|
|
last_update: 42
|
|
|
|
})
|
|
|
|
project.project_feature.update!(metrics_dashboard_access_level: ProjectFeature::ENABLED)
|
|
|
|
|
|
|
|
additional_metrics(window_params)
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
end
|
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
|
2019-09-30 21:07:59 +05:30
|
|
|
describe 'GET #metrics_dashboard' do
|
2020-07-28 23:09:34 +05:30
|
|
|
let(:metrics_dashboard_req_params) { environment_params(dashboard_params) }
|
2019-07-31 22:56:46 +05:30
|
|
|
|
2019-09-30 21:07:59 +05:30
|
|
|
shared_examples_for '200 response' do
|
2020-07-28 23:09:34 +05:30
|
|
|
it_behaves_like 'GET #metrics_dashboard correctly formatted response' do
|
|
|
|
let(:expected_keys) { %w(dashboard status metrics_data) }
|
|
|
|
let(:status_code) { :ok }
|
|
|
|
end
|
2019-07-31 22:56:46 +05:30
|
|
|
end
|
|
|
|
|
2019-09-30 21:07:59 +05:30
|
|
|
shared_examples_for 'error response' do |status_code|
|
2020-07-28 23:09:34 +05:30
|
|
|
it_behaves_like 'GET #metrics_dashboard correctly formatted response' do
|
|
|
|
let(:expected_keys) { %w(message status) }
|
|
|
|
let(:status_code) { status_code }
|
|
|
|
end
|
2019-09-30 21:07:59 +05:30
|
|
|
end
|
2019-07-31 22:56:46 +05:30
|
|
|
|
2019-09-30 21:07:59 +05:30
|
|
|
shared_examples_for 'includes all dashboards' do
|
|
|
|
it 'includes info for all findable dashboard' do
|
2019-07-31 22:56:46 +05:30
|
|
|
get :metrics_dashboard, params: environment_params(dashboard_params)
|
|
|
|
|
2019-09-30 21:07:59 +05:30
|
|
|
expect(json_response).to have_key('all_dashboards')
|
|
|
|
expect(json_response['all_dashboards']).to be_an_instance_of(Array)
|
|
|
|
expect(json_response['all_dashboards']).to all( include('path', 'default', 'display_name') )
|
2019-07-31 22:56:46 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-30 21:07:59 +05:30
|
|
|
shared_examples_for 'the default dashboard' do
|
2019-12-21 20:55:43 +05:30
|
|
|
it_behaves_like 'includes all dashboards'
|
2020-07-28 23:09:34 +05:30
|
|
|
it_behaves_like 'GET #metrics_dashboard for dashboard', 'Environment metrics'
|
2019-07-31 22:56:46 +05:30
|
|
|
end
|
|
|
|
|
2019-09-30 21:07:59 +05:30
|
|
|
shared_examples_for 'the specified dashboard' do |expected_dashboard|
|
|
|
|
it_behaves_like 'includes all dashboards'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
it_behaves_like 'GET #metrics_dashboard for dashboard', expected_dashboard
|
2019-07-31 22:56:46 +05:30
|
|
|
|
2019-09-30 21:07:59 +05:30
|
|
|
context 'when the dashboard cannot not be processed' do
|
2019-07-31 22:56:46 +05:30
|
|
|
before do
|
|
|
|
allow(YAML).to receive(:safe_load).and_return({})
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'error response', :unprocessable_entity
|
|
|
|
end
|
2019-09-30 21:07:59 +05:30
|
|
|
end
|
2019-07-31 22:56:46 +05:30
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
shared_examples_for 'specified dashboard embed' do |expected_titles|
|
2019-09-30 21:07:59 +05:30
|
|
|
it_behaves_like '200 response'
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
it 'contains only the specified charts' do
|
2019-09-30 21:07:59 +05:30
|
|
|
get :metrics_dashboard, params: environment_params(dashboard_params)
|
2019-07-31 22:56:46 +05:30
|
|
|
|
2019-09-30 21:07:59 +05:30
|
|
|
dashboard = json_response['dashboard']
|
|
|
|
panel_group = dashboard['panel_groups'].first
|
|
|
|
titles = panel_group['panels'].map { |panel| panel['title'] }
|
|
|
|
|
|
|
|
expect(dashboard['dashboard']).to be_nil
|
|
|
|
expect(dashboard['panel_groups'].length).to eq 1
|
|
|
|
expect(panel_group['group']).to be_nil
|
2019-10-12 21:52:04 +05:30
|
|
|
expect(titles).to eq expected_titles
|
2019-07-31 22:56:46 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
shared_examples_for 'the default dynamic dashboard' do
|
|
|
|
it_behaves_like 'specified dashboard embed', ['Memory Usage (Total)', 'Core Usage (Total)']
|
|
|
|
end
|
|
|
|
|
2019-09-30 21:07:59 +05:30
|
|
|
shared_examples_for 'dashboard can be specified' do
|
|
|
|
context 'when dashboard is specified' do
|
|
|
|
let(:dashboard_path) { '.gitlab/dashboards/test.yml' }
|
|
|
|
let(:dashboard_params) { { format: :json, dashboard: dashboard_path } }
|
2019-07-31 22:56:46 +05:30
|
|
|
|
2019-09-30 21:07:59 +05:30
|
|
|
it_behaves_like 'error response', :not_found
|
2019-07-31 22:56:46 +05:30
|
|
|
|
2019-09-30 21:07:59 +05:30
|
|
|
context 'when the project dashboard is available' do
|
|
|
|
let(:dashboard_yml) { fixture_file('lib/gitlab/metrics/dashboard/sample_dashboard.yml') }
|
|
|
|
let(:project) { project_with_dashboard(dashboard_path, dashboard_yml) }
|
|
|
|
let(:environment) { create(:environment, name: 'production', project: project) }
|
|
|
|
|
2020-01-01 13:55:28 +05:30
|
|
|
before do
|
|
|
|
project.add_maintainer(user)
|
|
|
|
end
|
|
|
|
|
2019-09-30 21:07:59 +05:30
|
|
|
it_behaves_like 'the specified dashboard', 'Test Dashboard'
|
2019-07-31 22:56:46 +05:30
|
|
|
end
|
|
|
|
|
2019-09-30 21:07:59 +05:30
|
|
|
context 'when the specified dashboard is the default dashboard' do
|
2019-10-12 21:52:04 +05:30
|
|
|
let(:dashboard_path) { system_dashboard_path }
|
2019-09-30 21:07:59 +05:30
|
|
|
|
|
|
|
it_behaves_like 'the default dashboard'
|
|
|
|
end
|
2019-07-31 22:56:46 +05:30
|
|
|
end
|
2019-09-30 21:07:59 +05:30
|
|
|
end
|
2019-07-31 22:56:46 +05:30
|
|
|
|
2019-09-30 21:07:59 +05:30
|
|
|
shared_examples_for 'dashboard can be embedded' do
|
|
|
|
context 'when the embedded flag is included' do
|
|
|
|
let(:dashboard_params) { { format: :json, embedded: true } }
|
2019-07-31 22:56:46 +05:30
|
|
|
|
2019-09-30 21:07:59 +05:30
|
|
|
it_behaves_like 'the default dynamic dashboard'
|
2019-07-31 22:56:46 +05:30
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
context 'when incomplete dashboard params are provided' do
|
|
|
|
let(:dashboard_params) { { format: :json, embedded: true, title: 'Title' } }
|
|
|
|
|
|
|
|
# The title param should be ignored.
|
|
|
|
it_behaves_like 'the default dynamic dashboard'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when invalid params are provided' do
|
|
|
|
let(:dashboard_params) { { format: :json, embedded: true, metric_id: 16 } }
|
2019-07-31 22:56:46 +05:30
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
# The superfluous param should be ignored.
|
2019-09-30 21:07:59 +05:30
|
|
|
it_behaves_like 'the default dynamic dashboard'
|
2019-07-31 22:56:46 +05:30
|
|
|
end
|
2019-10-12 21:52:04 +05:30
|
|
|
|
|
|
|
context 'when the dashboard is correctly specified' do
|
|
|
|
let(:dashboard_params) do
|
|
|
|
{
|
|
|
|
format: :json,
|
|
|
|
embedded: true,
|
|
|
|
dashboard: system_dashboard_path,
|
|
|
|
group: business_metric_title,
|
|
|
|
title: 'title',
|
|
|
|
y_label: 'y_label'
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'error response', :not_found
|
|
|
|
|
|
|
|
context 'and exists' do
|
|
|
|
let!(:metric) { create(:prometheus_metric, project: project) }
|
|
|
|
|
|
|
|
it_behaves_like 'specified dashboard embed', ['title']
|
|
|
|
end
|
|
|
|
end
|
2019-07-31 22:56:46 +05:30
|
|
|
end
|
|
|
|
end
|
2019-09-30 21:07:59 +05:30
|
|
|
|
|
|
|
shared_examples_for 'dashboard cannot be specified' do
|
|
|
|
context 'when dashboard is specified' do
|
|
|
|
let(:dashboard_params) { { format: :json, dashboard: '.gitlab/dashboards/test.yml' } }
|
|
|
|
|
|
|
|
it_behaves_like 'the default dashboard'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:dashboard_params) { { format: :json } }
|
|
|
|
|
|
|
|
it_behaves_like 'the default dashboard'
|
|
|
|
it_behaves_like 'dashboard can be specified'
|
|
|
|
it_behaves_like 'dashboard can be embedded'
|
2020-05-24 23:13:21 +05:30
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
context 'with anonymous user and public dashboard visibility' do
|
|
|
|
let(:project) { create(:project, :public) }
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
project.project_feature.update!(metrics_dashboard_access_level: ProjectFeature::ENABLED)
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'the default dashboard'
|
|
|
|
end
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
context 'permissions' do
|
|
|
|
before do
|
|
|
|
allow(controller).to receive(:can?).and_return true
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'checks :metrics_dashboard ability' do
|
|
|
|
expect(controller).to receive(:can?).with(anything, :metrics_dashboard, anything)
|
|
|
|
|
|
|
|
get :metrics, params: environment_params
|
|
|
|
end
|
|
|
|
end
|
2019-07-31 22:56:46 +05:30
|
|
|
end
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
describe 'GET #search' do
|
|
|
|
before do
|
|
|
|
create(:environment, name: 'staging', project: project)
|
|
|
|
create(:environment, name: 'review/patch-1', project: project)
|
|
|
|
create(:environment, name: 'review/patch-2', project: project)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:query) { 'pro' }
|
|
|
|
|
|
|
|
it 'responds with status code 200' do
|
|
|
|
get :search, params: environment_params(format: :json, query: query)
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns matched results' do
|
|
|
|
get :search, params: environment_params(format: :json, query: query)
|
|
|
|
|
|
|
|
expect(json_response).to contain_exactly('production')
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when query is review' do
|
|
|
|
let(:query) { 'review' }
|
|
|
|
|
|
|
|
it 'returns matched results' do
|
|
|
|
get :search, params: environment_params(format: :json, query: query)
|
|
|
|
|
|
|
|
expect(json_response).to contain_exactly('review/patch-1', 'review/patch-2')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when query is empty' do
|
|
|
|
let(:query) { '' }
|
|
|
|
|
|
|
|
it 'returns matched results' do
|
|
|
|
get :search, params: environment_params(format: :json, query: query)
|
|
|
|
|
|
|
|
expect(json_response)
|
|
|
|
.to contain_exactly('production', 'staging', 'review/patch-1', 'review/patch-2')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when query is review/patch-3' do
|
|
|
|
let(:query) { 'review/patch-3' }
|
|
|
|
|
|
|
|
it 'responds with status code 204' do
|
|
|
|
get :search, params: environment_params(format: :json, query: query)
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:no_content)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when query is partially matched in the middle of environment name' do
|
|
|
|
let(:query) { 'patch' }
|
|
|
|
|
|
|
|
it 'responds with status code 204' do
|
|
|
|
get :search, params: environment_params(format: :json, query: query)
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:no_content)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when query contains a wildcard character' do
|
|
|
|
let(:query) { 'review%' }
|
|
|
|
|
|
|
|
it 'prevents wildcard injection' do
|
|
|
|
get :search, params: environment_params(format: :json, query: query)
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:no_content)
|
|
|
|
end
|
|
|
|
end
|
2019-12-04 20:38:33 +05:30
|
|
|
|
|
|
|
context 'when query matches case insensitively' do
|
|
|
|
let(:query) { 'Prod' }
|
|
|
|
|
|
|
|
it 'returns matched results' do
|
|
|
|
get :search, params: environment_params(format: :json, query: query)
|
|
|
|
|
|
|
|
expect(json_response).to contain_exactly('production')
|
|
|
|
end
|
|
|
|
end
|
2019-03-02 22:35:43 +05:30
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def environment_params(opts = {})
|
|
|
|
opts.reverse_merge(namespace_id: project.namespace,
|
|
|
|
project_id: project,
|
|
|
|
id: environment.id)
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
2019-07-07 11:18:12 +05:30
|
|
|
|
|
|
|
def additional_metrics(opts = {})
|
|
|
|
get :additional_metrics, params: environment_params(format: :json, **opts)
|
|
|
|
end
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|