2017-06-30 12:22:23 +00:00
|
|
|
require 'spec_helper'
|
2017-07-11 12:31:04 +00:00
|
|
|
require Rails.root.join('db', 'post_migrate', '20170711145558_migrate_stages_statuses.rb')
|
2017-06-30 12:22:23 +00:00
|
|
|
|
2018-02-28 23:35:43 +00:00
|
|
|
describe MigrateStagesStatuses, :sidekiq, :migration do
|
2017-06-30 12:22:23 +00:00
|
|
|
let(:jobs) { table(:ci_builds) }
|
|
|
|
let(:stages) { table(:ci_stages) }
|
|
|
|
let(:pipelines) { table(:ci_pipelines) }
|
|
|
|
let(:projects) { table(:projects) }
|
|
|
|
|
|
|
|
STATUSES = { created: 0, pending: 1, running: 2, success: 3,
|
2017-07-11 10:01:35 +00:00
|
|
|
failed: 4, canceled: 5, skipped: 6, manual: 7 }.freeze
|
2017-06-30 12:22:23 +00:00
|
|
|
|
|
|
|
before do
|
2017-07-05 08:54:48 +00:00
|
|
|
stub_const("#{described_class.name}::BATCH_SIZE", 2)
|
2017-08-23 12:48:26 +00:00
|
|
|
stub_const("#{described_class.name}::RANGE_SIZE", 1)
|
2017-07-05 08:54:48 +00:00
|
|
|
|
2017-06-30 12:22:23 +00:00
|
|
|
projects.create!(id: 1, name: 'gitlab1', path: 'gitlab1')
|
|
|
|
projects.create!(id: 2, name: 'gitlab2', path: 'gitlab2')
|
|
|
|
|
2017-07-04 11:43:26 +00:00
|
|
|
pipelines.create!(id: 1, project_id: 1, ref: 'master', sha: 'adf43c3a')
|
|
|
|
pipelines.create!(id: 2, project_id: 2, ref: 'feature', sha: '21a3deb')
|
2017-06-30 12:22:23 +00:00
|
|
|
|
|
|
|
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: 'build', status: 'success')
|
|
|
|
create_job(project: 1, pipeline: 1, stage: 'build', status: 'failed')
|
|
|
|
create_job(project: 2, pipeline: 2, stage: 'test', status: 'success')
|
2017-07-04 11:43:26 +00:00
|
|
|
create_job(project: 2, pipeline: 2, stage: 'test', status: 'success')
|
2017-07-04 12:07:17 +00:00
|
|
|
create_job(project: 2, pipeline: 2, stage: 'test', status: 'failed', retried: true)
|
2017-06-30 12:22:23 +00:00
|
|
|
|
2017-07-04 11:43:26 +00:00
|
|
|
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: 'build', status: nil)
|
|
|
|
stages.create!(id: 3, pipeline_id: 2, project_id: 2, name: 'test', status: nil)
|
2017-06-30 12:22:23 +00:00
|
|
|
end
|
|
|
|
|
2017-07-04 11:43:26 +00:00
|
|
|
it 'correctly migrates stages statuses' do
|
2018-07-23 04:34:54 +00:00
|
|
|
perform_enqueued_jobs do
|
2017-07-05 08:54:48 +00:00
|
|
|
expect(stages.where(status: nil).count).to eq 3
|
2017-06-30 12:22:23 +00:00
|
|
|
|
2017-07-05 08:54:48 +00:00
|
|
|
migrate!
|
2017-06-30 12:22:23 +00:00
|
|
|
|
2017-07-05 08:54:48 +00:00
|
|
|
expect(stages.where(status: nil)).to be_empty
|
|
|
|
expect(stages.all.order('id ASC').pluck(:status))
|
|
|
|
.to eq [STATUSES[:running], STATUSES[:failed], STATUSES[:success]]
|
|
|
|
end
|
2017-06-30 12:22:23 +00:00
|
|
|
end
|
|
|
|
|
2017-07-05 08:54:48 +00:00
|
|
|
it 'correctly schedules background migrations' do
|
|
|
|
Sidekiq::Testing.fake! do
|
|
|
|
Timecop.freeze do
|
|
|
|
migrate!
|
|
|
|
|
2018-01-05 09:15:03 +00:00
|
|
|
expect(described_class::MIGRATION).to be_scheduled_delayed_migration(5.minutes, 1, 1)
|
|
|
|
expect(described_class::MIGRATION).to be_scheduled_delayed_migration(5.minutes, 2, 2)
|
|
|
|
expect(described_class::MIGRATION).to be_scheduled_delayed_migration(10.minutes, 3, 3)
|
2017-08-23 12:48:26 +00:00
|
|
|
expect(BackgroundMigrationWorker.jobs.size).to eq 3
|
2017-07-05 08:54:48 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-04 12:07:17 +00:00
|
|
|
def create_job(project:, pipeline:, stage:, status:, **opts)
|
2017-07-11 10:01:35 +00:00
|
|
|
stages = { test: 1, build: 2, deploy: 3 }
|
2017-07-04 13:31:15 +00:00
|
|
|
|
2017-06-30 12:22:23 +00:00
|
|
|
jobs.create!(project_id: project, commit_id: pipeline,
|
2017-07-04 13:31:15 +00:00
|
|
|
stage_idx: stages[stage.to_sym], stage: stage,
|
2017-07-04 12:07:17 +00:00
|
|
|
status: status, **opts)
|
2017-06-30 12:22:23 +00:00
|
|
|
end
|
|
|
|
end
|