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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::BackgroundMigration::BackfillSnippetRepositories, :migration, schema: 2021_03_13_045845 do
let(:gitlab_shell) { Gitlab::Shell.new }
let(:users) { table(:users) }
let(:snippets) { table(:snippets) }
let(:snippet_repositories) { table(:snippet_repositories) }
let(:user_state) { 'active' }
let(:user_type) { nil }
let(:user_name) { 'Test' }
let!(:user) do
users.create!(id: 1,
email: 'user@example.com',
projects_limit: 10,
username: 'test',
name: user_name,
state: user_state,
last_activity_on: 1.minute.ago,
user_type: user_type,
confirmed_at: 1.day.ago)
end
let!(:migration_bot) do
users.create!(id: 100,
email: "noreply+gitlab-migration-bot%s@#{Settings.gitlab.host}",
user_type: HasUserType::USER_TYPES[:migration_bot],
name: 'GitLab Migration Bot',
projects_limit: 10,
username: 'bot')
end
let!(:snippet_with_repo) { snippets.create!(id: 1, type: 'PersonalSnippet', author_id: user.id, file_name: file_name, content: content) }
let!(:snippet_with_empty_repo) { snippets.create!(id: 2, type: 'PersonalSnippet', author_id: user.id, file_name: file_name, content: content) }
let!(:snippet_without_repo) { snippets.create!(id: 3, type: 'PersonalSnippet', author_id: user.id, file_name: file_name, content: content) }
let(:file_name) { 'file_name.rb' }
let(:content) { 'content' }
let(:ids) { snippets.pluck('MIN(id)', 'MAX(id)').first }
let(:service) { described_class.new }
subject { service.perform(*ids) }
before do
allow(snippet_with_repo).to receive(:disk_path).and_return(disk_path(snippet_with_repo))
raw_repository(snippet_with_repo).create_from_bundle(TestEnv.factory_repo_bundle_path)
raw_repository(snippet_with_empty_repo).create_repository
end
after do
raw_repository(snippet_with_repo).remove
raw_repository(snippet_without_repo).remove
raw_repository(snippet_with_empty_repo).remove
end
describe '#perform' do
it 'logs successful migrated snippets' do
expect_next_instance_of(Gitlab::BackgroundMigration::Logger) do |instance|
expect(instance).to receive(:info).exactly(3).times
end
subject
end
context 'when snippet has a non empty repository' do
it 'does not perform any action' do
expect(service).not_to receive(:create_repository_and_files).with(snippet_with_repo)
subject
end
end
shared_examples 'migration_bot user commits files' do
before do
allow(Gitlab::CurrentSettings).to receive(:default_branch_name).and_return('main')
end
it do
subject
last_commit = raw_repository(snippet).commit
expect(last_commit.author_name).to eq migration_bot.name
expect(last_commit.author_email).to eq migration_bot.email
end
end
shared_examples 'commits the file to the repository' do
before do
allow(Gitlab::CurrentSettings).to receive(:default_branch_name).and_return('main')
end
context 'when author can update snippet and use git' do
it 'creates the repository and commit the file' do
subject
blob = blob_at(snippet, file_name)
last_commit = raw_repository(snippet).commit
aggregate_failures do
expect(blob).to be
expect(blob.data).to eq content
expect(last_commit.author_name).to eq user.name
expect(last_commit.author_email).to eq user.email
end
end
end
context 'when author cannot update snippet or use git' do
context 'when user is blocked' do
let(:user_state) { 'blocked' }
it_behaves_like 'migration_bot user commits files'
end
context 'when user is deactivated' do
let(:user_state) { 'deactivated' }
it_behaves_like 'migration_bot user commits files'
end
context 'when user is a ghost' do
let(:user_type) { HasUserType::USER_TYPES[:ghost] }
it_behaves_like 'migration_bot user commits files'
end
end
end
context 'when snippet has an empty repo' do
before do
expect(repository_exists?(snippet_with_empty_repo)).to be_truthy
end
it_behaves_like 'commits the file to the repository' do
let(:snippet) { snippet_with_empty_repo }
end
end
context 'when snippet does not have a repository' do
it 'creates the repository' do
expect { subject }.to change { repository_exists?(snippet_without_repo) }.from(false).to(true)
end
it_behaves_like 'commits the file to the repository' do
let(:snippet) { snippet_without_repo }
end
end
context 'when an error is raised' do
before do
allow(service).to receive(:create_commit).and_raise(StandardError)
end
it 'logs errors' do
expect_next_instance_of(Gitlab::BackgroundMigration::Logger) do |instance|
expect(instance).to receive(:error).exactly(3).times
end
subject
end
it "retries #{described_class::MAX_RETRIES} times the operation if it fails" do
expect(service).to receive(:create_commit).exactly(snippets.count * described_class::MAX_RETRIES).times
subject
end
it 'destroys the snippet repository' do
expect(service).to receive(:destroy_snippet_repository).exactly(3).times.and_call_original
subject
expect(snippet_repositories.count).to eq 0
end
it 'deletes the repository on disk' do
subject
aggregate_failures do
expect(repository_exists?(snippet_with_repo)).to be_falsey
expect(repository_exists?(snippet_without_repo)).to be_falsey
expect(repository_exists?(snippet_with_empty_repo)).to be_falsey
end
end
end
context 'with invalid file names' do
using RSpec::Parameterized::TableSyntax
where(:invalid_file_name, :converted_file_name) do
'filename.js // with comment' | 'filename-js-with-comment'
'.git/hooks/pre-commit' | 'git-hooks-pre-commit'
'https://gitlab.com' | 'https-gitlab-com'
'html://web.title%mp4/mpg/mpeg.net' | 'html-web-title-mp4-mpg-mpeg-net'
'../../etc/passwd' | 'etc-passwd'
'.' | 'snippetfile1.txt'
end
with_them do
let!(:snippet_with_invalid_path) { snippets.create!(id: 4, type: 'PersonalSnippet', author_id: user.id, file_name: invalid_file_name, content: content) }
let!(:snippet_with_valid_path) { snippets.create!(id: 5, type: 'PersonalSnippet', author_id: user.id, file_name: file_name, content: content) }
let(:ids) { [4, 5] }
after do
raw_repository(snippet_with_invalid_path).remove
raw_repository(snippet_with_valid_path).remove
end
it 'checks for file path errors when errors are raised' do
expect(service).to receive(:set_file_path_error).once.and_call_original
subject
end
it 'converts invalid filenames' do
subject
expect(blob_at(snippet_with_invalid_path, converted_file_name)).to be
end
it 'does not convert valid filenames on subsequent migrations' do
subject
expect(blob_at(snippet_with_valid_path, file_name)).to be
end
end
end
context 'when snippet content size is higher than the existing limit' do
let(:limit) { 15 }
let(:content) { 'a' * (limit + 1) }
let(:snippet) { snippet_without_repo }
let(:ids) { [snippet.id, snippet.id] }
before do
allow(Gitlab::CurrentSettings).to receive(:snippet_size_limit).and_return(limit)
end
it_behaves_like 'migration_bot user commits files'
end
context 'when user name is invalid' do
let(:user_name) { '.' }
let!(:snippet) { snippets.create!(id: 4, type: 'PersonalSnippet', author_id: user.id, file_name: file_name, content: content) }
let(:ids) { [4, 4] }
after do
raw_repository(snippet).remove
end
it_behaves_like 'migration_bot user commits files'
end
context 'when both user name and snippet file_name are invalid' do
let(:user_name) { '.' }
let!(:other_user) do
users.create!(id: 2,
email: 'user2@example.com',
projects_limit: 10,
username: 'test2',
name: 'Test2',
state: user_state,
last_activity_on: 1.minute.ago,
user_type: user_type,
confirmed_at: 1.day.ago)
end
let!(:invalid_snippet) { snippets.create!(id: 4, type: 'PersonalSnippet', author_id: user.id, file_name: '.', content: content) }
let!(:snippet) { snippets.create!(id: 5, type: 'PersonalSnippet', author_id: other_user.id, file_name: file_name, content: content) }
let(:ids) { [4, 5] }
before do
allow(Gitlab::CurrentSettings).to receive(:default_branch_name).and_return('main')
end
after do
raw_repository(snippet).remove
raw_repository(invalid_snippet).remove
end
it 'updates the file_name only when it is invalid' do
subject
expect(blob_at(invalid_snippet, 'snippetfile1.txt')).to be
expect(blob_at(snippet, file_name)).to be
end
it_behaves_like 'migration_bot user commits files' do
let(:snippet) { invalid_snippet }
end
it 'does not alter the commit author in subsequent migrations' do
subject
last_commit = raw_repository(snippet).commit
expect(last_commit.author_name).to eq other_user.name
expect(last_commit.author_email).to eq other_user.email
end
it "increases the number of retries temporarily from #{described_class::MAX_RETRIES} to #{described_class::MAX_RETRIES + 1}" do
expect(service).to receive(:create_commit).with(Snippet.find(invalid_snippet.id)).exactly(described_class::MAX_RETRIES + 1).times.and_call_original
expect(service).to receive(:create_commit).with(Snippet.find(snippet.id)).once.and_call_original
subject
end
end
end
def blob_at(snippet, path)
raw_repository(snippet).blob_at('main', path)
end
def repository_exists?(snippet)
gitlab_shell.repository_exists?('default', "#{disk_path(snippet)}.git")
end
def raw_repository(snippet)
Gitlab::Git::Repository.new('default',
"#{disk_path(snippet)}.git",
Gitlab::GlRepository::SNIPPET.identifier_for_container(snippet),
"@snippets/#{snippet.id}")
end
def hashed_repository(snippet)
Storage::Hashed.new(snippet, prefix: '@snippets')
end
def disk_path(snippet)
hashed_repository(snippet).disk_path
end
def ls_files(snippet)
raw_repository(snippet).ls_files(snippet.default_branch)
end
end
|