usage_data_spec.rb 8.6 KB
Newer Older
1 2 3
require 'spec_helper'

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

  describe '#data' do
8 9 10
    before do
      create(:jira_service, project: projects[0])
      create(:jira_service, project: projects[1])
11
      create(:jira_cloud_service, project: projects[2])
12 13 14 15
      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)
16 17 18 19

      gcp_cluster = create(:cluster, :provided_by_gcp)
      create(:cluster, :provided_by_user)
      create(:cluster, :provided_by_user, :disabled)
20 21 22
      create(:cluster, :group)
      create(:cluster, :group, :disabled)
      create(:cluster, :group, :disabled)
23 24
      create(:clusters_applications_helm, :installed, cluster: gcp_cluster)
      create(:clusters_applications_ingress, :installed, cluster: gcp_cluster)
A
Amit Rathi 已提交
25
      create(:clusters_applications_cert_managers, :installed, cluster: gcp_cluster)
26 27
      create(:clusters_applications_prometheus, :installed, cluster: gcp_cluster)
      create(:clusters_applications_runner, :installed, cluster: gcp_cluster)
28
      create(:clusters_applications_knative, :installed, cluster: gcp_cluster)
29 30

      ProjectFeature.first.update_attribute('repository_access_level', 0)
31 32
    end

33
    subject { described_class.data }
34 35 36 37 38 39

    it "gathers usage data" do
      expect(subject.keys).to match_array(%i(
        active_user_count
        counts
        recorded_at
S
Sean McGivern 已提交
40
        edition
41
        version
42
        installation_type
43
        uuid
S
Sean McGivern 已提交
44
        hostname
45 46 47 48 49 50 51 52
        mattermost_enabled
        signup_enabled
        ldap_enabled
        gravatar_enabled
        omniauth_enabled
        reply_by_email_enabled
        container_registry_enabled
        gitlab_shared_runners_enabled
53 54 55
        gitlab_pages
        git
        database
56
        avg_cycle_analytics
T
Tiago Botelho 已提交
57
        web_ide_commits
58 59 60 61 62 63 64
      ))
    end

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

      expect(count_data[:boards]).to eq(1)
65
      expect(count_data[:projects]).to eq(3)
66 67

      expect(count_data.keys).to match_array(%i(
68
        assignee_lists
69 70
        boards
        ci_builds
71 72
        ci_internal_pipelines
        ci_external_pipelines
73 74
        ci_pipeline_config_auto_devops
        ci_pipeline_config_repository
75 76
        ci_runners
        ci_triggers
77
        ci_pipeline_schedules
78 79
        auto_devops_enabled
        auto_devops_disabled
80 81 82
        deploy_keys
        deployments
        environments
83 84
        clusters
        clusters_enabled
85 86
        project_clusters_enabled
        group_clusters_enabled
87
        clusters_disabled
88 89
        project_clusters_disabled
        group_clusters_disabled
90 91 92 93
        clusters_platforms_gke
        clusters_platforms_user
        clusters_applications_helm
        clusters_applications_ingress
A
Amit Rathi 已提交
94
        clusters_applications_cert_managers
95 96
        clusters_applications_prometheus
        clusters_applications_runner
97
        clusters_applications_knative
98
        in_review_folder
99 100 101
        groups
        issues
        keys
102
        label_lists
103 104 105
        labels
        lfs_objects
        merge_requests
106
        milestone_lists
107 108 109
        milestones
        notes
        projects
110
        projects_imported_from_github
111
        projects_jira_active
112 113
        projects_jira_server_active
        projects_jira_cloud_active
114 115
        projects_slack_notifications_active
        projects_slack_slash_active
116
        projects_prometheus_active
117
        projects_with_repositories_enabled
118 119 120
        pages_domains
        protected_branches
        releases
121
        remote_mirrors
122
        snippets
123
        suggestions
124
        todos
R
Robert Speicher 已提交
125
        uploads
126 127 128
        web_hooks
      ))
    end
129 130 131 132 133 134

    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)
135 136 137
      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)
138 139
      expect(count_data[:projects_slack_notifications_active]).to eq(2)
      expect(count_data[:projects_slack_slash_active]).to eq(1)
140
      expect(count_data[:projects_with_repositories_enabled]).to eq(2)
141

142 143 144 145 146 147 148
      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)
149 150 151 152
      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 已提交
153
      expect(count_data[:clusters_applications_cert_managers]).to eq(1)
154 155
      expect(count_data[:clusters_applications_prometheus]).to eq(1)
      expect(count_data[:clusters_applications_runner]).to eq(1)
156
      expect(count_data[:clusters_applications_knative]).to eq(1)
157
    end
158 159 160 161 162 163 164

    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
165 166
  end

167 168 169 170
  describe '#features_usage_data_ce' do
    subject { described_class.features_usage_data_ce }

    it 'gathers feature usage data' do
171 172 173 174
      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?)
175
      expect(subject[:omniauth_enabled]).to eq(Gitlab::Auth.omniauth_enabled?)
176 177 178
      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)
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
    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)
    end
  end

194
  describe '#license_usage_data' do
195
    subject { described_class.license_usage_data }
196 197

    it "gathers license data" do
198
      expect(subject[:uuid]).to eq(Gitlab::CurrentSettings.uuid)
199
      expect(subject[:version]).to eq(Gitlab::VERSION)
200
      expect(subject[:installation_type]).to eq(Gitlab::INSTALLATION_TYPE)
201 202 203 204
      expect(subject[:active_user_count]).to eq(User.active.count)
      expect(subject[:recorded_at]).to be_a(Time)
    end
  end
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220

  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
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

  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
246
end