summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/metrics/dashboard/url_spec.rb
blob: 830d43169a99f94411912d49ce25c37e03e9fb05 (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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Metrics::Dashboard::Url do
  include Gitlab::Routing.url_helpers

  describe '#metrics_regex' do
    let(:environment_id) { 1 }
    let(:url_params) do
      [
        'foo',
        'bar',
        environment_id,
        {
          start: '2019-08-02T05:43:09.000Z',
          dashboard: 'config/prometheus/common_metrics.yml',
          group: 'awesome group',
          anchor: 'title'
        }
      ]
    end

    let(:expected_params) do
      {
        'url' => url,
        'namespace' => 'foo',
        'project' => 'bar',
        'environment' => '1',
        'query' => '?dashboard=config%2Fprometheus%2Fcommon_metrics.yml&group=awesome+group&start=2019-08-02T05%3A43%3A09.000Z',
        'anchor' => '#title'
      }
    end

    subject { described_class.metrics_regex }

    context 'for /-/environments/:environment_id/metrics route' do
      let(:url) { metrics_namespace_project_environment_url(*url_params) }

      it_behaves_like 'regex which matches url when expected'
    end

    context 'for /-/metrics?environment=:environment_id route' do
      let(:url) { namespace_project_metrics_dashboard_url(*url_params) }
      let(:url_params) do
        [
          'namespace1',
          'project1',
          {
            environment: environment_id,
            start: '2019-08-02T05:43:09.000Z',
            dashboard: 'config/prometheus/common_metrics.yml',
            group: 'awesome group',
            anchor: 'title'
          }
        ]
      end

      let(:expected_params) do
        {
          'url' => url,
          'namespace' => 'namespace1',
          'project' => 'project1',
          'environment' => "#{environment_id}",
          'query' => "?dashboard=config%2Fprometheus%2Fcommon_metrics.yml&environment=#{environment_id}&group=awesome+group&start=2019-08-02T05%3A43%3A09.000Z",
          'anchor' => '#title'
        }
      end

      it_behaves_like 'regex which matches url when expected'
    end

    context 'for metrics_dashboard route' do
      let(:url) { metrics_dashboard_namespace_project_environment_url(*url_params) }

      it_behaves_like 'regex which matches url when expected'
    end
  end

  describe '#clusters_regex' do
    let(:url) { Gitlab::Routing.url_helpers.namespace_project_cluster_url(*url_params) }
    let(:url_params) do
      [
        'foo',
        'bar',
        '1',
        {
          group: 'Cluster Health',
          title: 'Memory Usage',
          y_label: 'Memory 20(GiB)',
          anchor: 'title'
        }
      ]
    end

    let(:expected_params) do
      {
        'url' => url,
        'namespace' => 'foo',
        'project' => 'bar',
        'cluster_id' => '1',
        'query' => '?group=Cluster+Health&title=Memory+Usage&y_label=Memory+20%28GiB%29',
        'anchor' => '#title'
      }
    end

    subject { described_class.clusters_regex }

    it_behaves_like 'regex which matches url when expected'

    context 'for metrics_dashboard route' do
      let(:url) do
        metrics_dashboard_namespace_project_cluster_url(
          *url_params, cluster_type: :project, embedded: true, format: :json
        )
      end

      let(:expected_params) do
        {
          'url' => url,
          'namespace' => 'foo',
          'project' => 'bar',
          'cluster_id' => '1',
          'query' => '?cluster_type=project&embedded=true',
          'anchor' => nil
        }
      end

      it_behaves_like 'regex which matches url when expected'
    end
  end

  describe '#grafana_regex' do
    let(:url) do
      namespace_project_grafana_api_metrics_dashboard_url(
        'foo',
        'bar',
        start: '2019-08-02T05:43:09.000Z',
        dashboard: 'config/prometheus/common_metrics.yml',
        group: 'awesome group',
        anchor: 'title'
      )
    end

    let(:expected_params) do
      {
        'url' => url,
        'namespace' => 'foo',
        'project' => 'bar',
        'query' => '?dashboard=config%2Fprometheus%2Fcommon_metrics.yml&group=awesome+group&start=2019-08-02T05%3A43%3A09.000Z',
        'anchor' => '#title'
      }
    end

    subject { described_class.grafana_regex }

    it_behaves_like 'regex which matches url when expected'
  end

  describe '#alert_regex' do
    let(:url) { Gitlab::Routing.url_helpers.metrics_dashboard_namespace_project_prometheus_alert_url(*url_params) }
    let(:url_params) do
      [
        'foo',
        'bar',
        '1',
        {
          start: '2020-02-10T12:59:49.938Z',
          end: '2020-02-10T20:59:49.938Z',
          anchor: "anchor"
        }
      ]
    end

    let(:expected_params) do
      {
        'url' => url,
        'namespace' => 'foo',
        'project' => 'bar',
        'alert' => '1',
        'query' => "?end=2020-02-10T20%3A59%3A49.938Z&start=2020-02-10T12%3A59%3A49.938Z",
        'anchor' => '#anchor'
      }
    end

    subject { described_class.alert_regex }

    it_behaves_like 'regex which matches url when expected'

    it_behaves_like 'regex which matches url when expected' do
      let(:url) { Gitlab::Routing.url_helpers.metrics_dashboard_namespace_project_prometheus_alert_url(*url_params, format: :json) }

      let(:expected_params) do
        {
          'url' => url,
          'namespace' => 'foo',
          'project' => 'bar',
          'alert' => '1',
          'query' => nil,
          'anchor' => nil
        }
      end
    end
  end

  describe '#build_dashboard_url' do
    it 'builds the url for the dashboard endpoint' do
      url = described_class.build_dashboard_url('foo', 'bar', 1)

      expect(url).to match described_class.metrics_regex
    end
  end
end