Commit fc03eef3 authored by Ash McKenzie's avatar Ash McKenzie

Merge branch 'jc-remove-namespace-exists-calls' into 'master'

Get rid of namespace exist? calls entirely

Closes #33463

See merge request gitlab-org/gitlab!18611
parents 8855feb7 b1792fd2
...@@ -7,12 +7,6 @@ module Gitlab ...@@ -7,12 +7,6 @@ module Gitlab
@storage = storage @storage = storage
end end
def exists?(name)
request = Gitaly::NamespaceExistsRequest.new(storage_name: @storage, name: name)
gitaly_client_call(:namespace_exists, request, timeout: GitalyClient.fast_timeout).exists
end
def add(name) def add(name)
request = Gitaly::AddNamespaceRequest.new(storage_name: @storage, name: name) request = Gitaly::AddNamespaceRequest.new(storage_name: @storage, name: name)
......
...@@ -285,18 +285,6 @@ module Gitlab ...@@ -285,18 +285,6 @@ module Gitlab
end end
end end
# Check if such directory exists in repositories.
#
# Usage:
# exists?(storage, 'gitlab')
# exists?(storage, 'gitlab/cookies.git')
#
# rubocop: disable CodeReuse/ActiveRecord
def exists?(storage, dir_name)
Gitlab::GitalyClient::NamespaceService.new(storage).exists?(dir_name)
end
# rubocop: enable CodeReuse/ActiveRecord
def repository_exists?(storage, dir_name) def repository_exists?(storage, dir_name)
Gitlab::Git::Repository.new(storage, dir_name, nil, nil).exists? Gitlab::Git::Repository.new(storage, dir_name, nil, nil).exists?
rescue GRPC::Internal rescue GRPC::Internal
......
...@@ -234,10 +234,7 @@ FactoryBot.define do ...@@ -234,10 +234,7 @@ FactoryBot.define do
trait :broken_repo do trait :broken_repo do
after(:create) do |project| after(:create) do |project|
raise "Failed to create repository!" unless project.create_repository TestEnv.rm_storage_dir(project.repository_storage, "#{project.disk_path}.git/refs")
project.gitlab_shell.rm_directory(project.repository_storage,
File.join("#{project.disk_path}.git", 'refs'))
end end
end end
......
...@@ -90,7 +90,7 @@ describe Gitlab::BareRepositoryImport::Importer, :seed_helper do ...@@ -90,7 +90,7 @@ describe Gitlab::BareRepositoryImport::Importer, :seed_helper do
hook_path = File.join(repo_path, 'hooks') hook_path = File.join(repo_path, 'hooks')
expect(gitlab_shell.repository_exists?(project.repository_storage, repo_path)).to be(true) expect(gitlab_shell.repository_exists?(project.repository_storage, repo_path)).to be(true)
expect(gitlab_shell.exists?(project.repository_storage, hook_path)).to be(true) expect(TestEnv.storage_dir_exists?(project.repository_storage, hook_path)).to be(true)
end end
context 'hashed storage enabled' do context 'hashed storage enabled' do
......
...@@ -310,18 +310,18 @@ describe Gitlab::Shell do ...@@ -310,18 +310,18 @@ describe Gitlab::Shell do
let(:disk_path) { "#{project.disk_path}.git" } let(:disk_path) { "#{project.disk_path}.git" }
it 'returns true when the command succeeds' do it 'returns true when the command succeeds' do
expect(gitlab_shell.exists?(project.repository_storage, disk_path)).to be(true) expect(TestEnv.storage_dir_exists?(project.repository_storage, disk_path)).to be(true)
expect(gitlab_shell.remove_repository(project.repository_storage, project.disk_path)).to be(true) expect(gitlab_shell.remove_repository(project.repository_storage, project.disk_path)).to be(true)
expect(gitlab_shell.exists?(project.repository_storage, disk_path)).to be(false) expect(TestEnv.storage_dir_exists?(project.repository_storage, disk_path)).to be(false)
end end
it 'keeps the namespace directory' do it 'keeps the namespace directory' do
gitlab_shell.remove_repository(project.repository_storage, project.disk_path) gitlab_shell.remove_repository(project.repository_storage, project.disk_path)
expect(gitlab_shell.exists?(project.repository_storage, disk_path)).to be(false) expect(TestEnv.storage_dir_exists?(project.repository_storage, disk_path)).to be(false)
expect(gitlab_shell.exists?(project.repository_storage, project.disk_path.gsub(project.name, ''))).to be(true) expect(TestEnv.storage_dir_exists?(project.repository_storage, project.disk_path.gsub(project.name, ''))).to be(true)
end end
end end
...@@ -332,18 +332,18 @@ describe Gitlab::Shell do ...@@ -332,18 +332,18 @@ describe Gitlab::Shell do
old_path = project2.disk_path old_path = project2.disk_path
new_path = "project/new_path" new_path = "project/new_path"
expect(gitlab_shell.exists?(project2.repository_storage, "#{old_path}.git")).to be(true) expect(TestEnv.storage_dir_exists?(project2.repository_storage, "#{old_path}.git")).to be(true)
expect(gitlab_shell.exists?(project2.repository_storage, "#{new_path}.git")).to be(false) expect(TestEnv.storage_dir_exists?(project2.repository_storage, "#{new_path}.git")).to be(false)
expect(gitlab_shell.mv_repository(project2.repository_storage, old_path, new_path)).to be_truthy expect(gitlab_shell.mv_repository(project2.repository_storage, old_path, new_path)).to be_truthy
expect(gitlab_shell.exists?(project2.repository_storage, "#{old_path}.git")).to be(false) expect(TestEnv.storage_dir_exists?(project2.repository_storage, "#{old_path}.git")).to be(false)
expect(gitlab_shell.exists?(project2.repository_storage, "#{new_path}.git")).to be(true) expect(TestEnv.storage_dir_exists?(project2.repository_storage, "#{new_path}.git")).to be(true)
end end
it 'returns false when the command fails' do it 'returns false when the command fails' do
expect(gitlab_shell.mv_repository(project2.repository_storage, project2.disk_path, '')).to be_falsy expect(gitlab_shell.mv_repository(project2.repository_storage, project2.disk_path, '')).to be_falsy
expect(gitlab_shell.exists?(project2.repository_storage, "#{project2.disk_path}.git")).to be(true) expect(TestEnv.storage_dir_exists?(project2.repository_storage, "#{project2.disk_path}.git")).to be(true)
end end
end end
...@@ -403,56 +403,32 @@ describe Gitlab::Shell do ...@@ -403,56 +403,32 @@ describe Gitlab::Shell do
it 'creates a namespace' do it 'creates a namespace' do
subject.add_namespace(storage, "mepmep") subject.add_namespace(storage, "mepmep")
expect(subject.exists?(storage, "mepmep")).to be(true) expect(TestEnv.storage_dir_exists?(storage, "mepmep")).to be(true)
end end
end end
describe '#exists?' do describe '#repository_exists?' do
context 'when the namespace does not exist' do context 'when the repository does not exist' do
it 'returns false' do it 'returns false' do
expect(subject.exists?(storage, "non-existing")).to be(false) expect(subject.repository_exists?(storage, "non-existing.git")).to be(false)
end end
end end
context 'when the namespace exists' do context 'when the repository exists' do
it 'returns true' do it 'returns true' do
subject.add_namespace(storage, "mepmep") project = create(:project, :repository, :legacy_storage)
expect(subject.exists?(storage, "mepmep")).to be(true) expect(subject.repository_exists?(storage, project.repository.disk_path + ".git")).to be(true)
end end
end end
end end
describe '#repository_exists?' do
context 'when the storage path does not exist' do
subject { described_class.new.repository_exists?(storage, "non-existing.git") }
it { is_expected.to be_falsey }
end
context 'when the repository does not exist' do
let(:project) { create(:project, :repository, :legacy_storage) }
subject { described_class.new.repository_exists?(storage, "#{project.repository.disk_path}-some-other-repo.git") }
it { is_expected.to be_falsey }
end
context 'when the repository exists' do
let(:project) { create(:project, :repository, :legacy_storage) }
subject { described_class.new.repository_exists?(storage, "#{project.repository.disk_path}.git") }
it { is_expected.to be_truthy }
end
end
describe '#remove' do describe '#remove' do
it 'removes the namespace' do it 'removes the namespace' do
subject.add_namespace(storage, "mepmep") subject.add_namespace(storage, "mepmep")
subject.rm_namespace(storage, "mepmep") subject.rm_namespace(storage, "mepmep")
expect(subject.exists?(storage, "mepmep")).to be(false) expect(TestEnv.storage_dir_exists?(storage, "mepmep")).to be(false)
end end
end end
...@@ -461,8 +437,8 @@ describe Gitlab::Shell do ...@@ -461,8 +437,8 @@ describe Gitlab::Shell do
subject.add_namespace(storage, "mepmep") subject.add_namespace(storage, "mepmep")
subject.mv_namespace(storage, "mepmep", "2mep") subject.mv_namespace(storage, "mepmep", "2mep")
expect(subject.exists?(storage, "mepmep")).to be(false) expect(TestEnv.storage_dir_exists?(storage, "mepmep")).to be(false)
expect(subject.exists?(storage, "2mep")).to be(true) expect(TestEnv.storage_dir_exists?(storage, "2mep")).to be(true)
end end
end end
end end
......
...@@ -4284,22 +4284,25 @@ describe Project do ...@@ -4284,22 +4284,25 @@ describe Project do
describe '#check_repository_path_availability' do describe '#check_repository_path_availability' do
let(:project) { build(:project, :repository, :legacy_storage) } let(:project) { build(:project, :repository, :legacy_storage) }
subject { project.check_repository_path_availability }
context 'when the repository already exists' do context 'when the repository already exists' do
let(:project) { create(:project, :repository, :legacy_storage) } let(:project) { create(:project, :repository, :legacy_storage) }
it { is_expected.to be_falsey } it 'returns false when repository already exists' do
expect(project.check_repository_path_availability).to be_falsey
end
end end
context 'when the repository does not exist' do context 'when the repository does not exist' do
it { is_expected.to be_truthy } it 'returns false when repository already exists' do
expect(project.check_repository_path_availability).to be_truthy
end
it 'skips gitlab-shell exists?' do it 'skips gitlab-shell exists?' do
project.skip_disk_validation = true project.skip_disk_validation = true
expect(project.gitlab_shell).not_to receive(:repository_exists?) expect(project.gitlab_shell).not_to receive(:repository_exists?)
is_expected.to be_truthy expect(project.check_repository_path_availability).to be_truthy
end end
end end
end end
......
...@@ -55,8 +55,8 @@ describe Groups::DestroyService do ...@@ -55,8 +55,8 @@ describe Groups::DestroyService do
end end
it 'verifies that paths have been deleted' do it 'verifies that paths have been deleted' do
expect(gitlab_shell.exists?(project.repository_storage, group.path)).to be_falsey expect(TestEnv.storage_dir_exists?(project.repository_storage, group.path)).to be_falsey
expect(gitlab_shell.exists?(project.repository_storage, remove_path)).to be_falsey expect(TestEnv.storage_dir_exists?(project.repository_storage, remove_path)).to be_falsey
end end
end end
end end
...@@ -73,13 +73,13 @@ describe Groups::DestroyService do ...@@ -73,13 +73,13 @@ describe Groups::DestroyService do
after do after do
# Clean up stale directories # Clean up stale directories
gitlab_shell.rm_namespace(project.repository_storage, group.path) TestEnv.rm_storage_dir(project.repository_storage, group.path)
gitlab_shell.rm_namespace(project.repository_storage, remove_path) TestEnv.rm_storage_dir(project.repository_storage, remove_path)
end end
it 'verifies original paths and projects still exist' do it 'verifies original paths and projects still exist' do
expect(gitlab_shell.exists?(project.repository_storage, group.path)).to be_truthy expect(TestEnv.storage_dir_exists?(project.repository_storage, group.path)).to be_truthy
expect(gitlab_shell.exists?(project.repository_storage, remove_path)).to be_falsey expect(TestEnv.storage_dir_exists?(project.repository_storage, remove_path)).to be_falsey
expect(Project.unscoped.count).to eq(1) expect(Project.unscoped.count).to eq(1)
expect(Group.unscoped.count).to eq(2) expect(Group.unscoped.count).to eq(2)
end end
......
...@@ -222,7 +222,7 @@ describe Projects::AfterRenameService do ...@@ -222,7 +222,7 @@ describe Projects::AfterRenameService do
def expect_repository_exist(full_path_with_extension) def expect_repository_exist(full_path_with_extension)
expect( expect(
gitlab_shell.exists?( TestEnv.storage_dir_exists?(
project.repository_storage, project.repository_storage,
full_path_with_extension full_path_with_extension
) )
......
...@@ -346,21 +346,21 @@ describe Projects::DestroyService do ...@@ -346,21 +346,21 @@ describe Projects::DestroyService do
let(:path) { project.disk_path + '.git' } let(:path) { project.disk_path + '.git' }
before do before do
expect(project.gitlab_shell.exists?(project.repository_storage, path)).to be_truthy expect(TestEnv.storage_dir_exists?(project.repository_storage, path)).to be_truthy
expect(project.gitlab_shell.exists?(project.repository_storage, remove_path)).to be_falsey expect(TestEnv.storage_dir_exists?(project.repository_storage, remove_path)).to be_falsey
# Dont run sidekiq to check if renamed repository exists # Dont run sidekiq to check if renamed repository exists
Sidekiq::Testing.fake! { destroy_project(project, user, {}) } Sidekiq::Testing.fake! { destroy_project(project, user, {}) }
expect(project.gitlab_shell.exists?(project.repository_storage, path)).to be_falsey expect(TestEnv.storage_dir_exists?(project.repository_storage, path)).to be_falsey
expect(project.gitlab_shell.exists?(project.repository_storage, remove_path)).to be_truthy expect(TestEnv.storage_dir_exists?(project.repository_storage, remove_path)).to be_truthy
end end
it 'restores the repositories' do it 'restores the repositories' do
Sidekiq::Testing.fake! { described_class.new(project, user).attempt_repositories_rollback } Sidekiq::Testing.fake! { described_class.new(project, user).attempt_repositories_rollback }
expect(project.gitlab_shell.exists?(project.repository_storage, path)).to be_truthy expect(TestEnv.storage_dir_exists?(project.repository_storage, path)).to be_truthy
expect(project.gitlab_shell.exists?(project.repository_storage, remove_path)).to be_falsey expect(TestEnv.storage_dir_exists?(project.repository_storage, remove_path)).to be_falsey
end end
end end
......
...@@ -151,7 +151,7 @@ describe Projects::UpdateService do ...@@ -151,7 +151,7 @@ describe Projects::UpdateService do
context 'when we update project but not enabling a wiki' do context 'when we update project but not enabling a wiki' do
it 'does not try to create an empty wiki' do it 'does not try to create an empty wiki' do
Gitlab::Shell.new.rm_directory(project.repository_storage, project.wiki.path) TestEnv.rm_storage_dir(project.repository_storage, project.wiki.path)
result = update_project(project, user, { name: 'test1' }) result = update_project(project, user, { name: 'test1' })
...@@ -172,7 +172,7 @@ describe Projects::UpdateService do ...@@ -172,7 +172,7 @@ describe Projects::UpdateService do
context 'when enabling a wiki' do context 'when enabling a wiki' do
it 'creates a wiki' do it 'creates a wiki' do
project.project_feature.update(wiki_access_level: ProjectFeature::DISABLED) project.project_feature.update(wiki_access_level: ProjectFeature::DISABLED)
Gitlab::Shell.new.rm_directory(project.repository_storage, project.wiki.path) TestEnv.rm_storage_dir(project.repository_storage, project.wiki.path)
result = update_project(project, user, project_feature_attributes: { wiki_access_level: ProjectFeature::ENABLED }) result = update_project(project, user, project_feature_attributes: { wiki_access_level: ProjectFeature::ENABLED })
......
...@@ -243,6 +243,22 @@ module TestEnv ...@@ -243,6 +243,22 @@ module TestEnv
FileUtils.chmod_R 0755, target_repo_path FileUtils.chmod_R 0755, target_repo_path
end end
def rm_storage_dir(storage, dir)
Gitlab::GitalyClient::StorageSettings.allow_disk_access do
repos_path = Gitlab.config.repositories.storages[storage].legacy_disk_path
target_repo_refs_path = File.join(repos_path, dir)
FileUtils.remove_dir(target_repo_refs_path)
end
rescue Errno::ENOENT
end
def storage_dir_exists?(storage, dir)
Gitlab::GitalyClient::StorageSettings.allow_disk_access do
repos_path = Gitlab.config.repositories.storages[storage].legacy_disk_path
File.exist?(File.join(repos_path, dir))
end
end
def create_bare_repository(path) def create_bare_repository(path)
FileUtils.mkdir_p(path) FileUtils.mkdir_p(path)
......
...@@ -68,7 +68,7 @@ describe RepositoryCheck::SingleRepositoryWorker do ...@@ -68,7 +68,7 @@ describe RepositoryCheck::SingleRepositoryWorker do
it 'creates missing wikis' do it 'creates missing wikis' do
project = create(:project, :wiki_enabled) project = create(:project, :wiki_enabled)
Gitlab::Shell.new.rm_directory(project.repository_storage, project.wiki.path) TestEnv.rm_storage_dir(project.repository_storage, project.wiki.path)
subject.perform(project.id) subject.perform(project.id)
...@@ -77,12 +77,12 @@ describe RepositoryCheck::SingleRepositoryWorker do ...@@ -77,12 +77,12 @@ describe RepositoryCheck::SingleRepositoryWorker do
it 'does not create a wiki if the main repo does not exist at all' do it 'does not create a wiki if the main repo does not exist at all' do
project = create(:project, :repository) project = create(:project, :repository)
Gitlab::Shell.new.rm_directory(project.repository_storage, project.path) TestEnv.rm_storage_dir(project.repository_storage, project.path)
Gitlab::Shell.new.rm_directory(project.repository_storage, project.wiki.path) TestEnv.rm_storage_dir(project.repository_storage, project.wiki.path)
subject.perform(project.id) subject.perform(project.id)
expect(Gitlab::Shell.new.exists?(project.repository_storage, project.wiki.path)).to eq(false) expect(TestEnv.storage_dir_exists?(project.repository_storage, project.wiki.path)).to eq(false)
end end
def create_push_event(project) def create_push_event(project)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment