Skip to content

Commit 2e1c37b

Browse files
committed
Test environment
1 parent 1e7cbf1 commit 2e1c37b

17 files changed

+180
-82
lines changed

.dockerignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@
88
/.php_cs.cache
99
/.travis.yml
1010
/composer.lock
11-
/tests/app/runtime
11+
/tests/runtime/*
1212
/vendor

.editorconfig

+3
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@ max_line_length = 80
1313

1414
[*.md]
1515
trim_trailing_whitespace = false
16+
17+
[Makefile]
18+
indent_style = tab

phpunit.xml.dist

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
<testsuite name="Test Suite">
1010
<directory>./tests</directory>
1111
<exclude>./tests/app</exclude>
12+
<exclude>./tests/docker</exclude>
13+
<exclude>./tests/runtime</exclude>
1214
</testsuite>
1315
</testsuites>
1416
</phpunit>

tests/Makefile

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
init: clean build
2+
docker-compose run php72 true
3+
docker-compose down
4+
5+
test: test-php72 test-php71 test-php70 test-php56
6+
test-php72:
7+
docker-compose run php72 vendor/bin/phpunit --verbose
8+
docker-compose down
9+
test-php71:
10+
docker-compose run php71 vendor/bin/phpunit --verbose
11+
docker-compose down
12+
test-php70:
13+
docker-compose run php70 vendor/bin/phpunit --verbose
14+
docker-compose down
15+
test-php56:
16+
docker-compose run php56 vendor/bin/phpunit --verbose
17+
docker-compose down
18+
19+
benchmark: benchmark-php72 benchmark-php71 benchmark-php70 benchmark-php56
20+
benchmark-php72:
21+
docker-compose run php72 tests/yii benchmark/waiting
22+
docker-compose down
23+
benchmark-php71:
24+
docker-compose run php71 tests/yii benchmark/waiting
25+
docker-compose down
26+
benchmark-php70:
27+
docker-compose run php70 tests/yii benchmark/waiting
28+
docker-compose down
29+
benchmark-php56:
30+
docker-compose run php56 tests/yii benchmark/waiting
31+
docker-compose down
32+
33+
check-cs:
34+
docker-compose run php72 php-cs-fixer fix --diff --dry-run
35+
docker-compose down
36+
37+
build:
38+
docker-compose pull
39+
docker-compose build
40+
41+
clean:
42+
docker-compose down
43+
sudo rm -rf runtime/*
44+
sudo rm -f ../.php_cs.cache
45+
sudo rm -f ../composer.lock
46+
sudo rm -rf ../vendor/

tests/app/config/main.php

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
'id' => 'yii2-queue-app',
44
'basePath' => dirname(__DIR__),
55
'vendorPath' => dirname(dirname(__DIR__)) . '/vendor',
6+
'runtimePath' => dirname(dirname(__DIR__)) . '/runtime',
67
'bootstrap' => [
78
'fileQueue',
89
'mysqlQueue',

tests/app/docker/mysql/8.0/auth.cnf

-2
This file was deleted.

tests/app/docker/php/entrypoint.sh

-13
This file was deleted.

tests/docker-compose.yml

+59-44
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
1-
version: "3"
1+
version: "3.5"
22
services:
33

44
# https://docs.docker.com/samples/library/php/
55
php72:
66
container_name: yii2_queue_php72
77
build:
8-
context: .
9-
dockerfile: tests/app/docker/php/7.2/Dockerfile
10-
command: &php_command ["sleep", "infinity"]
8+
context: ..
9+
dockerfile: tests/docker/php/7.2/Dockerfile
10+
volumes:
11+
- ./runtime/composer72:/root/.composer
12+
- ..:/code
1113
dns: &php_dns
12-
- 8.8.8.8
13-
- 4.4.4.4
14-
volumes: &php_volumes
15-
#- .:/code
16-
- ./tests/app/runtime/composer:/root/.composer
14+
- 8.8.8.8
15+
- 4.4.4.4
1716
environment: &php_environment
1817
MYSQL_HOST: mysql
1918
MYSQL_PORT: 3306
@@ -37,99 +36,115 @@ services:
3736
GEARMAN_HOST: gearmand
3837
GEARMAN_PORT: 4730
3938
depends_on: &php_depends_on
40-
- mysql
41-
- postgres
42-
- redis
43-
- rabbitmq
44-
- beanstalk
45-
- gearmand
39+
- mysql
40+
- postgres
41+
- redis
42+
- rabbitmq
43+
- beanstalk
44+
- gearmand
45+
networks:
46+
net: {}
4647
php71:
4748
container_name: yii2_queue_php71
4849
build:
49-
context: .
50-
dockerfile: tests/app/docker/php/7.1/Dockerfile
51-
command: *php_command
50+
context: ..
51+
dockerfile: tests/docker/php/7.1/Dockerfile
52+
volumes:
53+
- ./runtime/composer71:/root/.composer
5254
dns: *php_dns
53-
volumes: *php_volumes
5455
environment: *php_environment
5556
depends_on: *php_depends_on
57+
networks:
58+
net: {}
5659
php70:
5760
container_name: yii2_queue_php70
5861
build:
59-
context: .
60-
dockerfile: tests/app/docker/php/7.0/Dockerfile
61-
command: *php_command
62+
context: ..
63+
dockerfile: tests/docker/php/7.0/Dockerfile
64+
volumes:
65+
- ./runtime/composer70:/root/.composer
6266
dns: *php_dns
63-
volumes: *php_volumes
6467
environment: *php_environment
6568
depends_on: *php_depends_on
69+
networks:
70+
net: {}
6671
php56:
6772
container_name: yii2_queue_php56
6873
build:
69-
context: .
70-
dockerfile: tests/app/docker/php/5.6/Dockerfile
71-
command: *php_command
74+
context: ..
75+
dockerfile: tests/docker/php/5.6/Dockerfile
76+
volumes:
77+
- ./runtime/composer56:/root/.composer
7278
dns: *php_dns
73-
volumes: *php_volumes
7479
environment: *php_environment
7580
depends_on: *php_depends_on
81+
networks:
82+
net: {}
7683

7784
# https://docs.docker.com/samples/library/mysql/
7885
mysql:
79-
container_name: yii2-queue-mysql
80-
image: mysql:8.0
86+
container_name: yii2_queue_mysql
87+
image: mysql:5.7
8188
ports:
82-
- 3306:3306
83-
volumes:
84-
- ./tests/app/docker/mysql/8.0/auth.cnf:/etc/mysql/conf.d/auth.cnf:ro
85-
#- ./tests/app/runtime/mysql:/var/lib/mysql
89+
- 3307:3306
8690
environment:
8791
MYSQL_ALLOW_EMPTY_PASSWORD: 1
8892
MYSQL_USER: yii2_queue_test
8993
MYSQL_PASSWORD: yii2_queue_test
9094
MYSQL_DATABASE: yii2_queue_test
95+
networks:
96+
net: {}
9197

9298
# https://docs.docker.com/samples/library/postgres/
9399
postgres:
94100
container_name: yii2_queue_postgres
95101
image: postgres:10.4
96102
ports:
97-
- 5432:5432
98-
#volumes:
99-
# - ./tests/app/runtime/postgres:/var/lib/postgresql/data
103+
- 5433:5432
100104
environment:
101105
POSTGRES_USER: yii2_queue_test
102106
POSTGRES_PASSWORD: yii2_queue_test
103107
POSTGRES_DB: yii2_queue_test
108+
networks:
109+
net: {}
104110

105111
# https://docs.docker.com/samples/library/redis/
106112
redis:
107113
container_name: yii2_queue_redis
108114
image: redis:4.0
109115
ports:
110-
- 6379:6379
111-
#volumes:
112-
# - ./tests/app/runtime/redis:/data
116+
- 6380:6379
117+
networks:
118+
net: {}
113119

114120
# https://docs.docker.com/samples/library/rabbitmq/
115121
rabbitmq:
116122
container_name: yii2_queue_rabbitmq
117123
image: rabbitmq:3.7
118124
ports:
119-
- 5672:5672
120-
#volumes:
121-
# - ./tests/app/runtime/rabbitmq:/var/lib/rabbitmq
125+
- 5673:5672
126+
networks:
127+
net: {}
122128

123129
# https://hub.docker.com/r/schickling/beanstalkd/
124130
beanstalk:
125131
container_name: yii2_queue_beanstalk
126132
image: schickling/beanstalkd
127133
ports:
128-
- 11300:11300
134+
- 11301:11300
135+
networks:
136+
net: {}
129137

130138
# https://hub.docker.com/r/artefactual/gearmand/
131139
gearmand:
132140
container_name: yii2_queue_gearmand
133141
image: artefactual/gearmand
134142
ports:
135-
- 4730:4730
143+
- 4731:4730
144+
networks:
145+
net: {}
146+
147+
networks:
148+
net:
149+
name: yii2_queue_net
150+

tests/app/docker/php/5.6/Dockerfile tests/docker/php/5.6/Dockerfile

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,5 @@ RUN apt-get update \
2727
COPY . /code
2828
WORKDIR /code
2929

30-
ENTRYPOINT ["tests/app/docker/php/entrypoint.sh"]
30+
ENTRYPOINT ["tests/docker/php/entrypoint.sh"]
31+
CMD ["sleep", "infinity"]

tests/app/docker/php/7.0/Dockerfile tests/docker/php/7.0/Dockerfile

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,5 @@ RUN apt-get update \
3535
COPY . /code
3636
WORKDIR /code
3737

38-
ENTRYPOINT ["tests/app/docker/php/entrypoint.sh"]
38+
ENTRYPOINT ["tests/docker/php/entrypoint.sh"]
39+
CMD ["sleep", "infinity"]

tests/app/docker/php/7.1/Dockerfile tests/docker/php/7.1/Dockerfile

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,5 @@ RUN apt-get update \
3535
COPY . /code
3636
WORKDIR /code
3737

38-
ENTRYPOINT ["tests/app/docker/php/entrypoint.sh"]
38+
ENTRYPOINT ["tests/docker/php/entrypoint.sh"]
39+
CMD ["sleep", "infinity"]

tests/app/docker/php/7.2/Dockerfile tests/docker/php/7.2/Dockerfile

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,5 @@ RUN apt-get update \
3535
COPY . /code
3636
WORKDIR /code
3737

38-
ENTRYPOINT ["tests/app/docker/php/entrypoint.sh"]
38+
ENTRYPOINT ["tests/docker/php/entrypoint.sh"]
39+
CMD ["sleep", "infinity"]

tests/docker/php/entrypoint.sh

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/sh
2+
3+
flock tests/runtime/composer-install.lock composer install --prefer-dist --no-interaction \
4+
&& tests/yii sqlite-migrate/up --interactive=0 \
5+
&& tests/docker/wait-for-it.sh $MYSQL_HOST:$MYSQL_PORT -t 180 \
6+
&& tests/docker/php/mysql-lock.php tests/yii mysql-migrate/up --interactive=0 \
7+
&& tests/docker/wait-for-it.sh $POSTGRES_HOST:$POSTGRES_PORT -t 180 \
8+
&& tests/docker/php/mysql-lock.php tests/yii pgsql-migrate/up --interactive=0 \
9+
&& tests/docker/wait-for-it.sh $REDIS_HOST:$REDIS_PORT -t 180 \
10+
&& tests/docker/wait-for-it.sh $RABBITMQ_HOST:$RABBITMQ_PORT -t 180 \
11+
&& tests/docker/wait-for-it.sh $BEANSTALK_HOST:$BEANSTALK_PORT -t 180 \
12+
&& tests/docker/wait-for-it.sh $GEARMAN_HOST:$GEARMAN_PORT -t 180 \
13+
&& exec "$@"

tests/docker/php/mysql-lock.php

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/usr/bin/env php
2+
<?php
3+
/**
4+
* Запуск команды с сетевой синхронизацией через MySQL.
5+
*
6+
* Когда запускается сеть из docker-контейнеров каждый php-контейнер в числе
7+
* прочих запускает команду миграции БД. И, чтобы исключить высокую вероятность,
8+
* запуска нескольких таких процессов одновременно, используется синхронизация
9+
* на уровне блокировок MySQL. Это гарантирует, что одновременно будет работать
10+
* только одна из запущенных команд, а остальные будут ждать завершения.
11+
*
12+
* @author Roman Zhuravlev <[email protected]>
13+
*/
14+
15+
$params = $_SERVER['argv'];
16+
array_shift($params);
17+
$command = implode(' ', $params);
18+
19+
$mysql = new PDO(
20+
sprintf(
21+
'mysql:host=%s;port=%s;dbname=%s',
22+
getenv('MYSQL_HOST'),
23+
getenv('MYSQL_PORT'),
24+
getenv('MYSQL_DATABASE')
25+
),
26+
getenv('MYSQL_USER'),
27+
getenv('MYSQL_PASSWORD')
28+
);
29+
30+
// Waiting a lock for the command
31+
$query = $mysql->prepare('SELECT GET_LOCK(?, -1)');
32+
$query->execute([md5($command)]);
33+
if (!$query->fetch(PDO::FETCH_NUM)[0]) {
34+
throw new Exception('Cannot get the lock.');
35+
}
36+
37+
// Executes the command
38+
passthru($command, $exitCode);
39+
40+
// Releases the lock
41+
$query = $mysql->prepare('SELECT RELEASE_LOCK(?)');
42+
$query->execute([md5($command)]);
43+
if (!$query->fetch(PDO::FETCH_NUM)[0]) {
44+
throw new Exception('Cannot release the lock.');
45+
}
46+
47+
exit($exitCode);
File renamed without changes.

tests/run-tests.sh

-18
This file was deleted.
File renamed without changes.

0 commit comments

Comments
 (0)