Merge pull request #24 from rw4lll/feat/docker-engine-api-client

add withHostname, withName, withLabels support
This commit is contained in:
Shyim
2024-12-29 15:29:56 +01:00
committed by GitHub
5 changed files with 102 additions and 47 deletions
-10
View File
@@ -31,16 +31,6 @@ class Container extends GenericContainer
return $this->withCommand($cmd); 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 * @deprecated Use `withPrivilegedMode` instead
*/ */
+46 -2
View File
@@ -29,6 +29,16 @@ class GenericContainer implements TestContainer
protected string $image; protected string $image;
protected ?string $name = null;
/**
* User-defined key/value metadata.
* @var array<string, string>|null $labels
*/
protected ?array $labels = null;
protected ?string $hostname = null;
protected string $id; protected string $id;
/** @var list<string> */ /** @var list<string> */
@@ -140,9 +150,20 @@ class GenericContainer implements TestContainer
return $this; return $this;
} }
public function withHostname(string $hostname): static
{
$this->hostname = $hostname;
return $this;
}
public function withMount(string $localPath, string $containerPath): static 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; return $this;
} }
@@ -170,6 +191,23 @@ class GenericContainer implements TestContainer
return $this; return $this;
} }
public function withName(string $name): static
{
$this->name = $name;
return $this;
}
/**
* @param array<string, string> $labels
*/
public function withLabels(array $labels): static
{
$this->labels = $labels;
return $this;
}
public function withPrivilegedMode(bool $privileged = true): static public function withPrivilegedMode(bool $privileged = true): static
{ {
$this->isPrivileged = $privileged; $this->isPrivileged = $privileged;
@@ -196,9 +234,13 @@ class GenericContainer implements TestContainer
{ {
$this->startAttempts++; $this->startAttempts++;
$containerConfig = $this->createContainerConfig(); $containerConfig = $this->createContainerConfig();
$queryParameters = [];
if ($this->name !== null) {
$queryParameters['name'] = $this->name;
}
try { try {
/** @var ContainerCreateResponse|null $containerCreateResponse */ /** @var ContainerCreateResponse|null $containerCreateResponse */
$containerCreateResponse = $this->dockerClient->containerCreate($containerConfig); $containerCreateResponse = $this->dockerClient->containerCreate($containerConfig, $queryParameters);
$this->id = $containerCreateResponse?->getId() ?? ''; $this->id = $containerCreateResponse?->getId() ?? '';
} catch (ContainerCreateNotFoundException) { } catch (ContainerCreateNotFoundException) {
if ($this->startAttempts >= self::MAX_START_ATTEMPTS) { if ($this->startAttempts >= self::MAX_START_ATTEMPTS) {
@@ -223,6 +265,8 @@ class GenericContainer implements TestContainer
$containerCreatePostBody = new ContainersCreatePostBody(); $containerCreatePostBody = new ContainersCreatePostBody();
$containerCreatePostBody->setImage($this->image); $containerCreatePostBody->setImage($this->image);
$containerCreatePostBody->setCmd($this->command); $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); $envs = array_map(static fn ($key, $value) => "$key=$value", array_keys($this->env), $this->env);
$containerCreatePostBody->setEnv($envs); $containerCreatePostBody->setEnv($envs);
@@ -23,7 +23,6 @@ class StartedGenericContainer implements StartedTestContainer
$this->dockerClient = DockerContainerClient::getDockerClient(); $this->dockerClient = DockerContainerClient::getDockerClient();
} }
public function getId(): string public function getId(): string
{ {
return $this->id; return $this->id;
+26 -26
View File
@@ -8,36 +8,36 @@ use Docker\Docker;
interface StartedTestContainer 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<string> $command * @param list<string> $command
*/ */
public function exec(array $command): string; 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 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;
} }
+30 -8
View File
@@ -4,18 +4,13 @@ declare(strict_types=1);
namespace Testcontainers\Container; namespace Testcontainers\Container;
use Testcontainers\Utils\PortGenerator\PortGenerator;
use Testcontainers\Wait\WaitStrategy; use Testcontainers\Wait\WaitStrategy;
interface TestContainer interface TestContainer
{ {
public function start(): StartedGenericContainer; public function start(): StartedGenericContainer;
/**
* TODO: replace with array after deprecated implementation is removed
* @param array<string, string>|string $env
*/
public function withEnvironment(array | string $env, ?string $value): static;
/** /**
* @param array<string> $command * @param array<string> $command
*/ */
@@ -23,12 +18,39 @@ interface TestContainer
public function withEntrypoint(string $entryPoint): static; public function withEntrypoint(string $entryPoint): static;
/**
* TODO: replace with array after deprecated implementation is removed
* @param array<string, string>|string $env
*/
public function withEnvironment(array | string $env, ?string $value): static;
/** @param int|string|array<int|string> $ports One or more ports to expose. */ /** @param int|string|array<int|string> $ports One or more ports to expose. */
public function withExposedPorts(...$ports): static; 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<string, string> $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 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;
} }