From 24651454512686103c55b5602f52ebae63cf4bb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Mart=C3=ADn?= Date: Tue, 18 Feb 2020 09:43:03 +0100 Subject: [PATCH] recover Redis fields for local testing --- spec/support/redis.rb | 59 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/spec/support/redis.rb b/spec/support/redis.rb index 3beabf6fd4..72fe3f9f5f 100644 --- a/spec/support/redis.rb +++ b/spec/support/redis.rb @@ -18,11 +18,70 @@ module CartoDB end class RedisTest + REDIS_PID = "/tmp/redis-test.pid" + REDIS_CACHE_PATH = "/tmp" + REDIS_DB_NAME = "redis_test.rdb" def self.down + if ENV['REDIS_PORT'] + if File.file?("/tmp/redis-test-#{ENV['REDIS_PORT']}.tmp") + puts "\n[redis] Shutting down test server..." + pid = File.read("/tmp/redis-test-#{ENV['REDIS_PORT']}.tmp").to_i + system("kill -9 #{pid}") + File.delete("/tmp/redis-test-#{ENV['REDIS_PORT']}.tmp") + end + else + if File.file?(REDIS_PID) + puts "\n[redis] Shutting down test server..." + pid = File.read(REDIS_PID).to_i + system("kill -9 #{pid}") + File.delete(REDIS_PID) + end + File.delete(File.join(REDIS_CACHE_PATH, REDIS_DB_NAME)) if File.file?(File.join(REDIS_CACHE_PATH, REDIS_DB_NAME)) + end end def self.up + down + if ENV['REDIS_PORT'] + print "Setting up redis config..." + port = ENV['REDIS_PORT'] + new_redis_pid = "/tmp/redis-test-#{ENV['REDIS_PORT']}.tmp" + new_cache_path = "/tmp/redis-#{ENV['REDIS_PORT']}" + new_logfile = "/tmp/redis-#{ENV['REDIS_PORT']}/stdout" + Dir.mkdir "/tmp/redis-#{ENV['REDIS_PORT']}" unless File.exists?("/tmp/redis-#{ENV['REDIS_PORT']}") + else + port = Cartodb.config[:redis]["port"] + end + print "[redis] Starting test server on port #{port}... " + + raise "Your OS is not supported" unless OS.unix? + + redis_cell_base_path = '/etc/redis/redis-cell' + redis_cell_path = "#{redis_cell_base_path}/libredis_cell.so" + redis_cell_path = "#{redis_cell_base_path}/libredis_cell.dylib" if OS.mac? + + raise "Please drop redis-cell binaries in #{redis_cell_base_path}" unless FileTest.exist?(redis_cell_path) + + redis_options = { + "port" => port, + "daemonize" => 'yes', + "pidfile" => new_redis_pid || REDIS_PID, + "timeout" => 300, + "dbfilename" => REDIS_DB_NAME, + "dir" => new_cache_path || REDIS_CACHE_PATH, + "loglevel" => "notice", + "logfile" => new_logfile || "stdout", + "loadmodule" => redis_cell_path + }.map { |k, v| "#{k} #{v}" }.join("\n") + + output = `printf '#{redis_options}' | redis-server - 2>&1` + if $?.success? + puts('done') + sleep 2 + else + raise "Error starting test Redis server: #{output}" + end end end