diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2023-06-06 00:09:04 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2023-06-06 00:09:04 +0300 |
commit | 96e23b2017cbe56969771960f6c274c5d3599397 (patch) | |
tree | b8b17da1ab080dd41fc64fc0262de2cf16754559 /spec/controllers | |
parent | 2f1a81fd16ff9968d6b986f8a407d963bc2218f9 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/controllers')
-rw-r--r-- | spec/controllers/projects/environments_controller_spec.rb | 405 | ||||
-rw-r--r-- | spec/controllers/projects/settings/operations_controller_spec.rb | 48 |
2 files changed, 0 insertions, 453 deletions
diff --git a/spec/controllers/projects/environments_controller_spec.rb b/spec/controllers/projects/environments_controller_spec.rb index 22804339fef..5f03d721fe7 100644 --- a/spec/controllers/projects/environments_controller_spec.rb +++ b/spec/controllers/projects/environments_controller_spec.rb @@ -3,7 +3,6 @@ require 'spec_helper' RSpec.describe Projects::EnvironmentsController, feature_category: :continuous_delivery do - include MetricsDashboardHelpers include KubernetesHelpers let_it_be(:project) { create(:project, :repository) } @@ -15,7 +14,6 @@ RSpec.describe Projects::EnvironmentsController, feature_category: :continuous_d let!(:environment) { create(:environment, name: 'production', project: project) } before do - stub_feature_flags(remove_monitor_metrics: false) sign_in(user) end @@ -538,405 +536,6 @@ RSpec.describe Projects::EnvironmentsController, feature_category: :continuous_d end end - describe 'GET #metrics_redirect' do - it 'redirects to metrics dashboard page' do - get :metrics_redirect, params: { namespace_id: project.namespace, project_id: project } - - expect(response).to redirect_to(project_metrics_dashboard_path(project)) - end - - context 'when metrics dashboard feature is unavailable' do - before do - stub_feature_flags(remove_monitor_metrics: true) - end - - it 'returns 404 not found' do - get :metrics_redirect, params: { namespace_id: project.namespace, project_id: project } - - expect(response).to have_gitlab_http_status(:not_found) - end - end - end - - describe 'GET #metrics' do - before do - allow(controller).to receive(:environment).and_return(environment) - end - - context 'when environment has no metrics' do - it 'redirects to metrics dashboard page' do - expect(environment).not_to receive(:metrics) - - get :metrics, params: environment_params - - expect(response).to redirect_to(project_metrics_dashboard_path(project, environment: environment)) - end - - context 'when requesting metrics as JSON' do - it 'returns a metrics JSON document' do - expect(environment).to receive(:metrics).and_return(nil) - - get :metrics, params: environment_params(format: :json) - - expect(response).to have_gitlab_http_status(:no_content) - 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 - get :metrics, params: environment_params(format: :json) - - 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 - - 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 - - context 'with anonymous user and public dashboard visibility' do - let(:project) { create(:project, :public) } - let(:user) { create(:user) } - - it 'redirects to metrics dashboard page' do - project.project_feature.update!(metrics_dashboard_access_level: ProjectFeature::ENABLED) - - get :metrics, params: environment_params - - expect(response).to redirect_to(project_metrics_dashboard_path(project, environment: environment)) - end - end - - context 'when metrics dashboard feature is unavailable' do - before do - stub_feature_flags(remove_monitor_metrics: true) - end - - it 'returns 404 not found' do - expect(environment).not_to receive(:metrics) - - get :metrics, params: environment_params - - expect(response).to have_gitlab_http_status(:not_found) - end - end - end - - describe 'GET #additional_metrics' do - let(:window_params) { { start: '1554702993.5398998', end: '1554717396.996232' } } - - 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 - additional_metrics(window_params) - - expect(response).to have_gitlab_http_status(:no_content) - 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 - additional_metrics(window_params) - - 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 - end - - 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) - end - end - - context 'when only one time param is provided' do - it 'raises an error when start is missing' do - expect { additional_metrics(end: '1552647300.651094') } - .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 - end - - 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 - - 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 - - context 'when metrics dashboard feature is unavailable' do - before do - stub_feature_flags(remove_monitor_metrics: true) - end - - it 'returns 404 not found' do - additional_metrics(window_params) - - expect(response).to have_gitlab_http_status(:not_found) - end - end - end - - describe 'GET #metrics_dashboard' do - let(:metrics_dashboard_req_params) { environment_params(dashboard_params) } - - shared_examples_for '200 response' do - it_behaves_like 'GET #metrics_dashboard correctly formatted response' do - let(:expected_keys) { %w(dashboard status metrics_data) } - let(:status_code) { :ok } - end - end - - shared_examples_for 'error response' do |status_code| - it_behaves_like 'GET #metrics_dashboard correctly formatted response' do - let(:expected_keys) { %w(message status) } - let(:status_code) { status_code } - end - end - - shared_examples_for 'includes all dashboards' do - it 'includes info for all findable dashboard' do - get :metrics_dashboard, params: environment_params(dashboard_params) - - 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')) - end - end - - shared_examples_for 'the default dashboard' do - it_behaves_like 'includes all dashboards' - it_behaves_like 'GET #metrics_dashboard for dashboard', 'Environment metrics' - end - - shared_examples_for 'the specified dashboard' do |expected_dashboard| - it_behaves_like 'includes all dashboards' - - it_behaves_like 'GET #metrics_dashboard for dashboard', expected_dashboard - - context 'when the dashboard cannot not be processed' do - before do - allow(YAML).to receive(:safe_load).and_return({}) - end - - it_behaves_like 'error response', :unprocessable_entity - end - end - - shared_examples_for 'specified dashboard embed' do |expected_titles| - it_behaves_like '200 response' - - it 'contains only the specified charts' do - get :metrics_dashboard, params: environment_params(dashboard_params) - - 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 - expect(titles).to eq expected_titles - end - end - - shared_examples_for 'the default dynamic dashboard' do - it_behaves_like 'specified dashboard embed', ['Memory Usage (Total)', 'Core Usage (Total)'] - end - - 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 } } - - it_behaves_like 'error response', :not_found - - 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) } - - before do - project.add_maintainer(user) - end - - it_behaves_like 'the specified dashboard', 'Test Dashboard' - end - - context 'when the specified dashboard is the default dashboard' do - let(:dashboard_path) { system_dashboard_path } - - it_behaves_like 'the default dashboard' - end - end - end - - shared_examples_for 'dashboard can be embedded' do - context 'when the embedded flag is included' do - let(:dashboard_params) { { format: :json, embedded: true } } - - it_behaves_like 'the default dynamic dashboard' - - 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 } } - - # The superfluous param should be ignored. - it_behaves_like 'the default dynamic dashboard' - end - - 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 - end - end - - 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' - - 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 - - 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 - - context 'when metrics dashboard feature is unavailable' do - before do - stub_feature_flags(remove_monitor_metrics: true) - end - - it 'returns 404 not found' do - get :metrics_dashboard, params: environment_params(dashboard_params) - - expect(response).to have_gitlab_http_status(:not_found) - end - end - end - end - describe 'GET #search' do before do create(:environment, name: 'staging', project: project) @@ -1053,8 +652,4 @@ RSpec.describe Projects::EnvironmentsController, feature_category: :continuous_d def environment_params(opts = {}) opts.reverse_merge(namespace_id: project.namespace, project_id: project, id: environment.id) end - - def additional_metrics(opts = {}) - get :additional_metrics, params: environment_params(format: :json, **opts) - end end diff --git a/spec/controllers/projects/settings/operations_controller_spec.rb b/spec/controllers/projects/settings/operations_controller_spec.rb index 04dbd9ab671..770c1991144 100644 --- a/spec/controllers/projects/settings/operations_controller_spec.rb +++ b/spec/controllers/projects/settings/operations_controller_spec.rb @@ -11,8 +11,6 @@ RSpec.describe Projects::Settings::OperationsController, feature_category: :inci end before do - stub_feature_flags(remove_monitor_metrics: false) - sign_in(user) end @@ -67,20 +65,6 @@ RSpec.describe Projects::Settings::OperationsController, feature_category: :inci end end - shared_examples 'PATCHable without metrics dashboard' do - context 'when metrics dashboard feature is unavailable' do - before do - stub_feature_flags(remove_monitor_metrics: true) - end - - include_examples 'PATCHable' do - let(:permitted_params) do - ActionController::Parameters.new({}).permit! - end - end - end - end - describe 'GET #show' do it 'renders show template' do get :show, params: project_params(project) @@ -339,38 +323,6 @@ RSpec.describe Projects::Settings::OperationsController, feature_category: :inci end end - context 'metrics dashboard setting', feature_category: :metrics do - describe 'PATCH #update' do - let(:params) do - { - metrics_setting_attributes: { - external_dashboard_url: 'https://gitlab.com' - } - } - end - - include_examples 'PATCHable' - include_examples 'PATCHable without metrics dashboard' - end - end - - context 'grafana integration', feature_category: :metrics do - describe 'PATCH #update' do - let(:params) do - { - grafana_integration_attributes: { - grafana_url: 'https://grafana.gitlab.com', - token: 'eyJrIjoicDRlRTREdjhhOEZ5WjZPWXUzazJOSW0zZHJUejVOd3IiLCJuIjoiVGVzdCBLZXkiLCJpZCI6MX0=', - enabled: 'true' - } - } - end - - include_examples 'PATCHable' - include_examples 'PATCHable without metrics dashboard' - end - end - context 'prometheus integration' do describe 'POST #reset_alerting_token' do context 'with existing alerting setting' do |