mirror of
https://github.com/stan220/testcontainers-php.git
synced 2026-09-12 11:29:36 +00:00
use FixedPortGenerator for legacy implementations
This commit is contained in:
@@ -17,6 +17,7 @@ use Docker\Docker;
|
|||||||
use Docker\Stream\CreateImageStream;
|
use Docker\Stream\CreateImageStream;
|
||||||
use InvalidArgumentException;
|
use InvalidArgumentException;
|
||||||
use Testcontainers\ContainerClient\DockerContainerClient;
|
use Testcontainers\ContainerClient\DockerContainerClient;
|
||||||
|
use Testcontainers\Utils\PortGenerator\PortGenerator;
|
||||||
use Testcontainers\Utils\PortGenerator\RandomUniquePortGenerator;
|
use Testcontainers\Utils\PortGenerator\RandomUniquePortGenerator;
|
||||||
use Testcontainers\Utils\PortNormalizer;
|
use Testcontainers\Utils\PortNormalizer;
|
||||||
use Testcontainers\Wait\WaitForContainer;
|
use Testcontainers\Wait\WaitForContainer;
|
||||||
@@ -44,6 +45,8 @@ class GenericContainer implements TestContainer
|
|||||||
|
|
||||||
protected WaitStrategy $waitStrategy;
|
protected WaitStrategy $waitStrategy;
|
||||||
|
|
||||||
|
protected PortGenerator $portGenerator;
|
||||||
|
|
||||||
protected bool $isPrivileged = false;
|
protected bool $isPrivileged = false;
|
||||||
protected ?string $networkName = null;
|
protected ?string $networkName = null;
|
||||||
|
|
||||||
@@ -63,6 +66,7 @@ class GenericContainer implements TestContainer
|
|||||||
$this->image = $image;
|
$this->image = $image;
|
||||||
$this->dockerClient = DockerContainerClient::getDockerClient();
|
$this->dockerClient = DockerContainerClient::getDockerClient();
|
||||||
$this->waitStrategy = new WaitForContainer();
|
$this->waitStrategy = new WaitForContainer();
|
||||||
|
$this->portGenerator = new RandomUniquePortGenerator();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getId(): string
|
public function getId(): string
|
||||||
@@ -174,6 +178,13 @@ class GenericContainer implements TestContainer
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function withPortGenerator(PortGenerator $portGenerator): static
|
||||||
|
{
|
||||||
|
$this->portGenerator = $portGenerator;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
public function start(): StartedGenericContainer
|
public function start(): StartedGenericContainer
|
||||||
{
|
{
|
||||||
$this->startAttempts++;
|
$this->startAttempts++;
|
||||||
@@ -266,12 +277,11 @@ class GenericContainer implements TestContainer
|
|||||||
*/
|
*/
|
||||||
protected function createPortBindings(): array
|
protected function createPortBindings(): array
|
||||||
{
|
{
|
||||||
$portGenerator = new RandomUniquePortGenerator();
|
|
||||||
$portBindings = [];
|
$portBindings = [];
|
||||||
|
|
||||||
foreach ($this->exposedPorts as $port) {
|
foreach ($this->exposedPorts as $port) {
|
||||||
$portBinding = new PortBinding();
|
$portBinding = new PortBinding();
|
||||||
$portBinding->setHostPort((string)$portGenerator->generatePort());
|
$portBinding->setHostPort((string)$this->portGenerator->generatePort());
|
||||||
$portBinding->setHostIp('0.0.0.0');
|
$portBinding->setHostIp('0.0.0.0');
|
||||||
$portBindings[$port] = [$portBinding];
|
$portBindings[$port] = [$portBinding];
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Testcontainers\Container;
|
namespace Testcontainers\Container;
|
||||||
|
|
||||||
|
use Testcontainers\Utils\PortGenerator\FixedPortGenerator;
|
||||||
use Testcontainers\Wait\WaitForExec;
|
use Testcontainers\Wait\WaitForExec;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -16,6 +17,7 @@ class MariaDBContainer extends Container
|
|||||||
public function __construct(string $version = 'latest', string $mysqlRootPassword = 'root')
|
public function __construct(string $version = 'latest', string $mysqlRootPassword = 'root')
|
||||||
{
|
{
|
||||||
parent::__construct('mariadb:' . $version);
|
parent::__construct('mariadb:' . $version);
|
||||||
|
$this->withPortGenerator(new FixedPortGenerator([3306]));
|
||||||
$this->withExposedPorts(3306);
|
$this->withExposedPorts(3306);
|
||||||
$this->withEnvironment('MARIADB_ROOT_PASSWORD', $mysqlRootPassword);
|
$this->withEnvironment('MARIADB_ROOT_PASSWORD', $mysqlRootPassword);
|
||||||
$this->withWait(new WaitForExec([
|
$this->withWait(new WaitForExec([
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Testcontainers\Container;
|
namespace Testcontainers\Container;
|
||||||
|
|
||||||
|
use Testcontainers\Utils\PortGenerator\FixedPortGenerator;
|
||||||
use Testcontainers\Wait\WaitForExec;
|
use Testcontainers\Wait\WaitForExec;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -16,6 +17,7 @@ class MySQLContainer extends Container
|
|||||||
public function __construct(string $version = 'latest', string $mysqlRootPassword = 'root')
|
public function __construct(string $version = 'latest', string $mysqlRootPassword = 'root')
|
||||||
{
|
{
|
||||||
parent::__construct('mysql:' . $version);
|
parent::__construct('mysql:' . $version);
|
||||||
|
$this->withPortGenerator(new FixedPortGenerator([3306]));
|
||||||
$this->withExposedPorts(3306);
|
$this->withExposedPorts(3306);
|
||||||
$this->withEnvironment('MYSQL_ROOT_PASSWORD', $mysqlRootPassword);
|
$this->withEnvironment('MYSQL_ROOT_PASSWORD', $mysqlRootPassword);
|
||||||
$this->withWait(new WaitForExec([
|
$this->withWait(new WaitForExec([
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Testcontainers\Container;
|
namespace Testcontainers\Container;
|
||||||
|
|
||||||
|
use Testcontainers\Utils\PortGenerator\FixedPortGenerator;
|
||||||
use Testcontainers\Wait\WaitForLog;
|
use Testcontainers\Wait\WaitForLog;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -16,6 +17,7 @@ class OpenSearchContainer extends Container
|
|||||||
public function __construct(string $version = 'latest')
|
public function __construct(string $version = 'latest')
|
||||||
{
|
{
|
||||||
parent::__construct('opensearchproject/opensearch:' . $version);
|
parent::__construct('opensearchproject/opensearch:' . $version);
|
||||||
|
$this->withPortGenerator(new FixedPortGenerator([9200]));
|
||||||
$this->withExposedPorts(9200);
|
$this->withExposedPorts(9200);
|
||||||
$this->withEnvironment('discovery.type', 'single-node');
|
$this->withEnvironment('discovery.type', 'single-node');
|
||||||
$this->withEnvironment('OPENSEARCH_INITIAL_ADMIN_PASSWORD', 'c3o_ZPHo!');
|
$this->withEnvironment('OPENSEARCH_INITIAL_ADMIN_PASSWORD', 'c3o_ZPHo!');
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Testcontainers\Container;
|
namespace Testcontainers\Container;
|
||||||
|
|
||||||
|
use Testcontainers\Utils\PortGenerator\FixedPortGenerator;
|
||||||
use Testcontainers\Wait\WaitForExec;
|
use Testcontainers\Wait\WaitForExec;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -20,6 +21,7 @@ class PostgresContainer extends Container
|
|||||||
public readonly string $database = 'test'
|
public readonly string $database = 'test'
|
||||||
) {
|
) {
|
||||||
parent::__construct('postgres:' . $version);
|
parent::__construct('postgres:' . $version);
|
||||||
|
$this->withPortGenerator(new FixedPortGenerator([5432]));
|
||||||
$this->withExposedPorts(5432);
|
$this->withExposedPorts(5432);
|
||||||
$this->withEnvironment('POSTGRES_USER', $this->username);
|
$this->withEnvironment('POSTGRES_USER', $this->username);
|
||||||
$this->withEnvironment('POSTGRES_PASSWORD', $this->password);
|
$this->withEnvironment('POSTGRES_PASSWORD', $this->password);
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Testcontainers\Container;
|
namespace Testcontainers\Container;
|
||||||
|
|
||||||
|
use Testcontainers\Utils\PortGenerator\FixedPortGenerator;
|
||||||
use Testcontainers\Wait\WaitForLog;
|
use Testcontainers\Wait\WaitForLog;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -16,6 +17,7 @@ class RedisContainer extends Container
|
|||||||
public function __construct(string $version = 'latest')
|
public function __construct(string $version = 'latest')
|
||||||
{
|
{
|
||||||
parent::__construct('redis:' . $version);
|
parent::__construct('redis:' . $version);
|
||||||
|
$this->withPortGenerator(new FixedPortGenerator([6379]));
|
||||||
$this->withExposedPorts(6379);
|
$this->withExposedPorts(6379);
|
||||||
$this->withWait(new WaitForLog('Ready to accept connections'));
|
$this->withWait(new WaitForLog('Ready to accept connections'));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,13 +17,6 @@ use Testcontainers\Container\RedisContainer;
|
|||||||
*/
|
*/
|
||||||
class ContainerTest extends TestCase
|
class ContainerTest extends TestCase
|
||||||
{
|
{
|
||||||
//TODO: remove after check
|
|
||||||
//To make it work, fixed port should be first implemented
|
|
||||||
protected function setUp(): void
|
|
||||||
{
|
|
||||||
$this->markTestIncomplete();
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testMySQL(): void
|
public function testMySQL(): void
|
||||||
{
|
{
|
||||||
$container = MySQLContainer::make();
|
$container = MySQLContainer::make();
|
||||||
|
|||||||
Reference in New Issue
Block a user