From b76ae638462ab0f673e5915986070518dd3f9ad3 Mon Sep 17 00:00:00 2001 From: GitLab Bot Date: Thu, 19 Aug 2021 09:08:42 +0000 Subject: Add latest changes from gitlab-org/gitlab@14-2-stable-ee --- spec/tasks/gitlab/backup_rake_spec.rb | 13 ++- spec/tasks/gitlab/db_rake_spec.rb | 37 ++++++- spec/tasks/gitlab/ldap_rake_spec.rb | 16 +-- .../tasks/gitlab/product_intelligence_rake_spec.rb | 80 +++++++++++++++ spec/tasks/gitlab/smtp_rake_spec.rb | 112 +++++++++++++++++++++ spec/tasks/gitlab/storage_rake_spec.rb | 4 +- 6 files changed, 245 insertions(+), 17 deletions(-) create mode 100644 spec/tasks/gitlab/product_intelligence_rake_spec.rb create mode 100644 spec/tasks/gitlab/smtp_rake_spec.rb (limited to 'spec/tasks') diff --git a/spec/tasks/gitlab/backup_rake_spec.rb b/spec/tasks/gitlab/backup_rake_spec.rb index ebaaf179546..99deaa8d154 100644 --- a/spec/tasks/gitlab/backup_rake_spec.rb +++ b/spec/tasks/gitlab/backup_rake_spec.rb @@ -412,6 +412,16 @@ RSpec.describe 'gitlab:app namespace rake task', :delete do expect { run_rake_task('gitlab:backup:create') }.to output.to_stdout_from_any_process end end + + context 'CRON env is set' do + before do + stub_env('CRON', '1') + end + + it 'does not output to stdout' do + expect { run_rake_task('gitlab:backup:create') }.not_to output.to_stdout_from_any_process + end + end end # backup_create task @@ -480,8 +490,7 @@ RSpec.describe 'gitlab:app namespace rake task', :delete do 'lfs.tar.gz', 'pages.tar.gz', 'registry.tar.gz', - 'repositories', - 'tmp' + 'repositories' ) end diff --git a/spec/tasks/gitlab/db_rake_spec.rb b/spec/tasks/gitlab/db_rake_spec.rb index 03fbd238ee9..8e98a42510e 100644 --- a/spec/tasks/gitlab/db_rake_spec.rb +++ b/spec/tasks/gitlab/db_rake_spec.rb @@ -201,9 +201,36 @@ RSpec.describe 'gitlab:db namespace rake task', :silence_stdout do let(:reindex) { double('reindex') } let(:indexes) { double('indexes') } + it 'cleans up any leftover indexes' do + expect(Gitlab::Database::Reindexing).to receive(:cleanup_leftovers!) + + run_rake_task('gitlab:db:reindex') + end + + context 'when async index creation is enabled' do + it 'executes async index creation prior to any reindexing actions' do + stub_feature_flags(database_async_index_creation: true) + + expect(Gitlab::Database::AsyncIndexes).to receive(:create_pending_indexes!).ordered + expect(Gitlab::Database::Reindexing).to receive(:perform).ordered + + run_rake_task('gitlab:db:reindex') + end + end + + context 'when async index creation is disabled' do + it 'does not execute async index creation' do + stub_feature_flags(database_async_index_creation: false) + + expect(Gitlab::Database::AsyncIndexes).not_to receive(:create_pending_indexes!) + + run_rake_task('gitlab:db:reindex') + end + end + context 'when no index_name is given' do it 'uses all candidate indexes' do - expect(Gitlab::Database::Reindexing).to receive(:candidate_indexes).and_return(indexes) + expect(Gitlab::Database::PostgresIndex).to receive(:reindexing_support).and_return(indexes) expect(Gitlab::Database::Reindexing).to receive(:perform).with(indexes) run_rake_task('gitlab:db:reindex') @@ -214,7 +241,7 @@ RSpec.describe 'gitlab:db namespace rake task', :silence_stdout do let(:index) { double('index') } before do - allow(Gitlab::Database::Reindexing).to receive(:candidate_indexes).and_return(indexes) + allow(Gitlab::Database::PostgresIndex).to receive(:reindexing_support).and_return(indexes) end it 'calls the index rebuilder with the proper arguments' do @@ -270,8 +297,8 @@ RSpec.describe 'gitlab:db namespace rake task', :silence_stdout do let(:ctx) { double('ctx', migrations: all_migrations, schema_migration: double, get_all_versions: existing_versions) } let(:instrumentation) { instance_double(Gitlab::Database::Migrations::Instrumentation, observations: observations) } let(:existing_versions) { [1] } - let(:all_migrations) { [double('migration1', version: 1), pending_migration] } - let(:pending_migration) { double('migration2', version: 2) } + let(:all_migrations) { [double('migration1', version: 1, name: 'test'), pending_migration] } + let(:pending_migration) { double('migration2', version: 2, name: 'test') } let(:filename) { Gitlab::Database::Migrations::Instrumentation::STATS_FILENAME } let(:result_dir) { Dir.mktmpdir } let(:observations) { %w[some data] } @@ -297,7 +324,7 @@ RSpec.describe 'gitlab:db namespace rake task', :silence_stdout do end it 'instruments the pending migration' do - expect(instrumentation).to receive(:observe).with(2).and_yield + expect(instrumentation).to receive(:observe).with(version: 2, name: 'test').and_yield subject end diff --git a/spec/tasks/gitlab/ldap_rake_spec.rb b/spec/tasks/gitlab/ldap_rake_spec.rb index 693bb7826a3..b18c2c88a46 100644 --- a/spec/tasks/gitlab/ldap_rake_spec.rb +++ b/spec/tasks/gitlab/ldap_rake_spec.rb @@ -38,13 +38,13 @@ RSpec.describe 'gitlab:ldap:secret rake tasks' do it 'displays error when key does not exist' do Settings.encrypted(ldap_secret_file).write('somevalue') allow(Gitlab::Application.secrets).to receive(:encrypted_settings_key_base).and_return(nil) - expect { run_rake_task('gitlab:ldap:secret:show') }.to output(/Missing encryption key encrypted_settings_key_base./).to_stdout + expect { run_rake_task('gitlab:ldap:secret:show') }.to output(/Missing encryption key encrypted_settings_key_base./).to_stderr end it 'displays error when key is changed' do Settings.encrypted(ldap_secret_file).write('somevalue') allow(Gitlab::Application.secrets).to receive(:encrypted_settings_key_base).and_return(SecureRandom.hex(64)) - expect { run_rake_task('gitlab:ldap:secret:show') }.to output(/Couldn't decrypt .* Perhaps you passed the wrong key?/).to_stdout + expect { run_rake_task('gitlab:ldap:secret:show') }.to output(/Couldn't decrypt .* Perhaps you passed the wrong key?/).to_stderr end it 'outputs the unencrypted content when present' do @@ -64,18 +64,18 @@ RSpec.describe 'gitlab:ldap:secret rake tasks' do it 'displays error when key does not exist' do allow(Gitlab::Application.secrets).to receive(:encrypted_settings_key_base).and_return(nil) - expect { run_rake_task('gitlab:ldap:secret:edit') }.to output(/Missing encryption key encrypted_settings_key_base./).to_stdout + expect { run_rake_task('gitlab:ldap:secret:edit') }.to output(/Missing encryption key encrypted_settings_key_base./).to_stderr end it 'displays error when key is changed' do Settings.encrypted(ldap_secret_file).write('somevalue') allow(Gitlab::Application.secrets).to receive(:encrypted_settings_key_base).and_return(SecureRandom.hex(64)) - expect { run_rake_task('gitlab:ldap:secret:edit') }.to output(/Couldn't decrypt .* Perhaps you passed the wrong key?/).to_stdout + expect { run_rake_task('gitlab:ldap:secret:edit') }.to output(/Couldn't decrypt .* Perhaps you passed the wrong key?/).to_stderr end it 'displays error when write directory does not exist' do FileUtils.rm_rf(Rails.root.join('tmp/tests/ldapenc')) - expect { run_rake_task('gitlab:ldap:secret:edit') }.to output(/Directory .* does not exist./).to_stdout + expect { run_rake_task('gitlab:ldap:secret:edit') }.to output(/Directory .* does not exist./).to_stderr end it 'shows a warning when content is invalid' do @@ -87,7 +87,7 @@ RSpec.describe 'gitlab:ldap:secret rake tasks' do it 'displays error when $EDITOR is not set' do stub_env('EDITOR', nil) - expect { run_rake_task('gitlab:ldap:secret:edit') }.to output(/No \$EDITOR specified to open file. Please provide one when running the command/).to_stdout + expect { run_rake_task('gitlab:ldap:secret:edit') }.to output(/No \$EDITOR specified to open file. Please provide one when running the command/).to_stderr end end @@ -106,12 +106,12 @@ RSpec.describe 'gitlab:ldap:secret rake tasks' do it 'displays error when key does not exist' do allow(Gitlab::Application.secrets).to receive(:encrypted_settings_key_base).and_return(nil) - expect { run_rake_task('gitlab:ldap:secret:write') }.to output(/Missing encryption key encrypted_settings_key_base./).to_stdout + expect { run_rake_task('gitlab:ldap:secret:write') }.to output(/Missing encryption key encrypted_settings_key_base./).to_stderr end it 'displays error when write directory does not exist' do FileUtils.rm_rf('tmp/tests/ldapenc/') - expect { run_rake_task('gitlab:ldap:secret:write') }.to output(/Directory .* does not exist./).to_stdout + expect { run_rake_task('gitlab:ldap:secret:write') }.to output(/Directory .* does not exist./).to_stderr end it 'shows a warning when content is invalid' do diff --git a/spec/tasks/gitlab/product_intelligence_rake_spec.rb b/spec/tasks/gitlab/product_intelligence_rake_spec.rb new file mode 100644 index 00000000000..029e181ad06 --- /dev/null +++ b/spec/tasks/gitlab/product_intelligence_rake_spec.rb @@ -0,0 +1,80 @@ +# frozen_string_literal: true + +require 'rake_helper' + +RSpec.describe 'gitlab:product_intelligence:activate_metrics', :silence_stdout do + def fake_metric(key_path, milestone: 'test_milestone', status: 'implemented') + Gitlab::Usage::MetricDefinition.new(key_path, { key_path: key_path, milestone: milestone, status: status }) + end + + before do + Rake.application.rake_require 'tasks/gitlab/product_intelligence' + stub_warn_user_is_not_gitlab + end + + describe 'activate_metrics' do + it 'fails if the MILESTONE env var is not set' do + stub_env('MILESTONE' => nil) + + expect { run_rake_task('gitlab:product_intelligence:activate_metrics') }.to raise_error(RuntimeError, 'Please supply the MILESTONE env var') + end + + context 'with MILESTONE env var' do + subject do + updated_metrics = [] + + file = double('file') + allow(file).to receive(:<<) { |contents| updated_metrics << YAML.safe_load(contents) } + allow(File).to receive(:open).and_yield(file) + + stub_env('MILESTONE' => 'test_milestone') + run_rake_task('gitlab:product_intelligence:activate_metrics') + + updated_metrics + end + + let(:metric_definitions) do + { + matching_metric: fake_metric('matching_metric'), + matching_metric2: fake_metric('matching_metric2'), + other_status_metric: fake_metric('other_status_metric', status: 'deprecated'), + other_milestone_metric: fake_metric('other_milestone_metric', milestone: 'other_milestone') + } + end + + before do + allow(Gitlab::Usage::MetricDefinition).to receive(:definitions).and_return(metric_definitions) + end + + context 'with metric matching status and milestone' do + it 'updates matching_metric yaml file' do + expect(subject).to eq([ + { 'key_path' => 'matching_metric', 'milestone' => 'test_milestone', 'status' => 'data_available' }, + { 'key_path' => 'matching_metric2', 'milestone' => 'test_milestone', 'status' => 'data_available' } + ]) + end + end + + context 'without metrics definitions' do + let(:metric_definitions) { {} } + + it 'runs successfully with no updates' do + expect(subject).to eq([]) + end + end + + context 'without matching metrics' do + let(:metric_definitions) do + { + other_status_metric: fake_metric('other_status_metric', status: 'deprecated'), + other_milestone_metric: fake_metric('other_milestone_metric', milestone: 'other_milestone') + } + end + + it 'runs successfully with no updates' do + expect(subject).to eq([]) + end + end + end + end +end diff --git a/spec/tasks/gitlab/smtp_rake_spec.rb b/spec/tasks/gitlab/smtp_rake_spec.rb new file mode 100644 index 00000000000..572df8421d5 --- /dev/null +++ b/spec/tasks/gitlab/smtp_rake_spec.rb @@ -0,0 +1,112 @@ +# frozen_string_literal: true + +require 'rake_helper' + +RSpec.describe 'gitlab:smtp:secret rake tasks' do + let(:smtp_secret_file) { 'tmp/tests/smtpenc/smtp_secret.yaml.enc' } + + before do + Rake.application.rake_require 'tasks/gitlab/smtp' + stub_env('EDITOR', 'cat') + stub_warn_user_is_not_gitlab + FileUtils.mkdir_p('tmp/tests/smtpenc/') + allow(Gitlab.config.gitlab).to receive(:email_smtp_secret_file).and_return(smtp_secret_file) + allow(Gitlab::Application.secrets).to receive(:encrypted_settings_key_base).and_return(SecureRandom.hex(64)) + end + + after do + FileUtils.rm_rf(Rails.root.join('tmp/tests/smtpenc')) + end + + describe ':show' do + it 'displays error when file does not exist' do + expect { run_rake_task('gitlab:smtp:secret:show') }.to output(/File .* does not exist. Use `gitlab-rake gitlab:smtp:secret:edit` to change that./).to_stdout + end + + it 'displays error when key does not exist' do + Settings.encrypted(smtp_secret_file).write('somevalue') + allow(Gitlab::Application.secrets).to receive(:encrypted_settings_key_base).and_return(nil) + expect { run_rake_task('gitlab:smtp:secret:show') }.to output(/Missing encryption key encrypted_settings_key_base./).to_stderr + end + + it 'displays error when key is changed' do + Settings.encrypted(smtp_secret_file).write('somevalue') + allow(Gitlab::Application.secrets).to receive(:encrypted_settings_key_base).and_return(SecureRandom.hex(64)) + expect { run_rake_task('gitlab:smtp:secret:show') }.to output(/Couldn't decrypt .* Perhaps you passed the wrong key?/).to_stderr + end + + it 'outputs the unencrypted content when present' do + encrypted = Settings.encrypted(smtp_secret_file) + encrypted.write('somevalue') + expect { run_rake_task('gitlab:smtp:secret:show') }.to output(/somevalue/).to_stdout + end + end + + describe 'edit' do + it 'creates encrypted file' do + expect { run_rake_task('gitlab:smtp:secret:edit') }.to output(/File encrypted and saved./).to_stdout + expect(File.exist?(smtp_secret_file)).to be true + value = Settings.encrypted(smtp_secret_file) + expect(value.read).to match(/password: '123'/) + end + + it 'displays error when key does not exist' do + allow(Gitlab::Application.secrets).to receive(:encrypted_settings_key_base).and_return(nil) + expect { run_rake_task('gitlab:smtp:secret:edit') }.to output(/Missing encryption key encrypted_settings_key_base./).to_stderr + end + + it 'displays error when key is changed' do + Settings.encrypted(smtp_secret_file).write('somevalue') + allow(Gitlab::Application.secrets).to receive(:encrypted_settings_key_base).and_return(SecureRandom.hex(64)) + expect { run_rake_task('gitlab:smtp:secret:edit') }.to output(/Couldn't decrypt .* Perhaps you passed the wrong key?/).to_stderr + end + + it 'displays error when write directory does not exist' do + FileUtils.rm_rf(Rails.root.join('tmp/tests/smtpenc')) + expect { run_rake_task('gitlab:smtp:secret:edit') }.to output(/Directory .* does not exist./).to_stderr + end + + it 'shows a warning when content is invalid' do + Settings.encrypted(smtp_secret_file).write('somevalue') + expect { run_rake_task('gitlab:smtp:secret:edit') }.to output(/WARNING: Content was not a valid SMTP secret yml file/).to_stdout + value = Settings.encrypted(smtp_secret_file) + expect(value.read).to match(/somevalue/) + end + + it 'displays error when $EDITOR is not set' do + stub_env('EDITOR', nil) + expect { run_rake_task('gitlab:smtp:secret:edit') }.to output(/No \$EDITOR specified to open file. Please provide one when running the command/).to_stderr + end + end + + describe 'write' do + before do + allow($stdin).to receive(:tty?).and_return(false) + allow($stdin).to receive(:read).and_return('username: foo') + end + + it 'creates encrypted file from stdin' do + expect { run_rake_task('gitlab:smtp:secret:write') }.to output(/File encrypted and saved./).to_stdout + expect(File.exist?(smtp_secret_file)).to be true + value = Settings.encrypted(smtp_secret_file) + expect(value.read).to match(/username: foo/) + end + + it 'displays error when key does not exist' do + allow(Gitlab::Application.secrets).to receive(:encrypted_settings_key_base).and_return(nil) + expect { run_rake_task('gitlab:smtp:secret:write') }.to output(/Missing encryption key encrypted_settings_key_base./).to_stderr + end + + it 'displays error when write directory does not exist' do + FileUtils.rm_rf('tmp/tests/smtpenc/') + expect { run_rake_task('gitlab:smtp:secret:write') }.to output(/Directory .* does not exist./).to_stderr + end + + it 'shows a warning when content is invalid' do + Settings.encrypted(smtp_secret_file).write('somevalue') + expect { run_rake_task('gitlab:smtp:secret:edit') }.to output(/WARNING: Content was not a valid SMTP secret yml file/).to_stdout + value = Settings.encrypted(smtp_secret_file) + expect(value.read).to match(/somevalue/) + end + end +end diff --git a/spec/tasks/gitlab/storage_rake_spec.rb b/spec/tasks/gitlab/storage_rake_spec.rb index fefcd05af3b..570f67c8bb7 100644 --- a/spec/tasks/gitlab/storage_rake_spec.rb +++ b/spec/tasks/gitlab/storage_rake_spec.rb @@ -90,7 +90,7 @@ RSpec.describe 'rake gitlab:storage:*', :silence_stdout do shared_examples 'wait until database is ready' do it 'checks if the database is ready once' do - expect(Gitlab::Database).to receive(:exists?).once + expect(Gitlab::Database.main).to receive(:exists?).once run_rake_task(task) end @@ -102,7 +102,7 @@ RSpec.describe 'rake gitlab:storage:*', :silence_stdout do end it 'tries for 3 times, polling every 0.1 seconds' do - expect(Gitlab::Database).to receive(:exists?).exactly(3).times.and_return(false) + expect(Gitlab::Database.main).to receive(:exists?).exactly(3).times.and_return(false) run_rake_task(task) end -- cgit v1.2.3