diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-12-17 14:59:07 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-12-17 14:59:07 +0300 |
commit | 8b573c94895dc0ac0e1d9d59cf3e8745e8b539ca (patch) | |
tree | 544930fb309b30317ae9797a9683768705d664c4 /spec/tasks | |
parent | 4b1de649d0168371549608993deac953eb692019 (diff) |
Add latest changes from gitlab-org/gitlab@13-7-stable-eev13.7.0-rc42
Diffstat (limited to 'spec/tasks')
-rw-r--r-- | spec/tasks/gettext_rake_spec.rb | 177 | ||||
-rw-r--r-- | spec/tasks/gitlab/db_rake_spec.rb | 48 | ||||
-rw-r--r-- | spec/tasks/gitlab/ldap_rake_spec.rb | 109 | ||||
-rw-r--r-- | spec/tasks/gitlab/packages/events_rake_spec.rb | 43 | ||||
-rw-r--r-- | spec/tasks/gitlab/user_management_rake_spec.rb | 83 | ||||
-rw-r--r-- | spec/tasks/gitlab/workhorse_rake_spec.rb | 68 |
6 files changed, 464 insertions, 64 deletions
diff --git a/spec/tasks/gettext_rake_spec.rb b/spec/tasks/gettext_rake_spec.rb new file mode 100644 index 00000000000..a535ac92a75 --- /dev/null +++ b/spec/tasks/gettext_rake_spec.rb @@ -0,0 +1,177 @@ +# frozen_string_literal: true + +require "rake_helper" + +RSpec.describe 'gettext' do + let(:locale_path) { Rails.root.join('tmp/gettext_spec') } + let(:pot_file_path) { File.join(locale_path, 'gitlab.pot') } + + before do + Rake.application.rake_require('tasks/gettext') + + FileUtils.rm_r(locale_path) if Dir.exist?(locale_path) + FileUtils.mkdir_p(locale_path) + + allow(Rails.root).to receive(:join).and_call_original + allow(Rails.root).to receive(:join).with('locale').and_return(locale_path) + end + + after do + FileUtils.rm_r(locale_path) if Dir.exist?(locale_path) + end + + describe ':compile' do + before do + allow(Rake::Task).to receive(:[]).and_call_original + end + + it 'creates a pot file and invokes the \'gettext:po_to_json\' task' do + expect(Rake::Task).to receive(:[]).with('gettext:po_to_json').and_return(double(invoke: true)) + + expect { run_rake_task('gettext:compile') } + .to change { File.exist?(pot_file_path) } + .to be_truthy + end + end + + describe ':regenerate' do + before do + # this task takes a *really* long time to complete, so stub it for the spec + allow(Rake::Task['gettext:find']).to receive(:invoke) { invoke_find.call } + end + + context 'when the locale folder is not found' do + let(:invoke_find) { -> { true } } + + before do + FileUtils.rm_r(locale_path) if Dir.exist?(locale_path) + end + + it 'raises an error' do + expect { run_rake_task('gettext:regenerate') } + .to raise_error(/Cannot find '#{locale_path}' folder/) + end + end + + context 'where there are existing /**/gitlab.po files' do + let(:locale_nz_path) { File.join(locale_path, 'en_NZ') } + let(:po_file_path) { File.join(locale_nz_path, 'gitlab.po') } + + let(:invoke_find) { -> { File.write pot_file_path, 'pot file test updates' } } + + before do + FileUtils.mkdir(locale_nz_path) + File.write(po_file_path, fixture_file('valid.po')) + end + + it 'does not remove that locale' do + expect { run_rake_task('gettext:regenerate') } + .not_to change { Dir.exist?(locale_nz_path) } + end + end + + context 'when there are locale folders without a gitlab.po file' do + let(:empty_locale_path) { File.join(locale_path, 'en_NZ') } + + let(:invoke_find) { -> { File.write pot_file_path, 'pot file test updates' } } + + before do + FileUtils.mkdir(empty_locale_path) + end + + it 'removes those folders' do + expect { run_rake_task('gettext:regenerate') } + .to change { Dir.exist?(empty_locale_path) } + .to eq false + end + end + + context 'when the gitlab.pot file cannot be generated' do + let(:invoke_find) { -> { true } } + + it 'prints an error' do + expect { run_rake_task('gettext:regenerate') } + .to raise_error(/gitlab.pot file not generated/) + end + end + + context 'when gettext:find changes the revision dates' do + let(:invoke_find) { -> { File.write pot_file_path, fixture_file('valid.po') } } + + before do + File.write pot_file_path, fixture_file('valid.po') + end + + it 'resets the changes' do + pot_file = File.read(pot_file_path) + expect(pot_file).to include('PO-Revision-Date: 2017-07-13 12:10-0500') + expect(pot_file).to include('PO-Creation-Date: 2016-07-13 12:11-0500') + + run_rake_task('gettext:regenerate') + + pot_file = File.read(pot_file_path) + expect(pot_file).not_to include('PO-Revision-Date: 2017-07-13 12:10-0500') + expect(pot_file).not_to include('PO-Creation-Date: 2016-07-13 12:11-0500') + end + end + end + + describe ':lint' do + before do + # make sure we test on the fixture files, not the actual gitlab repo as + # this takes a long time + allow(Rails.root) + .to receive(:join) + .with('locale/*/gitlab.po') + .and_return(File.join(locale_path, '*/gitlab.po')) + end + + context 'when all PO files are valid' do + before do + nz_locale_path = File.join(locale_path, 'en_NZ') + FileUtils.mkdir(nz_locale_path) + + po_file_path = File.join(nz_locale_path, 'gitlab.po') + File.write(po_file_path, fixture_file('valid.po')) + File.write(pot_file_path, fixture_file('valid.po')) + end + + it 'completes without error' do + expect { run_rake_task('gettext:lint') } + .not_to raise_error + end + end + + context 'when there are invalid PO files' do + before do + nz_locale_path = File.join(locale_path, 'en_NZ') + FileUtils.mkdir(nz_locale_path) + + po_file_path = File.join(nz_locale_path, 'gitlab.po') + File.write(po_file_path, fixture_file('invalid.po')) + File.write(pot_file_path, fixture_file('valid.po')) + end + + it 'raises an error' do + expect { run_rake_task('gettext:lint') } + .to raise_error(/Not all PO-files are valid/) + end + end + + context 'when the .pot file is invalid' do + before do + nz_locale_path = File.join(locale_path, 'en_NZ') + FileUtils.mkdir(nz_locale_path) + + po_file_path = File.join(nz_locale_path, 'gitlab.po') + File.write(po_file_path, fixture_file('valid.po')) + File.write(pot_file_path, fixture_file('invalid.po')) + end + + it 'raises an error' do + expect { run_rake_task('gettext:lint') } + .to raise_error(/Not all PO-files are valid/) + end + end + end +end diff --git a/spec/tasks/gitlab/db_rake_spec.rb b/spec/tasks/gitlab/db_rake_spec.rb index 046e066a45f..edfdb44022b 100644 --- a/spec/tasks/gitlab/db_rake_spec.rb +++ b/spec/tasks/gitlab/db_rake_spec.rb @@ -129,7 +129,7 @@ RSpec.describe 'gitlab:db namespace rake task' do let(:output) { StringIO.new } before do - allow(File).to receive(:read).with(structure_file).and_return(input) + stub_file_read(structure_file, content: input) allow(File).to receive(:open).with(structure_file, any_args).and_yield(output) end @@ -235,8 +235,8 @@ RSpec.describe 'gitlab:db namespace rake task' do let(:indexes) { double('indexes') } context 'when no index_name is given' do - it 'rebuilds a random number of large indexes' do - expect(Gitlab::Database::Reindexing).to receive_message_chain('candidate_indexes.random_few').and_return(indexes) + it 'uses all candidate indexes' do + expect(Gitlab::Database::Reindexing).to receive(:candidate_indexes).and_return(indexes) expect(Gitlab::Database::Reindexing).to receive(:perform).with(indexes) run_rake_task('gitlab:db:reindex') @@ -246,17 +246,53 @@ RSpec.describe 'gitlab:db namespace rake task' do context 'with index name given' do let(:index) { double('index') } + before do + allow(Gitlab::Database::Reindexing).to receive(:candidate_indexes).and_return(indexes) + end + it 'calls the index rebuilder with the proper arguments' do - expect(Gitlab::Database::PostgresIndex).to receive(:by_identifier).with('public.foo_idx').and_return(index) + allow(indexes).to receive(:where).with(identifier: 'public.foo_idx').and_return([index]) expect(Gitlab::Database::Reindexing).to receive(:perform).with([index]) run_rake_task('gitlab:db:reindex', '[public.foo_idx]') end it 'raises an error if the index does not exist' do - expect(Gitlab::Database::PostgresIndex).to receive(:by_identifier).with('public.absent_index').and_raise(ActiveRecord::RecordNotFound) + allow(indexes).to receive(:where).with(identifier: 'public.absent_index').and_return([]) + + expect { run_rake_task('gitlab:db:reindex', '[public.absent_index]') }.to raise_error(/Index not found/) + end + + it 'raises an error if the index is not fully qualified with a schema' do + expect { run_rake_task('gitlab:db:reindex', '[foo_idx]') }.to raise_error(/Index name is not fully qualified/) + end + end + end + + describe 'active' do + using RSpec::Parameterized::TableSyntax + + let(:task) { 'gitlab:db:active' } + let(:self_monitoring) { double('self_monitoring') } - expect { run_rake_task('gitlab:db:reindex', '[public.absent_index]') }.to raise_error(ActiveRecord::RecordNotFound) + where(:needs_migration, :self_monitoring_project, :project_count, :exit_status, :exit_code) do + true | nil | nil | 1 | false + false | :self_monitoring | 1 | 1 | false + false | nil | 0 | 1 | false + false | :self_monitoring | 2 | 0 | true + end + + with_them do + it 'exits 0 or 1 depending on user modifications to the database' do + allow_any_instance_of(ActiveRecord::MigrationContext).to receive(:needs_migration?).and_return(needs_migration) + allow_any_instance_of(ApplicationSetting).to receive(:self_monitoring_project).and_return(self_monitoring_project) + allow(Project).to receive(:count).and_return(project_count) + + expect { run_rake_task(task) }.to raise_error do |error| + expect(error).to be_a(SystemExit) + expect(error.status).to eq(exit_status) + expect(error.success?).to be(exit_code) + end end end end diff --git a/spec/tasks/gitlab/ldap_rake_spec.rb b/spec/tasks/gitlab/ldap_rake_spec.rb index 275fcb98dd0..5286cd98944 100644 --- a/spec/tasks/gitlab/ldap_rake_spec.rb +++ b/spec/tasks/gitlab/ldap_rake_spec.rb @@ -13,3 +13,112 @@ RSpec.describe 'gitlab:ldap:rename_provider rake task' do run_rake_task('gitlab:ldap:rename_provider', 'ldapmain', 'ldapfoo') end end + +RSpec.describe 'gitlab:ldap:secret rake tasks' do + let(:ldap_secret_file) { 'tmp/tests/ldapenc/ldap_secret.yaml.enc' } + + before do + Rake.application.rake_require 'tasks/gitlab/ldap' + stub_env('EDITOR', 'cat') + stub_warn_user_is_not_gitlab + FileUtils.mkdir_p('tmp/tests/ldapenc/') + allow(Gitlab.config.ldap).to receive(:secret_file).and_return(ldap_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/ldapenc')) + end + + describe ':show' do + it 'displays error when file does not exist' do + expect { run_rake_task('gitlab:ldap:secret:show') }.to output(/File .* does not exist. Use `gitlab-rake gitlab:ldap:secret:edit` to change that./).to_stdout + end + + 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 + 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 + end + + it 'outputs the unencrypted content when present' do + encrypted = Settings.encrypted(ldap_secret_file) + encrypted.write('somevalue') + expect { run_rake_task('gitlab:ldap:secret:show') }.to output(/somevalue/).to_stdout + end + end + + describe 'edit' do + it 'creates encrypted file' do + expect { run_rake_task('gitlab:ldap:secret:edit') }.to output(/File encrypted and saved./).to_stdout + expect(File.exist?(ldap_secret_file)).to be true + value = Settings.encrypted(ldap_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:ldap:secret:edit') }.to output(/Missing encryption key encrypted_settings_key_base./).to_stdout + 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 + 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 + end + + it 'shows a warning when content is invalid' do + Settings.encrypted(ldap_secret_file).write('somevalue') + expect { run_rake_task('gitlab:ldap:secret:edit') }.to output(/WARNING: Content was not a valid LDAP secret yml file/).to_stdout + value = Settings.encrypted(ldap_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:ldap:secret:edit') }.to output(/No \$EDITOR specified to open file. Please provide one when running the command/).to_stdout + end + end + + describe 'write' do + before do + allow(STDIN).to receive(:tty?).and_return(false) + allow(STDIN).to receive(:read).and_return('testvalue') + end + + it 'creates encrypted file from stdin' do + expect { run_rake_task('gitlab:ldap:secret:write') }.to output(/File encrypted and saved./).to_stdout + expect(File.exist?(ldap_secret_file)).to be true + value = Settings.encrypted(ldap_secret_file) + expect(value.read).to match(/testvalue/) + 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:ldap:secret:write') }.to output(/Missing encryption key encrypted_settings_key_base./).to_stdout + 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 + end + + it 'shows a warning when content is invalid' do + Settings.encrypted(ldap_secret_file).write('somevalue') + expect { run_rake_task('gitlab:ldap:secret:edit') }.to output(/WARNING: Content was not a valid LDAP secret yml file/).to_stdout + value = Settings.encrypted(ldap_secret_file) + expect(value.read).to match(/somevalue/) + end + end +end diff --git a/spec/tasks/gitlab/packages/events_rake_spec.rb b/spec/tasks/gitlab/packages/events_rake_spec.rb index ac28f9b5fc2..a485dc2ce58 100644 --- a/spec/tasks/gitlab/packages/events_rake_spec.rb +++ b/spec/tasks/gitlab/packages/events_rake_spec.rb @@ -7,32 +7,49 @@ RSpec.describe 'gitlab:packages:events namespace rake task' do Rake.application.rake_require 'tasks/gitlab/packages/events' end - describe 'generate' do - subject do - file = double('file') - yml_file = nil + subject do + file = double('file') + yml_file = nil - allow(file).to receive(:<<) { |contents| yml_file = contents } - allow(File).to receive(:open).and_yield(file) + allow(file).to receive(:<<) { |contents| yml_file = contents } + allow(File).to receive(:open).and_yield(file) - run_rake_task('gitlab:packages:events:generate') + run_rake_task("gitlab:packages:events:#{task}") - YAML.safe_load(yml_file) - end + YAML.safe_load(yml_file) + end + + describe 'generate_unique' do + let(:task) { 'generate_unique' } it 'excludes guest events' do expect(subject.find { |event| event['name'].include?("guest") }).to be_nil end - ::Packages::Event::EVENT_SCOPES.keys.each do |event_scope| - it "includes includes `#{event_scope}` scope" do + Packages::Event::EVENT_SCOPES.keys.each do |event_scope| + it "includes `#{event_scope}` scope" do expect(subject.find { |event| event['name'].include?(event_scope) }).not_to be_nil end end it 'excludes some event types' do - expect(subject.find { |event| event['name'].include?("search_package") }).to be_nil - expect(subject.find { |event| event['name'].include?("list_package") }).to be_nil + expect(subject.grep(/search_package/)).to be_empty + expect(subject.grep(/list_package/)).to be_empty + end + end + + describe 'generate_guest' do + let(:task) { 'generate_guest' } + + Packages::Event::EVENT_SCOPES.keys.each do |event_scope| + it "includes `#{event_scope}` scope" do + expect(subject.find { |event| event.include?(event_scope) }).not_to be_nil + end + end + + it 'excludes some event types' do + expect(subject.find { |event| event.include?("search_package") }).to be_nil + expect(subject.find { |event| event.include?("list_package") }).to be_nil end end end diff --git a/spec/tasks/gitlab/user_management_rake_spec.rb b/spec/tasks/gitlab/user_management_rake_spec.rb new file mode 100644 index 00000000000..958055780d0 --- /dev/null +++ b/spec/tasks/gitlab/user_management_rake_spec.rb @@ -0,0 +1,83 @@ +# frozen_string_literal: true + +require 'rake_helper' + +RSpec.describe 'gitlab:user_management tasks' do + before do + Rake.application.rake_require 'tasks/gitlab/user_management' + end + + describe 'disable_project_and_group_creation' do + let(:group) { create(:group) } + + subject(:run_rake) { run_rake_task('gitlab:user_management:disable_project_and_group_creation', group.id) } + + it 'returns output info' do + expect { run_rake }.to output(/.*Done.*/).to_stdout + end + + context 'with users' do + let(:user_1) { create(:user, projects_limit: 10, can_create_group: true) } + let(:user_2) { create(:user, projects_limit: 10, can_create_group: true) } + let(:user_other) { create(:user, projects_limit: 10, can_create_group: true) } + + shared_examples 'updates proper users' do + it 'updates members' do + run_rake + + expect(user_1.reload.projects_limit).to eq(0) + expect(user_1.can_create_group).to eq(false) + expect(user_2.reload.projects_limit).to eq(0) + expect(user_2.can_create_group).to eq(false) + end + + it 'does not update other users' do + run_rake + + expect(user_other.reload.projects_limit).to eq(10) + expect(user_other.reload.can_create_group).to eq(true) + end + end + + context 'in the group' do + let(:other_group) { create(:group) } + + before do + group.add_developer(user_1) + group.add_developer(user_2) + other_group.add_developer(user_other) + end + + it_behaves_like 'updates proper users' + end + + context 'in the descendant groups' do + let(:subgroup) { create(:group, parent: group) } + let(:sub_subgroup) { create(:group, parent: subgroup) } + let(:other_group) { create(:group) } + + before do + subgroup.add_developer(user_1) + sub_subgroup.add_developer(user_2) + other_group.add_developer(user_other) + end + + it_behaves_like 'updates proper users' + end + + context 'in the children projects' do + let(:project_1) { create(:project, namespace: group) } + let(:project_2) { create(:project, namespace: group) } + let(:other_project) { create(:project) } + + before do + project_1.add_developer(user_1) + project_2.add_developer(user_2) + other_project.add_developer(user_other) + end + + it_behaves_like 'updates proper users' + end + end + end +end diff --git a/spec/tasks/gitlab/workhorse_rake_spec.rb b/spec/tasks/gitlab/workhorse_rake_spec.rb index 2efbf0febac..0757f6ca015 100644 --- a/spec/tasks/gitlab/workhorse_rake_spec.rb +++ b/spec/tasks/gitlab/workhorse_rake_spec.rb @@ -9,8 +9,12 @@ RSpec.describe 'gitlab:workhorse namespace rake task' do describe 'install' do let(:repo) { 'https://gitlab.com/gitlab-org/gitlab-workhorse.git' } - let(:clone_path) { Rails.root.join('tmp/tests/gitlab-workhorse').to_s } - let(:version) { File.read(Rails.root.join(Gitlab::Workhorse::VERSION_FILE)).chomp } + let(:clone_path) { Dir.mktmpdir('gitlab:workhorse:install-rake-test') } + let(:workhorse_source) { Rails.root.join('workhorse').to_s } + + after do + FileUtils.rm_rf(clone_path) + end context 'no dir given' do it 'aborts and display a help message' do @@ -20,7 +24,7 @@ RSpec.describe 'gitlab:workhorse namespace rake task' do end end - context 'when an underlying Git command fail' do + context 'when an underlying Git command fails' do it 'aborts and display a help message' do expect(main_object) .to receive(:checkout_or_clone_version).and_raise 'Git error' @@ -29,52 +33,26 @@ RSpec.describe 'gitlab:workhorse namespace rake task' do end end - describe 'checkout or clone' do - before do - expect(Dir).to receive(:chdir).with(clone_path) - end - - it 'calls checkout_or_clone_version with the right arguments' do - expect(main_object) - .to receive(:checkout_or_clone_version).with(version: version, repo: repo, target_dir: clone_path, clone_opts: %w[--depth 1]) - - run_rake_task('gitlab:workhorse:install', clone_path) - end - end - - describe 'gmake/make' do - before do - FileUtils.mkdir_p(clone_path) - expect(Dir).to receive(:chdir).with(clone_path).and_call_original - end - - context 'gmake is available' do - before do - expect(main_object).to receive(:checkout_or_clone_version) - allow(Object).to receive(:run_command!).with(['gmake']).and_return(true) + it 'clones the origin and creates a gitlab-workhorse binary' do + FileUtils.rm_rf(clone_path) + + Dir.mktmpdir('fake-workhorse-origin') do |workhorse_origin| + [ + %W[git init -q #{workhorse_origin}], + %W[git -C #{workhorse_origin} checkout -q -b workhorse-move-notice], + %W[touch #{workhorse_origin}/proof-that-repo-got-cloned], + %W[git -C #{workhorse_origin} add .], + %W[git -C #{workhorse_origin} commit -q -m init], + %W[git -C #{workhorse_origin} checkout -q -b master] + ].each do |cmd| + raise "#{cmd.join(' ')} failed" unless system(*cmd) end - it 'calls gmake in the gitlab-workhorse directory' do - expect(Gitlab::Popen).to receive(:popen).with(%w[which gmake]).and_return(['/usr/bin/gmake', 0]) - expect(main_object).to receive(:run_command!).with(['gmake']).and_return(true) - - run_rake_task('gitlab:workhorse:install', clone_path) - end + run_rake_task('gitlab:workhorse:install', clone_path, File.join(workhorse_origin, '.git')) end - context 'gmake is not available' do - before do - expect(main_object).to receive(:checkout_or_clone_version) - allow(main_object).to receive(:run_command!).with(['make']).and_return(true) - end - - it 'calls make in the gitlab-workhorse directory' do - expect(Gitlab::Popen).to receive(:popen).with(%w[which gmake]).and_return(['', 42]) - expect(main_object).to receive(:run_command!).with(['make']).and_return(true) - - run_rake_task('gitlab:workhorse:install', clone_path) - end - end + expect(File.exist?(File.join(clone_path, 'proof-that-repo-got-cloned'))).to be true + expect(File.executable?(File.join(clone_path, 'gitlab-workhorse'))).to be true end end end |