use FixedPortGenerator for legacy implementations

This commit is contained in:
Sergei Shitikov
2024-09-29 18:10:01 +02:00
parent a58ff460fa
commit 353977c7bb
7 changed files with 22 additions and 9 deletions
+12 -2
View File
@@ -17,6 +17,7 @@ use Docker\Docker;
use Docker\Stream\CreateImageStream;
use InvalidArgumentException;
use Testcontainers\ContainerClient\DockerContainerClient;
use Testcontainers\Utils\PortGenerator\PortGenerator;
use Testcontainers\Utils\PortGenerator\RandomUniquePortGenerator;
use Testcontainers\Utils\PortNormalizer;
use Testcontainers\Wait\WaitForContainer;
@@ -44,6 +45,8 @@ class GenericContainer implements TestContainer
protected WaitStrategy $waitStrategy;
protected PortGenerator $portGenerator;
protected bool $isPrivileged = false;
protected ?string $networkName = null;
@@ -63,6 +66,7 @@ class GenericContainer implements TestContainer
$this->image = $image;
$this->dockerClient = DockerContainerClient::getDockerClient();
$this->waitStrategy = new WaitForContainer();
$this->portGenerator = new RandomUniquePortGenerator();
}
public function getId(): string
@@ -174,6 +178,13 @@ class GenericContainer implements TestContainer
return $this;
}
public function withPortGenerator(PortGenerator $portGenerator): static
{
$this->portGenerator = $portGenerator;
return $this;
}
public function start(): StartedGenericContainer
{
$this->startAttempts++;
@@ -266,12 +277,11 @@ class GenericContainer implements TestContainer
*/
protected function createPortBindings(): array
{
$portGenerator = new RandomUniquePortGenerator();
$portBindings = [];
foreach ($this->exposedPorts as $port) {
$portBinding = new PortBinding();
$portBinding->setHostPort((string)$portGenerator->generatePort());
$portBinding->setHostPort((string)$this->portGenerator->generatePort());
$portBinding->setHostIp('0.0.0.0');
$portBindings[$port] = [$portBinding];
}
+2
View File
@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Testcontainers\Container;
use Testcontainers\Utils\PortGenerator\FixedPortGenerator;
use Testcontainers\Wait\WaitForExec;
/**
@@ -16,6 +17,7 @@ class MariaDBContainer extends Container
public function __construct(string $version = 'latest', string $mysqlRootPassword = 'root')
{
parent::__construct('mariadb:' . $version);
$this->withPortGenerator(new FixedPortGenerator([3306]));
$this->withExposedPorts(3306);
$this->withEnvironment('MARIADB_ROOT_PASSWORD', $mysqlRootPassword);
$this->withWait(new WaitForExec([
+2
View File
@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Testcontainers\Container;
use Testcontainers\Utils\PortGenerator\FixedPortGenerator;
use Testcontainers\Wait\WaitForExec;
/**
@@ -16,6 +17,7 @@ class MySQLContainer extends Container
public function __construct(string $version = 'latest', string $mysqlRootPassword = 'root')
{
parent::__construct('mysql:' . $version);
$this->withPortGenerator(new FixedPortGenerator([3306]));
$this->withExposedPorts(3306);
$this->withEnvironment('MYSQL_ROOT_PASSWORD', $mysqlRootPassword);
$this->withWait(new WaitForExec([
+2
View File
@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Testcontainers\Container;
use Testcontainers\Utils\PortGenerator\FixedPortGenerator;
use Testcontainers\Wait\WaitForLog;
/**
@@ -16,6 +17,7 @@ class OpenSearchContainer extends Container
public function __construct(string $version = 'latest')
{
parent::__construct('opensearchproject/opensearch:' . $version);
$this->withPortGenerator(new FixedPortGenerator([9200]));
$this->withExposedPorts(9200);
$this->withEnvironment('discovery.type', 'single-node');
$this->withEnvironment('OPENSEARCH_INITIAL_ADMIN_PASSWORD', 'c3o_ZPHo!');
+2
View File
@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Testcontainers\Container;
use Testcontainers\Utils\PortGenerator\FixedPortGenerator;
use Testcontainers\Wait\WaitForExec;
/**
@@ -20,6 +21,7 @@ class PostgresContainer extends Container
public readonly string $database = 'test'
) {
parent::__construct('postgres:' . $version);
$this->withPortGenerator(new FixedPortGenerator([5432]));
$this->withExposedPorts(5432);
$this->withEnvironment('POSTGRES_USER', $this->username);
$this->withEnvironment('POSTGRES_PASSWORD', $this->password);
+2
View File
@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Testcontainers\Container;
use Testcontainers\Utils\PortGenerator\FixedPortGenerator;
use Testcontainers\Wait\WaitForLog;
/**
@@ -16,6 +17,7 @@ class RedisContainer extends Container
public function __construct(string $version = 'latest')
{
parent::__construct('redis:' . $version);
$this->withPortGenerator(new FixedPortGenerator([6379]));
$this->withExposedPorts(6379);
$this->withWait(new WaitForLog('Ready to accept connections'));
}
@@ -17,13 +17,6 @@ use Testcontainers\Container\RedisContainer;
*/
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
{
$container = MySQLContainer::make();