From 46cae556989f202b9ba890c0479b9826fcce1175 Mon Sep 17 00:00:00 2001 From: Gabriel Mazetto <gabriel@gitlab.com> Date: Tue, 17 Sep 2024 16:20:32 +0200 Subject: [PATCH] Refactor Context specs to share between the two contexts --- .../cli/context/omnibus_context_spec.rb | 36 ++- .../backup/cli/context/source_context_spec.rb | 303 +---------------- .../context_shared_examples.rb | 306 ++++++++++++++++++ 3 files changed, 336 insertions(+), 309 deletions(-) create mode 100644 gems/gitlab-backup-cli/spec/support/shared_examples/context_shared_examples.rb diff --git a/gems/gitlab-backup-cli/spec/gitlab/backup/cli/context/omnibus_context_spec.rb b/gems/gitlab-backup-cli/spec/gitlab/backup/cli/context/omnibus_context_spec.rb index 85a79f7e668f..6229c3689b32 100644 --- a/gems/gitlab-backup-cli/spec/gitlab/backup/cli/context/omnibus_context_spec.rb +++ b/gems/gitlab-backup-cli/spec/gitlab/backup/cli/context/omnibus_context_spec.rb @@ -3,17 +3,10 @@ RSpec.describe Gitlab::Backup::Cli::Context::OmnibusContext do subject(:context) { described_class.new } - let(:fake_gitlab_basepath) { Pathname.new(Dir.mktmpdir('gitlab', temp_path)) } let(:fake_omnibus_config_basepath) { Pathname.new(Dir.mktmpdir('omnibus', temp_path)) } let(:omnibus_config_path) { fake_omnibus_config_basepath.join('gitlab-backup-cli-config.yml') } - before do - allow(context).to receive(:gitlab_basepath).and_return(fake_gitlab_basepath) - FileUtils.mkdir fake_gitlab_basepath.join('config') - end - after do - fake_gitlab_basepath.rmtree fake_omnibus_config_basepath.rmtree end @@ -69,6 +62,18 @@ end end + it_behaves_like 'context exposing all common configuration methods' do + before do + custom_config = { + gitlab: { + config_path: "#{fake_gitlab_basepath}/config/gitlab.yml" + } + } + + patch_omnibus_config_fixture!('gitlab-backup-cli-config.yml', custom_config) + end + end + def stub_omnibus_config_env(value) stub_env(described_class::OMNIBUS_CONFIG_ENV, value) end @@ -79,4 +84,21 @@ def use_omnibus_config_fixture(fixture) stub_omnibus_config_env(omnibus_config_path) end + + def patch_omnibus_config_fixture!(fixture, custom_config) + use_omnibus_config_fixture(fixture) + + config = Psych.safe_load_file(omnibus_config_path, symbolize_names: true) + config.merge!(custom_config) + + serialize_to_yaml(config, omnibus_config_path) + end + + def serialize_to_yaml(content, filename) + content.deep_stringify_keys! + + File.open(filename, File::RDWR) do |file| + file.write(content.to_yaml) + end + end end diff --git a/gems/gitlab-backup-cli/spec/gitlab/backup/cli/context/source_context_spec.rb b/gems/gitlab-backup-cli/spec/gitlab/backup/cli/context/source_context_spec.rb index fb8460912b2e..373274bdc585 100644 --- a/gems/gitlab-backup-cli/spec/gitlab/backup/cli/context/source_context_spec.rb +++ b/gems/gitlab-backup-cli/spec/gitlab/backup/cli/context/source_context_spec.rb @@ -3,278 +3,6 @@ RSpec.describe Gitlab::Backup::Cli::Context::SourceContext do subject(:context) { described_class.new } - let(:fake_gitlab_basepath) { Pathname.new(Dir.mktmpdir('gitlab', temp_path)) } - - before do - allow(context).to receive(:gitlab_basepath).and_return(fake_gitlab_basepath) - FileUtils.mkdir fake_gitlab_basepath.join('config') - end - - after do - fake_gitlab_basepath.rmtree - end - - describe '#gitlab_version' do - it 'returns the GitLab version from the VERSION file' do - version_fixture = fixtures_path.join('VERSION') - FileUtils.copy(version_fixture, fake_gitlab_basepath) - - expect(context.gitlab_version).to eq('17.0.3-ee') - end - end - - describe '#backup_basedir' do - context 'with a relative path configured in gitlab.yml' do - it 'returns a full path based on gitlab basepath' do - use_gitlab_config_fixture('gitlab.yml') - - expect(context.backup_basedir).to eq(fake_gitlab_basepath.join('tmp/tests/backups')) - end - end - - context 'with full path configure in gitlab.yml' do - it 'returns a full path as configured in gitlab.yml' do - use_gitlab_config_fixture('gitlab-relativepaths.yml') - - expect(context.backup_basedir).to eq(Pathname('/tmp/gitlab/full/backups')) - end - end - end - - describe '#ci_builds_path' do - context 'with a missing configuration value' do - it 'returns the default value in full path' do - use_gitlab_config_fixture('gitlab-missingconfigs.yml') - - expect(context.ci_builds_path).to eq(fake_gitlab_basepath.join('builds')) - end - end - - context 'with a relative path configured in gitlab.yml' do - it 'returns a full path based on gitlab basepath' do - use_gitlab_config_fixture('gitlab-relativepaths.yml') - - expect(context.ci_builds_path).to eq(fake_gitlab_basepath.join('tests/builds')) - end - end - - context 'with a full path configured in gitlab.yml' do - it 'returns a full path as configured in gitlab.yml' do - use_gitlab_config_fixture('gitlab.yml') - - expect(context.ci_builds_path).to eq(Pathname('/tmp/gitlab/full/builds')) - end - end - end - - describe '#ci_jobs_artifacts_path' do - context 'with a missing configuration value' do - it 'returns the default value in full path' do - use_gitlab_config_fixture('gitlab-missingconfigs.yml') - - expect(context.ci_job_artifacts_path).to eq(fake_gitlab_basepath.join('test-shared/artifacts')) - end - end - - context 'with a relative path configured in gitlab.yml' do - it 'returns a full path based on gitlab basepath' do - use_gitlab_config_fixture('gitlab-relativepaths.yml') - - expect(context.ci_job_artifacts_path).to eq(fake_gitlab_basepath.join('tmp/tests/artifacts')) - end - end - - context 'with a full path configured in gitlab.yml' do - it 'returns a full path as configured in gitlab.yml' do - use_gitlab_config_fixture('gitlab.yml') - - expect(context.ci_job_artifacts_path).to eq(Pathname('/tmp/gitlab/full/artifacts')) - end - end - end - - describe '#ci_secure_files_path' do - context 'with a missing configuration value' do - it 'returns the default value in full path' do - use_gitlab_config_fixture('gitlab-missingconfigs.yml') - - expect(context.ci_secure_files_path).to eq(fake_gitlab_basepath.join('test-shared/ci_secure_files')) - end - end - - context 'with a relative path configured in gitlab.yml' do - it 'returns a full path based on gitlab basepath' do - use_gitlab_config_fixture('gitlab-relativepaths.yml') - - expect(context.ci_secure_files_path).to eq(fake_gitlab_basepath.join('tmp/tests/ci_secure_files')) - end - end - - context 'with a full path configured in gitlab.yml' do - it 'returns a full path as configured in gitlab.yml' do - use_gitlab_config_fixture('gitlab.yml') - - expect(context.ci_secure_files_path).to eq(Pathname('/tmp/gitlab/full/ci_secure_files')) - end - end - end - - describe '#ci_lfs_path' do - context 'with a missing configuration value' do - it 'returns the default value in full path' do - use_gitlab_config_fixture('gitlab-missingconfigs.yml') - - expect(context.ci_lfs_path).to eq(fake_gitlab_basepath.join('test-shared/lfs-objects')) - end - end - - context 'with a relative path configured in gitlab.yml' do - it 'returns a full path based on gitlab basepath' do - use_gitlab_config_fixture('gitlab-relativepaths.yml') - - expect(context.ci_lfs_path).to eq(fake_gitlab_basepath.join('tmp/tests/lfs-objects')) - end - end - - context 'with a full path configured in gitlab.yml' do - it 'returns a full path as configured in gitlab.yml' do - use_gitlab_config_fixture('gitlab.yml') - - expect(context.ci_lfs_path).to eq(Pathname('/tmp/gitlab/full/lfs-objects')) - end - end - end - - describe '#packages_path' do - context 'with a missing configuration value' do - it 'returns the default value in full path' do - use_gitlab_config_fixture('gitlab-missingconfigs.yml') - - expect(context.packages_path).to eq(fake_gitlab_basepath.join('test-shared/packages')) - end - end - - context 'with a relative path configured in gitlab.yml' do - it 'returns a full path based on gitlab basepath' do - use_gitlab_config_fixture('gitlab-relativepaths.yml') - - expect(context.packages_path).to eq(fake_gitlab_basepath.join('tmp/tests/packages')) - end - end - - context 'with a full path configured in gitlab.yml' do - it 'returns a full path as configured in gitlab.yml' do - use_gitlab_config_fixture('gitlab.yml') - - expect(context.packages_path).to eq(Pathname('/tmp/gitlab/full/packages')) - end - end - end - - describe '#pages_path' do - context 'with a missing configuration value' do - it 'returns the default value in full path' do - use_gitlab_config_fixture('gitlab-missingconfigs.yml') - - expect(context.pages_path).to eq(fake_gitlab_basepath.join('test-shared/pages')) - end - end - - context 'with a relative path configured in gitlab.yml' do - it 'returns a full path based on gitlab basepath' do - use_gitlab_config_fixture('gitlab-relativepaths.yml') - - expect(context.pages_path).to eq(fake_gitlab_basepath.join('tmp/tests/pages')) - end - end - - context 'with a full path configured in gitlab.yml' do - it 'returns a full path as configured in gitlab.yml' do - use_gitlab_config_fixture('gitlab.yml') - - expect(context.pages_path).to eq(Pathname('/tmp/gitlab/full/pages')) - end - end - end - - describe '#registry_path' do - context 'with a missing configuration value' do - it 'returns the default value in full path' do - use_gitlab_config_fixture('gitlab-missingconfigs.yml') - - expect(context.registry_path).to eq(fake_gitlab_basepath.join('test-shared/registry')) - end - end - - context 'with a relative path configured in gitlab.yml' do - it 'returns a full path based on gitlab basepath' do - use_gitlab_config_fixture('gitlab-relativepaths.yml') - - expect(context.registry_path).to eq(fake_gitlab_basepath.join('tmp/tests/registry')) - end - end - - context 'with a full path configured in gitlab.yml' do - it 'returns a full path as configured in gitlab.yml' do - use_gitlab_config_fixture('gitlab.yml') - - expect(context.registry_path).to eq(Pathname('/tmp/gitlab/full/registry')) - end - end - end - - describe '#terraform_state_path' do - context 'with a missing configuration value' do - it 'returns the default value in full path' do - use_gitlab_config_fixture('gitlab-missingconfigs.yml') - - expect(context.terraform_state_path).to eq(fake_gitlab_basepath.join('test-shared/terraform_state')) - end - end - - context 'with a relative path configured in gitlab.yml' do - it 'returns a full path based on gitlab basepath' do - use_gitlab_config_fixture('gitlab-relativepaths.yml') - - expect(context.terraform_state_path).to eq(fake_gitlab_basepath.join('tmp/tests/terraform_state')) - end - end - - context 'with a full path configured in gitlab.yml' do - it 'returns a full path as configured in gitlab.yml' do - use_gitlab_config_fixture('gitlab.yml') - - expect(context.terraform_state_path).to eq(Pathname('/tmp/gitlab/full/terraform_state')) - end - end - end - - describe '#upload_path' do - context 'with a missing configuration value' do - it 'returns the default value in full path' do - use_gitlab_config_fixture('gitlab-missingconfigs.yml') - - expect(context.upload_path).to eq(fake_gitlab_basepath.join('public/uploads')) - end - end - - context 'with a relative path configured in gitlab.yml' do - it 'returns a full path based on gitlab basepath' do - use_gitlab_config_fixture('gitlab-relativepaths.yml') - - expect(context.upload_path).to eq(fake_gitlab_basepath.join('tmp/tests/public/uploads')) - end - end - - context 'with a full path configured in gitlab.yml' do - it 'returns a full path as configured in gitlab.yml' do - use_gitlab_config_fixture('gitlab.yml') - - expect(context.upload_path).to eq(Pathname('/tmp/gitlab/full/public/uploads')) - end - end - end - describe '#env' do it 'returns content from RAILS_ENV when its defined' do stub_const('ENV', { 'RAILS_ENV' => 'railstest', 'RACK_ENV' => 'racktest' }) @@ -295,34 +23,5 @@ end end - describe '#gitlab_shared_path' do - context 'with shared path not configured in gitlab.yml' do - it 'returns the default value' do - use_gitlab_config_fixture('gitlab-empty.yml') - - expect(context.send(:gitlab_shared_path)).to eq(Pathname('shared')) - end - end - - context 'with shared path configured in gitlab.yml' do - it 'returns a relative path' do - use_gitlab_config_fixture('gitlab-relativepaths.yml') - - expect(context.send(:gitlab_shared_path)).to eq(Pathname('shared-tests')) - end - end - - context 'with a full path configured in gitlab.yml' do - it 'returns a full path as configured in gitlab.yml' do - use_gitlab_config_fixture('gitlab.yml') - - expect(context.send(:gitlab_shared_path)).to eq(Pathname('/tmp/gitlab/full/shared')) - end - end - end - - def use_gitlab_config_fixture(fixture) - gitlab_yml_fixture = fixtures_path.join(fixture) - FileUtils.copy(gitlab_yml_fixture, fake_gitlab_basepath.join('config/gitlab.yml')) - end + it_behaves_like 'context exposing all common configuration methods' end diff --git a/gems/gitlab-backup-cli/spec/support/shared_examples/context_shared_examples.rb b/gems/gitlab-backup-cli/spec/support/shared_examples/context_shared_examples.rb new file mode 100644 index 000000000000..1aa7874fbb78 --- /dev/null +++ b/gems/gitlab-backup-cli/spec/support/shared_examples/context_shared_examples.rb @@ -0,0 +1,306 @@ +# frozen_string_literal: true + +RSpec.shared_examples "context exposing all common configuration methods" do + let(:fake_gitlab_basepath) { Pathname.new(Dir.mktmpdir('gitlab', temp_path)) } + + before do + allow(context).to receive(:gitlab_basepath).and_return(fake_gitlab_basepath) + FileUtils.mkdir fake_gitlab_basepath.join('config') + end + + after do + fake_gitlab_basepath.rmtree + end + + describe '#gitlab_version' do + it 'returns the GitLab version from the VERSION file' do + version_fixture = fixtures_path.join('VERSION') + FileUtils.copy(version_fixture, fake_gitlab_basepath) + + expect(context.gitlab_version).to eq('17.0.3-ee') + end + end + + describe '#backup_basedir' do + context 'with a relative path configured in gitlab.yml' do + it 'returns a full path based on gitlab basepath' do + use_gitlab_config_fixture('gitlab.yml') + + expect(context.backup_basedir).to eq(fake_gitlab_basepath.join('tmp/tests/backups')) + end + end + + context 'with full path configure in gitlab.yml' do + it 'returns a full path as configured in gitlab.yml' do + use_gitlab_config_fixture('gitlab-relativepaths.yml') + + expect(context.backup_basedir).to eq(Pathname('/tmp/gitlab/full/backups')) + end + end + end + + describe '#ci_builds_path' do + context 'with a missing configuration value' do + it 'returns the default value in full path' do + use_gitlab_config_fixture('gitlab-missingconfigs.yml') + + expect(context.ci_builds_path).to eq(fake_gitlab_basepath.join('builds')) + end + end + + context 'with a relative path configured in gitlab.yml' do + it 'returns a full path based on gitlab basepath' do + use_gitlab_config_fixture('gitlab-relativepaths.yml') + + expect(context.ci_builds_path).to eq(fake_gitlab_basepath.join('tests/builds')) + end + end + + context 'with a full path configured in gitlab.yml' do + it 'returns a full path as configured in gitlab.yml' do + use_gitlab_config_fixture('gitlab.yml') + + expect(context.ci_builds_path).to eq(Pathname('/tmp/gitlab/full/builds')) + end + end + end + + describe '#ci_jobs_artifacts_path' do + context 'with a missing configuration value' do + it 'returns the default value in full path' do + use_gitlab_config_fixture('gitlab-missingconfigs.yml') + + expect(context.ci_job_artifacts_path).to eq(fake_gitlab_basepath.join('test-shared/artifacts')) + end + end + + context 'with a relative path configured in gitlab.yml' do + it 'returns a full path based on gitlab basepath' do + use_gitlab_config_fixture('gitlab-relativepaths.yml') + + expect(context.ci_job_artifacts_path).to eq(fake_gitlab_basepath.join('tmp/tests/artifacts')) + end + end + + context 'with a full path configured in gitlab.yml' do + it 'returns a full path as configured in gitlab.yml' do + use_gitlab_config_fixture('gitlab.yml') + + expect(context.ci_job_artifacts_path).to eq(Pathname('/tmp/gitlab/full/artifacts')) + end + end + end + + describe '#ci_secure_files_path' do + context 'with a missing configuration value' do + it 'returns the default value in full path' do + use_gitlab_config_fixture('gitlab-missingconfigs.yml') + + expect(context.ci_secure_files_path).to eq(fake_gitlab_basepath.join('test-shared/ci_secure_files')) + end + end + + context 'with a relative path configured in gitlab.yml' do + it 'returns a full path based on gitlab basepath' do + use_gitlab_config_fixture('gitlab-relativepaths.yml') + + expect(context.ci_secure_files_path).to eq(fake_gitlab_basepath.join('tmp/tests/ci_secure_files')) + end + end + + context 'with a full path configured in gitlab.yml' do + it 'returns a full path as configured in gitlab.yml' do + use_gitlab_config_fixture('gitlab.yml') + + expect(context.ci_secure_files_path).to eq(Pathname('/tmp/gitlab/full/ci_secure_files')) + end + end + end + + describe '#ci_lfs_path' do + context 'with a missing configuration value' do + it 'returns the default value in full path' do + use_gitlab_config_fixture('gitlab-missingconfigs.yml') + + expect(context.ci_lfs_path).to eq(fake_gitlab_basepath.join('test-shared/lfs-objects')) + end + end + + context 'with a relative path configured in gitlab.yml' do + it 'returns a full path based on gitlab basepath' do + use_gitlab_config_fixture('gitlab-relativepaths.yml') + + expect(context.ci_lfs_path).to eq(fake_gitlab_basepath.join('tmp/tests/lfs-objects')) + end + end + + context 'with a full path configured in gitlab.yml' do + it 'returns a full path as configured in gitlab.yml' do + use_gitlab_config_fixture('gitlab.yml') + + expect(context.ci_lfs_path).to eq(Pathname('/tmp/gitlab/full/lfs-objects')) + end + end + end + + describe '#packages_path' do + context 'with a missing configuration value' do + it 'returns the default value in full path' do + use_gitlab_config_fixture('gitlab-missingconfigs.yml') + + expect(context.packages_path).to eq(fake_gitlab_basepath.join('test-shared/packages')) + end + end + + context 'with a relative path configured in gitlab.yml' do + it 'returns a full path based on gitlab basepath' do + use_gitlab_config_fixture('gitlab-relativepaths.yml') + + expect(context.packages_path).to eq(fake_gitlab_basepath.join('tmp/tests/packages')) + end + end + + context 'with a full path configured in gitlab.yml' do + it 'returns a full path as configured in gitlab.yml' do + use_gitlab_config_fixture('gitlab.yml') + + expect(context.packages_path).to eq(Pathname('/tmp/gitlab/full/packages')) + end + end + end + + describe '#pages_path' do + context 'with a missing configuration value' do + it 'returns the default value in full path' do + use_gitlab_config_fixture('gitlab-missingconfigs.yml') + + expect(context.pages_path).to eq(fake_gitlab_basepath.join('test-shared/pages')) + end + end + + context 'with a relative path configured in gitlab.yml' do + it 'returns a full path based on gitlab basepath' do + use_gitlab_config_fixture('gitlab-relativepaths.yml') + + expect(context.pages_path).to eq(fake_gitlab_basepath.join('tmp/tests/pages')) + end + end + + context 'with a full path configured in gitlab.yml' do + it 'returns a full path as configured in gitlab.yml' do + use_gitlab_config_fixture('gitlab.yml') + + expect(context.pages_path).to eq(Pathname('/tmp/gitlab/full/pages')) + end + end + end + + describe '#registry_path' do + context 'with a missing configuration value' do + it 'returns the default value in full path' do + use_gitlab_config_fixture('gitlab-missingconfigs.yml') + + expect(context.registry_path).to eq(fake_gitlab_basepath.join('test-shared/registry')) + end + end + + context 'with a relative path configured in gitlab.yml' do + it 'returns a full path based on gitlab basepath' do + use_gitlab_config_fixture('gitlab-relativepaths.yml') + + expect(context.registry_path).to eq(fake_gitlab_basepath.join('tmp/tests/registry')) + end + end + + context 'with a full path configured in gitlab.yml' do + it 'returns a full path as configured in gitlab.yml' do + use_gitlab_config_fixture('gitlab.yml') + + expect(context.registry_path).to eq(Pathname('/tmp/gitlab/full/registry')) + end + end + end + + describe '#terraform_state_path' do + context 'with a missing configuration value' do + it 'returns the default value in full path' do + use_gitlab_config_fixture('gitlab-missingconfigs.yml') + + expect(context.terraform_state_path).to eq(fake_gitlab_basepath.join('test-shared/terraform_state')) + end + end + + context 'with a relative path configured in gitlab.yml' do + it 'returns a full path based on gitlab basepath' do + use_gitlab_config_fixture('gitlab-relativepaths.yml') + + expect(context.terraform_state_path).to eq(fake_gitlab_basepath.join('tmp/tests/terraform_state')) + end + end + + context 'with a full path configured in gitlab.yml' do + it 'returns a full path as configured in gitlab.yml' do + use_gitlab_config_fixture('gitlab.yml') + + expect(context.terraform_state_path).to eq(Pathname('/tmp/gitlab/full/terraform_state')) + end + end + end + + describe '#upload_path' do + context 'with a missing configuration value' do + it 'returns the default value in full path' do + use_gitlab_config_fixture('gitlab-missingconfigs.yml') + + expect(context.upload_path).to eq(fake_gitlab_basepath.join('public/uploads')) + end + end + + context 'with a relative path configured in gitlab.yml' do + it 'returns a full path based on gitlab basepath' do + use_gitlab_config_fixture('gitlab-relativepaths.yml') + + expect(context.upload_path).to eq(fake_gitlab_basepath.join('tmp/tests/public/uploads')) + end + end + + context 'with a full path configured in gitlab.yml' do + it 'returns a full path as configured in gitlab.yml' do + use_gitlab_config_fixture('gitlab.yml') + + expect(context.upload_path).to eq(Pathname('/tmp/gitlab/full/public/uploads')) + end + end + end + + describe '#gitlab_shared_path' do + context 'with shared path not configured in gitlab.yml' do + it 'returns the default value' do + use_gitlab_config_fixture('gitlab-empty.yml') + + expect(context.send(:gitlab_shared_path)).to eq(Pathname('shared')) + end + end + + context 'with shared path configured in gitlab.yml' do + it 'returns a relative path' do + use_gitlab_config_fixture('gitlab-relativepaths.yml') + + expect(context.send(:gitlab_shared_path)).to eq(Pathname('shared-tests')) + end + end + + context 'with a full path configured in gitlab.yml' do + it 'returns a full path as configured in gitlab.yml' do + use_gitlab_config_fixture('gitlab.yml') + + expect(context.send(:gitlab_shared_path)).to eq(Pathname('/tmp/gitlab/full/shared')) + end + end + end + + def use_gitlab_config_fixture(fixture) + gitlab_yml_fixture = fixtures_path.join(fixture) + FileUtils.copy(gitlab_yml_fixture, fake_gitlab_basepath.join('config/gitlab.yml')) + end +end -- GitLab