2020-10-09 04:08:55 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Tests\Integration\Services\Servers;
|
|
|
|
|
2022-10-14 16:59:20 +00:00
|
|
|
use Mockery\MockInterface;
|
2020-10-09 04:08:55 +00:00
|
|
|
use GuzzleHttp\Psr7\Request;
|
|
|
|
use GuzzleHttp\Psr7\Response;
|
|
|
|
use Pterodactyl\Models\Database;
|
|
|
|
use Pterodactyl\Models\DatabaseHost;
|
|
|
|
use GuzzleHttp\Exception\BadResponseException;
|
|
|
|
use Pterodactyl\Tests\Integration\IntegrationTestCase;
|
|
|
|
use Pterodactyl\Services\Servers\ServerDeletionService;
|
|
|
|
use Pterodactyl\Repositories\Wings\DaemonServerRepository;
|
|
|
|
use Pterodactyl\Services\Databases\DatabaseManagementService;
|
|
|
|
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
|
|
|
|
|
|
|
|
class ServerDeletionServiceTest extends IntegrationTestCase
|
|
|
|
{
|
2022-10-14 16:59:20 +00:00
|
|
|
private MockInterface $daemonServerRepository;
|
2020-10-09 04:08:55 +00:00
|
|
|
|
2022-10-14 16:59:20 +00:00
|
|
|
private MockInterface $databaseManagementService;
|
2020-10-09 04:08:55 +00:00
|
|
|
|
2022-10-14 16:59:20 +00:00
|
|
|
private static ?string $defaultLogger;
|
2020-10-09 04:08:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Stub out services that we don't want to test in here.
|
|
|
|
*/
|
|
|
|
public function setUp(): void
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
self::$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');
|
|
|
|
|
2022-11-29 17:53:59 +00:00
|
|
|
$this->daemonServerRepository = \Mockery::mock(DaemonServerRepository::class);
|
|
|
|
$this->databaseManagementService = \Mockery::mock(DatabaseManagementService::class);
|
2020-10-09 04:08:55 +00:00
|
|
|
|
|
|
|
$this->app->instance(DaemonServerRepository::class, $this->daemonServerRepository);
|
|
|
|
$this->app->instance(DatabaseManagementService::class, $this->databaseManagementService);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset the log driver.
|
|
|
|
*/
|
|
|
|
protected function tearDown(): void
|
|
|
|
{
|
|
|
|
config()->set('logging.default', self::$defaultLogger);
|
|
|
|
self::$defaultLogger = null;
|
|
|
|
|
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that a server is not deleted if the force option is not set and an error
|
|
|
|
* is returned by wings.
|
|
|
|
*/
|
|
|
|
public function testRegularDeleteFailsIfWingsReturnsError()
|
|
|
|
{
|
|
|
|
$server = $this->createServerModel();
|
|
|
|
|
|
|
|
$this->expectException(DaemonConnectionException::class);
|
|
|
|
|
|
|
|
$this->daemonServerRepository->expects('setServer->delete')->withNoArgs()->andThrows(
|
2021-01-23 20:33:34 +00:00
|
|
|
new DaemonConnectionException(new BadResponseException('Bad request', new Request('GET', '/test'), new Response()))
|
2020-10-09 04:08:55 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->getService()->handle($server);
|
|
|
|
|
|
|
|
$this->assertDatabaseHas('servers', ['id' => $server->id]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that a 404 from Wings while deleting a server does not cause the deletion to fail.
|
|
|
|
*/
|
|
|
|
public function testRegularDeleteIgnores404FromWings()
|
|
|
|
{
|
|
|
|
$server = $this->createServerModel();
|
|
|
|
|
|
|
|
$this->daemonServerRepository->expects('setServer->delete')->withNoArgs()->andThrows(
|
|
|
|
new DaemonConnectionException(new BadResponseException('Bad request', new Request('GET', '/test'), new Response(404)))
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->getService()->handle($server);
|
|
|
|
|
|
|
|
$this->assertDatabaseMissing('servers', ['id' => $server->id]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that an error from Wings does not cause the deletion to fail if the server is being
|
|
|
|
* force deleted.
|
|
|
|
*/
|
|
|
|
public function testForceDeleteIgnoresExceptionFromWings()
|
|
|
|
{
|
|
|
|
$server = $this->createServerModel();
|
|
|
|
|
|
|
|
$this->daemonServerRepository->expects('setServer->delete')->withNoArgs()->andThrows(
|
|
|
|
new DaemonConnectionException(new BadResponseException('Bad request', new Request('GET', '/test'), new Response(500)))
|
|
|
|
);
|
|
|
|
|
2022-10-14 16:59:20 +00:00
|
|
|
$this->getService()->withForce()->handle($server);
|
2020-10-09 04:08:55 +00:00
|
|
|
|
|
|
|
$this->assertDatabaseMissing('servers', ['id' => $server->id]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that a non-force-delete call does not delete the server if one of the databases
|
|
|
|
* cannot be deleted from the host.
|
|
|
|
*/
|
|
|
|
public function testExceptionWhileDeletingStopsProcess()
|
|
|
|
{
|
|
|
|
$server = $this->createServerModel();
|
2021-01-23 20:09:16 +00:00
|
|
|
$host = DatabaseHost::factory()->create();
|
2020-10-09 04:08:55 +00:00
|
|
|
|
|
|
|
/** @var \Pterodactyl\Models\Database $db */
|
2021-01-23 20:09:16 +00:00
|
|
|
$db = Database::factory()->create(['database_host_id' => $host->id, 'server_id' => $server->id]);
|
2020-10-09 04:08:55 +00:00
|
|
|
|
|
|
|
$server->refresh();
|
|
|
|
|
|
|
|
$this->daemonServerRepository->expects('setServer->delete')->withNoArgs()->andReturnUndefined();
|
2022-11-29 17:53:59 +00:00
|
|
|
$this->databaseManagementService->expects('delete')->with(\Mockery::on(function ($value) use ($db) {
|
2020-10-09 04:08:55 +00:00
|
|
|
return $value instanceof Database && $value->id === $db->id;
|
2022-11-29 17:53:59 +00:00
|
|
|
}))->andThrows(new \Exception());
|
2020-10-09 04:08:55 +00:00
|
|
|
|
2022-11-29 17:53:59 +00:00
|
|
|
$this->expectException(\Exception::class);
|
2020-10-09 04:08:55 +00:00
|
|
|
$this->getService()->handle($server);
|
|
|
|
|
|
|
|
$this->assertDatabaseHas('servers', ['id' => $server->id]);
|
|
|
|
$this->assertDatabaseHas('databases', ['id' => $db->id]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that a server is deleted even if the server databases cannot be deleted from the host.
|
|
|
|
*/
|
|
|
|
public function testExceptionWhileDeletingDatabasesDoesNotAbortIfForceDeleted()
|
|
|
|
{
|
|
|
|
$server = $this->createServerModel();
|
2021-01-23 20:09:16 +00:00
|
|
|
$host = DatabaseHost::factory()->create();
|
2020-10-09 04:08:55 +00:00
|
|
|
|
|
|
|
/** @var \Pterodactyl\Models\Database $db */
|
2021-01-23 20:09:16 +00:00
|
|
|
$db = Database::factory()->create(['database_host_id' => $host->id, 'server_id' => $server->id]);
|
2020-10-09 04:08:55 +00:00
|
|
|
|
|
|
|
$server->refresh();
|
|
|
|
|
|
|
|
$this->daemonServerRepository->expects('setServer->delete')->withNoArgs()->andReturnUndefined();
|
2022-11-29 17:53:59 +00:00
|
|
|
$this->databaseManagementService->expects('delete')->with(\Mockery::on(function ($value) use ($db) {
|
2020-10-09 04:08:55 +00:00
|
|
|
return $value instanceof Database && $value->id === $db->id;
|
2022-11-29 17:53:59 +00:00
|
|
|
}))->andThrows(new \Exception());
|
2020-10-09 04:08:55 +00:00
|
|
|
|
|
|
|
$this->getService()->withForce(true)->handle($server);
|
|
|
|
|
|
|
|
$this->assertDatabaseMissing('servers', ['id' => $server->id]);
|
|
|
|
$this->assertDatabaseMissing('databases', ['id' => $db->id]);
|
|
|
|
}
|
|
|
|
|
2022-10-14 16:59:20 +00:00
|
|
|
private function getService(): ServerDeletionService
|
2020-10-09 04:08:55 +00:00
|
|
|
{
|
|
|
|
return $this->app->make(ServerDeletionService::class);
|
|
|
|
}
|
|
|
|
}
|