summaryrefslogtreecommitdiff
path: root/spec/services/projects/import_export/relation_export_service_spec.rb
blob: 94f5653ee7d28f86cc34c7d70e79690c9471dc0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Projects::ImportExport::RelationExportService do
  using RSpec::Parameterized::TableSyntax

  subject(:service) { described_class.new(relation_export, 'jid') }

  let_it_be(:project_export_job) { create(:project_export_job) }
  let_it_be(:export_path) { "#{Dir.tmpdir}/project_tree_saver_spec" }
  let_it_be(:archive_path) { "#{Dir.tmpdir}/project_archive_spec" }

  let(:relation_export) { create(:project_relation_export, relation: relation, project_export_job: project_export_job) }

  before do
    stub_uploads_object_storage(ImportExportUploader, enabled: false)

    allow(project_export_job.project.import_export_shared).to receive(:export_path).and_return(export_path)
    allow(project_export_job.project.import_export_shared).to receive(:archive_path).and_return(archive_path)
    allow(FileUtils).to receive(:remove_entry).with(any_args).and_call_original
  end

  describe '#execute' do
    let(:relation) { 'labels' }

    it 'removes temporary paths used to export files' do
      expect(FileUtils).to receive(:remove_entry).with(export_path)
      expect(FileUtils).to receive(:remove_entry).with(archive_path)

      service.execute
    end

    context 'when saver fails to export relation' do
      before do
        allow_next_instance_of(Gitlab::ImportExport::Project::RelationSaver) do |saver|
          allow(saver).to receive(:save).and_return(false)
        end
      end

      it 'flags export as failed' do
        service.execute

        expect(relation_export.failed?).to eq(true)
      end

      it 'logs failed message' do
        expect_next_instance_of(Gitlab::Export::Logger) do |logger|
          expect(logger).to receive(:error).with(
            export_error: '',
            message: 'Project relation export failed',
            project_export_job_id: project_export_job.id,
            project_id: project_export_job.project.id,
            project_name: project_export_job.project.name
          )
        end

        service.execute
      end
    end

    context 'when an exception is raised' do
      before do
        allow_next_instance_of(Gitlab::ImportExport::Project::RelationSaver) do |saver|
          allow(saver).to receive(:save).and_raise('Error!')
        end
      end

      it 'flags export as failed' do
        service.execute

        expect(relation_export.failed?).to eq(true)
        expect(relation_export.export_error).to eq('Error!')
      end

      it 'logs exception error message' do
        expect_next_instance_of(Gitlab::Export::Logger) do |logger|
          expect(logger).to receive(:error).with(
            export_error: 'Error!',
            message: 'Project relation export failed',
            project_export_job_id: project_export_job.id,
            project_id: project_export_job.project.id,
            project_name: project_export_job.project.name
          )
        end

        service.execute
      end
    end

    describe 'relation name and saver class' do
      where(:relation_name, :saver) do
        Projects::ImportExport::RelationExport::UPLOADS_RELATION | Gitlab::ImportExport::UploadsSaver
        Projects::ImportExport::RelationExport::REPOSITORY_RELATION | Gitlab::ImportExport::RepoSaver
        Projects::ImportExport::RelationExport::WIKI_REPOSITORY_RELATION | Gitlab::ImportExport::WikiRepoSaver
        Projects::ImportExport::RelationExport::LFS_OBJECTS_RELATION | Gitlab::ImportExport::LfsSaver
        Projects::ImportExport::RelationExport::SNIPPETS_REPOSITORY_RELATION | Gitlab::ImportExport::SnippetsRepoSaver
        Projects::ImportExport::RelationExport::DESIGN_REPOSITORY_RELATION | Gitlab::ImportExport::DesignRepoSaver
        Projects::ImportExport::RelationExport::ROOT_RELATION | Gitlab::ImportExport::Project::RelationSaver
        'labels' | Gitlab::ImportExport::Project::RelationSaver
      end

      with_them do
        let(:relation) { relation_name }

        it 'exports relation using correct saver' do
          expect(saver).to receive(:new).and_call_original

          service.execute
        end

        it 'assigns finished status and relation file' do
          service.execute

          expect(relation_export.finished?).to eq(true)
          expect(relation_export.upload.export_file.filename).to eq("#{relation}.tar.gz")
        end
      end
    end
  end
end