shared_spec.rb 2.1 KB
Newer Older
1 2
# frozen_string_literal: true

3 4 5 6 7 8 9
require 'spec_helper'
require 'fileutils'

describe Gitlab::ImportExport::Shared do
  let(:project) { build(:project) }
  subject { project.import_export_shared }

10 11
  context 'with a repository on disk' do
    let(:project) { create(:project, :repository) }
12
    let(:base_path) { %(/tmp/gitlab_exports/#{project.disk_path}/) }
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

    describe '#archive_path' do
      it 'uses a random hash to avoid conflicts' do
        expect(subject.archive_path).to match(/#{base_path}\h{32}/)
      end

      it 'memoizes the path' do
        path = subject.archive_path

        2.times { expect(subject.archive_path).to eq(path) }
      end
    end

    describe '#export_path' do
      it 'uses a random hash relative to project path' do
        expect(subject.export_path).to match(/#{base_path}\h{32}\/\h{32}/)
      end

      it 'memoizes the path' do
        path = subject.export_path

        2.times { expect(subject.export_path).to eq(path) }
      end
    end
  end

39 40 41 42 43 44 45 46 47
  describe '#error' do
    let(:error) { StandardError.new('Error importing into /my/folder Permission denied @ unlink_internal - /var/opt/gitlab/gitlab-rails/shared/a/b/c/uploads/file') }

    it 'filters any full paths' do
      subject.error(error)

      expect(subject.errors).to eq(['Error importing into [FILTERED] Permission denied @ unlink_internal - [FILTERED]'])
    end

48 49 50 51 52 53 54 55 56 57
    it 'updates the import JID' do
      import_state = create(:import_state, project: project, jid: 'jid-test')

      expect_next_instance_of(Gitlab::Import::Logger) do |logger|
        expect(logger).to receive(:error).with(hash_including(import_jid: import_state.jid))
      end

      subject.error(error)
    end

58 59
    it 'calls the error logger without a backtrace' do
      expect(subject).to receive(:log_error).with(message: error.message)
60 61 62 63

      subject.error(error)
    end

64 65 66
    it 'calls the error logger with the full message' do
      backtrace = caller
      allow(error).to receive(:backtrace).and_return(caller)
67

68
      expect(subject).to receive(:log_error).with(message: error.message, error_backtrace: Gitlab::Profiler.clean_backtrace(backtrace))
69 70 71 72 73

      subject.error(error)
    end
  end
end