Start 2.0 development

Co-authored-by: Anthony <anthony@infraly.co>
Co-authored-by: Sky Mulley <daniel.mulley@outlook.com>
This commit is contained in:
robert dennis
2026-10-05 18:10:18 -04:00
co-authored by Anthony Sky Mulley
parent 98079a0166
commit 6c0413e39b
2764 changed files with 176950 additions and 72961 deletions
@@ -1,157 +1,92 @@
<?php
namespace Pterodactyl\Tests\Integration\Services\Databases;
declare(strict_types=1);
use Mockery\MockInterface;
use Pterodactyl\Models\Node;
namespace Pterodactyl\Tests\Pest\Integration\Services\Databases\DeployServerDatabaseServiceTest;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use InvalidArgumentException;
use Pterodactyl\Contracts\Databases\DeploysServerDatabases;
use Pterodactyl\Exceptions\Service\Database\NoSuitableDatabaseHostException;
use Pterodactyl\Models\Database;
use Pterodactyl\Models\DatabaseHost;
use Pterodactyl\Models\Node;
use Pterodactyl\Services\Databases\DatabaseHostGateway;
use Pterodactyl\Tests\Integration\IntegrationTestCase;
use Pterodactyl\Services\Databases\DatabaseManagementService;
use Pterodactyl\Services\Databases\DeployServerDatabaseService;
use Pterodactyl\Exceptions\Service\Database\NoSuitableDatabaseHostException;
use Pterodactyl\Tests\Support\Fakes\FakeDatabaseHostGateway;
class DeployServerDatabaseServiceTest extends IntegrationTestCase
use function pterodactylTestCase;
uses(IntegrationTestCase::class, DatabaseTransactions::class);
/**
* Setup tests.
*/
beforeEach(function () {
config()->set('pterodactyl.client_features.databases.enabled', true);
$this->app->instance(DatabaseHostGateway::class, $this->gateway = new FakeDatabaseHostGateway());
});
/**
* Ensure we reset the config to the expected value.
*/
afterEach(function () {
config()->set('pterodactyl.client_features.databases.allow_random', true);
Database::query()->delete();
DatabaseHost::query()->delete();
});
dataset('invalidDataProvider', function () {
return [[['remote' => '%']], [['database' => null, 'remote' => '%']], [['database' => '', 'remote' => '%']], [['database' => '']], [['database' => '', 'remote' => '']]];
});
test('error is thrown if database name is empty', function (array $data) {
$server = $this->createServerModel(['database_limit' => 5]);
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Expected a non-empty database name.');
getService()->deploy($server, $data);
})->with('invalidDataProvider');
test('error is thrown if no database hosts exist on node', function () {
$server = $this->createServerModel(['database_limit' => 5]);
$node = Node::factory()->create(['location_id' => $server->location->id]);
DatabaseHost::factory()->create(['node_id' => $node->id]);
config()->set('pterodactyl.client_features.databases.allow_random', false);
$this->expectException(NoSuitableDatabaseHostException::class);
getService()->deploy($server, ['database' => 'something', 'remote' => '%']);
});
test('error is thrown if no database hosts exist on system', function () {
$server = $this->createServerModel(['database_limit' => 5]);
$this->expectException(NoSuitableDatabaseHostException::class);
getService()->deploy($server, ['database' => 'something', 'remote' => '%']);
});
test('database host on same node is preferred', function () {
$server = $this->createServerModel(['database_limit' => 5]);
$node = Node::factory()->create(['location_id' => $server->location->id]);
DatabaseHost::factory()->create(['node_id' => $node->id]);
$host = DatabaseHost::factory()->create(['node_id' => $server->node_id]);
$response = getService()->deploy($server, ['database' => 'something', 'remote' => '%']);
expect($response)->toBeInstanceOf(Database::class);
expect($response->database_host_id)->toBe($host->id);
expect($response->database)->toBe("s{$server->id}_something");
$this->assertDatabaseHas('databases', ['id' => $response->id]);
/** @var FakeDatabaseHostGateway $gateway */
$gateway = $this->gateway;
$gateway->assertCreated("s{$server->id}_something");
$gateway->assertFlushed(1);
});
test('database host is selected if no suitable host exists on same node', function () {
$server = $this->createServerModel(['database_limit' => 5]);
$node = Node::factory()->create(['location_id' => $server->location->id]);
$host = DatabaseHost::factory()->create(['node_id' => $node->id]);
$response = getService()->deploy($server, ['database' => 'something', 'remote' => '%']);
expect($response)->toBeInstanceOf(Database::class);
expect($response->database_host_id)->toBe($host->id);
expect($response->database)->toBe("s{$server->id}_something");
$this->assertDatabaseHas('databases', ['id' => $response->id]);
/** @var FakeDatabaseHostGateway $gateway */
$gateway = $this->gateway;
$gateway->assertCreated("s{$server->id}_something");
$gateway->assertFlushed(1);
});
function getService(): DeploysServerDatabases
{
private MockInterface $managementService;
/**
* Setup tests.
*/
public function setUp(): void
{
parent::setUp();
$this->managementService = \Mockery::mock(DatabaseManagementService::class);
$this->swap(DatabaseManagementService::class, $this->managementService);
}
/**
* Ensure we reset the config to the expected value.
*/
protected function tearDown(): void
{
config()->set('pterodactyl.client_features.databases.allow_random', true);
Database::query()->delete();
DatabaseHost::query()->delete();
parent::tearDown();
}
/**
* Test that an error is thrown if either the database name or the remote host are empty.
*/
#[\PHPUnit\Framework\Attributes\DataProvider('invalidDataProvider')]
public function testErrorIsThrownIfDatabaseNameIsEmpty(array $data)
{
$server = $this->createServerModel();
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessageMatches('/^Expected a non-empty value\. Got: /');
$this->getService()->handle($server, $data);
}
/**
* Test that an error is thrown if there are no database hosts on the same node as the
* server and the allow_random config value is false.
*/
public function testErrorIsThrownIfNoDatabaseHostsExistOnNode()
{
$server = $this->createServerModel();
$node = Node::factory()->create(['location_id' => $server->location->id]);
DatabaseHost::factory()->create(['node_id' => $node->id]);
config()->set('pterodactyl.client_features.databases.allow_random', false);
$this->expectException(NoSuitableDatabaseHostException::class);
$this->getService()->handle($server, [
'database' => 'something',
'remote' => '%',
]);
}
/**
* Test that an error is thrown if no database hosts exist at all on the system.
*/
public function testErrorIsThrownIfNoDatabaseHostsExistOnSystem()
{
$server = $this->createServerModel();
$this->expectException(NoSuitableDatabaseHostException::class);
$this->getService()->handle($server, [
'database' => 'something',
'remote' => '%',
]);
}
/**
* Test that a database host on the same node as the server is preferred.
*/
public function testDatabaseHostOnSameNodeIsPreferred()
{
$server = $this->createServerModel();
$node = Node::factory()->create(['location_id' => $server->location->id]);
DatabaseHost::factory()->create(['node_id' => $node->id]);
$host = DatabaseHost::factory()->create(['node_id' => $server->node_id]);
$this->managementService->expects('create')->with($server, [
'database_host_id' => $host->id,
'database' => "s{$server->id}_something",
'remote' => '%',
])->andReturns(new Database());
$response = $this->getService()->handle($server, [
'database' => 'something',
'remote' => '%',
]);
$this->assertInstanceOf(Database::class, $response);
}
/**
* Test that a database host not assigned to the same node as the server is used if
* there are no same-node hosts and the allow_random configuration value is set to
* true.
*/
public function testDatabaseHostIsSelectedIfNoSuitableHostExistsOnSameNode()
{
$server = $this->createServerModel();
$node = Node::factory()->create(['location_id' => $server->location->id]);
$host = DatabaseHost::factory()->create(['node_id' => $node->id]);
$this->managementService->expects('create')->with($server, [
'database_host_id' => $host->id,
'database' => "s{$server->id}_something",
'remote' => '%',
])->andReturns(new Database());
$response = $this->getService()->handle($server, [
'database' => 'something',
'remote' => '%',
]);
$this->assertInstanceOf(Database::class, $response);
}
public static function invalidDataProvider(): array
{
return [
[['remote' => '%']],
[['database' => null, 'remote' => '%']],
[['database' => '', 'remote' => '%']],
[['database' => '']],
[['database' => '', 'remote' => '']],
];
}
private function getService(): DeployServerDatabaseService
{
return $this->app->make(DeployServerDatabaseService::class);
}
return (function () {
return $this->app->make(DeploysServerDatabases::class);
})->call(pterodactylTestCase());
}