diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-02-28 12:09:01 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-02-28 12:09:01 +0300 |
commit | 67cdfd2683b89bce260600fa8925eefdcdf9e3e5 (patch) | |
tree | 5d01075e0ef7bc62bb27459f75d79ef138aec660 /spec/migrations | |
parent | ca2a7ed5bd43483f10fd74f46f31e32614889738 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/migrations')
4 files changed, 169 insertions, 0 deletions
diff --git a/spec/migrations/cleanup_empty_snippet_user_mentions_spec.rb b/spec/migrations/cleanup_empty_snippet_user_mentions_spec.rb new file mode 100644 index 00000000000..d229f0b2b59 --- /dev/null +++ b/spec/migrations/cleanup_empty_snippet_user_mentions_spec.rb @@ -0,0 +1,42 @@ +# frozen_string_literal: true + +require 'spec_helper' +require Rails.root.join('db', 'post_migrate', '20200127111953_cleanup_empty_snippet_user_mentions') + +describe CleanupEmptySnippetUserMentions, :migration, :sidekiq do + let(:users) { table(:users) } + let(:projects) { table(:projects) } + let(:namespaces) { table(:namespaces) } + let(:snippets) { table(:snippets) } + let(:snippet_user_mentions) { table(:snippet_user_mentions) } + let(:notes) { table(:notes) } + + let(:user) { users.create!(name: 'root', email: 'root@example.com', username: 'root', projects_limit: 0) } + let(:group) { namespaces.create!(name: 'group1', path: 'group1', owner_id: user.id) } + let(:project) { projects.create!(name: 'gitlab1', path: 'gitlab1', namespace_id: group.id, visibility_level: 0) } + let(:snippet) { snippets.create!(title: "title1", title_html: 'title1', description: 'snippet description with @root mention', project_id: project.id, author_id: user.id) } + + let!(:resource1) { notes.create!(note: 'note for @root to check', noteable_id: snippet.id, noteable_type: 'Snippet') } + let!(:resource2) { notes.create!(note: 'note for @root to check', noteable_id: snippet.id, noteable_type: 'Snippet', system: true) } + let!(:resource3) { notes.create!(note: 'note for @root to check', noteable_id: snippet.id, noteable_type: 'Snippet') } + + # non-migrateable resources + # this note is already migrated, as it has a record in the snippet_user_mentions table + let!(:resource4) { notes.create!(note: 'note for @root to check', noteable_id: snippet.id, noteable_type: 'Snippet') } + let!(:user_mention) { snippet_user_mentions.create!(snippet_id: snippet.id, note_id: resource4.id, mentioned_users_ids: [1]) } + # this note points to an innexistent noteable record + let!(:resource5) { notes.create!(note: 'note for @root to check', noteable_id: snippets.maximum(:id) + 10, noteable_type: 'Snippet') } + + # these should get cleanup, by the migration + let!(:blank_snippet_user_mention1) { snippet_user_mentions.create!(snippet_id: snippet.id, note_id: resource1.id)} + let!(:blank_snippet_user_mention2) { snippet_user_mentions.create!(snippet_id: snippet.id, note_id: resource2.id)} + let!(:blank_snippet_user_mention3) { snippet_user_mentions.create!(snippet_id: snippet.id, note_id: resource3.id)} + + it 'cleanups blank user mentions' do + expect(snippet_user_mentions.count).to eq 4 + + migrate! + + expect(snippet_user_mentions.count).to eq 1 + end +end diff --git a/spec/migrations/migrate_snippet_mentions_to_db_spec.rb b/spec/migrations/migrate_snippet_mentions_to_db_spec.rb new file mode 100644 index 00000000000..6644329fc11 --- /dev/null +++ b/spec/migrations/migrate_snippet_mentions_to_db_spec.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true + +require 'spec_helper' +require Rails.root.join('db', 'post_migrate', '20200127131953_migrate_snippet_mentions_to_db') + +describe MigrateSnippetMentionsToDb, :migration, :sidekiq do + let(:users) { table(:users) } + let(:namespaces) { table(:namespaces) } + let(:projects) { table(:projects) } + let(:snippets) { table(:snippets) } + let(:snippet_user_mentions) { table(:snippet_user_mentions) } + + let(:user) { users.create!(name: 'root', email: 'root@example.com', username: 'root', projects_limit: 0) } + let(:group) { namespaces.create!(name: 'group1', path: 'group1', owner_id: user.id) } + let(:project) { projects.create!(name: 'gitlab1', path: 'gitlab1', namespace_id: group.id, visibility_level: 0) } + let!(:resource1) { snippets.create!(title: "title1", title_html: 'title1', description: 'snippet description with @root mention', project_id: project.id, author_id: user.id) } + let!(:resource2) { snippets.create!(title: "title2", title_html: "title2", description: 'snippet description with @group mention', project_id: project.id, author_id: user.id) } + let!(:resource3) { snippets.create!(title: "title3", title_html: "title3", description: 'snippet description with @project mention', project_id: project.id, author_id: user.id) } + + # non-migrateable resources + # this snippet is already migrated, as it has a record in the snippet_user_mentions table + let!(:resource4) { snippets.create!(title: "title4", title_html: "title4", description: 'snippet description with @project mention', project_id: project.id, author_id: user.id) } + let!(:user_mention) { snippet_user_mentions.create!(snippet_id: resource4.id, mentioned_users_ids: [1]) } + # this snippet has no mentions so should be filtered out + let!(:resource5) { snippets.create!(title: "title5", title_html: "title5", description: 'snippet description with no mention', project_id: project.id, author_id: user.id) } + + it_behaves_like 'schedules resource mentions migration', Snippet, false +end diff --git a/spec/migrations/migrate_snippet_notes_mentions_to_db_spec.rb b/spec/migrations/migrate_snippet_notes_mentions_to_db_spec.rb new file mode 100644 index 00000000000..2ebe80e6ae3 --- /dev/null +++ b/spec/migrations/migrate_snippet_notes_mentions_to_db_spec.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +require 'spec_helper' +require Rails.root.join('db', 'post_migrate', '20200127151953_migrate_snippet_notes_mentions_to_db') + +describe MigrateSnippetNotesMentionsToDb, :migration, :sidekiq do + let(:users) { table(:users) } + let(:projects) { table(:projects) } + let(:namespaces) { table(:namespaces) } + let(:snippets) { table(:snippets) } + let(:snippet_user_mentions) { table(:snippet_user_mentions) } + let(:notes) { table(:notes) } + + let(:user) { users.create!(name: 'root', email: 'root@example.com', username: 'root', projects_limit: 0) } + let(:group) { namespaces.create!(name: 'group1', path: 'group1', owner_id: user.id) } + let(:project) { projects.create!(name: 'gitlab1', path: 'gitlab1', namespace_id: group.id, visibility_level: 0) } + let(:snippet) { snippets.create!(title: "title1", title_html: 'title1', description: 'snippet description with @root mention', project_id: project.id, author_id: user.id) } + + let!(:resource1) { notes.create!(note: 'note for @root to check', noteable_id: snippet.id, noteable_type: 'Snippet') } + let!(:resource2) { notes.create!(note: 'note for @root to check', noteable_id: snippet.id, noteable_type: 'Snippet', system: true) } + let!(:resource3) { notes.create!(note: 'note for @root to check', noteable_id: snippet.id, noteable_type: 'Snippet') } + + # non-migrateable resources + # this note is already migrated, as it has a record in the snippet_user_mentions table + let!(:resource4) { notes.create!(note: 'note for @root to check', noteable_id: snippet.id, noteable_type: 'Snippet') } + let!(:user_mention) { snippet_user_mentions.create!(snippet_id: snippet.id, note_id: resource4.id, mentioned_users_ids: [1]) } + # this note points to an innexistent noteable record + let!(:resource5) { notes.create!(note: 'note for @root to check', noteable_id: snippets.maximum(:id) + 10, noteable_type: 'Snippet') } + + it_behaves_like 'schedules resource mentions migration', Snippet, true +end diff --git a/spec/migrations/schedule_migrate_security_scans_spec.rb b/spec/migrations/schedule_migrate_security_scans_spec.rb new file mode 100644 index 00000000000..ae066e8f91c --- /dev/null +++ b/spec/migrations/schedule_migrate_security_scans_spec.rb @@ -0,0 +1,68 @@ +# frozen_string_literal: true + +require 'spec_helper' +require Rails.root.join('db', 'post_migrate', '20200217225719_schedule_migrate_security_scans.rb') + +# rubocop: disable RSpec/FactoriesInMigrationSpecs +describe ScheduleMigrateSecurityScans, :migration, :sidekiq do + let(:migration) { described_class.new } + let(:namespaces) { table(:namespaces) } + let(:projects) { table(:projects) } + let(:builds) { table(:ci_builds) } + let(:job_artifacts) { table(:ci_job_artifacts) } + + let(:namespace) { namespaces.create!(name: "foo", path: "bar") } + let(:project) { projects.create!(namespace_id: namespace.id) } + let(:build) { builds.create! } + + before do + stub_const("#{described_class.name}::BATCH_SIZE", 1) + stub_const("#{described_class.name}::INTERVAL", 5.minutes.to_i) + end + + context 'no security job artifacts' do + before do + table(:ci_job_artifacts) + end + + it 'does not schedule migration' do + Sidekiq::Testing.fake! do + migrate! + + expect(BackgroundMigrationWorker.jobs).to be_empty + end + end + end + + context 'has security job artifacts' do + let!(:job_artifact_1) { job_artifacts.create!(project_id: project.id, job_id: build.id, file_type: 5) } + let!(:job_artifact_2) { job_artifacts.create!(project_id: project.id, job_id: build.id, file_type: 8) } + + it 'schedules migration of security scans' do + Sidekiq::Testing.fake! do + Timecop.freeze do + migration.up + + expect(described_class::MIGRATION).to be_scheduled_delayed_migration(5.minutes, job_artifact_1.id, job_artifact_1.id) + expect(described_class::MIGRATION).to be_scheduled_delayed_migration(10.minutes, job_artifact_2.id, job_artifact_2.id) + expect(BackgroundMigrationWorker.jobs.size).to eq(2) + end + end + end + end + + context 'has non-security job artifacts' do + let!(:job_artifact_1) { job_artifacts.create!(project_id: project.id, job_id: build.id, file_type: 4) } + let!(:job_artifact_2) { job_artifacts.create!(project_id: project.id, job_id: build.id, file_type: 9) } + + it 'schedules migration of security scans' do + Sidekiq::Testing.fake! do + Timecop.freeze do + migration.up + + expect(BackgroundMigrationWorker.jobs).to be_empty + end + end + end + end +end |