summaryrefslogtreecommitdiff
path: root/spec/lib/system_check/orphans/namespace_check_spec.rb
blob: f7491e40438ef72753c4f94a9804eaacdaf324c2 (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
# frozen_string_literal: true

require 'spec_helper'
require 'rake_helper'

describe SystemCheck::Orphans::NamespaceCheck do
  let(:storages) { Gitlab.config.repositories.storages.reject { |key, _| key.eql? 'broken' } }

  before do
    allow(Gitlab.config.repositories).to receive(:storages).and_return(storages)
    allow(subject).to receive(:fetch_disk_namespaces).and_return(disk_namespaces)
    silence_output
  end

  describe '#multi_check' do
    context 'all orphans' do
      let(:disk_namespaces) { %w(/repos/orphan1 /repos/orphan2 repos/@hashed) }

      it 'prints list of all orphaned namespaces except @hashed' do
        expect_list_of_orphans(%w(orphan1 orphan2))

        subject.multi_check
      end
    end

    context 'few orphans with existing namespace' do
      let!(:first_level) { create(:group, path: 'my-namespace') }
      let(:disk_namespaces) { %w(/repos/orphan1 /repos/orphan2 /repos/my-namespace /repos/@hashed) }

      it 'prints list of orphaned namespaces' do
        expect_list_of_orphans(%w(orphan1 orphan2))

        subject.multi_check
      end
    end

    context 'few orphans with existing namespace and parents with same name as orphans' do
      let!(:first_level) { create(:group, path: 'my-namespace') }
      let!(:second_level) { create(:group, path: 'second-level', parent: first_level) }
      let(:disk_namespaces) { %w(/repos/orphan1 /repos/orphan2 /repos/my-namespace /repos/second-level /repos/@hashed) }

      it 'prints list of orphaned namespaces ignoring parents with same namespace as orphans' do
        expect_list_of_orphans(%w(orphan1 orphan2 second-level))

        subject.multi_check
      end
    end

    context 'no orphans' do
      let(:disk_namespaces) { %w(@hashed) }

      it 'prints an empty list ignoring @hashed' do
        expect_list_of_orphans([])

        subject.multi_check
      end
    end
  end

  def expect_list_of_orphans(orphans)
    expect(subject).to receive(:print_orphans).with(orphans, 'default')
  end
end