diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-11-23 12:10:20 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-11-23 12:10:20 +0300 |
commit | b563a5209a13da8da32688a5d503a7c0e2bc3ec3 (patch) | |
tree | 67114fae70174dff02f5c95089fca0d0e0b7d4d4 /spec/migrations | |
parent | 22622fab4a943df0988c86d175f28bb004ff8663 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/migrations')
-rw-r--r-- | spec/migrations/20211110143306_add_not_null_constraint_to_security_findings_uuid_spec.rb | 23 | ||||
-rw-r--r-- | spec/migrations/20211110151350_schedule_drop_invalid_security_findings_spec.rb | 71 |
2 files changed, 94 insertions, 0 deletions
diff --git a/spec/migrations/20211110143306_add_not_null_constraint_to_security_findings_uuid_spec.rb b/spec/migrations/20211110143306_add_not_null_constraint_to_security_findings_uuid_spec.rb new file mode 100644 index 00000000000..946fbf7f568 --- /dev/null +++ b/spec/migrations/20211110143306_add_not_null_constraint_to_security_findings_uuid_spec.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true +require 'spec_helper' +require_migration! + +RSpec.describe AddNotNullConstraintToSecurityFindingsUuid do + let_it_be(:security_findings) { table(:security_findings) } + let_it_be(:migration) { described_class.new } + + before do + allow(migration).to receive(:transaction_open?).and_return(false) + allow(migration).to receive(:with_lock_retries).and_yield + end + + it 'adds a check constraint' do + constraint = security_findings.connection.check_constraints(:security_findings).find { |constraint| constraint.expression == "uuid IS NOT NULL" } + expect(constraint).to be_nil + + migration.up + + constraint = security_findings.connection.check_constraints(:security_findings).find { |constraint| constraint.expression == "uuid IS NOT NULL" } + expect(constraint).to be_a(ActiveRecord::ConnectionAdapters::CheckConstraintDefinition) + end +end diff --git a/spec/migrations/20211110151350_schedule_drop_invalid_security_findings_spec.rb b/spec/migrations/20211110151350_schedule_drop_invalid_security_findings_spec.rb new file mode 100644 index 00000000000..90debca13e1 --- /dev/null +++ b/spec/migrations/20211110151350_schedule_drop_invalid_security_findings_spec.rb @@ -0,0 +1,71 @@ +# frozen_string_literal: true + +require 'spec_helper' +require_migration! + +RSpec.describe ScheduleDropInvalidSecurityFindings, :migration, schema: 20211108211434 do + let_it_be(:background_migration_jobs) { table(:background_migration_jobs) } + + let_it_be(:namespace) { table(:namespaces).create!(name: 'user', path: 'user') } + let_it_be(:project) { table(:projects).create!(namespace_id: namespace.id) } + + let_it_be(:pipelines) { table(:ci_pipelines) } + let_it_be(:pipeline) { pipelines.create!(project_id: project.id) } + + let_it_be(:ci_builds) { table(:ci_builds) } + let_it_be(:ci_build) { ci_builds.create! } + + let_it_be(:security_scans) { table(:security_scans) } + let_it_be(:security_scan) do + security_scans.create!( + scan_type: 1, + status: 1, + build_id: ci_build.id, + project_id: project.id, + pipeline_id: pipeline.id + ) + end + + let_it_be(:vulnerability_scanners) { table(:vulnerability_scanners) } + let_it_be(:vulnerability_scanner) { vulnerability_scanners.create!(project_id: project.id, external_id: 'test 1', name: 'test scanner 1') } + + let_it_be(:security_findings) { table(:security_findings) } + let_it_be(:security_finding_without_uuid) do + security_findings.create!( + severity: 1, + confidence: 1, + scan_id: security_scan.id, + scanner_id: vulnerability_scanner.id, + uuid: nil + ) + end + + let_it_be(:security_finding_with_uuid) do + security_findings.create!( + severity: 1, + confidence: 1, + scan_id: security_scan.id, + scanner_id: vulnerability_scanner.id, + uuid: 'bd95c085-71aa-51d7-9bb6-08ae669c262e' + ) + end + + before do + stub_const("#{described_class}::BATCH_SIZE", 1) + stub_const("#{described_class}::SUB_BATCH_SIZE", 1) + end + + around do |example| + freeze_time { Sidekiq::Testing.fake! { example.run } } + end + + it 'schedules background migrations' do + migrate! + + expect(background_migration_jobs.count).to eq(1) + expect(background_migration_jobs.first.arguments).to match_array([security_finding_without_uuid.id, security_finding_without_uuid.id, described_class::SUB_BATCH_SIZE]) + + expect(BackgroundMigrationWorker.jobs.size).to eq(1) + expect(described_class::MIGRATION).to be_scheduled_delayed_migration(2.minutes, security_finding_without_uuid.id, security_finding_without_uuid.id, described_class::SUB_BATCH_SIZE) + end +end |