mirror of
https://github.com/stan220/testcontainers-php.git
synced 2026-09-12 11:29:36 +00:00
Added test for GenericContainer implementation + implemented exec related stuff
This commit is contained in:
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Testcontainers\Container;
|
namespace Testcontainers\Container;
|
||||||
|
|
||||||
|
use Docker\API\Client;
|
||||||
use Docker\API\Exception\ContainerCreateNotFoundException;
|
use Docker\API\Exception\ContainerCreateNotFoundException;
|
||||||
use Docker\API\Model\ContainersCreatePostBody;
|
use Docker\API\Model\ContainersCreatePostBody;
|
||||||
use Docker\API\Model\ContainersIdExecPostBody;
|
use Docker\API\Model\ContainersIdExecPostBody;
|
||||||
@@ -35,6 +36,9 @@ class GenericContainer
|
|||||||
|
|
||||||
protected string $id;
|
protected string $id;
|
||||||
|
|
||||||
|
/** @var array<string> */
|
||||||
|
protected array $command = [];
|
||||||
|
|
||||||
protected ?string $entryPoint = null;
|
protected ?string $entryPoint = null;
|
||||||
|
|
||||||
protected ?HealthConfig $healthConfig = null;
|
protected ?HealthConfig $healthConfig = null;
|
||||||
@@ -77,6 +81,29 @@ class GenericContainer
|
|||||||
return $this->id;
|
return $this->id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function withCommand(array $command): self
|
||||||
|
{
|
||||||
|
$this->command = $command;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function exec(array $command): string
|
||||||
|
{
|
||||||
|
$execConfig = (new ContainersIdExecPostBody())
|
||||||
|
->setCmd($command)
|
||||||
|
->setAttachStdout(true)
|
||||||
|
->setAttachStderr(true);
|
||||||
|
|
||||||
|
// Create and start the exec command
|
||||||
|
$exec = $this->dockerClient->containerExec($this->id, $execConfig);
|
||||||
|
$contents = $this->dockerClient
|
||||||
|
->execStart($exec->getId(), null, Client::FETCH_RESPONSE)
|
||||||
|
?->getBody()
|
||||||
|
->getContents() ?? '';
|
||||||
|
|
||||||
|
return preg_replace('/[\x00-\x1F\x7F]/u', '', $contents);
|
||||||
|
}
|
||||||
|
|
||||||
public function withEntryPoint(string $entryPoint): self
|
public function withEntryPoint(string $entryPoint): self
|
||||||
{
|
{
|
||||||
$this->entryPoint = $entryPoint;
|
$this->entryPoint = $entryPoint;
|
||||||
@@ -194,12 +221,6 @@ class GenericContainer
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function wait(): self
|
|
||||||
{
|
|
||||||
$this->wait->wait($this->id);
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function stop(): self
|
public function stop(): self
|
||||||
{
|
{
|
||||||
$this->dockerClient->containerStop($this->id);
|
$this->dockerClient->containerStop($this->id);
|
||||||
@@ -211,6 +232,8 @@ class GenericContainer
|
|||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$containerCreatePostBody = new ContainersCreatePostBody();
|
$containerCreatePostBody = new ContainersCreatePostBody();
|
||||||
|
//setup only if we need to expose ports
|
||||||
|
if(!empty($this->exposedPorts)) {
|
||||||
$portMap = new \ArrayObject();
|
$portMap = new \ArrayObject();
|
||||||
|
|
||||||
foreach ($this->exposedPorts as $port) {
|
foreach ($this->exposedPorts as $port) {
|
||||||
@@ -223,7 +246,9 @@ class GenericContainer
|
|||||||
$hostConfig = new HostConfig();
|
$hostConfig = new HostConfig();
|
||||||
$hostConfig->setPortBindings($portMap);
|
$hostConfig->setPortBindings($portMap);
|
||||||
$containerCreatePostBody->setHostConfig($hostConfig);
|
$containerCreatePostBody->setHostConfig($hostConfig);
|
||||||
|
}
|
||||||
$containerCreatePostBody->setImage($this->image);
|
$containerCreatePostBody->setImage($this->image);
|
||||||
|
$containerCreatePostBody->setCmd($this->command);
|
||||||
$envs = [];
|
$envs = [];
|
||||||
foreach ($this->env as $key => $value) {
|
foreach ($this->env as $key => $value) {
|
||||||
$envs[] = $key . '=' . $value;
|
$envs[] = $key . '=' . $value;
|
||||||
@@ -245,7 +270,9 @@ class GenericContainer
|
|||||||
if(!isset($this->wait)) {
|
if(!isset($this->wait)) {
|
||||||
$this->withWait(new WaitForContainerRunning());
|
$this->withWait(new WaitForContainerRunning());
|
||||||
}
|
}
|
||||||
$this->wait();
|
|
||||||
|
$this->wait->wait($this->id);
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,23 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Testcontainers\Tests\Integration;
|
||||||
|
|
||||||
|
use Testcontainers\Container\GenericContainer;
|
||||||
|
|
||||||
|
class GenericContainerTest extends ContainerTestCase
|
||||||
|
{
|
||||||
|
public static function setUpBeforeClass(): void
|
||||||
|
{
|
||||||
|
self::$container = (new GenericContainer('alpine'))
|
||||||
|
->withCommand(['tail', '-f', '/dev/null'])
|
||||||
|
->start();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testExec(): void
|
||||||
|
{
|
||||||
|
$actual = self::$container->exec(['echo', 'testcontainers']);
|
||||||
|
self::assertSame('testcontainers', $actual);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user