debian-mirror-gitlab/spec/support/redis/redis_shared_examples.rb

408 lines
12 KiB
Ruby
Raw Normal View History

2019-10-12 21:52:04 +05:30
# frozen_string_literal: true
2017-09-10 17:25:29 +05:30
RSpec.shared_examples "redis_shared_examples" do
2017-08-17 22:00:37 +05:30
include StubENV
2016-09-13 17:45:13 +05:30
2019-03-02 22:35:43 +05:30
let(:test_redis_url) { "redis://redishost:#{redis_port}"}
2021-09-04 01:27:46 +05:30
let(:config_file_name) { instance_specific_config_file }
let(:config_old_format_socket) { "spec/fixtures/config/redis_old_format_socket.yml" }
let(:config_new_format_socket) { "spec/fixtures/config/redis_new_format_socket.yml" }
let(:old_socket_path) {"/path/to/old/redis.sock" }
let(:new_socket_path) {"/path/to/redis.sock" }
let(:config_old_format_host) { "spec/fixtures/config/redis_old_format_host.yml" }
let(:config_new_format_host) { "spec/fixtures/config/redis_new_format_host.yml" }
let(:redis_port) { 6379 }
let(:redis_database) { 99 }
let(:sentinel_port) { 26379 }
let(:config_with_environment_variable_inside) { "spec/fixtures/config/redis_config_with_env.yml"}
let(:config_env_variable_url) {"TEST_GITLAB_REDIS_URL"}
let(:rails_root) { Dir.mktmpdir('redis_shared_examples') }
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
before do
2021-09-04 01:27:46 +05:30
allow(described_class).to receive(:config_file_name).and_return(Rails.root.join(config_file_name).to_s)
2022-01-26 12:08:38 +05:30
redis_clear_raw_config!(described_class)
2017-08-17 22:00:37 +05:30
end
2018-03-17 18:26:18 +05:30
after do
2022-01-26 12:08:38 +05:30
redis_clear_raw_config!(described_class)
2017-08-17 22:00:37 +05:30
end
2016-09-13 17:45:13 +05:30
2021-09-04 01:27:46 +05:30
describe '.config_file_name' do
subject { described_class.config_file_name }
before do
# Undo top-level stub of config_file_name because we are testing that method now.
allow(described_class).to receive(:config_file_name).and_call_original
allow(described_class).to receive(:rails_root).and_return(rails_root)
FileUtils.mkdir_p(File.join(rails_root, 'config'))
end
after do
FileUtils.rm_rf(rails_root)
end
context 'when there is no config file anywhere' do
it { expect(subject).to be_nil }
context 'but resque.yml exists' do
before do
FileUtils.touch(File.join(rails_root, 'config', 'resque.yml'))
end
it { expect(subject).to eq("#{rails_root}/config/resque.yml") }
it 'returns a path that exists' do
expect(File.file?(subject)).to eq(true)
end
context 'and there is a global env override' do
before do
stub_env('GITLAB_REDIS_CONFIG_FILE', 'global override')
end
it { expect(subject).to eq('global override') }
context 'and there is an instance specific config file' do
before do
FileUtils.touch(File.join(rails_root, instance_specific_config_file))
end
it { expect(subject).to eq("#{rails_root}/#{instance_specific_config_file}") }
it 'returns a path that exists' do
expect(File.file?(subject)).to eq(true)
end
context 'and there is a specific env override' do
before do
stub_env(environment_config_file_name, 'instance specific override')
end
it { expect(subject).to eq('instance specific override') }
end
end
end
end
end
end
2021-12-11 22:18:48 +05:30
describe '.store' do
let(:rails_env) { 'development' }
subject { described_class.new(rails_env).store }
shared_examples 'redis store' do
2022-01-26 12:08:38 +05:30
let(:redis_store) { ::Redis::Store }
let(:redis_store_to_s) { "Redis Client connected to #{host} against DB #{redis_database}" }
2021-12-11 22:18:48 +05:30
it 'instantiates Redis::Store' do
2022-01-26 12:08:38 +05:30
is_expected.to be_a(redis_store)
expect(subject.to_s).to eq(redis_store_to_s)
2021-12-11 22:18:48 +05:30
end
context 'with the namespace' do
let(:namespace) { 'namespace_name' }
2022-01-26 12:08:38 +05:30
let(:redis_store_to_s) { "Redis Client connected to #{host} against DB #{redis_database} with namespace #{namespace}" }
2021-12-11 22:18:48 +05:30
subject { described_class.new(rails_env).store(namespace: namespace) }
it "uses specified namespace" do
2022-01-26 12:08:38 +05:30
expect(subject.to_s).to eq(redis_store_to_s)
2021-12-11 22:18:48 +05:30
end
end
end
context 'with old format' do
it_behaves_like 'redis store' do
let(:config_file_name) { config_old_format_host }
let(:host) { "localhost:#{redis_port}" }
end
end
context 'with new format' do
it_behaves_like 'redis store' do
let(:config_file_name) { config_new_format_host }
let(:host) { "development-host:#{redis_port}" }
end
end
end
2016-09-13 17:45:13 +05:30
describe '.params' do
2021-09-04 01:27:46 +05:30
subject { described_class.new(rails_env).params }
let(:rails_env) { 'development' }
let(:config_file_name) { config_old_format_socket }
2016-09-13 17:45:13 +05:30
2016-11-03 12:29:30 +05:30
it 'withstands mutation' do
params1 = described_class.params
params2 = described_class.params
params1[:foo] = :bar
expect(params2).not_to have_key(:foo)
end
2016-09-13 17:45:13 +05:30
context 'when url contains unix socket reference' do
context 'with old format' do
2017-09-10 17:25:29 +05:30
let(:config_file_name) { config_old_format_socket }
2016-09-13 17:45:13 +05:30
2017-08-17 22:00:37 +05:30
it 'returns path key instead' do
2017-09-10 17:25:29 +05:30
is_expected.to include(path: old_socket_path)
2016-09-13 17:45:13 +05:30
is_expected.not_to have_key(:url)
end
end
context 'with new format' do
2017-09-10 17:25:29 +05:30
let(:config_file_name) { config_new_format_socket }
2016-09-13 17:45:13 +05:30
2017-08-17 22:00:37 +05:30
it 'returns path key instead' do
2017-09-10 17:25:29 +05:30
is_expected.to include(path: new_socket_path)
2016-09-13 17:45:13 +05:30
is_expected.not_to have_key(:url)
end
end
end
context 'when url is host based' do
context 'with old format' do
2017-09-10 17:25:29 +05:30
let(:config_file_name) { config_old_format_host }
2016-09-13 17:45:13 +05:30
2017-08-17 22:00:37 +05:30
it 'returns hash with host, port, db, and password' do
2017-09-10 17:25:29 +05:30
is_expected.to include(host: 'localhost', password: 'mypassword', port: redis_port, db: redis_database)
2016-09-13 17:45:13 +05:30
is_expected.not_to have_key(:url)
end
end
context 'with new format' do
2017-09-10 17:25:29 +05:30
let(:config_file_name) { config_new_format_host }
2016-09-13 17:45:13 +05:30
2021-09-04 01:27:46 +05:30
where(:rails_env, :host) do
[
%w[development development-host],
%w[test test-host],
%w[production production-host]
]
end
with_them do
it 'returns hash with host, port, db, and password' do
is_expected.to include(host: host, password: 'mynewpassword', port: redis_port, db: redis_database)
is_expected.not_to have_key(:url)
end
2016-09-13 17:45:13 +05:30
end
end
end
end
2016-11-03 12:29:30 +05:30
describe '.url' do
2021-09-04 01:27:46 +05:30
let(:config_file_name) { config_old_format_socket }
2018-11-08 19:23:39 +05:30
it 'withstands mutation' do
url1 = described_class.url
url2 = described_class.url
url1 << 'foobar' unless url1.frozen?
expect(url2).not_to end_with('foobar')
end
2017-08-17 22:00:37 +05:30
context 'when yml file with env variable' do
2017-09-10 17:25:29 +05:30
let(:config_file_name) { config_with_environment_variable_inside }
2017-08-17 22:00:37 +05:30
2019-03-02 22:35:43 +05:30
before do
2017-09-10 17:25:29 +05:30
stub_env(config_env_variable_url, test_redis_url)
2017-08-17 22:00:37 +05:30
end
it 'reads redis url from env variable' do
2017-09-10 17:25:29 +05:30
expect(described_class.url).to eq test_redis_url
2017-08-17 22:00:37 +05:30
end
end
2016-11-03 12:29:30 +05:30
end
2021-09-04 01:27:46 +05:30
describe '.version' do
it 'returns a version' do
expect(described_class.version).to be_present
end
end
2016-11-03 12:29:30 +05:30
describe '._raw_config' do
subject { described_class._raw_config }
2019-12-21 20:55:43 +05:30
2017-09-10 17:25:29 +05:30
let(:config_file_name) { '/var/empty/doesnotexist' }
2016-11-03 12:29:30 +05:30
2019-07-07 11:18:12 +05:30
it 'is frozen' do
2016-11-03 12:29:30 +05:30
expect(subject).to be_frozen
end
it 'returns false when the file does not exist' do
expect(subject).to eq(false)
end
2017-09-10 17:25:29 +05:30
it "returns false when the filename can't be determined" do
expect(described_class).to receive(:config_file_name).and_return(nil)
expect(subject).to eq(false)
end
2016-11-03 12:29:30 +05:30
end
describe '.with' do
2021-09-04 01:27:46 +05:30
let(:config_file_name) { config_old_format_socket }
2017-09-10 17:25:29 +05:30
before do
clear_pool
end
after do
clear_pool
end
2016-11-03 12:29:30 +05:30
2020-03-13 15:44:24 +05:30
context 'when running on single-threaded runtime' do
2017-09-10 17:25:29 +05:30
before do
2020-03-13 15:44:24 +05:30
allow(Gitlab::Runtime).to receive(:multi_threaded?).and_return(false)
2017-09-10 17:25:29 +05:30
end
2016-11-03 12:29:30 +05:30
it 'instantiates a connection pool with size 5' do
expect(ConnectionPool).to receive(:new).with(size: 5).and_call_original
2017-09-10 17:25:29 +05:30
described_class.with { |_redis_shared_example| true }
2016-11-03 12:29:30 +05:30
end
end
2020-03-13 15:44:24 +05:30
context 'when running on multi-threaded runtime' do
2016-11-03 12:29:30 +05:30
before do
2020-03-13 15:44:24 +05:30
allow(Gitlab::Runtime).to receive(:multi_threaded?).and_return(true)
allow(Gitlab::Runtime).to receive(:max_threads).and_return(18)
2016-11-03 12:29:30 +05:30
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
2017-09-10 17:25:29 +05:30
described_class.with { |_redis_shared_example| true }
2016-11-03 12:29:30 +05:30
end
end
2021-09-04 01:27:46 +05:30
context 'when there is no config at all' do
before do
# Undo top-level stub of config_file_name because we are testing that method now.
allow(described_class).to receive(:config_file_name).and_call_original
allow(described_class).to receive(:rails_root).and_return(rails_root)
end
after do
FileUtils.rm_rf(rails_root)
end
it 'can run an empty block' do
expect { described_class.with { nil } }.not_to raise_error
end
end
2017-08-17 22:00:37 +05:30
end
2021-11-18 22:05:49 +05:30
describe '#db' do
let(:rails_env) { 'development' }
subject { described_class.new(rails_env).db }
context 'with old format' do
let(:config_file_name) { config_old_format_host }
it 'returns the correct db' do
expect(subject).to eq(redis_database)
end
end
context 'with new format' do
let(:config_file_name) { config_new_format_host }
it 'returns the correct db' do
expect(subject).to eq(redis_database)
end
end
end
2017-08-17 22:00:37 +05:30
describe '#sentinels' do
2021-09-04 01:27:46 +05:30
subject { described_class.new(rails_env).sentinels }
let(:rails_env) { 'development' }
2017-08-17 22:00:37 +05:30
context 'when sentinels are defined' do
2017-09-10 17:25:29 +05:30
let(:config_file_name) { config_new_format_host }
2017-08-17 22:00:37 +05:30
2021-09-04 01:27:46 +05:30
where(:rails_env, :hosts) do
[
['development', %w[development-replica1 development-replica2]],
['test', %w[test-replica1 test-replica2]],
['production', %w[production-replica1 production-replica2]]
]
end
with_them do
it 'returns an array of hashes with host and port keys' do
is_expected.to include(host: hosts[0], port: sentinel_port)
is_expected.to include(host: hosts[1], port: sentinel_port)
end
2017-08-17 22:00:37 +05:30
end
end
context 'when sentinels are not defined' do
2017-09-10 17:25:29 +05:30
let(:config_file_name) { config_old_format_host }
2017-08-17 22:00:37 +05:30
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
2017-09-10 17:25:29 +05:30
let(:config_file_name) { config_new_format_host }
2017-08-17 22:00:37 +05:30
it 'returns true' do
is_expected.to be_truthy
end
end
context 'when sentinels are not defined' do
2017-09-10 17:25:29 +05:30
let(:config_file_name) { config_old_format_host }
2017-08-17 22:00:37 +05:30
it 'returns false' do
is_expected.to be_falsey
end
end
2016-11-03 12:29:30 +05:30
end
2016-09-13 17:45:13 +05:30
describe '#raw_config_hash' do
it 'returns old-style single url config in a hash' do
2017-09-10 17:25:29 +05:30
expect(subject).to receive(:fetch_config) { test_redis_url }
expect(subject.send(:raw_config_hash)).to eq(url: test_redis_url)
2016-09-13 17:45:13 +05:30
end
end
describe '#fetch_config' do
it 'returns false when no config file is present' do
2016-11-03 12:29:30 +05:30
allow(described_class).to receive(:_raw_config) { false }
2016-09-13 17:45:13 +05:30
2017-09-10 17:25:29 +05:30
expect(subject.send(:fetch_config)).to eq false
end
it 'returns false when config file is present but has invalid YAML' do
allow(described_class).to receive(:_raw_config) { "# development: true" }
expect(subject.send(:fetch_config)).to eq false
2016-09-13 17:45:13 +05:30
end
2021-11-18 22:05:49 +05:30
it 'has a value for the legacy default URL' do
allow(subject).to receive(:fetch_config) { false }
expect(subject.send(:raw_config_hash)).to include(url: a_string_matching(%r{\Aredis://localhost:638[012]\Z}))
end
2016-09-13 17:45:13 +05:30
end
2016-11-03 12:29:30 +05:30
def clear_pool
described_class.remove_instance_variable(:@pool)
rescue NameError
# raised if @pool was not set; ignore
end
2016-09-13 17:45:13 +05:30
end