Welcome to mirror list, hosted at ThFree Co, Russian Federation.

validator_spec.rb « dashboard « metrics « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: aaa9daf8feebd99b6027be9c27432379a8999473 (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Metrics::Dashboard::Validator do
  include MetricsDashboardHelpers

  let_it_be(:valid_dashboard) { load_sample_dashboard }
  let_it_be(:invalid_dashboard) { load_dashboard_yaml(fixture_file('lib/gitlab/metrics/dashboard/invalid_dashboard.yml')) }
  let_it_be(:duplicate_id_dashboard) { load_dashboard_yaml(fixture_file('lib/gitlab/metrics/dashboard/duplicate_id_dashboard.yml')) }

  let_it_be(:project) { create(:project) }

  describe '#validate' do
    context 'valid dashboard schema' do
      it 'returns true' do
        expect(described_class.validate(valid_dashboard)).to be true
      end

      context 'with duplicate metric_ids' do
        it 'returns false' do
          expect(described_class.validate(duplicate_id_dashboard)).to be false
        end
      end

      context 'with dashboard_path and project' do
        subject { described_class.validate(valid_dashboard, dashboard_path: 'test/path.yml', project: project) }

        context 'with no conflicting metric identifiers in db' do
          it { is_expected.to be true }
        end

        context 'with metric identifier present in current dashboard' do
          before do
            create(:prometheus_metric,
              identifier: 'metric_a1',
              dashboard_path: 'test/path.yml',
              project: project
            )
          end

          it { is_expected.to be true }
        end

        context 'with metric identifier present in another dashboard' do
          before do
            create(:prometheus_metric,
              identifier: 'metric_a1',
              dashboard_path: 'some/other/dashboard/path.yml',
              project: project
            )
          end

          it { is_expected.to be false }
        end
      end
    end

    context 'invalid dashboard schema' do
      it 'returns false' do
        expect(described_class.validate(invalid_dashboard)).to be false
      end
    end
  end

  describe '#validate!' do
    shared_examples 'validation failed' do |errors_message|
      it 'raises error with corresponding messages', :aggregate_failures do
        expect { subject }.to raise_error do |error|
          expect(error).to be_kind_of(Gitlab::Metrics::Dashboard::Validator::Errors::InvalidDashboardError)
          expect(error.message).to eq(errors_message)
        end
      end
    end

    context 'valid dashboard schema' do
      it 'returns true' do
        expect(described_class.validate!(valid_dashboard)).to be true
      end

      context 'with duplicate metric_ids' do
        subject { described_class.validate!(duplicate_id_dashboard) }

        it_behaves_like 'validation failed', 'metric_id must be unique across a project'
      end

      context 'with dashboard_path and project' do
        subject { described_class.validate!(valid_dashboard, dashboard_path: 'test/path.yml', project: project) }

        context 'with no conflicting metric identifiers in db' do
          it { is_expected.to be true }
        end

        context 'with metric identifier present in current dashboard' do
          before do
            create(:prometheus_metric,
              identifier: 'metric_a1',
              dashboard_path: 'test/path.yml',
              project: project
            )
          end

          it { is_expected.to be true }
        end

        context 'with metric identifier present in another dashboard' do
          before do
            create(:prometheus_metric,
              identifier: 'metric_a1',
              dashboard_path: 'some/other/dashboard/path.yml',
              project: project
            )
          end

          it_behaves_like 'validation failed', 'metric_id must be unique across a project'
        end
      end
    end

    context 'invalid dashboard schema' do
      subject { described_class.validate!(invalid_dashboard) }

      context 'wrong property type' do
        it_behaves_like 'validation failed', "'this_should_be_a_int' at /panel_groups/0/panels/0/weight is not of type: number"
      end

      context 'panel groups missing' do
        let_it_be(:invalid_dashboard) { load_dashboard_yaml(fixture_file('lib/gitlab/metrics/dashboard/dashboard_missing_panel_groups.yml')) }

        it_behaves_like 'validation failed', 'root is missing required keys: panel_groups'
      end

      context 'groups are missing panels and group keys' do
        let_it_be(:invalid_dashboard) { load_dashboard_yaml(fixture_file('lib/gitlab/metrics/dashboard/dashboard_groups_missing_panels_and_group.yml')) }

        it_behaves_like 'validation failed', '/panel_groups/0 is missing required keys: group'
      end

      context 'panel is missing metrics key' do
        let_it_be(:invalid_dashboard) { load_dashboard_yaml(fixture_file('lib/gitlab/metrics/dashboard/dashboard_panel_is_missing_metrics.yml')) }

        it_behaves_like 'validation failed', '/panel_groups/0/panels/0 is missing required keys: metrics'
      end
    end
  end

  describe '#errors' do
    context 'valid dashboard schema' do
      it 'returns no errors' do
        expect(described_class.errors(valid_dashboard)).to eq []
      end

      context 'with duplicate metric_ids' do
        it 'returns errors' do
          expect(described_class.errors(duplicate_id_dashboard)).to eq [Gitlab::Metrics::Dashboard::Validator::Errors::DuplicateMetricIds.new]
        end
      end

      context 'with dashboard_path and project' do
        subject { described_class.errors(valid_dashboard, dashboard_path: 'test/path.yml', project: project) }

        context 'with no conflicting metric identifiers in db' do
          it { is_expected.to eq [] }
        end

        context 'with metric identifier present in current dashboard' do
          before do
            create(:prometheus_metric,
                   identifier: 'metric_a1',
                   dashboard_path: 'test/path.yml',
                   project: project
                  )
          end

          it { is_expected.to eq [] }
        end

        context 'with metric identifier present in another dashboard' do
          before do
            create(:prometheus_metric,
                   identifier: 'metric_a1',
                   dashboard_path: 'some/other/dashboard/path.yml',
                   project: project
                  )
          end

          it { is_expected.to eq [Gitlab::Metrics::Dashboard::Validator::Errors::DuplicateMetricIds.new] }
        end
      end
    end

    context 'invalid dashboard schema' do
      it 'returns collection of validation errors' do
        expect(described_class.errors(invalid_dashboard)).to all be_kind_of(Gitlab::Metrics::Dashboard::Validator::Errors::SchemaValidationError)
      end
    end
  end
end