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

describe Gitlab::BackgroundMigration::MigrateStageStatus, :migration, schema: 20170711145320 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: 1, name: 'gitlab1', path: 'gitlab1')
    pipelines.create!(id: 1, project_id: 1, ref: 'master', sha: 'adf43c3a')
    stages.create!(id: 1, pipeline_id: 1, project_id: 1, name: 'test', status: nil)
    stages.create!(id: 2, pipeline_id: 1, project_id: 1, name: 'deploy', status: nil)
  end

  context 'when stage status is known' do
    before do
      create_job(project: 1, pipeline: 1, stage: 'test', status: 'success')
      create_job(project: 1, pipeline: 1, stage: 'test', status: 'running')
      create_job(project: 1, pipeline: 1, stage: 'deploy', status: 'failed')
    end

    it 'sets a correct stage status' do
      described_class.new.perform(1, 2)

      expect(stages.first.status).to eq STATUSES[:running]
      expect(stages.second.status).to eq STATUSES[:failed]
    end
  end

  context 'when stage status is not known' do
    it 'sets a skipped stage status' do
      described_class.new.perform(1, 2)

      expect(stages.first.status).to eq STATUSES[:skipped]
      expect(stages.second.status).to eq STATUSES[:skipped]
    end
  end

  context 'when stage status includes status of a retried job' do
    before do
      create_job(project: 1, pipeline: 1, stage: 'test', status: 'canceled')
      create_job(project: 1, pipeline: 1, stage: 'deploy', status: 'failed', retried: true)
      create_job(project: 1, pipeline: 1, stage: 'deploy', status: 'success')
    end

    it 'sets a correct stage status' do
      described_class.new.perform(1, 2)

      expect(stages.first.status).to eq STATUSES[:canceled]
      expect(stages.second.status).to eq STATUSES[:success]
    end
  end

  context 'when some job in the stage is blocked / manual' do
    before do
      create_job(project: 1, pipeline: 1, stage: 'test', status: 'failed')
      create_job(project: 1, pipeline: 1, stage: 'test', status: 'manual')
      create_job(project: 1, pipeline: 1, stage: 'deploy', status: 'success', when: 'manual')
    end

    it 'sets a correct stage status' do
      described_class.new.perform(1, 2)

      expect(stages.first.status).to eq STATUSES[:manual]
      expect(stages.second.status).to eq STATUSES[:success]
    end
  end

  def create_job(project:, pipeline:, stage:, status:, **opts)
    stages = { test: 1, build: 2, deploy: 3 }

    jobs.create!(project_id: project, commit_id: pipeline,
                 stage_idx: stages[stage.to_sym], stage: stage,
                 status: status, **opts)
  end
end