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

Support for waiting on tcp port to open #12

Merged
merged 5 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 18 additions & 13 deletions src/Container/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,20 @@
use Symfony\Component\Process\Process;
use Testcontainer\Exception\ContainerNotReadyException;
use Testcontainer\Registry;
use Testcontainer\Trait\DockerContainerAwareTrait;
use Testcontainer\Wait\WaitForNothing;
use Testcontainer\Wait\WaitInterface;

/**
* @phpstan-type ContainerInspect array{0: array{NetworkSettings: array{IPAddress: string}}}
* @phpstan-type ContainerInspectSingleNetwork array<int, array{'NetworkSettings': array{'IPAddress': string}}>
* @phpstan-type ContainerInspectMultipleNetworks array<int, array{'NetworkSettings': array{'Networks': array<string, array{'IPAddress': string}>}}>
* @phpstan-type ContainerInspect ContainerInspectSingleNetwork|ContainerInspectMultipleNetworks
* @phpstan-type DockerNetwork array{CreatedAt: string, Driver: string, ID: string, IPv6: string, Internal: string, Labels: string, Name: string, Scope: string}
*/
class Container
{
use DockerContainerAwareTrait;

private string $id;

private ?string $entryPoint = null;
Expand Down Expand Up @@ -56,6 +62,11 @@ public static function make(string $image): self
return new Container($image);
}

public function getId(): string
{
return $this->id;
}

public function withEntryPoint(string $entryPoint): self
{
$this->entryPoint = $entryPoint;
Expand Down Expand Up @@ -157,13 +168,7 @@ public function run(bool $wait = true): self
$this->process = new Process($params);
$this->process->mustRun();

$inspect = new Process(['docker', 'inspect', $this->id]);
$inspect->mustRun();

/** @var ContainerInspect $inspectedData */
$inspectedData = json_decode($inspect->getOutput(), true, 512, JSON_THROW_ON_ERROR);

$this->inspectedData = $inspectedData;
$this->inspectedData = self::dockerContainerInspect($this->id);

Registry::add($this);

Expand Down Expand Up @@ -251,10 +256,10 @@ public function logs(): string

public function getAddress(): string
{
if ($this->network !== null && !empty($this->inspectedData[0]['NetworkSettings']['Networks'][$this->network]['IPAddress'])) {
return $this->inspectedData[0]['NetworkSettings']['Networks'][$this->network]['IPAddress'];
}

return $this->inspectedData[0]['NetworkSettings']['IPAddress'];
return self::dockerContainerAddress(
containerId: $this->id,
networkName: $this->network,
inspectedData: $this->inspectedData
);
}
}
108 changes: 108 additions & 0 deletions src/Trait/DockerContainerAwareTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

declare(strict_types=1);

namespace Testcontainer\Trait;

use JsonException;
use Symfony\Component\Process\Process;
use Testcontainer\Container\Container;
use UnexpectedValueException;

/**
* @phpstan-import-type ContainerInspect from Container
* @phpstan-import-type DockerNetwork from Container
*/
trait DockerContainerAwareTrait
{
/**
* @param string $containerId
* @param string|null $networkName
* @param ContainerInspect|null $inspectedData
* @return string
*
* @throws JsonException
*/
private static function dockerContainerAddress(string $containerId, ?string $networkName = null, ?array $inspectedData = null): string
{
if (! is_array($inspectedData)) {
$inspectedData = self::dockerContainerInspect($containerId);
}

if (is_string($networkName)) {
$containerAddress = $inspectedData[0]['NetworkSettings']['Networks'][$networkName]['IPAddress'] ?? null;

if (is_string($containerAddress)) {
return $containerAddress;
}
}

$containerAddress = $inspectedData[0]['NetworkSettings']['IPAddress'] ?? null;

if (is_string($containerAddress)) {
return $containerAddress;
}

throw new UnexpectedValueException('Unable to find container IP address');
}

/**
* @param string $containerId
* @return ContainerInspect
*
* @throws JsonException
*/
private static function dockerContainerInspect(string $containerId): array
{
$process = new Process(['docker', 'inspect', $containerId]);
$process->mustRun();

/** @var ContainerInspect */
return json_decode($process->getOutput(), true, 512, JSON_THROW_ON_ERROR);
}

/**
* @param string $networkName
* @return DockerNetwork|false
*
* @throws JsonException
*/
private static function dockerNetworkFind(string $networkName): array|false
{
$process = new Process(['docker', 'network', 'ls', '--format', 'json', '--filter', 'name=' . $networkName]);
$process->mustRun();

$json = $process->getOutput();

if ($json === '') {
return false;
}

$json = str_replace("\n", ',', $json);
$json = '['. rtrim($json, ',') .']';

/** @var array<int, DockerNetwork> $output */
$output = json_decode($json, true, 512, JSON_THROW_ON_ERROR);

/** @var array<int, DockerNetwork> $matchingNetworks */
$matchingNetworks = array_filter($output, static fn (array $network) => $network['Name'] === $networkName);

if (count($matchingNetworks) === 0) {
return false;
}

return $matchingNetworks[0];
}

private static function dockerNetworkCreate(string $networkName, string $driver = 'bridge'): void
{
$process = new Process(['docker', 'network', 'create', '--driver', $driver, $networkName]);
$process->mustRun();
}

private static function dockerNetworkRemove(string $networkName): void
{
$process = new Process(['docker', 'network', 'rm', $networkName, '-f']);
$process->mustRun();
}
}
17 changes: 5 additions & 12 deletions src/Wait/WaitForHttp.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@

namespace Testcontainer\Wait;

use Symfony\Component\Process\Process;
use Testcontainer\Exception\ContainerNotReadyException;
use Testcontainer\Trait\DockerContainerAwareTrait;

/**
* @phpstan-import-type ContainerInspect from \Testcontainer\Container\Container
*/
class WaitForHttp implements WaitInterface
{
use DockerContainerAwareTrait;

public const METHOD_GET = 'GET';
public const METHOD_POST = 'POST';
public const METHOD_PUT = 'PUT';
Expand Down Expand Up @@ -59,16 +58,10 @@ public function withStatusCode(int $statusCode): self

public function wait(string $id): void
{
$process = new Process(['docker', 'inspect', $id]);
$process->mustRun();

/** @var ContainerInspect $data */
$data = json_decode($process->getOutput(), true);

$ip = $data[0]['NetworkSettings']['IPAddress'];
$containerAddress = self::dockerContainerAddress(containerId: $id);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, sprintf('http://%s:%d%s', $ip, $this->port, $this->path));
curl_setopt($ch, CURLOPT_URL, sprintf('http://%s:%d%s', $containerAddress, $this->port, $this->path));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $this->method);
curl_setopt($ch, CURLOPT_HEADER, true);
Expand Down
34 changes: 34 additions & 0 deletions src/Wait/WaitForTcpPortOpen.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Testcontainer\Wait;

use JsonException;
use RuntimeException;
use Testcontainer\Exception\ContainerNotReadyException;
use Testcontainer\Trait\DockerContainerAwareTrait;

final class WaitForTcpPortOpen implements WaitInterface
{
use DockerContainerAwareTrait;

public function __construct(private readonly int $port, private readonly ?string $network = null)
{
}

public static function make(int $port, ?string $network = null): self
{
return new self($port, $network);
}

/**
* @throws JsonException
*/
public function wait(string $id): void
{
if (@fsockopen(self::dockerContainerAddress(containerId: $id, networkName: $this->network), $this->port) === false) {
shyim marked this conversation as resolved.
Show resolved Hide resolved
throw new ContainerNotReadyException($id, new RuntimeException('Unable to connect to container TCP port'));
}
}
}
49 changes: 49 additions & 0 deletions tests/Integration/WaitStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,26 @@
use Predis\Connection\ConnectionException;
use Symfony\Component\Process\Process;
use Testcontainer\Container\Container;
use Testcontainer\Exception\ContainerNotReadyException;
use Testcontainer\Registry;
use Testcontainer\Trait\DockerContainerAwareTrait;
use Testcontainer\Wait\WaitForExec;
use Testcontainer\Wait\WaitForHealthCheck;
use Testcontainer\Wait\WaitForHttp;
use Testcontainer\Wait\WaitForLog;
use Testcontainer\Wait\WaitForTcpPortOpen;

class WaitStrategyTest extends TestCase
{
use DockerContainerAwareTrait;

public static function tearDownAfterClass(): void
{
parent::tearDownAfterClass();

Registry::cleanup();
}

public function testWaitForExec(): void
{
$called = false;
Expand Down Expand Up @@ -90,6 +103,42 @@ public function testWaitForHTTP(): void
$this->assertNotEmpty($response);
}

/**
* @dataProvider provideWaitForTcpPortOpen
*/
public function testWaitForTcpPortOpen(bool $wait): void
{
$container = Container::make('nginx:alpine');

if ($wait) {
$container->withWait(WaitForTcpPortOpen::make(80));
}

$container->run();

if ($wait) {
static::assertIsResource(fsockopen($container->getAddress(), 80), 'Failed to connect to container');
return;
}

$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],
];
}

public function testWaitForHealthCheck(): void
{
$container = Container::make('nginx')
Expand Down
Loading