Files

139 lines
6.6 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\Databases\DeployServerDatabaseTest;
2026-10-05 18:10:18 -04:00
use Illuminate\Foundation\Testing\DatabaseTransactions;
2026-10-11 07:34:41 -04:00
use Illuminate\Validation\ValidationException;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Contracts\Databases\DeploysServerDatabases;
use Pterodactyl\Exceptions\Service\Database\NoSuitableDatabaseHostException;
use Pterodactyl\Models\Database;
use Pterodactyl\Models\DatabaseHost;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Models\Node;
use Pterodactyl\Services\Databases\DatabaseHostGateway;
use Pterodactyl\Tests\Integration\IntegrationTestCase;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Tests\Support\Fakes\FakeDatabaseHostGateway;
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' => '']]];
});
2026-10-11 07:34:41 -04:00
test('validation fails if database name is empty', function (array $data) {
2026-10-05 18:10:18 -04:00
$server = $this->createServerModel(['database_limit' => 5]);
2026-10-11 07:34:41 -04:00
try {
getService()->deploy($server, $data);
$this->fail('The database was deployed.');
} catch (ValidationException $validationException) {
expect($validationException->errors())->toHaveKey('database');
}
2026-10-05 18:10:18 -04:00
})->with('invalidDataProvider');
2026-10-11 07:34:41 -04:00
test('databases deployed without the http api meet the same rules', function (array $input, string $field) {
$server = $this->createServerModel(['database_limit' => 5]);
DatabaseHost::factory()->create(['node_id' => $server->node_id]);
try {
getService()->deploy($server, [...['database' => 'direct', 'remote' => '%'], ...$input]);
$this->fail('The database was deployed.');
} catch (ValidationException $validationException) {
expect(array_keys($validationException->errors()))->toBe([$field]);
}
expect($server->databases()->exists())->toBeFalse()
->and($this->gateway->calls)->toBe([]);
})->with([
'a name that is too short' => [['database' => 'ab'], 'database'],
'a name with a dot' => [['database' => 'my.database'], 'database'],
'a name that is too long' => [['database' => str_repeat('a', 49)], 'database'],
'a missing remote' => [['remote' => null], 'remote'],
'a remote with a space' => [['remote' => '10.0.0. %'], 'remote'],
'an unknown host' => [['database_host_id' => 0], 'database_host_id'],
'a negative connection limit' => [['max_connections' => -1], 'max_connections'],
]);
test('values are coerced the way laravel validates them, and a given host is used', function () {
$server = $this->createServerModel(['database_limit' => 5]);
DatabaseHost::factory()->create(['node_id' => $server->node_id]);
$host = DatabaseHost::factory()->create(['node_id' => $server->node_id]);
$response = getService()->deploy($server, ['database' => 'chosen', 'remote' => '%', 'database_host_id' => (string) $host->id, 'max_connections' => '15']);
$response->refresh();
expect($response->database_host_id)->toBe($host->id)
->and($response->database)->toBe("s{$server->id}_chosen")
->and($response->max_connections)->toBe(15);
});
test('a database deployed without a connection limit has none', function () {
$server = $this->createServerModel(['database_limit' => 5]);
DatabaseHost::factory()->create(['node_id' => $server->node_id]);
$response = getService()->deploy($server, ['database' => 'unlimited', 'remote' => '%']);
expect($response->refresh()->max_connections)->toBeNull();
});
2026-10-05 18:10:18 -04:00
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
{
2026-10-05 18:10:18 -04:00
return (function () {
return $this->app->make(DeploysServerDatabases::class);
})->call(pterodactylTestCase());
}