2019-07-25 01:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-07-10 23:35:47 -04:00
|
|
|
RSpec.shared_examples "redis_shared_examples" do
|
2016-12-13 14:54:50 -05:00
|
|
|
include StubENV
|
2015-11-27 11:32:32 -05:00
|
|
|
|
2019-01-16 07:09:29 -05:00
|
|
|
let(:test_redis_url) { "redis://redishost:#{redis_port}"}
|
2021-06-01 14:10:04 -04:00
|
|
|
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"}
|
2021-06-09 02:10:29 -04:00
|
|
|
let(:rails_root) { Dir.mktmpdir('redis_shared_examples') }
|
2017-03-15 20:36:20 -04:00
|
|
|
|
2017-08-10 18:31:42 -04:00
|
|
|
before do
|
2021-06-01 14:10:04 -04:00
|
|
|
allow(described_class).to receive(:config_file_name).and_return(Rails.root.join(config_file_name).to_s)
|
2021-12-17 10:13:39 -05:00
|
|
|
redis_clear_raw_config!(described_class)
|
2017-03-15 20:36:20 -04:00
|
|
|
end
|
|
|
|
|
2017-08-10 18:31:42 -04:00
|
|
|
after do
|
2021-12-17 10:13:39 -05:00
|
|
|
redis_clear_raw_config!(described_class)
|
2017-03-15 20:36:20 -04:00
|
|
|
end
|
2016-08-05 21:01:52 -04:00
|
|
|
|
2021-06-01 14:10:04 -04:00
|
|
|
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-11-05 08:10:25 -04:00
|
|
|
describe '.store' do
|
|
|
|
let(:rails_env) { 'development' }
|
|
|
|
|
|
|
|
subject { described_class.new(rails_env).store }
|
|
|
|
|
|
|
|
shared_examples 'redis store' do
|
2021-11-24 13:14:31 -05:00
|
|
|
let(:redis_store) { ::Redis::Store }
|
|
|
|
let(:redis_store_to_s) { "Redis Client connected to #{host} against DB #{redis_database}" }
|
|
|
|
|
2021-11-05 08:10:25 -04:00
|
|
|
it 'instantiates Redis::Store' do
|
2021-11-24 13:14:31 -05:00
|
|
|
is_expected.to be_a(redis_store)
|
|
|
|
|
|
|
|
expect(subject.to_s).to eq(redis_store_to_s)
|
2021-11-05 08:10:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'with the namespace' do
|
|
|
|
let(:namespace) { 'namespace_name' }
|
2021-11-24 13:14:31 -05:00
|
|
|
let(:redis_store_to_s) { "Redis Client connected to #{host} against DB #{redis_database} with namespace #{namespace}" }
|
2021-11-05 08:10:25 -04:00
|
|
|
|
|
|
|
subject { described_class.new(rails_env).store(namespace: namespace) }
|
|
|
|
|
|
|
|
it "uses specified namespace" do
|
2021-11-24 13:14:31 -05:00
|
|
|
expect(subject.to_s).to eq(redis_store_to_s)
|
2021-11-05 08:10:25 -04:00
|
|
|
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
|
|
|
|
|
2015-11-27 11:32:32 -05:00
|
|
|
describe '.params' do
|
2021-06-01 14:10:04 -04:00
|
|
|
subject { described_class.new(rails_env).params }
|
|
|
|
|
|
|
|
let(:rails_env) { 'development' }
|
2021-06-04 08:10:17 -04:00
|
|
|
let(:config_file_name) { config_old_format_socket }
|
2015-11-27 11:32:32 -05:00
|
|
|
|
2016-09-22 05:32:49 -04:00
|
|
|
it 'withstands mutation' do
|
|
|
|
params1 = described_class.params
|
|
|
|
params2 = described_class.params
|
|
|
|
params1[:foo] = :bar
|
|
|
|
|
|
|
|
expect(params2).not_to have_key(:foo)
|
|
|
|
end
|
|
|
|
|
2015-11-27 11:32:32 -05:00
|
|
|
context 'when url contains unix socket reference' do
|
|
|
|
context 'with old format' do
|
2017-07-10 23:35:47 -04:00
|
|
|
let(:config_file_name) { config_old_format_socket }
|
2015-11-27 11:32:32 -05:00
|
|
|
|
2017-03-15 20:36:20 -04:00
|
|
|
it 'returns path key instead' do
|
2017-07-10 23:35:47 -04:00
|
|
|
is_expected.to include(path: old_socket_path)
|
2015-11-27 11:32:32 -05:00
|
|
|
is_expected.not_to have_key(:url)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with new format' do
|
2017-07-10 23:35:47 -04:00
|
|
|
let(:config_file_name) { config_new_format_socket }
|
2015-11-27 11:32:32 -05:00
|
|
|
|
2017-03-15 20:36:20 -04:00
|
|
|
it 'returns path key instead' do
|
2017-07-10 23:35:47 -04:00
|
|
|
is_expected.to include(path: new_socket_path)
|
2015-11-27 11:32:32 -05:00
|
|
|
is_expected.not_to have_key(:url)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when url is host based' do
|
|
|
|
context 'with old format' do
|
2017-07-10 23:35:47 -04:00
|
|
|
let(:config_file_name) { config_old_format_host }
|
2015-11-27 11:32:32 -05:00
|
|
|
|
2017-03-15 20:36:20 -04:00
|
|
|
it 'returns hash with host, port, db, and password' do
|
2017-07-10 23:35:47 -04:00
|
|
|
is_expected.to include(host: 'localhost', password: 'mypassword', port: redis_port, db: redis_database)
|
2015-11-27 11:32:32 -05:00
|
|
|
is_expected.not_to have_key(:url)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with new format' do
|
2017-07-10 23:35:47 -04:00
|
|
|
let(:config_file_name) { config_new_format_host }
|
2015-11-27 11:32:32 -05:00
|
|
|
|
2021-06-01 14:10:04 -04:00
|
|
|
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
|
2015-11-27 11:32:32 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-22 05:32:49 -04:00
|
|
|
describe '.url' do
|
2021-06-04 08:10:17 -04:00
|
|
|
let(:config_file_name) { config_old_format_socket }
|
|
|
|
|
2018-06-21 11:49:15 -04:00
|
|
|
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
|
|
|
|
|
2016-12-13 14:54:50 -05:00
|
|
|
context 'when yml file with env variable' do
|
2017-07-10 23:35:47 -04:00
|
|
|
let(:config_file_name) { config_with_environment_variable_inside }
|
2016-12-13 14:54:50 -05:00
|
|
|
|
2019-01-16 07:09:29 -05:00
|
|
|
before do
|
2017-07-10 23:35:47 -04:00
|
|
|
stub_env(config_env_variable_url, test_redis_url)
|
2016-12-13 14:54:50 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'reads redis url from env variable' do
|
2017-07-10 23:35:47 -04:00
|
|
|
expect(described_class.url).to eq test_redis_url
|
2016-12-13 14:54:50 -05:00
|
|
|
end
|
|
|
|
end
|
2016-09-22 05:32:49 -04:00
|
|
|
end
|
|
|
|
|
2021-06-01 14:10:04 -04:00
|
|
|
describe '.version' do
|
|
|
|
it 'returns a version' do
|
|
|
|
expect(described_class.version).to be_present
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-22 05:32:49 -04:00
|
|
|
describe '._raw_config' do
|
|
|
|
subject { described_class._raw_config }
|
2019-10-18 07:11:44 -04:00
|
|
|
|
2017-07-10 23:35:47 -04:00
|
|
|
let(:config_file_name) { '/var/empty/doesnotexist' }
|
2016-09-22 05:32:49 -04:00
|
|
|
|
2019-04-05 04:43:27 -04:00
|
|
|
it 'is frozen' do
|
2016-09-22 05:32:49 -04:00
|
|
|
expect(subject).to be_frozen
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false when the file does not exist' do
|
|
|
|
expect(subject).to eq(false)
|
|
|
|
end
|
2017-07-11 13:38:27 -04:00
|
|
|
|
|
|
|
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-09-22 05:32:49 -04:00
|
|
|
end
|
|
|
|
|
2016-10-03 10:42:02 -04:00
|
|
|
describe '.with' do
|
2021-06-04 08:10:17 -04:00
|
|
|
let(:config_file_name) { config_old_format_socket }
|
|
|
|
|
2017-06-14 14:18:56 -04:00
|
|
|
before do
|
|
|
|
clear_pool
|
|
|
|
end
|
|
|
|
after do
|
|
|
|
clear_pool
|
|
|
|
end
|
2016-10-03 10:42:02 -04:00
|
|
|
|
2020-01-16 13:08:46 -05:00
|
|
|
context 'when running on single-threaded runtime' do
|
2017-06-14 14:18:56 -04:00
|
|
|
before do
|
2020-01-16 13:08:46 -05:00
|
|
|
allow(Gitlab::Runtime).to receive(:multi_threaded?).and_return(false)
|
2017-06-14 14:18:56 -04:00
|
|
|
end
|
2016-10-03 10:42:02 -04:00
|
|
|
|
|
|
|
it 'instantiates a connection pool with size 5' do
|
|
|
|
expect(ConnectionPool).to receive(:new).with(size: 5).and_call_original
|
|
|
|
|
2017-07-10 23:35:47 -04:00
|
|
|
described_class.with { |_redis_shared_example| true }
|
2016-10-03 10:42:02 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-01-16 13:08:46 -05:00
|
|
|
context 'when running on multi-threaded runtime' do
|
2016-10-03 10:42:02 -04:00
|
|
|
before do
|
2020-01-16 13:08:46 -05:00
|
|
|
allow(Gitlab::Runtime).to receive(:multi_threaded?).and_return(true)
|
|
|
|
allow(Gitlab::Runtime).to receive(:max_threads).and_return(18)
|
2016-10-03 10:42:02 -04:00
|
|
|
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-07-10 23:35:47 -04:00
|
|
|
described_class.with { |_redis_shared_example| true }
|
2016-10-03 10:42:02 -04:00
|
|
|
end
|
|
|
|
end
|
2021-06-09 02:10:29 -04:00
|
|
|
|
|
|
|
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
|
2016-10-25 09:50:41 -04:00
|
|
|
end
|
|
|
|
|
2021-10-08 14:13:02 -04:00
|
|
|
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
|
|
|
|
|
2016-10-25 09:50:41 -04:00
|
|
|
describe '#sentinels' do
|
2021-06-01 14:10:04 -04:00
|
|
|
subject { described_class.new(rails_env).sentinels }
|
|
|
|
|
|
|
|
let(:rails_env) { 'development' }
|
2016-10-25 09:50:41 -04:00
|
|
|
|
|
|
|
context 'when sentinels are defined' do
|
2017-07-10 23:35:47 -04:00
|
|
|
let(:config_file_name) { config_new_format_host }
|
2016-10-25 09:50:41 -04:00
|
|
|
|
2021-06-01 14:10:04 -04:00
|
|
|
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
|
2016-10-25 09:50:41 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when sentinels are not defined' do
|
2017-07-10 23:35:47 -04:00
|
|
|
let(:config_file_name) { config_old_format_host }
|
2016-10-25 09:50:41 -04:00
|
|
|
|
|
|
|
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-07-10 23:35:47 -04:00
|
|
|
let(:config_file_name) { config_new_format_host }
|
2016-10-25 09:50:41 -04:00
|
|
|
|
|
|
|
it 'returns true' do
|
|
|
|
is_expected.to be_truthy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when sentinels are not defined' do
|
2017-07-10 23:35:47 -04:00
|
|
|
let(:config_file_name) { config_old_format_host }
|
2016-10-25 09:50:41 -04:00
|
|
|
|
|
|
|
it 'returns false' do
|
|
|
|
is_expected.to be_falsey
|
|
|
|
end
|
|
|
|
end
|
2016-10-03 10:42:02 -04:00
|
|
|
end
|
|
|
|
|
2016-07-07 21:33:37 -04:00
|
|
|
describe '#raw_config_hash' do
|
2015-11-27 11:32:32 -05:00
|
|
|
it 'returns old-style single url config in a hash' do
|
2017-07-10 23:35:47 -04:00
|
|
|
expect(subject).to receive(:fetch_config) { test_redis_url }
|
|
|
|
expect(subject.send(:raw_config_hash)).to eq(url: test_redis_url)
|
2015-11-27 11:32:32 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-07 21:33:37 -04:00
|
|
|
describe '#fetch_config' do
|
2015-11-27 11:32:32 -05:00
|
|
|
it 'returns false when no config file is present' do
|
2016-09-22 05:32:49 -04:00
|
|
|
allow(described_class).to receive(:_raw_config) { false }
|
2015-11-27 11:32:32 -05:00
|
|
|
|
2017-07-11 13:38:27 -04:00
|
|
|
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
|
2015-11-27 11:32:32 -05:00
|
|
|
end
|
2021-09-30 14:11:31 -04:00
|
|
|
|
|
|
|
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
|
2015-11-27 11:32:32 -05:00
|
|
|
end
|
2016-09-22 05:32:49 -04:00
|
|
|
|
2016-10-03 10:42:02 -04:00
|
|
|
def clear_pool
|
|
|
|
described_class.remove_instance_variable(:@pool)
|
|
|
|
rescue NameError
|
|
|
|
# raised if @pool was not set; ignore
|
|
|
|
end
|
2015-11-27 11:32:32 -05:00
|
|
|
end
|