summaryrefslogtreecommitdiff
path: root/spec/services/metrics/dashboard/custom_dashboard_service_spec.rb
blob: d4391ecb6b96a757f4e709170ac0ba3bcd7a07b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Metrics::Dashboard::CustomDashboardService, :use_clean_rails_memory_store_caching do
  include MetricsDashboardHelpers

  let_it_be(:user) { create(:user) }
  let_it_be(:project) { create(:project) }
  let_it_be(:environment) { create(:environment, project: project) }

  let(:dashboard_path) { '.gitlab/dashboards/test.yml' }
  let(:service_params) { [project, user, { environment: environment, dashboard_path: dashboard_path }] }

  subject { described_class.new(*service_params) }

  before do
    project.add_maintainer(user)
  end

  describe '#raw_dashboard' do
    let(:project) { project_with_dashboard(dashboard_path) }

    it_behaves_like '#raw_dashboard raises error if dashboard loading fails'
  end

  describe '#get_dashboard' do
    let(:service_call) { subject.get_dashboard }

    context 'when the dashboard does not exist' do
      it_behaves_like 'misconfigured dashboard service response', :not_found

      it 'does not update gitlab_metrics_dashboard_processing_time_ms metric', :prometheus do
        service_call
        metric = subject.send(:processing_time_metric)
        labels = subject.send(:processing_time_metric_labels)

        expect(metric.get(labels)).to eq(0)
      end
    end

    it_behaves_like 'raises error for users with insufficient permissions'

    context 'when the dashboard exists' do
      let(:project) { project_with_dashboard(dashboard_path) }

      it_behaves_like 'valid dashboard service response'
      it_behaves_like 'updates gitlab_metrics_dashboard_processing_time_ms metric'

      it 'caches the unprocessed dashboard for subsequent calls' do
        expect_any_instance_of(described_class)
          .to receive(:get_raw_dashboard)
          .once
          .and_call_original

        described_class.new(*service_params).get_dashboard
        described_class.new(*service_params).get_dashboard
      end

      context 'and the dashboard is then deleted' do
        it 'does not return the previously cached dashboard' do
          described_class.new(*service_params).get_dashboard

          delete_project_dashboard(project, user, dashboard_path)

          expect_any_instance_of(described_class)
          .to receive(:get_raw_dashboard)
          .once
          .and_call_original

          described_class.new(*service_params).get_dashboard
        end
      end
    end

    context 'when the dashboard is configured incorrectly' do
      let(:project) { project_with_dashboard(dashboard_path, {}) }

      it_behaves_like 'misconfigured dashboard service response', :unprocessable_entity
    end
  end

  describe '.all_dashboard_paths' do
    let(:all_dashboards) { described_class.all_dashboard_paths(project) }

    context 'when there are no project dashboards' do
      it 'returns an empty array' do
        expect(all_dashboards).to be_empty
      end
    end

    context 'when there are project dashboards available' do
      let(:dashboard_path) { '.gitlab/dashboards/test.yml' }
      let(:project) { project_with_dashboard(dashboard_path) }

      it 'returns the dashboard attributes' do
        expect(all_dashboards).to eq(
          [{
            path: dashboard_path,
            display_name: 'test.yml',
            default: false,
            system_dashboard: false,
            out_of_the_box_dashboard: false
          }]
        )
      end
    end
  end

  describe '.valid_params?' do
    let(:params) { { dashboard_path: '.gitlab/dashboard/test.yml' } }

    subject { described_class.valid_params?(params) }

    it { is_expected.to be_truthy }

    context 'missing dashboard_path' do
      let(:params) { {} }

      it { is_expected.to be_falsey }
    end

    context 'empty dashboard_path' do
      let(:params) { { dashboard_path: '' } }

      it { is_expected.to be_falsey }
    end
  end
end