This commit is contained in:
Saeed Vaziry
2024-10-13 12:33:12 +02:00
parent 386d8e73a7
commit 224e0ac2b0
49 changed files with 3668 additions and 766 deletions

View File

@ -5,9 +5,12 @@
use App\Enums\ServiceStatus;
use App\Facades\SSH;
use App\Models\Server;
use App\Web\Pages\Servers\Services\Index;
use App\Web\Pages\Servers\Services\Widgets\ServicesList;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Http;
use Livewire\Livewire;
use Tests\TestCase;
class ServicesTest extends TestCase
@ -18,14 +21,13 @@ public function test_see_services_list(): void
{
$this->actingAs($this->user);
$this->get(route('servers.services', $this->server))
$this->get(Index::getUrl(['server' => $this->server]))
->assertSuccessful()
->assertSee('mysql')
->assertSee('nginx')
->assertSee('php')
->assertSee('supervisor')
->assertSee('redis')
->assertSee('vito-agent')
->assertSee('ufw');
}
@ -37,13 +39,16 @@ public function test_restart_service(string $name): void
$this->actingAs($this->user);
$service = $this->server->services()->where('name', $name)->firstOrFail();
$service->status = ServiceStatus::STOPPED;
$service->save();
SSH::fake('Active: active');
$this->get(route('servers.services.restart', [
Livewire::test(ServicesList::class, [
'server' => $this->server,
'service' => $service,
]))->assertSessionDoesntHaveErrors();
])
->callTableAction('restart', $service->id)
->assertSuccessful();
$service->refresh();
@ -61,10 +66,11 @@ public function test_failed_to_restart_service(string $name): void
SSH::fake('Active: inactive');
$this->get(route('servers.services.restart', [
Livewire::test(ServicesList::class, [
'server' => $this->server,
'service' => $service,
]))->assertSessionDoesntHaveErrors();
])
->callTableAction('restart', $service->id)
->assertSuccessful();
$service->refresh();
@ -82,10 +88,11 @@ public function test_stop_service(string $name): void
SSH::fake('Active: inactive');
$this->get(route('servers.services.stop', [
Livewire::test(ServicesList::class, [
'server' => $this->server,
'service' => $service,
]))->assertSessionDoesntHaveErrors();
])
->callTableAction('stop', $service->id)
->assertSuccessful();
$service->refresh();
@ -103,10 +110,11 @@ public function test_failed_to_stop_service(string $name): void
SSH::fake('Active: active');
$this->get(route('servers.services.stop', [
Livewire::test(ServicesList::class, [
'server' => $this->server,
'service' => $service,
]))->assertSessionDoesntHaveErrors();
])
->callTableAction('stop', $service->id)
->assertSuccessful();
$service->refresh();
@ -121,13 +129,16 @@ public function test_start_service(string $name): void
$this->actingAs($this->user);
$service = $this->server->services()->where('name', $name)->firstOrFail();
$service->status = ServiceStatus::STOPPED;
$service->save();
SSH::fake('Active: active');
$this->get(route('servers.services.start', [
Livewire::test(ServicesList::class, [
'server' => $this->server,
'service' => $service,
]))->assertSessionDoesntHaveErrors();
])
->callTableAction('start', $service->id)
->assertSuccessful();
$service->refresh();
@ -145,10 +156,11 @@ public function test_failed_to_start_service(string $name): void
SSH::fake('Active: inactive');
$this->get(route('servers.services.start', [
Livewire::test(ServicesList::class, [
'server' => $this->server,
'service' => $service,
]))->assertSessionDoesntHaveErrors();
])
->callTableAction('start', $service->id)
->assertSuccessful();
$service->refresh();
@ -163,13 +175,16 @@ public function test_enable_service(string $name): void
$this->actingAs($this->user);
$service = $this->server->services()->where('name', $name)->firstOrFail();
$service->status = ServiceStatus::DISABLED;
$service->save();
SSH::fake('Active: active');
$this->get(route('servers.services.enable', [
Livewire::test(ServicesList::class, [
'server' => $this->server,
'service' => $service,
]))->assertSessionDoesntHaveErrors();
])
->callTableAction('enable', $service->id)
->assertSuccessful();
$service->refresh();
@ -187,10 +202,11 @@ public function test_failed_to_enable_service(string $name): void
SSH::fake('Active: inactive');
$this->get(route('servers.services.enable', [
Livewire::test(ServicesList::class, [
'server' => $this->server,
'service' => $service,
]))->assertSessionDoesntHaveErrors();
])
->callTableAction('enable', $service->id)
->assertSuccessful();
$service->refresh();
@ -208,10 +224,11 @@ public function test_disable_service(string $name): void
SSH::fake('Active: inactive');
$this->get(route('servers.services.disable', [
Livewire::test(ServicesList::class, [
'server' => $this->server,
'service' => $service,
]))->assertSessionDoesntHaveErrors();
])
->callTableAction('disable', $service->id)
->assertSuccessful();
$service->refresh();
@ -229,10 +246,11 @@ public function test_failed_to_disable_service(string $name): void
SSH::fake('Active: active');
$this->get(route('servers.services.disable', [
Livewire::test(ServicesList::class, [
'server' => $this->server,
'service' => $service,
]))->assertSessionDoesntHaveErrors();
])
->callTableAction('disable', $service->id)
->assertSuccessful();
$service->refresh();
@ -262,13 +280,15 @@ public function test_install_service(string $name, string $type, string $version
if (! File::exists($keys['public_key_path']) || ! File::exists($keys['private_key_path'])) {
$server->provider()->generateKeyPair();
}
$this->post(route('servers.services.install', [
Livewire::test(Index::class, [
'server' => $server,
]), [
'name' => $name,
'type' => $type,
'version' => $version,
])->assertSessionDoesntHaveErrors();
])
->callAction('install', [
'name' => $name,
'version' => $version,
])
->assertSuccessful();
$this->assertDatabaseHas('services', [
'server_id' => $server->id,