diff --git a/src/Orchestration/Adapter/DockerCLI.php b/src/Orchestration/Adapter/DockerCLI.php index a24fc73..9dbc4c6 100644 --- a/src/Orchestration/Adapter/DockerCLI.php +++ b/src/Orchestration/Adapter/DockerCLI.php @@ -113,6 +113,12 @@ public function getStats(string $container = null, array $filters = []): array $result = Console::execute('docker stats --no-trunc --format "id={{.ID}}&name={{.Name}}&cpu={{.CPUPerc}}&memory={{.MemPerc}}&diskIO={{.BlockIO}}&memoryIO={{.MemUsage}}&networkIO={{.NetIO}}" --no-stream'.$containersString, '', $output); + $dump = function($value) { + $p = var_export($value, true); + $b = debug_backtrace(); + print($b[0]['file'] . ':' . $b[0]['line'] . ' - ' . $p . "\n"); + }; + $dump($output); if ($result !== 0) { throw new Orchestration("Docker Error: {$output}"); } @@ -312,9 +318,7 @@ public function run(string $image, $output = ''; foreach ($command as $key => $value) { - if (str_contains($value, ' ')) { - $command[$key] = "'".$value."'"; - } + $command[$key] = \escapeshellarg($command[$key]); } $labelString = ''; @@ -389,9 +393,7 @@ public function execute( int $timeout = -1 ): bool { foreach ($command as $key => $value) { - if (str_contains($value, ' ')) { - $command[$key] = "'".$value."'"; - } + $command[$key] = \escapeshellarg($command[$key]); } $parsedVariables = []; diff --git a/tests/Orchestration/Base.php b/tests/Orchestration/Base.php index 8679f6c..294e7d7 100644 --- a/tests/Orchestration/Base.php +++ b/tests/Orchestration/Base.php @@ -2,6 +2,7 @@ namespace Utopia\Tests; +use Utopia\CLI\Console; use PHPUnit\Framework\TestCase; use Utopia\Orchestration\Orchestration; @@ -474,6 +475,16 @@ public function testUsageStats(): void mountFolder: __DIR__.'/Resources', labels: ['utopia-container-type' => 'stats'] ); + $dump = function($value) { + $p = var_export($value, true); + $b = debug_backtrace(); + print($b[0]['file'] . ':' . $b[0]['line'] . ' - ' . $p . "\n"); + }; + $dump($containerId1); + + $output = ''; + Console::execute("docker logs $containerId1", '', $output); + $dump($output); $this->assertNotEmpty($containerId1); @@ -488,18 +499,38 @@ public function testUsageStats(): void workdir: '/usr/local/src/', mountFolder: __DIR__.'/Resources', ); + $dump($containerId2); + + $output = ''; + Console::execute("docker logs $containerId2", '', $output); + $dump($output); $this->assertNotEmpty($containerId2); sleep(2); + $output = ''; + Console::execute('docker ps -a', '', $output); + $dump($output); + + $output = ''; + Console::execute('docker stats --no-stream', '', $output); + $dump($output); + // This allows CPU-heavy load check $output = ''; - static::getOrchestration()->execute($containerId1, ['screen', '-d', '-m', "'stress --cpu 1 --timeout 5'"], $output); // Run in screen so it's background task - static::getOrchestration()->execute($containerId2, ['screen', '-d', '-m', "'stress --cpu 1 --timeout 5'"], $output); + static::getOrchestration()->execute($containerId1, ['stress --cpu 1 --timeout 5'], $output); // Run in screen so it's background task + $dump($output); + $output = ''; + static::getOrchestration()->execute($containerId2, ['stress --cpu 1 --timeout 5'], $output); + $dump($output); // Set CPU stress-test start \sleep(1); + $output = ''; + Console::execute('docker stats --no-stream', '', $output); + $dump($output); + // Fetch stats, should include high CPU usage $stats = static::getOrchestration()->getStats();