From 68d26ed455552c580316f3df5d03b55f59648435 Mon Sep 17 00:00:00 2001 From: Gabriel Mazetto <gabriel@gitlab.com> Date: Tue, 18 Feb 2025 18:57:17 +0000 Subject: [PATCH] Remove I18n calls as we don't have I18n setup for the CLI yet --- .../lib/gitlab/backup/cli.rb | 4 + .../lib/gitlab/backup/cli/errors.rb | 1 + .../database_missing_connection_error.rb | 22 +++++ .../lib/gitlab/backup/cli/services.rb | 2 + .../gitlab/backup/cli/services/database.rb | 9 ++ .../{targets => services}/gitaly_backup.rb | 2 +- .../{targets => services}/gitaly_client.rb | 2 +- .../gitlab/backup/cli/services/postgres.rb | 12 +++ .../lib/gitlab/backup/cli/shell/command.rb | 16 ++- .../lib/gitlab/backup/cli/targets.rb | 2 - .../gitlab/backup/cli/targets/repositories.rb | 22 ++--- .../lib/gitlab/backup/cli/tasks/artifacts.rb | 2 +- .../lib/gitlab/backup/cli/tasks/builds.rb | 2 +- .../backup/cli/tasks/ci_secure_files.rb | 2 +- .../lib/gitlab/backup/cli/tasks/database.rb | 2 +- .../lib/gitlab/backup/cli/tasks/lfs.rb | 2 +- .../lib/gitlab/backup/cli/tasks/packages.rb | 2 +- .../lib/gitlab/backup/cli/tasks/pages.rb | 2 +- .../lib/gitlab/backup/cli/tasks/registry.rb | 2 +- .../gitlab/backup/cli/tasks/repositories.rb | 2 +- .../backup/cli/tasks/terraform_state.rb | 2 +- .../lib/gitlab/backup/cli/tasks/uploads.rb | 2 +- .../database-different-connection-names.yml | 97 +++++++++++++++++++ .../backup/cli/services/database_spec.rb | 7 ++ .../gitaly_backup_spec.rb | 2 +- .../backup/cli/services/postgres_spec.rb | 18 ++++ .../gitlab/backup/cli/shell/command_spec.rb | 18 ++++ .../backup/cli/targets/repositories_spec.rb | 27 +++--- .../spec/gitlab/backup/cli_spec.rb | 6 ++ 29 files changed, 245 insertions(+), 46 deletions(-) create mode 100644 gems/gitlab-backup-cli/lib/gitlab/backup/cli/errors/database_missing_connection_error.rb rename gems/gitlab-backup-cli/lib/gitlab/backup/cli/{targets => services}/gitaly_backup.rb (99%) rename gems/gitlab-backup-cli/lib/gitlab/backup/cli/{targets => services}/gitaly_client.rb (98%) create mode 100644 gems/gitlab-backup-cli/spec/fixtures/config/database-different-connection-names.yml rename gems/gitlab-backup-cli/spec/gitlab/backup/cli/{targets => services}/gitaly_backup_spec.rb (99%) diff --git a/gems/gitlab-backup-cli/lib/gitlab/backup/cli.rb b/gems/gitlab-backup-cli/lib/gitlab/backup/cli.rb index 79f35b18ce059..2eb4d0d6f2a6b 100644 --- a/gems/gitlab-backup-cli/lib/gitlab/backup/cli.rb +++ b/gems/gitlab-backup-cli/lib/gitlab/backup/cli.rb @@ -48,6 +48,10 @@ def self.update_process_title!(status_message = nil) Process.setproctitle(process_title) end + def self.root + Pathname.new(File.expand_path(File.join(__dir__, '../../../'))) + end + def self.rails_environment! require File.join(GITLAB_PATH, 'config/application') diff --git a/gems/gitlab-backup-cli/lib/gitlab/backup/cli/errors.rb b/gems/gitlab-backup-cli/lib/gitlab/backup/cli/errors.rb index 9d69dc5620c12..045230a07ea27 100644 --- a/gems/gitlab-backup-cli/lib/gitlab/backup/cli/errors.rb +++ b/gems/gitlab-backup-cli/lib/gitlab/backup/cli/errors.rb @@ -6,6 +6,7 @@ module Cli module Errors autoload :DatabaseBackupError, 'gitlab/backup/cli/errors/database_backup_error' autoload :DatabaseConfigMissingError, 'gitlab/backup/cli/errors/database_config_missing_error' + autoload :DatabaseMissingConnectionError, 'gitlab/backup/cli/errors/database_missing_connection_error' autoload :FileBackupError, 'gitlab/backup/cli/errors/file_backup_error' autoload :FileRestoreError, 'gitlab/backup/cli/errors/file_restore_error' autoload :GitalyBackupError, 'gitlab/backup/cli/errors/gitaly_backup_error' diff --git a/gems/gitlab-backup-cli/lib/gitlab/backup/cli/errors/database_missing_connection_error.rb b/gems/gitlab-backup-cli/lib/gitlab/backup/cli/errors/database_missing_connection_error.rb new file mode 100644 index 0000000000000..c11f17823cacf --- /dev/null +++ b/gems/gitlab-backup-cli/lib/gitlab/backup/cli/errors/database_missing_connection_error.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +module Gitlab + module Backup + module Cli + module Errors + class DatabaseMissingConnectionError < StandardError + def initialize(connection_name) + @connection_name = connection_name + super(build_message) + end + + private + + def build_message + "Database connection for #{@connection_name} is missing" + end + end + end + end + end +end diff --git a/gems/gitlab-backup-cli/lib/gitlab/backup/cli/services.rb b/gems/gitlab-backup-cli/lib/gitlab/backup/cli/services.rb index 725106eb8ff9b..8488c66fe2a84 100644 --- a/gems/gitlab-backup-cli/lib/gitlab/backup/cli/services.rb +++ b/gems/gitlab-backup-cli/lib/gitlab/backup/cli/services.rb @@ -6,6 +6,8 @@ module Cli module Services autoload :Database, 'gitlab/backup/cli/services/database' autoload :Postgres, 'gitlab/backup/cli/services/postgres' + autoload :GitalyBackup, 'gitlab/backup/cli/services/gitaly_backup' + autoload :GitalyClient, 'gitlab/backup/cli/services/gitaly_client' end end end diff --git a/gems/gitlab-backup-cli/lib/gitlab/backup/cli/services/database.rb b/gems/gitlab-backup-cli/lib/gitlab/backup/cli/services/database.rb index 611dd773ead2b..0bd3f5380c2dc 100644 --- a/gems/gitlab-backup-cli/lib/gitlab/backup/cli/services/database.rb +++ b/gems/gitlab-backup-cli/lib/gitlab/backup/cli/services/database.rb @@ -4,6 +4,11 @@ module Gitlab module Backup module Cli module Services + # GitLab installations may include multiple databases + # in a denormalized setup + # + # This represents either the main one or one of the denormalized + # with the required connection params class Database # @return [ActiveRecord::DatabaseConfigurations::HashConfig] database configuration attr_reader :configuration @@ -11,6 +16,9 @@ class Database # @return [String] Snapshot ID from the database attr_reader :snapshot_id + # @return [String] Name used in database yml for connection params + attr_reader :connection_name + # Mapping of activerecord config keys to corresponding ENV variables DATABASE_ENV_VARIABLES = { username: 'PGUSER', @@ -33,6 +41,7 @@ class Database def initialize(configuration) @configuration = configuration @snapshot_id = nil + @connection_name = configuration.name end # Database connection params and credentials as PG ENV variables diff --git a/gems/gitlab-backup-cli/lib/gitlab/backup/cli/targets/gitaly_backup.rb b/gems/gitlab-backup-cli/lib/gitlab/backup/cli/services/gitaly_backup.rb similarity index 99% rename from gems/gitlab-backup-cli/lib/gitlab/backup/cli/targets/gitaly_backup.rb rename to gems/gitlab-backup-cli/lib/gitlab/backup/cli/services/gitaly_backup.rb index c3770e6d78a3e..fa0b00899898a 100644 --- a/gems/gitlab-backup-cli/lib/gitlab/backup/cli/targets/gitaly_backup.rb +++ b/gems/gitlab-backup-cli/lib/gitlab/backup/cli/services/gitaly_backup.rb @@ -3,7 +3,7 @@ module Gitlab module Backup module Cli - module Targets + module Services class GitalyBackup # Backup and restores repositories using gitaly-backup # diff --git a/gems/gitlab-backup-cli/lib/gitlab/backup/cli/targets/gitaly_client.rb b/gems/gitlab-backup-cli/lib/gitlab/backup/cli/services/gitaly_client.rb similarity index 98% rename from gems/gitlab-backup-cli/lib/gitlab/backup/cli/targets/gitaly_client.rb rename to gems/gitlab-backup-cli/lib/gitlab/backup/cli/services/gitaly_client.rb index 5385348386ba2..48550d63680d9 100644 --- a/gems/gitlab-backup-cli/lib/gitlab/backup/cli/targets/gitaly_client.rb +++ b/gems/gitlab-backup-cli/lib/gitlab/backup/cli/services/gitaly_client.rb @@ -3,7 +3,7 @@ module Gitlab module Backup module Cli - module Targets + module Services class GitalyClient attr_reader :storages, :gitaly_token diff --git a/gems/gitlab-backup-cli/lib/gitlab/backup/cli/services/postgres.rb b/gems/gitlab-backup-cli/lib/gitlab/backup/cli/services/postgres.rb index eeee924ded9e0..974d64646d8d5 100644 --- a/gems/gitlab-backup-cli/lib/gitlab/backup/cli/services/postgres.rb +++ b/gems/gitlab-backup-cli/lib/gitlab/backup/cli/services/postgres.rb @@ -34,8 +34,20 @@ def entries end end + # @return [Database] + def main_database + each do |database| + return database if database.connection_name == 'main' + end + + raise Gitlab::Backup::Cli::Errors::DatabaseMissingConnectionError, 'main' + end + private + # Return ActiveRecord parsed database configurations object + # + # @return [ActiveRecord::DatabaseConfigurations] def database_configurations return @database_configurations if defined?(@database_configurations) diff --git a/gems/gitlab-backup-cli/lib/gitlab/backup/cli/shell/command.rb b/gems/gitlab-backup-cli/lib/gitlab/backup/cli/shell/command.rb index b273ad2e4901e..66228b6c67382 100644 --- a/gems/gitlab-backup-cli/lib/gitlab/backup/cli/shell/command.rb +++ b/gems/gitlab-backup-cli/lib/gitlab/backup/cli/shell/command.rb @@ -7,8 +7,12 @@ module Shell # Abstraction to control shell command execution # It provides an easier API to common usages class Command < Base + # @return [Hash<String,String>] a hash containing key=>values to be set as ENV when running the command attr_reader :env + # @return [String|Pathname] A path where a Shell::Command should run from + attr_reader :chdir + # Result data structure from running a command # # @attr [String] stdout @@ -26,10 +30,12 @@ class Command < Base # @example Usage # Shell::Command.new('echo', 'Some amazing output').capture - # @param [Array<String>] cmd_args - # @param [Hash<String,String>] env - def initialize(*cmd_args, env: {}) + # @param [Array<String>] cmd_args a list of command and its arguments to run + # @param [String|Pathname] chdir a path where Shell::Command should run from + # @param [Hash<String,String>] env a hash containing key=>values to be set as ENV when running the command + def initialize(*cmd_args, chdir: Gitlab::Backup::Cli.root, env: {}) @cmd_args = cmd_args.freeze + @chdir = chdir @env = env.freeze end @@ -54,7 +60,7 @@ def cmd_args(with_env: false) # @return [Command::Result] Captured output from executing a process def capture start = Time.now - stdout, stderr, status = Open3.capture3(env, *cmd_args) + stdout, stderr, status = Open3.capture3(env, *cmd_args, chdir: chdir) duration = Time.now - start Result.new(stdout: stdout, stderr: stderr, status: status, duration: duration) @@ -80,7 +86,7 @@ def run_single_pipeline!(input: nil, output: nil) options[:in] = input if input # redirect stdin options[:out] = output if output # redirect stdout - status_list = Open3.pipeline(cmd_args(with_env: true), **options) + status_list = Open3.pipeline(cmd_args(with_env: true), chdir: chdir, **options) duration = Time.now - start err_write.close # close the pipe before reading diff --git a/gems/gitlab-backup-cli/lib/gitlab/backup/cli/targets.rb b/gems/gitlab-backup-cli/lib/gitlab/backup/cli/targets.rb index 8a6679276769a..553d4349cbf0c 100644 --- a/gems/gitlab-backup-cli/lib/gitlab/backup/cli/targets.rb +++ b/gems/gitlab-backup-cli/lib/gitlab/backup/cli/targets.rb @@ -8,8 +8,6 @@ module Targets autoload :Database, 'gitlab/backup/cli/targets/database' autoload :Files, 'gitlab/backup/cli/targets/files' autoload :ObjectStorage, 'gitlab/backup/cli/targets/object_storage' - autoload :GitalyBackup, 'gitlab/backup/cli/targets/gitaly_backup' - autoload :GitalyClient, 'gitlab/backup/cli/targets/gitaly_client' autoload :Repositories, 'gitlab/backup/cli/targets/repositories' end end diff --git a/gems/gitlab-backup-cli/lib/gitlab/backup/cli/targets/repositories.rb b/gems/gitlab-backup-cli/lib/gitlab/backup/cli/targets/repositories.rb index 9cb1b430f80bf..6d954ddd7514c 100644 --- a/gems/gitlab-backup-cli/lib/gitlab/backup/cli/targets/repositories.rb +++ b/gems/gitlab-backup-cli/lib/gitlab/backup/cli/targets/repositories.rb @@ -9,27 +9,25 @@ module Targets # Backup and restores repositories by querying the database class Repositories < Target def dump(destination) - strategy.start(:create, destination) + gitaly_backup.start(:create, destination) enqueue_consecutive ensure - strategy.finish! + gitaly_backup.finish! end def restore(source) - strategy.start(:restore, - source, - remove_all_repositories: remove_all_repositories) + gitaly_backup.start(:restore, source, remove_all_repositories: remove_all_repositories) enqueue_consecutive ensure - strategy.finish! + gitaly_backup.finish! restore_object_pools end - def strategy - @strategy ||= GitalyBackup.new(context) + def gitaly_backup + @gitaly_backup ||= Services::GitalyBackup.new(context) end private @@ -54,16 +52,16 @@ def enqueue_consecutive_snippets end def enqueue_project(project) - strategy.enqueue(project, Gitlab::Backup::Cli::RepoType::PROJECT) - strategy.enqueue(project, Gitlab::Backup::Cli::RepoType::WIKI) + gitaly_backup.enqueue(project, Gitlab::Backup::Cli::RepoType::PROJECT) + gitaly_backup.enqueue(project, Gitlab::Backup::Cli::RepoType::WIKI) return unless project.design_management_repository - strategy.enqueue(project.design_management_repository, Gitlab::Backup::Cli::RepoType::DESIGN) + gitaly_backup.enqueue(project.design_management_repository, Gitlab::Backup::Cli::RepoType::DESIGN) end def enqueue_snippet(snippet) - strategy.enqueue(snippet, Gitlab::Backup::Cli::RepoType::SNIPPET) + gitaly_backup.enqueue(snippet, Gitlab::Backup::Cli::RepoType::SNIPPET) end def project_relation diff --git a/gems/gitlab-backup-cli/lib/gitlab/backup/cli/tasks/artifacts.rb b/gems/gitlab-backup-cli/lib/gitlab/backup/cli/tasks/artifacts.rb index 9af8ceaaa8844..f979bf6a32954 100644 --- a/gems/gitlab-backup-cli/lib/gitlab/backup/cli/tasks/artifacts.rb +++ b/gems/gitlab-backup-cli/lib/gitlab/backup/cli/tasks/artifacts.rb @@ -7,7 +7,7 @@ module Tasks class Artifacts < Task def self.id = 'artifacts' - def human_name = _('artifacts') + def human_name = 'Artifacts' def destination_path = 'artifacts.tar.gz' diff --git a/gems/gitlab-backup-cli/lib/gitlab/backup/cli/tasks/builds.rb b/gems/gitlab-backup-cli/lib/gitlab/backup/cli/tasks/builds.rb index ed5dffe5071f1..530e5fc84d39c 100644 --- a/gems/gitlab-backup-cli/lib/gitlab/backup/cli/tasks/builds.rb +++ b/gems/gitlab-backup-cli/lib/gitlab/backup/cli/tasks/builds.rb @@ -7,7 +7,7 @@ module Tasks class Builds < Task def self.id = 'builds' - def human_name = _('builds') + def human_name = 'CI Builds' def destination_path = 'builds.tar.gz' diff --git a/gems/gitlab-backup-cli/lib/gitlab/backup/cli/tasks/ci_secure_files.rb b/gems/gitlab-backup-cli/lib/gitlab/backup/cli/tasks/ci_secure_files.rb index 55a8f84343db9..e8a795e8e3e23 100644 --- a/gems/gitlab-backup-cli/lib/gitlab/backup/cli/tasks/ci_secure_files.rb +++ b/gems/gitlab-backup-cli/lib/gitlab/backup/cli/tasks/ci_secure_files.rb @@ -7,7 +7,7 @@ module Tasks class CiSecureFiles < Task def self.id = 'ci_secure_files' - def human_name = _('ci secure files') + def human_name = 'CI Secure Files' def destination_path = 'ci_secure_files.tar.gz' diff --git a/gems/gitlab-backup-cli/lib/gitlab/backup/cli/tasks/database.rb b/gems/gitlab-backup-cli/lib/gitlab/backup/cli/tasks/database.rb index 6574027e076e2..0c46ba80ebf79 100644 --- a/gems/gitlab-backup-cli/lib/gitlab/backup/cli/tasks/database.rb +++ b/gems/gitlab-backup-cli/lib/gitlab/backup/cli/tasks/database.rb @@ -7,7 +7,7 @@ module Tasks class Database < Task def self.id = 'db' - def human_name = _('databases') + def human_name = 'Databases' def destination_path = 'db' diff --git a/gems/gitlab-backup-cli/lib/gitlab/backup/cli/tasks/lfs.rb b/gems/gitlab-backup-cli/lib/gitlab/backup/cli/tasks/lfs.rb index 50cb98eadbe73..8ae5066228781 100644 --- a/gems/gitlab-backup-cli/lib/gitlab/backup/cli/tasks/lfs.rb +++ b/gems/gitlab-backup-cli/lib/gitlab/backup/cli/tasks/lfs.rb @@ -7,7 +7,7 @@ module Tasks class Lfs < Task def self.id = 'lfs' - def human_name = _('lfs objects') + def human_name = 'LFS Objects' def destination_path = 'lfs.tar.gz' diff --git a/gems/gitlab-backup-cli/lib/gitlab/backup/cli/tasks/packages.rb b/gems/gitlab-backup-cli/lib/gitlab/backup/cli/tasks/packages.rb index 6696a23df42d1..c725441fc93d8 100644 --- a/gems/gitlab-backup-cli/lib/gitlab/backup/cli/tasks/packages.rb +++ b/gems/gitlab-backup-cli/lib/gitlab/backup/cli/tasks/packages.rb @@ -7,7 +7,7 @@ module Tasks class Packages < Task def self.id = 'packages' - def human_name = _('packages') + def human_name = 'Packages' def destination_path = 'packages.tar.gz' diff --git a/gems/gitlab-backup-cli/lib/gitlab/backup/cli/tasks/pages.rb b/gems/gitlab-backup-cli/lib/gitlab/backup/cli/tasks/pages.rb index e5715efb33cb1..d61eeda3e7a10 100644 --- a/gems/gitlab-backup-cli/lib/gitlab/backup/cli/tasks/pages.rb +++ b/gems/gitlab-backup-cli/lib/gitlab/backup/cli/tasks/pages.rb @@ -11,7 +11,7 @@ class Pages < Task def self.id = 'pages' - def human_name = _('pages') + def human_name = 'Pages' def destination_path = 'pages.tar.gz' diff --git a/gems/gitlab-backup-cli/lib/gitlab/backup/cli/tasks/registry.rb b/gems/gitlab-backup-cli/lib/gitlab/backup/cli/tasks/registry.rb index 9d6893a969e04..dfc7a9e82df6c 100644 --- a/gems/gitlab-backup-cli/lib/gitlab/backup/cli/tasks/registry.rb +++ b/gems/gitlab-backup-cli/lib/gitlab/backup/cli/tasks/registry.rb @@ -9,7 +9,7 @@ def self.id = 'registry' def enabled = Gitlab.config.registry.enabled - def human_name = _('container registry images') + def human_name = 'Container Registry Images' def destination_path = 'registry.tar.gz' diff --git a/gems/gitlab-backup-cli/lib/gitlab/backup/cli/tasks/repositories.rb b/gems/gitlab-backup-cli/lib/gitlab/backup/cli/tasks/repositories.rb index 5f20f4c233e55..d9d627ce98387 100644 --- a/gems/gitlab-backup-cli/lib/gitlab/backup/cli/tasks/repositories.rb +++ b/gems/gitlab-backup-cli/lib/gitlab/backup/cli/tasks/repositories.rb @@ -7,7 +7,7 @@ module Tasks class Repositories < Task def self.id = 'repositories' - def human_name = _('repositories') + def human_name = 'Repositories' def destination_path = 'repositories' diff --git a/gems/gitlab-backup-cli/lib/gitlab/backup/cli/tasks/terraform_state.rb b/gems/gitlab-backup-cli/lib/gitlab/backup/cli/tasks/terraform_state.rb index 01cc7cf18e48f..9d2e366bd662b 100644 --- a/gems/gitlab-backup-cli/lib/gitlab/backup/cli/tasks/terraform_state.rb +++ b/gems/gitlab-backup-cli/lib/gitlab/backup/cli/tasks/terraform_state.rb @@ -7,7 +7,7 @@ module Tasks class TerraformState < Task def self.id = 'terraform_state' - def human_name = _('terraform states') + def human_name = 'Terraform States' def destination_path = 'terraform_state.tar.gz' diff --git a/gems/gitlab-backup-cli/lib/gitlab/backup/cli/tasks/uploads.rb b/gems/gitlab-backup-cli/lib/gitlab/backup/cli/tasks/uploads.rb index e2979fad614c8..a3f2d1c03e74f 100644 --- a/gems/gitlab-backup-cli/lib/gitlab/backup/cli/tasks/uploads.rb +++ b/gems/gitlab-backup-cli/lib/gitlab/backup/cli/tasks/uploads.rb @@ -7,7 +7,7 @@ module Tasks class Uploads < Task def self.id = 'uploads' - def human_name = _('uploads') + def human_name = 'Uploads' def destination_path = 'uploads.tar.gz' diff --git a/gems/gitlab-backup-cli/spec/fixtures/config/database-different-connection-names.yml b/gems/gitlab-backup-cli/spec/fixtures/config/database-different-connection-names.yml new file mode 100644 index 0000000000000..3ecf6a51cdb59 --- /dev/null +++ b/gems/gitlab-backup-cli/spec/fixtures/config/database-different-connection-names.yml @@ -0,0 +1,97 @@ +# +# PRODUCTION +# +production: + other: + adapter: postgresql + encoding: unicode + database: gitlabhq_production + username: git + password: "secure password" + host: localhost + # load_balancing: + # hosts: + # - host1.example.com + # - host2.example.com + # discover: + # nameserver: 1.2.3.4 + # port: 8600 + # record: secondary.postgresql.service.consul + # interval: 300 + ci: + adapter: postgresql + encoding: unicode + database: gitlabhq_production + database_tasks: false + username: git + password: "secure password" + host: localhost + +# +# Development specific +# +development: + other: + adapter: postgresql + encoding: unicode + database: gitlabhq_development + username: postgres + password: "secure password" + host: localhost + variables: + statement_timeout: 15s + ci: + adapter: postgresql + encoding: unicode + database: gitlabhq_development_ci + database_tasks: false + username: postgres + password: "secure password" + host: localhost + variables: + statement_timeout: 15s + +# +# Staging specific +# +staging: + other: + adapter: postgresql + encoding: unicode + database: gitlabhq_staging + username: git + password: "secure password" + host: localhost + ci: + adapter: postgresql + encoding: unicode + database: gitlabhq_staging + database_tasks: false + username: git + password: "secure password" + host: localhost + +test: &test + other: &main + adapter: postgresql + encoding: unicode + database: gitlabhq_test + username: postgres + password: + host: localhost + prepared_statements: false + variables: + statement_timeout: 15s + other_replica: + <<: *main + database_tasks: false + ci: + adapter: postgresql + encoding: unicode + database: gitlabhq_ci_test + username: postgres + password: + host: localhost + prepared_statements: false + variables: + statement_timeout: 15s diff --git a/gems/gitlab-backup-cli/spec/gitlab/backup/cli/services/database_spec.rb b/gems/gitlab-backup-cli/spec/gitlab/backup/cli/services/database_spec.rb index 5e03311e8350e..e4e6626a7035c 100644 --- a/gems/gitlab-backup-cli/spec/gitlab/backup/cli/services/database_spec.rb +++ b/gems/gitlab-backup-cli/spec/gitlab/backup/cli/services/database_spec.rb @@ -55,6 +55,13 @@ expect(database.connection_params).to include(expected) end end + + describe '#connection_name' do + it 'returns a connection name string' do + expect(database.connection_name).to be_a(String) + expect(database.connection_name).to eq('main') + end + end end context 'with test connection' do diff --git a/gems/gitlab-backup-cli/spec/gitlab/backup/cli/targets/gitaly_backup_spec.rb b/gems/gitlab-backup-cli/spec/gitlab/backup/cli/services/gitaly_backup_spec.rb similarity index 99% rename from gems/gitlab-backup-cli/spec/gitlab/backup/cli/targets/gitaly_backup_spec.rb rename to gems/gitlab-backup-cli/spec/gitlab/backup/cli/services/gitaly_backup_spec.rb index ab8ad369910b4..b7d4ebbcdc9e9 100644 --- a/gems/gitlab-backup-cli/spec/gitlab/backup/cli/targets/gitaly_backup_spec.rb +++ b/gems/gitlab-backup-cli/spec/gitlab/backup/cli/services/gitaly_backup_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' require 'open3' -RSpec.describe Gitlab::Backup::Cli::Targets::GitalyBackup do +RSpec.describe Gitlab::Backup::Cli::Services::GitalyBackup do let(:context) { Gitlab::Backup::Cli::Context.build } let(:gitaly_backup) { described_class.new(context) } diff --git a/gems/gitlab-backup-cli/spec/gitlab/backup/cli/services/postgres_spec.rb b/gems/gitlab-backup-cli/spec/gitlab/backup/cli/services/postgres_spec.rb index f3afe12a6e21d..788f0afcdb6b1 100644 --- a/gems/gitlab-backup-cli/spec/gitlab/backup/cli/services/postgres_spec.rb +++ b/gems/gitlab-backup-cli/spec/gitlab/backup/cli/services/postgres_spec.rb @@ -29,4 +29,22 @@ expect { |b| postgres.each(&b) }.to yield_successive_args(*postgres.entries) end end + + describe '#main_database' do + it 'returns a Database object for the main configuration entry' do + main_database = postgres.entries.find { |e| e.connection_name == 'main' } + + expect(postgres.main_database).to be_an(Gitlab::Backup::Cli::Services::Database) + expect(postgres.main_database).to eq(main_database) + end + + context 'with database configuration missing main entry' do + it 'raises an error' do + database_fixture_path = fixtures_path.join('config/database-different-connection-names.yml') + allow(context).to receive(:database_config_file_path).and_return(database_fixture_path) + + expect { postgres.main_database }.to raise_error(Gitlab::Backup::Cli::Errors::DatabaseMissingConnectionError) + end + end + end end diff --git a/gems/gitlab-backup-cli/spec/gitlab/backup/cli/shell/command_spec.rb b/gems/gitlab-backup-cli/spec/gitlab/backup/cli/shell/command_spec.rb index 2249f9f0b0915..be40add1e3092 100644 --- a/gems/gitlab-backup-cli/spec/gitlab/backup/cli/shell/command_spec.rb +++ b/gems/gitlab-backup-cli/spec/gitlab/backup/cli/shell/command_spec.rb @@ -86,6 +86,12 @@ expect(result.stdout.chomp).to eq('variable value data') end + + it 'switches to the directory set by @chdir' do + result = command.new('pwd', chdir: tmpdir).capture + + expect(result.stdout.chomp).to eq(tmpdir.to_s) + end end describe '#run_single_pipeline!' do @@ -105,6 +111,18 @@ expect(output).to eq('variable value data') end + it 'switches to the directory set by @chdir' do + pwd_command = command.new('pwd', chdir: tmpdir) + read_io, write_io = IO.pipe + + pwd_command.run_single_pipeline!(output: write_io) + write_io.close + output = read_io.read.chomp + read_io.close + + expect(output).to eq(tmpdir.to_s) + end + it 'accepts stdin and stdout redirection' do echo_command = command.new(%(ruby -e "print 'stdin is : ' + STDIN.readline")) input_r, input_w = IO.pipe diff --git a/gems/gitlab-backup-cli/spec/gitlab/backup/cli/targets/repositories_spec.rb b/gems/gitlab-backup-cli/spec/gitlab/backup/cli/targets/repositories_spec.rb index 160dd1802307a..30efc84e12869 100644 --- a/gems/gitlab-backup-cli/spec/gitlab/backup/cli/targets/repositories_spec.rb +++ b/gems/gitlab-backup-cli/spec/gitlab/backup/cli/targets/repositories_spec.rb @@ -4,25 +4,26 @@ RSpec.describe Gitlab::Backup::Cli::Targets::Repositories do let(:context) { Gitlab::Backup::Cli::Context.build } - let(:strategy) { repo_target.strategy } + let(:gitaly_backup) { repo_target.gitaly_backup } subject(:repo_target) { described_class.new(context) } describe '#dump' do - it 'starts and finishes the strategy' do - expect(strategy).to receive(:start).with(:create, '/path/to/destination') + it 'starts and finishes the gitaly_backup' do + expect(gitaly_backup).to receive(:start).with(:create, '/path/to/destination') expect(repo_target).to receive(:enqueue_consecutive) - expect(strategy).to receive(:finish!) + expect(gitaly_backup).to receive(:finish!) repo_target.dump('/path/to/destination') end end describe '#restore' do - it 'starts and finishes the strategy' do - expect(strategy).to receive(:start).with(:restore, '/path/to/destination', remove_all_repositories: ["default"]) + it 'starts and finishes the gitaly_backup' do + expect(gitaly_backup).to receive(:start) + .with(:restore, '/path/to/destination', remove_all_repositories: ["default"]) expect(repo_target).to receive(:enqueue_consecutive) - expect(strategy).to receive(:finish!) + expect(gitaly_backup).to receive(:finish!) expect(repo_target).to receive(:restore_object_pools) repo_target.restore('/path/to/destination') @@ -42,8 +43,8 @@ let(:project) { instance_double('Project', design_management_repository: nil) } it 'enqueues project and wiki' do - expect(strategy).to receive(:enqueue).with(project, Gitlab::Backup::Cli::RepoType::PROJECT) - expect(strategy).to receive(:enqueue).with(project, Gitlab::Backup::Cli::RepoType::WIKI) + expect(gitaly_backup).to receive(:enqueue).with(project, Gitlab::Backup::Cli::RepoType::PROJECT) + expect(gitaly_backup).to receive(:enqueue).with(project, Gitlab::Backup::Cli::RepoType::WIKI) repo_target.send(:enqueue_project, project) end @@ -53,9 +54,9 @@ let(:project) { instance_double('Project', design_management_repository: design_repo) } it 'enqueues project, wiki, and design' do - expect(strategy).to receive(:enqueue).with(project, Gitlab::Backup::Cli::RepoType::PROJECT) - expect(strategy).to receive(:enqueue).with(project, Gitlab::Backup::Cli::RepoType::WIKI) - expect(strategy).to receive(:enqueue).with(design_repo, Gitlab::Backup::Cli::RepoType::DESIGN) + expect(gitaly_backup).to receive(:enqueue).with(project, Gitlab::Backup::Cli::RepoType::PROJECT) + expect(gitaly_backup).to receive(:enqueue).with(project, Gitlab::Backup::Cli::RepoType::WIKI) + expect(gitaly_backup).to receive(:enqueue).with(design_repo, Gitlab::Backup::Cli::RepoType::DESIGN) repo_target.send(:enqueue_project, project) end @@ -66,7 +67,7 @@ let(:snippet) { instance_double('Snippet') } it 'enqueues the snippet' do - expect(strategy).to receive(:enqueue).with(snippet, Gitlab::Backup::Cli::RepoType::SNIPPET) + expect(gitaly_backup).to receive(:enqueue).with(snippet, Gitlab::Backup::Cli::RepoType::SNIPPET) repo_target.send(:enqueue_snippet, snippet) end diff --git a/gems/gitlab-backup-cli/spec/gitlab/backup/cli_spec.rb b/gems/gitlab-backup-cli/spec/gitlab/backup/cli_spec.rb index b59c3bc9175f6..8d2cf7511fe62 100644 --- a/gems/gitlab-backup-cli/spec/gitlab/backup/cli_spec.rb +++ b/gems/gitlab-backup-cli/spec/gitlab/backup/cli_spec.rb @@ -47,6 +47,12 @@ end end + describe '.root' do + it 'returns a Pathname' do + expect(cli.root).to be_a_kind_of(Pathname) + end + end + def get_process_title ps = `ps -p #{Process.pid} -o command` ps.split("\n").last.strip -- GitLab