repository_spec.rb 3.58 KB
Newer Older
1
2
# frozen_string_literal: true

3
4
5
6
require 'spec_helper'

describe Backup::Repository do
  let(:progress) { StringIO.new }
Zeger-Jan van de Weg's avatar
Zeger-Jan van de Weg committed
7
  let!(:project) { create(:project, :wiki_repo) }
8
  subject { described_class.new(progress) }
9
10
11
12

  before do
    allow(progress).to receive(:puts)
    allow(progress).to receive(:print)
13
    allow(FileUtils).to receive(:mv).and_return(true)
14
15
16
17
18
19
20
21
22
23
24

    allow_any_instance_of(described_class).to receive(:progress).and_return(progress)
  end

  describe '#dump' do
    describe 'repo failure' do
      before do
        allow(Gitlab::Popen).to receive(:popen).and_return(['normal output', 0])
      end

      it 'does not raise error' do
25
        expect { subject.dump }.not_to raise_error
26
27
28
29
30
      end
    end
  end

  describe '#restore' do
31
32
33
    let(:timestamp) { Time.utc(2017, 3, 22) }
    let(:temp_dirs) do
      Gitlab.config.repositories.storages.map do |name, storage|
34
35
36
        Gitlab::GitalyClient::StorageSettings.allow_disk_access do
          File.join(storage.legacy_disk_path, '..', 'repositories.old.' + timestamp.to_i.to_s)
        end
37
38
39
40
41
42
43
44
45
46
47
      end
    end

    around do |example|
      Timecop.freeze(timestamp) { example.run }
    end

    after do
      temp_dirs.each { |path| FileUtils.rm_rf(path) }
    end

48
49
    describe 'command failure' do
      before do
Ahmad Hassan's avatar
Ahmad Hassan committed
50
        allow_any_instance_of(Gitlab::Shell).to receive(:create_repository).and_return(false)
51
52
      end

Nick Thomas's avatar
Nick Thomas committed
53
54
      context 'hashed storage' do
        it 'shows the appropriate error' do
55
          subject.restore
56

Ahmad Hassan's avatar
Ahmad Hassan committed
57
          expect(progress).to have_received(:puts).with("[Failed] restoring #{project.full_path} repository")
Nick Thomas's avatar
Nick Thomas committed
58
59
60
61
62
63
64
        end
      end

      context 'legacy storage' do
        let!(:project) { create(:project, :legacy_storage) }

        it 'shows the appropriate error' do
65
          subject.restore
Nick Thomas's avatar
Nick Thomas committed
66

Ahmad Hassan's avatar
Ahmad Hassan committed
67
          expect(progress).to have_received(:puts).with("[Failed] restoring #{project.full_path} repository")
Nick Thomas's avatar
Nick Thomas committed
68
        end
69
70
      end
    end
71
72
73
74
75
76
77
78
79
80
81
82
83

    context 'restoring object pools' do
      it 'schedules restoring of the pool' do
        pool_repository = create(:pool_repository, :failed)
        pool_repository.delete_object_pool

        subject.restore

        pool_repository.reload
        expect(pool_repository).not_to be_failed
        expect(pool_repository.object_pool.exists?).to be(true)
      end
    end
84
85
  end

86
87
88
89
90
  describe '#prepare_directories', :seed_helper do
    before do
      allow(FileUtils).to receive(:mkdir_p).and_call_original
      allow(FileUtils).to receive(:mv).and_call_original
    end
91

92
93
94
    after(:all) do
      ensure_seeds
    end
95

96
97
98
    it' removes all repositories' do
      # Sanity check: there should be something for us to delete
      expect(list_repositories).to include(File.join(SEED_STORAGE_PATH, TEST_REPO_PATH))
99

100
      subject.prepare_directories
101

102
      expect(list_repositories).to be_empty
103
104
    end

105
106
    def list_repositories
      Dir[File.join(SEED_STORAGE_PATH, '*.git')]
107
108
109
    end
  end

110
111
112
113
114
115
116
  describe '#empty_repo?' do
    context 'for a wiki' do
      let(:wiki) { create(:project_wiki) }

      it 'invalidates the emptiness cache' do
        expect(wiki.repository).to receive(:expire_emptiness_caches).once

117
        subject.send(:empty_repo?, wiki)
118
119
120
121
122
123
      end

      context 'wiki repo has content' do
        let!(:wiki_page) { create(:wiki_page, wiki: wiki) }

        it 'returns true, regardless of bad cache value' do
124
          expect(subject.send(:empty_repo?, wiki)).to be(false)
125
126
127
128
129
        end
      end

      context 'wiki repo does not have content' do
        it 'returns true, regardless of bad cache value' do
130
          expect(subject.send(:empty_repo?, wiki)).to be_truthy
131
132
133
134
135
        end
      end
    end
  end
end