Skip to content

Commit

Permalink
Test environment
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuravljov committed Jun 10, 2018
1 parent 1e7cbf1 commit 2e1c37b
Show file tree
Hide file tree
Showing 17 changed files with 180 additions and 82 deletions.
2 changes: 1 addition & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
/.php_cs.cache
/.travis.yml
/composer.lock
/tests/app/runtime
/tests/runtime/*
/vendor
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ max_line_length = 80

[*.md]
trim_trailing_whitespace = false

[Makefile]
indent_style = tab
2 changes: 2 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
<testsuite name="Test Suite">
<directory>./tests</directory>
<exclude>./tests/app</exclude>
<exclude>./tests/docker</exclude>
<exclude>./tests/runtime</exclude>
</testsuite>
</testsuites>
</phpunit>
46 changes: 46 additions & 0 deletions tests/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
init: clean build
docker-compose run php72 true
docker-compose down

test: test-php72 test-php71 test-php70 test-php56
test-php72:
docker-compose run php72 vendor/bin/phpunit --verbose
docker-compose down
test-php71:
docker-compose run php71 vendor/bin/phpunit --verbose
docker-compose down
test-php70:
docker-compose run php70 vendor/bin/phpunit --verbose
docker-compose down
test-php56:
docker-compose run php56 vendor/bin/phpunit --verbose
docker-compose down

benchmark: benchmark-php72 benchmark-php71 benchmark-php70 benchmark-php56
benchmark-php72:
docker-compose run php72 tests/yii benchmark/waiting
docker-compose down
benchmark-php71:
docker-compose run php71 tests/yii benchmark/waiting
docker-compose down
benchmark-php70:
docker-compose run php70 tests/yii benchmark/waiting
docker-compose down
benchmark-php56:
docker-compose run php56 tests/yii benchmark/waiting
docker-compose down

check-cs:
docker-compose run php72 php-cs-fixer fix --diff --dry-run
docker-compose down

build:
docker-compose pull
docker-compose build

clean:
docker-compose down
sudo rm -rf runtime/*
sudo rm -f ../.php_cs.cache
sudo rm -f ../composer.lock
sudo rm -rf ../vendor/
1 change: 1 addition & 0 deletions tests/app/config/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
'id' => 'yii2-queue-app',
'basePath' => dirname(__DIR__),
'vendorPath' => dirname(dirname(__DIR__)) . '/vendor',
'runtimePath' => dirname(dirname(__DIR__)) . '/runtime',
'bootstrap' => [
'fileQueue',
'mysqlQueue',
Expand Down
2 changes: 0 additions & 2 deletions tests/app/docker/mysql/8.0/auth.cnf

This file was deleted.

13 changes: 0 additions & 13 deletions tests/app/docker/php/entrypoint.sh

This file was deleted.

103 changes: 59 additions & 44 deletions tests/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
version: "3"
version: "3.5"
services:

# https://docs.docker.com/samples/library/php/
php72:
container_name: yii2_queue_php72
build:
context: .
dockerfile: tests/app/docker/php/7.2/Dockerfile
command: &php_command ["sleep", "infinity"]
context: ..
dockerfile: tests/docker/php/7.2/Dockerfile
volumes:
- ./runtime/composer72:/root/.composer
- ..:/code
dns: &php_dns
- 8.8.8.8
- 4.4.4.4
volumes: &php_volumes
#- .:/code
- ./tests/app/runtime/composer:/root/.composer
- 8.8.8.8
- 4.4.4.4
environment: &php_environment
MYSQL_HOST: mysql
MYSQL_PORT: 3306
Expand All @@ -37,99 +36,115 @@ services:
GEARMAN_HOST: gearmand
GEARMAN_PORT: 4730
depends_on: &php_depends_on
- mysql
- postgres
- redis
- rabbitmq
- beanstalk
- gearmand
- mysql
- postgres
- redis
- rabbitmq
- beanstalk
- gearmand
networks:
net: {}
php71:
container_name: yii2_queue_php71
build:
context: .
dockerfile: tests/app/docker/php/7.1/Dockerfile
command: *php_command
context: ..
dockerfile: tests/docker/php/7.1/Dockerfile
volumes:
- ./runtime/composer71:/root/.composer
dns: *php_dns
volumes: *php_volumes
environment: *php_environment
depends_on: *php_depends_on
networks:
net: {}
php70:
container_name: yii2_queue_php70
build:
context: .
dockerfile: tests/app/docker/php/7.0/Dockerfile
command: *php_command
context: ..
dockerfile: tests/docker/php/7.0/Dockerfile
volumes:
- ./runtime/composer70:/root/.composer
dns: *php_dns
volumes: *php_volumes
environment: *php_environment
depends_on: *php_depends_on
networks:
net: {}
php56:
container_name: yii2_queue_php56
build:
context: .
dockerfile: tests/app/docker/php/5.6/Dockerfile
command: *php_command
context: ..
dockerfile: tests/docker/php/5.6/Dockerfile
volumes:
- ./runtime/composer56:/root/.composer
dns: *php_dns
volumes: *php_volumes
environment: *php_environment
depends_on: *php_depends_on
networks:
net: {}

# https://docs.docker.com/samples/library/mysql/
mysql:
container_name: yii2-queue-mysql
image: mysql:8.0
container_name: yii2_queue_mysql
image: mysql:5.7
ports:
- 3306:3306
volumes:
- ./tests/app/docker/mysql/8.0/auth.cnf:/etc/mysql/conf.d/auth.cnf:ro
#- ./tests/app/runtime/mysql:/var/lib/mysql
- 3307:3306
environment:
MYSQL_ALLOW_EMPTY_PASSWORD: 1
MYSQL_USER: yii2_queue_test
MYSQL_PASSWORD: yii2_queue_test
MYSQL_DATABASE: yii2_queue_test
networks:
net: {}

# https://docs.docker.com/samples/library/postgres/
postgres:
container_name: yii2_queue_postgres
image: postgres:10.4
ports:
- 5432:5432
#volumes:
# - ./tests/app/runtime/postgres:/var/lib/postgresql/data
- 5433:5432
environment:
POSTGRES_USER: yii2_queue_test
POSTGRES_PASSWORD: yii2_queue_test
POSTGRES_DB: yii2_queue_test
networks:
net: {}

# https://docs.docker.com/samples/library/redis/
redis:
container_name: yii2_queue_redis
image: redis:4.0
ports:
- 6379:6379
#volumes:
# - ./tests/app/runtime/redis:/data
- 6380:6379
networks:
net: {}

# https://docs.docker.com/samples/library/rabbitmq/
rabbitmq:
container_name: yii2_queue_rabbitmq
image: rabbitmq:3.7
ports:
- 5672:5672
#volumes:
# - ./tests/app/runtime/rabbitmq:/var/lib/rabbitmq
- 5673:5672
networks:
net: {}

# https://hub.docker.com/r/schickling/beanstalkd/
beanstalk:
container_name: yii2_queue_beanstalk
image: schickling/beanstalkd
ports:
- 11300:11300
- 11301:11300
networks:
net: {}

# https://hub.docker.com/r/artefactual/gearmand/
gearmand:
container_name: yii2_queue_gearmand
image: artefactual/gearmand
ports:
- 4730:4730
- 4731:4730
networks:
net: {}

networks:
net:
name: yii2_queue_net

Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ RUN apt-get update \
COPY . /code
WORKDIR /code

ENTRYPOINT ["tests/app/docker/php/entrypoint.sh"]
ENTRYPOINT ["tests/docker/php/entrypoint.sh"]
CMD ["sleep", "infinity"]
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ RUN apt-get update \
COPY . /code
WORKDIR /code

ENTRYPOINT ["tests/app/docker/php/entrypoint.sh"]
ENTRYPOINT ["tests/docker/php/entrypoint.sh"]
CMD ["sleep", "infinity"]
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ RUN apt-get update \
COPY . /code
WORKDIR /code

ENTRYPOINT ["tests/app/docker/php/entrypoint.sh"]
ENTRYPOINT ["tests/docker/php/entrypoint.sh"]
CMD ["sleep", "infinity"]
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ RUN apt-get update \
COPY . /code
WORKDIR /code

ENTRYPOINT ["tests/app/docker/php/entrypoint.sh"]
ENTRYPOINT ["tests/docker/php/entrypoint.sh"]
CMD ["sleep", "infinity"]
13 changes: 13 additions & 0 deletions tests/docker/php/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/sh

flock tests/runtime/composer-install.lock composer install --prefer-dist --no-interaction \
&& tests/yii sqlite-migrate/up --interactive=0 \
&& tests/docker/wait-for-it.sh $MYSQL_HOST:$MYSQL_PORT -t 180 \
&& tests/docker/php/mysql-lock.php tests/yii mysql-migrate/up --interactive=0 \
&& tests/docker/wait-for-it.sh $POSTGRES_HOST:$POSTGRES_PORT -t 180 \
&& tests/docker/php/mysql-lock.php tests/yii pgsql-migrate/up --interactive=0 \
&& tests/docker/wait-for-it.sh $REDIS_HOST:$REDIS_PORT -t 180 \
&& tests/docker/wait-for-it.sh $RABBITMQ_HOST:$RABBITMQ_PORT -t 180 \
&& tests/docker/wait-for-it.sh $BEANSTALK_HOST:$BEANSTALK_PORT -t 180 \
&& tests/docker/wait-for-it.sh $GEARMAN_HOST:$GEARMAN_PORT -t 180 \
&& exec "$@"
47 changes: 47 additions & 0 deletions tests/docker/php/mysql-lock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/env php
<?php
/**
* Запуск команды с сетевой синхронизацией через MySQL.
*
* Когда запускается сеть из docker-контейнеров каждый php-контейнер в числе
* прочих запускает команду миграции БД. И, чтобы исключить высокую вероятность,
* запуска нескольких таких процессов одновременно, используется синхронизация
* на уровне блокировок MySQL. Это гарантирует, что одновременно будет работать
* только одна из запущенных команд, а остальные будут ждать завершения.
*
* @author Roman Zhuravlev <[email protected]>
*/

$params = $_SERVER['argv'];
array_shift($params);
$command = implode(' ', $params);

$mysql = new PDO(
sprintf(
'mysql:host=%s;port=%s;dbname=%s',
getenv('MYSQL_HOST'),
getenv('MYSQL_PORT'),
getenv('MYSQL_DATABASE')
),
getenv('MYSQL_USER'),
getenv('MYSQL_PASSWORD')
);

// Waiting a lock for the command
$query = $mysql->prepare('SELECT GET_LOCK(?, -1)');
$query->execute([md5($command)]);
if (!$query->fetch(PDO::FETCH_NUM)[0]) {
throw new Exception('Cannot get the lock.');
}

// Executes the command
passthru($command, $exitCode);

// Releases the lock
$query = $mysql->prepare('SELECT RELEASE_LOCK(?)');
$query->execute([md5($command)]);
if (!$query->fetch(PDO::FETCH_NUM)[0]) {
throw new Exception('Cannot release the lock.');
}

exit($exitCode);
File renamed without changes.
18 changes: 0 additions & 18 deletions tests/run-tests.sh

This file was deleted.

File renamed without changes.

0 comments on commit 2e1c37b

Please sign in to comment.