Files

220 lines
9.1 KiB
PHP
Raw Permalink Normal View History

<?php
2026-10-05 18:10:18 -04:00
declare(strict_types=1);
2026-10-08 17:22:14 -04:00
namespace Pterodactyl\Tests\Pest\Integration\Actions\Servers\DeleteServerTest;
2026-10-05 18:10:18 -04:00
use Exception;
2026-10-08 17:40:00 -04:00
use Illuminate\Contracts\Debug\ExceptionHandler;
2026-10-05 18:10:18 -04:00
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Http;
2026-10-08 17:40:00 -04:00
use Pterodactyl\Contracts\Databases\DeletesDatabases;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Contracts\Servers\DeletesServers;
use Pterodactyl\Events\Server\OperationCompleted;
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
use Pterodactyl\Models\Database;
use Pterodactyl\Models\DatabaseHost;
2026-10-08 17:40:00 -04:00
use Pterodactyl\Models\Server;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Services\Databases\DatabaseHostGateway;
use Pterodactyl\Tests\Integration\IntegrationTestCase;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Tests\Support\Fakes\FakeDaemonServer;
use Pterodactyl\Tests\Support\Fakes\FakeDatabaseHostGateway;
use function pterodactylTestCase;
uses(IntegrationTestCase::class, DatabaseTransactions::class);
/**
* Stub out services that we don't want to test in here.
*/
2026-10-08 17:40:00 -04:00
beforeEach(function (): void {
2026-10-05 18:10:18 -04:00
$this->defaultLogger = config('logging.default');
// There will be some log calls during this test, don't actually write to the disk.
config()->set('logging.default', 'null');
$this->daemonServerRepository = new FakeDaemonServer;
$this->app->instance(DatabaseHostGateway::class, $this->gateway = new FakeDatabaseHostGateway());
});
/**
* Reset the log driver.
*/
2026-10-08 17:40:00 -04:00
afterEach(function (): void {
2026-10-05 18:10:18 -04:00
config()->set('logging.default', $this->defaultLogger);
$this->defaultLogger = null;
});
2026-10-08 17:40:00 -04:00
test('regular delete fails if wings returns error', function (): void {
2026-10-05 18:10:18 -04:00
$server = $this->createServerModel();
$this->daemonServerRepository->throwable = new DaemonConnectionException(Http::failedRequest([], 200));
try {
getService()->delete($server);
$this->fail('Expected DaemonConnectionException to be thrown.');
} catch (DaemonConnectionException) {
}
2026-10-08 17:40:00 -04:00
2026-10-05 18:10:18 -04:00
$this->daemonServerRepository->assertDeleted();
$this->assertDatabaseHas('servers', ['id' => $server->id]);
});
2026-10-08 17:40:00 -04:00
test('regular delete ignores404 from wings', function (): void {
2026-10-05 18:10:18 -04:00
$server = $this->createServerModel();
$this->daemonServerRepository->throwable = new DaemonConnectionException(Http::failedRequest([], 404));
getService()->delete($server);
$this->daemonServerRepository->assertDeleted();
$this->assertDatabaseMissing('servers', ['id' => $server->id]);
});
2026-10-08 17:40:00 -04:00
test('force delete ignores exception from wings', function (): void {
2026-10-05 18:10:18 -04:00
$server = $this->createServerModel();
$this->daemonServerRepository->throwable = new DaemonConnectionException(Http::failedRequest([], 500));
getService()->withForce()->delete($server);
$this->daemonServerRepository->assertDeleted();
$this->assertDatabaseMissing('servers', ['id' => $server->id]);
});
2026-10-08 17:40:00 -04:00
test('a remote database drop failure is reported without blocking the deletion', function (): void {
2026-10-05 18:10:18 -04:00
$server = $this->createServerModel();
$host = DatabaseHost::factory()->create();
2026-10-08 17:40:00 -04:00
/** @var Database $failing */
$failing = Database::factory()->create(['database_host_id' => $host->id, 'server_id' => $server->id]);
/** @var Database $other */
$other = Database::factory()->create(['database_host_id' => $host->id, 'server_id' => $server->id]);
2026-10-05 18:10:18 -04:00
$server->refresh();
$this->gateway->throwOn['dropDatabase'] = new Exception();
2026-10-08 17:40:00 -04:00
$reported = [];
$this->app->make(ExceptionHandler::class)->reportable(function (Exception $exception) use (&$reported): bool {
$reported[] = $exception;
return false;
});
getService()->delete($server);
2026-10-05 18:10:18 -04:00
$this->daemonServerRepository->assertDeleted();
2026-10-08 17:40:00 -04:00
$this->assertDatabaseMissing('servers', ['id' => $server->id]);
$this->assertDatabaseMissing('databases', ['id' => $failing->id]);
$this->assertDatabaseMissing('databases', ['id' => $other->id]);
// Every database is still attempted after the first drop fails.
expect($this->gateway->count('dropDatabase'))->toBe(2)
->and($reported)->toHaveCount(2);
2026-10-05 18:10:18 -04:00
});
2026-10-08 17:40:00 -04:00
test('remote databases are dropped after the server rows are deleted', function (): void {
$server = $this->createServerModel();
$host = DatabaseHost::factory()->create();
/** @var Database $db */
$db = Database::factory()->create(['database_host_id' => $host->id, 'server_id' => $server->id]);
$server->refresh();
$serverExisted = null;
$this->app->instance(DeletesDatabases::class, new class($serverExisted, $server->id) implements DeletesDatabases
{
public function __construct(private ?bool &$serverExisted, private readonly int $serverId) {}
public function delete(Database $database): bool
{
$this->serverExisted = Server::query()->whereKey($this->serverId)->exists();
return true;
}
});
getService()->delete($server);
expect($serverExisted)->toBeFalse();
$this->assertDatabaseMissing('databases', ['id' => $db->id]);
});
test('exception while deleting databases does not abort if force deleted', function (): void {
2026-10-05 18:10:18 -04:00
$server = $this->createServerModel();
$host = DatabaseHost::factory()->create();
/** @var Database $db */
$db = Database::factory()->create(['database_host_id' => $host->id, 'server_id' => $server->id]);
$server->refresh();
$this->gateway->throwOn['dropDatabase'] = new Exception();
getService()->withForce(true)->delete($server);
$this->daemonServerRepository->assertDeleted();
$this->assertDatabaseMissing('servers', ['id' => $server->id]);
$this->assertDatabaseMissing('databases', ['id' => $db->id]);
});
2026-10-08 19:17:30 -04:00
test('databases on different hosts are each dropped on their own host', function (): void {
$server = $this->createServerModel();
$first = DatabaseHost::factory()->create();
$second = DatabaseHost::factory()->create();
/** @var Database $a */
$a = Database::factory()->create(['database_host_id' => $first->id, 'server_id' => $server->id]);
/** @var Database $b */
$b = Database::factory()->create(['database_host_id' => $second->id, 'server_id' => $server->id]);
$server->refresh();
// Record which host the dynamic connection points at for each drop. Resolving the
// connection does not open a socket, so no real database host is needed.
$gateway = new class extends FakeDatabaseHostGateway
{
/** @var array<string, mixed> */
public array $hosts = [];
public function dropDatabase(string $database): bool
{
$this->hosts[$database] = DB::connection($this->getConnection())->getConfig('host');
return parent::dropDatabase($database);
}
};
$this->app->instance(DatabaseHostGateway::class, $gateway);
try {
getService()->delete($server);
} finally {
DB::purge(DatabaseHostGateway::DEFAULT_CONNECTION_NAME);
}
2026-10-11 14:09:34 -04:00
$this->assertActivityOn('server:database.delete', $server, $a);
$this->assertActivityOn('server:database.delete', $server, $b);
2026-10-08 19:17:30 -04:00
expect($gateway->hosts)->toBe([$a->database => $first->host, $b->database => $second->host]);
});
2026-10-05 18:10:18 -04:00
function getService(): DeletesServers
{
2026-10-08 17:40:00 -04:00
return (fn () => $this->app->make(DeletesServers::class))->call(pterodactylTestCase());
2026-10-05 18:10:18 -04:00
}
2026-10-08 17:40:00 -04:00
test('deleting a server reads database hosts once for all databases', function (): void {
2026-10-05 18:10:18 -04:00
$server = $this->createServerModel();
$host = DatabaseHost::factory()->create(['node_id' => $server->node_id]);
Database::factory()->count(10)->create(['server_id' => $server->id, 'database_host_id' => $host->id]);
/** @var FakeDatabaseHostGateway $gateway */
$gateway = $this->gateway;
DB::flushQueryLog();
DB::enableQueryLog();
try {
$this->app->make(DeletesServers::class)->delete($server);
$reads = array_filter(DB::getQueryLog(), fn (array $query): bool => str_starts_with(mb_strtolower($query['query']), 'select') && str_contains($query['query'], 'from `database_hosts`'));
expect($reads)->toHaveCount(1);
} finally {
DB::disableQueryLog();
}
2026-10-08 17:40:00 -04:00
2026-10-05 18:10:18 -04:00
expect($gateway->count('dropDatabase'))->toBe(10);
expect($gateway->count('dropUser'))->toBe(10);
expect($gateway->count('flush'))->toBe(10);
2026-10-08 17:40:00 -04:00
2026-10-05 18:10:18 -04:00
$this->daemonServerRepository->assertDeleted();
$this->assertDatabaseMissing('servers', ['id' => $server->id]);
$this->assertDatabaseMissing('databases', ['server_id' => $server->id]);
});
2026-10-08 17:40:00 -04:00
test('a deleted server is reported to operation listeners by uuid only', function (): void {
2026-10-05 18:10:18 -04:00
Event::fake([OperationCompleted::class]);
$server = $this->createServerModel();
$kept = $this->createServerModel();
$this->daemonServerRepository->throwable = new DaemonConnectionException(Http::failedRequest([], 500));
try {
getService()->delete($kept);
} catch (DaemonConnectionException) {
}
2026-10-08 17:40:00 -04:00
2026-10-05 18:10:18 -04:00
$this->daemonServerRepository->throwable = null;
2026-10-05 18:10:18 -04:00
getService()->delete($server);
2026-10-05 18:10:18 -04:00
Event::assertDispatchedTimes(OperationCompleted::class, 1);
Event::assertDispatched(OperationCompleted::class, fn (OperationCompleted $event): bool => $event->operation === 'delete'
&& $event->successful
&& $event->serverUuid === $server->uuid
&& $event->resourceUuid === $server->uuid);
});