summaryrefslogtreecommitdiff
path: root/spec/tasks/gitlab
diff options
context:
space:
mode:
Diffstat (limited to 'spec/tasks/gitlab')
-rw-r--r--spec/tasks/gitlab/backup_rake_spec.rb2
-rw-r--r--spec/tasks/gitlab/db/decomposition/rollback/bump_ci_sequences_rake_spec.rb2
-rw-r--r--spec/tasks/gitlab/db/lock_writes_rake_spec.rb39
-rw-r--r--spec/tasks/gitlab/db/truncate_legacy_tables_rake_spec.rb8
-rw-r--r--spec/tasks/gitlab/db_rake_spec.rb2
-rw-r--r--spec/tasks/gitlab/security/update_banned_ssh_keys_rake_spec.rb56
-rw-r--r--spec/tasks/gitlab/seed/runner_fleet_rake_spec.rb43
7 files changed, 133 insertions, 19 deletions
diff --git a/spec/tasks/gitlab/backup_rake_spec.rb b/spec/tasks/gitlab/backup_rake_spec.rb
index dc74f25db87..972851cba8c 100644
--- a/spec/tasks/gitlab/backup_rake_spec.rb
+++ b/spec/tasks/gitlab/backup_rake_spec.rb
@@ -2,7 +2,7 @@
require 'rake_helper'
-RSpec.describe 'gitlab:app namespace rake task', :delete do
+RSpec.describe 'gitlab:app namespace rake task', :delete, feature_category: :backup_restore do
let(:enable_registry) { true }
let(:backup_restore_pid_path) { "#{Rails.application.root}/tmp/backup_restore.pid" }
let(:backup_tasks) { %w[db repo uploads builds artifacts pages lfs terraform_state registry packages] }
diff --git a/spec/tasks/gitlab/db/decomposition/rollback/bump_ci_sequences_rake_spec.rb b/spec/tasks/gitlab/db/decomposition/rollback/bump_ci_sequences_rake_spec.rb
index b03e964ce87..9cdbf8539c6 100644
--- a/spec/tasks/gitlab/db/decomposition/rollback/bump_ci_sequences_rake_spec.rb
+++ b/spec/tasks/gitlab/db/decomposition/rollback/bump_ci_sequences_rake_spec.rb
@@ -3,7 +3,7 @@
require 'rake_helper'
RSpec.describe 'gitlab:db:decomposition:rollback:bump_ci_sequences', :silence_stdout,
- :suppress_gitlab_schemas_validate_connection do
+ :suppress_gitlab_schemas_validate_connection, feature_category: :pods do
before :all do
Rake.application.rake_require 'tasks/gitlab/db/decomposition/rollback/bump_ci_sequences'
diff --git a/spec/tasks/gitlab/db/lock_writes_rake_spec.rb b/spec/tasks/gitlab/db/lock_writes_rake_spec.rb
index e3155d3c377..a0a99b65767 100644
--- a/spec/tasks/gitlab/db/lock_writes_rake_spec.rb
+++ b/spec/tasks/gitlab/db/lock_writes_rake_spec.rb
@@ -2,8 +2,8 @@
require 'rake_helper'
-RSpec.describe 'gitlab:db:lock_writes', :silence_stdout, :reestablished_active_record_base,
- :suppress_gitlab_schemas_validate_connection do
+RSpec.describe 'gitlab:db:lock_writes', :silence_stdout, :reestablished_active_record_base, :delete,
+ :suppress_gitlab_schemas_validate_connection, feature_category: :pods do
before :all do
Rake.application.rake_require 'active_record/railties/databases'
Rake.application.rake_require 'tasks/seed_fu'
@@ -14,10 +14,10 @@ RSpec.describe 'gitlab:db:lock_writes', :silence_stdout, :reestablished_active_r
Rake::Task.define_task :environment
end
- let!(:project) { create(:project) }
- let!(:ci_build) { create(:ci_build) }
let(:main_connection) { ApplicationRecord.connection }
let(:ci_connection) { Ci::ApplicationRecord.connection }
+ let!(:user) { create(:user) }
+ let!(:ci_build) { create(:ci_build) }
let(:detached_partition_table) { '_test_gitlab_main_part_20220101' }
@@ -37,10 +37,23 @@ RSpec.describe 'gitlab:db:lock_writes', :silence_stdout, :reestablished_active_r
drop_after: Time.current
)
end
+ end
+
+ after do
+ run_rake_task('gitlab:db:unlock_writes')
+ end
- allow(Gitlab::Database::GitlabSchema).to receive(:table_schema).and_call_original
- allow(Gitlab::Database::GitlabSchema).to receive(:table_schema)
- .with(detached_partition_table).and_return(:gitlab_main)
+ after(:all) do
+ drop_detached_partition_sql = <<~SQL
+ DROP TABLE IF EXISTS gitlab_partitions_dynamic._test_gitlab_main_part_20220101
+ SQL
+
+ ApplicationRecord.connection.execute(drop_detached_partition_sql)
+ Ci::ApplicationRecord.connection.execute(drop_detached_partition_sql)
+
+ Gitlab::Database::SharedModel.using_connection(ApplicationRecord.connection) do
+ Postgresql::DetachedPartition.delete_all
+ end
end
context 'single database' do
@@ -60,7 +73,7 @@ RSpec.describe 'gitlab:db:lock_writes', :silence_stdout, :reestablished_active_r
it 'will be still able to modify tables that belong to the main two schemas' do
run_rake_task('gitlab:db:lock_writes')
expect do
- Project.last.touch
+ User.last.touch
Ci::Build.last.touch
end.not_to raise_error
end
@@ -81,7 +94,7 @@ RSpec.describe 'gitlab:db:lock_writes', :silence_stdout, :reestablished_active_r
context 'when locking writes' do
it 'still allows writes on the tables with the correct connections' do
- Project.update_all(updated_at: Time.now)
+ User.update_all(updated_at: Time.now)
Ci::Build.update_all(updated_at: Time.now)
end
@@ -90,7 +103,7 @@ RSpec.describe 'gitlab:db:lock_writes', :silence_stdout, :reestablished_active_r
connections.each do |connection|
Gitlab::Database::SharedModel.using_connection(connection) do
LooseForeignKeys::DeletedRecord.create!(
- fully_qualified_table_name: "public.projects",
+ fully_qualified_table_name: "public.users",
primary_key_value: 1,
cleanup_attempts: 0
)
@@ -101,8 +114,8 @@ RSpec.describe 'gitlab:db:lock_writes', :silence_stdout, :reestablished_active_r
it 'prevents writes on the main tables on the ci database' do
run_rake_task('gitlab:db:lock_writes')
expect do
- ci_connection.execute("delete from projects")
- end.to raise_error(ActiveRecord::StatementInvalid, /Table: "projects" is write protected/)
+ ci_connection.execute("delete from users")
+ end.to raise_error(ActiveRecord::StatementInvalid, /Table: "users" is write protected/)
end
it 'prevents writes on the ci tables on the main database' do
@@ -135,7 +148,7 @@ RSpec.describe 'gitlab:db:lock_writes', :silence_stdout, :reestablished_active_r
it 'allows writes on the main tables on the ci database' do
run_rake_task('gitlab:db:lock_writes')
expect do
- ci_connection.execute("delete from projects")
+ ci_connection.execute("delete from users")
end.not_to raise_error
end
diff --git a/spec/tasks/gitlab/db/truncate_legacy_tables_rake_spec.rb b/spec/tasks/gitlab/db/truncate_legacy_tables_rake_spec.rb
index e95c2e241a8..a7ced4a69f3 100644
--- a/spec/tasks/gitlab/db/truncate_legacy_tables_rake_spec.rb
+++ b/spec/tasks/gitlab/db/truncate_legacy_tables_rake_spec.rb
@@ -3,7 +3,7 @@
require 'rake_helper'
RSpec.describe 'gitlab:db:truncate_legacy_tables', :silence_stdout, :reestablished_active_record_base,
- :suppress_gitlab_schemas_validate_connection do
+ :suppress_gitlab_schemas_validate_connection, feature_category: :pods do
let(:main_connection) { ApplicationRecord.connection }
let(:ci_connection) { Ci::ApplicationRecord.connection }
let(:test_gitlab_main_table) { '_test_gitlab_main_table' }
@@ -56,14 +56,16 @@ RSpec.describe 'gitlab:db:truncate_legacy_tables', :silence_stdout, :reestablish
Gitlab::Database::LockWritesManager.new(
table_name: test_gitlab_ci_table,
connection: main_connection,
- database_name: "main"
+ database_name: "main",
+ with_retries: false
).lock_writes
# Locking main table on the ci database
Gitlab::Database::LockWritesManager.new(
table_name: test_gitlab_main_table,
connection: ci_connection,
- database_name: "ci"
+ database_name: "ci",
+ with_retries: false
).lock_writes
end
diff --git a/spec/tasks/gitlab/db_rake_spec.rb b/spec/tasks/gitlab/db_rake_spec.rb
index 22abfc33d1b..7671c65d22c 100644
--- a/spec/tasks/gitlab/db_rake_spec.rb
+++ b/spec/tasks/gitlab/db_rake_spec.rb
@@ -22,7 +22,7 @@ RSpec.describe 'gitlab:db namespace rake task', :silence_stdout do
describe 'mark_migration_complete' do
context 'with a single database' do
- let(:main_model) { ActiveRecord::Base }
+ let(:main_model) { ApplicationRecord }
before do
skip_if_multiple_databases_are_setup
diff --git a/spec/tasks/gitlab/security/update_banned_ssh_keys_rake_spec.rb b/spec/tasks/gitlab/security/update_banned_ssh_keys_rake_spec.rb
new file mode 100644
index 00000000000..85f71da8c97
--- /dev/null
+++ b/spec/tasks/gitlab/security/update_banned_ssh_keys_rake_spec.rb
@@ -0,0 +1,56 @@
+# frozen_string_literal: true
+
+require 'rake_helper'
+
+# We need to load the constants here, or else stubbed
+# constants will be overwritten when `require 'git'`
+# is hit in the rake task.
+require 'git'
+
+RSpec.describe 'gitlab:security namespace rake tasks', :silence_stdout, feature_category: :security do
+ let(:fixture_path) { Rails.root.join('spec/fixtures/tasks/gitlab/security') }
+ let(:output_file) { File.join(__dir__, 'tmp/banned_keys_test.yml') }
+ let(:git_url) { 'https://github.com/rapid7/ssh-badkeys.git' }
+ let(:mock_git) { class_double('Git') }
+
+ subject(:execute) { run_rake_task('gitlab:security:update_banned_ssh_keys', git_url, output_file) }
+
+ before do
+ Rake.application.rake_require 'tasks/gitlab/security/update_banned_ssh_keys'
+ stub_const('Git', mock_git)
+ allow(Dir).to receive(:mktmpdir).and_return(fixture_path)
+ allow(mock_git).to receive(:clone)
+ end
+
+ around do |example|
+ test_dir = File.dirname(output_file)
+ FileUtils.mkdir_p(test_dir)
+
+ example.run
+
+ FileUtils.rm_rf(test_dir)
+ end
+
+ it 'adds banned keys when clone is successful' do
+ expect(mock_git).to receive(:clone).with(git_url, 'ssh-badkeys', path: fixture_path)
+
+ execute
+
+ actual = File.read(output_file)
+ expected = File.read(File.join(fixture_path, 'expected_banned_keys.yml'))
+ expect(actual).to eq(expected)
+ end
+
+ it 'exits when clone fails' do
+ expect(mock_git).to receive(:clone).with(git_url, 'ssh-badkeys', path: fixture_path).and_raise(RuntimeError)
+
+ expect { execute }.to raise_error(SystemExit)
+ end
+
+ it 'exits when max config size reaches' do
+ stub_const('MAX_CONFIG_SIZE', 0.bytes)
+ expect(mock_git).to receive(:clone).with(git_url, 'ssh-badkeys', path: fixture_path)
+
+ expect { execute }.to output(/banned_ssh_keys.yml has grown too large - halting execution/).to_stdout
+ end
+end
diff --git a/spec/tasks/gitlab/seed/runner_fleet_rake_spec.rb b/spec/tasks/gitlab/seed/runner_fleet_rake_spec.rb
new file mode 100644
index 00000000000..e0390d2aa09
--- /dev/null
+++ b/spec/tasks/gitlab/seed/runner_fleet_rake_spec.rb
@@ -0,0 +1,43 @@
+# frozen_string_literal: true
+
+require 'rake_helper'
+
+RSpec.describe 'gitlab:seed:runner_fleet rake task', :silence_stdout, feature_category: :runner_fleet do
+ let(:registration_prefix) { 'rf-' }
+ let(:runner_count) { 10 }
+ let(:job_count) { 20 }
+ let(:task_params) { [username, registration_prefix, runner_count, job_count] }
+ let(:runner_releases_url) do
+ ::Gitlab::CurrentSettings.current_application_settings.public_runner_releases_url
+ end
+
+ before do
+ Rake.application.rake_require('tasks/gitlab/seed/runner_fleet')
+
+ WebMock.stub_request(:get, runner_releases_url).to_return(
+ body: '[]',
+ status: 200,
+ headers: { 'Content-Type' => 'application/json' }
+ )
+ end
+
+ subject(:rake_task) { run_rake_task('gitlab:seed:runner_fleet', task_params) }
+
+ context 'with admin username', :enable_admin_mode do
+ let(:username) { 'runner_fleet_seed' }
+ let!(:admin) { create(:user, :admin, username: username) }
+
+ it 'performs runner fleet seed successfully' do
+ expect { rake_task }
+ .to change { Group.count }.by(6)
+ .and change { Project.count }.by(3)
+ .and change { Ci::Runner.count }.by(runner_count)
+ .and change { Ci::Runner.instance_type.count }.by(1)
+ .and change { Ci::Build.count }.by(job_count)
+
+ expect(Group.search(registration_prefix).count).to eq 6
+ expect(Project.search(registration_prefix).count).to eq 3
+ expect(Ci::Runner.search(registration_prefix).count).to eq runner_count
+ end
+ end
+end