Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions src/Orchestration/Adapter/DockerCLI.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}");
}
Expand Down Expand Up @@ -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 = '';
Expand Down Expand Up @@ -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 = [];
Expand Down
49 changes: 46 additions & 3 deletions tests/Orchestration/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Utopia\Tests;

use Utopia\CLI\Console;
use PHPUnit\Framework\TestCase;
use Utopia\Orchestration\Orchestration;

Expand Down Expand Up @@ -474,9 +475,23 @@ 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);

$output = '';
Console::execute("docker inspect $containerId1", '', $output);
$dump($output);

$containerId2 = static::getOrchestration()->run(
'containerstack/alpine-stress',
'UsageStats2',
Expand All @@ -488,18 +503,46 @@ 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 inspect $containerId2", '', $output);
$dump($output);

$output = '';
static::getOrchestration()->execute($containerId1, ['which', 'screen'], $output);
$dump($output);
sleep(5);

$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, ['screen', '-d', '-m', 'stress --cpu 1 --timeout 5'], $output); // Run in screen so it's background task
$dump($output);
$output = '';
static::getOrchestration()->execute($containerId2, ['screen', '-d', '-m', '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();

Expand Down