summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/background_migration/backfill_project_fullpath_in_repo_config_spec.rb
blob: 4714712f7338c3b86299c632a0bdce51112d1e7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# frozen_string_literal: true

require 'spec_helper'

describe Gitlab::BackgroundMigration::BackfillProjectFullpathInRepoConfig, :migration, schema: 20181010133639 do
  let(:namespaces) { table(:namespaces) }
  let(:projects) { table(:projects) }
  let(:group) { namespaces.create!(name: 'foo', path: 'foo') }
  let(:subgroup) { namespaces.create!(name: 'bar', path: 'bar', parent_id: group.id) }

  describe described_class::Storage::Hashed do
    let(:project) { double(id: 555) }

    subject(:project_storage) { described_class.new(project) }

    it 'has the correct disk_path' do
      expect(project_storage.disk_path).to eq('@hashed/91/a7/91a73fd806ab2c005c13b4dc19130a884e909dea3f72d46e30266fe1a1f588d8')
    end
  end

  describe described_class::Storage::LegacyProject do
    let(:project) { double(full_path: 'this/is/the/full/path') }

    subject(:project_storage) { described_class.new(project) }

    it 'has the correct disk_path' do
      expect(project_storage.disk_path).to eq('this/is/the/full/path')
    end
  end

  describe described_class::Project do
    let(:project_record) { projects.create!(namespace_id: subgroup.id, name: 'baz', path: 'baz') }

    subject(:project) { described_class.find(project_record.id) }

    describe '#full_path' do
      it 'returns path containing all parent namespaces' do
        expect(project.full_path).to eq('foo/bar/baz')
      end

      it 'raises OrphanedNamespaceError when any parent namespace does not exist' do
        subgroup.update_attribute(:parent_id, namespaces.maximum(:id).succ)

        expect { project.full_path }.to raise_error(Gitlab::BackgroundMigration::BackfillProjectFullpathInRepoConfig::OrphanedNamespaceError)
      end
    end
  end

  describe described_class::Up do
    describe '#perform' do
      subject(:migrate) { described_class.new.perform(projects.minimum(:id), projects.maximum(:id)) }

      it 'asks the gitaly client to set config' do
        projects.create!(namespace_id: subgroup.id, name: 'baz', path: 'baz')
        projects.create!(namespace_id: subgroup.id, name: 'buzz', path: 'buzz', storage_version: 1)

        expect_next_instance_of(Gitlab::GitalyClient::RepositoryService) do |repository_service|
          allow(repository_service).to receive(:cleanup)
          expect(repository_service).to receive(:set_config).with('gitlab.fullpath' => 'foo/bar/baz')
        end

        expect_next_instance_of(Gitlab::GitalyClient::RepositoryService) do |repository_service|
          allow(repository_service).to receive(:cleanup)
          expect(repository_service).to receive(:set_config).with('gitlab.fullpath' => 'foo/bar/buzz')
        end

        migrate
      end
    end
  end

  describe described_class::Down do
    describe '#perform' do
      subject(:migrate) { described_class.new.perform(projects.minimum(:id), projects.maximum(:id)) }

      it 'asks the gitaly client to set config' do
        projects.create!(namespace_id: subgroup.id, name: 'baz', path: 'baz')

        expect_next_instance_of(Gitlab::GitalyClient::RepositoryService) do |repository_service|
          allow(repository_service).to receive(:cleanup)
          expect(repository_service).to receive(:delete_config).with(['gitlab.fullpath'])
        end

        migrate
      end
    end
  end
end