Skip to content

Commit

Permalink
Add health check for redmine instances, simplify supported redmine ve…
Browse files Browse the repository at this point in the history
…rsions
  • Loading branch information
Art4 committed Jul 1, 2024
1 parent a7d4d62 commit d3f4e43
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 26 deletions.
1 change: 0 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,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->runHeathChecks($version);

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

private function runHeathChecks(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 running 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 d3f4e43

Please sign in to comment.