PageRenderTime 26ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/spec/lib/gitlab/import_export/lfs_restorer_spec.rb

https://gitlab.com/klml/gitlab-ee
Ruby | 128 lines | 95 code | 31 blank | 2 comment | 0 complexity | fc8a0f9875f414a69aa3e9eab91c5857 MD5 | raw file
  1. # frozen_string_literal: true
  2. require 'spec_helper'
  3. RSpec.describe Gitlab::ImportExport::LfsRestorer do
  4. include UploadHelpers
  5. let(:export_path) { "#{Dir.tmpdir}/lfs_object_restorer_spec" }
  6. let(:project) { create(:project) }
  7. let(:shared) { project.import_export_shared }
  8. let(:saver) { Gitlab::ImportExport::LfsSaver.new(project: project, shared: shared) }
  9. subject(:restorer) { described_class.new(project: project, shared: shared) }
  10. before do
  11. allow(Gitlab::ImportExport).to receive(:storage_path).and_return(export_path)
  12. FileUtils.mkdir_p(shared.export_path)
  13. end
  14. after do
  15. FileUtils.rm_rf(shared.export_path)
  16. end
  17. describe '#restore' do
  18. context 'when the archive contains lfs files' do
  19. let(:lfs_object) { create(:lfs_object, :correct_oid, :with_file) }
  20. # Use the LfsSaver to save data to be restored
  21. def save_lfs_data
  22. %w(project wiki).each do |repository_type|
  23. create(
  24. :lfs_objects_project,
  25. project: project,
  26. repository_type: repository_type,
  27. lfs_object: lfs_object
  28. )
  29. end
  30. saver.save
  31. project.lfs_objects.delete_all
  32. end
  33. before do
  34. save_lfs_data
  35. project.reload
  36. end
  37. it 'succeeds' do
  38. expect(restorer.restore).to eq(true)
  39. expect(shared.errors).to be_empty
  40. end
  41. it 'does not create a new `LfsObject` records, as one already exists' do
  42. expect { restorer.restore }.not_to change { LfsObject.count }
  43. end
  44. it 'creates new `LfsObjectsProject` records in order to link the project to the existing `LfsObject`' do
  45. expect { restorer.restore }.to change { LfsObjectsProject.count }.by(2)
  46. end
  47. it 'restores the correct `LfsObject` records' do
  48. restorer.restore
  49. expect(project.lfs_objects).to contain_exactly(lfs_object)
  50. end
  51. it 'restores the correct `LfsObjectsProject` records for the project' do
  52. restorer.restore
  53. expect(
  54. project.lfs_objects_projects.pluck(:repository_type)
  55. ).to contain_exactly('project', 'wiki')
  56. end
  57. it 'assigns the file correctly' do
  58. restorer.restore
  59. expect(project.lfs_objects.first.file.read).to eq(lfs_object.file.read)
  60. end
  61. context 'when there is not an existing `LfsObject`' do
  62. before do
  63. lfs_object.destroy
  64. end
  65. it 'creates a new lfs object' do
  66. expect { restorer.restore }.to change { LfsObject.count }.by(1)
  67. end
  68. it 'stores the upload' do
  69. expect_any_instance_of(LfsObjectUploader).to receive(:store!)
  70. restorer.restore
  71. end
  72. end
  73. context 'when there is no lfs-objects.json file' do
  74. before do
  75. json_file = File.join(shared.export_path, ::Gitlab::ImportExport.lfs_objects_filename)
  76. FileUtils.rm_rf(json_file)
  77. end
  78. it 'restores the correct `LfsObject` records' do
  79. restorer.restore
  80. expect(project.lfs_objects).to contain_exactly(lfs_object)
  81. end
  82. it 'restores a single `LfsObjectsProject` record for the project with "project" for the `repository_type`' do
  83. restorer.restore
  84. expect(
  85. project.lfs_objects_projects.pluck(:repository_type)
  86. ).to contain_exactly('project')
  87. end
  88. end
  89. end
  90. context 'without any LFS-objects' do
  91. it 'succeeds' do
  92. expect(restorer.restore).to be_truthy
  93. expect(shared.errors).to be_empty
  94. end
  95. end
  96. end
  97. end