fix issue with passing envs into the container. Remove container registry usage.

This commit is contained in:
Sergei Shitikov
2024-09-01 14:43:34 +02:00
parent 58f811ec8a
commit b0e4f60592
10 changed files with 60 additions and 46 deletions
+6 -7
View File
@@ -14,7 +14,6 @@ use Docker\API\Model\PortBinding;
use Docker\Docker;
use Psr\Http\Message\ResponseInterface;
use Testcontainers\ContainerRuntime\ContainerRuntimeClient;
use Testcontainers\Registry;
use Testcontainers\Wait\WaitForContainerRunning;
use Testcontainers\Wait\WaitInterface;
@@ -225,7 +224,11 @@ class GenericContainer
$hostConfig->setPortBindings($portMap);
$containerCreatePostBody->setHostConfig($hostConfig);
$containerCreatePostBody->setImage($this->image);
//$containerCreatePostBody->setEnv($this->env);
$envs = [];
foreach ($this->env as $key => $value) {
$envs[] = $key . '=' . $value;
}
$containerCreatePostBody->setEnv($envs);
$containerCreateResponse = $this->dockerClient->containerCreate($containerCreatePostBody);
$this->id = $containerCreateResponse?->getId() ?? '';
@@ -237,8 +240,6 @@ class GenericContainer
return $this->start();
}
Registry::add($this);
$this->dockerClient->containerStart($this->id);
if(!isset($this->wait)) {
@@ -260,8 +261,6 @@ class GenericContainer
$this->dockerClient->containerStop($this->id);
$this->dockerClient->containerDelete($this->id);
Registry::remove($this);
return $this;
}
@@ -317,7 +316,7 @@ class GenericContainer
{
$response = $this->dockerClient->containerInspect($this->id);
$settings = $response->getNetworkSettings();
var_dump($settings);
//var_dump($settings);
$ports = [];
foreach ($settings->getPorts() as $port => $value) {
+6 -11
View File
@@ -4,24 +4,19 @@ declare(strict_types=1);
namespace Testcontainers\Container;
use Testcontainers\Wait\WaitForExec;
class MariaDBContainer extends GenericContainer
{
private function __construct(string $version, string $mysqlRootPassword)
public function __construct(string $version = 'latest', string $mysqlRootPassword = 'root')
{
parent::__construct('mariadb:' . $version);
$this->withExposedPorts(3306);
$this->withEnvironment('MARIADB_ROOT_PASSWORD', $mysqlRootPassword);
$binary = 'mysqladmin';
if ($version === 'latest' || version_compare($version, '11.0.0', '>')) {
$binary = 'mariadb-admin';
}
$this->withWait(new WaitForExec([$binary, 'ping', '-h', '127.0.0.1']));
}
/**
* @deprecated Use constructor instead
* Left for backward compatibility
*/
public static function make(string $version = 'latest', string $mysqlRootPassword = 'root'): self
{
return new self($version, $mysqlRootPassword);
+6 -2
View File
@@ -8,13 +8,17 @@ use Testcontainers\Wait\WaitForExec;
class MySQLContainer extends GenericContainer
{
private function __construct(string $version, string $mysqlRootPassword)
public function __construct(string $version = 'latest', string $mysqlRootPassword = 'root')
{
parent::__construct('mysql:' . $version);
$this->withExposedPorts(3306);
$this->withEnvironment('MYSQL_ROOT_PASSWORD', $mysqlRootPassword);
$this->withWait(new WaitForExec(['mysqladmin', 'ping', '-h', '127.0.0.1']));
}
/**
* @deprecated Use constructor instead
* Left for backward compatibility
*/
public static function make(string $version = 'latest', string $mysqlRootPassword = 'root'): self
{
return new self($version, $mysqlRootPassword);
+15 -2
View File
@@ -8,23 +8,36 @@ use Testcontainers\Wait\WaitForHttp;
class OpenSearchContainer extends GenericContainer
{
private function __construct(string $version)
public function __construct(string $version = 'latest')
{
parent::__construct('opensearchproject/opensearch:' . $version);
$this->withExposedPorts(9200);
$this->withEnvironment('discovery.type', 'single-node');
$this->withEnvironment('OPENSEARCH_INITIAL_ADMIN_PASSWORD', 'c3o_ZPHo!');
$this->withWait(WaitForHttp::make(9200));
}
/**
* @deprecated Use constructor instead
* Left for backward compatibility
*/
public static function make(string $version = 'latest'): self
{
return new self($version);
}
public function disableSecurityPlugin(): self
public function withDisabledSecurityPlugin(): self
{
$this->withEnvironment('plugins.security.disabled', 'true');
return $this;
}
/**
* @deprecated Use withDisabledSecurityPlugin instead
*/
public function disableSecurityPlugin(): self
{
return $this->withDisabledSecurityPlugin();
}
}
+5 -1
View File
@@ -8,13 +8,17 @@ use Testcontainers\Wait\WaitForExec;
class PostgresContainer extends GenericContainer
{
private function __construct(string $version, string $rootPassword)
public function __construct(string $version = 'latest', string $rootPassword = 'root')
{
parent::__construct('postgres:' . $version);
$this->withEnvironment('POSTGRES_PASSWORD', $rootPassword);
$this->withWait(new WaitForExec(["pg_isready", "-h", "127.0.0.1"]));
}
/**
* @deprecated Use constructor instead
* Left for backward compatibility
*/
public static function make(string $version = 'latest', string $dbPassword = 'root'): self
{
return new self($version, $dbPassword);
+7 -2
View File
@@ -8,12 +8,17 @@ use Testcontainers\Wait\WaitForLog;
class RedisContainer extends GenericContainer
{
private function __construct(string $version)
public function __construct(string $version = 'latest')
{
parent::__construct('redis:' . $version);
$this->withWait(new WaitForLog('Ready to accept connections'));
$this->withExposedPorts(6379);
//$this->withWait(new WaitForLog('Ready to accept connections'));
}
/**
* @deprecated Use constructor instead
* Left for backward compatibility
*/
public static function make(string $version = 'latest'): self
{
return new self($version);
+2
View File
@@ -41,6 +41,8 @@ class WaitForContainerRunning implements WaitInterface
return;
}
var_dump($containerStatus);
usleep($this->pollInterval * 1000);
}
}
-1
View File
@@ -8,7 +8,6 @@ use Closure;
use Docker\API\Model\ContainersIdExecPostBody;
use Docker\API\Model\ExecIdStartPostBody;
use Docker\Docker;
use Testcontainers\Exception\ContainerNotReadyException;
class WaitForExec implements WaitInterface
{
+13 -16
View File
@@ -4,33 +4,30 @@ declare(strict_types=1);
namespace Testcontainers\Tests\Integration;
use PHPUnit\Framework\TestCase;
use Testcontainers\Container\GenericContainer;
use Testcontainers\Registry;
use Predis\Client;
use Testcontainers\Container\RedisContainer;
class RedisContainerTest extends TestCase
class RedisContainerTest extends ContainerTestCase
{
// public static function tearDownAfterClass(): void
// {
// parent::tearDownAfterClass();
//
// Registry::cleanup();
// }
public static function setUpBeforeClass(): void
{
self::$container = (new RedisContainer())
->start();
}
public function testRedisContainer(): void
{
$redisContainer = (new GenericContainer('redis:alpine'))
->withExposedPorts(6379)
->start();
$redisClient = new \Predis\Client([
$redisClient = new Client([
'host' => 'localhost',
'port' => 6379,
]);
$redisClient->ping();
$this->assertTrue($redisClient->isConnected());
$redisClient->set('greetings', 'Hello, World!');
$this->assertEquals('Hello, World!', $redisClient->get('greetings'));
$redisContainer->remove();
}
}
-4
View File
@@ -10,8 +10,6 @@ use Predis\Connection\ConnectionException;
use Symfony\Component\Process\Process;
use Testcontainers\Container\GenericContainer;
use Testcontainers\Exception\ContainerNotReadyException;
use Testcontainers\Registry;
use Testcontainers\Trait\DockerContainerAwareTrait;
use Testcontainers\Wait\WaitForExec;
use Testcontainers\Wait\WaitForHealthCheck;
use Testcontainers\Wait\WaitForHttp;
@@ -24,8 +22,6 @@ class WaitStrategyTest extends TestCase
public static function tearDownAfterClass(): void
{
parent::tearDownAfterClass();
Registry::cleanup();
}
public function testWaitForExec(): void