Commit aa57a76f authored by Gabriel Mazetto's avatar Gabriel Mazetto

Rescue database errors / connection for Geo.enabled? and return false

parent b05f9e0f
......@@ -31,7 +31,12 @@ module Gitlab
end
def self.enabled?
self.cache_value(:geo_node_enabled) { GeoNode.exists? }
GeoNode.connected? && self.cache_value(:geo_node_enabled) { GeoNode.exists? }
rescue => e
# We can't use the actual classes in rescue because we load only one of them based on database supported
raise e unless %w(PG::UndefinedTable Mysql2::Error).include? e.class.name
false
end
def self.current_node_enabled?
......
......@@ -25,6 +25,43 @@ describe Gitlab::Geo, lib: true do
end
end
describe 'primary?' do
context 'when current node is a primary node' do
before(:each) do
primary_node
end
it 'returns true' do
expect(described_class.primary?).to be_truthy
end
it 'returns false when GeoNode is disabled' do
allow(described_class).to receive(:enabled?) { false }
expect(described_class.primary?).to be_falsey
end
end
end
describe 'secondary?' do
context 'when current node is a secondary node' do
before(:each) do
secondary_node
allow(described_class).to receive(:current_node) { secondary_node }
end
it 'returns true' do
expect(described_class.secondary?).to be_truthy
end
it 'returns false when GeoNode is disabled' do
allow(described_class).to receive(:enabled?) { false }
expect(described_class.secondary?).to be_falsey
end
end
end
describe 'enabled?' do
context 'when any GeoNode exists' do
before do
......@@ -42,6 +79,24 @@ describe Gitlab::Geo, lib: true do
end
end
context 'when there is a database issue' do
it 'returns false when database connection is down' do
allow(GeoNode).to receive(:connected?) { false }
expect(described_class.enabled?).to be_falsey
end
it 'returns false when database schema does not contain required tables' do
if Gitlab::Database.mysql?
allow(GeoNode).to receive(:exists?).and_raise(Mysql2::Error)
else
allow(GeoNode).to receive(:exists?).and_raise(PG::UndefinedTable)
end
expect(described_class.enabled?).to be_falsey
end
end
context 'with RequestStore enabled' do
before do
RequestStore.begin!
......
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