Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add health check for redmine services #410

Merged
3 changes: 0 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
version: "3.7"

services:
php:
build: # Info to build the Docker image
Expand All @@ -25,7 +23,6 @@ services:
- ./.docker/redmine-dev_data/sqlite:/usr/src/redmine/sqlite

# Make sure the following services are configured in:
# - /tests/RedmineExtension/RedmineInstance.php
# - /tests/Behat/behat.yml

redmine-50103:
Expand Down
8 changes: 0 additions & 8 deletions tests/RedmineExtension/BehatHookTracer.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,6 @@ final class BehatHookTracer implements InstanceRegistration
*/
private static array $instances = [];

/**
* @return RedmineVersion[]
*/
public static function getSupportedRedmineVersions(): array
{
return RedmineInstance::getSupportedVersions();
}

public static function getRedmineInstance(RedmineVersion $redmineVersion): RedmineInstance
{
if (static::$tracer === null) {
Expand Down
46 changes: 29 additions & 17 deletions tests/RedmineExtension/RedmineInstance.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,11 @@

final class RedmineInstance
{
/**
* Make sure that supported versions have a service in /docker-composer.yml
* and are configured in /tests/Behat/behat.yml
*/
public static function getSupportedVersions(): array
{
return [
RedmineVersion::V5_1_3,
RedmineVersion::V5_0_9,
RedmineVersion::V4_2_10,
];
}

/**
* @param InstanceRegistration $tracer Required to ensure that RedmineInstance is created while Test Runner is running
*/
public static function create(InstanceRegistration $tracer, RedmineVersion $version): void
{
if (! in_array($version, static::getSupportedVersions())) {
throw new InvalidArgumentException('Redmine ' . $version->asString() . ' is not supported.');
}

$tracer->registerInstance(new self($tracer, $version));
}

Expand Down Expand Up @@ -77,6 +60,8 @@ private function __construct(InstanceRegistration $tracer, RedmineVersion $versi
$this->redmineUrl = 'http://redmine-' . $versionId . ':3000';
$this->apiKey = sha1($versionId . (string) time());

$this->runHealthChecks($version);

$this->createDatabaseBackup();
$this->createFilesBackup();
$this->runDatabaseMigration();
Expand Down Expand Up @@ -104,6 +89,33 @@ public function getApiKey(): string
return $this->apiKey;
}

private function runHealthChecks(RedmineVersion $version): void
{
$ch = curl_init($this->redmineUrl);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

if ($data === false || $statusCode !== 200) {
throw new InvalidArgumentException(sprintf(
'Could not connect to Redmine server at %s, please make sure that Redmine %s has a docker service in /docker-composer.yml and is correctly configured in /tests/Behat/behat.yml.',
$this->redmineUrl,
$version->asString(),
));
}

if (! file_exists($this->rootPath . $this->workingDB)) {
throw new InvalidArgumentException(sprintf(
'Could not find database file in %s, please make sure that Redmine %s has a docker service in /docker-composer.yml and is correctly configured in /tests/Behat/behat.yml.',
$this->rootPath . $this->workingDB,
$version->asString(),
));
}
}

public function reset(InstanceRegistration $tracer): void
{
if ($tracer !== $this->tracer) {
Expand Down
Loading