Welcome to mirror list, hosted at ThFree Co, Russian Federation.

metrics_spec.rb « import « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1a988af0dbd51b971ccc0cf5775084578af09b29 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Import::Metrics, :aggregate_failures do
  let(:importer) { :test_importer }
  let(:project) { build(:project, id: non_existing_record_id, created_at: Time.current) }
  let(:histogram) { double(:histogram) }
  let(:counter) { double(:counter) }

  subject { described_class.new(importer, project) }

  before do
    allow(counter).to receive(:increment)
    allow(histogram).to receive(:observe)
  end

  describe '#track_start_import' do
    context 'when project is not a github import' do
      it 'does not emit importer metrics' do
        expect(subject).not_to receive(:track_usage_event)

        subject.track_start_import
      end
    end

    context 'when project is a github import' do
      before do
        project.import_type = 'github'
      end

      it 'emits importer metrics' do
        expect(subject).to receive(:track_usage_event).with(:github_import_project_start, project.id)

        subject.track_start_import
      end
    end
  end

  describe '#track_failed_import' do
    context 'when project is not a github import' do
      it 'does not emit importer metrics' do
        expect(subject).not_to receive(:track_usage_event)
        expect_no_snowplow_event(
          category: :test_importer,
          action: 'create',
          label: 'github_import_project_state',
          project: project,
          extra: { import_type: 'github', state: 'failed' }
        )

        subject.track_failed_import
      end
    end

    context 'when project is a github import' do
      before do
        project.import_type = 'github'
        allow(project).to receive(:import_status).and_return('failed')
      end

      it 'emits importer metrics' do
        expect(subject).to receive(:track_usage_event).with(:github_import_project_failure, project.id)

        subject.track_failed_import

        expect_snowplow_event(
          category: :test_importer,
          action: 'create',
          label: 'github_import_project_state',
          project: project,
          extra: { import_type: 'github', state: 'failed' }
        )
      end
    end
  end

  describe '#track_finished_import' do
    context 'when project is a github import' do
      before do
        project.import_type = 'github'
        allow(Gitlab::Metrics).to receive(:counter) { counter }
        allow(Gitlab::Metrics).to receive(:histogram) { histogram }
        allow(project).to receive(:beautified_import_status_name).and_return('completed')
      end

      it 'emits importer metrics' do
        expect(Gitlab::Metrics).to receive(:counter).with(
          :test_importer_imported_projects_total,
          'The number of imported projects'
        )

        expect(Gitlab::Metrics).to receive(:histogram).with(
          :test_importer_total_duration_seconds,
          'Total time spent importing projects, in seconds',
          {},
          described_class::IMPORT_DURATION_BUCKETS
        )

        expect(counter).to receive(:increment)

        subject.track_finished_import

        expect_snowplow_event(
          category: :test_importer,
          action: 'create',
          label: 'github_import_project_state',
          project: project,
          extra: { import_type: 'github', state: 'completed' }
        )

        expect(subject.duration).not_to be_nil
      end

      context 'when import is partially completed' do
        before do
          allow(project).to receive(:beautified_import_status_name).and_return('partially completed')
        end

        it 'emits snowplow metrics' do
          expect(subject).to receive(:track_usage_event).with(:github_import_project_partially_completed, project.id)

          subject.track_finished_import

          expect_snowplow_event(
            category: :test_importer,
            action: 'create',
            label: 'github_import_project_state',
            project: project,
            extra: { import_type: 'github', state: 'partially completed' }
          )
        end
      end
    end

    context 'when project is not a github import' do
      it 'does not emit importer metrics' do
        expect(subject).not_to receive(:track_usage_event)

        subject.track_finished_import

        expect_no_snowplow_event(
          category: :test_importer,
          action: 'create',
          label: 'github_import_project_state',
          project: project,
          extra: { import_type: 'github', state: 'completed' }
        )
      end
    end
  end

  describe '#track_cancelled_import' do
    context 'when project is not a github import' do
      it 'does not emit importer metrics' do
        expect(subject).not_to receive(:track_usage_event)
        expect_no_snowplow_event(
          category: :test_importer,
          action: 'create',
          label: 'github_import_project_state',
          project: project,
          extra: { import_type: 'github', state: 'canceled' }
        )

        subject.track_canceled_import
      end
    end

    context 'when project is a github import' do
      before do
        project.import_type = 'github'
        allow(project).to receive(:import_status).and_return('canceled')
      end

      it 'emits importer metrics' do
        expect(subject).to receive(:track_usage_event).with(:github_import_project_cancelled, project.id)

        subject.track_canceled_import

        expect_snowplow_event(
          category: :test_importer,
          action: 'create',
          label: 'github_import_project_state',
          project: project,
          extra: { import_type: 'github', state: 'canceled' }
        )
      end
    end
  end

  describe '#issues_counter' do
    it 'creates a counter for issues' do
      expect(Gitlab::Metrics).to receive(:counter).with(
        :test_importer_imported_issues_total,
        'The number of imported issues'
      )

      subject.issues_counter
    end
  end

  describe '#merge_requests_counter' do
    it 'creates a counter for issues' do
      expect(Gitlab::Metrics).to receive(:counter).with(
        :test_importer_imported_merge_requests_total,
        'The number of imported merge (pull) requests'
      )

      subject.merge_requests_counter
    end
  end
end