summaryrefslogtreecommitdiff
path: root/spec/services/projects/git_deduplication_service_spec.rb
blob: 9e6279da7de71e58751ad14fa158cee7bcef0387 (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# 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

          context 'when visibility level of the project' do
            before do
              allow(pool.source_project).to receive(:repository_access_level).and_return(ProjectFeature::ENABLED)
            end

            context 'is private' do
              it 'does not call fetch' do
                allow(pool.source_project).to receive(:visibility_level).and_return(Gitlab::VisibilityLevel::PRIVATE)
                expect(pool.object_pool).not_to receive(:fetch)

                service.execute
              end
            end

            context 'is public' do
              it 'calls fetch' do
                allow(pool.source_project).to receive(:visibility_level).and_return(Gitlab::VisibilityLevel::PUBLIC)
                expect(pool.object_pool).to receive(:fetch)

                service.execute
              end
            end

            context 'is internal' do
              it 'calls fetch' do
                allow(pool.source_project).to receive(:visibility_level).and_return(Gitlab::VisibilityLevel::INTERNAL)
                expect(pool.object_pool).to receive(:fetch)

                service.execute
              end
            end
          end

          context 'when the repository access level' do
            before do
              allow(pool.source_project).to receive(:visibility_level).and_return(Gitlab::VisibilityLevel::PUBLIC)
            end

            context 'is private' do
              it 'does not call fetch' do
                allow(pool.source_project).to receive(:repository_access_level).and_return(ProjectFeature::PRIVATE)

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

                service.execute
              end
            end

            context 'is greater than private' do
              it 'calls fetch' do
                allow(pool.source_project).to receive(:repository_access_level).and_return(ProjectFeature::PUBLIC)

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

                service.execute
              end
            end
          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