summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/background_migration/migrate_events_to_push_event_payloads_spec.rb
blob: 7351d45336a21430edfd3282a9f61e2297f52985 (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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
require 'spec_helper'

describe Gitlab::BackgroundMigration::MigrateEventsToPushEventPayloads::Event, :migration, schema: 20170608152748 do
  describe '#commit_title' do
    it 'returns nil when there are no commits' do
      expect(described_class.new.commit_title).to be_nil
    end

    it 'returns nil when there are commits without commit messages' do
      event = described_class.new

      allow(event).to receive(:commits).and_return([{ id: '123' }])

      expect(event.commit_title).to be_nil
    end

    it 'returns the commit message when it is less than 70 characters long' do
      event = described_class.new

      allow(event).to receive(:commits).and_return([{ message: 'Hello world' }])

      expect(event.commit_title).to eq('Hello world')
    end

    it 'returns the first line of a commit message if multiple lines are present' do
      event = described_class.new

      allow(event).to receive(:commits).and_return([{ message: "Hello\n\nworld" }])

      expect(event.commit_title).to eq('Hello')
    end

    it 'truncates the commit to 70 characters when it is too long' do
      event = described_class.new

      allow(event).to receive(:commits).and_return([{ message: 'a' * 100 }])

      expect(event.commit_title).to eq(('a' * 67) + '...')
    end
  end

  describe '#commit_from_sha' do
    it 'returns nil when pushing to a new ref' do
      event = described_class.new

      allow(event).to receive(:create?).and_return(true)

      expect(event.commit_from_sha).to be_nil
    end

    it 'returns the ID of the first commit when pushing to an existing ref' do
      event = described_class.new

      allow(event).to receive(:create?).and_return(false)
      allow(event).to receive(:data).and_return(before: '123')

      expect(event.commit_from_sha).to eq('123')
    end
  end

  describe '#commit_to_sha' do
    it 'returns nil when removing an existing ref' do
      event = described_class.new

      allow(event).to receive(:remove?).and_return(true)

      expect(event.commit_to_sha).to be_nil
    end

    it 'returns the ID of the last commit when pushing to an existing ref' do
      event = described_class.new

      allow(event).to receive(:remove?).and_return(false)
      allow(event).to receive(:data).and_return(after: '123')

      expect(event.commit_to_sha).to eq('123')
    end
  end

  describe '#data' do
    it 'returns the deserialized data' do
      event = described_class.new(data: { before: '123' })

      expect(event.data).to eq(before: '123')
    end

    it 'returns an empty hash when no data is present' do
      event = described_class.new

      expect(event.data).to eq({})
    end
  end

  describe '#commits' do
    it 'returns an Array of commits' do
      event = described_class.new(data: { commits: [{ id: '123' }] })

      expect(event.commits).to eq([{ id: '123' }])
    end

    it 'returns an empty array when no data is present' do
      event = described_class.new

      expect(event.commits).to eq([])
    end
  end

  describe '#commit_count' do
    it 'returns the number of commits' do
      event = described_class.new(data: { total_commits_count: 2 })

      expect(event.commit_count).to eq(2)
    end

    it 'returns 0 when no data is present' do
      event = described_class.new

      expect(event.commit_count).to eq(0)
    end
  end

  describe '#ref' do
    it 'returns the name of the ref' do
      event = described_class.new(data: { ref: 'refs/heads/master' })

      expect(event.ref).to eq('refs/heads/master')
    end
  end

  describe '#trimmed_ref_name' do
    it 'returns the trimmed ref name for a branch' do
      event = described_class.new(data: { ref: 'refs/heads/master' })

      expect(event.trimmed_ref_name).to eq('master')
    end

    it 'returns the trimmed ref name for a tag' do
      event = described_class.new(data: { ref: 'refs/tags/v1.2' })

      expect(event.trimmed_ref_name).to eq('v1.2')
    end
  end

  describe '#create?' do
    it 'returns true when creating a new ref' do
      event = described_class.new(data: { before: described_class::BLANK_REF })

      expect(event.create?).to eq(true)
    end

    it 'returns false when pushing to an existing ref' do
      event = described_class.new(data: { before: '123' })

      expect(event.create?).to eq(false)
    end
  end

  describe '#remove?' do
    it 'returns true when removing an existing ref' do
      event = described_class.new(data: { after: described_class::BLANK_REF })

      expect(event.remove?).to eq(true)
    end

    it 'returns false when pushing to an existing ref' do
      event = described_class.new(data: { after: '123' })

      expect(event.remove?).to eq(false)
    end
  end

  describe '#push_action' do
    let(:event) { described_class.new }

    it 'returns :created when creating a new ref' do
      allow(event).to receive(:create?).and_return(true)

      expect(event.push_action).to eq(:created)
    end

    it 'returns :removed when removing an existing ref' do
      allow(event).to receive(:create?).and_return(false)
      allow(event).to receive(:remove?).and_return(true)

      expect(event.push_action).to eq(:removed)
    end

    it 'returns :pushed when pushing to an existing ref' do
      allow(event).to receive(:create?).and_return(false)
      allow(event).to receive(:remove?).and_return(false)

      expect(event.push_action).to eq(:pushed)
    end
  end

  describe '#ref_type' do
    let(:event) { described_class.new }

    it 'returns :tag for a tag' do
      allow(event).to receive(:ref).and_return('refs/tags/1.2')

      expect(event.ref_type).to eq(:tag)
    end

    it 'returns :branch for a branch' do
      allow(event).to receive(:ref).and_return('refs/heads/1.2')

      expect(event.ref_type).to eq(:branch)
    end
  end
end

##
# The background migration relies on a temporary table, hence we're migrating
# to a specific version of the database where said table is still present.
#
describe Gitlab::BackgroundMigration::MigrateEventsToPushEventPayloads, :migration, schema: 20170825154015 do
  let(:user_class) do
    Class.new(ActiveRecord::Base) do
      self.table_name = 'users'
    end
  end

  let(:migration) { described_class.new }
  let(:user_class) { table(:users) }
  let(:author) { build(:user).becomes(user_class).tap(&:save!).becomes(User) }
  let(:namespace) { create(:namespace, owner: author) }
  let(:projects) { table(:projects) }
  let(:project) { projects.create(namespace_id: namespace.id, creator_id: author.id) }

  # We can not rely on FactoryBot as the state of Event may change in ways that
  # the background migration does not expect, hence we use the Event class of
  # the migration itself.
  def create_push_event(project, author, data = nil)
    klass = Gitlab::BackgroundMigration::MigrateEventsToPushEventPayloads::Event

    klass.create!(
      action: klass::PUSHED,
      project_id: project.id,
      author_id: author.id,
      data: data
    )
  end

  describe '#perform' do
    it 'returns if data should not be migrated' do
      allow(migration).to receive(:migrate?).and_return(false)

      expect(migration).not_to receive(:find_events)

      migration.perform(1, 10)
    end

    it 'migrates the range of events if data is to be migrated' do
      event1 = create_push_event(project, author, { commits: [] })
      event2 = create_push_event(project, author, { commits: [] })

      allow(migration).to receive(:migrate?).and_return(true)

      expect(migration).to receive(:process_event).twice

      migration.perform(event1.id, event2.id)
    end
  end

  describe '#process_event' do
    it 'processes a regular event' do
      event = double(:event, push_event?: false)

      expect(migration).to receive(:replicate_event)
      expect(migration).not_to receive(:create_push_event_payload)

      migration.process_event(event)
    end

    it 'processes a push event' do
      event = double(:event, push_event?: true)

      expect(migration).to receive(:replicate_event)
      expect(migration).to receive(:create_push_event_payload)

      migration.process_event(event)
    end
  end

  describe '#replicate_event' do
    it 'replicates the event to the "events_for_migration" table' do
      event = create_push_event(
        project,
        author,
        data: { commits: [] },
        title: 'bla'
      )

      attributes = event
        .attributes.with_indifferent_access.except(:title, :data)

      expect(described_class::EventForMigration)
        .to receive(:create!)
        .with(attributes)

      migration.replicate_event(event)
    end
  end

  describe '#create_push_event_payload' do
    let(:push_data) do
      {
        commits: [],
        ref: 'refs/heads/master',
        before: '156e0e9adc587a383a7eeb5b21ddecb9044768a8',
        after: '0' * 40,
        total_commits_count: 1
      }
    end

    let(:event) do
      create_push_event(project, author, push_data)
    end

    before do
      # The foreign key in push_event_payloads at this point points to the
      # "events_for_migration" table so we need to make sure a row exists in
      # said table.
      migration.replicate_event(event)
    end

    it 'creates a push event payload for an event' do
      payload = migration.create_push_event_payload(event)

      expect(PushEventPayload.count).to eq(1)
      expect(payload.valid?).to eq(true)
    end

    it 'does not create push event payloads for removed events' do
      allow(event).to receive(:id).and_return(-1)

      payload = migration.create_push_event_payload(event)

      expect(payload).to be_nil
      expect(PushEventPayload.count).to eq(0)
    end

    it 'encodes and decodes the commit IDs from and to binary data' do
      payload = migration.create_push_event_payload(event)
      packed = migration.pack(push_data[:before])

      expect(payload.commit_from).to eq(packed)
      expect(payload.commit_to).to be_nil
    end
  end

  describe '#find_events' do
    it 'returns the events for the given ID range' do
      event1 = create_push_event(project, author, { commits: [] })
      event2 = create_push_event(project, author, { commits: [] })
      event3 = create_push_event(project, author, { commits: [] })
      events = migration.find_events(event1.id, event2.id)

      expect(events.length).to eq(2)
      expect(events.pluck(:id)).not_to include(event3.id)
    end
  end

  describe '#migrate?' do
    it 'returns true when data should be migrated' do
      allow(described_class::Event)
        .to receive(:table_exists?).and_return(true)

      allow(described_class::PushEventPayload)
        .to receive(:table_exists?).and_return(true)

      allow(described_class::EventForMigration)
        .to receive(:table_exists?).and_return(true)

      expect(migration.migrate?).to eq(true)
    end

    it 'returns false if the "events" table does not exist' do
      allow(described_class::Event)
        .to receive(:table_exists?).and_return(false)

      expect(migration.migrate?).to eq(false)
    end

    it 'returns false if the "push_event_payloads" table does not exist' do
      allow(described_class::Event)
        .to receive(:table_exists?).and_return(true)

      allow(described_class::PushEventPayload)
        .to receive(:table_exists?).and_return(false)

      expect(migration.migrate?).to eq(false)
    end

    it 'returns false when the "events_for_migration" table does not exist' do
      allow(described_class::Event)
        .to receive(:table_exists?).and_return(true)

      allow(described_class::PushEventPayload)
        .to receive(:table_exists?).and_return(true)

      allow(described_class::EventForMigration)
        .to receive(:table_exists?).and_return(false)

      expect(migration.migrate?).to eq(false)
    end
  end

  describe '#pack' do
    it 'packs a SHA1 into a 20 byte binary string' do
      packed = migration.pack('156e0e9adc587a383a7eeb5b21ddecb9044768a8')

      expect(packed.bytesize).to eq(20)
    end

    it 'returns nil if the input value is nil' do
      expect(migration.pack(nil)).to be_nil
    end
  end
end