Skip to content

Commit aba52e4

Browse files
authored
Merge pull request #127 from mermshaus/update-tests-php8
Update tests to work in PHP 8 Docker Compose setup
2 parents 79e0bdf + c099a45 commit aba52e4

File tree

11 files changed

+152
-78
lines changed

11 files changed

+152
-78
lines changed

.github/workflows/ci.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,22 +80,27 @@ jobs:
8080
- name: Run UnitTests
8181
if: matrix.ssl_test == 'no'
8282
run: ./vendor/bin/phpunit
83+
env:
84+
SSL_TEST: "no"
85+
TEST_RABBITMQ_CONNECTION_URI: "amqp://guest:guest@localhost:5672/"
8386
- name: Run UnitTests
8487
if: matrix.ssl_test == 'yes'
8588
run: ./vendor/bin/phpunit
8689
env:
8790
SSL_TEST: "yes"
8891
SSL_CA: "ssl/ca.pem"
8992
SSL_PEER_NAME: "server.rmq"
93+
TEST_RABBITMQ_CONNECTION_URI: "amqp://guest:guest@localhost:5672/"
9094
- name: Run UnitTests
9195
if: matrix.ssl_test == 'client'
9296
run: ./vendor/bin/phpunit
9397
env:
94-
SSL_TEST: "yes"
98+
SSL_TEST: "client"
9599
SSL_CA: "ssl/ca.pem"
96100
SSL_PEER_NAME: "server.rmq"
97101
SSL_CLIENT_CERT: "ssl/client.pem"
98102
SSL_CLIENT_KEY: "ssl/client.key"
103+
TEST_RABBITMQ_CONNECTION_URI: "amqp://guest:guest@localhost:5672/"
99104
- name: Docker Logs
100105
if: ${{ failure() }}
101106
run: docker logs rabbitmq

README.md

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -211,18 +211,14 @@ There is [amqp interop](https://github.com/queue-interop/amqp-interop) compatibl
211211

212212
## Testing
213213

214-
You need access to a RabbitMQ instance to run the test suite. You can either connect to an existing instance or use the
215-
provided Docker Compose setup to create an isolated environment, including a RabbitMQ container, to run the test suite
216-
in.
214+
Create client/server SSL certificates by running:
217215

218-
**Local RabbitMQ**
216+
```
217+
$ cd test/ssl && make all && cd -
218+
```
219219

220-
- Change `TEST_RABBITMQ_CONNECTION_URI` in `phpunit.xml` to fit your environment. Then run:
220+
You need access to a RabbitMQ instance in order to run the test suite. The easiest way is to use the provided Docker Compose setup to create an isolated environment, including a RabbitMQ container, to run the test suite in.
221221

222-
```
223-
$ vendor/bin/phpunit
224-
```
225-
226222
**Docker Compose**
227223

228224
- Use Docker Compose to create a network with a RabbitMQ container and a PHP container to run the tests in. The project

docker-compose.yml

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,19 @@ services:
1818
- main
1919
hostname: rabbit_node_1
2020
ports:
21-
- "15672:15672"
2221
- "5672:5672"
22+
- "5673:5673"
23+
- "15672:15672"
2324
tty: true
2425
bunny:
2526
build: docker/bunny
26-
init: true
2727
environment:
28-
SSL_TEST: 'yes'
29-
SSL_CA: ssl/ca.pem
30-
SSL_PEER_NAME: server.rmq
31-
SSL_CLIENT_CERT: ssl/client.pem
32-
SSL_CLIENT_KEY: ssl/client.key
28+
SSL_TEST: "client"
29+
SSL_CA: "ssl/ca.pem"
30+
SSL_CLIENT_CERT: "ssl/client.pem"
31+
SSL_CLIENT_KEY: "ssl/client.key"
32+
SSL_PEER_NAME: "server.rmq"
33+
TEST_RABBITMQ_CONNECTION_URI: "amqp://testuser:testpassword@bunny_rabbit_node_1_1:5672/testvhost"
3334
volumes:
3435
- .:/opt/bunny
3536
networks:

docker/bunny/Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM php:7.4-cli
1+
FROM php:8.1-cli
22

33
RUN apt-get update \
44
&& apt-get dist-upgrade -y \
@@ -10,6 +10,7 @@ RUN apt-get update \
1010

1111
RUN pecl install xdebug \
1212
&& docker-php-ext-enable xdebug
13+
RUN echo "xdebug.mode=coverage" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
1314

1415
RUN curl --silent --show-error https://getcomposer.org/installer | php
1516
RUN mv composer.phar /usr/local/bin/composer

docker/rabbitmq/entrypoint.sh

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,37 @@
11
#!/bin/bash
22

3-
set -eu
3+
set -eux
44

55
is_rabbitmq_gte_3_7_0() {
66
[[ "3.7.0" = $(echo -e "3.7.0\n$RABBITMQ_VERSION" | sort -V | head -n1) ]]
77
}
88

9+
install_certs() {
10+
local certs_dir="/rabbitmq-certs"
11+
12+
rm -rf "${certs_dir}"
13+
mkdir "${certs_dir}"
14+
cp "${TEST_DATA_ROOT}"/{ca.pem,server.pem,server.key} "${certs_dir}"
15+
chown rabbitmq "${certs_dir}"/{ca.pem,server.pem,server.key}
16+
chmod 0400 "${certs_dir}"/{ca.pem,server.pem,server.key}
17+
}
18+
19+
install_config() {
20+
if [[ -n "$CONFIG_NAME" ]]; then
21+
if is_rabbitmq_gte_3_7_0; then
22+
cp "${TEST_DATA_ROOT}/${CONFIG_NAME}.conf" /etc/rabbitmq/rabbitmq.conf
23+
chown rabbitmq /etc/rabbitmq/rabbitmq.conf
24+
else
25+
cp "${TEST_DATA_ROOT}/${CONFIG_NAME}.config" /etc/rabbitmq/rabbitmq.config
26+
chown rabbitmq /etc/rabbitmq/rabbitmq.config
27+
fi
28+
fi
29+
}
30+
931
TEST_DATA_ROOT=/opt/bunny/test/ssl
1032
CONFIG_NAME=${CONFIG_NAME:-}
1133

12-
cp ${TEST_DATA_ROOT}/{ca.pem,server.pem,server.key} /etc/rabbitmq/
13-
chown rabbitmq /etc/rabbitmq/{ca.pem,server.pem,server.key}
14-
chmod 0400 /etc/rabbitmq/{ca.pem,server.pem,server.key}
15-
16-
if [[ -n "$CONFIG_NAME" ]]; then
17-
if is_rabbitmq_gte_3_7_0; then
18-
cp ${TEST_DATA_ROOT}/${CONFIG_NAME}.conf /etc/rabbitmq/rabbitmq.conf
19-
chown rabbitmq /etc/rabbitmq/rabbitmq.conf
20-
else
21-
cp ${TEST_DATA_ROOT}/${CONFIG_NAME}.config /etc/rabbitmq/rabbitmq.config
22-
chown rabbitmq /etc/rabbitmq/rabbitmq.config
23-
fi
24-
fi
34+
install_certs
35+
install_config
2536

2637
exec "$@"

phpunit.xml

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,13 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<phpunit
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/6.5/phpunit.xsd"
5-
bootstrap="vendor/autoload.php"
6-
colors="true">
7-
8-
<testsuites>
9-
<testsuite name="Tests">
10-
<directory suffix="Test.php">test/Bunny</directory>
11-
</testsuite>
12-
</testsuites>
13-
14-
<filter>
15-
<whitelist processUncoveredFilesFromWhitelist="true">
16-
<directory suffix=".php">src</directory>
17-
</whitelist>
18-
</filter>
19-
20-
<php>
21-
<!-- This is the default configuration when running the test suite
22-
through the docker-compose setup -->
23-
<env name="TEST_RABBITMQ_CONNECTION_URI" value="amqp://guest:guest@localhost:5672/"/>
24-
25-
<!-- RabbitMQ running on localhost with default user (guest:guest) and
26-
vhost "/" would be (slashes in the actual vhost name need to be
27-
percent-encoded, see https://www.rabbitmq.com/uri-spec.html): -->
28-
<!--<env name="TEST_RABBITMQ_CONNECTION_URI" value="amqp://localhost:5672/%2f"/>-->
29-
</php>
30-
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.5/phpunit.xsd" bootstrap="vendor/autoload.php" colors="true">
3+
<coverage processUncoveredFiles="true">
4+
<include>
5+
<directory suffix=".php">src</directory>
6+
</include>
7+
</coverage>
8+
<testsuites>
9+
<testsuite name="Tests">
10+
<directory suffix="Test.php">test/Bunny</directory>
11+
</testsuite>
12+
</testsuites>
3113
</phpunit>

test/Bunny/SSLTest.php

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
use Bunny\Exception\ClientException;
77
use Bunny\Test\Exception\TimeoutException;
88
use Bunny\Test\Library\AsynchronousClientHelper;
9+
use Bunny\Test\Library\Environment;
910
use Bunny\Test\Library\SynchronousClientHelper;
1011
use PHPUnit\Framework\TestCase;
1112

1213
use React\EventLoop\Factory;
1314

1415
use function dirname;
1516
use function file_exists;
16-
use function getenv;
1717
use function is_file;
1818
use function putenv;
1919

@@ -78,7 +78,9 @@ public function testConnectWithMissingClientCert()
7878
// let's try without client certificate - it should fail
7979
unset($options['ssl']['local_cert'], $options['ssl']['local_pk']);
8080

81-
$this->expectException(ClientException::class);
81+
if (Environment::getSslTest() === 'client') {
82+
$this->expectException(ClientException::class);
83+
}
8284

8385
$client = $this->helper->createClient($options);
8486
$client->connect();
@@ -112,26 +114,20 @@ public function testConnectWithWrongPeerName()
112114
protected function getOptions()
113115
{
114116
// should we do SSL-tests
115-
if (empty(getenv('SSL_TEST'))) {
116-
$this->markTestSkipped('Skipped due empty ENV-variable "SSL_TEST"');
117+
if (!in_array(Environment::getSslTest(), ['yes', 'client'], true)) {
118+
$this->markTestSkipped('Skipped because env var SSL_TEST not set to "yes" or "client"');
117119
}
118120

119121
// checking CA-file
120-
$caFile = getenv('SSL_CA');
121-
if (empty($caFile)) {
122-
$this->fail('Missing CA file ENV-variable: "SSL_CA"');
123-
}
122+
$caFile = Environment::getSslCa();
123+
124124
$testsDir = dirname(__DIR__);
125125
$caFile = $testsDir . '/' . $caFile;
126126
if (!file_exists($caFile) || !is_file($caFile)) {
127127
$this->fail('Missing CA file: "' . $caFile . '"');
128128
}
129129

130-
$peerName = getenv('SSL_PEER_NAME');
131-
if (empty($peerName)) {
132-
// setting default value from tests/ssl/Makefile
133-
$peerName = 'server.rmq';
134-
}
130+
$peerName = Environment::getSslPeerName();
135131

136132
// minimal SSL-options
137133
$options = [
@@ -145,8 +141,9 @@ protected function getOptions()
145141
];
146142

147143

148-
$certFile = getenv('SSL_CLIENT_CERT');
149-
$keyFile = getenv('SSL_CLIENT_KEY');
144+
$certFile = Environment::getSslClientCert();
145+
$keyFile = Environment::getSslClientKey();
146+
150147
if (!empty($certFile) && !empty($keyFile)) {
151148
$certFile = $testsDir . '/' . $certFile;
152149
$keyFile = $testsDir . '/' . $keyFile;
@@ -159,6 +156,7 @@ protected function getOptions()
159156
$options['ssl']['local_cert'] = $certFile;
160157
$options['ssl']['local_pk'] = $keyFile;
161158
}
159+
162160
return $options;
163161
}
164162
}

test/Library/AsynchronousClientHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function getDefaultOptions(): array
2929
{
3030
$options = [];
3131

32-
$options = array_merge($options, $this->parseAmqpUri(getenv('TEST_RABBITMQ_CONNECTION_URI')));
32+
$options = array_merge($options, $this->parseAmqpUri(Environment::getTestRabbitMqConnectionUri()));
3333

3434
return $options;
3535
}

test/Library/Environment.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Bunny\Test\Library;
6+
7+
final class Environment
8+
{
9+
public static function getSslCa(): string
10+
{
11+
return trim(self::getenv('SSL_CA'));
12+
}
13+
14+
public static function getSslClientCert(): string
15+
{
16+
return trim(self::getenv('SSL_CLIENT_CERT', ''));
17+
}
18+
19+
public static function getSslClientKey(): string
20+
{
21+
return trim(self::getenv('SSL_CLIENT_KEY', ''));
22+
}
23+
24+
public static function getSslPeerName(): string
25+
{
26+
return trim(self::getenv('SSL_PEER_NAME'));
27+
}
28+
29+
/**
30+
* Can have the following values:
31+
*
32+
* - "client" -> run all SSL tests, expect peer cert (c.f. rabbitmq.ssl.verify_peer.conf)
33+
* - "yes" -> run all SSL tests, do *not* expect peer cert (c.f. rabbitmq.ssl.verify_none.conf)
34+
* - "no" (default) -> skip SSL-related tests
35+
*/
36+
public static function getSslTest(): string
37+
{
38+
$value = self::getenv('SSL_TEST', 'no');
39+
40+
switch ($value) {
41+
case 'client':
42+
case 'no':
43+
case 'yes':
44+
return $value;
45+
}
46+
47+
throw new EnvironmentException('SSL_TEST');
48+
}
49+
50+
public static function getTestRabbitMqConnectionUri(): string
51+
{
52+
return trim(self::getenv('TEST_RABBITMQ_CONNECTION_URI'));
53+
}
54+
55+
private static function getenv(string $envVariable, ?string $default = null):string
56+
{
57+
$value = getenv($envVariable);
58+
59+
if ($value === false && $default === null) {
60+
throw new EnvironmentException($envVariable);
61+
}
62+
63+
return $value!==false?$value:$default;
64+
}
65+
}

test/Library/EnvironmentException.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Bunny\Test\Library;
4+
5+
class EnvironmentException extends \RuntimeException
6+
{
7+
public function __construct(string $envVariable)
8+
{
9+
$value = getenv($envVariable);
10+
11+
$valueFormatted = $value===false?'false': '"' . $value . '"';
12+
13+
parent::__construct(sprintf('Invalid value for env var %s: %s', $envVariable, $valueFormatted), 1);
14+
}
15+
}

test/Library/SynchronousClientHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function getDefaultOptions(): array
7373
{
7474
$options = [];
7575

76-
$options = array_merge($options, $this->parseAmqpUri(getenv('TEST_RABBITMQ_CONNECTION_URI')));
76+
$options = array_merge($options, $this->parseAmqpUri(Environment::getTestRabbitMqConnectionUri()));
7777

7878
return $options;
7979
}

0 commit comments

Comments
 (0)