summaryrefslogtreecommitdiff
path: root/spec/requests/api/graphql/metrics/dashboard_query_spec.rb
blob: 1b84acff0e22777b5ed6c40bd0ec80ba1899a4f5 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'Getting Metrics Dashboard' do
  include GraphqlHelpers

  let_it_be(:current_user) { create(:user) }

  let(:project) { create(:project) }
  let(:environment) { create(:environment, project: project) }

  let(:query) do
    graphql_query_for(
      'project', { 'fullPath' => project.full_path },
      query_graphql_field(
        :environments, { 'name' => environment.name },
        query_graphql_field(
          :nodes, nil,
          query_graphql_field(
            :metricsDashboard, { 'path' => path },
            all_graphql_fields_for('MetricsDashboard'.classify)
          )
        )
      )
    )
  end

  context 'with metrics_dashboard_exhaustive_validations feature flag off' do
    before do
      stub_feature_flags(metrics_dashboard_exhaustive_validations: false)
    end

    context 'for anonymous user' do
      before do
        post_graphql(query, current_user: current_user)
      end

      context 'requested dashboard is available' do
        let(:path) { 'config/prometheus/common_metrics.yml' }

        it_behaves_like 'a working graphql query'

        it 'returns nil' do
          dashboard = graphql_data.dig('project', 'environments', 'nodes')

          expect(dashboard).to be_nil
        end
      end
    end

    context 'for user with developer access' do
      before do
        project.add_developer(current_user)
        post_graphql(query, current_user: current_user)
      end

      context 'requested dashboard is available' do
        let(:path) { 'config/prometheus/common_metrics.yml' }

        it_behaves_like 'a working graphql query'

        it 'returns metrics dashboard' do
          dashboard = graphql_data.dig('project', 'environments', 'nodes', 0, 'metricsDashboard')

          expect(dashboard).to eql("path" => path, "schemaValidationWarnings" => nil)
        end

        context 'invalid dashboard' do
          let(:path) { '.gitlab/dashboards/metrics.yml' }
          let(:project) { create(:project, :repository, :custom_repo, namespace: current_user.namespace, files: { path => "---\ndashboard: 'test'" }) }

          it 'returns metrics dashboard' do
            dashboard = graphql_data.dig('project', 'environments', 'nodes', 0, 'metricsDashboard')

            expect(dashboard).to eql("path" => path, "schemaValidationWarnings" => ["panel_groups: should be an array of panel_groups objects"])
          end
        end

        context 'empty dashboard' do
          let(:path) { '.gitlab/dashboards/metrics.yml' }
          let(:project) { create(:project, :repository, :custom_repo, namespace: current_user.namespace, files: { path => "" }) }

          it 'returns metrics dashboard' do
            dashboard = graphql_data.dig('project', 'environments', 'nodes', 0, 'metricsDashboard')

            expect(dashboard).to eql("path" => path, "schemaValidationWarnings" => ["dashboard: can't be blank", "panel_groups: should be an array of panel_groups objects"])
          end
        end
      end

      context 'requested dashboard can not be found' do
        let(:path) { 'config/prometheus/i_am_not_here.yml' }

        it_behaves_like 'a working graphql query'

        it 'returns nil' do
          dashboard = graphql_data.dig('project', 'environments', 'nodes', 0, 'metricsDashboard')

          expect(dashboard).to be_nil
        end
      end
    end
  end

  context 'with metrics_dashboard_exhaustive_validations feature flag on' do
    before do
      stub_feature_flags(metrics_dashboard_exhaustive_validations: true)
    end

    context 'for anonymous user' do
      before do
        post_graphql(query, current_user: current_user)
      end

      context 'requested dashboard is available' do
        let(:path) { 'config/prometheus/common_metrics.yml' }

        it_behaves_like 'a working graphql query'

        it 'returns nil' do
          dashboard = graphql_data.dig('project', 'environments', 'nodes')

          expect(dashboard).to be_nil
        end
      end
    end

    context 'for user with developer access' do
      before do
        project.add_developer(current_user)
        post_graphql(query, current_user: current_user)
      end

      context 'requested dashboard is available' do
        let(:path) { 'config/prometheus/common_metrics.yml' }

        it_behaves_like 'a working graphql query'

        it 'returns metrics dashboard' do
          dashboard = graphql_data.dig('project', 'environments', 'nodes', 0, 'metricsDashboard')

          expect(dashboard).to eql("path" => path, "schemaValidationWarnings" => nil)
        end

        context 'invalid dashboard' do
          let(:path) { '.gitlab/dashboards/metrics.yml' }
          let(:project) { create(:project, :repository, :custom_repo, namespace: current_user.namespace, files: { path => "---\ndashboard: 'test'" }) }

          it 'returns metrics dashboard' do
            dashboard = graphql_data.dig('project', 'environments', 'nodes', 0, 'metricsDashboard')

            expect(dashboard).to eql("path" => path, "schemaValidationWarnings" => ["root is missing required keys: panel_groups"])
          end
        end

        context 'empty dashboard' do
          let(:path) { '.gitlab/dashboards/metrics.yml' }
          let(:project) { create(:project, :repository, :custom_repo, namespace: current_user.namespace, files: { path => "" }) }

          it 'returns metrics dashboard' do
            dashboard = graphql_data.dig('project', 'environments', 'nodes', 0, 'metricsDashboard')

            expect(dashboard).to eql("path" => path, "schemaValidationWarnings" => ["root is missing required keys: dashboard, panel_groups"])
          end
        end
      end

      context 'requested dashboard can not be found' do
        let(:path) { 'config/prometheus/i_am_not_here.yml' }

        it_behaves_like 'a working graphql query'

        it 'returns nil' do
          dashboard = graphql_data.dig('project', 'environments', 'nodes', 0, 'metricsDashboard')

          expect(dashboard).to be_nil
        end
      end
    end
  end
end