Commit 28ffe701 authored by Douglas Barbosa Alexandre's avatar Douglas Barbosa Alexandre

Merge branch 'mk/add-replication-working-rake-task' into 'master'

Geo: Add rake task to check DB replication

Closes #245530

See merge request gitlab-org/gitlab!41618
parents 1dd797d7 6ee0ea3c
---
title: 'Geo: Add rake task to check if DB replication is working'
merge_request: 41618
author:
type: added
......@@ -32,5 +32,31 @@ namespace :gitlab do
exit 1
end
end
desc 'GitLab | Geo | Check Geo database replication'
task check_database_replication_working: :gitlab_environment do
unless ::Gitlab::Geo.secondary?
abort 'This command is only available on a secondary node'.color(:red)
end
geo_health_check = Gitlab::Geo::HealthCheck.new
enabled = geo_health_check.replication_enabled?
success = enabled && geo_health_check.replication_working?
if success
puts 'SUCCESS - Database replication is working.'.color(:green)
elsif enabled
abort "ERROR - Database replication is enabled, but not working.\n"\
"This rake task is intended for programmatic use. Please run\n"\
"the full Geo check task for more information:\n"\
" gitlab-rake gitlab:geo:check".color(:red)
else
abort "ERROR - Database replication is not enabled.\n"\
"This rake task is intended for programmatic use. Please run\n"\
"the full Geo check task for more information:\n"\
" gitlab-rake gitlab:geo:check".color(:red)
end
end
end
end
......@@ -50,4 +50,55 @@ RSpec.describe 'gitlab:geo rake tasks', :geo do
end
end
end
describe 'gitlab:geo:check_database_replication_working' do
let(:run_task) do
run_rake_task('gitlab:geo:check_database_replication_working')
end
before do
stub_secondary_node
end
context 'when DB replication is enabled' do
let(:enabled) { true }
before do
allow_next_instance_of(Gitlab::Geo::HealthCheck) do |health_check|
allow(health_check).to receive(:replication_enabled?).and_return(enabled)
allow(health_check).to receive(:replication_working?).and_return(working)
end
end
context 'when DB replication is working' do
let(:working) { true }
it 'prints a success message' do
expect { run_task }.to output(/SUCCESS - Database replication is working/).to_stdout
end
end
context 'when DB replication is not working' do
let(:working) { false }
it 'exits with non-success code' do
expect { run_task }.to abort_execution.with_message(/ERROR - Database replication is enabled, but not working/)
end
end
end
context 'when DB replication is not enabled' do
let(:enabled) { false }
before do
allow_next_instance_of(Gitlab::Geo::HealthCheck) do |health_check|
allow(health_check).to receive(:replication_enabled?).and_return(enabled)
end
end
it 'exits with non-success code' do
expect { run_task }.to abort_execution.with_message(/ERROR - Database replication is not enabled/)
end
end
end
end
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