Welcome to mirror list, hosted at ThFree Co, Russian Federation.

saver_spec.rb « import_export « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f5eed81f73cd4a32e1f99452b726fdedb531b48b (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
# frozen_string_literal: true

require 'spec_helper'
require 'fileutils'

RSpec.describe Gitlab::ImportExport::Saver do
  let!(:project) { create(:project, :public, name: 'project') }
  let(:base_path) { "#{Dir.tmpdir}/project_tree_saver_spec" }
  let(:archive_path) { "#{base_path}/archive" }
  let(:export_path) { "#{archive_path}/export" }
  let(:shared) { project.import_export_shared }

  subject { described_class.new(exportable: project, shared: shared) }

  before do
    allow(shared).to receive(:base_path).and_return(base_path)
    allow_next_instance_of(Gitlab::ImportExport) do |instance|
      allow(instance).to receive(:storage_path).and_return(export_path)
    end

    FileUtils.mkdir_p(shared.export_path)
    FileUtils.touch("#{shared.export_path}/tmp.bundle")
    allow(FileUtils).to receive(:rm_rf).and_call_original
  end

  after do
    FileUtils.rm_rf(export_path)
  end

  it 'saves the repo using object storage' do
    stub_uploads_object_storage(ImportExportUploader)

    subject.save # rubocop:disable Rails/SaveBang

    expect(ImportExportUpload.find_by(project: project).export_file.url)
      .to match(%r[\/uploads\/-\/system\/import_export_upload\/export_file.*])
  end

  it 'logs metrics after saving' do
    stub_uploads_object_storage(ImportExportUploader)
    expect(Gitlab::Export::Logger).to receive(:info).with(
      hash_including(
        message: 'Export archive saved',
        exportable_class: 'Project',
        'correlation_id' => anything,
        archive_file: anything,
        compress_duration_s: anything
      )).and_call_original

    expect(Gitlab::Export::Logger).to receive(:info).with(
      hash_including(
        message: 'Export archive uploaded',
        exportable_class: 'Project',
        'correlation_id' => anything,
        archive_file: anything,
        compress_duration_s: anything,
        assign_duration_s: anything,
        upload_duration_s: anything,
        upload_bytes: anything
      )).and_call_original

    subject.save # rubocop:disable Rails/SaveBang
  end

  it 'removes archive path and keeps base path untouched' do
    allow(shared).to receive(:archive_path).and_return(archive_path)

    subject.save # rubocop:disable Rails/SaveBang

    expect(FileUtils).not_to have_received(:rm_rf).with(base_path)
    expect(FileUtils).to have_received(:rm_rf).with(archive_path)
    expect(Dir.exist?(archive_path)).to eq(false)
  end

  context 'when save throws an exception' do
    before do
      expect(subject).to receive(:save_upload).and_raise(SocketError.new)
    end

    it 'logs a saver error' do
      allow(Gitlab::Export::Logger).to receive(:info).with(anything).and_call_original
      expect(Gitlab::Export::Logger).to receive(:info).with(
        hash_including(
          message: 'Export archive saver failed',
          exportable_class: 'Project',
          'correlation_id' => anything
        )).and_call_original

      subject.save # rubocop:disable Rails/SaveBang
    end
  end
end