summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/usage_data_spec.rb
blob: a46778bb6c38076ed7a19572d040104080141843 (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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
# frozen_string_literal: true

require 'spec_helper'

describe Gitlab::UsageData, :aggregate_failures do
  include UsageDataHelpers

  before do
    allow(ActiveRecord::Base.connection).to receive(:transaction_open?).and_return(false)
  end

  shared_examples "usage data execution" do
    describe '#data' do
      let!(:ud) { build(:usage_data) }

      before do
        allow(described_class).to receive(:grafana_embed_usage_data).and_return(2)
      end

      subject { described_class.data }

      it 'gathers usage data' do
        expect(subject.keys).to include(*UsageDataHelpers::USAGE_DATA_KEYS)
      end

      it 'gathers usage counts' do
        count_data = subject[:counts]

        expect(count_data[:boards]).to eq(1)
        expect(count_data[:projects]).to eq(4)
        expect(count_data.values_at(*UsageDataHelpers::SMAU_KEYS)).to all(be_an(Integer))
        expect(count_data.keys).to include(*UsageDataHelpers::COUNTS_KEYS)
        expect(UsageDataHelpers::COUNTS_KEYS - count_data.keys).to be_empty
      end

      it 'gathers projects data correctly' do
        count_data = subject[:counts]

        expect(count_data[:projects]).to eq(4)
        expect(count_data[:projects_asana_active]).to eq(0)
        expect(count_data[:projects_prometheus_active]).to eq(1)
        expect(count_data[:projects_jira_active]).to eq(4)
        expect(count_data[:projects_jira_server_active]).to eq(2)
        expect(count_data[:projects_jira_cloud_active]).to eq(2)
        expect(count_data[:projects_slack_notifications_active]).to eq(2)
        expect(count_data[:projects_slack_slash_active]).to eq(1)
        expect(count_data[:projects_slack_active]).to eq(2)
        expect(count_data[:projects_slack_slash_commands_active]).to eq(1)
        expect(count_data[:projects_custom_issue_tracker_active]).to eq(1)
        expect(count_data[:projects_mattermost_active]).to eq(0)
        expect(count_data[:projects_with_repositories_enabled]).to eq(3)
        expect(count_data[:projects_with_error_tracking_enabled]).to eq(1)
        expect(count_data[:projects_with_alerts_service_enabled]).to eq(1)
        expect(count_data[:projects_with_prometheus_alerts]).to eq(2)
        expect(count_data[:issues_created_from_gitlab_error_tracking_ui]).to eq(1)
        expect(count_data[:issues_with_associated_zoom_link]).to eq(2)
        expect(count_data[:issues_using_zoom_quick_actions]).to eq(3)
        expect(count_data[:issues_with_embedded_grafana_charts_approx]).to eq(2)
        expect(count_data[:incident_issues]).to eq(4)

        expect(count_data[:clusters_enabled]).to eq(6)
        expect(count_data[:project_clusters_enabled]).to eq(4)
        expect(count_data[:group_clusters_enabled]).to eq(1)
        expect(count_data[:instance_clusters_enabled]).to eq(1)
        expect(count_data[:clusters_disabled]).to eq(3)
        expect(count_data[:project_clusters_disabled]).to eq(1)
        expect(count_data[:group_clusters_disabled]).to eq(1)
        expect(count_data[:instance_clusters_disabled]).to eq(1)
        expect(count_data[:clusters_platforms_eks]).to eq(1)
        expect(count_data[:clusters_platforms_gke]).to eq(1)
        expect(count_data[:clusters_platforms_user]).to eq(1)
        expect(count_data[:clusters_applications_helm]).to eq(1)
        expect(count_data[:clusters_applications_ingress]).to eq(1)
        expect(count_data[:clusters_applications_cert_managers]).to eq(1)
        expect(count_data[:clusters_applications_crossplane]).to eq(1)
        expect(count_data[:clusters_applications_prometheus]).to eq(1)
        expect(count_data[:clusters_applications_runner]).to eq(1)
        expect(count_data[:clusters_applications_knative]).to eq(1)
        expect(count_data[:clusters_applications_elastic_stack]).to eq(1)
        expect(count_data[:grafana_integrated_projects]).to eq(2)
        expect(count_data[:clusters_applications_jupyter]).to eq(1)
        expect(count_data[:clusters_management_project]).to eq(1)
      end

      it 'works when queries time out' do
        allow_any_instance_of(ActiveRecord::Relation)
          .to receive(:count).and_raise(ActiveRecord::StatementInvalid.new(''))

        expect { subject }.not_to raise_error
      end

      it 'jira usage works when queries time out' do
        allow_any_instance_of(ActiveRecord::Relation)
          .to receive(:find_in_batches).and_raise(ActiveRecord::StatementInvalid.new(''))

        expect { described_class.jira_usage }.not_to raise_error
      end
    end

    describe '#usage_data_counters' do
      subject { described_class.usage_data_counters }

      it { is_expected.to all(respond_to :totals) }

      describe 'the results of calling #totals on all objects in the array' do
        subject { described_class.usage_data_counters.map(&:totals) }

        it { is_expected.to all(be_a Hash) }
        it { is_expected.to all(have_attributes(keys: all(be_a Symbol), values: all(be_a Integer))) }
      end

      it 'does not have any conflicts' do
        all_keys = subject.flat_map { |counter| counter.totals.keys }

        expect(all_keys.size).to eq all_keys.to_set.size
      end
    end

    describe '#license_usage_data' do
      subject { described_class.license_usage_data }

      it 'gathers license data' do
        expect(subject[:uuid]).to eq(Gitlab::CurrentSettings.uuid)
        expect(subject[:version]).to eq(Gitlab::VERSION)
        expect(subject[:installation_type]).to eq('gitlab-development-kit')
        expect(subject[:active_user_count]).to eq(User.active.size)
        expect(subject[:recorded_at]).to be_a(Time)
      end
    end

    context 'when not relying on database records' do
      describe '#features_usage_data_ce' do
        subject { described_class.features_usage_data_ce }

        it 'gathers feature usage data' do
          expect(subject[:mattermost_enabled]).to eq(Gitlab.config.mattermost.enabled)
          expect(subject[:signup_enabled]).to eq(Gitlab::CurrentSettings.allow_signup?)
          expect(subject[:ldap_enabled]).to eq(Gitlab.config.ldap.enabled)
          expect(subject[:gravatar_enabled]).to eq(Gitlab::CurrentSettings.gravatar_enabled?)
          expect(subject[:omniauth_enabled]).to eq(Gitlab::Auth.omniauth_enabled?)
          expect(subject[:reply_by_email_enabled]).to eq(Gitlab::IncomingEmail.enabled?)
          expect(subject[:container_registry_enabled]).to eq(Gitlab.config.registry.enabled)
          expect(subject[:dependency_proxy_enabled]).to eq(Gitlab.config.dependency_proxy.enabled)
          expect(subject[:gitlab_shared_runners_enabled]).to eq(Gitlab.config.gitlab_ci.shared_runners_enabled)
          expect(subject[:web_ide_clientside_preview_enabled]).to eq(Gitlab::CurrentSettings.web_ide_clientside_preview_enabled?)
        end

        context 'with existing container expiration policies' do
          let_it_be(:disabled) { create(:container_expiration_policy, enabled: false) }
          let_it_be(:enabled) { create(:container_expiration_policy, enabled: true) }
          %i[keep_n cadence older_than].each do |attribute|
            ContainerExpirationPolicy.send("#{attribute}_options").keys.each do |value|
              let_it_be("container_expiration_policy_with_#{attribute}_set_to_#{value}") { create(:container_expiration_policy, attribute => value) }
            end
          end

          let(:inactive_policies) { ::ContainerExpirationPolicy.where(enabled: false) }
          let(:active_policies) { ::ContainerExpirationPolicy.active }

          it 'gathers usage data' do
            expect(subject[:projects_with_expiration_policy_enabled]).to eq 16
            expect(subject[:projects_with_expiration_policy_disabled]).to eq 1

            expect(subject[:projects_with_expiration_policy_enabled_with_keep_n_unset]).to eq 10
            expect(subject[:projects_with_expiration_policy_enabled_with_keep_n_set_to_1]).to eq 1
            expect(subject[:projects_with_expiration_policy_enabled_with_keep_n_set_to_5]).to eq 1
            expect(subject[:projects_with_expiration_policy_enabled_with_keep_n_set_to_10]).to eq 1
            expect(subject[:projects_with_expiration_policy_enabled_with_keep_n_set_to_25]).to eq 1
            expect(subject[:projects_with_expiration_policy_enabled_with_keep_n_set_to_50]).to eq 1

            expect(subject[:projects_with_expiration_policy_enabled_with_older_than_unset]).to eq 12
            expect(subject[:projects_with_expiration_policy_enabled_with_older_than_set_to_7d]).to eq 1
            expect(subject[:projects_with_expiration_policy_enabled_with_older_than_set_to_14d]).to eq 1
            expect(subject[:projects_with_expiration_policy_enabled_with_older_than_set_to_30d]).to eq 1
            expect(subject[:projects_with_expiration_policy_enabled_with_older_than_set_to_90d]).to eq 1

            expect(subject[:projects_with_expiration_policy_enabled_with_cadence_set_to_1d]).to eq 12
            expect(subject[:projects_with_expiration_policy_enabled_with_cadence_set_to_7d]).to eq 1
            expect(subject[:projects_with_expiration_policy_enabled_with_cadence_set_to_14d]).to eq 1
            expect(subject[:projects_with_expiration_policy_enabled_with_cadence_set_to_1month]).to eq 1
            expect(subject[:projects_with_expiration_policy_enabled_with_cadence_set_to_3month]).to eq 1
          end
        end
      end

      describe '#components_usage_data' do
        subject { described_class.components_usage_data }

        it 'gathers components usage data' do
          expect(Gitlab::UsageData).to receive(:app_server_type).and_return('server_type')
          expect(subject[:app_server][:type]).to eq('server_type')
          expect(subject[:gitlab_pages][:enabled]).to eq(Gitlab.config.pages.enabled)
          expect(subject[:gitlab_pages][:version]).to eq(Gitlab::Pages::VERSION)
          expect(subject[:git][:version]).to eq(Gitlab::Git.version)
          expect(subject[:database][:adapter]).to eq(Gitlab::Database.adapter_name)
          expect(subject[:database][:version]).to eq(Gitlab::Database.version)
          expect(subject[:gitaly][:version]).to be_present
          expect(subject[:gitaly][:servers]).to be >= 1
          expect(subject[:gitaly][:filesystems]).to be_an(Array)
          expect(subject[:gitaly][:filesystems].first).to be_a(String)
        end
      end

      describe '#app_server_type' do
        subject { described_class.app_server_type }

        it 'successfully identifies runtime and returns the identifier' do
          expect(Gitlab::Runtime).to receive(:identify).and_return(:runtime_identifier)

          is_expected.to eq('runtime_identifier')
        end

        context 'when runtime is not identified' do
          let(:exception) { Gitlab::Runtime::IdentificationError.new('exception message from runtime identify') }

          it 'logs the exception and returns unknown app server type' do
            expect(Gitlab::Runtime).to receive(:identify).and_raise(exception)

            expect(Gitlab::AppLogger).to receive(:error).with(exception.message)
            expect(Gitlab::ErrorTracking).to receive(:track_exception).with(exception)
            expect(subject).to eq('unknown_app_server_type')
          end
        end
      end

      describe '#cycle_analytics_usage_data' do
        subject { described_class.cycle_analytics_usage_data }

        it 'works when queries time out in new' do
          allow(Gitlab::CycleAnalytics::UsageData)
            .to receive(:new).and_raise(ActiveRecord::StatementInvalid.new(''))

          expect { subject }.not_to raise_error
        end

        it 'works when queries time out in to_json' do
          allow_any_instance_of(Gitlab::CycleAnalytics::UsageData)
            .to receive(:to_json).and_raise(ActiveRecord::StatementInvalid.new(''))

          expect { subject }.not_to raise_error
        end
      end

      describe '#ingress_modsecurity_usage' do
        subject { described_class.ingress_modsecurity_usage }

        it 'gathers variable data' do
          allow_any_instance_of(
            ::Clusters::Applications::IngressModsecurityUsageService
          ).to receive(:execute).and_return(
            {
              ingress_modsecurity_blocking: 1,
              ingress_modsecurity_disabled: 2
            }
          )

          expect(subject[:ingress_modsecurity_blocking]).to eq(1)
          expect(subject[:ingress_modsecurity_disabled]).to eq(2)
        end
      end

      describe '#grafana_embed_usage_data' do
        subject { described_class.grafana_embed_usage_data }

        let(:project) { create(:project) }
        let(:description_with_embed) { "Some comment\n\nhttps://grafana.example.com/d/xvAk4q0Wk/go-processes?orgId=1&from=1573238522762&to=1573240322762&var-job=prometheus&var-interval=10m&panelId=1&fullscreen" }
        let(:description_with_unintegrated_embed) { "Some comment\n\nhttps://grafana.exp.com/d/xvAk4q0Wk/go-processes?orgId=1&from=1573238522762&to=1573240322762&var-job=prometheus&var-interval=10m&panelId=1&fullscreen" }
        let(:description_with_non_grafana_inline_metric) { "Some comment\n\n#{Gitlab::Routing.url_helpers.metrics_namespace_project_environment_url(*['foo', 'bar', 12])}" }

        shared_examples "zero count" do
          it "does not count the issue" do
            expect(subject).to eq(0)
          end
        end

        context 'with project grafana integration enabled' do
          before do
            create(:grafana_integration, project: project, enabled: true)
          end

          context 'with valid and invalid embeds' do
            before do
              # Valid
              create(:issue, project: project, description: description_with_embed)
              create(:issue, project: project, description: description_with_embed)
              # In-Valid
              create(:issue, project: project, description: description_with_unintegrated_embed)
              create(:issue, project: project, description: description_with_non_grafana_inline_metric)
              create(:issue, project: project, description: nil)
              create(:issue, project: project, description: '')
              create(:issue, project: project)
            end

            it 'counts only the issues with embeds' do
              expect(subject).to eq(2)
            end
          end
        end

        context 'with project grafana integration disabled' do
          before do
            create(:grafana_integration, project: project, enabled: false)
          end

          context 'with one issue having a grafana link in the description and one without' do
            before do
              create(:issue, project: project, description: description_with_embed)
              create(:issue, project: project)
            end

            it_behaves_like('zero count')
          end
        end

        context 'with an un-integrated project' do
          context 'with one issue having a grafana link in the description and one without' do
            before do
              create(:issue, project: project, description: description_with_embed)
              create(:issue, project: project)
            end

            it_behaves_like('zero count')
          end
        end
      end

      describe '#count' do
        let(:relation) { double(:relation) }

        it 'returns the count when counting succeeds' do
          allow(relation).to receive(:count).and_return(1)

          expect(described_class.count(relation, batch: false)).to eq(1)
        end

        it 'returns the fallback value when counting fails' do
          allow(relation).to receive(:count).and_raise(ActiveRecord::StatementInvalid.new(''))

          expect(described_class.count(relation, fallback: 15, batch: false)).to eq(15)
        end
      end

      describe '#distinct_count' do
        let(:relation) { double(:relation) }

        it 'returns the count when counting succeeds' do
          allow(relation).to receive(:distinct_count_by).and_return(1)

          expect(described_class.distinct_count(relation, batch: false)).to eq(1)
        end

        it 'returns the fallback value when counting fails' do
          allow(relation).to receive(:distinct_count_by).and_raise(ActiveRecord::StatementInvalid.new(''))

          expect(described_class.distinct_count(relation, fallback: 15, batch: false)).to eq(15)
        end
      end
    end
  end

  context 'when usage usage_ping_batch_counter is true' do
    before do
      stub_feature_flags(usage_ping_batch_counter: true)
    end

    it_behaves_like 'usage data execution'
  end

  context 'when usage usage_ping_batch_counter is false' do
    before do
      stub_feature_flags(usage_ping_batch_counter: false)
    end

    it_behaves_like 'usage data execution'
  end

  describe '#alt_usage_data' do
    it 'returns the fallback when it gets an error' do
      expect(described_class.alt_usage_data { raise StandardError } ).to eq(-1)
    end

    it 'returns the evaluated block when give' do
      expect(described_class.alt_usage_data { Gitlab::CurrentSettings.uuid } ).to eq(Gitlab::CurrentSettings.uuid)
    end

    it 'returns the value when given' do
      expect(described_class.alt_usage_data(1)).to eq 1
    end
  end
end