2019-11-26 03:06:24 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-30 14:32:21 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Gitlab::ImportExport::LfsSaver do
|
|
|
|
let(:shared) { project.import_export_shared }
|
|
|
|
let(:export_path) { "#{Dir.tmpdir}/project_tree_saver_spec" }
|
|
|
|
let(:project) { create(:project) }
|
|
|
|
|
|
|
|
subject(:saver) { described_class.new(project: project, shared: shared) }
|
|
|
|
|
|
|
|
before do
|
2019-12-16 12:07:43 +00:00
|
|
|
allow_next_instance_of(Gitlab::ImportExport) do |instance|
|
|
|
|
allow(instance).to receive(:storage_path).and_return(export_path)
|
|
|
|
end
|
2018-03-30 14:32:21 +00:00
|
|
|
FileUtils.mkdir_p(shared.export_path)
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
FileUtils.rm_rf(shared.export_path)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#save' do
|
2018-04-03 16:54:42 +00:00
|
|
|
context 'when the project has LFS objects locally stored' do
|
2018-03-30 14:32:21 +00:00
|
|
|
let(:lfs_object) { create(:lfs_object, :with_file) }
|
2019-07-24 11:23:51 +00:00
|
|
|
let(:lfs_json_file) { File.join(shared.export_path, Gitlab::ImportExport.lfs_objects_filename) }
|
|
|
|
|
|
|
|
def lfs_json
|
2020-05-01 00:09:59 +00:00
|
|
|
Gitlab::Json.parse(IO.read(lfs_json_file))
|
2019-07-24 11:23:51 +00:00
|
|
|
end
|
2018-03-30 17:45:58 +00:00
|
|
|
|
2018-03-30 14:32:21 +00:00
|
|
|
before do
|
2018-03-30 17:45:58 +00:00
|
|
|
project.lfs_objects << lfs_object
|
2018-03-30 14:32:21 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not cause errors' do
|
|
|
|
saver.save
|
|
|
|
|
|
|
|
expect(shared.errors).to be_empty
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'copies the file in the correct location when there is an lfs object' do
|
|
|
|
saver.save
|
|
|
|
|
|
|
|
expect(File).to exist("#{shared.export_path}/lfs-objects/#{lfs_object.oid}")
|
|
|
|
end
|
2019-07-24 11:23:51 +00:00
|
|
|
|
|
|
|
describe 'saving a json file' do
|
|
|
|
before do
|
|
|
|
# Create two more LfsObjectProject records with different `repository_type`s
|
|
|
|
%w(wiki design).each do |repository_type|
|
|
|
|
create(
|
|
|
|
:lfs_objects_project,
|
|
|
|
project: project,
|
|
|
|
repository_type: repository_type,
|
|
|
|
lfs_object: lfs_object
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
FileUtils.rm_rf(lfs_json_file)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'saves a json file correctly' do
|
|
|
|
saver.save
|
|
|
|
|
|
|
|
expect(File.exist?(lfs_json_file)).to eq(true)
|
|
|
|
expect(lfs_json).to eq(
|
|
|
|
{
|
|
|
|
lfs_object.oid => [
|
|
|
|
LfsObjectsProject.repository_types['wiki'],
|
|
|
|
LfsObjectsProject.repository_types['design'],
|
|
|
|
nil
|
|
|
|
]
|
|
|
|
}
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not save a json file if feature is disabled' do
|
|
|
|
stub_feature_flags(export_lfs_objects_projects: false)
|
|
|
|
|
|
|
|
saver.save
|
|
|
|
|
|
|
|
expect(File.exist?(lfs_json_file)).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
2018-03-30 14:32:21 +00:00
|
|
|
end
|
2018-04-03 16:54:42 +00:00
|
|
|
|
|
|
|
context 'when the LFS objects are stored in object storage' do
|
|
|
|
let(:lfs_object) { create(:lfs_object, :object_storage) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(LfsObjectUploader).to receive(:object_store_enabled?).and_return(true)
|
|
|
|
project.lfs_objects << lfs_object
|
2019-07-24 11:23:51 +00:00
|
|
|
|
|
|
|
expect_next_instance_of(LfsObjectUploader) do |instance|
|
|
|
|
expect(instance).to receive(:url).and_return('http://my-object-storage.local')
|
|
|
|
end
|
2018-04-03 16:54:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'downloads the file to include in an archive' do
|
|
|
|
fake_uri = double
|
|
|
|
exported_file_path = "#{shared.export_path}/lfs-objects/#{lfs_object.oid}"
|
|
|
|
|
|
|
|
expect(fake_uri).to receive(:open).and_return(StringIO.new('LFS file content'))
|
|
|
|
expect(URI).to receive(:parse).with('http://my-object-storage.local').and_return(fake_uri)
|
|
|
|
|
|
|
|
saver.save
|
|
|
|
|
|
|
|
expect(File.read(exported_file_path)).to eq('LFS file content')
|
|
|
|
end
|
|
|
|
end
|
2018-03-30 14:32:21 +00:00
|
|
|
end
|
|
|
|
end
|