usage_data_spec.rb 14.6 KB
Newer Older
1 2
# frozen_string_literal: true

3 4 5
require 'spec_helper'

describe Gitlab::UsageData do
6
  let(:projects) { create_list(:project, 4) }
7
  let!(:board) { create(:board, project: projects[0]) }
8 9

  describe '#data' do
10 11
    before do
      create(:jira_service, project: projects[0])
12
      create(:jira_service, :without_properties_callback, project: projects[1])
13
      create(:jira_service, :jira_cloud_service, project: projects[2])
14 15
      create(:jira_service, :without_properties_callback, project: projects[3],
             properties: { url: 'https://mysite.atlassian.net' })
16 17 18 19
      create(:prometheus_service, project: projects[1])
      create(:service, project: projects[0], type: 'SlackSlashCommandsService', active: true)
      create(:service, project: projects[1], type: 'SlackService', active: true)
      create(:service, project: projects[2], type: 'SlackService', active: true)
20
      create(:service, project: projects[2], type: 'MattermostService', active: false)
21
      create(:service, project: projects[2], type: 'MattermostService', active: true, instance: true)
22
      create(:service, project: projects[2], type: 'CustomIssueTrackerService', active: true)
L
Logan King 已提交
23 24
      create(:project_error_tracking_setting, project: projects[0])
      create(:project_error_tracking_setting, project: projects[1], enabled: false)
25 26 27 28 29
      create_list(:issue, 4, project: projects[0])
      create(:zoom_meeting, project: projects[0], issue: projects[0].issues[0], issue_status: :added)
      create_list(:zoom_meeting, 2, project: projects[0], issue: projects[0].issues[1], issue_status: :removed)
      create(:zoom_meeting, project: projects[0], issue: projects[0].issues[2], issue_status: :added)
      create_list(:zoom_meeting, 2, project: projects[0], issue: projects[0].issues[2], issue_status: :removed)
30
      create(:sentry_issue, issue: projects[0].issues[0])
31 32 33 34 35

      # Enabled clusters
      gcp_cluster = create(:cluster_provider_gcp, :created).cluster
      create(:cluster_provider_aws, :created)
      create(:cluster_platform_kubernetes)
36
      create(:cluster, :group)
37 38 39

      # Disabled clusters
      create(:cluster, :disabled)
40 41
      create(:cluster, :group, :disabled)
      create(:cluster, :group, :disabled)
42 43

      # Applications
44 45
      create(:clusters_applications_helm, :installed, cluster: gcp_cluster)
      create(:clusters_applications_ingress, :installed, cluster: gcp_cluster)
J
João Cunha 已提交
46
      create(:clusters_applications_cert_manager, :installed, cluster: gcp_cluster)
47
      create(:clusters_applications_prometheus, :installed, cluster: gcp_cluster)
48
      create(:clusters_applications_crossplane, :installed, cluster: gcp_cluster)
49
      create(:clusters_applications_runner, :installed, cluster: gcp_cluster)
50
      create(:clusters_applications_knative, :installed, cluster: gcp_cluster)
51
      create(:clusters_applications_elastic_stack, :installed, cluster: gcp_cluster)
52
      create(:clusters_applications_jupyter, :installed, cluster: gcp_cluster)
53

54 55 56 57
      create(:grafana_integration, project: projects[0], enabled: true)
      create(:grafana_integration, project: projects[1], enabled: true)
      create(:grafana_integration, project: projects[2], enabled: false)

58 59
      allow(Gitlab::GrafanaEmbedUsageData).to receive(:issue_count).and_return(2)

60
      ProjectFeature.first.update_attribute('repository_access_level', 0)
61 62
    end

63
    subject { described_class.data }
64

65
    it 'gathers usage data', :aggregate_failures do
J
James Lopez 已提交
66
      expect(subject.keys).to include(*%i(
67 68 69
        active_user_count
        counts
        recorded_at
S
Sean McGivern 已提交
70
        edition
71
        version
72
        installation_type
73
        uuid
S
Sean McGivern 已提交
74
        hostname
75 76 77 78 79 80 81
        mattermost_enabled
        signup_enabled
        ldap_enabled
        gravatar_enabled
        omniauth_enabled
        reply_by_email_enabled
        container_registry_enabled
82
        dependency_proxy_enabled
83
        gitlab_shared_runners_enabled
84 85
        gitlab_pages
        git
86
        gitaly
87
        database
88
        avg_cycle_analytics
R
Ryan Cobb 已提交
89 90
        influxdb_metrics_enabled
        prometheus_metrics_enabled
91
        web_ide_clientside_preview_enabled
92
        ingress_modsecurity_enabled
93
      ))
94 95
    end

96
    it 'gathers usage counts' do
97 98 99 100 101 102 103 104 105 106 107 108 109
      smau_keys = %i(
        snippet_create
        snippet_update
        snippet_comment
        merge_request_comment
        merge_request_create
        commit_comment
        wiki_pages_create
        wiki_pages_update
        wiki_pages_delete
        web_ide_views
        web_ide_commits
        web_ide_merge_requests
110
        web_ide_previews
111 112 113 114 115 116
        navbar_searches
        cycle_analytics_views
        productivity_analytics_views
        source_code_pushes
      )

117
      expected_keys = %i(
118
        assignee_lists
119 120
        boards
        ci_builds
121 122
        ci_internal_pipelines
        ci_external_pipelines
123 124
        ci_pipeline_config_auto_devops
        ci_pipeline_config_repository
125 126
        ci_runners
        ci_triggers
127
        ci_pipeline_schedules
128 129
        auto_devops_enabled
        auto_devops_disabled
130 131
        deploy_keys
        deployments
132 133
        successful_deployments
        failed_deployments
134
        environments
135 136
        clusters
        clusters_enabled
137 138
        project_clusters_enabled
        group_clusters_enabled
139
        clusters_disabled
140 141
        project_clusters_disabled
        group_clusters_disabled
142
        clusters_platforms_eks
143 144 145 146
        clusters_platforms_gke
        clusters_platforms_user
        clusters_applications_helm
        clusters_applications_ingress
A
Amit Rathi 已提交
147
        clusters_applications_cert_managers
148
        clusters_applications_prometheus
149
        clusters_applications_crossplane
150
        clusters_applications_runner
151
        clusters_applications_knative
152
        clusters_applications_elastic_stack
153
        clusters_applications_jupyter
154
        in_review_folder
155
        grafana_integrated_projects
156 157
        groups
        issues
158
        issues_created_from_gitlab_error_tracking_ui
159 160
        issues_with_associated_zoom_link
        issues_using_zoom_quick_actions
161
        issues_with_embedded_grafana_charts_approx
162
        keys
163
        label_lists
164 165 166
        labels
        lfs_objects
        merge_requests
167
        milestone_lists
168 169
        milestones
        notes
170
        pool_repositories
171
        projects
172
        projects_imported_from_github
173
        projects_asana_active
174
        projects_jira_active
175 176
        projects_jira_server_active
        projects_jira_cloud_active
177 178
        projects_slack_notifications_active
        projects_slack_slash_active
179 180
        projects_slack_active
        projects_slack_slash_commands_active
181 182
        projects_custom_issue_tracker_active
        projects_mattermost_active
183
        projects_prometheus_active
184
        projects_with_repositories_enabled
L
Logan King 已提交
185
        projects_with_error_tracking_enabled
186 187 188
        pages_domains
        protected_branches
        releases
189
        remote_mirrors
190
        snippets
191
        suggestions
192
        todos
R
Robert Speicher 已提交
193
        uploads
194
        web_hooks
195
      ).push(*smau_keys)
196 197 198 199

      count_data = subject[:counts]

      expect(count_data[:boards]).to eq(1)
200
      expect(count_data[:projects]).to eq(4)
201
      expect(count_data.values_at(*smau_keys)).to all(be_an(Integer))
202 203
      expect(count_data.keys).to include(*expected_keys)
      expect(expected_keys - count_data.keys).to be_empty
204
    end
205

206
    it 'gathers projects data correctly', :aggregate_failures do
207 208
      count_data = subject[:counts]

209
      expect(count_data[:projects]).to eq(4)
210
      expect(count_data[:projects_asana_active]).to eq(0)
211
      expect(count_data[:projects_prometheus_active]).to eq(1)
212
      expect(count_data[:projects_jira_active]).to eq(4)
213
      expect(count_data[:projects_jira_server_active]).to eq(2)
214
      expect(count_data[:projects_jira_cloud_active]).to eq(2)
215 216
      expect(count_data[:projects_slack_notifications_active]).to eq(2)
      expect(count_data[:projects_slack_slash_active]).to eq(1)
217 218
      expect(count_data[:projects_slack_active]).to eq(2)
      expect(count_data[:projects_slack_slash_commands_active]).to eq(1)
219
      expect(count_data[:projects_custom_issue_tracker_active]).to eq(1)
220
      expect(count_data[:projects_mattermost_active]).to eq(0)
221
      expect(count_data[:projects_with_repositories_enabled]).to eq(3)
L
Logan King 已提交
222
      expect(count_data[:projects_with_error_tracking_enabled]).to eq(1)
223
      expect(count_data[:issues_created_from_gitlab_error_tracking_ui]).to eq(1)
224 225
      expect(count_data[:issues_with_associated_zoom_link]).to eq(2)
      expect(count_data[:issues_using_zoom_quick_actions]).to eq(3)
226
      expect(count_data[:issues_with_embedded_grafana_charts_approx]).to eq(2)
227

228 229
      expect(count_data[:clusters_enabled]).to eq(4)
      expect(count_data[:project_clusters_enabled]).to eq(3)
230 231 232 233 234
      expect(count_data[:group_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(2)
      expect(count_data[:group_clusters_enabled]).to eq(1)
235
      expect(count_data[:clusters_platforms_eks]).to eq(1)
236 237 238 239
      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)
A
Amit Rathi 已提交
240
      expect(count_data[:clusters_applications_cert_managers]).to eq(1)
241
      expect(count_data[:clusters_applications_crossplane]).to eq(1)
242 243
      expect(count_data[:clusters_applications_prometheus]).to eq(1)
      expect(count_data[:clusters_applications_runner]).to eq(1)
244
      expect(count_data[:clusters_applications_knative]).to eq(1)
245
      expect(count_data[:clusters_applications_elastic_stack]).to eq(1)
246
      expect(count_data[:grafana_integrated_projects]).to eq(2)
247
      expect(count_data[:clusters_applications_jupyter]).to eq(1)
248
    end
249 250 251 252 253 254 255

    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
256 257
  end

A
Alex Kalderimis 已提交
258 259 260 261 262 263 264 265
  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) }

266 267
      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))) }
A
Alex Kalderimis 已提交
268 269 270 271 272 273 274 275 276
    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

277 278 279
  describe '#features_usage_data_ce' do
    subject { described_class.features_usage_data_ce }

280
    it 'gathers feature usage data', :aggregate_failures do
281 282 283 284
      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?)
285
      expect(subject[:omniauth_enabled]).to eq(Gitlab::Auth.omniauth_enabled?)
286 287
      expect(subject[:reply_by_email_enabled]).to eq(Gitlab::IncomingEmail.enabled?)
      expect(subject[:container_registry_enabled]).to eq(Gitlab.config.registry.enabled)
288
      expect(subject[:dependency_proxy_enabled]).to eq(Gitlab.config.dependency_proxy.enabled)
289
      expect(subject[:gitlab_shared_runners_enabled]).to eq(Gitlab.config.gitlab_ci.shared_runners_enabled)
290
      expect(subject[:web_ide_clientside_preview_enabled]).to eq(Gitlab::CurrentSettings.web_ide_clientside_preview_enabled?)
291 292 293 294 295 296
    end
  end

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

297
    it 'gathers components usage data', :aggregate_failures do
298 299 300 301 302
      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)
303 304 305 306
      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)
307 308 309
    end
  end

310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
  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

328
  describe '#license_usage_data' do
329
    subject { described_class.license_usage_data }
330

331
    it 'gathers license data', :aggregate_failures do
332
      expect(subject[:uuid]).to eq(Gitlab::CurrentSettings.uuid)
333
      expect(subject[:version]).to eq(Gitlab::VERSION)
334
      expect(subject[:installation_type]).to eq('gitlab-development-kit')
335 336 337 338
      expect(subject[:active_user_count]).to eq(User.active.count)
      expect(subject[:recorded_at]).to be_a(Time)
    end
  end
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354

  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)).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)).to eq(15)
    end
  end
355 356

  describe '#approximate_counts' do
357
    it 'gets approximate counts for selected models', :aggregate_failures do
358 359 360
      create(:label)

      expect(Gitlab::Database::Count).to receive(:approximate_counts)
361
                                           .with(described_class::APPROXIMATE_COUNT_MODELS).once.and_call_original
362 363 364 365 366 367 368

      counts = described_class.approximate_counts.values

      expect(counts.count).to eq(described_class::APPROXIMATE_COUNT_MODELS.count)
      expect(counts.any? { |count| count < 0 }).to be_falsey
    end

369
    it 'returns default values if counts can not be retrieved', :aggregate_failures do
370 371 372 373
      described_class::APPROXIMATE_COUNT_MODELS.map do |model|
        model.name.underscore.pluralize.to_sym
      end

374
      expect(Gitlab::Database::Count).to receive(:approximate_counts).and_return({})
375 376 377
      expect(described_class.approximate_counts.values.uniq).to eq([-1])
    end
  end
378
end