transfer_spec.rb 1.94 KB
Newer Older
1 2 3
require 'spec_helper'

describe Gitlab::Geo::Transfer do
4 5 6 7 8
  include ::EE::GeoHelpers

  set(:primary_node) { create(:geo_node, :primary) }
  set(:secondary_node) { create(:geo_node) }
  set(:lfs_object) { create(:lfs_object, :with_file) }
9
  let(:url) { primary_node.geo_transfers_url(:lfs, lfs_object.id.to_s) }
10
  let(:content) { SecureRandom.random_bytes(10) }
11 12 13 14 15 16 17 18 19
  let(:size) { File.stat(lfs_object.file.path).size }

  subject do
    described_class.new(:lfs,
                        lfs_object.id,
                        lfs_object.file.path,
                        { sha256: lfs_object.oid })
  end

20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
  context '#download_from_primary' do
    before do
      stub_current_geo_node(secondary_node)
    end

    it 'when the destination filename is a directory' do
      transfer = described_class.new(:lfs, lfs_object.id, '/tmp', { sha256: lfs_object.id })

      expect(transfer.download_from_primary).to eq(nil)
    end

    it 'when the HTTP response is successful' do
      expect(FileUtils).to receive(:mv).with(anything, lfs_object.file.path).and_call_original
      response = double(success?: true)
      expect(HTTParty).to receive(:get).and_yield(content.to_s).and_return(response)

      expect(subject.download_from_primary).to eq(size)
Stan Hu's avatar
Stan Hu committed
37 38 39
      stat = File.stat(lfs_object.file.path)
      expect(stat.size).to eq(size)
      expect(stat.mode & 0777).to eq(0666 - File.umask)
40 41
      expect(File.binread(lfs_object.file.path)).to eq(content)
    end
42

43 44 45 46
    it 'when the HTTP response is unsuccessful' do
      expect(FileUtils).not_to receive(:mv).with(anything, lfs_object.file.path).and_call_original
      response = double(success?: false, code: 404, msg: 'No such file')
      expect(HTTParty).to receive(:get).and_return(response)
47

48 49
      expect(subject.download_from_primary).to eq(-1)
    end
50 51 52 53 54 55

    it 'when Tempfile fails' do
      expect(Tempfile).to receive(:new).and_raise(Errno::ENAMETOOLONG)

      expect(subject.download_from_primary).to eq(nil)
    end
56 57
  end
end