2023-03-20 21:17:28 +00:00
|
|
|
<?php
|
|
|
|
|
2023-04-01 19:50:57 +00:00
|
|
|
use App\Actions\RemoteProcess\TidyOutput;
|
2023-03-30 08:04:44 +00:00
|
|
|
use App\Models\User;
|
2023-03-29 10:27:02 +00:00
|
|
|
use App\Models\Server;
|
2023-03-30 19:24:43 +00: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 21:17:28 +00:00
|
|
|
|
|
|
|
it('starts a docker container correctly', function () {
|
|
|
|
|
2023-03-31 16:54:55 +00:00
|
|
|
|
2023-03-30 19:24:43 +00:00
|
|
|
test()->actingAs(User::factory([
|
|
|
|
'uuid' => Str::uuid(),
|
|
|
|
'email' => Str::uuid().'@example.com',
|
|
|
|
])->create());
|
2023-03-30 08:04:44 +00:00
|
|
|
|
2023-03-20 21:17:28 +00:00
|
|
|
$coolifyNamePrefix = 'coolify_test_';
|
2023-03-30 19:24:43 +00:00
|
|
|
|
|
|
|
|
2023-03-20 21:17:28 +00: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');
|
2023-03-29 10:27:02 +00:00
|
|
|
$host = Server::where('name', 'testing-local-docker-container')->first();
|
2023-03-20 21:17:28 +00:00
|
|
|
|
2023-04-01 19:50:57 +00:00
|
|
|
remoteProcess([
|
|
|
|
"docker rm -f $(docker ps --filter='name={$coolifyNamePrefix}*' -aq)"
|
|
|
|
], $host);
|
|
|
|
|
2023-03-20 21:17:28 +00:00
|
|
|
// Assert there's no containers start with coolify_test_*
|
2023-03-29 10:27:02 +00:00
|
|
|
$activity = remoteProcess([$areThereCoolifyTestContainers], $host);
|
2023-04-01 19:50:57 +00:00
|
|
|
$tidyOutput = (new TidyOutput($activity))();
|
|
|
|
$containers = formatDockerCmdOutputToJson($tidyOutput);
|
2023-03-20 21:17:28 +00:00
|
|
|
expect($containers)->toBeEmpty();
|
|
|
|
|
|
|
|
// start a container nginx -d --name = $containerName
|
2023-03-29 10:27:02 +00:00
|
|
|
$activity = remoteProcess(["docker run -d --rm --name {$containerName} nginx"], $host);
|
2023-03-21 09:08:36 +00:00
|
|
|
expect($activity->getExtraProperty('exitCode'))->toBe(0);
|
2023-03-20 21:17:28 +00:00
|
|
|
|
|
|
|
// docker ps name = $container
|
2023-03-29 10:27:02 +00:00
|
|
|
$activity = remoteProcess([$areThereCoolifyTestContainers], $host);
|
2023-04-01 19:50:57 +00:00
|
|
|
$tidyOutput = (new TidyOutput($activity))();
|
|
|
|
$containers = formatDockerCmdOutputToJson($tidyOutput);
|
2023-03-20 21:17:28 +00:00
|
|
|
expect($containers->where('Names', $containerName)->count())->toBe(1);
|
|
|
|
|
|
|
|
// Stop testing containers
|
2023-03-30 19:24:43 +00:00
|
|
|
$activity = remoteProcess([
|
2023-03-31 16:54:55 +00:00
|
|
|
"docker ps --filter='name={$coolifyNamePrefix}*' -aq && " .
|
|
|
|
"docker rm -f $(docker ps --filter='name={$coolifyNamePrefix}*' -aq)"
|
2023-03-30 19:24:43 +00:00
|
|
|
], $host);
|
2023-03-21 09:08:36 +00:00
|
|
|
expect($activity->getExtraProperty('exitCode'))->toBe(0);
|
2023-03-20 21:17:28 +00:00
|
|
|
});
|