mirror of
https://github.com/vitodeploy/vito.git
synced 2025-07-02 14:36:17 +00:00
40
tests/Feature/MetricsTest.php
Normal file
40
tests/Feature/MetricsTest.php
Normal file
@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Enums\ServiceStatus;
|
||||
use App\Models\Service;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
class MetricsTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_visit_metrics(): void
|
||||
{
|
||||
$this->actingAs($this->user);
|
||||
|
||||
Service::factory()->create([
|
||||
'server_id' => $this->server->id,
|
||||
'name' => 'vito-agent',
|
||||
'type' => 'monitoring',
|
||||
'version' => 'latest',
|
||||
'status' => ServiceStatus::READY,
|
||||
]);
|
||||
|
||||
$this->get(route('servers.metrics', ['server' => $this->server]))
|
||||
->assertSee('CPU Load')
|
||||
->assertSee('Memory Usage')
|
||||
->assertSee('Disk Usage')
|
||||
->assertSee('Resource Usage');
|
||||
}
|
||||
|
||||
public function test_cannot_visit_metrics(): void
|
||||
{
|
||||
$this->actingAs($this->user);
|
||||
|
||||
$this->get(route('servers.metrics', ['server' => $this->server]))
|
||||
->assertRedirect(route('servers.services', ['server' => $this->server]));
|
||||
}
|
||||
}
|
@ -4,7 +4,10 @@
|
||||
|
||||
use App\Enums\ServiceStatus;
|
||||
use App\Facades\SSH;
|
||||
use App\Models\Server;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\File;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Tests\TestCase;
|
||||
|
||||
class ServicesTest extends TestCase
|
||||
@ -22,6 +25,7 @@ public function test_see_services_list(): void
|
||||
->assertSee('php')
|
||||
->assertSee('supervisor')
|
||||
->assertSee('redis')
|
||||
->assertSee('vito-agent')
|
||||
->assertSee('ufw');
|
||||
}
|
||||
|
||||
@ -235,6 +239,45 @@ public function test_failed_to_disable_service(string $name): void
|
||||
$this->assertEquals(ServiceStatus::FAILED, $service->status);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider installData
|
||||
*/
|
||||
public function test_install_service(string $name, string $type, string $version): void
|
||||
{
|
||||
Http::fake([
|
||||
'https://api.github.com/repos/vito/vito-agent/releases/latest' => Http::response([
|
||||
'tag_name' => '0.1.0',
|
||||
]),
|
||||
]);
|
||||
SSH::fake('Active: active');
|
||||
|
||||
$this->actingAs($this->user);
|
||||
|
||||
$server = Server::factory()->create([
|
||||
'user_id' => $this->user->id,
|
||||
'project_id' => $this->user->current_project_id,
|
||||
]);
|
||||
|
||||
$keys = $server->sshKey();
|
||||
if (! File::exists($keys['public_key_path']) || ! File::exists($keys['private_key_path'])) {
|
||||
$server->provider()->generateKeyPair();
|
||||
}
|
||||
$this->post(route('servers.services.install', [
|
||||
'server' => $server,
|
||||
]), [
|
||||
'name' => $name,
|
||||
'type' => $type,
|
||||
'version' => $version,
|
||||
])->assertSessionDoesntHaveErrors();
|
||||
|
||||
$this->assertDatabaseHas('services', [
|
||||
'server_id' => $server->id,
|
||||
'name' => $name,
|
||||
'type' => $type,
|
||||
'status' => ServiceStatus::READY,
|
||||
]);
|
||||
}
|
||||
|
||||
public static function data(): array
|
||||
{
|
||||
return [
|
||||
@ -247,4 +290,50 @@ public static function data(): array
|
||||
['mysql'],
|
||||
];
|
||||
}
|
||||
|
||||
public static function installData(): array
|
||||
{
|
||||
return [
|
||||
[
|
||||
'nginx',
|
||||
'webserver',
|
||||
'latest',
|
||||
],
|
||||
[
|
||||
'php',
|
||||
'php',
|
||||
'7.4',
|
||||
],
|
||||
[
|
||||
'supervisor',
|
||||
'process_manager',
|
||||
'latest',
|
||||
],
|
||||
[
|
||||
'redis',
|
||||
'memory_database',
|
||||
'latest',
|
||||
],
|
||||
[
|
||||
'mysql',
|
||||
'database',
|
||||
'8.0',
|
||||
],
|
||||
[
|
||||
'mariadb',
|
||||
'database',
|
||||
'10.4',
|
||||
],
|
||||
[
|
||||
'postgresql',
|
||||
'database',
|
||||
'16',
|
||||
],
|
||||
[
|
||||
'vito-agent',
|
||||
'monitoring',
|
||||
'latest',
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
156
tests/Unit/Actions/Service/InstallTest.php
Normal file
156
tests/Unit/Actions/Service/InstallTest.php
Normal file
@ -0,0 +1,156 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Actions\Service;
|
||||
|
||||
use App\Actions\Service\Install;
|
||||
use App\Enums\ServiceStatus;
|
||||
use App\Facades\SSH;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Tests\TestCase;
|
||||
|
||||
class InstallTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_install_vito_agent(): void
|
||||
{
|
||||
SSH::fake('Active: active');
|
||||
Http::fake([
|
||||
'https://api.github.com/repos/vitodeploy/agent/tags' => Http::response([['name' => '0.1.0']]),
|
||||
]);
|
||||
|
||||
$service = app(Install::class)->install($this->server, [
|
||||
'type' => 'monitoring',
|
||||
'name' => 'vito-agent',
|
||||
'version' => 'latest',
|
||||
]);
|
||||
|
||||
$this->assertDatabaseHas('services', [
|
||||
'server_id' => $this->server->id,
|
||||
'name' => 'vito-agent',
|
||||
'type' => 'monitoring',
|
||||
'version' => '0.1.0',
|
||||
'status' => ServiceStatus::READY,
|
||||
]);
|
||||
|
||||
$this->assertNotNull($service->type_data);
|
||||
}
|
||||
|
||||
public function test_install_vito_agent_failed(): void
|
||||
{
|
||||
$this->expectExceptionMessage('Failed to fetch tags');
|
||||
SSH::fake('Active: inactive');
|
||||
Http::fake([
|
||||
'https://api.github.com/repos/vitodeploy/agent/tags' => Http::response([]),
|
||||
]);
|
||||
app(Install::class)->install($this->server, [
|
||||
'type' => 'monitoring',
|
||||
'name' => 'vito-agent',
|
||||
'version' => 'latest',
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_install_nginx(): void
|
||||
{
|
||||
$this->server->webserver()->delete();
|
||||
|
||||
SSH::fake('Active: active');
|
||||
|
||||
$service = app(Install::class)->install($this->server, [
|
||||
'type' => 'webserver',
|
||||
'name' => 'nginx',
|
||||
'version' => 'latest',
|
||||
]);
|
||||
|
||||
$this->assertDatabaseHas('services', [
|
||||
'server_id' => $this->server->id,
|
||||
'name' => 'nginx',
|
||||
'type' => 'webserver',
|
||||
'version' => 'latest',
|
||||
'status' => ServiceStatus::READY,
|
||||
]);
|
||||
|
||||
$this->assertNotNull($service->type_data);
|
||||
}
|
||||
|
||||
public function test_install_mysql(): void
|
||||
{
|
||||
$this->server->database()->delete();
|
||||
|
||||
SSH::fake('Active: active');
|
||||
|
||||
$service = app(Install::class)->install($this->server, [
|
||||
'type' => 'database',
|
||||
'name' => 'mysql',
|
||||
'version' => '8.0',
|
||||
]);
|
||||
|
||||
$this->assertDatabaseHas('services', [
|
||||
'server_id' => $this->server->id,
|
||||
'name' => 'mysql',
|
||||
'type' => 'database',
|
||||
'version' => '8.0',
|
||||
'status' => ServiceStatus::READY,
|
||||
]);
|
||||
|
||||
$this->assertNotNull($service->type_data);
|
||||
}
|
||||
|
||||
public function test_install_mysql_failed(): void
|
||||
{
|
||||
$this->expectException(ValidationException::class);
|
||||
app(Install::class)->install($this->server, [
|
||||
'type' => 'database',
|
||||
'name' => 'mysql',
|
||||
'version' => '8.0',
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_install_supervisor(): void
|
||||
{
|
||||
$this->server->processManager()->delete();
|
||||
|
||||
SSH::fake('Active: active');
|
||||
|
||||
$service = app(Install::class)->install($this->server, [
|
||||
'type' => 'process_manager',
|
||||
'name' => 'supervisor',
|
||||
'version' => 'latest',
|
||||
]);
|
||||
|
||||
$this->assertDatabaseHas('services', [
|
||||
'server_id' => $this->server->id,
|
||||
'name' => 'supervisor',
|
||||
'type' => 'process_manager',
|
||||
'version' => 'latest',
|
||||
'status' => ServiceStatus::READY,
|
||||
]);
|
||||
|
||||
$this->assertNotNull($service->type_data);
|
||||
}
|
||||
|
||||
public function test_install_redis(): void
|
||||
{
|
||||
$this->server->memoryDatabase()->delete();
|
||||
|
||||
SSH::fake('Active: active');
|
||||
|
||||
$service = app(Install::class)->install($this->server, [
|
||||
'type' => 'memory_database',
|
||||
'name' => 'redis',
|
||||
'version' => 'latest',
|
||||
]);
|
||||
|
||||
$this->assertDatabaseHas('services', [
|
||||
'server_id' => $this->server->id,
|
||||
'name' => 'redis',
|
||||
'type' => 'memory_database',
|
||||
'version' => 'latest',
|
||||
'status' => ServiceStatus::READY,
|
||||
]);
|
||||
|
||||
$this->assertNotNull($service->type_data);
|
||||
}
|
||||
}
|
86
tests/Unit/Actions/Service/UninstallTest.php
Normal file
86
tests/Unit/Actions/Service/UninstallTest.php
Normal file
@ -0,0 +1,86 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Actions\Service;
|
||||
|
||||
use App\Actions\Service\Uninstall;
|
||||
use App\Enums\ServiceStatus;
|
||||
use App\Facades\SSH;
|
||||
use App\Models\Database;
|
||||
use App\Models\Queue;
|
||||
use App\Models\Service;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Tests\TestCase;
|
||||
|
||||
class UninstallTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_uninstall_vito_agent(): void
|
||||
{
|
||||
SSH::fake();
|
||||
|
||||
Service::factory()->create([
|
||||
'server_id' => $this->server->id,
|
||||
'name' => 'vito-agent',
|
||||
'type' => 'monitoring',
|
||||
'version' => 'latest',
|
||||
'status' => ServiceStatus::READY,
|
||||
]);
|
||||
|
||||
app(Uninstall::class)->uninstall($this->server->monitoring());
|
||||
|
||||
$this->assertDatabaseMissing('services', [
|
||||
'server_id' => $this->server->id,
|
||||
'name' => 'vito-agent',
|
||||
'type' => 'monitoring',
|
||||
'version' => 'latest',
|
||||
'status' => ServiceStatus::READY,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Cannot uninstall nginx because some sites using it
|
||||
*/
|
||||
public function test_cannot_uninstall_nginx(): void
|
||||
{
|
||||
SSH::fake();
|
||||
|
||||
$this->expectException(ValidationException::class);
|
||||
|
||||
app(Uninstall::class)->uninstall($this->server->webserver());
|
||||
}
|
||||
|
||||
/**
|
||||
* Cannot uninstall mysql because some databases exist
|
||||
*/
|
||||
public function test_cannot_uninstall_mysql(): void
|
||||
{
|
||||
SSH::fake();
|
||||
|
||||
Database::factory()->create([
|
||||
'server_id' => $this->server->id,
|
||||
]);
|
||||
|
||||
$this->expectException(ValidationException::class);
|
||||
|
||||
app(Uninstall::class)->uninstall($this->server->database());
|
||||
}
|
||||
|
||||
/**
|
||||
* Cannot uninstall supervisor because some queues exist
|
||||
*/
|
||||
public function test_cannot_uninstall_supervisor(): void
|
||||
{
|
||||
SSH::fake();
|
||||
|
||||
Queue::factory()->create([
|
||||
'server_id' => $this->server->id,
|
||||
'site_id' => $this->site->id,
|
||||
]);
|
||||
|
||||
$this->expectException(ValidationException::class);
|
||||
|
||||
app(Uninstall::class)->uninstall($this->server->processManager());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user