From 214fdd16329abe12c773cb1c8b7e0b127c36cf8f Mon Sep 17 00:00:00 2001 From: Andrey Lebedev Date: Wed, 2 Feb 2022 13:14:39 +0300 Subject: [PATCH] [Cache] Fix connecting to Redis via a socket file --- .github/workflows/integration-tests.yml | 16 +++++++++++- phpunit.xml.dist | 1 + .../Tests/Functional/CachePoolsTest.php | 2 +- .../app/CachePools/redis_custom_config.yml | 4 +-- .../Adapter/AbstractRedisAdapterTest.php | 4 +-- .../Cache/Tests/Adapter/PredisAdapterTest.php | 7 ++--- .../Adapter/PredisClusterAdapterTest.php | 2 +- .../Cache/Tests/Adapter/RedisAdapterTest.php | 26 ++++++++++++++----- .../Tests/Simple/AbstractRedisCacheTest.php | 2 +- .../Component/Cache/Traits/RedisTrait.php | 2 +- src/Symfony/Component/Cache/phpunit.xml.dist | 1 + .../AbstractRedisSessionHandlerTestCase.php | 2 +- .../PredisClusterSessionHandlerTest.php | 2 +- .../Handler/PredisSessionHandlerTest.php | 2 +- .../Handler/RedisSessionHandlerTest.php | 2 +- .../Lock/Tests/Store/CombinedStoreTest.php | 2 +- .../Lock/Tests/Store/PredisStoreTest.php | 4 +-- .../Lock/Tests/Store/RedisArrayStoreTest.php | 2 +- .../Lock/Tests/Store/RedisStoreTest.php | 4 +-- .../Tests/Store/RetryTillSaveStoreTest.php | 2 +- .../RedisExt/RedisTransportFactoryTest.php | 2 +- .../Tests/Caster/RedisCasterTest.php | 8 +++--- 22 files changed, 64 insertions(+), 35 deletions(-) diff --git a/.github/workflows/integration-tests.yml b/.github/workflows/integration-tests.yml index 44aff90b5a81c..d1a4c29f0952c 100644 --- a/.github/workflows/integration-tests.yml +++ b/.github/workflows/integration-tests.yml @@ -33,7 +33,7 @@ jobs: redis: image: redis:6.0.0 ports: - - 6379:6379 + - 16379:6379 redis-cluster: image: grokzen/redis-cluster:5.0.4 ports: @@ -67,6 +67,19 @@ jobs: - name: Checkout uses: actions/checkout@v2 + - name: Install system dependencies + run: | + echo "::group::apt-get update" + sudo apt-get update + echo "::endgroup::" + + echo "::group::install tools & libraries" + sudo apt-get install redis-server + sudo -- sh -c 'echo unixsocket /var/run/redis/redis-server.sock >> /etc/redis/redis.conf' + sudo -- sh -c 'echo unixsocketperm 777 >> /etc/redis/redis.conf' + sudo service redis-server restart + echo "::endgroup::" + - name: Setup PHP uses: shivammathur/setup-php@v2 with: @@ -99,6 +112,7 @@ jobs: - name: Run tests run: ./phpunit --group integration -v env: + REDIS_HOST: 'localhost:16379' REDIS_CLUSTER_HOSTS: 'localhost:7000 localhost:7001 localhost:7002 localhost:7003 localhost:7004 localhost:7005' REDIS_SENTINEL_HOSTS: 'localhost:26379' REDIS_SENTINEL_SERVICE: redis_sentinel diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 0c4dd3ee87287..cd85992d44d55 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -18,6 +18,7 @@ + diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/CachePoolsTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/CachePoolsTest.php index b7f29a0857852..fc53e26661993 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/CachePoolsTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/CachePoolsTest.php @@ -121,7 +121,7 @@ protected static function createKernel(array $options = []): KernelInterface private function skipIfRedisUnavailable() { try { - (new \Redis())->connect(getenv('REDIS_HOST')); + (new \Redis())->connect(...explode(':', getenv('REDIS_HOST'))); } catch (\Exception $e) { self::markTestSkipped($e->getMessage()); } diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/CachePools/redis_custom_config.yml b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/CachePools/redis_custom_config.yml index df20c5357f7a4..8681e59a7bb4a 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/CachePools/redis_custom_config.yml +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/CachePools/redis_custom_config.yml @@ -8,8 +8,8 @@ services: cache.test_redis_connection: public: false class: Redis - calls: - - [connect, ['%env(REDIS_HOST)%']] + factory: ['Symfony\Component\Cache\Adapter\RedisAdapter', 'createConnection'] + arguments: ['redis://%env(REDIS_HOST)%'] cache.app: parent: cache.adapter.redis diff --git a/src/Symfony/Component/Cache/Tests/Adapter/AbstractRedisAdapterTest.php b/src/Symfony/Component/Cache/Tests/Adapter/AbstractRedisAdapterTest.php index cb0876d541b3e..a1e8d19edc5cc 100644 --- a/src/Symfony/Component/Cache/Tests/Adapter/AbstractRedisAdapterTest.php +++ b/src/Symfony/Component/Cache/Tests/Adapter/AbstractRedisAdapterTest.php @@ -36,9 +36,9 @@ public static function setUpBeforeClass(): void throw new SkippedTestSuiteError('Extension redis required.'); } try { - (new \Redis())->connect(getenv('REDIS_HOST')); + (new \Redis())->connect(...explode(':', getenv('REDIS_HOST'))); } catch (\Exception $e) { - throw new SkippedTestSuiteError($e->getMessage()); + throw new SkippedTestSuiteError(getenv('REDIS_HOST').': '.$e->getMessage()); } } diff --git a/src/Symfony/Component/Cache/Tests/Adapter/PredisAdapterTest.php b/src/Symfony/Component/Cache/Tests/Adapter/PredisAdapterTest.php index a1a2b4dda3fc8..f785499a7b733 100644 --- a/src/Symfony/Component/Cache/Tests/Adapter/PredisAdapterTest.php +++ b/src/Symfony/Component/Cache/Tests/Adapter/PredisAdapterTest.php @@ -22,7 +22,7 @@ class PredisAdapterTest extends AbstractRedisAdapterTest public static function setUpBeforeClass(): void { parent::setUpBeforeClass(); - self::$redis = new \Predis\Client(['host' => getenv('REDIS_HOST')], ['prefix' => 'prefix_']); + self::$redis = new \Predis\Client(array_combine(['host', 'port'], explode(':', getenv('REDIS_HOST')) + [1 => 6379]), ['prefix' => 'prefix_']); } public function testCreateConnection() @@ -35,10 +35,11 @@ public function testCreateConnection() $connection = $redis->getConnection(); $this->assertInstanceOf(StreamConnection::class, $connection); + $redisHost = explode(':', $redisHost); $params = [ 'scheme' => 'tcp', - 'host' => $redisHost, - 'port' => 6379, + 'host' => $redisHost[0], + 'port' => (int) ($redisHost[1] ?? 6379), 'persistent' => 0, 'timeout' => 3, 'read_write_timeout' => 0, diff --git a/src/Symfony/Component/Cache/Tests/Adapter/PredisClusterAdapterTest.php b/src/Symfony/Component/Cache/Tests/Adapter/PredisClusterAdapterTest.php index e2f09cd23ae44..936c04aad49fb 100644 --- a/src/Symfony/Component/Cache/Tests/Adapter/PredisClusterAdapterTest.php +++ b/src/Symfony/Component/Cache/Tests/Adapter/PredisClusterAdapterTest.php @@ -19,7 +19,7 @@ class PredisClusterAdapterTest extends AbstractRedisAdapterTest public static function setUpBeforeClass(): void { parent::setUpBeforeClass(); - self::$redis = new \Predis\Client([['host' => getenv('REDIS_HOST')]], ['prefix' => 'prefix_']); + self::$redis = new \Predis\Client(array_combine(['host', 'port'], explode(':', getenv('REDIS_HOST')) + [1 => 6379]), ['prefix' => 'prefix_']); } public static function tearDownAfterClass(): void diff --git a/src/Symfony/Component/Cache/Tests/Adapter/RedisAdapterTest.php b/src/Symfony/Component/Cache/Tests/Adapter/RedisAdapterTest.php index b020ddc9f43a8..aa2ce75a4555d 100644 --- a/src/Symfony/Component/Cache/Tests/Adapter/RedisAdapterTest.php +++ b/src/Symfony/Component/Cache/Tests/Adapter/RedisAdapterTest.php @@ -40,30 +40,42 @@ public function createCachePool(int $defaultLifetime = 0, string $testMethod = n return $adapter; } - public function testCreateConnection() + public function testCreateHostConnection() { $redis = RedisAdapter::createConnection('redis:?host[h1]&host[h2]&host[/foo:]'); $this->assertInstanceOf(\RedisArray::class, $redis); $this->assertSame(['h1:6379', 'h2:6379', '/foo'], $redis->_hosts()); @$redis = null; // some versions of phpredis connect on destruct, let's silence the warning - $redisHost = getenv('REDIS_HOST'); + $this->doTestCreateConnection(getenv('REDIS_HOST')); + } + + public function testCreateSocketConnection() + { + if (!getenv('REDIS_SOCKET') || !file_exists(getenv('REDIS_SOCKET'))) { + $this->markTestSkipped('Redis socket not found'); + } + + $this->doTestCreateConnection(getenv('REDIS_SOCKET')); + } - $redis = RedisAdapter::createConnection('redis://'.$redisHost); + private function doTestCreateConnection(string $uri) + { + $redis = RedisAdapter::createConnection('redis://'.$uri); $this->assertInstanceOf(\Redis::class, $redis); $this->assertTrue($redis->isConnected()); $this->assertSame(0, $redis->getDbNum()); - $redis = RedisAdapter::createConnection('redis://'.$redisHost.'/2'); + $redis = RedisAdapter::createConnection('redis://'.$uri.'/2'); $this->assertSame(2, $redis->getDbNum()); - $redis = RedisAdapter::createConnection('redis://'.$redisHost, ['timeout' => 3]); + $redis = RedisAdapter::createConnection('redis://'.$uri, ['timeout' => 3]); $this->assertEquals(3, $redis->getTimeout()); - $redis = RedisAdapter::createConnection('redis://'.$redisHost.'?timeout=4'); + $redis = RedisAdapter::createConnection('redis://'.$uri.'?timeout=4'); $this->assertEquals(4, $redis->getTimeout()); - $redis = RedisAdapter::createConnection('redis://'.$redisHost, ['read_timeout' => 5]); + $redis = RedisAdapter::createConnection('redis://'.$uri, ['read_timeout' => 5]); $this->assertEquals(5, $redis->getReadTimeout()); } diff --git a/src/Symfony/Component/Cache/Tests/Simple/AbstractRedisCacheTest.php b/src/Symfony/Component/Cache/Tests/Simple/AbstractRedisCacheTest.php index 40830fc7fba1f..3e8ededc0f470 100644 --- a/src/Symfony/Component/Cache/Tests/Simple/AbstractRedisCacheTest.php +++ b/src/Symfony/Component/Cache/Tests/Simple/AbstractRedisCacheTest.php @@ -39,7 +39,7 @@ public static function setUpBeforeClass(): void throw new SkippedTestSuiteError('Extension redis required.'); } try { - (new \Redis())->connect(getenv('REDIS_HOST')); + (new \Redis())->connect(...explode(':', getenv('REDIS_HOST'))); } catch (\Exception $e) { throw new SkippedTestSuiteError($e->getMessage()); } diff --git a/src/Symfony/Component/Cache/Traits/RedisTrait.php b/src/Symfony/Component/Cache/Traits/RedisTrait.php index d2e73ae566e6f..ce0a695ed4474 100644 --- a/src/Symfony/Component/Cache/Traits/RedisTrait.php +++ b/src/Symfony/Component/Cache/Traits/RedisTrait.php @@ -185,7 +185,7 @@ public static function createConnection($dsn, array $options = []) $initializer = static function ($redis) use ($connect, $params, $dsn, $auth, $hosts, $tls) { $host = $hosts[0]['host'] ?? $hosts[0]['path']; - $port = $hosts[0]['port'] ?? 6379; + $port = $hosts[0]['port'] ?? 0; if (isset($hosts[0]['host']) && $tls) { $host = 'tls://'.$host; diff --git a/src/Symfony/Component/Cache/phpunit.xml.dist b/src/Symfony/Component/Cache/phpunit.xml.dist index 591046cf1c41c..552fa55a2bc47 100644 --- a/src/Symfony/Component/Cache/phpunit.xml.dist +++ b/src/Symfony/Component/Cache/phpunit.xml.dist @@ -11,6 +11,7 @@ + diff --git a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/AbstractRedisSessionHandlerTestCase.php b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/AbstractRedisSessionHandlerTestCase.php index 3dcb3c51dbba3..c4da2b8673ae0 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/AbstractRedisSessionHandlerTestCase.php +++ b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/AbstractRedisSessionHandlerTestCase.php @@ -45,7 +45,7 @@ protected function setUp(): void self::markTestSkipped('Extension redis required.'); } try { - (new \Redis())->connect(getenv('REDIS_HOST')); + (new \Redis())->connect(...explode(':', getenv('REDIS_HOST'))); } catch (\Exception $e) { self::markTestSkipped($e->getMessage()); } diff --git a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/PredisClusterSessionHandlerTest.php b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/PredisClusterSessionHandlerTest.php index 8926fb1a93a14..fd4a13bef5fdf 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/PredisClusterSessionHandlerTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/PredisClusterSessionHandlerTest.php @@ -20,6 +20,6 @@ class PredisClusterSessionHandlerTest extends AbstractRedisSessionHandlerTestCas { protected function createRedisClient(string $host): Client { - return new Client([['host' => $host]]); + return new Client([array_combine(['host', 'port'], explode(':', getenv('REDIS_HOST')) + [1 => 6379])]); } } diff --git a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/PredisSessionHandlerTest.php b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/PredisSessionHandlerTest.php index bb33a3d9a56e2..0dc194ba1d19f 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/PredisSessionHandlerTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/PredisSessionHandlerTest.php @@ -20,6 +20,6 @@ class PredisSessionHandlerTest extends AbstractRedisSessionHandlerTestCase { protected function createRedisClient(string $host): Client { - return new Client(['host' => $host]); + return new Client(array_combine(['host', 'port'], explode(':', getenv('REDIS_HOST')) + [1 => 6379])); } } diff --git a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/RedisSessionHandlerTest.php b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/RedisSessionHandlerTest.php index e7fb1ca196ef4..7db8909919f52 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/RedisSessionHandlerTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/RedisSessionHandlerTest.php @@ -22,7 +22,7 @@ class RedisSessionHandlerTest extends AbstractRedisSessionHandlerTestCase protected function createRedisClient(string $host) { $client = new \Redis(); - $client->connect($host); + $client->connect(...explode(':', $host)); return $client; } diff --git a/src/Symfony/Component/Lock/Tests/Store/CombinedStoreTest.php b/src/Symfony/Component/Lock/Tests/Store/CombinedStoreTest.php index a1b6a53fe718a..991208f8b204e 100644 --- a/src/Symfony/Component/Lock/Tests/Store/CombinedStoreTest.php +++ b/src/Symfony/Component/Lock/Tests/Store/CombinedStoreTest.php @@ -41,7 +41,7 @@ protected function getClockDelay() */ public function getStore(): PersistingStoreInterface { - $redis = new \Predis\Client('tcp://'.getenv('REDIS_HOST').':6379'); + $redis = new \Predis\Client(array_combine(['host', 'port'], explode(':', getenv('REDIS_HOST')) + [1 => null])); try { $redis->connect(); } catch (\Exception $e) { diff --git a/src/Symfony/Component/Lock/Tests/Store/PredisStoreTest.php b/src/Symfony/Component/Lock/Tests/Store/PredisStoreTest.php index 179a8246f7b78..a74f404ba60a7 100644 --- a/src/Symfony/Component/Lock/Tests/Store/PredisStoreTest.php +++ b/src/Symfony/Component/Lock/Tests/Store/PredisStoreTest.php @@ -21,7 +21,7 @@ class PredisStoreTest extends AbstractRedisStoreTest { public static function setUpBeforeClass(): void { - $redis = new \Predis\Client('tcp://'.getenv('REDIS_HOST').':6379'); + $redis = new \Predis\Client(array_combine(['host', 'port'], explode(':', getenv('REDIS_HOST')) + [1 => null])); try { $redis->connect(); } catch (\Exception $e) { @@ -31,7 +31,7 @@ public static function setUpBeforeClass(): void protected function getRedisConnection() { - $redis = new \Predis\Client('tcp://'.getenv('REDIS_HOST').':6379'); + $redis = new \Predis\Client(array_combine(['host', 'port'], explode(':', getenv('REDIS_HOST')) + [1 => null])); $redis->connect(); return $redis; diff --git a/src/Symfony/Component/Lock/Tests/Store/RedisArrayStoreTest.php b/src/Symfony/Component/Lock/Tests/Store/RedisArrayStoreTest.php index 3b194d7c92f57..6574ac1a859c7 100644 --- a/src/Symfony/Component/Lock/Tests/Store/RedisArrayStoreTest.php +++ b/src/Symfony/Component/Lock/Tests/Store/RedisArrayStoreTest.php @@ -27,7 +27,7 @@ public static function setUpBeforeClass(): void throw new SkippedTestSuiteError('The RedisArray class is required.'); } try { - (new \Redis())->connect(getenv('REDIS_HOST')); + (new \Redis())->connect(...explode(':', getenv('REDIS_HOST'))); } catch (\Exception $e) { throw new SkippedTestSuiteError($e->getMessage()); } diff --git a/src/Symfony/Component/Lock/Tests/Store/RedisStoreTest.php b/src/Symfony/Component/Lock/Tests/Store/RedisStoreTest.php index 0f01e26407bae..15eb3292e477f 100644 --- a/src/Symfony/Component/Lock/Tests/Store/RedisStoreTest.php +++ b/src/Symfony/Component/Lock/Tests/Store/RedisStoreTest.php @@ -26,7 +26,7 @@ class RedisStoreTest extends AbstractRedisStoreTest public static function setUpBeforeClass(): void { try { - (new \Redis())->connect(getenv('REDIS_HOST')); + (new \Redis())->connect(...explode(':', getenv('REDIS_HOST'))); } catch (\Exception $e) { throw new SkippedTestSuiteError($e->getMessage()); } @@ -35,7 +35,7 @@ public static function setUpBeforeClass(): void protected function getRedisConnection() { $redis = new \Redis(); - $redis->connect(getenv('REDIS_HOST')); + $redis->connect(...explode(':', getenv('REDIS_HOST'))); return $redis; } diff --git a/src/Symfony/Component/Lock/Tests/Store/RetryTillSaveStoreTest.php b/src/Symfony/Component/Lock/Tests/Store/RetryTillSaveStoreTest.php index 12d7d7a1494fa..6de1f803fd6ad 100644 --- a/src/Symfony/Component/Lock/Tests/Store/RetryTillSaveStoreTest.php +++ b/src/Symfony/Component/Lock/Tests/Store/RetryTillSaveStoreTest.php @@ -24,7 +24,7 @@ class RetryTillSaveStoreTest extends AbstractStoreTest public function getStore(): PersistingStoreInterface { - $redis = new \Predis\Client('tcp://'.getenv('REDIS_HOST').':6379'); + $redis = new \Predis\Client(array_combine(['host', 'port'], explode(':', getenv('REDIS_HOST')) + [1 => null])); try { $redis->connect(); } catch (\Exception $e) { diff --git a/src/Symfony/Component/Messenger/Tests/Transport/RedisExt/RedisTransportFactoryTest.php b/src/Symfony/Component/Messenger/Tests/Transport/RedisExt/RedisTransportFactoryTest.php index 7e02c0d46acb5..bb8cf18da3395 100644 --- a/src/Symfony/Component/Messenger/Tests/Transport/RedisExt/RedisTransportFactoryTest.php +++ b/src/Symfony/Component/Messenger/Tests/Transport/RedisExt/RedisTransportFactoryTest.php @@ -48,7 +48,7 @@ public function testCreateTransport() private function skipIfRedisUnavailable() { try { - (new \Redis())->connect(getenv('REDIS_HOST')); + (new \Redis())->connect(...explode(':', getenv('REDIS_HOST'))); } catch (\Exception $e) { self::markTestSkipped($e->getMessage()); } diff --git a/src/Symfony/Component/VarDumper/Tests/Caster/RedisCasterTest.php b/src/Symfony/Component/VarDumper/Tests/Caster/RedisCasterTest.php index 7060a7ddec0f4..058b95d0d0ab6 100644 --- a/src/Symfony/Component/VarDumper/Tests/Caster/RedisCasterTest.php +++ b/src/Symfony/Component/VarDumper/Tests/Caster/RedisCasterTest.php @@ -38,10 +38,10 @@ public function testNotConnected() public function testConnected() { - $redisHost = getenv('REDIS_HOST'); + $redisHost = explode(':', getenv('REDIS_HOST')) + [1 => 6379]; $redis = new \Redis(); try { - $redis->connect($redisHost); + $redis->connect(...$redisHost); } catch (\Exception $e) { self::markTestSkipped($e->getMessage()); } @@ -49,8 +49,8 @@ public function testConnected() $xCast = <<