From 9feb72235ae13de98e80b2060f5ca20bc4efd84e Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sat, 13 Feb 2021 11:40:19 +0100 Subject: [PATCH 1/5] Added config to log in filesystem while running API tests --- bin/test/run-api-tests.sh | 2 ++ config/test/test_config.global.php | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/bin/test/run-api-tests.sh b/bin/test/run-api-tests.sh index 07b36881..dbd87a84 100755 --- a/bin/test/run-api-tests.sh +++ b/bin/test/run-api-tests.sh @@ -3,6 +3,8 @@ export APP_ENV=test export DB_DRIVER=postgres export TEST_ENV=api +rm -rf data/log/api-tests + # Try to stop server just in case it hanged in last execution vendor/bin/laminas mezzio:swoole:stop diff --git a/config/test/test_config.global.php b/config/test/test_config.global.php index 3608257e..7ea5644a 100644 --- a/config/test/test_config.global.php +++ b/config/test/test_config.global.php @@ -9,6 +9,8 @@ use Laminas\ConfigAggregator\ConfigAggregator; use Laminas\Diactoros\Response\EmptyResponse; use Laminas\ServiceManager\Factory\InvokableFactory; use Laminas\Stdlib\Glob; +use Monolog\Handler\RotatingFileHandler; +use Monolog\Logger; use PDO; use PHPUnit\Runner\Version; use SebastianBergmann\CodeCoverage\CodeCoverage; @@ -163,4 +165,28 @@ return [ ], ], + 'logger' => [ + 'Shlink' => [ + 'handlers' => [ + 'shlink_handler' => [ + 'params' => [ + 'level' => Logger::DEBUG, + 'filename' => 'data/log/api-tests/shlink_log.log', + ], + ], + ], + ], + 'Access' => [ + 'handlers' => [ + 'access_handler' => [ + 'name' => RotatingFileHandler::class, + 'params' => [ + 'level' => Logger::DEBUG, + 'filename' => 'data/log/api-tests/access_log.log', + ], + ], + ], + ], + ], + ]; From 4439685403cdf55f1486b4d06026ed2b48afa02d Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 14 Mar 2021 08:49:38 +0100 Subject: [PATCH 2/5] Fixed logs generated by shlink during API tests --- composer.json | 2 +- config/test/test_config.global.php | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/composer.json b/composer.json index 53acdf0a..89764118 100644 --- a/composer.json +++ b/composer.json @@ -33,7 +33,7 @@ "laminas/laminas-stdlib": "^3.2", "lcobucci/jwt": "^4.0", "league/uri": "^6.2", - "lstrojny/functional-php": "^1.15", + "lstrojny/functional-php": "^1.17", "mezzio/mezzio": "^3.3", "mezzio/mezzio-fastroute": "^3.1", "mezzio/mezzio-problem-details": "^1.3", diff --git a/config/test/test_config.global.php b/config/test/test_config.global.php index 7ea5644a..d12902a5 100644 --- a/config/test/test_config.global.php +++ b/config/test/test_config.global.php @@ -9,7 +9,7 @@ use Laminas\ConfigAggregator\ConfigAggregator; use Laminas\Diactoros\Response\EmptyResponse; use Laminas\ServiceManager\Factory\InvokableFactory; use Laminas\Stdlib\Glob; -use Monolog\Handler\RotatingFileHandler; +use Monolog\Handler\StreamHandler; use Monolog\Logger; use PDO; use PHPUnit\Runner\Version; @@ -169,9 +169,10 @@ return [ 'Shlink' => [ 'handlers' => [ 'shlink_handler' => [ + 'name' => StreamHandler::class, 'params' => [ 'level' => Logger::DEBUG, - 'filename' => 'data/log/api-tests/shlink_log.log', + 'stream' => 'data/log/api-tests/shlink.log', ], ], ], @@ -179,10 +180,10 @@ return [ 'Access' => [ 'handlers' => [ 'access_handler' => [ - 'name' => RotatingFileHandler::class, + 'name' => StreamHandler::class, 'params' => [ 'level' => Logger::DEBUG, - 'filename' => 'data/log/api-tests/access_log.log', + 'stream' => 'data/log/api-tests/access.log', ], ], ], From d104265f04dd14b6b14a5888313f08cf825fe9bb Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 14 Mar 2021 08:54:05 +0100 Subject: [PATCH 3/5] Updated CONTRIBUTING file, explaining how the logs are dumped during API tests --- CONTRIBUTING.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index eec7b0fb..837f7593 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -96,12 +96,14 @@ In order to ensure stability and no regressions are introduced while developing The project provides some tooling to run them against any of the supported database engines. -* **API tests**: These are E2E tests that spin up an instance of the app and test it from the outside, by interacting with the REST API. +* **API tests**: These are E2E tests that spin up an instance of the app with swoole, and test it from the outside by interacting with the REST API. These are the best tests to catch regressions, and to verify everything behaves as expected. They use Postgres as the database engine, and include some fixtures that ensure the same data exists at the beginning of the execution. + Since the app instance is run on a process different from the one running the tests, when a test fails it might not be obvious why. To help debugging that, the app will dump all its logs inside `data/log/api-tests`, where you will find the `shlink.log` and `access.log` files. + * **CLI tests**: *TBD. Once included, its purpose will be the same as API tests, but running through the command line* Depending on the kind of contribution, maybe not all kinds of tests are needed, but the more you provide, the better. From 562110fac4505cea84fa22d14dc424233c76752a Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 14 Mar 2021 08:55:39 +0100 Subject: [PATCH 4/5] Updated changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 17d1799f..97cf925c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com), and this ### Changed * [#1036](https://github.com/shlinkio/shlink/issues/1036) Updated to `happyr/doctrine-specification` 2.0. * [#1039](https://github.com/shlinkio/shlink/issues/1039) Updated to `endroid/qr-code` 4.0. +* [#1008](https://github.com/shlinkio/shlink/issues/1008) Ensured all logs are sent to the filesystem while running API tests, which helps debugging the reason for tests to fail. ### Deprecated * *Nothing* From d7523bcb57d7a1752abd7a6442e19841959127c9 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 14 Mar 2021 09:01:11 +0100 Subject: [PATCH 5/5] Reduced duplication by creating a function that builds test logger config --- config/test/test_config.global.php | 36 ++++++++++++------------------ 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/config/test/test_config.global.php b/config/test/test_config.global.php index d12902a5..c6d90e39 100644 --- a/config/test/test_config.global.php +++ b/config/test/test_config.global.php @@ -82,6 +82,18 @@ $buildDbConnection = function (): array { return $driverConfigMap[$driver] ?? []; }; +$buildTestLoggerConfig = fn (string $handlerName, string $filename) => [ + 'handlers' => [ + $handlerName => [ + 'name' => StreamHandler::class, + 'params' => [ + 'level' => Logger::DEBUG, + 'stream' => sprintf('data/log/api-tests/%s', $filename), + ], + ], + ], +]; + return [ 'debug' => true, @@ -166,28 +178,8 @@ return [ ], 'logger' => [ - 'Shlink' => [ - 'handlers' => [ - 'shlink_handler' => [ - 'name' => StreamHandler::class, - 'params' => [ - 'level' => Logger::DEBUG, - 'stream' => 'data/log/api-tests/shlink.log', - ], - ], - ], - ], - 'Access' => [ - 'handlers' => [ - 'access_handler' => [ - 'name' => StreamHandler::class, - 'params' => [ - 'level' => Logger::DEBUG, - 'stream' => 'data/log/api-tests/access.log', - ], - ], - ], - ], + 'Shlink' => $buildTestLoggerConfig('shlink_handler', 'shlink.log'), + 'Access' => $buildTestLoggerConfig('access_handler', 'access.log'), ], ];