coolify/tests/Feature/DockerCommandsTest.phpold

63 lines
2.2 KiB
Plaintext
Raw Normal View History

2023-03-20 22:17:28 +01:00
<?php
2023-05-03 07:15:45 +02:00
use App\Actions\CoolifyTask\RunRemoteProcess;
use App\Actions\CoolifyTask\TidyOutput;
use App\Models\Server;
use App\Models\User;
2023-03-30 21:24:43 +02:00
use Database\Seeders\DatabaseSeeder;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\RefreshDatabase;
uses(RefreshDatabase::class);
uses(DatabaseMigrations::class);
beforeEach(function () {
$this->seed(DatabaseSeeder::class);
});
2023-03-20 22:17:28 +01:00
it('starts a docker container correctly', function () {
2023-03-31 18:54:55 +02:00
2023-03-30 21:24:43 +02:00
test()->actingAs(User::factory([
'uuid' => Str::uuid(),
2023-05-24 14:26:50 +02:00
'email' => Str::uuid() . '@example.com',
2023-03-30 21:24:43 +02:00
])->create());
2023-03-30 10:04:44 +02:00
2023-03-20 22:17:28 +01:00
$coolifyNamePrefix = 'coolify_test_';
2023-03-30 21:24:43 +02:00
2023-03-20 22:17:28 +01:00
$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 22:17:28 +01:00
2023-05-24 15:25:08 +02:00
remote_process([
2023-04-12 13:39:26 +02:00
"docker rm -f $(docker ps --filter='name={$coolifyNamePrefix}*' -aq) > /dev/null 2>&1"
2023-04-01 21:50:57 +02:00
], $host);
2023-03-20 22:17:28 +01:00
// Assert there's no containers start with coolify_test_*
2023-05-24 15:25:08 +02:00
$activity = remote_process([$areThereCoolifyTestContainers], $host);
2023-04-07 16:58:45 +02:00
$tidyOutput = RunRemoteProcess::decodeOutput($activity);
2023-05-24 14:26:50 +02:00
$containers = format_docker_command_output_to_json($tidyOutput);
2023-03-20 22:17:28 +01:00
expect($containers)->toBeEmpty();
// start a container nginx -d --name = $containerName
2023-05-24 15:25:08 +02:00
$activity = remote_process(["docker run -d --rm --name {$containerName} nginx"], $host);
expect($activity->getExtraProperty('exitCode'))->toBe(0);
2023-03-20 22:17:28 +01:00
// docker ps name = $container
2023-05-24 15:25:08 +02:00
$activity = remote_process([$areThereCoolifyTestContainers], $host);
2023-04-07 16:58:45 +02:00
$tidyOutput = RunRemoteProcess::decodeOutput($activity);
2023-05-24 14:26:50 +02:00
$containers = format_docker_command_output_to_json($tidyOutput);
2023-03-20 22:17:28 +01:00
expect($containers->where('Names', $containerName)->count())->toBe(1);
// Stop testing containers
2023-05-24 15:25:08 +02:00
$activity = remote_process([
2023-03-31 18:54:55 +02:00
"docker ps --filter='name={$coolifyNamePrefix}*' -aq && " .
2023-08-11 20:48:52 +02:00
"docker rm -f $(docker ps --filter='name={$coolifyNamePrefix}*' -aq)"
2023-03-30 21:24:43 +02:00
], $host);
expect($activity->getExtraProperty('exitCode'))->toBe(0);
2023-03-20 22:17:28 +01:00
});