Added support to run API tests on roadrunner

This commit is contained in:
Alejandro Celaya 2022-08-27 09:09:14 +02:00
parent c5b6d203f5
commit ca515998e4
8 changed files with 37 additions and 18 deletions

1
.gitignore vendored
View file

@ -1,6 +1,7 @@
.idea
bin/.rr.*
bin/rr
config/roadrunner/.pid
build
!docker/build
composer.lock

View file

@ -2,24 +2,36 @@
export APP_ENV=test
export DB_DRIVER=postgres
export TEST_ENV=api
export TEST_RUNTIME=${TEST_RUNTIME:-"openswoole"}
export GENERATE_COVERAGE=${GENERATE_COVERAGE:-"no"}
# Reset logs
OUTPUT_LOGS=data/log/api-tests/output.log
rm -rf data/log/api-tests
mkdir data/log/api-tests
touch data/log/api-tests/output.log
touch $OUTPUT_LOGS
# Try to stop server just in case it hanged in last execution
vendor/bin/laminas mezzio:swoole:stop
[[ $TEST_RUNTIME == 'openswoole' ]] && vendor/bin/laminas mezzio:swoole:stop
[[ $TEST_RUNTIME == 'rr' ]] && bin/rr stop -f
echo 'Starting server...'
vendor/bin/laminas mezzio:swoole:start -d
sleep 2
[[ $TEST_RUNTIME == 'openswoole' ]] && vendor/bin/laminas mezzio:swoole:start -d
[[ $TEST_RUNTIME == 'rr' ]] && bin/rr serve -p -c=config/roadrunner/.rr.dev.yml \
-o=http.address=0.0.0.0:9999 \
-o=logs.encoding=json \
-o=logs.channels.http.encoding=json \
-o=logs.channels.server.encoding=json \
-o=logs.output="${PWD}/${OUTPUT_LOGS}" \
-o=logs.channels.http.output="${PWD}/${OUTPUT_LOGS}" \
-o=logs.channels.server.output="${PWD}/${OUTPUT_LOGS}" &
sleep 2 # Let's give the server a couple of seconds to start
vendor/bin/phpunit --order-by=random -c phpunit-api.xml --testdox --colors=always --log-junit=build/coverage-api/junit.xml $*
testsExitCode=$?
vendor/bin/laminas mezzio:swoole:stop
[[ $TEST_RUNTIME == 'openswoole' ]] && vendor/bin/laminas mezzio:swoole:stop
[[ $TEST_RUNTIME == 'rr' ]] && bin/rr stop -c config/roadrunner/.rr.dev.yml -o=http.address=0.0.0.0:9999
# Exit this script with the same code as the tests. If tests failed, this script has to fail
exit $testsExitCode

View file

@ -122,6 +122,7 @@
"test:db:postgres": "DB_DRIVER=postgres composer test:db:sqlite",
"test:db:ms": "DB_DRIVER=mssql composer test:db:sqlite",
"test:api": "bin/test/run-api-tests.sh",
"test:api:rr": "TEST_RUNTIME=rr bin/test/run-api-tests.sh",
"test:api:ci": "GENERATE_COVERAGE=yes composer test:api",
"test:api:pretty": "GENERATE_COVERAGE=pretty composer test:api",
"test:cli": "APP_ENV=test DB_DRIVER=maria TEST_ENV=cli php vendor/bin/phpunit --order-by=random --colors=always --testdox -c phpunit-cli.xml --log-junit=build/coverage-cli/junit.xml",

View file

@ -43,7 +43,7 @@ logs:
reload:
interval: 1s
patterns: ['.php', '.yml', '.yaml']
patterns: ['.php']
services:
http:
dirs: ['../../bin', '../../config', '../../data/migrations', '../../docker', '../../module', '../../vendor']

View file

@ -10,8 +10,8 @@ use Psr\Container\ContainerInterface;
use function register_shutdown_function;
use function sprintf;
use const ShlinkioTest\Shlink\SWOOLE_TESTING_HOST;
use const ShlinkioTest\Shlink\SWOOLE_TESTING_PORT;
use const ShlinkioTest\Shlink\API_TESTS_HOST;
use const ShlinkioTest\Shlink\API_TESTS_PORT;
/** @var ContainerInterface $container */
$container = require __DIR__ . '/../container.php';
@ -24,7 +24,7 @@ $httpClient = $container->get('shlink_test_api_client');
register_shutdown_function(function () use ($httpClient): void {
$httpClient->request(
'GET',
sprintf('http://%s:%s/api-tests/stop-coverage', SWOOLE_TESTING_HOST, SWOOLE_TESTING_PORT),
sprintf('http://%s:%s/api-tests/stop-coverage', API_TESTS_HOST, API_TESTS_PORT),
);
});

View file

@ -4,5 +4,5 @@ declare(strict_types=1);
namespace ShlinkioTest\Shlink;
const SWOOLE_TESTING_HOST = '127.0.0.1';
const SWOOLE_TESTING_PORT = 9999;
const API_TESTS_HOST = '127.0.0.1';
const API_TESTS_PORT = 9999;

View file

@ -34,8 +34,8 @@ use function Shlinkio\Shlink\Config\env;
use function sprintf;
use function sys_get_temp_dir;
use const ShlinkioTest\Shlink\SWOOLE_TESTING_HOST;
use const ShlinkioTest\Shlink\SWOOLE_TESTING_PORT;
use const ShlinkioTest\Shlink\API_TESTS_HOST;
use const ShlinkioTest\Shlink\API_TESTS_PORT;
$isApiTest = env('TEST_ENV') === 'api';
$isCliTest = env('TEST_ENV') === 'cli';
@ -136,8 +136,8 @@ return [
'mezzio-swoole' => [
'enable_coroutine' => false,
'swoole-http-server' => [
'host' => SWOOLE_TESTING_HOST,
'port' => SWOOLE_TESTING_PORT,
'host' => API_TESTS_HOST,
'port' => API_TESTS_PORT,
'process-name' => 'shlink_test',
'options' => [
'pid_file' => sys_get_temp_dir() . '/shlink-test-swoole.pid',
@ -188,7 +188,7 @@ return [
'dependencies' => [
'services' => [
'shlink_test_api_client' => new Client([
'base_uri' => sprintf('http://%s:%s/', SWOOLE_TESTING_HOST, SWOOLE_TESTING_PORT),
'base_uri' => sprintf('http://%s:%s/', API_TESTS_HOST, API_TESTS_PORT),
'http_errors' => false,
]),
],

View file

@ -1,8 +1,13 @@
#!/usr/bin/env bash
# Run docker containers if they are not up yet
if ! [[ $(docker ps | grep shlink_swoole) ]]; then
if ! [[ $(docker ps | grep shlink) ]]; then
docker-compose up -d
fi
docker exec -it shlink_swoole /bin/sh -c "$*"
if [[ "$*" == *"test:api:rr"* ]]; then
# API tests should be run inside the RoadRunner container when the test runtime is RoadRunner
docker exec -it shlink_roadrunner /bin/sh -c "$*"
else
docker exec -it shlink_swoole /bin/sh -c "$*"
fi