summaryrefslogtreecommitdiff
path: root/spec/support/shared_examples/workers/concerns/git_garbage_collect_methods_shared_examples.rb
blob: 503e331ea2ecdc6b23b57166b61d5ba845122c86 (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# frozen_string_literal: true

RSpec.shared_examples 'can collect git garbage' do |update_statistics: true|
  let!(:lease_uuid) { SecureRandom.uuid }
  let!(:lease_key) { "resource_housekeeping:#{resource.id}" }
  let(:params) { [resource.id, task, lease_key, lease_uuid] }
  let(:shell) { Gitlab::Shell.new }
  let(:repository) { resource.repository }
  let(:statistics_service_klass) { nil }

  subject { described_class.new }

  before do
    allow(subject).to receive(:find_resource).and_return(resource)
  end

  shared_examples 'it calls Gitaly' do
    let(:repository_service) { instance_double(Gitlab::GitalyClient::RepositoryService) }

    specify do
      expect_next_instance_of(Gitlab::GitalyClient::RepositoryService, repository.raw_repository) do |instance|
        expect(instance).to receive(:optimize_repository).and_call_original
      end

      subject.perform(*params)
    end

    context 'when optimized_housekeeping feature is disabled' do
      before do
        stub_feature_flags(optimized_housekeeping: false)
      end

      specify do
        expect(subject).to receive(:get_gitaly_client).with(task, repository.raw_repository).and_return(repository_service)
        expect(repository_service).to receive(gitaly_task)

        subject.perform(*params)
      end
    end
  end

  shared_examples 'it updates the resource statistics' do
    it 'updates the resource statistics' do
      expect_next_instance_of(statistics_service_klass, anything, nil, statistics: statistics_keys) do |service|
        expect(service).to receive(:execute)
      end

      subject.perform(*params)
    end

    it 'does nothing if the database is read-only' do
      allow(Gitlab::Database).to receive(:read_only?) { true }

      expect(statistics_service_klass).not_to receive(:new)

      subject.perform(*params)
    end
  end

  describe '#perform', :aggregate_failures do
    let(:gitaly_task) { :garbage_collect }
    let(:task) { :gc }

    context 'with active lease_uuid' do
      before do
        allow(subject).to receive(:get_lease_uuid).and_return(lease_uuid)
      end

      it_behaves_like 'it calls Gitaly'
      it_behaves_like 'it updates the resource statistics' if update_statistics

      it "flushes ref caches when the task if 'gc'" do
        expect(subject).to receive(:renew_lease).with(lease_key, lease_uuid).and_call_original
        expect(repository).to receive(:expire_branches_cache).and_call_original
        expect(repository).to receive(:branch_names).and_call_original
        expect(repository).to receive(:has_visible_content?).and_call_original
        expect(repository.raw_repository).to receive(:has_visible_content?).and_call_original

        subject.perform(*params)
      end

      it 'handles gRPC errors' do
        repository_service = instance_double(Gitlab::GitalyClient::RepositoryService)

        allow_next_instance_of(Projects::GitDeduplicationService) do |instance|
          allow(instance).to receive(:execute)
        end

        allow(repository.raw_repository).to receive(:gitaly_repository_client).and_return(repository_service)
        allow(repository_service).to receive(:optimize_repository).and_raise(GRPC::NotFound)

        expect { subject.perform(*params) }.to raise_exception(Gitlab::Git::Repository::NoRepository)
      end

      context 'when optimized_housekeeping feature flag is disabled' do
        before do
          stub_feature_flags(optimized_housekeeping: false)
        end

        it 'handles gRPC errors' do
          allow_next_instance_of(Gitlab::GitalyClient::RepositoryService, repository.raw_repository) do |instance|
            allow(instance).to receive(:garbage_collect).and_raise(GRPC::NotFound)
          end

          expect { subject.perform(*params) }.to raise_exception(Gitlab::Git::Repository::NoRepository)
        end
      end
    end

    context 'with different lease than the active one' do
      before do
        allow(subject).to receive(:get_lease_uuid).and_return(SecureRandom.uuid)
      end

      it 'returns silently' do
        expect(repository).not_to receive(:expire_branches_cache).and_call_original
        expect(repository).not_to receive(:branch_names).and_call_original
        expect(repository).not_to receive(:has_visible_content?).and_call_original

        subject.perform(*params)
      end
    end

    context 'with no active lease' do
      let(:params) { [resource.id] }

      before do
        allow(subject).to receive(:get_lease_uuid).and_return(false)
      end

      context 'when is able to get the lease' do
        before do
          allow(subject).to receive(:try_obtain_lease).and_return(SecureRandom.uuid)
        end

        it_behaves_like 'it calls Gitaly'
        it_behaves_like 'it updates the resource statistics' if update_statistics

        it "flushes ref caches when the task if 'gc'" do
          expect(subject).to receive(:get_lease_uuid).with("git_gc:#{task}:#{expected_default_lease}").and_return(false)
          expect(repository).to receive(:expire_branches_cache).and_call_original
          expect(repository).to receive(:branch_names).and_call_original
          expect(repository).to receive(:has_visible_content?).and_call_original
          expect(repository.raw_repository).to receive(:has_visible_content?).and_call_original

          subject.perform(*params)
        end
      end

      context 'when no lease can be obtained' do
        it 'returns silently' do
          expect(subject).to receive(:try_obtain_lease).and_return(false)

          expect(subject).not_to receive(:command)
          expect(repository).not_to receive(:expire_branches_cache).and_call_original
          expect(repository).not_to receive(:branch_names).and_call_original
          expect(repository).not_to receive(:has_visible_content?).and_call_original

          subject.perform(*params)
        end
      end
    end

    context 'repack_full' do
      let(:task) { :full_repack }
      let(:gitaly_task) { :repack_full }

      before do
        expect(subject).to receive(:get_lease_uuid).and_return(lease_uuid)
      end

      it_behaves_like 'it calls Gitaly'
      it_behaves_like 'it updates the resource statistics' if update_statistics
    end

    context 'pack_refs' do
      let(:task) { :pack_refs }
      let(:gitaly_task) { :pack_refs }

      before do
        expect(subject).to receive(:get_lease_uuid).and_return(lease_uuid)
      end

      it_behaves_like 'it calls Gitaly' do
        let(:repository_service) { instance_double(Gitlab::GitalyClient::RefService) }
      end

      it 'does not update the resource statistics' do
        expect(statistics_service_klass).not_to receive(:new)

        subject.perform(*params)
      end
    end

    context 'repack_incremental' do
      let(:task) { :incremental_repack }
      let(:gitaly_task) { :repack_incremental }

      before do
        expect(subject).to receive(:get_lease_uuid).and_return(lease_uuid)

        statistics_keys.delete(:repository_size)
      end

      it_behaves_like 'it calls Gitaly'
      it_behaves_like 'it updates the resource statistics' if update_statistics
    end

    context 'prune' do
      before do
        expect(subject).to receive(:get_lease_uuid).and_return(lease_uuid)
      end

      specify do
        expect_next_instance_of(Gitlab::GitalyClient::RepositoryService, repository.raw_repository) do |instance|
          expect(instance).to receive(:prune_unreachable_objects).and_call_original
        end

        subject.perform(resource.id, 'prune', lease_key, lease_uuid)
      end
    end

    shared_examples 'gc tasks' do
      before do
        allow(subject).to receive(:get_lease_uuid).and_return(lease_uuid)
        allow(subject).to receive(:bitmaps_enabled?).and_return(bitmaps_enabled)

        stub_feature_flags(optimized_housekeeping: false)
      end

      it 'cleans up repository after finishing' do
        expect(resource).to receive(:cleanup).and_call_original

        subject.perform(resource.id, 'gc', lease_key, lease_uuid)
      end

      it 'prune calls garbage_collect with the option prune: true' do
        repository_service = instance_double(Gitlab::GitalyClient::RepositoryService)

        expect(subject).to receive(:get_gitaly_client).with(:prune, repository.raw_repository).and_return(repository_service)
        expect(repository_service).to receive(:garbage_collect).with(bitmaps_enabled, prune: true)

        subject.perform(resource.id, 'prune', lease_key, lease_uuid)
      end
    end

    context 'with bitmaps enabled' do
      let(:bitmaps_enabled) { true }

      include_examples 'gc tasks'
    end

    context 'with bitmaps disabled' do
      let(:bitmaps_enabled) { false }

      include_examples 'gc tasks'
    end
  end
end