implement WaitForHttp and WaitForHostPort strategies

This commit is contained in:
Sergei Shitikov
2024-10-24 19:36:48 +02:00
parent a5c8d2bb73
commit 1cbfe638b9
5 changed files with 195 additions and 114 deletions
+11 -37
View File
@@ -21,12 +21,6 @@ use Testcontainers\Wait\WaitForTcpPortOpen;
*/
class WaitStrategyTest extends TestCase
{
//TODO: remove after check
protected function setUp(): void
{
$this->markTestIncomplete();
}
public function testWaitForExec(): void
{
$container = MySQLContainer::make()
@@ -86,12 +80,13 @@ class WaitStrategyTest extends TestCase
public function testWaitForHTTP(): void
{
$container = Container::make('nginx:alpine')
->withWait(WaitForHttp::make(80));
->withWait(WaitForHttp::make(3000))
->withPort('3000', '80');
$container->run();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, sprintf('http://%s:%d', $container->getAddress(), 80));
curl_setopt($ch, CURLOPT_URL, sprintf('http://%s:%d', $container->getAddress(), $container->getPort()));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = (string) curl_exec($ch);
@@ -99,42 +94,21 @@ class WaitStrategyTest extends TestCase
curl_close($ch);
$this->assertNotEmpty($response);
$container->stop();
}
/**
* @dataProvider provideWaitForTcpPortOpen
*/
public function testWaitForTcpPortOpen(bool $wait): void
public function testWaitForTcpPortOpen(): void
{
$container = Container::make('nginx:alpine');
if ($wait) {
$container->withWait(WaitForTcpPortOpen::make(80));
}
$container = Container::make('nginx:alpine')
->withWait(WaitForTcpPortOpen::make(80))
->withPort('80', '80');
$container->run();
if ($wait) {
static::assertIsResource(fsockopen($container->getAddress(), 80), 'Failed to connect to container');
return;
}
static::assertIsResource(fsockopen($container->getAddress(), 80), 'Failed to connect to container');
$containerId = $container->getId();
$this->expectExceptionObject(new ContainerNotReadyException($containerId));
(new WaitForTcpPortOpen(8080))->wait($containerId);
}
/**
* @return array<string, array<bool>>
*/
public function provideWaitForTcpPortOpen(): array
{
return [
'Can connect to container' => [true],
'Cannot connect to container' => [false],
];
$container->stop();
}
public function testWaitForHealthCheck(): void