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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
|
# frozen_string_literal: true
require 'rake_helper'
RSpec.describe 'rake gitlab:storage:*', :silence_stdout do
before do
Rake.application.rake_require 'tasks/gitlab/storage'
stub_warn_user_is_not_gitlab
end
shared_examples "rake listing entities" do |entity_name, storage_type|
context 'limiting to 2' do
before do
stub_env('LIMIT' => 2)
end
it "lists 2 out of 3 #{storage_type.downcase} #{entity_name}" do
create_collection
expect { run_rake_task(task) }.to output(/Found 3 #{entity_name} using #{storage_type} Storage.*Displaying first 2 #{entity_name}/m).to_stdout
end
end
context "without any #{storage_type.downcase} #{entity_name.singularize}" do
it 'displays message for empty results' do
expect { run_rake_task(task) }.to output(/Found 0 #{entity_name} using #{storage_type} Storage/).to_stdout
end
end
end
shared_examples "rake entities summary" do |entity_name, storage_type|
context "with existing 3 #{storage_type.downcase} #{entity_name}" do
it "reports 3 #{storage_type.downcase} #{entity_name}" do
create_collection
expect { run_rake_task(task) }.to output(/Found 3 #{entity_name} using #{storage_type} Storage/).to_stdout
end
end
context "without any #{storage_type.downcase} #{entity_name.singularize}" do
it 'displays message for empty results' do
expect { run_rake_task(task) }.to output(/Found 0 #{entity_name} using #{storage_type} Storage/).to_stdout
end
end
end
shared_examples "make sure database is writable" do
context 'read-only database' do
it 'does nothing' do
expect(Gitlab::Database).to receive(:read_only?).and_return(true)
expect(Project).not_to receive(:with_unmigrated_storage)
expect { run_rake_task(task) }.to abort_execution.with_message(/This task requires database write access. Exiting./)
end
end
end
shared_examples "handles custom BATCH env var" do |worker_klass|
context 'in batches of 1' do
before do
stub_env('BATCH' => 1)
end
it "enqueues one #{worker_klass} per project" do
projects.each do |project|
expect(worker_klass).to receive(:perform_async).with(project.id, project.id)
end
run_rake_task(task)
end
end
context 'in batches of 2' do
before do
stub_env('BATCH' => 2)
end
it "enqueues one #{worker_klass} per 2 projects" do
projects.map(&:id).sort.each_slice(2) do |first, last|
last ||= first
expect(worker_klass).to receive(:perform_async).with(first, last)
end
run_rake_task(task)
end
end
end
shared_examples 'wait until database is ready' do
it 'checks if the database is ready once' do
expect(ApplicationRecord.database).to receive(:exists?).once
run_rake_task(task)
end
context 'handles custom env vars' do
before do
stub_env('MAX_DATABASE_CONNECTION_CHECKS' => 3)
stub_env('MAX_DATABASE_CONNECTION_INTERVAL' => 0.1)
end
it 'tries for 3 times, polling every 0.1 seconds' do
expect(ApplicationRecord.database).to receive(:exists?).exactly(3).times.and_return(false)
run_rake_task(task)
end
end
end
describe 'gitlab:storage:migrate_to_hashed' do
let(:task) { 'gitlab:storage:migrate_to_hashed' }
context 'with rollback already scheduled', :redis do
it 'does nothing' do
Sidekiq::Testing.disable! do
::HashedStorage::RollbackerWorker.perform_async(1, 5)
expect(Project).not_to receive(:with_unmigrated_storage)
expect { run_rake_task(task) }.to abort_execution.with_message(/There is already a rollback operation in progress/)
end
end
end
context 'with 0 legacy projects' do
it 'does nothing' do
expect(::HashedStorage::MigratorWorker).not_to receive(:perform_async)
expect { run_rake_task(task) }.to abort_execution.with_message('There are no projects requiring storage migration. Nothing to do!')
end
end
context 'with 3 legacy projects' do
let(:projects) { create_list(:project, 3, :legacy_storage) }
it 'enqueues migrations and count projects correctly' do
projects.map(&:id).sort.tap do |ids|
stub_env('ID_FROM', ids[0])
stub_env('ID_TO', ids[1])
end
expect { run_rake_task(task) }.to output(/Enqueuing migration of 2 projects in batches/).to_stdout
end
it_behaves_like 'handles custom BATCH env var', ::HashedStorage::MigratorWorker
end
context 'with same id in range' do
it 'displays message when project cant be found' do
stub_env('ID_FROM', non_existing_record_id)
stub_env('ID_TO', non_existing_record_id)
expect { run_rake_task(task) }.to abort_execution.with_message(/There are no projects requiring storage migration with ID=#{non_existing_record_id}/)
end
it 'displays a message when project exists but its already migrated' do
project = create(:project)
stub_env('ID_FROM', project.id)
stub_env('ID_TO', project.id)
expect { run_rake_task(task) }.to abort_execution.with_message(/There are no projects requiring storage migration with ID=#{project.id}/)
end
it 'enqueues migration when project can be found' do
project = create(:project, :legacy_storage)
stub_env('ID_FROM', project.id)
stub_env('ID_TO', project.id)
expect { run_rake_task(task) }.to output(/Enqueueing storage migration .* \(ID=#{project.id}\)/).to_stdout
end
end
end
describe 'gitlab:storage:rollback_to_legacy' do
let(:task) { 'gitlab:storage:rollback_to_legacy' }
it_behaves_like 'make sure database is writable'
context 'with migration already scheduled', :redis do
it 'does nothing' do
Sidekiq::Testing.disable! do
::HashedStorage::MigratorWorker.perform_async(1, 5)
expect(Project).not_to receive(:with_unmigrated_storage)
expect { run_rake_task(task) }.to abort_execution.with_message(/There is already a migration operation in progress/)
end
end
end
context 'with 0 hashed projects' do
it 'does nothing' do
expect(::HashedStorage::RollbackerWorker).not_to receive(:perform_async)
expect { run_rake_task(task) }.to abort_execution.with_message('There are no projects that can have storage rolledback. Nothing to do!')
end
end
context 'with 3 hashed projects' do
let(:projects) { create_list(:project, 3) }
it 'enqueues migrations and count projects correctly' do
projects.map(&:id).sort.tap do |ids|
stub_env('ID_FROM', ids[0])
stub_env('ID_TO', ids[1])
end
expect { run_rake_task(task) }.to output(/Enqueuing rollback of 2 projects in batches/).to_stdout
end
it_behaves_like "handles custom BATCH env var", ::HashedStorage::RollbackerWorker
end
end
describe 'gitlab:storage:legacy_projects' do
it_behaves_like 'rake entities summary', 'projects', 'Legacy' do
let(:task) { 'gitlab:storage:legacy_projects' }
let(:create_collection) { create_list(:project, 3, :legacy_storage) }
end
it_behaves_like 'wait until database is ready' do
let(:task) { 'gitlab:storage:legacy_projects' }
end
end
describe 'gitlab:storage:list_legacy_projects' do
it_behaves_like 'rake listing entities', 'projects', 'Legacy' do
let(:task) { 'gitlab:storage:list_legacy_projects' }
let(:create_collection) { create_list(:project, 3, :legacy_storage) }
end
end
describe 'gitlab:storage:hashed_projects' do
it_behaves_like 'rake entities summary', 'projects', 'Hashed' do
let(:task) { 'gitlab:storage:hashed_projects' }
let(:create_collection) { create_list(:project, 3, storage_version: 1) }
end
end
describe 'gitlab:storage:list_hashed_projects' do
it_behaves_like 'rake listing entities', 'projects', 'Hashed' do
let(:task) { 'gitlab:storage:list_hashed_projects' }
let(:create_collection) { create_list(:project, 3, storage_version: 1) }
end
end
describe 'gitlab:storage:legacy_attachments' do
it_behaves_like 'rake entities summary', 'attachments', 'Legacy' do
let(:task) { 'gitlab:storage:legacy_attachments' }
let(:project) { create(:project, storage_version: 1) }
let(:create_collection) { create_list(:upload, 3, model: project) }
end
it_behaves_like 'wait until database is ready' do
let(:task) { 'gitlab:storage:legacy_attachments' }
end
end
describe 'gitlab:storage:list_legacy_attachments' do
it_behaves_like 'rake listing entities', 'attachments', 'Legacy' do
let(:task) { 'gitlab:storage:list_legacy_attachments' }
let(:project) { create(:project, storage_version: 1) }
let(:create_collection) { create_list(:upload, 3, model: project) }
end
end
describe 'gitlab:storage:hashed_attachments' do
it_behaves_like 'rake entities summary', 'attachments', 'Hashed' do
let(:task) { 'gitlab:storage:hashed_attachments' }
let(:project) { create(:project) }
let(:create_collection) { create_list(:upload, 3, model: project) }
end
end
describe 'gitlab:storage:list_hashed_attachments' do
it_behaves_like 'rake listing entities', 'attachments', 'Hashed' do
let(:task) { 'gitlab:storage:list_hashed_attachments' }
let(:project) { create(:project) }
let(:create_collection) { create_list(:upload, 3, model: project) }
end
end
end
|