summaryrefslogtreecommitdiff
path: root/spec/services/projects/git_deduplication_service_spec.rb
blob: 3acbc46b473ef81fd4f582b79ccbb90441742162 (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
# frozen_string_literal: true

require 'spec_helper'

describe Projects::GitDeduplicationService do
  include ExclusiveLeaseHelpers

  let(:pool) { create(:pool_repository, :ready) }
  let(:project) { create(:project, :repository) }
  let(:lease_key) { "git_deduplication:#{project.id}" }
  let(:lease_timeout) { Projects::GitDeduplicationService::LEASE_TIMEOUT }

  subject(:service) { described_class.new(project) }

  describe '#execute' do
    context 'when there is not already a lease' do
      context 'when the project does not have a pool repository' do
        it 'calls disconnect_git_alternates' do
          stub_exclusive_lease(lease_key, timeout: lease_timeout)

          expect(project.repository).to receive(:disconnect_alternates)

          service.execute
        end
      end

      context 'when the project has a pool repository' do
        let(:project) { create(:project, :repository, pool_repository: pool) }

        context 'when the project is a source project' do
          let(:lease_key) { "git_deduplication:#{pool.source_project.id}" }

          subject(:service) { described_class.new(pool.source_project) }

          it 'calls fetch' do
            stub_exclusive_lease(lease_key, timeout: lease_timeout)
            allow(pool.source_project).to receive(:git_objects_poolable?).and_return(true)

            expect(pool.object_pool).to receive(:fetch)

            service.execute
          end

          it 'does not call fetch if git objects are not poolable' do
            stub_exclusive_lease(lease_key, timeout: lease_timeout)
            allow(pool.source_project).to receive(:git_objects_poolable?).and_return(false)

            expect(pool.object_pool).not_to receive(:fetch)

            service.execute
          end

          it 'does not call fetch if pool and project are not on the same storage' do
            stub_exclusive_lease(lease_key, timeout: lease_timeout)
            allow(pool.source_project.repository).to receive(:storage).and_return('special_storage_001')

            expect(pool.object_pool).not_to receive(:fetch)

            service.execute
          end
        end

        it 'links the repository to the object pool' do
          expect(project).to receive(:link_pool_repository)

          service.execute
        end

        it 'does not link the repository to the object pool if they are not on the same storage' do
          allow(project.repository).to receive(:storage).and_return('special_storage_001')
          expect(project).not_to receive(:link_pool_repository)

          service.execute
        end
      end

      context 'when a lease is already out' do
        before do
          stub_exclusive_lease_taken(lease_key, timeout: lease_timeout)
        end

        it 'fails when a lease is already out' do
          expect(service).to receive(:log_error).with('Cannot obtain an exclusive lease. There must be another instance already in execution.')

          service.execute
        end
      end
    end
  end
end