PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/spec/lib/gitlab/background_migration/backfill_project_repositories_spec.rb

https://gitlab.com/18dit020/gitlab
Ruby | 106 lines | 77 code | 26 blank | 3 comment | 0 complexity | cb992a84e926d6dbbbcb2af1ba56d379 MD5 | raw file
  1. # frozen_string_literal: true
  2. require 'spec_helper'
  3. # rubocop:disable RSpec/FactoriesInMigrationSpecs
  4. RSpec.describe Gitlab::BackgroundMigration::BackfillProjectRepositories do
  5. let(:group) { create(:group, name: 'foo', path: 'foo') }
  6. describe described_class::ShardFinder do
  7. let(:shard) { create(:shard, name: 'default') }
  8. describe '#find_shard_id' do
  9. it 'creates a new shard when it does not exist yet' do
  10. expect { subject.find_shard_id('other') }.to change(Shard, :count).by(1)
  11. end
  12. it 'returns the shard when it exists' do
  13. other_shard = create(:shard, name: 'other')
  14. shard_id = subject.find_shard_id('other')
  15. expect(shard_id).to eq(other_shard.id)
  16. end
  17. it 'only queries the database once to retrieve shards' do
  18. subject.find_shard_id('default')
  19. expect { subject.find_shard_id('default') }.not_to exceed_query_limit(0)
  20. end
  21. end
  22. end
  23. describe described_class::Project do
  24. let!(:project_hashed_storage_1) { create(:project, name: 'foo', path: 'foo', namespace: group, storage_version: 1) }
  25. let!(:project_hashed_storage_2) { create(:project, name: 'bar', path: 'bar', namespace: group, storage_version: 2) }
  26. let!(:project_legacy_storage_3) { create(:project, name: 'baz', path: 'baz', namespace: group, storage_version: 0) }
  27. let!(:project_legacy_storage_4) { create(:project, name: 'zoo', path: 'zoo', namespace: group, storage_version: nil) }
  28. let!(:project_legacy_storage_5) { create(:project, name: 'test', path: 'test', namespace: group, storage_version: nil) }
  29. describe '.on_hashed_storage' do
  30. it 'finds projects with repository on hashed storage' do
  31. projects = described_class.on_hashed_storage.pluck(:id)
  32. expect(projects).to match_array([project_hashed_storage_1.id, project_hashed_storage_2.id])
  33. end
  34. end
  35. describe '.on_legacy_storage' do
  36. it 'finds projects with repository on legacy storage' do
  37. projects = described_class.on_legacy_storage.pluck(:id)
  38. expect(projects).to match_array([project_legacy_storage_3.id, project_legacy_storage_4.id, project_legacy_storage_5.id])
  39. end
  40. end
  41. describe '.without_project_repository' do
  42. it 'finds projects which do not have a projects_repositories entry' do
  43. create(:project_repository, project: project_hashed_storage_1)
  44. create(:project_repository, project: project_legacy_storage_3)
  45. projects = described_class.without_project_repository.pluck(:id)
  46. expect(projects).to contain_exactly(project_hashed_storage_2.id, project_legacy_storage_4.id, project_legacy_storage_5.id)
  47. end
  48. end
  49. describe '#disk_path' do
  50. context 'for projects on hashed storage' do
  51. it 'returns the correct disk_path' do
  52. project = described_class.find(project_hashed_storage_1.id)
  53. expect(project.disk_path).to eq(project_hashed_storage_1.disk_path)
  54. end
  55. end
  56. context 'for projects on legacy storage' do
  57. it 'returns the correct disk_path' do
  58. project = described_class.find(project_legacy_storage_3.id)
  59. expect(project.disk_path).to eq(project_legacy_storage_3.disk_path)
  60. end
  61. it 'returns the correct disk_path using the route entry' do
  62. project_legacy_storage_5.route.update!(path: 'zoo/new-test')
  63. project = described_class.find(project_legacy_storage_5.id)
  64. expect(project.disk_path).to eq('zoo/new-test')
  65. end
  66. it 'raises OrphanedNamespaceError when any parent namespace does not exist' do
  67. subgroup = create(:group, parent: group)
  68. project_orphaned_namespace = create(:project, name: 'baz', path: 'baz', namespace: subgroup, storage_version: nil)
  69. subgroup.update_column(:parent_id, non_existing_record_id)
  70. project = described_class.find(project_orphaned_namespace.id)
  71. project.route.destroy!
  72. subgroup.route.destroy!
  73. expect { project.reload.disk_path }
  74. .to raise_error(Gitlab::BackgroundMigration::BackfillProjectRepositories::OrphanedNamespaceError)
  75. end
  76. end
  77. end
  78. end
  79. end
  80. # rubocop:enable RSpec/FactoriesInMigrationSpecs