summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/backup/repository_spec.rb
blob: 3af69daa5857bdd0059eaefdfca2d66fffbe7181 (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
require 'spec_helper'

describe Backup::Repository do
  let(:progress) { StringIO.new }
  let!(:project) { create(:empty_project) }

  before do
    allow(progress).to receive(:puts)
    allow(progress).to receive(:print)

    allow_any_instance_of(String).to receive(:color) do |string, _color|
      string
    end

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

  describe '#dump' do
    describe 'repo failure' do
      before do
        allow_any_instance_of(Repository).to receive(:empty_repo?).and_raise(Rugged::OdbError)
        allow(Gitlab::Popen).to receive(:popen).and_return(['normal output', 0])
      end

      it 'does not raise error' do
        expect { described_class.new.dump }.not_to raise_error
      end

      it 'shows the appropriate error' do
        described_class.new.dump

        expect(progress).to have_received(:puts).with("Ignoring repository error and continuing backing up project: #{project.full_path} - Rugged::OdbError")
      end
    end

    describe 'command failure' do
      before do
        allow_any_instance_of(Repository).to receive(:empty_repo?).and_return(false)
        allow(Gitlab::Popen).to receive(:popen).and_return(['error', 1])
      end

      it 'shows the appropriate error' do
        described_class.new.dump

        expect(progress).to have_received(:puts).with("Ignoring error on #{project.full_path} - error")
      end
    end
  end

  describe '#restore' do
    describe 'command failure' do
      before do
        allow(Gitlab::Popen).to receive(:popen).and_return(['error', 1])
      end

      it 'shows the appropriate error' do
        described_class.new.restore

        expect(progress).to have_received(:puts).with("Ignoring error on #{project.full_path} - error")
      end
    end
  end

  describe '#empty_repo?' do
    context 'for a wiki' do
      let(:wiki) { create(:project_wiki) }

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

        before do
          wiki.repository.exists? # initial cache
        end

        context '`repository.exists?` is incorrectly cached as false' do
          before do
            repo = wiki.repository
            repo.send(:cache).expire(:exists?)
            repo.send(:cache).fetch(:exists?) { false }
            repo.send(:instance_variable_set, :@exists, false)
          end

          it 'returns false, regardless of bad cache value' do
            expect(described_class.new.send(:empty_repo?, wiki)).to be_falsey
          end
        end

        context '`repository.exists?` is correctly cached as true' do
          it 'returns false' do
            expect(described_class.new.send(:empty_repo?, wiki)).to be_falsey
          end
        end
      end

      context 'wiki repo does not have content' do
        context '`repository.exists?` is incorrectly cached as true' do
          before do
            repo = wiki.repository
            repo.send(:cache).expire(:exists?)
            repo.send(:cache).fetch(:exists?) { true }
            repo.send(:instance_variable_set, :@exists, true)
          end

          it 'returns true, regardless of bad cache value' do
            expect(described_class.new.send(:empty_repo?, wiki)).to be_truthy
          end
        end

        context '`repository.exists?` is correctly cached as false' do
          it 'returns true' do
            expect(described_class.new.send(:empty_repo?, wiki)).to be_truthy
          end
        end
      end
    end
  end
end