Fix specs. Rename migration file name which was conflicted with background migration's.

This commit is contained in:
Shinya Maeda 2018-06-03 15:04:47 +09:00
parent 0d00d02e84
commit bcd664f53a
5 changed files with 78 additions and 74 deletions

View File

@ -1,4 +1,4 @@
class ArchiveLegacyTraces < ActiveRecord::Migration class ScheduleToArchiveLegacyTraces < ActiveRecord::Migration
include Gitlab::Database::MigrationHelpers include Gitlab::Database::MigrationHelpers
DOWNTIME = false DOWNTIME = false
@ -21,7 +21,7 @@ class ArchiveLegacyTraces < ActiveRecord::Migration
def up def up
queue_background_migration_jobs_by_range_at_intervals( queue_background_migration_jobs_by_range_at_intervals(
::ArchiveLegacyTraces::Build.finished.without_archived_trace, ::ScheduleToArchiveLegacyTraces::Build.finished.without_archived_trace,
BACKGROUND_MIGRATION_CLASS, BACKGROUND_MIGRATION_CLASS,
5.minutes, 5.minutes,
batch_size: BATCH_SIZE) batch_size: BATCH_SIZE)

View File

@ -1,6 +1,8 @@
require 'spec_helper' require 'spec_helper'
describe Gitlab::BackgroundMigration::ArchiveLegacyTraces, :migration, schema: 20180529152628 do describe Gitlab::BackgroundMigration::ArchiveLegacyTraces, :migration, schema: 20180529152628 do
include TraceHelpers
let(:namespaces) { table(:namespaces) } let(:namespaces) { table(:namespaces) }
let(:projects) { table(:projects) } let(:projects) { table(:projects) }
let(:builds) { table(:ci_builds) } let(:builds) { table(:ci_builds) }
@ -9,52 +11,31 @@ describe Gitlab::BackgroundMigration::ArchiveLegacyTraces, :migration, schema: 2
before do before do
namespaces.create!(id: 123, name: 'gitlab1', path: 'gitlab1') namespaces.create!(id: 123, name: 'gitlab1', path: 'gitlab1')
projects.create!(id: 123, name: 'gitlab1', path: 'gitlab1', namespace_id: 123) projects.create!(id: 123, name: 'gitlab1', path: 'gitlab1', namespace_id: 123)
build = builds.create!(id: 1, project_id: 123, status: 'success') @build = builds.create!(id: 1, project_id: 123, status: 'success', type: 'Ci::Build')
@legacy_trace_dir = File.join(Settings.gitlab_ci.builds_path,
build.created_at.utc.strftime("%Y_%m"),
build.project_id.to_s)
FileUtils.mkdir_p(@legacy_trace_dir)
@legacy_trace_path = File.join(@legacy_trace_dir, "#{build.id}.log")
end end
context 'when trace file exsits at the right place' do context 'when trace file exsits at the right place' do
before do before do
File.open(@legacy_trace_path, 'wb') { |stream| stream.write('aiueo') } create_legacy_trace(@build, 'aiueo')
end end
it 'correctly archive legacy traces' do it 'correctly archive legacy traces' do
expect(job_artifacts.count).to eq(0) expect(job_artifacts.count).to eq(0)
expect(File.exist?(@legacy_trace_path)).to be_truthy expect(File.exist?(legacy_trace_path(@build))).to be_truthy
described_class.new.perform(1, 1) described_class.new.perform(1, 1)
expect(job_artifacts.count).to eq(1) expect(job_artifacts.count).to eq(1)
expect(File.exist?(@legacy_trace_path)).to be_falsy expect(File.exist?(legacy_trace_path(@build))).to be_falsy
expect(File.read(new_trace_path)).to eq('aiueo') expect(File.read(archived_trace_path(job_artifacts.first))).to eq('aiueo')
end end
end end
context 'when trace file does not exsits at the right place' do context 'when trace file does not exsits at the right place' do
it 'correctly archive legacy traces' do it 'does not raise errors and create job artifact row' do
expect(job_artifacts.count).to eq(0)
expect(File.exist?(@legacy_trace_path)).to be_falsy
described_class.new.perform(1, 1) described_class.new.perform(1, 1)
expect(job_artifacts.count).to eq(0) expect(job_artifacts.count).to eq(0)
end end
end end
def new_trace_path
job_artifact = job_artifacts.first
disk_hash = Digest::SHA2.hexdigest(job_artifact.project_id.to_s)
creation_date = job_artifact.created_at.utc.strftime('%Y_%m_%d')
File.join(Gitlab.config.artifacts.path, disk_hash[0..1], disk_hash[2..3], disk_hash,
creation_date, job_artifact.job_id.to_s, job_artifact.id.to_s, 'job.log')
end
end end

View File

@ -1,45 +0,0 @@
require 'spec_helper'
require Rails.root.join('db', 'post_migrate', '20180529152628_archive_legacy_traces')
describe ArchiveLegacyTraces, :migration do
let(:namespaces) { table(:namespaces) }
let(:projects) { table(:projects) }
let(:builds) { table(:ci_builds) }
let(:job_artifacts) { table(:ci_job_artifacts) }
before do
namespaces.create!(id: 123, name: 'gitlab1', path: 'gitlab1')
projects.create!(id: 123, name: 'gitlab1', path: 'gitlab1', namespace_id: 123)
build = builds.create!(id: 1)
@legacy_trace_path = File.join(
Settings.gitlab_ci.builds_path,
build.created_at.utc.strftime("%Y_%m"),
build.project_id.to_s,
"#{job.id}.log"
)
File.open(@legacy_trace_path, 'wb') { |stream| stream.write('aiueo') }
end
it 'correctly archive legacy traces' do
expect(job_artifacts.count).to eq(0)
expect(File.exist?(@legacy_trace_path)).to be_truthy
migrate!
expect(job_artifacts.count).to eq(1)
expect(File.exist?(@legacy_trace_path)).to be_falsy
expect(File.exist?(new_trace_path)).to be_truthy
end
def new_trace_path
job_artifact = job_artifacts.first
disk_hash = Digest::SHA2.hexdigest(job_artifact.project_id.to_s)
creation_date = job_artifact.created_at.utc.strftime('%Y_%m_%d')
File.join(disk_hash[0..1], disk_hash[2..3], disk_hash,
creation_date, job_artifact.job_id.to_s, job_artifact.id.to_s)
end
end

View File

@ -0,0 +1,45 @@
require 'spec_helper'
require Rails.root.join('db', 'post_migrate', '20180529152628_schedule_to_archive_legacy_traces')
describe ScheduleToArchiveLegacyTraces, :migration do
include TraceHelpers
let(:namespaces) { table(:namespaces) }
let(:projects) { table(:projects) }
let(:builds) { table(:ci_builds) }
let(:job_artifacts) { table(:ci_job_artifacts) }
before do
namespaces.create!(id: 123, name: 'gitlab1', path: 'gitlab1')
projects.create!(id: 123, name: 'gitlab1', path: 'gitlab1', namespace_id: 123)
@build_success = builds.create!(id: 1, project_id: 123, status: 'success', type: 'Ci::Build')
@build_failed = builds.create!(id: 2, project_id: 123, status: 'failed', type: 'Ci::Build')
@builds_canceled = builds.create!(id: 3, project_id: 123, status: 'canceled', type: 'Ci::Build')
@build_running = builds.create!(id: 4, project_id: 123, status: 'running', type: 'Ci::Build')
create_legacy_trace(@build_success, 'This job is done')
create_legacy_trace(@build_failed, 'This job is done')
create_legacy_trace(@builds_canceled, 'This job is done')
create_legacy_trace(@build_running, 'This job is not done yet')
end
it 'correctly archive legacy traces' do
expect(job_artifacts.count).to eq(0)
expect(File.exist?(legacy_trace_path(@build_success))).to be_truthy
expect(File.exist?(legacy_trace_path(@build_failed))).to be_truthy
expect(File.exist?(legacy_trace_path(@builds_canceled))).to be_truthy
expect(File.exist?(legacy_trace_path(@build_running))).to be_truthy
migrate!
expect(job_artifacts.count).to eq(3)
expect(File.exist?(legacy_trace_path(@build_success))).to be_falsy
expect(File.exist?(legacy_trace_path(@build_failed))).to be_falsy
expect(File.exist?(legacy_trace_path(@builds_canceled))).to be_falsy
expect(File.exist?(legacy_trace_path(@build_running))).to be_truthy
expect(File.exist?(archived_trace_path(job_artifacts.where(job_id: @build_success.id).first))).to be_truthy
expect(File.exist?(archived_trace_path(job_artifacts.where(job_id: @build_failed.id).first))).to be_truthy
expect(File.exist?(archived_trace_path(job_artifacts.where(job_id: @builds_canceled.id).first))).to be_truthy
expect(job_artifacts.where(job_id: @build_running.id)).not_to be_exist
end
end

View File

@ -0,0 +1,23 @@
module TraceHelpers
def create_legacy_trace(build, content)
File.open(legacy_trace_path(build), 'wb') { |stream| stream.write(content) }
end
def legacy_trace_path(build)
legacy_trace_dir = File.join(Settings.gitlab_ci.builds_path,
build.created_at.utc.strftime("%Y_%m"),
build.project_id.to_s)
FileUtils.mkdir_p(legacy_trace_dir)
File.join(legacy_trace_dir, "#{build.id}.log")
end
def archived_trace_path(job_artifact)
disk_hash = Digest::SHA2.hexdigest(job_artifact.project_id.to_s)
creation_date = job_artifact.created_at.utc.strftime('%Y_%m_%d')
File.join(Gitlab.config.artifacts.path, disk_hash[0..1], disk_hash[2..3], disk_hash,
creation_date, job_artifact.job_id.to_s, job_artifact.id.to_s, 'job.log')
end
end