diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-06-16 18:25:58 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-06-16 18:25:58 +0000 |
commit | a5f4bba440d7f9ea47046a0a561d49adf0a1e6d4 (patch) | |
tree | fb69158581673816a8cd895f9d352dcb3c678b1e /spec/lib/gitlab/background_migration | |
parent | d16b2e8639e99961de6ddc93909f3bb5c1445ba1 (diff) | |
download | gitlab-ce-a5f4bba440d7f9ea47046a0a561d49adf0a1e6d4.tar.gz |
Add latest changes from gitlab-org/gitlab@14-0-stable-eev14.0.0-rc42
Diffstat (limited to 'spec/lib/gitlab/background_migration')
5 files changed, 307 insertions, 5 deletions
diff --git a/spec/lib/gitlab/background_migration/cleanup_orphaned_lfs_objects_projects_spec.rb b/spec/lib/gitlab/background_migration/cleanup_orphaned_lfs_objects_projects_spec.rb new file mode 100644 index 00000000000..8a3671b2e53 --- /dev/null +++ b/spec/lib/gitlab/background_migration/cleanup_orphaned_lfs_objects_projects_spec.rb @@ -0,0 +1,82 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::BackgroundMigration::CleanupOrphanedLfsObjectsProjects, schema: 20210514063252 do + let(:lfs_objects_projects) { table(:lfs_objects_projects) } + let(:lfs_objects) { table(:lfs_objects) } + let(:projects) { table(:projects) } + let(:namespaces) { table(:namespaces) } + + let(:namespace) { namespaces.create!(name: 'namespace', path: 'namespace') } + let(:project) { projects.create!(namespace_id: namespace.id) } + let(:another_project) { projects.create!(namespace_id: namespace.id) } + let(:lfs_object) { lfs_objects.create!(oid: 'abcdef', size: 1) } + let(:another_lfs_object) { lfs_objects.create!(oid: '1abcde', size: 2) } + + let!(:without_object1) { create_object(project_id: project.id) } + let!(:without_object2) { create_object(project_id: another_project.id) } + let!(:without_object3) { create_object(project_id: another_project.id) } + let!(:with_project_and_object1) { create_object(project_id: project.id, lfs_object_id: lfs_object.id) } + let!(:with_project_and_object2) { create_object(project_id: project.id, lfs_object_id: another_lfs_object.id) } + let!(:with_project_and_object3) { create_object(project_id: another_project.id, lfs_object_id: another_lfs_object.id) } + let!(:without_project1) { create_object(lfs_object_id: lfs_object.id) } + let!(:without_project2) { create_object(lfs_object_id: another_lfs_object.id) } + let!(:without_project_and_object) { create_object } + + def create_object(project_id: non_existing_record_id, lfs_object_id: non_existing_record_id) + lfs_objects_project = nil + + ActiveRecord::Base.connection.disable_referential_integrity do + lfs_objects_project = lfs_objects_projects.create!(project_id: project_id, lfs_object_id: lfs_object_id) + end + + lfs_objects_project + end + + subject { described_class.new } + + describe '#perform' do + it 'lfs_objects_projects without an existing lfs object or project are removed' do + subject.perform(without_object1.id, without_object3.id) + + expect(lfs_objects_projects.all).to match_array([ + with_project_and_object1, with_project_and_object2, with_project_and_object3, + without_project1, without_project2, without_project_and_object + ]) + + subject.perform(with_project_and_object1.id, with_project_and_object3.id) + + expect(lfs_objects_projects.all).to match_array([ + with_project_and_object1, with_project_and_object2, with_project_and_object3, + without_project1, without_project2, without_project_and_object + ]) + + subject.perform(without_project1.id, without_project_and_object.id) + + expect(lfs_objects_projects.all).to match_array([ + with_project_and_object1, with_project_and_object2, with_project_and_object3 + ]) + + expect(lfs_objects.ids).to contain_exactly(lfs_object.id, another_lfs_object.id) + expect(projects.ids).to contain_exactly(project.id, another_project.id) + end + + it 'cache for affected projects is being reset' do + expect(ProjectCacheWorker).to receive(:bulk_perform_in) do |delay, args| + expect(delay).to eq(1.minute) + expect(args).to match_array([[project.id, [], [:lfs_objects_size]], [another_project.id, [], [:lfs_objects_size]]]) + end + + subject.perform(without_object1.id, with_project_and_object1.id) + + expect(ProjectCacheWorker).not_to receive(:bulk_perform_in) + + subject.perform(with_project_and_object1.id, with_project_and_object3.id) + + expect(ProjectCacheWorker).not_to receive(:bulk_perform_in) + + subject.perform(without_project1.id, without_project_and_object.id) + end + end +end diff --git a/spec/lib/gitlab/background_migration/disable_expiration_policies_linked_to_no_container_images_spec.rb b/spec/lib/gitlab/background_migration/disable_expiration_policies_linked_to_no_container_images_spec.rb new file mode 100644 index 00000000000..04eb9ad475f --- /dev/null +++ b/spec/lib/gitlab/background_migration/disable_expiration_policies_linked_to_no_container_images_spec.rb @@ -0,0 +1,142 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::BackgroundMigration::DisableExpirationPoliciesLinkedToNoContainerImages do + let_it_be(:projects) { table(:projects) } + let_it_be(:container_expiration_policies) { table(:container_expiration_policies) } + let_it_be(:container_repositories) { table(:container_repositories) } + let_it_be(:namespaces) { table(:namespaces) } + + let!(:namespace) { namespaces.create!(name: 'test', path: 'test') } + + let!(:policy1) { create_expiration_policy(project_id: 1, enabled: true) } + let!(:policy2) { create_expiration_policy(project_id: 2, enabled: false) } + let!(:policy3) { create_expiration_policy(project_id: 3, enabled: false) } + let!(:policy4) { create_expiration_policy(project_id: 4, enabled: true, with_images: true) } + let!(:policy5) { create_expiration_policy(project_id: 5, enabled: false, with_images: true) } + let!(:policy6) { create_expiration_policy(project_id: 6, enabled: false) } + let!(:policy7) { create_expiration_policy(project_id: 7, enabled: true) } + let!(:policy8) { create_expiration_policy(project_id: 8, enabled: true, with_images: true) } + let!(:policy9) { create_expiration_policy(project_id: 9, enabled: true) } + + describe '#perform' do + subject { described_class.new.perform(from_id, to_id) } + + shared_examples 'disabling policies with no images' do + it 'disables the proper policies' do + subject + + rows = container_expiration_policies.order(:project_id).to_h do |row| + [row.project_id, row.enabled] + end + expect(rows).to eq(expected_rows) + end + end + + context 'the whole range' do + let(:from_id) { 1 } + let(:to_id) { 9 } + + it_behaves_like 'disabling policies with no images' do + let(:expected_rows) do + { + 1 => false, + 2 => false, + 3 => false, + 4 => true, + 5 => false, + 6 => false, + 7 => false, + 8 => true, + 9 => false + } + end + end + end + + context 'a range with no policies to disable' do + let(:from_id) { 2 } + let(:to_id) { 6 } + + it_behaves_like 'disabling policies with no images' do + let(:expected_rows) do + { + 1 => true, + 2 => false, + 3 => false, + 4 => true, + 5 => false, + 6 => false, + 7 => true, + 8 => true, + 9 => true + } + end + end + end + + context 'a range with only images' do + let(:from_id) { 4 } + let(:to_id) { 5 } + + it_behaves_like 'disabling policies with no images' do + let(:expected_rows) do + { + 1 => true, + 2 => false, + 3 => false, + 4 => true, + 5 => false, + 6 => false, + 7 => true, + 8 => true, + 9 => true + } + end + end + end + + context 'a range with a single element' do + let(:from_id) { 9 } + let(:to_id) { 9 } + + it_behaves_like 'disabling policies with no images' do + let(:expected_rows) do + { + 1 => true, + 2 => false, + 3 => false, + 4 => true, + 5 => false, + 6 => false, + 7 => true, + 8 => true, + 9 => false + } + end + end + end + end + + def create_expiration_policy(project_id:, enabled:, with_images: false) + projects.create!(id: project_id, namespace_id: namespace.id, name: "gitlab-#{project_id}") + + if with_images + container_repositories.create!(project_id: project_id, name: "image-#{project_id}") + end + + container_expiration_policies.create!( + enabled: enabled, + project_id: project_id + ) + end + + def enabled_policies + container_expiration_policies.where(enabled: true) + end + + def disabled_policies + container_expiration_policies.where(enabled: false) + end +end diff --git a/spec/lib/gitlab/background_migration/migrate_issue_trackers_sensitive_data_spec.rb b/spec/lib/gitlab/background_migration/migrate_issue_trackers_sensitive_data_spec.rb index 8668216d014..80879c8c6d9 100644 --- a/spec/lib/gitlab/background_migration/migrate_issue_trackers_sensitive_data_spec.rb +++ b/spec/lib/gitlab/background_migration/migrate_issue_trackers_sensitive_data_spec.rb @@ -291,7 +291,7 @@ RSpec.describe Gitlab::BackgroundMigration::MigrateIssueTrackersSensitiveData, s services.create!(id: 20, type: 'JiraService', properties: jira_properties.to_json, category: 'issue_tracker') end - let!(:bugzilla_service_valid) do + let!(:bugzilla_integration_valid) do services.create!(id: 11, type: 'BugzillaService', title: nil, properties: tracker_properties.to_json, category: 'issue_tracker') end @@ -314,14 +314,14 @@ RSpec.describe Gitlab::BackgroundMigration::MigrateIssueTrackersSensitiveData, s expect(jira_service_valid.title).to eq(title) expect(jira_service_valid.description).to eq(description) - bugzilla_service_valid.reload - data = IssueTrackerData.find_by(service_id: bugzilla_service_valid.id) + bugzilla_integration_valid.reload + data = IssueTrackerData.find_by(service_id: bugzilla_integration_valid.id) expect(data.project_url).to eq(url) expect(data.issues_url).to eq(issues_url) expect(data.new_issue_url).to eq(new_issue_url) - expect(bugzilla_service_valid.title).to eq(title) - expect(bugzilla_service_valid.description).to eq(description) + expect(bugzilla_integration_valid.title).to eq(title) + expect(bugzilla_integration_valid.description).to eq(description) end end end diff --git a/spec/lib/gitlab/background_migration/recalculate_vulnerabilities_occurrences_uuid_spec.rb b/spec/lib/gitlab/background_migration/recalculate_vulnerabilities_occurrences_uuid_spec.rb index 990ef4fbe6a..70906961641 100644 --- a/spec/lib/gitlab/background_migration/recalculate_vulnerabilities_occurrences_uuid_spec.rb +++ b/spec/lib/gitlab/background_migration/recalculate_vulnerabilities_occurrences_uuid_spec.rb @@ -73,6 +73,14 @@ RSpec.describe Gitlab::BackgroundMigration::RecalculateVulnerabilitiesOccurrence expect(vulnerabilities_findings.pluck(:uuid)).to eq([desired_uuid_v5]) end + + it 'logs recalculation' do + expect_next_instance_of(Gitlab::BackgroundMigration::Logger) do |instance| + expect(instance).to receive(:info).once + end + + subject + end end context "when finding has a UUIDv5" do @@ -99,6 +107,32 @@ RSpec.describe Gitlab::BackgroundMigration::RecalculateVulnerabilitiesOccurrence end end + context 'when recalculation fails' do + before do + @uuid_v4 = create_finding!( + vulnerability_id: vulnerability_for_uuidv4.id, + project_id: project.id, + scanner_id: different_scanner.id, + primary_identifier_id: different_vulnerability_identifier.id, + report_type: 0, # "sast" + location_fingerprint: "fa18f432f1d56675f4098d318739c3cd5b14eb3e", + uuid: known_uuid_v4 + ) + + allow(Gitlab::ErrorTracking).to receive(:track_and_raise_for_dev_exception) + allow(::Gitlab::Database::BulkUpdate).to receive(:execute).and_raise(expected_error) + end + + let(:finding) { @uuid_v4 } + let(:expected_error) { RuntimeError.new } + + it 'captures the errors and does not crash entirely' do + expect { subject }.not_to raise_error + + expect(Gitlab::ErrorTracking).to have_received(:track_and_raise_for_dev_exception).with(expected_error).once + end + end + private def create_vulnerability!(project_id:, author_id:, title: 'test', severity: 7, confidence: 7, report_type: 0) diff --git a/spec/lib/gitlab/background_migration/update_jira_tracker_data_deployment_type_based_on_url_spec.rb b/spec/lib/gitlab/background_migration/update_jira_tracker_data_deployment_type_based_on_url_spec.rb new file mode 100644 index 00000000000..f7466a2ddfd --- /dev/null +++ b/spec/lib/gitlab/background_migration/update_jira_tracker_data_deployment_type_based_on_url_spec.rb @@ -0,0 +1,44 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::BackgroundMigration::UpdateJiraTrackerDataDeploymentTypeBasedOnUrl do + let(:services_table) { table(:services) } + let(:service_jira_cloud) { services_table.create!(id: 1, type: 'JiraService') } + let(:service_jira_server) { services_table.create!(id: 2, type: 'JiraService') } + + before do + jira_tracker_data = Class.new(ApplicationRecord) do + self.table_name = 'jira_tracker_data' + + def self.encryption_options + { + key: Settings.attr_encrypted_db_key_base_32, + encode: true, + mode: :per_attribute_iv, + algorithm: 'aes-256-gcm' + } + end + + attr_encrypted :url, encryption_options + attr_encrypted :api_url, encryption_options + attr_encrypted :username, encryption_options + attr_encrypted :password, encryption_options + end + + stub_const('JiraTrackerData', jira_tracker_data) + end + + let!(:tracker_data_cloud) { JiraTrackerData.create!(id: 1, service_id: service_jira_cloud.id, url: "https://test-domain.atlassian.net", deployment_type: 0) } + let!(:tracker_data_server) { JiraTrackerData.create!(id: 2, service_id: service_jira_server.id, url: "http://totally-not-jira-server.company.org", deployment_type: 0) } + + subject { described_class.new.perform(tracker_data_cloud.id, tracker_data_server.id) } + + it "changes unknown deployment_types based on URL" do + expect(JiraTrackerData.pluck(:deployment_type)).to eq([0, 0]) + + subject + + expect(JiraTrackerData.pluck(:deployment_type)).to eq([2, 1]) + end +end |