From e663bbfbcc533cee9afff2c8913afff4824d4026 Mon Sep 17 00:00:00 2001 From: Sergei Shitikov Date: Sat, 28 Dec 2024 13:26:52 +0100 Subject: [PATCH 1/2] add withHostname, withName, withLabels support --- src/Container/Container.php | 10 ------- src/Container/GenericContainer.php | 48 ++++++++++++++++++++++++++++-- 2 files changed, 46 insertions(+), 12 deletions(-) diff --git a/src/Container/Container.php b/src/Container/Container.php index 2dd943f..ade3661 100644 --- a/src/Container/Container.php +++ b/src/Container/Container.php @@ -31,16 +31,6 @@ class Container extends GenericContainer return $this->withCommand($cmd); } - /** - * @deprecated Use `withEntrypoint` instead - * TODO: this is just dummy method for compatibility, - * the implementation with Docker Engine API should be discussed - */ - public function withHostname(string $hostname): self - { - return $this; - } - /** * @deprecated Use `withPrivilegedMode` instead */ diff --git a/src/Container/GenericContainer.php b/src/Container/GenericContainer.php index 326291b..efbea98 100644 --- a/src/Container/GenericContainer.php +++ b/src/Container/GenericContainer.php @@ -29,6 +29,16 @@ class GenericContainer implements TestContainer protected string $image; + protected ?string $name = null; + + /** + * User-defined key/value metadata. + * @param array|null $labels + */ + protected ?array $labels = null; + + protected ?string $hostname = null; + protected string $id; /** @var list */ @@ -140,9 +150,20 @@ class GenericContainer implements TestContainer return $this; } + public function withHostname(string $hostname): static + { + $this->hostname = $hostname; + + return $this; + } + public function withMount(string $localPath, string $containerPath): static { - $this->mounts[] = new Mount(['type' => 'bind', 'source' => $localPath, 'target' => $containerPath]); + $this->mounts[] = new Mount([ + 'type' => 'bind', + 'source' => $localPath, + 'target' => $containerPath, + ]); return $this; } @@ -170,6 +191,23 @@ class GenericContainer implements TestContainer return $this; } + public function withName(string $name): static + { + $this->name = $name; + + return $this; + } + + /** + * @param array $labels + */ + public function withLabels(array $labels): static + { + $this->labels = $labels; + + return $this; + } + public function withPrivilegedMode(bool $privileged = true): static { $this->isPrivileged = $privileged; @@ -196,9 +234,13 @@ class GenericContainer implements TestContainer { $this->startAttempts++; $containerConfig = $this->createContainerConfig(); + $queryParameters = []; + if ($this->name !== null) { + $queryParameters['name'] = $this->name; + } try { /** @var ContainerCreateResponse|null $containerCreateResponse */ - $containerCreateResponse = $this->dockerClient->containerCreate($containerConfig); + $containerCreateResponse = $this->dockerClient->containerCreate($containerConfig, $queryParameters); $this->id = $containerCreateResponse?->getId() ?? ''; } catch (ContainerCreateNotFoundException) { if ($this->startAttempts >= self::MAX_START_ATTEMPTS) { @@ -223,6 +265,8 @@ class GenericContainer implements TestContainer $containerCreatePostBody = new ContainersCreatePostBody(); $containerCreatePostBody->setImage($this->image); $containerCreatePostBody->setCmd($this->command); + $containerCreatePostBody->setLabels($this->labels); + $containerCreatePostBody->setHostname($this->hostname); $envs = array_map(static fn ($key, $value) => "$key=$value", array_keys($this->env), $this->env); $containerCreatePostBody->setEnv($envs); From 774743a7cec40155494ed230414cb79eeffeb2ba Mon Sep 17 00:00:00 2001 From: Sergei Shitikov Date: Sat, 28 Dec 2024 18:57:23 +0100 Subject: [PATCH 2/2] update interfaces, small phpstan improvements --- src/Container/GenericContainer.php | 2 +- src/Container/StartedGenericContainer.php | 1 - src/Container/StartedTestContainer.php | 52 +++++++++++------------ src/Container/TestContainer.php | 38 +++++++++++++---- 4 files changed, 57 insertions(+), 36 deletions(-) diff --git a/src/Container/GenericContainer.php b/src/Container/GenericContainer.php index efbea98..ea85e7d 100644 --- a/src/Container/GenericContainer.php +++ b/src/Container/GenericContainer.php @@ -33,7 +33,7 @@ class GenericContainer implements TestContainer /** * User-defined key/value metadata. - * @param array|null $labels + * @var array|null $labels */ protected ?array $labels = null; diff --git a/src/Container/StartedGenericContainer.php b/src/Container/StartedGenericContainer.php index ec81e71..5e45977 100644 --- a/src/Container/StartedGenericContainer.php +++ b/src/Container/StartedGenericContainer.php @@ -23,7 +23,6 @@ class StartedGenericContainer implements StartedTestContainer $this->dockerClient = DockerContainerClient::getDockerClient(); } - public function getId(): string { return $this->id; diff --git a/src/Container/StartedTestContainer.php b/src/Container/StartedTestContainer.php index 8eefcdf..3a75b0b 100644 --- a/src/Container/StartedTestContainer.php +++ b/src/Container/StartedTestContainer.php @@ -8,36 +8,36 @@ use Docker\Docker; interface StartedTestContainer { - public function stop(): StoppedTestContainer; - - public function restart(): self; - - public function getClient(): Docker; - - public function getHost(): string; - - public function getFirstMappedPort(): int; - - public function getMappedPort(int $port): int; - - public function getName(): string; - - public function getLabels(): array; - - public function getId(): string; - - public function getLastExecId(): string | null; - - public function getNetworkNames(): array; - - public function getNetworkId(string $networkName): string; - - public function getIpAddress(string $networkName): string; - /** * @param list $command */ public function exec(array $command): string; + public function getClient(): Docker; + + public function getFirstMappedPort(): int; + + public function getHost(): string; + + public function getId(): string; + + public function getIpAddress(string $networkName): string; + + public function getLabels(): array; + public function logs(): string; + + public function getLastExecId(): string | null; + + public function getMappedPort(int $port): int; + + public function getName(): string; + + public function getNetworkId(string $networkName): string; + + public function getNetworkNames(): array; + + public function restart(): self; + + public function stop(): StoppedTestContainer; } diff --git a/src/Container/TestContainer.php b/src/Container/TestContainer.php index e5ec39c..2885e62 100644 --- a/src/Container/TestContainer.php +++ b/src/Container/TestContainer.php @@ -4,18 +4,13 @@ declare(strict_types=1); namespace Testcontainers\Container; +use Testcontainers\Utils\PortGenerator\PortGenerator; use Testcontainers\Wait\WaitStrategy; interface TestContainer { public function start(): StartedGenericContainer; - /** - * TODO: replace with array after deprecated implementation is removed - * @param array|string $env - */ - public function withEnvironment(array | string $env, ?string $value): static; - /** * @param array $command */ @@ -23,12 +18,39 @@ interface TestContainer public function withEntrypoint(string $entryPoint): static; + /** + * TODO: replace with array after deprecated implementation is removed + * @param array|string $env + */ + public function withEnvironment(array | string $env, ?string $value): static; + /** @param int|string|array $ports One or more ports to expose. */ public function withExposedPorts(...$ports): static; - public function withWait(WaitStrategy $waitStrategy): static; + public function withHealthCheckCommand( + string $command, + int $intervalInMilliseconds, + int $timeoutInMilliseconds, + int $retries, + int $startPeriodInMilliseconds + ): static; + + public function withHostname(string $hostname): static; + + /** + * @param array $labels + */ + public function withLabels(array $labels): static; + + public function withMount(string $localPath, string $containerPath): static; + + public function withName(string $name): static; public function withNetwork(string $networkName): static; - public function withPrivilegedMode(): static; + public function withPortGenerator(PortGenerator $portGenerator): static; + + public function withPrivilegedMode(bool $privileged): static; + + public function withWait(WaitStrategy $waitStrategy): static; }