diff options
Diffstat (limited to 'spec/tasks/gitlab')
-rw-r--r-- | spec/tasks/gitlab/container_registry_rake_spec.rb | 123 | ||||
-rw-r--r-- | spec/tasks/gitlab/db_rake_spec.rb | 59 |
2 files changed, 175 insertions, 7 deletions
diff --git a/spec/tasks/gitlab/container_registry_rake_spec.rb b/spec/tasks/gitlab/container_registry_rake_spec.rb new file mode 100644 index 00000000000..181d5c8b7c8 --- /dev/null +++ b/spec/tasks/gitlab/container_registry_rake_spec.rb @@ -0,0 +1,123 @@ +# frozen_string_literal: true + +require 'rake_helper' + +describe 'gitlab:container_registry namespace rake tasks' do + let_it_be(:application_settings) { Gitlab::CurrentSettings } + let_it_be(:api_url) { 'http://registry.gitlab' } + + before :all do + Rake.application.rake_require 'tasks/gitlab/container_registry' + end + + describe 'configure' do + before do + stub_access_token + stub_container_registry_config(enabled: true, api_url: api_url) + end + + subject { run_rake_task('gitlab:container_registry:configure') } + + shared_examples 'invalid config' do + it 'does not update the application settings' do + expect(application_settings).not_to receive(:update!) + + subject + end + + it 'does not raise an error' do + expect { subject }.not_to raise_error + end + + it 'prints a warning message' do + expect { subject }.to output(/Registry is not enabled or registry api url is not present./).to_stdout + end + end + + context 'when container registry is disabled' do + before do + stub_container_registry_config(enabled: false) + end + + it_behaves_like 'invalid config' + end + + context 'when container registry api_url is blank' do + before do + stub_container_registry_config(api_url: '') + end + + it_behaves_like 'invalid config' + end + + context 'when creating a registry client instance' do + let(:token) { 'foo' } + let(:client) { ContainerRegistry::Client.new(api_url, token: token) } + + before do + stub_registry_info({}) + end + + it 'uses a token with no access permissions' do + expect(Auth::ContainerRegistryAuthenticationService) + .to receive(:access_token).with([], []).and_return(token) + expect(ContainerRegistry::Client) + .to receive(:new).with(api_url, token: token).and_return(client) + + run_rake_task('gitlab:container_registry:configure') + end + end + + context 'when unabled to detect the container registry type' do + it 'fails and raises an error message' do + stub_registry_info({}) + + run_rake_task('gitlab:container_registry:configure') + + application_settings.reload + expect(application_settings.container_registry_vendor).to be_blank + expect(application_settings.container_registry_version).to be_blank + expect(application_settings.container_registry_features).to eq([]) + end + end + + context 'when able to detect the container registry type' do + context 'when using the GitLab container registry' do + it 'updates application settings accordingly' do + stub_registry_info(vendor: 'gitlab', version: '2.9.1-gitlab', features: %w[a,b,c]) + + run_rake_task('gitlab:container_registry:configure') + + application_settings.reload + expect(application_settings.container_registry_vendor).to eq('gitlab') + expect(application_settings.container_registry_version).to eq('2.9.1-gitlab') + expect(application_settings.container_registry_features).to eq(%w[a,b,c]) + end + end + + context 'when using a third-party container registry' do + it 'updates application settings accordingly' do + stub_registry_info(vendor: 'other', version: nil, features: nil) + + run_rake_task('gitlab:container_registry:configure') + + application_settings.reload + expect(application_settings.container_registry_vendor).to eq('other') + expect(application_settings.container_registry_version).to be_blank + expect(application_settings.container_registry_features).to eq([]) + end + end + end + end + + def stub_access_token + allow(Auth::ContainerRegistryAuthenticationService) + .to receive(:access_token).with([], []).and_return('foo') + end + + def stub_registry_info(output) + allow_next_instance_of(ContainerRegistry::Client) do |client| + allow(client).to receive(:registry_info).and_return(output) + end + end +end diff --git a/spec/tasks/gitlab/db_rake_spec.rb b/spec/tasks/gitlab/db_rake_spec.rb index 1d2341bb46f..c3da5af5439 100644 --- a/spec/tasks/gitlab/db_rake_spec.rb +++ b/spec/tasks/gitlab/db_rake_spec.rb @@ -115,19 +115,52 @@ describe 'gitlab:db namespace rake task' do end it 'can be executed multiple times within another rake task' do - Rake::Task.define_task(test_task_name => :environment) do + expect_multiple_executions_of_task(test_task_name, clean_rake_task) do expect_next_instance_of(Gitlab::Database::SchemaCleaner) do |cleaner| expect(cleaner).to receive(:clean).with(output) end - Rake::Task[clean_rake_task].invoke + end + end + end - expect_next_instance_of(Gitlab::Database::SchemaCleaner) do |cleaner| - expect(cleaner).to receive(:clean).with(output) + describe 'load_custom_structure' do + let_it_be(:db_config) { Rails.application.config_for(:database) } + let_it_be(:custom_load_task) { 'gitlab:db:load_custom_structure' } + let_it_be(:custom_filepath) { Pathname.new('db/directory') } + + it 'uses the psql command to load the custom structure file' do + expect(Gitlab::Database::CustomStructure).to receive(:custom_dump_filepath).and_return(custom_filepath) + + expect(Kernel).to receive(:system) + .with('psql', any_args, custom_filepath.to_path, db_config['database']).and_return(true) + + run_rake_task(custom_load_task) + end + + it 'raises an error when the call to the psql command fails' do + expect(Gitlab::Database::CustomStructure).to receive(:custom_dump_filepath).and_return(custom_filepath) + + expect(Kernel).to receive(:system) + .with('psql', any_args, custom_filepath.to_path, db_config['database']).and_return(nil) + + expect { run_rake_task(custom_load_task) }.to raise_error(/failed to execute:\s*psql/) + end + end + + describe 'dump_custom_structure' do + let_it_be(:test_task_name) { 'gitlab:db:_test_multiple_task_executions' } + let_it_be(:custom_dump_task) { 'gitlab:db:dump_custom_structure' } + + after do + Rake::Task[test_task_name].clear if Rake::Task.task_defined?(test_task_name) + end + + it 'can be executed multiple times within another rake task' do + expect_multiple_executions_of_task(test_task_name, custom_dump_task) do + expect_next_instance_of(Gitlab::Database::CustomStructure) do |custom_structure| + expect(custom_structure).to receive(:dump) end - Rake::Task[clean_rake_task].invoke end - - run_rake_task(test_task_name) end end @@ -135,4 +168,16 @@ describe 'gitlab:db namespace rake task' do Rake::Task[task_name].reenable Rake.application.invoke_task task_name end + + def expect_multiple_executions_of_task(test_task_name, task_to_invoke, count: 2) + Rake::Task.define_task(test_task_name => :environment) do + count.times do + yield + + Rake::Task[task_to_invoke].invoke + end + end + + run_rake_task(test_task_name) + end end |