Commit 7977917e authored by Mayra Cabrera's avatar Mayra Cabrera

Merge branch 'jc-replicas-rake' into 'master'

Add Praefect rake task to print out replica checksums

See merge request gitlab-org/gitlab!28369
parents 3824edb1 1cda0cd3
---
title: Add Praefect rake task to print out replica checksums
merge_request: 28369
author:
type: added
namespace :gitlab do
namespace :praefect do
def int?(string)
true if Integer(string) rescue false
end
def print_checksums(header, row)
header.each_with_index do |val, i|
width = [val.length, row[i].length].max
header[i] = header[i].ljust(width)
row[i] = row[i].ljust(width)
end
header_str = header.join(' | ')
puts header_str
puts '-' * header_str.length
puts row.join(' | ')
end
desc 'GitLab | Praefect | Check replicas'
task :replicas, [:project_id] => :gitlab_environment do |t, args|
warn_user_is_not_gitlab
unless int?(args.project_id)
puts 'argument must be a valid project_id'
next
end
project = Project.find_by_id(args.project_id)
if project.nil?
puts 'No project was found with that id'
next
end
begin
replicas_resp = project.repository.replicas
sorted_replicas = replicas_resp.replicas.sort_by { |r| r.repository.storage_name }
header = ['Project name'] << "#{replicas_resp.primary.repository.storage_name} (primary)"
header.concat(sorted_replicas.map { |r| r.repository.storage_name })
row = [project.name] << replicas_resp.primary.checksum
row.concat(sorted_replicas.map {|r| r.checksum})
rescue
puts 'Something went wrong when getting replicas.'
next
end
puts "\n"
print_checksums(header, row)
end
end
end
# frozen_string_literal: true
require 'rake_helper'
describe 'gitlab:praefect:replicas' do
before do
Rake.application.rake_require 'tasks/gitlab/praefect'
end
let(:project) { create(:project, :repository) }
let(:repository) { project.repository }
describe 'replicas', :praefect do
context 'when a valid project id is used as the argument' do
let(:project_arg) { project.id }
it "calls praefect info service's replicas method" do
expect_any_instance_of(Gitlab::GitalyClient::PraefectInfoService).to receive(:replicas).and_call_original
run_rake_task('gitlab:praefect:replicas', project_arg)
end
it 'prints out the expected row' do
row = /#{project.name}\s+\| #{project.repository.checksum}/
expect { run_rake_task('gitlab:praefect:replicas', project_arg) }.to output(row).to_stdout
end
end
context 'when a non existent project id is used as the argument' do
let(:project_arg) { '2' }
it "does not call praefect info service's replicas method" do
expect_any_instance_of(Gitlab::GitalyClient::PraefectInfoService).not_to receive(:replicas)
run_rake_task('gitlab:praefect:replicas', project_arg)
end
end
context 'when replicas throws an exception' do
before do
allow_next_instance_of(Gitlab::GitalyClient::PraefectInfoService) do |instance|
expect(instance).to receive(:replicas).and_raise("error")
end
end
it 'aborts with the correct error message' do
expect { run_rake_task('gitlab:praefect:replicas', project.id) }.to output("Something went wrong when getting replicas.\n").to_stdout
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