BigW Consortium Gitlab

redis_spec.rb 5.78 KB
Newer Older
1 2
require 'spec_helper'

3
describe Gitlab::Redis do
4
  include StubENV
5

Douwe Maan committed
6 7 8 9 10 11 12 13 14 15
  let(:config) { 'config/resque.yml' }

  before(:each) do
    stub_env('GITLAB_REDIS_CONFIG_FILE', Rails.root.join(config).to_s)
    clear_raw_config
  end

  after(:each) do
    clear_raw_config
  end
16

17 18 19
  describe '.params' do
    subject { described_class.params }

20 21 22 23 24 25 26 27
    it 'withstands mutation' do
      params1 = described_class.params
      params2 = described_class.params
      params1[:foo] = :bar

      expect(params2).not_to have_key(:foo)
    end

28
    context 'when url contains unix socket reference' do
Douwe Maan committed
29 30
      let(:config_old) { 'spec/fixtures/config/redis_old_format_socket.yml' }
      let(:config_new) { 'spec/fixtures/config/redis_new_format_socket.yml' }
31 32

      context 'with old format' do
Douwe Maan committed
33
        let(:config) { config_old }
34

Douwe Maan committed
35
        it 'returns path key instead' do
36
          is_expected.to include(path: '/path/to/old/redis.sock')
37 38 39 40 41
          is_expected.not_to have_key(:url)
        end
      end

      context 'with new format' do
Douwe Maan committed
42
        let(:config) { config_new }
43

Douwe Maan committed
44
        it 'returns path key instead' do
45 46 47 48 49 50 51
          is_expected.to include(path: '/path/to/redis.sock')
          is_expected.not_to have_key(:url)
        end
      end
    end

    context 'when url is host based' do
Douwe Maan committed
52 53
      let(:config_old) { 'spec/fixtures/config/redis_old_format_host.yml' }
      let(:config_new) { 'spec/fixtures/config/redis_new_format_host.yml' }
54 55

      context 'with old format' do
Douwe Maan committed
56
        let(:config) { config_old }
57

Douwe Maan committed
58
        it 'returns hash with host, port, db, and password' do
59 60 61 62 63 64
          is_expected.to include(host: 'localhost', password: 'mypassword', port: 6379, db: 99)
          is_expected.not_to have_key(:url)
        end
      end

      context 'with new format' do
Douwe Maan committed
65
        let(:config) { config_new }
66

Douwe Maan committed
67
        it 'returns hash with host, port, db, and password' do
68
          is_expected.to include(host: 'localhost', password: 'mynewpassword', port: 6379, db: 99)
69 70 71 72 73 74
          is_expected.not_to have_key(:url)
        end
      end
    end
  end

75 76 77 78 79 80 81 82
  describe '.url' do
    it 'withstands mutation' do
      url1 = described_class.url
      url2 = described_class.url
      url1 << 'foobar'

      expect(url2).not_to end_with('foobar')
    end
83 84

    context 'when yml file with env variable' do
Douwe Maan committed
85
      let(:config) { 'spec/fixtures/config/redis_config_with_env.yml' }
86 87 88 89 90 91 92 93 94

      before  do
        stub_env('TEST_GITLAB_REDIS_URL', 'redis://redishost:6379')
      end

      it 'reads redis url from env variable' do
        expect(described_class.url).to eq 'redis://redishost:6379'
      end
    end
95 96 97 98
  end

  describe '._raw_config' do
    subject { described_class._raw_config }
Douwe Maan committed
99
    let(:config) { '/var/empty/doesnotexist' }
100 101 102 103 104 105 106 107 108 109

    it 'should be frozen' do
      expect(subject).to be_frozen
    end

    it 'returns false when the file does not exist' do
      expect(subject).to eq(false)
    end
  end

110
  describe '.with' do
111 112 113 114 115 116 117
    before do
      clear_pool
    end

    after do
      clear_pool
    end
118 119

    context 'when running not on sidekiq workers' do
120 121 122
      before do
        allow(Sidekiq).to receive(:server?).and_return(false)
      end
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142

      it 'instantiates a connection pool with size 5' do
        expect(ConnectionPool).to receive(:new).with(size: 5).and_call_original

        described_class.with { |_redis| true }
      end
    end

    context 'when running on sidekiq workers' do
      before do
        allow(Sidekiq).to receive(:server?).and_return(true)
        allow(Sidekiq).to receive(:options).and_return({ concurrency: 18 })
      end

      it 'instantiates a connection pool with a size based on the concurrency of the worker' do
        expect(ConnectionPool).to receive(:new).with(size: 18 + 5).and_call_original

        described_class.with { |_redis| true }
      end
    end
143 144 145 146 147 148
  end

  describe '#sentinels' do
    subject { described_class.new(Rails.env).sentinels }

    context 'when sentinels are defined' do
Douwe Maan committed
149
      let(:config) { 'spec/fixtures/config/redis_new_format_host.yml' }
150 151 152 153 154 155 156 157

      it 'returns an array of hashes with host and port keys' do
        is_expected.to include(host: 'localhost', port: 26380)
        is_expected.to include(host: 'slave2', port: 26381)
      end
    end

    context 'when sentinels are not defined' do
Douwe Maan committed
158
      let(:config) { 'spec/fixtures/config/redis_old_format_host.yml' }
159 160 161 162 163 164 165 166 167 168 169

      it 'returns nil' do
        is_expected.to be_nil
      end
    end
  end

  describe '#sentinels?' do
    subject { described_class.new(Rails.env).sentinels? }

    context 'when sentinels are defined' do
Douwe Maan committed
170
      let(:config) { 'spec/fixtures/config/redis_new_format_host.yml' }
171 172 173 174 175 176 177

      it 'returns true' do
        is_expected.to be_truthy
      end
    end

    context 'when sentinels are not defined' do
Douwe Maan committed
178
      let(:config) { 'spec/fixtures/config/redis_old_format_host.yml' }
179 180 181 182 183

      it 'returns false' do
        is_expected.to be_falsey
      end
    end
184 185
  end

186
  describe '#raw_config_hash' do
187
    it 'returns default redis url when no config file is present' do
188
      expect(subject).to receive(:fetch_config) { false }
189

190
      expect(subject.send(:raw_config_hash)).to eq(url: Gitlab::Redis::DEFAULT_REDIS_URL)
191 192 193
    end

    it 'returns old-style single url config in a hash' do
194 195
      expect(subject).to receive(:fetch_config) { 'redis://myredis:6379' }
      expect(subject.send(:raw_config_hash)).to eq(url: 'redis://myredis:6379')
196 197 198
    end
  end

199
  describe '#fetch_config' do
200
    it 'returns false when no config file is present' do
201
      allow(described_class).to receive(:_raw_config) { false }
202

203
      expect(subject.send(:fetch_config)).to be_falsey
204 205
    end
  end
206 207 208 209 210 211

  def clear_raw_config
    described_class.remove_instance_variable(:@_raw_config)
  rescue NameError
    # raised if @_raw_config was not set; ignore
  end
212 213 214 215 216 217

  def clear_pool
    described_class.remove_instance_variable(:@pool)
  rescue NameError
    # raised if @pool was not set; ignore
  end
218
end