2019-10-15 05:06:09 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-11-21 15:26:53 -05:00
|
|
|
require 'rake_helper'
|
|
|
|
|
2021-06-08 11:10:00 -04:00
|
|
|
RSpec.describe 'gitlab:cleanup rake tasks', :silence_stdout do
|
2017-11-21 15:26:53 -05:00
|
|
|
before do
|
|
|
|
Rake.application.rake_require 'tasks/gitlab/cleanup'
|
|
|
|
end
|
|
|
|
|
2018-08-03 00:36:43 -04:00
|
|
|
# A single integration test that is redundant with one part of the
|
|
|
|
# Gitlab::Cleanup::ProjectUploads spec.
|
|
|
|
#
|
|
|
|
# Additionally, this tests DRY_RUN env var values, and the extra line of
|
|
|
|
# output that says you can disable DRY_RUN if it's enabled.
|
2018-07-26 17:23:33 -04:00
|
|
|
describe 'cleanup:project_uploads' do
|
2018-08-03 00:36:43 -04:00
|
|
|
let!(:logger) { double(:logger) }
|
2018-07-26 17:23:33 -04:00
|
|
|
|
2018-08-03 00:36:43 -04:00
|
|
|
before do
|
2020-02-06 16:08:48 -05:00
|
|
|
expect(main_object).to receive(:logger).and_return(logger).at_least(:once)
|
2018-07-26 17:23:33 -04:00
|
|
|
|
2020-02-06 16:08:48 -05:00
|
|
|
allow(logger).to receive(:info).at_least(:once)
|
|
|
|
allow(logger).to receive(:debug).at_least(:once)
|
2018-08-03 00:36:43 -04:00
|
|
|
end
|
2018-07-26 17:23:33 -04:00
|
|
|
|
2018-08-03 00:36:43 -04:00
|
|
|
context 'with a fixable orphaned project upload file' do
|
|
|
|
let(:orphaned) { create(:upload, :issuable_upload, :with_file, model: build(:project, :legacy_storage)) }
|
|
|
|
let(:new_path) { orphaned.absolute_path }
|
|
|
|
let(:path) { File.join(FileUploader.root, 'some', 'wrong', 'location', orphaned.path) }
|
2018-07-26 17:23:33 -04:00
|
|
|
|
2018-08-03 00:36:43 -04:00
|
|
|
before do
|
|
|
|
FileUtils.mkdir_p(File.dirname(path))
|
|
|
|
FileUtils.mv(new_path, path)
|
2018-07-26 17:23:33 -04:00
|
|
|
end
|
|
|
|
|
2018-08-03 00:36:43 -04:00
|
|
|
context 'with DRY_RUN disabled' do
|
|
|
|
before do
|
|
|
|
stub_env('DRY_RUN', 'false')
|
2018-07-26 17:23:33 -04:00
|
|
|
end
|
|
|
|
|
2018-08-03 00:36:43 -04:00
|
|
|
it 'moves the file to its proper location' do
|
|
|
|
run_rake_task('gitlab:cleanup:project_uploads')
|
2018-07-26 17:23:33 -04:00
|
|
|
|
2018-08-03 00:36:43 -04:00
|
|
|
expect(File.exist?(path)).to be_falsey
|
|
|
|
expect(File.exist?(new_path)).to be_truthy
|
2018-07-26 17:23:33 -04:00
|
|
|
end
|
|
|
|
|
2018-08-03 00:36:43 -04:00
|
|
|
it 'logs action as done' do
|
|
|
|
expect(logger).to receive(:info).with("Looking for orphaned project uploads to clean up...")
|
|
|
|
expect(logger).to receive(:info).with("Did fix #{path} -> #{new_path}")
|
2018-07-26 17:23:33 -04:00
|
|
|
|
|
|
|
run_rake_task('gitlab:cleanup:project_uploads')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-03 00:36:43 -04:00
|
|
|
shared_examples_for 'does not move the file' do
|
|
|
|
it 'does not move the file' do
|
|
|
|
run_rake_task('gitlab:cleanup:project_uploads')
|
2018-07-26 17:23:33 -04:00
|
|
|
|
2018-08-03 00:36:43 -04:00
|
|
|
expect(File.exist?(path)).to be_truthy
|
|
|
|
expect(File.exist?(new_path)).to be_falsey
|
2018-07-26 17:23:33 -04:00
|
|
|
end
|
|
|
|
|
2018-08-03 00:36:43 -04:00
|
|
|
it 'logs action as able to be done' do
|
|
|
|
expect(logger).to receive(:info).with("Looking for orphaned project uploads to clean up. Dry run...")
|
|
|
|
expect(logger).to receive(:info).with("Can fix #{path} -> #{new_path}")
|
|
|
|
expect(logger).to receive(:info).with(/To clean up these files run this command with DRY_RUN=false/)
|
2018-07-26 17:23:33 -04:00
|
|
|
|
2018-08-03 00:36:43 -04:00
|
|
|
run_rake_task('gitlab:cleanup:project_uploads')
|
|
|
|
end
|
2018-07-26 17:23:33 -04:00
|
|
|
end
|
|
|
|
|
2018-08-03 00:36:43 -04:00
|
|
|
context 'with DRY_RUN explicitly enabled' do
|
2018-07-26 17:23:33 -04:00
|
|
|
before do
|
|
|
|
stub_env('DRY_RUN', 'true')
|
|
|
|
end
|
|
|
|
|
2018-08-03 00:36:43 -04:00
|
|
|
it_behaves_like 'does not move the file'
|
2018-07-26 17:23:33 -04:00
|
|
|
end
|
|
|
|
|
2018-08-03 00:36:43 -04:00
|
|
|
context 'with DRY_RUN set to an unknown value' do
|
2018-07-26 17:23:33 -04:00
|
|
|
before do
|
|
|
|
stub_env('DRY_RUN', 'foo')
|
|
|
|
end
|
|
|
|
|
2018-08-03 00:36:43 -04:00
|
|
|
it_behaves_like 'does not move the file'
|
2018-07-26 17:23:33 -04:00
|
|
|
end
|
|
|
|
|
2018-08-03 00:36:43 -04:00
|
|
|
context 'with DRY_RUN unset' do
|
|
|
|
it_behaves_like 'does not move the file'
|
2018-07-26 17:23:33 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-06-13 17:07:59 -04:00
|
|
|
|
|
|
|
describe 'gitlab:cleanup:orphan_job_artifact_files' do
|
|
|
|
subject(:rake_task) { run_rake_task('gitlab:cleanup:orphan_job_artifact_files') }
|
|
|
|
|
|
|
|
it 'runs the task without errors' do
|
|
|
|
expect(Gitlab::Cleanup::OrphanJobArtifactFiles)
|
|
|
|
.to receive(:new).and_call_original
|
|
|
|
|
|
|
|
expect { rake_task }.not_to raise_error
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with DRY_RUN set to false' do
|
|
|
|
before do
|
|
|
|
stub_env('DRY_RUN', 'false')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'passes dry_run correctly' do
|
|
|
|
expect(Gitlab::Cleanup::OrphanJobArtifactFiles)
|
|
|
|
.to receive(:new)
|
2021-01-25 07:09:07 -05:00
|
|
|
.with(dry_run: false,
|
2019-06-13 17:07:59 -04:00
|
|
|
niceness: anything,
|
|
|
|
logger: anything)
|
|
|
|
.and_call_original
|
|
|
|
|
|
|
|
rake_task
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-07-12 08:25:12 -04:00
|
|
|
|
2020-03-23 08:09:47 -04:00
|
|
|
describe 'gitlab:cleanup:orphan_lfs_file_references' do
|
|
|
|
subject(:rake_task) { run_rake_task('gitlab:cleanup:orphan_lfs_file_references') }
|
|
|
|
|
|
|
|
let(:project) { create(:project, :repository) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
stub_env('PROJECT_ID', project.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'runs the task without errors' do
|
|
|
|
expect(Gitlab::Cleanup::OrphanLfsFileReferences)
|
|
|
|
.to receive(:new).and_call_original
|
|
|
|
|
|
|
|
expect { rake_task }.not_to raise_error
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with DRY_RUN set to false' do
|
|
|
|
before do
|
|
|
|
stub_env('DRY_RUN', 'false')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'passes dry_run correctly' do
|
|
|
|
expect(Gitlab::Cleanup::OrphanLfsFileReferences)
|
|
|
|
.to receive(:new)
|
|
|
|
.with(project,
|
|
|
|
dry_run: false,
|
|
|
|
logger: anything)
|
|
|
|
.and_call_original
|
|
|
|
|
|
|
|
rake_task
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'gitlab:cleanup:orphan_lfs_files' do
|
|
|
|
subject(:rake_task) { run_rake_task('gitlab:cleanup:orphan_lfs_files') }
|
|
|
|
|
|
|
|
it 'runs RemoveUnreferencedLfsObjectsWorker' do
|
|
|
|
expect_any_instance_of(RemoveUnreferencedLfsObjectsWorker)
|
|
|
|
.to receive(:perform)
|
|
|
|
.and_call_original
|
|
|
|
|
|
|
|
rake_task
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-12 08:25:12 -04:00
|
|
|
context 'sessions' do
|
2021-11-24 13:14:31 -05:00
|
|
|
describe 'gitlab:cleanup:sessions:active_sessions_lookup_keys', :clean_gitlab_redis_sessions do
|
2019-07-12 08:25:12 -04:00
|
|
|
subject(:rake_task) { run_rake_task('gitlab:cleanup:sessions:active_sessions_lookup_keys') }
|
|
|
|
|
|
|
|
let!(:user) { create(:user) }
|
|
|
|
let(:existing_session_id) { '5' }
|
|
|
|
|
|
|
|
before do
|
2021-11-24 13:14:31 -05:00
|
|
|
Gitlab::Redis::Sessions.with do |redis|
|
2021-12-10 16:11:20 -05:00
|
|
|
redis.set(ActiveSession.key_name(user.id, existing_session_id),
|
|
|
|
ActiveSession.new(session_id: 'x').dump)
|
|
|
|
redis.sadd(ActiveSession.lookup_key_name(user.id), (1..10).to_a)
|
2019-07-12 08:25:12 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'runs the task without errors' do
|
|
|
|
expect { rake_task }.not_to raise_error
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'removes expired active session lookup keys' do
|
2021-11-24 13:14:31 -05:00
|
|
|
Gitlab::Redis::Sessions.with do |redis|
|
2021-12-10 16:11:20 -05:00
|
|
|
lookup_key = ActiveSession.lookup_key_name(user.id)
|
|
|
|
|
2019-07-12 08:25:12 -04:00
|
|
|
expect { subject }.to change { redis.scard(lookup_key) }.from(10).to(1)
|
2021-12-10 16:11:20 -05:00
|
|
|
expect(redis.smembers(lookup_key)).to contain_exactly existing_session_id
|
2019-07-12 08:25:12 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-11-21 15:26:53 -05:00
|
|
|
end
|