diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-07-26 00:08:32 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-07-26 00:08:32 +0300 |
commit | aadc5da1b1ccba52944e6df0f2173fad5d3fe675 (patch) | |
tree | 3b43f157e9117be6713fc0c79014878e06308c6e /spec | |
parent | 0599ea8fb389d70cab67a78e80d61eadc6aeaaff (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec')
54 files changed, 386 insertions, 82 deletions
diff --git a/spec/controllers/projects/blob_controller_spec.rb b/spec/controllers/projects/blob_controller_spec.rb index cc807098498..887a5ba598f 100644 --- a/spec/controllers/projects/blob_controller_spec.rb +++ b/spec/controllers/projects/blob_controller_spec.rb @@ -352,7 +352,6 @@ RSpec.describe Projects::BlobController do project_new_merge_request_path( forked_project, merge_request: { - source_project_id: forked_project.id, target_project_id: project.id, source_branch: "fork-test-1", target_branch: "master" diff --git a/spec/controllers/projects/commit_controller_spec.rb b/spec/controllers/projects/commit_controller_spec.rb index a72c98552a5..59b2168c41a 100644 --- a/spec/controllers/projects/commit_controller_spec.rb +++ b/spec/controllers/projects/commit_controller_spec.rb @@ -378,7 +378,6 @@ RSpec.describe Projects::CommitController do project_new_merge_request_path( source_project, merge_request: { - source_project_id: source_project.id, target_project_id: project.id, source_branch: branch, target_branch: 'feature' diff --git a/spec/controllers/search_controller_spec.rb b/spec/controllers/search_controller_spec.rb index b4d4e01e972..6922b0f3831 100644 --- a/spec/controllers/search_controller_spec.rb +++ b/spec/controllers/search_controller_spec.rb @@ -410,9 +410,20 @@ RSpec.describe SearchController do expect(payload[:metadata]['meta.search.project_ids']).to eq(%w(456 789)) expect(payload[:metadata]['meta.search.type']).to eq('basic') expect(payload[:metadata]['meta.search.level']).to eq('global') + expect(payload[:metadata]['meta.search.language']).to eq('ruby') end - get :show, params: { scope: 'issues', search: 'hello world', group_id: '123', project_id: '456', project_ids: %w(456 789), confidential: true, state: true, force_search_results: true } + get :show, params: { + scope: 'issues', + search: 'hello world', + group_id: '123', + project_id: '456', + project_ids: %w(456 789), + confidential: true, + state: true, + force_search_results: true, + language: 'ruby' + } end it 'appends the default scope in meta.search.scope' do diff --git a/spec/features/projects/merge_request_button_spec.rb b/spec/features/projects/merge_request_button_spec.rb index 335ae6794b7..eb52a7821f9 100644 --- a/spec/features/projects/merge_request_button_spec.rb +++ b/spec/features/projects/merge_request_button_spec.rb @@ -11,6 +11,8 @@ RSpec.describe 'Merge Request button' do let(:forked_project) { fork_project(project, user, repository: true) } shared_examples 'Merge request button only shown when allowed' do + let(:extra_mr_params) { {} } + context 'not logged in' do it 'does not show Create merge request button' do visit url @@ -31,11 +33,8 @@ RSpec.describe 'Merge Request button' do href = project_new_merge_request_path( project, merge_request: { - source_project_id: project.id, - source_branch: 'feature', - target_project_id: project.id, - target_branch: 'master' - } + source_branch: 'feature' + }.merge(extra_mr_params) ) visit url @@ -90,11 +89,8 @@ RSpec.describe 'Merge Request button' do href = project_new_merge_request_path( forked_project, merge_request: { - source_project_id: forked_project.id, - source_branch: 'feature', - target_project_id: forked_project.id, - target_branch: 'master' - } + source_branch: 'feature' + }.merge(extra_mr_params) ) visit fork_url @@ -121,6 +117,7 @@ RSpec.describe 'Merge Request button' do it_behaves_like 'Merge request button only shown when allowed' do let(:url) { project_compare_path(project, from: 'master', to: 'feature') } let(:fork_url) { project_compare_path(forked_project, from: 'master', to: 'feature') } + let(:extra_mr_params) { { target_project_id: project.id, target_branch: 'master' } } end it 'shows the correct merge request button when viewing across forks', :js do @@ -128,9 +125,8 @@ RSpec.describe 'Merge Request button' do project.add_developer(user) href = project_new_merge_request_path( - project, + forked_project, merge_request: { - source_project_id: forked_project.id, source_branch: 'feature', target_project_id: project.id, target_branch: 'master' diff --git a/spec/frontend/projects/compare/components/app_spec.js b/spec/frontend/projects/compare/components/app_spec.js index 18e7f2e0f6e..c9ffdf20c32 100644 --- a/spec/frontend/projects/compare/components/app_spec.js +++ b/spec/frontend/projects/compare/components/app_spec.js @@ -34,7 +34,8 @@ describe('CompareApp component', () => { expect(wrapper.props()).toEqual( expect.objectContaining({ projectCompareIndexPath: defaultProps.projectCompareIndexPath, - refsProjectPath: defaultProps.refsProjectPath, + sourceProjectRefsPath: defaultProps.sourceProjectRefsPath, + targetProjectRefsPath: defaultProps.targetProjectRefsPath, paramsFrom: defaultProps.paramsFrom, paramsTo: defaultProps.paramsTo, }), diff --git a/spec/frontend/projects/compare/components/mock_data.js b/spec/frontend/projects/compare/components/mock_data.js index 61309928c26..81d64469a2a 100644 --- a/spec/frontend/projects/compare/components/mock_data.js +++ b/spec/frontend/projects/compare/components/mock_data.js @@ -1,7 +1,12 @@ -const refsProjectPath = 'some/refs/path'; +const sourceProjectRefsPath = 'some/refs/path'; +const targetProjectRefsPath = 'some/refs/path'; const paramsName = 'to'; const paramsBranch = 'main'; -const defaultProject = { +const sourceProject = { + name: 'some-to-name', + id: '2', +}; +const targetProject = { name: 'some-to-name', id: '1', }; @@ -9,29 +14,31 @@ const defaultProject = { export const appDefaultProps = { projectCompareIndexPath: 'some/path', projectMergeRequestPath: '', - projects: [defaultProject], + projects: [sourceProject], paramsFrom: 'main', paramsTo: 'target/branch', createMrPath: '', - refsProjectPath, - defaultProject, + sourceProjectRefsPath, + targetProjectRefsPath, + sourceProject, + targetProject, }; export const revisionCardDefaultProps = { - selectedProject: defaultProject, + selectedProject: targetProject, paramsBranch, revisionText: 'Source', - refsProjectPath, + refsProjectPath: sourceProjectRefsPath, paramsName, }; export const repoDropdownDefaultProps = { - selectedProject: defaultProject, + selectedProject: targetProject, paramsName, }; export const revisionDropdownDefaultProps = { - refsProjectPath, + refsProjectPath: sourceProjectRefsPath, paramsBranch, paramsName, }; diff --git a/spec/frontend/vue_shared/components/source_viewer/plugins/utils/dependency_linker_util_spec.js b/spec/frontend/vue_shared/components/source_viewer/plugins/utils/dependency_linker_util_spec.js index ee200747af9..8079d5ad99a 100644 --- a/spec/frontend/vue_shared/components/source_viewer/plugins/utils/dependency_linker_util_spec.js +++ b/spec/frontend/vue_shared/components/source_viewer/plugins/utils/dependency_linker_util_spec.js @@ -14,10 +14,11 @@ describe('createLink', () => { it('escapes the user-controlled content', () => { const unescapedXSS = '<script>XSS</script>'; - const escapedXSS = '&lt;script&gt;XSS&lt;/script&gt;'; + const escapedPackageName = '<script>XSS</script>'; + const escapedHref = '&lt;script&gt;XSS&lt;/script&gt;'; const href = `http://test.com/${unescapedXSS}`; const innerText = `testing${unescapedXSS}`; - const result = `<a href="http://test.com/${escapedXSS}" rel="nofollow noreferrer noopener">testing${escapedXSS}</a>`; + const result = `<a href="http://test.com/${escapedHref}" rel="nofollow noreferrer noopener">testing${escapedPackageName}</a>`; expect(createLink(href, innerText)).toBe(result); }); diff --git a/spec/lib/gitlab/background_migration/backfill_ci_namespace_mirrors_spec.rb b/spec/lib/gitlab/background_migration/backfill_ci_namespace_mirrors_spec.rb index 8980a26932b..7d128644300 100644 --- a/spec/lib/gitlab/background_migration/backfill_ci_namespace_mirrors_spec.rb +++ b/spec/lib/gitlab/background_migration/backfill_ci_namespace_mirrors_spec.rb @@ -2,7 +2,8 @@ require 'spec_helper' -RSpec.describe Gitlab::BackgroundMigration::BackfillCiNamespaceMirrors, :migration, schema: 20211208122200 do +RSpec.describe Gitlab::BackgroundMigration::BackfillCiNamespaceMirrors, :migration, + :suppress_gitlab_schemas_validate_connection, schema: 20211208122200 do let(:namespaces) { table(:namespaces) } let(:ci_namespace_mirrors) { table(:ci_namespace_mirrors) } diff --git a/spec/lib/gitlab/background_migration/backfill_ci_project_mirrors_spec.rb b/spec/lib/gitlab/background_migration/backfill_ci_project_mirrors_spec.rb index 4eec83879e3..16470409339 100644 --- a/spec/lib/gitlab/background_migration/backfill_ci_project_mirrors_spec.rb +++ b/spec/lib/gitlab/background_migration/backfill_ci_project_mirrors_spec.rb @@ -2,7 +2,8 @@ require 'spec_helper' -RSpec.describe Gitlab::BackgroundMigration::BackfillCiProjectMirrors, :migration, schema: 20211208122201 do +RSpec.describe Gitlab::BackgroundMigration::BackfillCiProjectMirrors, :migration, + :suppress_gitlab_schemas_validate_connection, schema: 20211208122201 do let(:namespaces) { table(:namespaces) } let(:projects) { table(:projects) } let(:ci_project_mirrors) { table(:ci_project_mirrors) } diff --git a/spec/lib/gitlab/background_migration/backfill_ci_queuing_tables_spec.rb b/spec/lib/gitlab/background_migration/backfill_ci_queuing_tables_spec.rb index 1aac5970a77..aaf8c124a83 100644 --- a/spec/lib/gitlab/background_migration/backfill_ci_queuing_tables_spec.rb +++ b/spec/lib/gitlab/background_migration/backfill_ci_queuing_tables_spec.rb @@ -2,7 +2,8 @@ require 'spec_helper' -RSpec.describe Gitlab::BackgroundMigration::BackfillCiQueuingTables, :migration, schema: 20220208115439 do +RSpec.describe Gitlab::BackgroundMigration::BackfillCiQueuingTables, :migration, + :suppress_gitlab_schemas_validate_connection, schema: 20220208115439 do let(:namespaces) { table(:namespaces) } let(:projects) { table(:projects) } let(:ci_cd_settings) { table(:project_ci_cd_settings) } diff --git a/spec/lib/gitlab/background_migration/backfill_projects_with_coverage_spec.rb b/spec/lib/gitlab/background_migration/backfill_projects_with_coverage_spec.rb index 49056154744..4a65ecf8c75 100644 --- a/spec/lib/gitlab/background_migration/backfill_projects_with_coverage_spec.rb +++ b/spec/lib/gitlab/background_migration/backfill_projects_with_coverage_spec.rb @@ -2,7 +2,8 @@ require 'spec_helper' -RSpec.describe Gitlab::BackgroundMigration::BackfillProjectsWithCoverage, schema: 20210818185845 do +RSpec.describe Gitlab::BackgroundMigration::BackfillProjectsWithCoverage, + :suppress_gitlab_schemas_validate_connection, schema: 20210818185845 do let(:projects) { table(:projects) } let(:project_ci_feature_usages) { table(:project_ci_feature_usages) } let(:ci_pipelines) { table(:ci_pipelines) } diff --git a/spec/lib/gitlab/background_migration/copy_ci_builds_columns_to_security_scans_spec.rb b/spec/lib/gitlab/background_migration/copy_ci_builds_columns_to_security_scans_spec.rb index db822f36c21..2b082ca7379 100644 --- a/spec/lib/gitlab/background_migration/copy_ci_builds_columns_to_security_scans_spec.rb +++ b/spec/lib/gitlab/background_migration/copy_ci_builds_columns_to_security_scans_spec.rb @@ -2,7 +2,8 @@ require 'spec_helper' -RSpec.describe Gitlab::BackgroundMigration::CopyCiBuildsColumnsToSecurityScans, schema: 20210728174349 do +RSpec.describe Gitlab::BackgroundMigration::CopyCiBuildsColumnsToSecurityScans, + :suppress_gitlab_schemas_validate_connection, schema: 20210728174349 do let(:migration) { described_class.new } let_it_be(:namespaces) { table(:namespaces) } diff --git a/spec/lib/gitlab/background_migration/drop_invalid_security_findings_spec.rb b/spec/lib/gitlab/background_migration/drop_invalid_security_findings_spec.rb index 7cc64889fc8..5fdd8683d06 100644 --- a/spec/lib/gitlab/background_migration/drop_invalid_security_findings_spec.rb +++ b/spec/lib/gitlab/background_migration/drop_invalid_security_findings_spec.rb @@ -1,7 +1,8 @@ # frozen_string_literal: true require 'spec_helper' -RSpec.describe Gitlab::BackgroundMigration::DropInvalidSecurityFindings, schema: 20211108211434 do +RSpec.describe Gitlab::BackgroundMigration::DropInvalidSecurityFindings, :suppress_gitlab_schemas_validate_connection, + schema: 20211108211434 do let(:namespace) { table(:namespaces).create!(name: 'user', path: 'user', type: Namespaces::UserNamespace.sti_name) } let(:project) { table(:projects).create!(namespace_id: namespace.id) } diff --git a/spec/lib/gitlab/background_migration/extract_project_topics_into_separate_table_spec.rb b/spec/lib/gitlab/background_migration/extract_project_topics_into_separate_table_spec.rb index 65d55f85a98..51a09d50a19 100644 --- a/spec/lib/gitlab/background_migration/extract_project_topics_into_separate_table_spec.rb +++ b/spec/lib/gitlab/background_migration/extract_project_topics_into_separate_table_spec.rb @@ -2,7 +2,8 @@ require 'spec_helper' -RSpec.describe Gitlab::BackgroundMigration::ExtractProjectTopicsIntoSeparateTable, schema: 20210730104800 do +RSpec.describe Gitlab::BackgroundMigration::ExtractProjectTopicsIntoSeparateTable, + :suppress_gitlab_schemas_validate_connection, schema: 20210730104800 do it 'correctly extracts project topics into separate table' do namespaces = table(:namespaces) projects = table(:projects) diff --git a/spec/lib/gitlab/background_migration/migrate_project_taggings_context_from_tags_to_topics_spec.rb b/spec/lib/gitlab/background_migration/migrate_project_taggings_context_from_tags_to_topics_spec.rb index 5e2f32c54be..5495d786a48 100644 --- a/spec/lib/gitlab/background_migration/migrate_project_taggings_context_from_tags_to_topics_spec.rb +++ b/spec/lib/gitlab/background_migration/migrate_project_taggings_context_from_tags_to_topics_spec.rb @@ -2,7 +2,8 @@ require 'spec_helper' -RSpec.describe Gitlab::BackgroundMigration::MigrateProjectTaggingsContextFromTagsToTopics, schema: 20210511095658 do +RSpec.describe Gitlab::BackgroundMigration::MigrateProjectTaggingsContextFromTagsToTopics, + :suppress_gitlab_schemas_validate_connection, schema: 20210511095658 do it 'correctly migrates project taggings context from tags to topics' do taggings = table(:taggings) diff --git a/spec/lib/gitlab/background_migration/nullify_orphan_runner_id_on_ci_builds_spec.rb b/spec/lib/gitlab/background_migration/nullify_orphan_runner_id_on_ci_builds_spec.rb index 11e7c70250e..2f0eef3c399 100644 --- a/spec/lib/gitlab/background_migration/nullify_orphan_runner_id_on_ci_builds_spec.rb +++ b/spec/lib/gitlab/background_migration/nullify_orphan_runner_id_on_ci_builds_spec.rb @@ -2,7 +2,8 @@ require 'spec_helper' -RSpec.describe Gitlab::BackgroundMigration::NullifyOrphanRunnerIdOnCiBuilds, migration: :gitlab_ci, schema: 20220223112304 do +RSpec.describe Gitlab::BackgroundMigration::NullifyOrphanRunnerIdOnCiBuilds, + :suppress_gitlab_schemas_validate_connection, migration: :gitlab_ci, schema: 20220223112304 do let(:namespaces) { table(:namespaces) } let(:projects) { table(:projects) } let(:ci_runners) { table(:ci_runners) } @@ -20,7 +21,9 @@ RSpec.describe Gitlab::BackgroundMigration::NullifyOrphanRunnerIdOnCiBuilds, mig end after do - helpers.add_concurrent_foreign_key(:ci_builds, :ci_runners, column: :runner_id, on_delete: :nullify, validate: false) + helpers.add_concurrent_foreign_key( + :ci_builds, :ci_runners, column: :runner_id, on_delete: :nullify, validate: false + ) end describe '#perform' do 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 8d71b117107..a609227be05 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 @@ -20,7 +20,7 @@ def create_background_migration_job(ids, status) ) end -RSpec.describe Gitlab::BackgroundMigration::RecalculateVulnerabilitiesOccurrencesUuid, schema: 20211124132705 do +RSpec.describe Gitlab::BackgroundMigration::RecalculateVulnerabilitiesOccurrencesUuid, :suppress_gitlab_schemas_validate_connection, schema: 20211124132705 do let(:background_migration_jobs) { table(:background_migration_jobs) } let(:pending_jobs) { background_migration_jobs.where(status: Gitlab::Database::BackgroundMigrationJob.statuses['pending']) } let(:succeeded_jobs) { background_migration_jobs.where(status: Gitlab::Database::BackgroundMigrationJob.statuses['succeeded']) } diff --git a/spec/lib/gitlab/background_migration/remove_all_trace_expiration_dates_spec.rb b/spec/lib/gitlab/background_migration/remove_all_trace_expiration_dates_spec.rb index 8cdcec9621c..eabc012f98b 100644 --- a/spec/lib/gitlab/background_migration/remove_all_trace_expiration_dates_spec.rb +++ b/spec/lib/gitlab/background_migration/remove_all_trace_expiration_dates_spec.rb @@ -2,7 +2,8 @@ require 'spec_helper' -RSpec.describe Gitlab::BackgroundMigration::RemoveAllTraceExpirationDates, :migration, schema: 20220131000001 do +RSpec.describe Gitlab::BackgroundMigration::RemoveAllTraceExpirationDates, :migration, + :suppress_gitlab_schemas_validate_connection, schema: 20220131000001 do subject(:perform) { migration.perform(1, 99) } let(:migration) { described_class.new } diff --git a/spec/lib/gitlab/background_migration/remove_occurrence_pipelines_and_duplicate_vulnerabilities_findings_spec.rb b/spec/lib/gitlab/background_migration/remove_occurrence_pipelines_and_duplicate_vulnerabilities_findings_spec.rb index 07cff32304e..33ad74fbee8 100644 --- a/spec/lib/gitlab/background_migration/remove_occurrence_pipelines_and_duplicate_vulnerabilities_findings_spec.rb +++ b/spec/lib/gitlab/background_migration/remove_occurrence_pipelines_and_duplicate_vulnerabilities_findings_spec.rb @@ -1,7 +1,8 @@ # frozen_string_literal: true require 'spec_helper' -RSpec.describe Gitlab::BackgroundMigration::RemoveOccurrencePipelinesAndDuplicateVulnerabilitiesFindings, :migration, schema: 20220326161803 do +RSpec.describe Gitlab::BackgroundMigration::RemoveOccurrencePipelinesAndDuplicateVulnerabilitiesFindings, :migration, + :suppress_gitlab_schemas_validate_connection, schema: 20220326161803 do let(:namespace) { table(:namespaces).create!(name: 'user', path: 'user') } let(:users) { table(:users) } let(:user) { create_user! } diff --git a/spec/lib/gitlab/ci/parsers/sbom/cyclonedx_spec.rb b/spec/lib/gitlab/ci/parsers/sbom/cyclonedx_spec.rb new file mode 100644 index 00000000000..59a7a03acea --- /dev/null +++ b/spec/lib/gitlab/ci/parsers/sbom/cyclonedx_spec.rb @@ -0,0 +1,90 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::Ci::Parsers::Sbom::Cyclonedx do + let(:report) { instance_double('Gitlab::Ci::Reports::Sbom::Report') } + let(:raw_report_data) { report_data.to_json } + + let(:base_report_data) do + { + 'bomFormat' => 'CycloneDX', + 'specVersion' => '1.4', + 'version' => 1 + } + end + + subject(:parse!) { described_class.new(raw_report_data, report).parse! } + + context 'when report JSON is invalid' do + let(:raw_report_data) { '{ ' } + + it 'handles errors and adds them to the report' do + expect(report).to receive(:add_error).with(a_string_including("Report JSON is invalid:")) + + expect { parse! }.not_to raise_error + end + end + + context 'when report uses an unsupported spec version' do + let(:report_data) { base_report_data.merge({ 'specVersion' => '1.3' }) } + + it 'reports unsupported version as an error' do + expect(report).to receive(:add_error).with("Unsupported CycloneDX spec version. Must be one of: 1.4") + + parse! + end + end + + context 'when cyclonedx report has no components' do + let(:report_data) { base_report_data } + + it 'skips component processing' do + expect(report).not_to receive(:add_component) + + parse! + end + end + + context 'when report has components' do + let(:report_data) { base_report_data.merge({ 'components' => components }) } + let(:components) do + [ + { + "name" => "activesupport", + "version" => "5.1.4", + "purl" => "pkg:gem/activesupport@5.1.4", + "type" => "library", + "bom-ref" => "pkg:gem/activesupport@5.1.4" + }, + { + "name" => "byebug", + "version" => "10.0.0", + "purl" => "pkg:gem/byebug@10.0.0", + "type" => "library", + "bom-ref" => "pkg:gem/byebug@10.0.0" + }, + { + "name" => "minimal-component", + "type" => "library" + }, + { + # Should be skipped + "name" => "unrecognized-type", + "type" => "unknown" + } + ] + end + + it 'adds each component, ignoring unused attributes' do + expect(report).to receive(:add_component) + .with({ "name" => "activesupport", "version" => "5.1.4", "type" => "library" }) + expect(report).to receive(:add_component) + .with({ "name" => "byebug", "version" => "10.0.0", "type" => "library" }) + expect(report).to receive(:add_component) + .with({ "name" => "minimal-component", "type" => "library" }) + + parse! + end + end +end diff --git a/spec/lib/gitlab/ci/reports/sbom/component_spec.rb b/spec/lib/gitlab/ci/reports/sbom/component_spec.rb new file mode 100644 index 00000000000..672117c311f --- /dev/null +++ b/spec/lib/gitlab/ci/reports/sbom/component_spec.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::Ci::Reports::Sbom::Component do + let(:attributes) do + { + 'type' => 'library', + 'name' => 'component-name', + 'version' => 'v0.0.1' + } + end + + subject { described_class.new(attributes) } + + it 'has correct attributes' do + expect(subject).to have_attributes( + component_type: 'library', + name: 'component-name', + version: 'v0.0.1' + ) + end +end diff --git a/spec/lib/gitlab/ci/reports/sbom/report_spec.rb b/spec/lib/gitlab/ci/reports/sbom/report_spec.rb new file mode 100644 index 00000000000..fe1025d16bf --- /dev/null +++ b/spec/lib/gitlab/ci/reports/sbom/report_spec.rb @@ -0,0 +1,65 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::Ci::Reports::Sbom::Report do + subject(:report) { described_class.new } + + describe '#add_error' do + it 'appends errors to a list' do + report.add_error('error1') + report.add_error('error2') + + expect(report.errors).to match_array(%w[error1 error2]) + end + end + + describe '#add_source' do + let_it_be(:sources) do + [ + { + 'type' => :dependency_file, + 'data' => { + 'input_file' => { 'name' => 'package-lock.json' }, + 'package_manager' => { 'name' => 'npm' }, + 'language' => { 'name' => 'JavaScript' } + }, + 'fingerprint' => '4ee1623c8f3ddd152b3c1fc340b3ece3cbcf807efa2726307ea34e7d6d36a6c1' + }, + { + 'type' => :dependency_file, + 'data' => { + 'input_file' => { 'name' => 'go.sum' }, + 'package_manager' => { 'name' => 'go' }, + 'language' => { 'name' => 'Go' } + }, + 'fingerprint' => 'e78eee13d87248d5b7e3df21de67365a4996b3a547e033b8e8b180b24c300fd8' + } + ] + end + + it 'stores each source with the given attributes' do + sources.each { |source| report.add_source(source) } + + expect(report.sources.size).to eq(2) + expect(report.sources).to all(be_a(Gitlab::Ci::Reports::Sbom::Source)) + end + end + + describe '#add_component' do + let_it_be(:components) do + [ + { 'type' => 'library', 'name' => 'component1', 'version' => 'v0.0.1' }, + { 'type' => 'library', 'name' => 'component2', 'version' => 'v0.0.2' }, + { 'type' => 'library', 'name' => 'component2' } + ] + end + + it 'appends components to a list' do + components.each { |component| report.add_component(component) } + + expect(report.components.size).to eq(3) + expect(report.components).to all(be_a(Gitlab::Ci::Reports::Sbom::Component)) + end + end +end diff --git a/spec/lib/gitlab/ci/reports/sbom/source_spec.rb b/spec/lib/gitlab/ci/reports/sbom/source_spec.rb new file mode 100644 index 00000000000..f84ed85b651 --- /dev/null +++ b/spec/lib/gitlab/ci/reports/sbom/source_spec.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::Ci::Reports::Sbom::Source do + let(:attributes) do + { + 'type' => :dependency_file, + 'data' => { + 'input_file' => { 'name' => 'package-lock.json' }, + 'package_manager' => { 'name' => 'npm' }, + 'language' => { 'name' => 'JavaScript' } + }, + 'fingerprint' => '4ee1623c8f3ddd152b3c1fc340b3ece3cbcf807efa2726307ea34e7d6d36a6c1' + } + end + + subject { described_class.new(**attributes) } + + it 'has correct attributes' do + expect(subject).to have_attributes( + source_type: :dependency_file, + data: { + 'input_file' => { 'name' => 'package-lock.json' }, + 'package_manager' => { 'name' => 'npm' }, + 'language' => { 'name' => 'JavaScript' } + }, + fingerprint: '4ee1623c8f3ddd152b3c1fc340b3ece3cbcf807efa2726307ea34e7d6d36a6c1' + ) + end +end diff --git a/spec/lib/gitlab/database/bulk_update_spec.rb b/spec/lib/gitlab/database/bulk_update_spec.rb index 08b4d50f83b..fa519cffd6b 100644 --- a/spec/lib/gitlab/database/bulk_update_spec.rb +++ b/spec/lib/gitlab/database/bulk_update_spec.rb @@ -91,7 +91,8 @@ RSpec.describe Gitlab::Database::BulkUpdate do .to eq(['MR a', 'Issue a', 'Issue b']) end - context 'validates prepared_statements support', :reestablished_active_record_base do + context 'validates prepared_statements support', :reestablished_active_record_base, + :suppress_gitlab_schemas_validate_connection do using RSpec::Parameterized::TableSyntax where(:prepared_statements) do diff --git a/spec/lib/gitlab/database/load_balancing/load_balancer_spec.rb b/spec/lib/gitlab/database/load_balancing/load_balancer_spec.rb index 34eb64997c1..90b588750dd 100644 --- a/spec/lib/gitlab/database/load_balancing/load_balancer_spec.rb +++ b/spec/lib/gitlab/database/load_balancing/load_balancer_spec.rb @@ -358,7 +358,11 @@ RSpec.describe Gitlab::Database::LoadBalancing::LoadBalancer, :request_store do end it 'returns true for deeply wrapped/nested errors' do - top = twice_wrapped_exception(ActionView::Template::Error, ActiveRecord::StatementInvalid, ActiveRecord::ConnectionNotEstablished) + top = twice_wrapped_exception( + ActionView::Template::Error, + ActiveRecord::StatementInvalid, + ActiveRecord::ConnectionNotEstablished + ) expect(lb.connection_error?(top)).to eq(true) end diff --git a/spec/lib/gitlab/database/load_balancing_spec.rb b/spec/lib/gitlab/database/load_balancing_spec.rb index f320fe0276f..76dfaa74ae6 100644 --- a/spec/lib/gitlab/database/load_balancing_spec.rb +++ b/spec/lib/gitlab/database/load_balancing_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -RSpec.describe Gitlab::Database::LoadBalancing do +RSpec.describe Gitlab::Database::LoadBalancing, :suppress_gitlab_schemas_validate_connection do describe '.base_models' do it 'returns the models to apply load balancing to' do models = described_class.base_models diff --git a/spec/lib/gitlab/database/migration_helpers/restrict_gitlab_schema_spec.rb b/spec/lib/gitlab/database/migration_helpers/restrict_gitlab_schema_spec.rb index 1009ec354c3..e43cfe0814e 100644 --- a/spec/lib/gitlab/database/migration_helpers/restrict_gitlab_schema_spec.rb +++ b/spec/lib/gitlab/database/migration_helpers/restrict_gitlab_schema_spec.rb @@ -5,6 +5,13 @@ require 'spec_helper' RSpec.describe Gitlab::Database::MigrationHelpers::RestrictGitlabSchema, query_analyzers: false, stub_feature_flags: false do let(:schema_class) { Class.new(Gitlab::Database::Migration[1.0]).include(described_class) } + # We keep only the GitlabSchemasValidateConnection analyzer running + around do |example| + Gitlab::Database::QueryAnalyzers::GitlabSchemasValidateConnection.with_suppressed(false) do + example.run + end + end + describe '#restrict_gitlab_migration' do it 'invalid schema raises exception' do expect { schema_class.restrict_gitlab_migration gitlab_schema: :gitlab_non_exisiting } diff --git a/spec/lib/gitlab/database/migration_helpers_spec.rb b/spec/lib/gitlab/database/migration_helpers_spec.rb index c289277d6be..091e8dcd8b4 100644 --- a/spec/lib/gitlab/database/migration_helpers_spec.rb +++ b/spec/lib/gitlab/database/migration_helpers_spec.rb @@ -15,7 +15,7 @@ RSpec.describe Gitlab::Database::MigrationHelpers do end describe 'overridden dynamic model helpers' do - let(:test_table) { '__test_batching_table' } + let(:test_table) { '_test_batching_table' } before do model.connection.execute(<<~SQL) diff --git a/spec/lib/gitlab/database/query_analyzers/gitlab_schemas_validate_connection_spec.rb b/spec/lib/gitlab/database/query_analyzers/gitlab_schemas_validate_connection_spec.rb index 5e8afc0102e..ddf5793049d 100644 --- a/spec/lib/gitlab/database/query_analyzers/gitlab_schemas_validate_connection_spec.rb +++ b/spec/lib/gitlab/database/query_analyzers/gitlab_schemas_validate_connection_spec.rb @@ -5,6 +5,13 @@ require 'spec_helper' RSpec.describe Gitlab::Database::QueryAnalyzers::GitlabSchemasValidateConnection, query_analyzers: false do let(:analyzer) { described_class } + # We keep only the GitlabSchemasValidateConnection analyzer running + around do |example| + Gitlab::Database::QueryAnalyzers::GitlabSchemasValidateConnection.with_suppressed(false) do + example.run + end + end + context 'properly observes all queries', :request_store do using RSpec::Parameterized::TableSyntax @@ -61,6 +68,24 @@ RSpec.describe Gitlab::Database::QueryAnalyzers::GitlabSchemasValidateConnection end end + context "when analyzer is enabled for tests", :query_analyzers do + before do + skip_if_multiple_databases_not_setup + end + + it "throws an error when trying to access a table that belongs to the gitlab_main schema from the ci database" do + expect do + Ci::ApplicationRecord.connection.execute("select * from users limit 1") + end.to raise_error(Gitlab::Database::QueryAnalyzers::GitlabSchemasValidateConnection::CrossSchemaAccessError) + end + + it "throws an error when trying to access a table that belongs to the gitlab_ci schema from the main database" do + expect do + ApplicationRecord.connection.execute("select * from ci_builds limit 1") + end.to raise_error(Gitlab::Database::QueryAnalyzers::GitlabSchemasValidateConnection::CrossSchemaAccessError) + end + end + def process_sql(model, sql) Gitlab::Database::QueryAnalyzer.instance.within([analyzer]) do # Skip load balancer and retrieve connection assigned to model diff --git a/spec/lib/gitlab/usage/service_ping_report_spec.rb b/spec/lib/gitlab/usage/service_ping_report_spec.rb index 1e8f9db4dea..7a37a31b195 100644 --- a/spec/lib/gitlab/usage/service_ping_report_spec.rb +++ b/spec/lib/gitlab/usage/service_ping_report_spec.rb @@ -111,8 +111,12 @@ RSpec.describe Gitlab::Usage::ServicePingReport, :use_clean_rails_memory_store_c # Because test cases are run inside a transaction, if any query raise and error all queries that follows # it are automatically canceled by PostgreSQL, to avoid that problem, and to provide exhaustive information # about every metric, queries are wrapped explicitly in sub transactions. - ApplicationRecord.transaction do - ApplicationRecord.connection.execute(query)&.first&.values&.first + table = PgQuery.parse(query).tables.first + gitlab_schema = Gitlab::Database::GitlabSchema.tables_to_schema[table] + base_model = gitlab_schema == :gitlab_main ? ApplicationRecord : Ci::ApplicationRecord + + base_model.transaction do + base_model.connection.execute(query)&.first&.values&.first end rescue ActiveRecord::StatementInvalid => e e.message diff --git a/spec/lib/marginalia_spec.rb b/spec/lib/marginalia_spec.rb index 693b7bd45c9..3c8dea547a9 100644 --- a/spec/lib/marginalia_spec.rb +++ b/spec/lib/marginalia_spec.rb @@ -11,6 +11,11 @@ RSpec.describe 'Marginalia spec' do render body: nil end + def first_ci_pipeline + Ci::Pipeline.first + render body: nil + end + private [:auth_user, :current_user, :set_experimentation_subject_id_cookie, :signed_in?].each do |method| @@ -36,7 +41,7 @@ RSpec.describe 'Marginalia spec' do describe 'For rails web requests' do let(:correlation_id) { SecureRandom.uuid } - let(:recorded) { ActiveRecord::QueryRecorder.new { make_request(correlation_id) } } + let(:recorded) { ActiveRecord::QueryRecorder.new { make_request(correlation_id, :first_user) } } let(:component_map) do { @@ -54,10 +59,11 @@ RSpec.describe 'Marginalia spec' do end context 'when using CI database' do + let(:recorded) { ActiveRecord::QueryRecorder.new { make_request(correlation_id, :first_ci_pipeline) } } let(:component_map) do { "application" => "test", - "endpoint_id" => "MarginaliaTestController#first_user", + "endpoint_id" => "MarginaliaTestController#first_ci_pipeline", "correlation_id" => correlation_id, "db_config_name" => 'ci' } @@ -65,8 +71,6 @@ RSpec.describe 'Marginalia spec' do before do skip_if_multiple_databases_not_setup - - allow(User).to receive(:connection) { Ci::ApplicationRecord.connection } end it 'generates a query that includes the component and value' do @@ -140,11 +144,11 @@ RSpec.describe 'Marginalia spec' do end end - def make_request(correlation_id) + def make_request(correlation_id, action_name) request_env = Rack::MockRequest.env_for('/') ::Labkit::Correlation::CorrelationId.use_id(correlation_id) do - MarginaliaTestController.action(:first_user).call(request_env) + MarginaliaTestController.action(action_name).call(request_env) end end end diff --git a/spec/migrations/2021061716138_cascade_delete_freeze_periods_spec.rb b/spec/migrations/2021061716138_cascade_delete_freeze_periods_spec.rb index d35184e78a8..8dfeacc4774 100644 --- a/spec/migrations/2021061716138_cascade_delete_freeze_periods_spec.rb +++ b/spec/migrations/2021061716138_cascade_delete_freeze_periods_spec.rb @@ -4,7 +4,7 @@ require 'spec_helper' require_migration! -RSpec.describe CascadeDeleteFreezePeriods do +RSpec.describe CascadeDeleteFreezePeriods, :suppress_gitlab_schemas_validate_connection do let(:namespace) { table(:namespaces).create!(name: 'deploy_freeze', path: 'deploy_freeze') } let(:project) { table(:projects).create!(id: 1, namespace_id: namespace.id) } let(:freeze_periods) { table(:ci_freeze_periods) } diff --git a/spec/migrations/20210818185845_backfill_projects_with_coverage_spec.rb b/spec/migrations/20210818185845_backfill_projects_with_coverage_spec.rb index 29f554a003b..13a6aa5413e 100644 --- a/spec/migrations/20210818185845_backfill_projects_with_coverage_spec.rb +++ b/spec/migrations/20210818185845_backfill_projects_with_coverage_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' require_migration! -RSpec.describe BackfillProjectsWithCoverage do +RSpec.describe BackfillProjectsWithCoverage, :suppress_gitlab_schemas_validate_connection do let(:projects) { table(:projects) } let(:ci_pipelines) { table(:ci_pipelines) } let(:ci_daily_build_group_report_results) { table(:ci_daily_build_group_report_results) } diff --git a/spec/migrations/20211116111644_schedule_remove_occurrence_pipelines_and_duplicate_vulnerabilities_findings_spec.rb b/spec/migrations/20211116111644_schedule_remove_occurrence_pipelines_and_duplicate_vulnerabilities_findings_spec.rb index c1d96f50dc8..cf6a033b4b8 100644 --- a/spec/migrations/20211116111644_schedule_remove_occurrence_pipelines_and_duplicate_vulnerabilities_findings_spec.rb +++ b/spec/migrations/20211116111644_schedule_remove_occurrence_pipelines_and_duplicate_vulnerabilities_findings_spec.rb @@ -3,7 +3,8 @@ require 'spec_helper' require_migration! -RSpec.describe ScheduleRemoveOccurrencePipelinesAndDuplicateVulnerabilitiesFindings, :migration do +RSpec.describe ScheduleRemoveOccurrencePipelinesAndDuplicateVulnerabilitiesFindings, + :suppress_gitlab_schemas_validate_connection, :migration 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(:users) { table(:users) } diff --git a/spec/migrations/20220124130028_dedup_runner_projects_spec.rb b/spec/migrations/20220124130028_dedup_runner_projects_spec.rb index 127f4798f33..3429ccc4df1 100644 --- a/spec/migrations/20220124130028_dedup_runner_projects_spec.rb +++ b/spec/migrations/20220124130028_dedup_runner_projects_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' require_migration! -RSpec.describe DedupRunnerProjects, :migration, schema: 20220120085655 do +RSpec.describe DedupRunnerProjects, :migration, :suppress_gitlab_schemas_validate_connection, schema: 20220120085655 do let(:namespaces) { table(:namespaces) } let(:projects) { table(:projects) } let(:runners) { table(:ci_runners) } diff --git a/spec/migrations/20220128155251_remove_dangling_running_builds_spec.rb b/spec/migrations/20220128155251_remove_dangling_running_builds_spec.rb index a48464befdf..3478305bbad 100644 --- a/spec/migrations/20220128155251_remove_dangling_running_builds_spec.rb +++ b/spec/migrations/20220128155251_remove_dangling_running_builds_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' require_migration!('remove_dangling_running_builds') -RSpec.describe RemoveDanglingRunningBuilds do +RSpec.describe RemoveDanglingRunningBuilds, :suppress_gitlab_schemas_validate_connection do let(:namespace) { table(:namespaces).create!(name: 'user', path: 'user') } let(:project) { table(:projects).create!(namespace_id: namespace.id) } let(:runner) { table(:ci_runners).create!(runner_type: 1) } diff --git a/spec/migrations/associate_existing_dast_builds_with_variables_spec.rb b/spec/migrations/associate_existing_dast_builds_with_variables_spec.rb index 74429e498df..d57a243bade 100644 --- a/spec/migrations/associate_existing_dast_builds_with_variables_spec.rb +++ b/spec/migrations/associate_existing_dast_builds_with_variables_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' require_migration! -RSpec.describe AssociateExistingDastBuildsWithVariables do +RSpec.describe AssociateExistingDastBuildsWithVariables, :suppress_gitlab_schemas_validate_connection do subject(:migration) { described_class.new } let_it_be(:namespaces_table) { table(:namespaces) } diff --git a/spec/migrations/clean_up_pending_builds_table_spec.rb b/spec/migrations/clean_up_pending_builds_table_spec.rb index 9c8d4413337..17e62e1b486 100644 --- a/spec/migrations/clean_up_pending_builds_table_spec.rb +++ b/spec/migrations/clean_up_pending_builds_table_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' require_migration! -RSpec.describe CleanUpPendingBuildsTable do +RSpec.describe CleanUpPendingBuildsTable, :suppress_gitlab_schemas_validate_connection do let(:namespaces) { table(:namespaces) } let(:projects) { table(:projects) } let(:queue) { table(:ci_pending_builds) } diff --git a/spec/migrations/delete_security_findings_without_uuid_spec.rb b/spec/migrations/delete_security_findings_without_uuid_spec.rb index b32ea89f8aa..bfd89f1aa82 100644 --- a/spec/migrations/delete_security_findings_without_uuid_spec.rb +++ b/spec/migrations/delete_security_findings_without_uuid_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' require_migration! -RSpec.describe DeleteSecurityFindingsWithoutUuid do +RSpec.describe DeleteSecurityFindingsWithoutUuid, :suppress_gitlab_schemas_validate_connection do let(:users) { table(:users) } let(:namespaces) { table(:namespaces) } let(:projects) { table(:projects) } diff --git a/spec/migrations/disable_job_token_scope_when_unused_spec.rb b/spec/migrations/disable_job_token_scope_when_unused_spec.rb index d969c98aa0f..ef78698caf7 100644 --- a/spec/migrations/disable_job_token_scope_when_unused_spec.rb +++ b/spec/migrations/disable_job_token_scope_when_unused_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' require_migration! -RSpec.describe DisableJobTokenScopeWhenUnused do +RSpec.describe DisableJobTokenScopeWhenUnused, :suppress_gitlab_schemas_validate_connection do let(:ci_cd_settings) { table(:project_ci_cd_settings) } let(:links) { table(:ci_job_token_project_scope_links) } let(:namespaces) { table(:namespaces) } diff --git a/spec/migrations/migrate_protected_attribute_to_pending_builds_spec.rb b/spec/migrations/migrate_protected_attribute_to_pending_builds_spec.rb index 2108adcc973..01805a9eb79 100644 --- a/spec/migrations/migrate_protected_attribute_to_pending_builds_spec.rb +++ b/spec/migrations/migrate_protected_attribute_to_pending_builds_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' require_migration! -RSpec.describe MigrateProtectedAttributeToPendingBuilds do +RSpec.describe MigrateProtectedAttributeToPendingBuilds, :suppress_gitlab_schemas_validate_connection do let(:namespaces) { table(:namespaces) } let(:projects) { table(:projects) } let(:queue) { table(:ci_pending_builds) } diff --git a/spec/migrations/re_schedule_latest_pipeline_id_population_with_all_security_related_artifact_types_spec.rb b/spec/migrations/re_schedule_latest_pipeline_id_population_with_all_security_related_artifact_types_spec.rb index 8a9b993b869..45a2772adda 100644 --- a/spec/migrations/re_schedule_latest_pipeline_id_population_with_all_security_related_artifact_types_spec.rb +++ b/spec/migrations/re_schedule_latest_pipeline_id_population_with_all_security_related_artifact_types_spec.rb @@ -3,7 +3,8 @@ require 'spec_helper' require_migration! -RSpec.describe ReScheduleLatestPipelineIdPopulationWithAllSecurityRelatedArtifactTypes do +RSpec.describe ReScheduleLatestPipelineIdPopulationWithAllSecurityRelatedArtifactTypes, + :suppress_gitlab_schemas_validate_connection do let(:namespaces) { table(:namespaces) } let(:pipelines) { table(:ci_pipelines) } let(:projects) { table(:projects) } diff --git a/spec/migrations/schedule_copy_ci_builds_columns_to_security_scans2_spec.rb b/spec/migrations/schedule_copy_ci_builds_columns_to_security_scans2_spec.rb index 012c7d065fc..f5d461cfce2 100644 --- a/spec/migrations/schedule_copy_ci_builds_columns_to_security_scans2_spec.rb +++ b/spec/migrations/schedule_copy_ci_builds_columns_to_security_scans2_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' require_migration! -RSpec.describe ScheduleCopyCiBuildsColumnsToSecurityScans2 do +RSpec.describe ScheduleCopyCiBuildsColumnsToSecurityScans2, :suppress_gitlab_schemas_validate_connection do let_it_be(:namespaces) { table(:namespaces) } let_it_be(:projects) { table(:projects) } let_it_be(:ci_pipelines) { table(:ci_pipelines) } diff --git a/spec/migrations/schedule_populate_status_column_of_security_scans_spec.rb b/spec/migrations/schedule_populate_status_column_of_security_scans_spec.rb index 601935db8db..36b5df31661 100644 --- a/spec/migrations/schedule_populate_status_column_of_security_scans_spec.rb +++ b/spec/migrations/schedule_populate_status_column_of_security_scans_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' require_migration! -RSpec.describe SchedulePopulateStatusColumnOfSecurityScans do +RSpec.describe SchedulePopulateStatusColumnOfSecurityScans, :suppress_gitlab_schemas_validate_connection do before do allow(Gitlab).to receive(:ee?).and_return(ee?) stub_const("#{described_class.name}::BATCH_SIZE", 1) diff --git a/spec/migrations/start_backfill_ci_queuing_tables_spec.rb b/spec/migrations/start_backfill_ci_queuing_tables_spec.rb index a1e4179efb6..08fd244089f 100644 --- a/spec/migrations/start_backfill_ci_queuing_tables_spec.rb +++ b/spec/migrations/start_backfill_ci_queuing_tables_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' require_migration! -RSpec.describe StartBackfillCiQueuingTables do +RSpec.describe StartBackfillCiQueuingTables, :suppress_gitlab_schemas_validate_connection do let(:namespaces) { table(:namespaces) } let(:projects) { table(:projects) } let(:builds) { table(:ci_builds) } diff --git a/spec/support/database/gitlab_schemas_validate_connection.rb b/spec/support/database/gitlab_schemas_validate_connection.rb new file mode 100644 index 00000000000..118c6ea5001 --- /dev/null +++ b/spec/support/database/gitlab_schemas_validate_connection.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +RSpec.configure do |config| + def with_gitlab_schemas_validate_connection_prevented + Gitlab::Database::QueryAnalyzers::GitlabSchemasValidateConnection.with_suppressed do + yield + end + end + + config.around(:each, :suppress_gitlab_schemas_validate_connection) do |example| + with_gitlab_schemas_validate_connection_prevented(&example) + end + + config.around(:each, query_analyzers: false) do |example| + with_gitlab_schemas_validate_connection_prevented(&example) + end +end diff --git a/spec/support/shared_examples/workers/batched_background_migration_worker_shared_examples.rb b/spec/support/shared_examples/workers/batched_background_migration_worker_shared_examples.rb index 1da21633504..07af193900a 100644 --- a/spec/support/shared_examples/workers/batched_background_migration_worker_shared_examples.rb +++ b/spec/support/shared_examples/workers/batched_background_migration_worker_shared_examples.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -RSpec.shared_examples 'it runs batched background migration jobs' do |tracking_database| +RSpec.shared_examples 'it runs batched background migration jobs' do |tracking_database, table_name| include ExclusiveLeaseHelpers describe 'defining the job attributes' do @@ -136,8 +136,10 @@ RSpec.shared_examples 'it runs batched background migration jobs' do |tracking_d let(:job_interval) { 5.minutes } let(:lease_timeout) { 15.minutes } let(:lease_key) { described_class.name.demodulize.underscore } - let(:migration) { build(:batched_background_migration, :active, interval: job_interval) } let(:interval_variance) { described_class::INTERVAL_VARIANCE } + let(:migration) do + build(:batched_background_migration, :active, interval: job_interval, table_name: table_name) + end before do allow(Gitlab::Database::BackgroundMigration::BatchedMigration).to receive(:active_migration) @@ -249,7 +251,7 @@ RSpec.shared_examples 'it runs batched background migration jobs' do |tracking_d create( :batched_background_migration, :active, - table_name: table_name, + table_name: new_table_name, column_name: :id, max_value: migration_records, batch_size: batch_size, @@ -261,14 +263,14 @@ RSpec.shared_examples 'it runs batched background migration jobs' do |tracking_d end let(:base_model) { Gitlab::Database.database_base_models[tracking_database] } - let(:table_name) { 'example_data' } + let(:new_table_name) { '_test_example_data' } let(:batch_size) { 5 } let(:sub_batch_size) { 2 } let(:number_of_batches) { 10 } let(:migration_records) { batch_size * number_of_batches } let(:connection) { Gitlab::Database.database_base_models[tracking_database].connection } - let(:example_data) { define_batchable_model(table_name, connection: connection) } + let(:example_data) { define_batchable_model(new_table_name, connection: connection) } around do |example| Gitlab::Database::SharedModel.using_connection(connection) do @@ -283,16 +285,16 @@ RSpec.shared_examples 'it runs batched background migration jobs' do |tracking_d # - one record beyond the migration's range # - one record that doesn't match the migration job's batch condition connection.execute(<<~SQL) - CREATE TABLE #{table_name} ( + CREATE TABLE #{new_table_name} ( id integer primary key, some_column integer, status smallint); - INSERT INTO #{table_name} (id, some_column, status) + INSERT INTO #{new_table_name} (id, some_column, status) SELECT generate_series, generate_series, 1 FROM generate_series(1, #{migration_records + 1}); - UPDATE #{table_name} + UPDATE #{new_table_name} SET status = 0 WHERE some_column = #{migration_records - 5}; SQL diff --git a/spec/support_specs/database/prevent_cross_joins_spec.rb b/spec/support_specs/database/prevent_cross_joins_spec.rb index efeabd15b58..5a80d0c0203 100644 --- a/spec/support_specs/database/prevent_cross_joins_spec.rb +++ b/spec/support_specs/database/prevent_cross_joins_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -RSpec.describe Database::PreventCrossJoins do +RSpec.describe Database::PreventCrossJoins, :suppress_gitlab_schemas_validate_connection do context 'when running in a default scope' do context 'when only non-CI tables are used' do it 'does not raise exception' do diff --git a/spec/tasks/gitlab/background_migrations_rake_spec.rb b/spec/tasks/gitlab/background_migrations_rake_spec.rb index bbd33f71e60..d8ce00a65e6 100644 --- a/spec/tasks/gitlab/background_migrations_rake_spec.rb +++ b/spec/tasks/gitlab/background_migrations_rake_spec.rb @@ -2,7 +2,7 @@ require 'rake_helper' -RSpec.describe 'gitlab:background_migrations namespace rake tasks' do +RSpec.describe 'gitlab:background_migrations namespace rake tasks', :suppress_gitlab_schemas_validate_connection do before do Rake.application.rake_require 'tasks/gitlab/background_migrations' end @@ -155,7 +155,7 @@ RSpec.describe 'gitlab:background_migrations namespace rake tasks' do context 'with multiple databases' do subject(:status_task) { run_rake_task('gitlab:background_migrations:status') } - let(:base_models) { { 'main' => main_model, 'ci' => ci_model } } + let(:base_models) { { main: main_model, ci: ci_model } } let(:main_model) { double(:model, connection: connection) } let(:ci_model) { double(:model, connection: connection) } 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 29b80176ef8..b03e964ce87 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 @@ -2,7 +2,8 @@ require 'rake_helper' -RSpec.describe 'gitlab:db:decomposition:rollback:bump_ci_sequences', :silence_stdout do +RSpec.describe 'gitlab:db:decomposition:rollback:bump_ci_sequences', :silence_stdout, + :suppress_gitlab_schemas_validate_connection 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 341e1a3f56c..3eb6d35f3f9 100644 --- a/spec/tasks/gitlab/db/lock_writes_rake_spec.rb +++ b/spec/tasks/gitlab/db/lock_writes_rake_spec.rb @@ -2,7 +2,8 @@ require 'rake_helper' -RSpec.describe 'gitlab:db:lock_writes', :silence_stdout, :reestablished_active_record_base do +RSpec.describe 'gitlab:db:lock_writes', :silence_stdout, :reestablished_active_record_base, + :suppress_gitlab_schemas_validate_connection do before :all do Rake.application.rake_require 'active_record/railties/databases' Rake.application.rake_require 'tasks/seed_fu' diff --git a/spec/tasks/gitlab/db/validate_config_rake_spec.rb b/spec/tasks/gitlab/db/validate_config_rake_spec.rb index 03d7504e8b1..ad15c7f0d1c 100644 --- a/spec/tasks/gitlab/db/validate_config_rake_spec.rb +++ b/spec/tasks/gitlab/db/validate_config_rake_spec.rb @@ -2,7 +2,7 @@ require 'rake_helper' -RSpec.describe 'gitlab:db:validate_config', :silence_stdout do +RSpec.describe 'gitlab:db:validate_config', :silence_stdout, :suppress_gitlab_schemas_validate_connection do # We don't need to delete this data since it only modifies `ar_internal_metadata` # which would not be cleaned either by `DbCleaner` self.use_transactional_tests = false diff --git a/spec/workers/database/batched_background_migration/ci_database_worker_spec.rb b/spec/workers/database/batched_background_migration/ci_database_worker_spec.rb index 2b4a42060d9..dfe7a266be2 100644 --- a/spec/workers/database/batched_background_migration/ci_database_worker_spec.rb +++ b/spec/workers/database/batched_background_migration/ci_database_worker_spec.rb @@ -3,5 +3,5 @@ require 'spec_helper' RSpec.describe Database::BatchedBackgroundMigration::CiDatabaseWorker, :clean_gitlab_redis_shared_state do - it_behaves_like 'it runs batched background migration jobs', :ci + it_behaves_like 'it runs batched background migration jobs', :ci, :ci_builds end diff --git a/spec/workers/database/batched_background_migration_worker_spec.rb b/spec/workers/database/batched_background_migration_worker_spec.rb index a6c7db60abe..e57bd7581c2 100644 --- a/spec/workers/database/batched_background_migration_worker_spec.rb +++ b/spec/workers/database/batched_background_migration_worker_spec.rb @@ -3,5 +3,5 @@ require 'spec_helper' RSpec.describe Database::BatchedBackgroundMigrationWorker do - it_behaves_like 'it runs batched background migration jobs', :main + it_behaves_like 'it runs batched background migration jobs', :main, :events end |