2017-11-08 03:08:02 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2018-02-15 21:20:55 +00:00
|
|
|
# Rollback DB to 10.5 (later than this was originally written for) because it still needs to work.
|
|
|
|
describe Gitlab::BackgroundMigration::PopulateUntrackedUploads, :sidekiq, :migration, schema: 20180208183958 do
|
2017-11-15 07:15:30 +00:00
|
|
|
include TrackUntrackedUploadsHelpers
|
|
|
|
|
2017-11-15 10:36:25 +00:00
|
|
|
subject { described_class.new }
|
|
|
|
|
2018-02-15 21:20:55 +00:00
|
|
|
let!(:appearances) { table(:appearances) }
|
|
|
|
let!(:namespaces) { table(:namespaces) }
|
|
|
|
let!(:notes) { table(:notes) }
|
|
|
|
let!(:projects) { table(:projects) }
|
|
|
|
let!(:routes) { table(:routes) }
|
|
|
|
let!(:untracked_files_for_uploads) { table(:untracked_files_for_uploads) }
|
|
|
|
let!(:uploads) { table(:uploads) }
|
|
|
|
let!(:users) { table(:users) }
|
2017-11-08 03:08:02 +00:00
|
|
|
|
2017-11-15 10:36:25 +00:00
|
|
|
before do
|
2017-11-15 13:19:07 +00:00
|
|
|
ensure_temporary_tracking_table_exists
|
2017-12-06 20:20:06 +00:00
|
|
|
uploads.delete_all
|
2017-11-15 13:19:07 +00:00
|
|
|
end
|
|
|
|
|
2017-11-10 01:17:56 +00:00
|
|
|
context 'with untracked files and tracked files in untracked_files_for_uploads' do
|
2018-02-15 21:20:55 +00:00
|
|
|
let!(:appearance) { create_or_update_appearance(logo: true, header_logo: true) }
|
|
|
|
let!(:user1) { create_user(avatar: true) }
|
|
|
|
let!(:user2) { create_user(avatar: true) }
|
|
|
|
let!(:project1) { create_project(avatar: true) }
|
|
|
|
let!(:project2) { create_project(avatar: true) }
|
2017-11-08 03:08:02 +00:00
|
|
|
|
2017-11-15 07:15:30 +00:00
|
|
|
before do
|
2018-02-15 21:20:55 +00:00
|
|
|
add_markdown_attachment(project1)
|
|
|
|
add_markdown_attachment(project2)
|
2017-11-08 03:08:02 +00:00
|
|
|
|
2017-11-10 01:17:56 +00:00
|
|
|
# File records created by PrepareUntrackedUploads
|
2018-02-15 21:20:55 +00:00
|
|
|
untracked_files_for_uploads.create!(path: get_uploads(appearance, 'Appearance').first.path)
|
|
|
|
untracked_files_for_uploads.create!(path: get_uploads(appearance, 'Appearance').last.path)
|
|
|
|
untracked_files_for_uploads.create!(path: get_uploads(user1, 'User').first.path)
|
|
|
|
untracked_files_for_uploads.create!(path: get_uploads(user2, 'User').first.path)
|
|
|
|
untracked_files_for_uploads.create!(path: get_uploads(project1, 'Project').first.path)
|
|
|
|
untracked_files_for_uploads.create!(path: get_uploads(project2, 'Project').first.path)
|
2018-02-15 22:22:38 +00:00
|
|
|
untracked_files_for_uploads.create!(path: "#{legacy_project_uploads_dir(project1).sub("#{TrackUntrackedUploadsHelpers::PUBLIC_DIR}/", '')}/#{get_uploads(project1, 'Project').last.path}")
|
|
|
|
untracked_files_for_uploads.create!(path: "#{legacy_project_uploads_dir(project2).sub("#{TrackUntrackedUploadsHelpers::PUBLIC_DIR}/", '')}/#{get_uploads(project2, 'Project').last.path}")
|
2017-11-08 03:08:02 +00:00
|
|
|
|
2017-11-15 07:15:30 +00:00
|
|
|
# Untrack 4 files
|
2018-02-15 21:20:55 +00:00
|
|
|
get_uploads(user2, 'User').delete_all
|
|
|
|
get_uploads(project2, 'Project').delete_all # 2 files: avatar and a Markdown upload
|
|
|
|
get_uploads(appearance, 'Appearance').where("path like '%header_logo%'").delete_all
|
2017-11-08 03:08:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds untracked files to the uploads table' do
|
|
|
|
expect do
|
2017-12-01 13:58:49 +00:00
|
|
|
subject.perform(1, untracked_files_for_uploads.reorder(:id).last.id)
|
2017-11-08 03:08:02 +00:00
|
|
|
end.to change { uploads.count }.from(4).to(8)
|
|
|
|
|
2018-02-15 21:20:55 +00:00
|
|
|
expect(get_uploads(user2, 'User').count).to eq(1)
|
|
|
|
expect(get_uploads(project2, 'Project').count).to eq(2)
|
|
|
|
expect(get_uploads(appearance, 'Appearance').count).to eq(2)
|
2017-11-08 03:08:02 +00:00
|
|
|
end
|
|
|
|
|
2017-11-22 18:44:33 +00:00
|
|
|
it 'deletes rows after processing them' do
|
2017-11-15 10:36:25 +00:00
|
|
|
expect(subject).to receive(:drop_temp_table_if_finished) # Don't drop the table so we can look at it
|
|
|
|
|
2017-11-08 04:38:17 +00:00
|
|
|
expect do
|
2017-12-05 18:43:08 +00:00
|
|
|
subject.perform(1, untracked_files_for_uploads.last.id)
|
2017-11-22 18:44:33 +00:00
|
|
|
end.to change { untracked_files_for_uploads.count }.from(8).to(0)
|
2017-11-08 04:38:17 +00:00
|
|
|
end
|
|
|
|
|
2017-11-08 03:08:02 +00:00
|
|
|
it 'does not create duplicate uploads of already tracked files' do
|
2017-12-05 18:43:08 +00:00
|
|
|
subject.perform(1, untracked_files_for_uploads.last.id)
|
2017-11-08 03:08:02 +00:00
|
|
|
|
2018-02-15 21:20:55 +00:00
|
|
|
expect(get_uploads(user1, 'User').count).to eq(1)
|
|
|
|
expect(get_uploads(project1, 'Project').count).to eq(2)
|
|
|
|
expect(get_uploads(appearance, 'Appearance').count).to eq(2)
|
2017-11-08 03:08:02 +00:00
|
|
|
end
|
2017-11-08 04:38:17 +00:00
|
|
|
|
|
|
|
it 'uses the start and end batch ids [only 1st half]' do
|
2017-11-27 04:57:51 +00:00
|
|
|
ids = untracked_files_for_uploads.all.order(:id).pluck(:id)
|
|
|
|
start_id = ids[0]
|
|
|
|
end_id = ids[3]
|
2017-11-08 04:38:17 +00:00
|
|
|
|
|
|
|
expect do
|
2017-11-15 10:36:25 +00:00
|
|
|
subject.perform(start_id, end_id)
|
2017-11-08 04:38:17 +00:00
|
|
|
end.to change { uploads.count }.from(4).to(6)
|
|
|
|
|
2018-02-15 21:20:55 +00:00
|
|
|
expect(get_uploads(user1, 'User').count).to eq(1)
|
|
|
|
expect(get_uploads(user2, 'User').count).to eq(1)
|
|
|
|
expect(get_uploads(appearance, 'Appearance').count).to eq(2)
|
|
|
|
expect(get_uploads(project1, 'Project').count).to eq(2)
|
|
|
|
expect(get_uploads(project2, 'Project').count).to eq(0)
|
2017-11-08 04:38:17 +00:00
|
|
|
|
|
|
|
# Only 4 have been either confirmed or added to uploads
|
2017-11-22 18:44:33 +00:00
|
|
|
expect(untracked_files_for_uploads.count).to eq(4)
|
2017-11-08 04:38:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'uses the start and end batch ids [only 2nd half]' do
|
2017-11-27 04:57:51 +00:00
|
|
|
ids = untracked_files_for_uploads.all.order(:id).pluck(:id)
|
|
|
|
start_id = ids[4]
|
|
|
|
end_id = ids[7]
|
2017-11-08 04:38:17 +00:00
|
|
|
|
|
|
|
expect do
|
2017-11-15 10:36:25 +00:00
|
|
|
subject.perform(start_id, end_id)
|
2017-11-08 04:38:17 +00:00
|
|
|
end.to change { uploads.count }.from(4).to(6)
|
|
|
|
|
2018-02-15 21:20:55 +00:00
|
|
|
expect(get_uploads(user1, 'User').count).to eq(1)
|
|
|
|
expect(get_uploads(user2, 'User').count).to eq(0)
|
|
|
|
expect(get_uploads(appearance, 'Appearance').count).to eq(1)
|
|
|
|
expect(get_uploads(project1, 'Project').count).to eq(2)
|
|
|
|
expect(get_uploads(project2, 'Project').count).to eq(2)
|
2017-11-08 04:38:17 +00:00
|
|
|
|
|
|
|
# Only 4 have been either confirmed or added to uploads
|
2017-11-22 18:44:33 +00:00
|
|
|
expect(untracked_files_for_uploads.count).to eq(4)
|
2017-11-08 04:38:17 +00:00
|
|
|
end
|
2017-11-15 10:36:25 +00:00
|
|
|
|
|
|
|
it 'does not drop the temporary tracking table after processing the batch, if there are still untracked rows' do
|
|
|
|
subject.perform(1, untracked_files_for_uploads.last.id - 1)
|
|
|
|
|
2017-12-06 07:08:45 +00:00
|
|
|
expect(ActiveRecord::Base.connection.table_exists?(:untracked_files_for_uploads)).to be_truthy
|
2017-11-15 10:36:25 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'drops the temporary tracking table after processing the batch, if there are no untracked rows left' do
|
2018-02-13 20:31:30 +00:00
|
|
|
expect(subject).to receive(:drop_temp_table_if_finished)
|
2017-11-15 10:36:25 +00:00
|
|
|
|
2018-02-13 20:31:30 +00:00
|
|
|
subject.perform(1, untracked_files_for_uploads.last.id)
|
2017-11-15 10:36:25 +00:00
|
|
|
end
|
2017-12-05 20:26:20 +00:00
|
|
|
|
|
|
|
it 'does not block a whole batch because of one bad path' do
|
2018-02-15 21:20:55 +00:00
|
|
|
untracked_files_for_uploads.create!(path: "#{Gitlab::BackgroundMigration::PrepareUntrackedUploads::RELATIVE_UPLOAD_DIR}/#{get_full_path(project2)}/._7d37bf4c747916390e596744117d5d1a")
|
2017-12-05 20:26:20 +00:00
|
|
|
expect(untracked_files_for_uploads.count).to eq(9)
|
|
|
|
expect(uploads.count).to eq(4)
|
|
|
|
|
|
|
|
subject.perform(1, untracked_files_for_uploads.last.id)
|
|
|
|
|
|
|
|
expect(untracked_files_for_uploads.count).to eq(1)
|
|
|
|
expect(uploads.count).to eq(8)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'an unparseable path is shown in error output' do
|
2018-02-15 21:20:55 +00:00
|
|
|
bad_path = "#{Gitlab::BackgroundMigration::PrepareUntrackedUploads::RELATIVE_UPLOAD_DIR}/#{get_full_path(project2)}/._7d37bf4c747916390e596744117d5d1a"
|
2017-12-05 20:26:20 +00:00
|
|
|
untracked_files_for_uploads.create!(path: bad_path)
|
|
|
|
|
|
|
|
expect(Rails.logger).to receive(:error).with(/Error parsing path "#{bad_path}":/)
|
|
|
|
|
|
|
|
subject.perform(1, untracked_files_for_uploads.last.id)
|
|
|
|
end
|
2017-11-08 03:08:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'with no untracked files' do
|
|
|
|
it 'does not add to the uploads table (and does not raise error)' do
|
|
|
|
expect do
|
2017-11-15 10:36:25 +00:00
|
|
|
subject.perform(1, 1000)
|
2017-11-08 03:08:02 +00:00
|
|
|
end.not_to change { uploads.count }.from(0)
|
|
|
|
end
|
|
|
|
end
|
2017-11-15 13:19:07 +00:00
|
|
|
|
2017-11-24 07:12:24 +00:00
|
|
|
describe 'upload outcomes for each path pattern' do
|
|
|
|
shared_examples_for 'non_markdown_file' do
|
2018-02-15 21:20:55 +00:00
|
|
|
let!(:expected_upload_attrs) { model_uploads.first.attributes.slice('path', 'uploader', 'size', 'checksum') }
|
2017-11-24 07:12:24 +00:00
|
|
|
let!(:untracked_file) { untracked_files_for_uploads.create!(path: expected_upload_attrs['path']) }
|
2017-11-08 03:08:02 +00:00
|
|
|
|
|
|
|
before do
|
2018-02-15 21:20:55 +00:00
|
|
|
model_uploads.delete_all
|
2017-11-08 03:08:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates an Upload record' do
|
|
|
|
expect do
|
2017-12-05 18:43:08 +00:00
|
|
|
subject.perform(1, untracked_files_for_uploads.last.id)
|
2018-02-15 21:20:55 +00:00
|
|
|
end.to change { model_uploads.count }.from(0).to(1)
|
2017-11-08 03:08:02 +00:00
|
|
|
|
2018-02-15 21:20:55 +00:00
|
|
|
expect(model_uploads.first.attributes).to include(expected_upload_attrs)
|
2017-11-08 03:08:02 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-11-15 07:15:30 +00:00
|
|
|
context 'for an appearance logo file path' do
|
2018-02-15 21:20:55 +00:00
|
|
|
let(:model) { create_or_update_appearance(logo: true) }
|
|
|
|
let(:model_uploads) { get_uploads(model, 'Appearance') }
|
2017-11-08 03:08:02 +00:00
|
|
|
|
2017-11-24 07:12:24 +00:00
|
|
|
it_behaves_like 'non_markdown_file'
|
2017-11-15 07:15:30 +00:00
|
|
|
end
|
2017-11-08 03:08:02 +00:00
|
|
|
|
2017-11-15 07:15:30 +00:00
|
|
|
context 'for an appearance header_logo file path' do
|
2018-02-15 21:20:55 +00:00
|
|
|
let(:model) { create_or_update_appearance(header_logo: true) }
|
|
|
|
let(:model_uploads) { get_uploads(model, 'Appearance') }
|
2017-11-08 03:08:02 +00:00
|
|
|
|
2017-11-24 07:12:24 +00:00
|
|
|
it_behaves_like 'non_markdown_file'
|
2017-11-08 03:08:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'for a pre-Markdown Note attachment file path' do
|
2018-02-15 21:20:55 +00:00
|
|
|
let(:model) { create_note(attachment: true) }
|
|
|
|
let!(:expected_upload_attrs) { get_uploads(model, 'Note').first.attributes.slice('path', 'uploader', 'size', 'checksum') }
|
2017-12-12 21:13:07 +00:00
|
|
|
let!(:untracked_file) { untracked_files_for_uploads.create!(path: expected_upload_attrs['path']) }
|
|
|
|
|
|
|
|
before do
|
2018-02-15 21:20:55 +00:00
|
|
|
get_uploads(model, 'Note').delete_all
|
2017-11-08 03:08:02 +00:00
|
|
|
end
|
|
|
|
|
2017-12-12 21:13:07 +00:00
|
|
|
# Can't use the shared example because Note doesn't have an `uploads` association
|
|
|
|
it 'creates an Upload record' do
|
|
|
|
expect do
|
|
|
|
subject.perform(1, untracked_files_for_uploads.last.id)
|
2018-02-15 21:20:55 +00:00
|
|
|
end.to change { get_uploads(model, 'Note').count }.from(0).to(1)
|
2017-11-08 03:08:02 +00:00
|
|
|
|
2018-02-15 21:20:55 +00:00
|
|
|
expect(get_uploads(model, 'Note').first.attributes).to include(expected_upload_attrs)
|
2017-12-12 21:13:07 +00:00
|
|
|
end
|
2017-11-08 03:08:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'for a user avatar file path' do
|
2018-02-15 21:20:55 +00:00
|
|
|
let(:model) { create_user(avatar: true) }
|
|
|
|
let(:model_uploads) { get_uploads(model, 'User') }
|
2017-11-08 03:08:02 +00:00
|
|
|
|
2017-11-24 07:12:24 +00:00
|
|
|
it_behaves_like 'non_markdown_file'
|
2017-11-08 03:08:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'for a group avatar file path' do
|
2018-02-15 21:20:55 +00:00
|
|
|
let(:model) { create_group(avatar: true) }
|
|
|
|
let(:model_uploads) { get_uploads(model, 'Namespace') }
|
2017-11-08 03:08:02 +00:00
|
|
|
|
2017-11-24 07:12:24 +00:00
|
|
|
it_behaves_like 'non_markdown_file'
|
2017-11-08 03:08:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'for a project avatar file path' do
|
2018-02-15 21:20:55 +00:00
|
|
|
let(:model) { create_project(avatar: true) }
|
|
|
|
let(:model_uploads) { get_uploads(model, 'Project') }
|
2017-11-08 03:08:02 +00:00
|
|
|
|
2017-11-24 07:12:24 +00:00
|
|
|
it_behaves_like 'non_markdown_file'
|
2017-11-08 03:08:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'for a project Markdown attachment (notes, issues, MR descriptions) file path' do
|
2018-02-15 21:20:55 +00:00
|
|
|
let(:model) { create_project }
|
2017-11-15 07:15:30 +00:00
|
|
|
|
2017-11-08 03:08:02 +00:00
|
|
|
before do
|
2017-11-15 07:15:30 +00:00
|
|
|
# Upload the file
|
2018-02-15 21:20:55 +00:00
|
|
|
add_markdown_attachment(model)
|
2017-11-15 07:15:30 +00:00
|
|
|
|
|
|
|
# Create the untracked_files_for_uploads record
|
2018-02-15 21:20:55 +00:00
|
|
|
untracked_files_for_uploads.create!(path: "#{Gitlab::BackgroundMigration::PrepareUntrackedUploads::RELATIVE_UPLOAD_DIR}/#{get_full_path(model)}/#{get_uploads(model, 'Project').first.path}")
|
2017-11-15 07:15:30 +00:00
|
|
|
|
|
|
|
# Save the expected upload attributes
|
2018-02-15 21:20:55 +00:00
|
|
|
@expected_upload_attrs = get_uploads(model, 'Project').first.attributes.slice('path', 'uploader', 'size', 'checksum')
|
2017-11-15 07:15:30 +00:00
|
|
|
|
|
|
|
# Untrack the file
|
2018-02-15 21:20:55 +00:00
|
|
|
get_uploads(model, 'Project').delete_all
|
2017-11-08 03:08:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates an Upload record' do
|
|
|
|
expect do
|
2017-12-05 18:43:08 +00:00
|
|
|
subject.perform(1, untracked_files_for_uploads.last.id)
|
2018-02-15 21:20:55 +00:00
|
|
|
end.to change { get_uploads(model, 'Project').count }.from(0).to(1)
|
2017-11-08 03:08:02 +00:00
|
|
|
|
2018-02-15 21:20:55 +00:00
|
|
|
expect(get_uploads(model, 'Project').first.attributes).to include(@expected_upload_attrs)
|
2017-11-08 03:08:02 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-11-24 07:12:24 +00:00
|
|
|
end
|