diff options
author | syasonik <syasonik@gitlab.com> | 2019-04-22 14:23:35 +0800 |
---|---|---|
committer | syasonik <syasonik@gitlab.com> | 2019-04-24 18:23:04 +0800 |
commit | a08d4cad90f98169339a3793d18f1bae4e46ad83 (patch) | |
tree | 91238d10798fe5e896f05fe8c8d83e388706e6e0 /spec | |
parent | 25f957711dac1d401982c18da439580b2e9912c9 (diff) | |
download | gitlab-ce-a08d4cad90f98169339a3793d18f1bae4e46ad83.tar.gz |
Defend against dashboard errors, rework sequence
Diffstat (limited to 'spec')
-rw-r--r-- | spec/controllers/projects/environments_controller_spec.rb | 13 | ||||
-rw-r--r-- | spec/lib/gitlab/metrics_dashboard/processor_spec.rb | 26 | ||||
-rw-r--r-- | spec/lib/gitlab/metrics_dashboard/service_spec.rb | 16 |
3 files changed, 55 insertions, 0 deletions
diff --git a/spec/controllers/projects/environments_controller_spec.rb b/spec/controllers/projects/environments_controller_spec.rb index 6c3aad55622..b43698a6ef7 100644 --- a/spec/controllers/projects/environments_controller_spec.rb +++ b/spec/controllers/projects/environments_controller_spec.rb @@ -482,6 +482,19 @@ describe Projects::EnvironmentsController do expect(json_response.keys).to contain_exactly('dashboard', 'status') expect(json_response['dashboard']).to be_an_instance_of(Hash) end + + context 'when the dashboard could not be provided' do + before do + allow(YAML).to receive(:load_file).and_return({}) + end + + it 'returns an error response' do + get :metrics_dashboard, params: environment_params(format: :json) + + expect(response).to have_gitlab_http_status(:unprocessable_entity) + expect(json_response.keys).to contain_exactly('message', 'status', 'http_status') + end + end end end diff --git a/spec/lib/gitlab/metrics_dashboard/processor_spec.rb b/spec/lib/gitlab/metrics_dashboard/processor_spec.rb index 1bd905989fe..9a4897944c6 100644 --- a/spec/lib/gitlab/metrics_dashboard/processor_spec.rb +++ b/spec/lib/gitlab/metrics_dashboard/processor_spec.rb @@ -47,6 +47,32 @@ describe Gitlab::MetricsDashboard::Processor do expect(actual_metrics_order).to eq expected_metrics_order end end + + shared_examples_for 'errors with message' do |expected_message| + it 'raises a DashboardLayoutError' do + error_class = Gitlab::MetricsDashboard::Stages::BaseStage::DashboardLayoutError + + expect { dashboard }.to raise_error(error_class, expected_message) + end + end + + context 'when the dashboard is missing panel_groups' do + let(:dashboard_yml) { {} } + + it_behaves_like 'errors with message', 'Top-level key :panel_groups must be an array' + end + + context 'when the dashboard contains a panel_group which is missing panels' do + let(:dashboard_yml) { { panel_groups: [{}] } } + + it_behaves_like 'errors with message', 'Each "panel_group" must define an array :panels' + end + + context 'when the dashboard contains a panel which is missing metrics' do + let(:dashboard_yml) { { panel_groups: [{ panels: [{}] }] } } + + it_behaves_like 'errors with message', 'Each "panel" must define an array :metrics' + end end private diff --git a/spec/lib/gitlab/metrics_dashboard/service_spec.rb b/spec/lib/gitlab/metrics_dashboard/service_spec.rb index 710c71fd6bd..38c7942f250 100644 --- a/spec/lib/gitlab/metrics_dashboard/service_spec.rb +++ b/spec/lib/gitlab/metrics_dashboard/service_spec.rb @@ -24,5 +24,21 @@ describe Gitlab::MetricsDashboard::Service, :use_clean_rails_memory_store_cachin described_class.new(project, environment).get_dashboard described_class.new(project, environment).get_dashboard end + + context 'when the dashboard is configured incorrectly' do + let(:bad_dashboard) { {} } + + before do + allow(described_class).to receive(:system_dashboard).and_return(bad_dashboard) + end + + it 'returns an appropriate message and status code' do + result = described_class.new(project, environment).get_dashboard + + expect(result.keys).to contain_exactly(:message, :http_status, :status) + expect(result[:status]).to eq(:error) + expect(result[:status]).to eq(:unprocessable_entity) + end + end end end |