summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/background_migration/migrate_build_stage_spec.rb
blob: e112e9e9e3d0b6cf53de3bd974683c61f965072b (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
require 'spec_helper'

describe Gitlab::BackgroundMigration::MigrateBuildStage, :migration, schema: 20180212101928 do
  let(:projects) { table(:projects) }
  let(:pipelines) { table(:ci_pipelines) }
  let(:stages) { table(:ci_stages) }
  let(:jobs) { table(:ci_builds) }

  STATUSES = { created: 0, pending: 1, running: 2, success: 3,
               failed: 4, canceled: 5, skipped: 6, manual: 7 }.freeze

  before do
    projects.create!(id: 123, name: 'gitlab', path: 'gitlab-ce')
    pipelines.create!(id: 1, project_id: 123, ref: 'master', sha: 'adf43c3a')

    jobs.create!(id: 1, commit_id: 1, project_id: 123,
                 stage_idx: 2, stage: 'build', status: :success)
    jobs.create!(id: 2, commit_id: 1, project_id: 123,
                 stage_idx: 2, stage: 'build', status: :success)
    jobs.create!(id: 3, commit_id: 1, project_id: 123,
                 stage_idx: 1, stage: 'test', status: :failed)
    jobs.create!(id: 4, commit_id: 1, project_id: 123,
                 stage_idx: 1, stage: 'test', status: :success)
    jobs.create!(id: 5, commit_id: 1, project_id: 123,
                 stage_idx: 3, stage: 'deploy', status: :pending)
    jobs.create!(id: 6, commit_id: 1, project_id: 123,
                 stage_idx: 3, stage: nil, status: :pending)
  end

  it 'correctly migrates builds stages' do
    expect(stages.count).to be_zero

    described_class.new.perform(1, 6)

    expect(stages.count).to eq 3
    expect(stages.all.pluck(:name)).to match_array %w[test build deploy]
    expect(jobs.where(stage_id: nil)).to be_one
    expect(jobs.find_by(stage_id: nil).id).to eq 6
    expect(stages.all.pluck(:status)).to match_array [STATUSES[:success],
                                                      STATUSES[:failed],
                                                      STATUSES[:pending]]
  end

  it 'recovers from unique constraint violation only twice' do
    allow(described_class::Migratable::Stage)
      .to receive(:find_by).and_return(nil)

    expect(described_class::Migratable::Stage)
      .to receive(:find_by).exactly(3).times

    expect { described_class.new.perform(1, 6) }
      .to raise_error ActiveRecord::RecordNotUnique
  end
end