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

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

require 'spec_helper'

RSpec.describe Gitlab::Git::ObjectPool do
  let(:pool_repository) { create(:pool_repository) }
  let(:source_repository) { pool_repository.source_project.repository }

  subject { pool_repository.object_pool }

  describe '#storage' do
    it "equals the pool repository's shard name" do
      expect(subject.storage).not_to be_nil
      expect(subject.storage).to eq(pool_repository.shard_name)
    end
  end

  describe '#create' do
    before do
      subject.create # rubocop:disable Rails/SaveBang
    end

    context "when the pool doesn't exist yet" do
      it 'creates the pool' do
        expect(subject.exists?).to be(true)
      end
    end

    context 'when the pool already exists' do
      it 'raises an FailedPrecondition' do
        expect do
          subject.create # rubocop:disable Rails/SaveBang
        end.to raise_error(GRPC::FailedPrecondition)
      end
    end
  end

  describe '#exists?' do
    context "when the object pool doesn't exist" do
      it 'returns false' do
        expect(subject.exists?).to be(false)
      end
    end

    context 'when the object pool exists' do
      let(:pool) { create(:pool_repository, :ready) }

      subject { pool.object_pool }

      it 'returns true' do
        expect(subject.exists?).to be(true)
      end
    end
  end

  describe '#link' do
    let!(:pool_repository) { create(:pool_repository, :ready) }

    context 'when linked for the first time' do
      it 'sets a remote' do
        expect do
          subject.link(source_repository)
        end.not_to raise_error
      end
    end

    context 'when the remote is already set' do
      before do
        subject.link(source_repository)
      end

      it "doesn't raise an error" do
        expect do
          subject.link(source_repository)
        end.not_to raise_error
      end
    end
  end

  describe '#fetch' do
    context 'when the object pool repository exists' do
      let!(:pool_repository) { create(:pool_repository, :ready) }

      context 'without changes' do
        it 'does not raise an error' do
          expect { subject.fetch }.not_to raise_error
        end
      end

      context 'with new commit in source repository' do
        let(:branch_name) { Gitlab::Git::Ref.extract_branch_name(source_repository.root_ref) }
        let(:source_ref_name) { "refs/heads/#{branch_name}" }
        let(:pool_ref_name) { "refs/remotes/origin/heads/#{branch_name}" }

        let(:new_commit_id) do
          source_repository.create_file(
            pool_repository.source_project.owner,
            'a.file',
            'This is a file',
            branch_name: branch_name,
            message: 'Add a file'
          )
        end

        it 'fetches objects from the source repository' do
          # Sanity-check that the commit does not yet exist in the pool repository.
          expect(subject.repository.commit(new_commit_id)).to be_nil

          subject.fetch

          expect(subject.repository.commit(pool_ref_name).id).to eq(new_commit_id)
          expect(subject.repository.commit_count(pool_ref_name))
            .to eq(source_repository.raw_repository.commit_count(source_ref_name))
        end
      end
    end
  end
end