Skip to content

Commit

Permalink
Merge pull request #410 from Art4/add-redmine-server-health-check-for…
Browse files Browse the repository at this point in the history
…-redmine-services

Add health check for redmine services
  • Loading branch information
Art4 authored Jul 3, 2024
2 parents 066212f + f93c4fc commit a3c0dd7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 28 deletions.
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

0 comments on commit a3c0dd7

Please sign in to comment.