2017-05-04 17:02:51 -04:00
|
|
|
# encoding: utf-8
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
require Rails.root.join('db', 'post_migrate', '20170523083112_migrate_old_artifacts.rb')
|
|
|
|
|
|
|
|
describe MigrateOldArtifacts do
|
|
|
|
let(:migration) { described_class.new }
|
|
|
|
let!(:directory) { Dir.mktmpdir }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(Gitlab.config.artifacts).to receive(:path).and_return(directory)
|
|
|
|
end
|
2017-08-15 13:44:37 -04:00
|
|
|
|
2017-05-04 17:02:51 -04:00
|
|
|
after do
|
|
|
|
FileUtils.remove_entry_secure(directory)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with migratable data' do
|
2017-11-08 04:46:47 -05:00
|
|
|
set(:project1) { create(:project, ci_id: 2) }
|
|
|
|
set(:project2) { create(:project, ci_id: 3) }
|
|
|
|
set(:project3) { create(:project) }
|
2017-05-04 17:02:51 -04:00
|
|
|
|
2017-11-08 04:46:47 -05:00
|
|
|
set(:pipeline1) { create(:ci_empty_pipeline, project: project1) }
|
|
|
|
set(:pipeline2) { create(:ci_empty_pipeline, project: project2) }
|
|
|
|
set(:pipeline3) { create(:ci_empty_pipeline, project: project3) }
|
2017-05-04 17:02:51 -04:00
|
|
|
|
|
|
|
let!(:build_with_legacy_artifacts) { create(:ci_build, pipeline: pipeline1) }
|
|
|
|
let!(:build_without_artifacts) { create(:ci_build, pipeline: pipeline1) }
|
2017-11-08 04:46:47 -05:00
|
|
|
let!(:build2) { create(:ci_build, pipeline: pipeline2) }
|
|
|
|
let!(:build3) { create(:ci_build, pipeline: pipeline3) }
|
2017-05-04 17:02:51 -04:00
|
|
|
|
|
|
|
before do
|
2017-11-08 04:46:47 -05:00
|
|
|
setup_builds(build2, build3)
|
|
|
|
|
2017-05-04 17:02:51 -04:00
|
|
|
store_artifacts_in_legacy_path(build_with_legacy_artifacts)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "legacy artifacts are not accessible" do
|
|
|
|
expect(build_with_legacy_artifacts.artifacts?).to be_falsey
|
|
|
|
end
|
|
|
|
|
|
|
|
it "legacy artifacts are set" do
|
2017-11-23 12:51:20 -05:00
|
|
|
expect(build_with_legacy_artifacts.legacy_artifacts_file_identifier).not_to be_nil
|
2017-05-04 17:02:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#min_id' do
|
|
|
|
subject { migration.send(:min_id) }
|
|
|
|
|
|
|
|
it 'returns the newest build for which ci_id is not defined' do
|
|
|
|
is_expected.to eq(build3.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#builds_with_artifacts' do
|
|
|
|
subject { migration.send(:builds_with_artifacts).map(&:id) }
|
|
|
|
|
|
|
|
it 'returns a list of builds that has artifacts and could be migrated' do
|
|
|
|
is_expected.to contain_exactly(build_with_legacy_artifacts.id, build2.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#up' do
|
|
|
|
context 'when migrating artifacts' do
|
|
|
|
before do
|
|
|
|
migration.up
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'all files do have artifacts' do
|
2018-03-15 11:11:49 -04:00
|
|
|
Ci::Build.with_artifacts_archive do |build|
|
2017-05-04 17:02:51 -04:00
|
|
|
expect(build).to have_artifacts
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'artifacts are no longer present on legacy path' do
|
|
|
|
expect(File.exist?(legacy_path(build_with_legacy_artifacts))).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when there are aritfacts in old and new directory' do
|
|
|
|
before do
|
|
|
|
store_artifacts_in_legacy_path(build2)
|
|
|
|
|
|
|
|
migration.up
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not move old files' do
|
|
|
|
expect(File.exist?(legacy_path(build2))).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def store_artifacts_in_legacy_path(build)
|
|
|
|
FileUtils.mkdir_p(legacy_path(build))
|
|
|
|
|
|
|
|
FileUtils.copy(
|
2017-06-01 03:52:19 -04:00
|
|
|
Rails.root.join('spec/fixtures/ci_build_artifacts.zip'),
|
|
|
|
File.join(legacy_path(build), "ci_build_artifacts.zip"))
|
2017-08-15 13:44:37 -04:00
|
|
|
|
2017-05-04 17:02:51 -04:00
|
|
|
FileUtils.copy(
|
2017-06-01 03:52:19 -04:00
|
|
|
Rails.root.join('spec/fixtures/ci_build_artifacts_metadata.gz'),
|
|
|
|
File.join(legacy_path(build), "ci_build_artifacts_metadata.gz"))
|
|
|
|
|
2017-05-04 17:02:51 -04:00
|
|
|
build.update_columns(
|
|
|
|
artifacts_file: 'ci_build_artifacts.zip',
|
2017-06-01 03:52:19 -04:00
|
|
|
artifacts_metadata: 'ci_build_artifacts_metadata.gz')
|
|
|
|
|
2017-05-04 17:02:51 -04:00
|
|
|
build.reload
|
|
|
|
end
|
|
|
|
|
|
|
|
def legacy_path(build)
|
|
|
|
File.join(directory,
|
|
|
|
build.created_at.utc.strftime('%Y_%m'),
|
|
|
|
build.project.ci_id.to_s,
|
|
|
|
build.id.to_s)
|
|
|
|
end
|
2017-11-08 04:46:47 -05:00
|
|
|
|
|
|
|
def new_legacy_path(build)
|
|
|
|
File.join(directory,
|
|
|
|
build.created_at.utc.strftime('%Y_%m'),
|
|
|
|
build.project_id.to_s,
|
|
|
|
build.id.to_s)
|
|
|
|
end
|
|
|
|
|
|
|
|
def setup_builds(*builds)
|
|
|
|
builds.each do |build|
|
|
|
|
FileUtils.mkdir_p(new_legacy_path(build))
|
|
|
|
|
|
|
|
build.update_columns(
|
|
|
|
artifacts_file: 'ci_build_artifacts.zip',
|
|
|
|
artifacts_metadata: 'ci_build_artifacts_metadata.gz')
|
|
|
|
|
|
|
|
build.reload
|
|
|
|
end
|
|
|
|
end
|
2017-05-04 17:02:51 -04:00
|
|
|
end
|
|
|
|
end
|