lasthourcloud/tests/Feature/DockerCommandsTest.php

37 lines
1.5 KiB
PHP
Raw Normal View History

2023-03-20 21:17:28 +00:00
<?php
2023-03-30 08:04:44 +00:00
use App\Models\User;
use App\Models\Server;
2023-03-20 21:17:28 +00:00
use Tests\Support\Output;
it('starts a docker container correctly', function () {
2023-03-30 08:04:44 +00:00
test()->actingAs(User::factory()->create());
2023-03-20 21:17:28 +00:00
$coolifyNamePrefix = 'coolify_test_';
$format = '{"ID":"{{ .ID }}", "Image": "{{ .Image }}", "Names":"{{ .Names }}"}';
$areThereCoolifyTestContainers = "docker ps --filter=\"name={$coolifyNamePrefix}*\" --format '{$format}' ";
// Generate a known name
$containerName = 'coolify_test_' . now()->format('Ymd_his');
$host = Server::where('name', 'testing-local-docker-container')->first();
2023-03-20 21:17:28 +00:00
// Assert there's no containers start with coolify_test_*
$activity = remoteProcess([$areThereCoolifyTestContainers], $host);
$containers = Output::containerList($activity->getExtraProperty('stdout'));
2023-03-20 21:17:28 +00:00
expect($containers)->toBeEmpty();
// start a container nginx -d --name = $containerName
$activity = remoteProcess(["docker run -d --rm --name {$containerName} nginx"], $host);
expect($activity->getExtraProperty('exitCode'))->toBe(0);
2023-03-20 21:17:28 +00:00
// docker ps name = $container
$activity = remoteProcess([$areThereCoolifyTestContainers], $host);
$containers = Output::containerList($activity->getExtraProperty('stdout'));
2023-03-20 21:17:28 +00:00
expect($containers->where('Names', $containerName)->count())->toBe(1);
// Stop testing containers
$activity = remoteProcess(["docker stop $(docker ps --filter='name={$coolifyNamePrefix}*' -q)"], $host);
expect($activity->getExtraProperty('exitCode'))->toBe(0);
2023-03-20 21:17:28 +00:00
});