PhyrePanel-mirror/web/tests/Unit/DockerTest.php
2024-04-30 11:51:35 +03:00

122 lines
4.4 KiB
PHP

<?php
namespace tests\Unit;
use App\Filament\Pages\Modules;
use App\Models\Module;
use App\Models\User;
use Filament\Actions\DeleteAction;
use Livewire\Livewire;
use Modules\Docker\App\Models\DockerContainer;
use Modules\Docker\Filament\Clusters\Docker\Pages\DockerCatalog;
use Modules\Docker\Filament\Clusters\Docker\Resources\DockerContainerResource\Pages\CreateDockerContainer;
use Modules\Docker\Filament\Clusters\Docker\Resources\DockerContainerResource\Pages\EditDockerContainer;
use Modules\Docker\Filament\Clusters\Docker\Resources\DockerContainerResource\Pages\ListDockerContainers;
use Modules\Docker\PostInstall;
use Psy\Util\Str;
use Tests\TestCase;
class DockerTest extends TestCase
{
public function testDockerImages()
{
ini_set('memory_limit', '-1');
ini_set('max_execution_time', 0);
$this->actingAs(User::factory()->create());
if (!is_file('/usr/local/phyre/web/storage/installed')) {
file_put_contents('/usr/local/phyre/web/storage/installed', '');
}
$modulesTest = Livewire::test(Modules::class);
$modulesTest->call('openInstallModal', 'Docker');
$installLogFilePath = $modulesTest->get('installLogFilePath');
$dockerIsInstalled = false;
for ($i = 0; $i < 400; $i++) {
$modulesTest->call('getInstallLog');
$logFile = file_get_contents($installLogFilePath);
if (strpos($logFile, 'Done!') !== false) {
$dockerIsInstalled = true;
break;
}
sleep(1);
}
$this->assertTrue($dockerIsInstalled);
$findModule = Module::where('name', 'Docker')->first();
$this->assertNotEmpty($findModule);
$dockerImage = 'nginx';
$dockerCatalogTest = Livewire::test(DockerCatalog::class);
$livewireCatalogIndex = $dockerCatalogTest->set('keyword', $dockerImage)
->assertSee($dockerImage);
$viewData = $livewireCatalogIndex->viewData('dockerImages');
$this->assertNotEmpty($viewData);
$livewireCatalogIndex->set('keyword', 'non-existing-image')
->assertDontSee('non-existing-image');
$pullDockerImage = $dockerCatalogTest->call('pullDockerImage', $dockerImage)
->assertSee('Pull Docker Image');
$pullLog = '';
$isDockerImagePulled = false;
for ($i = 0; $i < 300; $i++) {
$pullLog = @file_get_contents($pullDockerImage->get('pullLogFile'));
if (str_contains($pullLog, 'DONE!')) {
$isDockerImagePulled = true;
break;
}
sleep(1);
}
$this->assertTrue($isDockerImagePulled);
$this->assertNotEmpty($pullLog);
$this->assertStringContainsString('DONE!', $pullLog);
$createDockerContainerTest = Livewire::test(CreateDockerContainer::class);
$createDockerContainerTest->assertSee('Create Docker Container');
$dockerName = \Illuminate\Support\Str::random(10);
$create = $createDockerContainerTest->fillForm([
'name' => $dockerName,
'image' => 'nginx',
'environmentVariables' => [
'PATH' => '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin',
'NGINX_VERSION' => '1.25.5',
'NJS_VERSION' => '0.8.4',
'NJS_RELEASE' => '2~bookworm',
'PKG_RELEASE' => '1~bookworm',
],
'volumeMapping' => [],
'port' => '83',
'externalPort' => '3000',
])->call('create');
$this->assertDatabaseHas(DockerContainer::class, [
'name' => $dockerName,
]);
//
// $listDockerContainersTest = Livewire::test(ListDockerContainers::class);
// $listDockerContainersTest->assertSee($dockerName);
//
// $findDockerContainer = DockerContainer::where('name', $dockerName)->first();
//
// $editDockerContainersTest = Livewire::test(EditDockerContainer::class, [
// 'record'=> $findDockerContainer->id
// ]);
// $editDockerContainersTest->assertSee('Edit Docker Container');
// $editDockerContainersTest->callAction(DeleteAction::class);
//
// $this->assertModelMissing($findDockerContainer);
//
//
// // Unistall docker
// $modulesTest = Livewire::test(Modules::class);
// $modulesTest->call('openUnistallModal', 'Docker');
}
}