Skip to content
Open
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
8 changes: 4 additions & 4 deletions src/Container/GenericContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use RuntimeException;
use Testcontainers\ContainerClient\DockerContainerClient;
use Testcontainers\Utils\PortGenerator\PortGenerator;
use Testcontainers\Utils\PortGenerator\RandomUniquePortGenerator;
use Testcontainers\Utils\PortNormalizer;
use Testcontainers\Utils\TarBuilder;
use Testcontainers\Wait\WaitForContainer;
Expand Down Expand Up @@ -60,7 +59,7 @@ class GenericContainer implements TestContainer

protected WaitStrategy $waitStrategy;

protected PortGenerator $portGenerator;
protected ?PortGenerator $portGenerator = null;

protected bool $isPrivileged = false;

Expand Down Expand Up @@ -107,7 +106,6 @@ public function __construct(string $image)
$this->image = $image;
$this->dockerClient = DockerContainerClient::getDockerClient();
$this->waitStrategy = new WaitForContainer();
$this->portGenerator = new RandomUniquePortGenerator();
}

public function getId(): string
Expand Down Expand Up @@ -505,7 +503,9 @@ protected function createPortBindings(): array

foreach ($this->exposedPorts as $port) {
$portBinding = new PortBinding();
$portBinding->setHostPort((string)$this->portGenerator->generatePort());
if ($this->portGenerator !== null) {
$portBinding->setHostPort((string)$this->portGenerator->generatePort());
}
$portBinding->setHostIp('0.0.0.0');
$portBindings[$port] = [$portBinding];
}
Expand Down
67 changes: 67 additions & 0 deletions tests/Unit/Container/GenericContainerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

declare(strict_types=1);

namespace Testcontainers\Tests\Unit\Container;

use Docker\API\Model\ContainersCreatePostBody;
use Docker\Docker as DockerClient;
use PHPUnit\Framework\TestCase;
use RuntimeException;
use Testcontainers\Container\GenericContainer;
use Testcontainers\ContainerClient\DockerContainerClient;
use Testcontainers\Utils\PortGenerator\FixedPortGenerator;
use Testcontainers\Utils\PortGenerator\PortGenerator;

/**
* @covers \Testcontainers\Container\GenericContainer
*/
class GenericContainerTest extends TestCase
{
protected function tearDown(): void
{
(new \ReflectionClass(DockerContainerClient::class))->getProperty('dockerClient')->setValue(null, null);
parent::tearDown();
}

/**
* @return array<string, array{?PortGenerator, ?string}>
*/
public static function portGenerators(): array
{
return [
'docker allocates by default' => [null, null],
'port generator' => [new FixedPortGenerator([8080]), '8080'],
];
}

/**
* @dataProvider portGenerators
*/
public function testHostPortOfExposedPort(?PortGenerator $portGenerator, ?string $hostPort): void
{
$created = null;
$docker = $this->createMock(DockerClient::class);
$docker->method('containerCreate')->willReturnCallback(
static function (ContainersCreatePostBody $body) use (&$created): never {
$created = $body;
throw new RuntimeException('created');
}
);
DockerContainerClient::setDockerClient($docker);
$container = (new GenericContainer('nginx'))->withExposedPorts(80);
if ($portGenerator !== null) {
$container->withPortGenerator($portGenerator);
}

try {
$container->start();
} catch (RuntimeException) {
}

self::assertInstanceOf(ContainersCreatePostBody::class, $created);
$binding = $created->getHostConfig()?->getPortBindings()['80/tcp'][0] ?? null;
self::assertNotNull($binding);
self::assertSame($hostPort, $binding->isInitialized('hostPort') ? $binding->getHostPort() : null);
}
}
Loading