mirror of
https://github.com/stan220/testcontainers-php.git
synced 2026-09-08 17:29:29 +00:00
Fixes in WaitForHealthCheck params and legacy tests
This commit is contained in:
@@ -4,6 +4,8 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Testcontainers\Container;
|
namespace Testcontainers\Container;
|
||||||
|
|
||||||
|
use Testcontainers\Utils\PortGenerator\FixedPortGenerator;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Added for backward compatibility.
|
* Added for backward compatibility.
|
||||||
* @deprecated Use GenericContainer instead.
|
* @deprecated Use GenericContainer instead.
|
||||||
@@ -52,6 +54,7 @@ class Container extends GenericContainer
|
|||||||
*/
|
*/
|
||||||
public function withPort(string $localPort, string $containerPort): self
|
public function withPort(string $localPort, string $containerPort): self
|
||||||
{
|
{
|
||||||
|
$this->withPortGenerator(new FixedPortGenerator([(int)$localPort]));
|
||||||
return $this->withExposedPorts($containerPort);
|
return $this->withExposedPorts($containerPort);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -123,12 +123,19 @@ class GenericContainer implements TestContainer
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function withHealthCheckCommand(string $command, int $healthCheckIntervalInMS = 1000): static
|
public function withHealthCheckCommand(
|
||||||
{
|
string $command,
|
||||||
$this->healthConfig = new HealthConfig([
|
int $intervalInMilliseconds = 1000,
|
||||||
'Test' => ['CMD', $command],
|
int $timeoutInMilliseconds = 3000,
|
||||||
'Interval' => $healthCheckIntervalInMS,
|
int $retries = 3,
|
||||||
]);
|
int $startPeriodInMilliseconds = 0
|
||||||
|
): static {
|
||||||
|
$this->healthConfig = new HealthConfig();
|
||||||
|
$this->healthConfig->setTest(['CMD-SHELL', $command]);
|
||||||
|
$this->healthConfig->setInterval($intervalInMilliseconds * 1_000_000);
|
||||||
|
$this->healthConfig->setTimeout($timeoutInMilliseconds * 1_000_000);
|
||||||
|
$this->healthConfig->setRetries($retries);
|
||||||
|
$this->healthConfig->setStartPeriod($startPeriodInMilliseconds * 1_000_000);
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,7 +8,8 @@ use PHPUnit\Framework\TestCase;
|
|||||||
use Predis\Client;
|
use Predis\Client;
|
||||||
use Predis\Connection\ConnectionException;
|
use Predis\Connection\ConnectionException;
|
||||||
use Testcontainers\Container\Container;
|
use Testcontainers\Container\Container;
|
||||||
use Testcontainers\Exception\ContainerNotReadyException;
|
use Testcontainers\Container\MySQLContainer;
|
||||||
|
use Testcontainers\Container\RedisContainer;
|
||||||
use Testcontainers\Wait\WaitForExec;
|
use Testcontainers\Wait\WaitForExec;
|
||||||
use Testcontainers\Wait\WaitForHealthCheck;
|
use Testcontainers\Wait\WaitForHealthCheck;
|
||||||
use Testcontainers\Wait\WaitForHttp;
|
use Testcontainers\Wait\WaitForHttp;
|
||||||
@@ -28,7 +29,7 @@ class WaitStrategyTest extends TestCase
|
|||||||
|
|
||||||
public function testWaitForExec(): void
|
public function testWaitForExec(): void
|
||||||
{
|
{
|
||||||
$container = Container::make('mysql')
|
$container = MySQLContainer::make()
|
||||||
->withEnvironment('MYSQL_ROOT_PASSWORD', 'root')
|
->withEnvironment('MYSQL_ROOT_PASSWORD', 'root')
|
||||||
->withWait(
|
->withWait(
|
||||||
new WaitForExec([
|
new WaitForExec([
|
||||||
@@ -52,11 +53,13 @@ class WaitStrategyTest extends TestCase
|
|||||||
$version = $query->fetchColumn();
|
$version = $query->fetchColumn();
|
||||||
|
|
||||||
$this->assertNotEmpty($version);
|
$this->assertNotEmpty($version);
|
||||||
|
|
||||||
|
$container->stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testWaitForLog(): void
|
public function testWaitForLog(): void
|
||||||
{
|
{
|
||||||
$container = Container::make('redis:6.2.5')
|
$container = RedisContainer::make()
|
||||||
->withWait(new WaitForLog('Ready to accept connections'));
|
->withWait(new WaitForLog('Ready to accept connections'));
|
||||||
|
|
||||||
$container->run();
|
$container->run();
|
||||||
@@ -138,6 +141,7 @@ class WaitStrategyTest extends TestCase
|
|||||||
{
|
{
|
||||||
$container = Container::make('nginx')
|
$container = Container::make('nginx')
|
||||||
->withHealthCheckCommand('curl --fail http://localhost')
|
->withHealthCheckCommand('curl --fail http://localhost')
|
||||||
|
->withPort('80', '80')
|
||||||
->withWait(new WaitForHealthCheck());
|
->withWait(new WaitForHealthCheck());
|
||||||
|
|
||||||
$container->run();
|
$container->run();
|
||||||
@@ -153,5 +157,7 @@ class WaitStrategyTest extends TestCase
|
|||||||
$this->assertIsString($response);
|
$this->assertIsString($response);
|
||||||
|
|
||||||
$this->assertStringContainsString('Welcome to nginx!', $response);
|
$this->assertStringContainsString('Welcome to nginx!', $response);
|
||||||
|
|
||||||
|
$container->stop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user