mirror of
https://github.com/stan220/testcontainers-php.git
synced 2026-09-12 08:29:34 +00:00
Code reuse on determining docker container address
This commit is contained in:
@@ -4,14 +4,13 @@ declare(strict_types=1);
|
||||
|
||||
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';
|
||||
@@ -59,16 +58,10 @@ class WaitForHttp implements WaitInterface
|
||||
|
||||
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 = $this->getContainerAddress(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);
|
||||
|
||||
@@ -6,14 +6,13 @@ namespace Testcontainer\Wait;
|
||||
|
||||
use JsonException;
|
||||
use RuntimeException;
|
||||
use Symfony\Component\Process\Process;
|
||||
use Testcontainer\Exception\ContainerNotReadyException;
|
||||
use Testcontainer\Trait\DockerContainerAwareTrait;
|
||||
|
||||
/**
|
||||
* @phpstan-import-type ContainerInspect from \Testcontainer\Container\Container
|
||||
*/
|
||||
final class WaitForTcpPortOpen implements WaitInterface
|
||||
{
|
||||
use DockerContainerAwareTrait;
|
||||
|
||||
public function __construct(private readonly int $port)
|
||||
{
|
||||
}
|
||||
@@ -28,28 +27,8 @@ final class WaitForTcpPortOpen implements WaitInterface
|
||||
*/
|
||||
public function wait(string $id): void
|
||||
{
|
||||
if (@fsockopen($this->findContainerAddress($id), $this->port) === false) {
|
||||
if (@fsockopen($this->getContainerAddress($id), $this->port) === false) {
|
||||
throw new ContainerNotReadyException($id, new RuntimeException('Unable to connect to container TCP port'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws JsonException
|
||||
*/
|
||||
private function findContainerAddress(string $id): string
|
||||
{
|
||||
$process = new Process(['docker', 'inspect', $id]);
|
||||
$process->mustRun();
|
||||
|
||||
/** @var ContainerInspect $data */
|
||||
$data = json_decode($process->getOutput(), true, 512, JSON_THROW_ON_ERROR);
|
||||
|
||||
$containerAddress = $data[0]['NetworkSettings']['IPAddress'] ?? null;
|
||||
|
||||
if (! is_string($containerAddress)) {
|
||||
throw new ContainerNotReadyException($id, new RuntimeException('Unable to find container IP address'));
|
||||
}
|
||||
|
||||
return $containerAddress;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user