diff options
author | Bob Van Landuyt <bob@gitlab.com> | 2017-04-26 15:45:58 +0200 |
---|---|---|
committer | Bob Van Landuyt <bob@gitlab.com> | 2017-05-01 14:18:51 +0200 |
commit | 650500bd41f6381dd044dd71e9dd14c82d00a3c4 (patch) | |
tree | a84ca4b4369dcb90d760dedd05ef4e619e682f45 | |
parent | d6dfbf02f706c138b43c06886ba8b36512e32b0a (diff) | |
download | gitlab-ce-bvl-handle-missing-repository-storage.tar.gz |
Raise `InvalidStorage` if the storage_path doesn't existbvl-handle-missing-repository-storage
-rw-r--r-- | changelogs/unreleased/bvl-handle-missing-repository-storage.yml | 4 | ||||
-rw-r--r-- | config/gitlab.yml.example | 3 | ||||
-rw-r--r-- | lib/gitlab/git/repository.rb | 12 | ||||
-rw-r--r-- | spec/initializers/6_validations_spec.rb | 10 | ||||
-rw-r--r-- | spec/lib/gitlab/git/repository_spec.rb | 14 |
5 files changed, 41 insertions, 2 deletions
diff --git a/changelogs/unreleased/bvl-handle-missing-repository-storage.yml b/changelogs/unreleased/bvl-handle-missing-repository-storage.yml new file mode 100644 index 00000000000..ab38d8f2d2d --- /dev/null +++ b/changelogs/unreleased/bvl-handle-missing-repository-storage.yml @@ -0,0 +1,4 @@ +--- +title: Raise different exception when repository storage goes offline +merge_request: 10971 +author: diff --git a/config/gitlab.yml.example b/config/gitlab.yml.example index c2eaf263937..2b40e2fa3b5 100644 --- a/config/gitlab.yml.example +++ b/config/gitlab.yml.example @@ -582,6 +582,9 @@ test: path: tmp/tests/pages repositories: storages: + broken: + path: tmp/tests/invalid_repository_storage/ + gitaly_address: unix:tmp/tests/gitaly/missing_gitaly.socket default: path: tmp/tests/repositories/ gitaly_address: unix:tmp/tests/gitaly/gitaly.socket diff --git a/lib/gitlab/git/repository.rb b/lib/gitlab/git/repository.rb index 452dba7971d..748258290c6 100644 --- a/lib/gitlab/git/repository.rb +++ b/lib/gitlab/git/repository.rb @@ -15,9 +15,13 @@ module Gitlab SEARCH_CONTEXT_LINES = 3 NoRepository = Class.new(StandardError) + InvalidStorage = Class.new(StandardError) InvalidBlobName = Class.new(StandardError) InvalidRef = Class.new(StandardError) + # Path to where the repository is stored + attr_reader :storage_path + # Full path to repo attr_reader :path @@ -33,8 +37,8 @@ module Gitlab @repository_storage = repository_storage @relative_path = relative_path - storage_path = Gitlab.config.repositories.storages[@repository_storage]['path'] - @path = File.join(storage_path, @relative_path) + @storage_path = Gitlab.config.repositories.storages[@repository_storage]['path'] + @path = File.join(@storage_path, @relative_path) @name = @relative_path.split("/").last @attributes = Gitlab::Git::Attributes.new(path) end @@ -66,6 +70,10 @@ module Gitlab def rugged @rugged ||= Rugged::Repository.new(path, alternates: alternate_object_directories) rescue Rugged::RepositoryError, Rugged::OSError + unless File.directory?(storage_path) + raise InvalidStorage.new("storage unavailable: #{storage_path}") + end + raise NoRepository.new('no repository for such path') end diff --git a/spec/initializers/6_validations_spec.rb b/spec/initializers/6_validations_spec.rb index 374517fec37..4ebbded3abb 100644 --- a/spec/initializers/6_validations_spec.rb +++ b/spec/initializers/6_validations_spec.rb @@ -84,6 +84,16 @@ describe '6_validations', lib: true do expect { validate_storages_paths }.not_to raise_error end end + + context 'when non-existant storage paths' do + before do + mock_storages('foo' => { 'path' => 'tmp/tests/paths/a/b/broken' }) + end + + it 'throws an error' do + expect { validate_storages_paths }.to raise_error(Errno::ENOENT) + end + end end def mock_storages(storages) diff --git a/spec/lib/gitlab/git/repository_spec.rb b/spec/lib/gitlab/git/repository_spec.rb index f88653cb1fe..bc10ba8583a 100644 --- a/spec/lib/gitlab/git/repository_spec.rb +++ b/spec/lib/gitlab/git/repository_spec.rb @@ -69,6 +69,20 @@ describe Gitlab::Git::Repository, seed_helper: true do repository.rugged end end + + context 'exceptions' do + it 'raises `NoRepository` when there is no repository present' do + repository = described_class.new('default', 'non-existant') + + expect { repository.rugged }.to raise_error(described_class::NoRepository) + end + + it 'raises `InvalidStorage` when the storage is missing' do + repository = described_class.new('broken', TEST_REPO_PATH) + + expect { repository.rugged }.to raise_error(described_class::InvalidStorage) + end + end end describe "#discover_default_branch" do |