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

3 4 5
require 'spec_helper'

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

  describe '#data' do
10 11 12
    before do
      create(:jira_service, project: projects[0])
      create(:jira_service, project: projects[1])
13
      create(:jira_service, :jira_cloud_service, project: projects[2])
14 15 16 17
      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)
L
Logan King 已提交
18 19
      create(:project_error_tracking_setting, project: projects[0])
      create(:project_error_tracking_setting, project: projects[1], enabled: false)
20 21 22 23

      gcp_cluster = create(:cluster, :provided_by_gcp)
      create(:cluster, :provided_by_user)
      create(:cluster, :provided_by_user, :disabled)
24 25 26
      create(:cluster, :group)
      create(:cluster, :group, :disabled)
      create(:cluster, :group, :disabled)
27 28
      create(:clusters_applications_helm, :installed, cluster: gcp_cluster)
      create(:clusters_applications_ingress, :installed, cluster: gcp_cluster)
J
João Cunha 已提交
29
      create(:clusters_applications_cert_manager, :installed, cluster: gcp_cluster)
30 31
      create(:clusters_applications_prometheus, :installed, cluster: gcp_cluster)
      create(:clusters_applications_runner, :installed, cluster: gcp_cluster)
32
      create(:clusters_applications_knative, :installed, cluster: gcp_cluster)
33 34

      ProjectFeature.first.update_attribute('repository_access_level', 0)
35 36
    end

37
    subject { described_class.data }
38

39
    it 'gathers usage data' do
J
James Lopez 已提交
40
      expect(subject.keys).to include(*%i(
41 42 43
        active_user_count
        counts
        recorded_at
S
Sean McGivern 已提交
44
        edition
45
        version
46
        installation_type
47
        uuid
S
Sean McGivern 已提交
48
        hostname
49 50 51 52 53 54 55 56
        mattermost_enabled
        signup_enabled
        ldap_enabled
        gravatar_enabled
        omniauth_enabled
        reply_by_email_enabled
        container_registry_enabled
        gitlab_shared_runners_enabled
57 58
        gitlab_pages
        git
59
        gitaly
60
        database
61
        avg_cycle_analytics
R
Ryan Cobb 已提交
62 63
        influxdb_metrics_enabled
        prometheus_metrics_enabled
64
        cycle_analytics_views
65
        productivity_analytics_views
66
      ))
67

A
Alex Kalderimis 已提交
68
      expect(subject).to include(
M
Mark Chao 已提交
69 70 71
        snippet_create: a_kind_of(Integer),
        snippet_update: a_kind_of(Integer),
        snippet_comment: a_kind_of(Integer),
72
        merge_request_comment: a_kind_of(Integer),
73
        merge_request_create: a_kind_of(Integer),
74
        commit_comment: a_kind_of(Integer),
A
Alex Kalderimis 已提交
75 76 77 78 79
        wiki_pages_create: a_kind_of(Integer),
        wiki_pages_update: a_kind_of(Integer),
        wiki_pages_delete: a_kind_of(Integer),
        web_ide_views: a_kind_of(Integer),
        web_ide_commits: a_kind_of(Integer),
80
        web_ide_merge_requests: a_kind_of(Integer),
I
Igor 已提交
81
        navbar_searches: a_kind_of(Integer),
82
        cycle_analytics_views: a_kind_of(Integer),
83
        productivity_analytics_views: a_kind_of(Integer),
I
Igor 已提交
84
        source_code_pushes: a_kind_of(Integer)
A
Alex Kalderimis 已提交
85
      )
86 87
    end

88
    it 'gathers usage counts' do
89
      expected_keys = %i(
90
        assignee_lists
91 92
        boards
        ci_builds
93 94
        ci_internal_pipelines
        ci_external_pipelines
95 96
        ci_pipeline_config_auto_devops
        ci_pipeline_config_repository
97 98
        ci_runners
        ci_triggers
99
        ci_pipeline_schedules
100 101
        auto_devops_enabled
        auto_devops_disabled
102 103
        deploy_keys
        deployments
104 105
        successful_deployments
        failed_deployments
106
        environments
107 108
        clusters
        clusters_enabled
109 110
        project_clusters_enabled
        group_clusters_enabled
111
        clusters_disabled
112 113
        project_clusters_disabled
        group_clusters_disabled
114 115 116 117
        clusters_platforms_gke
        clusters_platforms_user
        clusters_applications_helm
        clusters_applications_ingress
A
Amit Rathi 已提交
118
        clusters_applications_cert_managers
119 120
        clusters_applications_prometheus
        clusters_applications_runner
121
        clusters_applications_knative
122
        in_review_folder
123 124 125
        groups
        issues
        keys
126
        label_lists
127 128 129
        labels
        lfs_objects
        merge_requests
130
        milestone_lists
131 132
        milestones
        notes
133
        pool_repositories
134
        projects
135
        projects_imported_from_github
136
        projects_jira_active
137 138
        projects_jira_server_active
        projects_jira_cloud_active
139 140
        projects_slack_notifications_active
        projects_slack_slash_active
141
        projects_prometheus_active
142
        projects_with_repositories_enabled
L
Logan King 已提交
143
        projects_with_error_tracking_enabled
144 145 146
        pages_domains
        protected_branches
        releases
147
        remote_mirrors
148
        snippets
149
        suggestions
150
        todos
R
Robert Speicher 已提交
151
        uploads
152
        web_hooks
153
        user_preferences
154 155 156 157 158 159 160 161
      )

      count_data = subject[:counts]

      expect(count_data[:boards]).to eq(1)
      expect(count_data[:projects]).to eq(3)
      expect(count_data.keys).to include(*expected_keys)
      expect(expected_keys - count_data.keys).to be_empty
162
    end
163 164 165 166 167 168

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

      expect(count_data[:projects]).to eq(3)
      expect(count_data[:projects_prometheus_active]).to eq(1)
169 170 171
      expect(count_data[:projects_jira_active]).to eq(3)
      expect(count_data[:projects_jira_server_active]).to eq(2)
      expect(count_data[:projects_jira_cloud_active]).to eq(1)
172 173
      expect(count_data[:projects_slack_notifications_active]).to eq(2)
      expect(count_data[:projects_slack_slash_active]).to eq(1)
174
      expect(count_data[:projects_with_repositories_enabled]).to eq(2)
L
Logan King 已提交
175
      expect(count_data[:projects_with_error_tracking_enabled]).to eq(1)
176

177 178 179 180 181 182 183
      expect(count_data[:clusters_enabled]).to eq(7)
      expect(count_data[:project_clusters_enabled]).to eq(6)
      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)
184 185 186 187
      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 已提交
188
      expect(count_data[:clusters_applications_cert_managers]).to eq(1)
189 190
      expect(count_data[:clusters_applications_prometheus]).to eq(1)
      expect(count_data[:clusters_applications_runner]).to eq(1)
191
      expect(count_data[:clusters_applications_knative]).to eq(1)
192
    end
193 194 195 196 197 198 199

    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
200 201
  end

A
Alex Kalderimis 已提交
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
  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 do
        is_expected
          .to all(be_a Hash)
          .and all(have_attributes(keys: all(be_a Symbol), values: all(be_a Integer)))
      end
    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

224 225 226 227
  describe '#features_usage_data_ce' do
    subject { described_class.features_usage_data_ce }

    it 'gathers feature usage data' do
228 229 230 231
      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?)
232
      expect(subject[:omniauth_enabled]).to eq(Gitlab::Auth.omniauth_enabled?)
233 234 235
      expect(subject[:reply_by_email_enabled]).to eq(Gitlab::IncomingEmail.enabled?)
      expect(subject[:container_registry_enabled]).to eq(Gitlab.config.registry.enabled)
      expect(subject[:gitlab_shared_runners_enabled]).to eq(Gitlab.config.gitlab_ci.shared_runners_enabled)
236 237 238 239 240 241 242 243 244 245 246 247
    end
  end

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

    it 'gathers components usage data' do
      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)
248 249 250 251
      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)
252 253 254
    end
  end

255
  describe '#license_usage_data' do
256
    subject { described_class.license_usage_data }
257

258
    it 'gathers license data' do
259
      expect(subject[:uuid]).to eq(Gitlab::CurrentSettings.uuid)
260
      expect(subject[:version]).to eq(Gitlab::VERSION)
261
      expect(subject[:installation_type]).to eq('gitlab-development-kit')
262 263 264 265
      expect(subject[:active_user_count]).to eq(User.active.count)
      expect(subject[:recorded_at]).to be_a(Time)
    end
  end
266 267 268 269 270 271 272 273 274 275

  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

276 277 278 279 280 281
    it 'returns the count for count_by when provided' do
      allow(relation).to receive(:count).with(:creator_id).and_return(2)

      expect(described_class.count(relation, count_by: :creator_id)).to eq(2)
    end

282 283 284 285 286 287
    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
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

  describe '#approximate_counts' do
    it 'gets approximate counts for selected models' do
      create(:label)

      expect(Gitlab::Database::Count).to receive(:approximate_counts)
        .with(described_class::APPROXIMATE_COUNT_MODELS).once.and_call_original

      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

    it 'returns default values if counts can not be retrieved' do
      described_class::APPROXIMATE_COUNT_MODELS.map do |model|
        model.name.underscore.pluralize.to_sym
      end

      expect(Gitlab::Database::Count).to receive(:approximate_counts)
        .and_return({})

      expect(described_class.approximate_counts.values.uniq).to eq([-1])
    end
  end
313
end