2017-11-21 15:26:53 -05:00
|
|
|
require 'rake_helper'
|
|
|
|
|
|
|
|
describe 'gitlab:cleanup rake tasks' do
|
|
|
|
before do
|
|
|
|
Rake.application.rake_require 'tasks/gitlab/cleanup'
|
|
|
|
end
|
|
|
|
|
2018-07-26 17:23:33 -04:00
|
|
|
describe 'cleanup namespaces and repos' do
|
Port cleanup tasks to use Gitaly
Rake tasks cleaning up the Git storage were still using direct disk
access, which won't work if these aren't attached. To mitigate a
migration issue was created.
To port gitlab:cleanup:dirs, and gitlab:cleanup:repos, a new RPC was
required, ListDirectories. This was implemented in Gitaly, through
https://gitlab.com/gitlab-org/gitaly/merge_requests/868.
To be able to use the new RPC the Gitaly server was bumped to v0.120.
This is an RPC that will not use feature gates, as this doesn't scale on
.com so there is no way to test it at scale. Futhermore, we _know_ it
doesn't scale, but this might be a useful task for smaller instances.
Lastly, the tests are slightly updated to also work when the disk isn't
attached. Eventhough this is not planned, it was very little effort and
thus I applied the boy scout rule.
Closes https://gitlab.com/gitlab-org/gitaly/issues/954
Closes https://gitlab.com/gitlab-org/gitlab-ce/issues/40529
2018-09-07 05:16:34 -04:00
|
|
|
let(:gitlab_shell) { Gitlab::Shell.new }
|
|
|
|
let(:storage) { storages.keys.first }
|
2017-11-21 15:26:53 -05:00
|
|
|
let(:storages) do
|
|
|
|
{
|
2018-03-14 09:42:49 -04:00
|
|
|
'default' => Gitlab::GitalyClient::StorageSettings.new(@default_storage_hash.merge('path' => 'tmp/tests/default_storage'))
|
2017-11-21 15:26:53 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2018-03-14 09:42:49 -04:00
|
|
|
before(:all) do
|
|
|
|
@default_storage_hash = Gitlab.config.repositories.storages.default.to_h
|
|
|
|
end
|
|
|
|
|
2017-11-21 15:26:53 -05:00
|
|
|
before do
|
|
|
|
allow(Gitlab.config.repositories).to receive(:storages).and_return(storages)
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
Port cleanup tasks to use Gitaly
Rake tasks cleaning up the Git storage were still using direct disk
access, which won't work if these aren't attached. To mitigate a
migration issue was created.
To port gitlab:cleanup:dirs, and gitlab:cleanup:repos, a new RPC was
required, ListDirectories. This was implemented in Gitaly, through
https://gitlab.com/gitlab-org/gitaly/merge_requests/868.
To be able to use the new RPC the Gitaly server was bumped to v0.120.
This is an RPC that will not use feature gates, as this doesn't scale on
.com so there is no way to test it at scale. Futhermore, we _know_ it
doesn't scale, but this might be a useful task for smaller instances.
Lastly, the tests are slightly updated to also work when the disk isn't
attached. Eventhough this is not planned, it was very little effort and
thus I applied the boy scout rule.
Closes https://gitlab.com/gitlab-org/gitaly/issues/954
Closes https://gitlab.com/gitlab-org/gitlab-ce/issues/40529
2018-09-07 05:16:34 -04:00
|
|
|
Gitlab::GitalyClient::StorageService.new(storage).delete_all_repositories
|
2017-11-21 15:26:53 -05:00
|
|
|
end
|
|
|
|
|
2017-11-24 13:29:25 -05:00
|
|
|
describe 'cleanup:repos' do
|
|
|
|
before do
|
Port cleanup tasks to use Gitaly
Rake tasks cleaning up the Git storage were still using direct disk
access, which won't work if these aren't attached. To mitigate a
migration issue was created.
To port gitlab:cleanup:dirs, and gitlab:cleanup:repos, a new RPC was
required, ListDirectories. This was implemented in Gitaly, through
https://gitlab.com/gitlab-org/gitaly/merge_requests/868.
To be able to use the new RPC the Gitaly server was bumped to v0.120.
This is an RPC that will not use feature gates, as this doesn't scale on
.com so there is no way to test it at scale. Futhermore, we _know_ it
doesn't scale, but this might be a useful task for smaller instances.
Lastly, the tests are slightly updated to also work when the disk isn't
attached. Eventhough this is not planned, it was very little effort and
thus I applied the boy scout rule.
Closes https://gitlab.com/gitlab-org/gitaly/issues/954
Closes https://gitlab.com/gitlab-org/gitlab-ce/issues/40529
2018-09-07 05:16:34 -04:00
|
|
|
gitlab_shell.add_namespace(storage, 'broken/project.git')
|
|
|
|
gitlab_shell.add_namespace(storage, '@hashed/12/34/5678.git')
|
2017-11-24 13:29:25 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'moves it to an orphaned path' do
|
Port cleanup tasks to use Gitaly
Rake tasks cleaning up the Git storage were still using direct disk
access, which won't work if these aren't attached. To mitigate a
migration issue was created.
To port gitlab:cleanup:dirs, and gitlab:cleanup:repos, a new RPC was
required, ListDirectories. This was implemented in Gitaly, through
https://gitlab.com/gitlab-org/gitaly/merge_requests/868.
To be able to use the new RPC the Gitaly server was bumped to v0.120.
This is an RPC that will not use feature gates, as this doesn't scale on
.com so there is no way to test it at scale. Futhermore, we _know_ it
doesn't scale, but this might be a useful task for smaller instances.
Lastly, the tests are slightly updated to also work when the disk isn't
attached. Eventhough this is not planned, it was very little effort and
thus I applied the boy scout rule.
Closes https://gitlab.com/gitlab-org/gitaly/issues/954
Closes https://gitlab.com/gitlab-org/gitlab-ce/issues/40529
2018-09-07 05:16:34 -04:00
|
|
|
now = Time.now
|
|
|
|
|
|
|
|
Timecop.freeze(now) do
|
|
|
|
run_rake_task('gitlab:cleanup:repos')
|
|
|
|
repo_list = Gitlab::GitalyClient::StorageService.new(storage).list_directories(depth: 0)
|
2017-11-24 13:29:25 -05:00
|
|
|
|
Port cleanup tasks to use Gitaly
Rake tasks cleaning up the Git storage were still using direct disk
access, which won't work if these aren't attached. To mitigate a
migration issue was created.
To port gitlab:cleanup:dirs, and gitlab:cleanup:repos, a new RPC was
required, ListDirectories. This was implemented in Gitaly, through
https://gitlab.com/gitlab-org/gitaly/merge_requests/868.
To be able to use the new RPC the Gitaly server was bumped to v0.120.
This is an RPC that will not use feature gates, as this doesn't scale on
.com so there is no way to test it at scale. Futhermore, we _know_ it
doesn't scale, but this might be a useful task for smaller instances.
Lastly, the tests are slightly updated to also work when the disk isn't
attached. Eventhough this is not planned, it was very little effort and
thus I applied the boy scout rule.
Closes https://gitlab.com/gitlab-org/gitaly/issues/954
Closes https://gitlab.com/gitlab-org/gitlab-ce/issues/40529
2018-09-07 05:16:34 -04:00
|
|
|
expect(repo_list.last).to include("broken+orphaned+#{now.to_i}")
|
|
|
|
end
|
2017-11-24 13:29:25 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'ignores @hashed repos' do
|
|
|
|
run_rake_task('gitlab:cleanup:repos')
|
2017-11-21 15:26:53 -05:00
|
|
|
|
Port cleanup tasks to use Gitaly
Rake tasks cleaning up the Git storage were still using direct disk
access, which won't work if these aren't attached. To mitigate a
migration issue was created.
To port gitlab:cleanup:dirs, and gitlab:cleanup:repos, a new RPC was
required, ListDirectories. This was implemented in Gitaly, through
https://gitlab.com/gitlab-org/gitaly/merge_requests/868.
To be able to use the new RPC the Gitaly server was bumped to v0.120.
This is an RPC that will not use feature gates, as this doesn't scale on
.com so there is no way to test it at scale. Futhermore, we _know_ it
doesn't scale, but this might be a useful task for smaller instances.
Lastly, the tests are slightly updated to also work when the disk isn't
attached. Eventhough this is not planned, it was very little effort and
thus I applied the boy scout rule.
Closes https://gitlab.com/gitlab-org/gitaly/issues/954
Closes https://gitlab.com/gitlab-org/gitlab-ce/issues/40529
2018-09-07 05:16:34 -04:00
|
|
|
expect(gitlab_shell.exists?(storage, '@hashed/12/34/5678.git')).to be(true)
|
2017-11-24 13:29:25 -05:00
|
|
|
end
|
2017-11-21 15:26:53 -05:00
|
|
|
end
|
|
|
|
|
2017-11-24 13:29:25 -05:00
|
|
|
describe 'cleanup:dirs' do
|
|
|
|
it 'removes missing namespaces' do
|
Port cleanup tasks to use Gitaly
Rake tasks cleaning up the Git storage were still using direct disk
access, which won't work if these aren't attached. To mitigate a
migration issue was created.
To port gitlab:cleanup:dirs, and gitlab:cleanup:repos, a new RPC was
required, ListDirectories. This was implemented in Gitaly, through
https://gitlab.com/gitlab-org/gitaly/merge_requests/868.
To be able to use the new RPC the Gitaly server was bumped to v0.120.
This is an RPC that will not use feature gates, as this doesn't scale on
.com so there is no way to test it at scale. Futhermore, we _know_ it
doesn't scale, but this might be a useful task for smaller instances.
Lastly, the tests are slightly updated to also work when the disk isn't
attached. Eventhough this is not planned, it was very little effort and
thus I applied the boy scout rule.
Closes https://gitlab.com/gitlab-org/gitaly/issues/954
Closes https://gitlab.com/gitlab-org/gitlab-ce/issues/40529
2018-09-07 05:16:34 -04:00
|
|
|
gitlab_shell.add_namespace(storage, "namespace_1/project.git")
|
|
|
|
gitlab_shell.add_namespace(storage, "namespace_2/project.git")
|
|
|
|
allow(Namespace).to receive(:pluck).and_return(['namespace_1'])
|
2017-11-24 13:29:25 -05:00
|
|
|
|
|
|
|
stub_env('REMOVE', 'true')
|
|
|
|
run_rake_task('gitlab:cleanup:dirs')
|
|
|
|
|
Port cleanup tasks to use Gitaly
Rake tasks cleaning up the Git storage were still using direct disk
access, which won't work if these aren't attached. To mitigate a
migration issue was created.
To port gitlab:cleanup:dirs, and gitlab:cleanup:repos, a new RPC was
required, ListDirectories. This was implemented in Gitaly, through
https://gitlab.com/gitlab-org/gitaly/merge_requests/868.
To be able to use the new RPC the Gitaly server was bumped to v0.120.
This is an RPC that will not use feature gates, as this doesn't scale on
.com so there is no way to test it at scale. Futhermore, we _know_ it
doesn't scale, but this might be a useful task for smaller instances.
Lastly, the tests are slightly updated to also work when the disk isn't
attached. Eventhough this is not planned, it was very little effort and
thus I applied the boy scout rule.
Closes https://gitlab.com/gitlab-org/gitaly/issues/954
Closes https://gitlab.com/gitlab-org/gitlab-ce/issues/40529
2018-09-07 05:16:34 -04:00
|
|
|
expect(gitlab_shell.exists?(storage, 'namespace_1')).to be(true)
|
|
|
|
expect(gitlab_shell.exists?(storage, 'namespace_2')).to be(false)
|
2017-11-24 13:29:25 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'ignores @hashed directory' do
|
Port cleanup tasks to use Gitaly
Rake tasks cleaning up the Git storage were still using direct disk
access, which won't work if these aren't attached. To mitigate a
migration issue was created.
To port gitlab:cleanup:dirs, and gitlab:cleanup:repos, a new RPC was
required, ListDirectories. This was implemented in Gitaly, through
https://gitlab.com/gitlab-org/gitaly/merge_requests/868.
To be able to use the new RPC the Gitaly server was bumped to v0.120.
This is an RPC that will not use feature gates, as this doesn't scale on
.com so there is no way to test it at scale. Futhermore, we _know_ it
doesn't scale, but this might be a useful task for smaller instances.
Lastly, the tests are slightly updated to also work when the disk isn't
attached. Eventhough this is not planned, it was very little effort and
thus I applied the boy scout rule.
Closes https://gitlab.com/gitlab-org/gitaly/issues/954
Closes https://gitlab.com/gitlab-org/gitlab-ce/issues/40529
2018-09-07 05:16:34 -04:00
|
|
|
gitlab_shell.add_namespace(storage, '@hashed/12/34/5678.git')
|
2017-11-21 15:26:53 -05:00
|
|
|
|
2017-11-24 13:29:25 -05:00
|
|
|
run_rake_task('gitlab:cleanup:dirs')
|
2017-11-21 15:26:53 -05:00
|
|
|
|
Port cleanup tasks to use Gitaly
Rake tasks cleaning up the Git storage were still using direct disk
access, which won't work if these aren't attached. To mitigate a
migration issue was created.
To port gitlab:cleanup:dirs, and gitlab:cleanup:repos, a new RPC was
required, ListDirectories. This was implemented in Gitaly, through
https://gitlab.com/gitlab-org/gitaly/merge_requests/868.
To be able to use the new RPC the Gitaly server was bumped to v0.120.
This is an RPC that will not use feature gates, as this doesn't scale on
.com so there is no way to test it at scale. Futhermore, we _know_ it
doesn't scale, but this might be a useful task for smaller instances.
Lastly, the tests are slightly updated to also work when the disk isn't
attached. Eventhough this is not planned, it was very little effort and
thus I applied the boy scout rule.
Closes https://gitlab.com/gitlab-org/gitaly/issues/954
Closes https://gitlab.com/gitlab-org/gitlab-ce/issues/40529
2018-09-07 05:16:34 -04:00
|
|
|
expect(gitlab_shell.exists?(storage, '@hashed/12/34/5678.git')).to be(true)
|
2017-11-24 13:29:25 -05:00
|
|
|
end
|
2017-11-21 15:26:53 -05:00
|
|
|
end
|
|
|
|
end
|
2018-07-26 17:23:33 -04:00
|
|
|
|
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
|
|
|
|
expect(main_object).to receive(:logger).and_return(logger).at_least(1).times
|
2018-07-26 17:23:33 -04:00
|
|
|
|
2018-08-03 00:36:43 -04:00
|
|
|
allow(logger).to receive(:info).at_least(1).times
|
|
|
|
allow(logger).to receive(:debug).at_least(1).times
|
|
|
|
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)
|
|
|
|
.with(limit: anything,
|
|
|
|
dry_run: false,
|
|
|
|
niceness: anything,
|
|
|
|
logger: anything)
|
|
|
|
.and_call_original
|
|
|
|
|
|
|
|
rake_task
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-07-12 08:25:12 -04:00
|
|
|
|
|
|
|
context 'sessions' do
|
|
|
|
describe 'gitlab:cleanup:sessions:active_sessions_lookup_keys', :clean_gitlab_redis_shared_state do
|
|
|
|
subject(:rake_task) { run_rake_task('gitlab:cleanup:sessions:active_sessions_lookup_keys') }
|
|
|
|
|
|
|
|
let!(:user) { create(:user) }
|
|
|
|
let(:existing_session_id) { '5' }
|
|
|
|
|
|
|
|
before do
|
|
|
|
Gitlab::Redis::SharedState.with do |redis|
|
|
|
|
redis.set("session:user:gitlab:#{user.id}:#{existing_session_id}",
|
|
|
|
Marshal.dump(true))
|
|
|
|
redis.sadd("session:lookup:user:gitlab:#{user.id}", (1..10).to_a)
|
|
|
|
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
|
|
|
|
Gitlab::Redis::SharedState.with do |redis|
|
|
|
|
lookup_key = "session:lookup:user:gitlab:#{user.id}"
|
|
|
|
expect { subject }.to change { redis.scard(lookup_key) }.from(10).to(1)
|
|
|
|
expect(redis.smembers("session:lookup:user:gitlab:#{user.id}")).to(
|
|
|
|
eql([existing_session_id]))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-11-21 15:26:53 -05:00
|
|
|
end
|