diff --git a/gems/gitlab-backup-cli/lib/gitlab/backup/cli.rb b/gems/gitlab-backup-cli/lib/gitlab/backup/cli.rb index 79f35b18ce059a7db89ca3e03dbd67c69e4dc32b..2eb4d0d6f2a6b2dfa67573950df5b77748f9c6bf 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 9d69dc5620c1204b2324c6ba6b899f4793d67f5e..045230a07ea278145abfa6582818c0672f8053d9 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 0000000000000000000000000000000000000000..c11f17823cacff7dc5e6dfce27cd651e28759e82 --- /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 725106eb8ff9b6411d59bafd12e993592b9a60f5..8488c66fe2a8453dd0386bc6de3b9d7884608da6 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 611dd773ead2b283fe9d04ff5b91d163c40a27dd..0bd3f5380c2dca24b575460dcb29895912b66dcb 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 c3770e6d78a3e530f30a10e2a82ec80d45876a51..fa0b00899898ad227096ab7edd91265ab955db75 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 5385348386ba2a6f6f8200fd1363593ea5a8e35e..48550d63680d9ec8cecbfc0833468d49d4140d40 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 eeee924ded9e038d85c686b766f947a24d77b611..974d64646d8d5a72089a0937976e47eae3bea1fe 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 b273ad2e4901e2ffde270209ed7889ff2ba5967c..66228b6c67382e83ab959910e856028cb4642df4 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 8a6679276769aa1b6fb8e6f66ddaf6d1a9048d53..553d4349cbf0cdf6a9b8d5be00c505f182f77097 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 9cb1b430f80bfe979cf8c35a0b591bc80a8ac8a1..6d954ddd7514c7f7a03b01556b51647ea4d89be0 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 9af8ceaaa8844815415e1dc0d7bb321498c2af0d..f979bf6a329546393334224463a9367c20f5988a 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 ed5dffe5071f1abf279070892d7e781888684baa..530e5fc84d39c7c60d218c3fc3c44edb0265f368 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 55a8f84343db9812d653b36f525a8fbea25e5df5..e8a795e8e3e23635ec9bf52e9ed55324fd5d2982 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 6574027e076e25b146870ed4bdc3ba0a56da93a6..0c46ba80ebf79679e2f2d712e421a2f554746bde 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 50cb98eadbe73e90a418fb7c59d8d45a0e4abddc..8ae5066228781ccdab2c7915b44eb2370cef704e 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 6696a23df42d1a01d4fbc0ffd90753ae0860c6cf..c725441fc93d88b7e5206d72a17e9b63e50a33ef 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 e5715efb33cb10d67d646feb0c4bb5127ce8a78b..d61eeda3e7a10ef445e7fb35b3f6834455d768ad 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 9d6893a969e04eadeb74a282e5a740152c6bdd3d..dfc7a9e82df6c504508236d23d22b905a211909c 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 5f20f4c233e558b1db44685428ac6ad2ae3a7639..d9d627ce98387c0bf730f809437f13527faca30e 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 01cc7cf18e48f76096a61d27f3adab735d8f3756..9d2e366bd662bf3aaf3ccc672bff96d4a911d1e5 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 e2979fad614c8f13dae6467aa58bc3c1ae019908..a3f2d1c03e74fd9a97b36e628717576911c15f79 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 0000000000000000000000000000000000000000..3ecf6a51cdb593a0a380c140c3d71f22ce7ae772 --- /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 5e03311e8350e58fe59ebbb75ae6a39da51d9866..e4e6626a7035c59735a6d51a82ccaa835e0b07b4 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 ab8ad369910b4da2525006c63e432e20d457ca6f..b7d4ebbcdc9e978e42ce9addb733c334088def07 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 f3afe12a6e21d383a0725cf7c0527f9356fad889..788f0afcdb6b118864b1b0a3c02c650422a27c0b 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 2249f9f0b0915580f0ca0382e2d9ee312c000f52..be40add1e3092772efd4beba5ff4c1fd6e8d917c 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 160dd1802307ab4390d3471ace44f4a5f7f7041f..30efc84e12869997e6d6737c1646c102cca6b294 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 b59c3bc9175f67548d158c6c2883da5ee3d45434..8d2cf7511fe62a9cd0bf6a258e6aed8be2e3e8ec 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