summaryrefslogtreecommitdiff
path: root/spec/lib/gitaly/server_spec.rb
blob: 390855b30ad17f6a4106f6349d9c3a3464d0813e (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# frozen_string_literal: true

require 'spec_helper'

describe Gitaly::Server do
  let(:server) { described_class.new('default') }

  describe '.all' do
    let(:storages) { Gitlab.config.repositories.storages }

    it 'includes all storages' do
      expect(storages.count).to eq(described_class.all.count)
      expect(storages.keys).to eq(described_class.all.map(&:storage))
    end
  end

  subject { described_class.all.first }

  it { is_expected.to respond_to(:server_version) }
  it { is_expected.to respond_to(:git_binary_version) }
  it { is_expected.to respond_to(:up_to_date?) }
  it { is_expected.to respond_to(:address) }
  it { is_expected.to respond_to(:replication_factor) }

  describe 'readable?' do
    context 'when the storage is readable' do
      it 'returns true' do
        expect(server).to be_readable
      end
    end

    context 'when the storage is not readable', :broken_storage do
      it 'returns false' do
        expect(server).not_to be_readable
      end
    end
  end

  describe 'writeable?' do
    context 'when the storage is writeable' do
      it 'returns true' do
        expect(server).to be_writeable
      end
    end

    context 'when the storage is not writeable', :broken_storage do
      it 'returns false' do
        expect(server).not_to be_writeable
      end
    end
  end

  describe "#filesystem_type" do
    subject { server.filesystem_type }

    it { is_expected.to be_present }
  end

  describe 'request memoization' do
    context 'when requesting multiple properties', :request_store do
      it 'uses memoization for the info request' do
        expect do
          subject.server_version
          subject.up_to_date?
        end.to change { Gitlab::GitalyClient.get_request_count }.by(1)
      end
    end
  end

  context "when examining disk statistics for a given server" do
    let(:disk_available) { 42 }
    let(:disk_used) { 42 }
    let(:storage_status) { double('storage_status') }

    before do
      allow(storage_status).to receive(:storage_name).and_return('default')
      allow(storage_status).to receive(:available).and_return(disk_available)
      allow(storage_status).to receive(:used).and_return(disk_used)
      response = double("response")
      allow(response).to receive(:storage_statuses).and_return([storage_status])
      allow_next_instance_of(Gitlab::GitalyClient::ServerService) do |instance|
        allow(instance).to receive(:disk_statistics).and_return(response)
      end
    end

    describe '#disk_available' do
      subject { server.disk_available }

      it { is_expected.to be_present }

      it "returns disk available for the storage of the instantiated server" do
        is_expected.to eq(disk_available)
      end
    end

    describe '#disk_used' do
      subject { server.disk_used }

      it { is_expected.to be_present }

      it "returns disk used for the storage of the instantiated server" do
        is_expected.to eq(disk_used)
      end
    end

    describe '#disk_stats' do
      subject { server.disk_stats }

      it { is_expected.to be_present }

      it "returns the storage of the instantiated server" do
        is_expected.to eq(storage_status)
      end
    end
  end

  describe '#expected_version?' do
    using RSpec::Parameterized::TableSyntax

    where(:expected_version, :server_version, :result) do
      '1.1.1'                                    | '1.1.1'               | true
      '1.1.2'                                    | '1.1.1'               | false
      '1.73.0'                                   | '1.73.0-18-gf756ebe2' | false
      '594c3ea3e0e5540e5915bd1c49713a0381459dd6' | '1.55.6-45-g594c3ea3' | true
      '594c3ea3e0e5540e5915bd1c49713a0381459dd6' | '1.55.6-46-gabc123ff' | false
      '594c3ea3e0e5540e5915bd1c49713a0381459dd6' | '1.55.6'              | false
    end

    with_them do
      it do
        allow(Gitlab::GitalyClient).to receive(:expected_server_version).and_return(expected_version)
        allow(server).to receive(:server_version).and_return(server_version)

        expect(server.expected_version?).to eq(result)
      end
    end
  end

  describe 'replication_factor' do
    context 'when examining for a given server' do
      let(:storage_status) { double('storage_status', storage_name: 'default') }

      before do
        response = double('response', storage_statuses: [storage_status])
        allow_next_instance_of(Gitlab::GitalyClient::ServerService) do |instance|
          allow(instance).to receive(:info).and_return(response)
        end
      end

      it do
        allow(storage_status).to receive(:replication_factor).and_return(2)
        expect(server.replication_factor).to eq(2)
      end
    end
  end
end