forked from Alex/Pterodactyl-Panel
Fix up database creation and handling code for servers; ref #2447
This commit is contained in:
parent
a4d7170fac
commit
8697185900
@ -42,18 +42,6 @@ interface DatabaseRepositoryInterface extends RepositoryInterface
|
||||
*/
|
||||
public function getDatabasesForHost(int $host, int $count = 25): LengthAwarePaginator;
|
||||
|
||||
/**
|
||||
* Create a new database if it does not already exist on the host with
|
||||
* the provided details.
|
||||
*
|
||||
* @param array $data
|
||||
* @return \Pterodactyl\Models\Database
|
||||
*
|
||||
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
||||
* @throws \Pterodactyl\Exceptions\Repository\DuplicateDatabaseNameException
|
||||
*/
|
||||
public function createIfNotExists(array $data): Database;
|
||||
|
||||
/**
|
||||
* Create a new database on a given connection.
|
||||
*
|
||||
|
@ -362,7 +362,7 @@ class ServersController extends Controller
|
||||
public function newDatabase(StoreServerDatabaseRequest $request, Server $server)
|
||||
{
|
||||
$this->databaseManagementService->create($server, [
|
||||
'database' => $request->input('database'),
|
||||
'database' => DatabaseManagementService::generateUniqueDatabaseName($request->input('database'), $server->id),
|
||||
'remote' => $request->input('remote'),
|
||||
'database_host_id' => $request->input('database_host_id'),
|
||||
'max_connections' => $request->input('max_connections'),
|
||||
|
@ -110,7 +110,9 @@ class DatabaseController extends ApplicationApiController
|
||||
*/
|
||||
public function store(StoreServerDatabaseRequest $request, Server $server): JsonResponse
|
||||
{
|
||||
$database = $this->databaseManagementService->create($server, $request->validated());
|
||||
$database = $this->databaseManagementService->create($server, array_merge($request->validated(), [
|
||||
'database' => $request->databaseName(),
|
||||
]));
|
||||
|
||||
return $this->fractal->item($database)
|
||||
->transformWith($this->getTransformer(ServerDatabaseTransformer::class))
|
||||
|
@ -2,9 +2,12 @@
|
||||
|
||||
namespace Pterodactyl\Http\Requests\Api\Application\Servers\Databases;
|
||||
|
||||
use Webmozart\Assert\Assert;
|
||||
use Pterodactyl\Models\Server;
|
||||
use Illuminate\Validation\Rule;
|
||||
use Illuminate\Database\Query\Builder;
|
||||
use Pterodactyl\Services\Acl\Api\AdminAcl;
|
||||
use Pterodactyl\Services\Databases\DatabaseManagementService;
|
||||
use Pterodactyl\Http\Requests\Api\Application\ApplicationApiRequest;
|
||||
|
||||
class StoreServerDatabaseRequest extends ApplicationApiRequest
|
||||
@ -26,14 +29,16 @@ class StoreServerDatabaseRequest extends ApplicationApiRequest
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
$server = $this->route()->parameter('server');
|
||||
|
||||
return [
|
||||
'database' => [
|
||||
'required',
|
||||
'string',
|
||||
'alpha_dash',
|
||||
'min:1',
|
||||
'max:24',
|
||||
Rule::unique('databases')->where(function (Builder $query) {
|
||||
$query->where('database_host_id', $this->input('host') ?? 0);
|
||||
'max:48',
|
||||
Rule::unique('databases')->where(function (Builder $query) use ($server) {
|
||||
$query->where('server_id', $server->id)->where('database', $this->databaseName());
|
||||
}),
|
||||
],
|
||||
'remote' => 'required|string|regex:/^[0-9%.]{1,15}$/',
|
||||
@ -68,4 +73,18 @@ class StoreServerDatabaseRequest extends ApplicationApiRequest
|
||||
'database' => 'Database Name',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the database name in the expected format.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function databaseName(): string
|
||||
{
|
||||
$server = $this->route()->parameter('server');
|
||||
|
||||
Assert::isInstanceOf($server, Server::class);
|
||||
|
||||
return DatabaseManagementService::generateUniqueDatabaseName($this->input('database'), $server->id);
|
||||
}
|
||||
}
|
||||
|
@ -9,6 +9,7 @@ use Pterodactyl\Models\Permission;
|
||||
use Illuminate\Database\Query\Builder;
|
||||
use Pterodactyl\Contracts\Http\ClientPermissionsRequest;
|
||||
use Pterodactyl\Http\Requests\Api\Client\ClientApiRequest;
|
||||
use Pterodactyl\Services\Databases\DatabaseManagementService;
|
||||
|
||||
class StoreDatabaseRequest extends ClientApiRequest implements ClientPermissionsRequest
|
||||
{
|
||||
@ -33,19 +34,23 @@ class StoreDatabaseRequest extends ClientApiRequest implements ClientPermissions
|
||||
'database' => [
|
||||
'required',
|
||||
'alpha_dash',
|
||||
'min:3',
|
||||
'min:1',
|
||||
'max:48',
|
||||
// Yes, I am aware that you could have the same database name across two unique hosts. However,
|
||||
// I don't really care about that for this validation. We just want to make sure it is unique to
|
||||
// the server itself. No need for complexity.
|
||||
Rule::unique('databases', 'database')->where(function (Builder $query) use ($server) {
|
||||
$query->where('server_id', $server->id);
|
||||
Rule::unique('databases')->where(function (Builder $query) use ($server) {
|
||||
$query->where('server_id', $server->id)
|
||||
->where('database', DatabaseManagementService::generateUniqueDatabaseName($this->input('database'), $server->id));
|
||||
}),
|
||||
],
|
||||
'remote' => 'required|string|regex:/^[0-9%.]{1,15}$/',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function messages()
|
||||
{
|
||||
return [
|
||||
|
@ -93,31 +93,6 @@ class DatabaseRepository extends EloquentRepository implements DatabaseRepositor
|
||||
->paginate($count, $this->getColumns());
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new database if it does not already exist on the host with
|
||||
* the provided details.
|
||||
*
|
||||
* @param array $data
|
||||
* @return \Pterodactyl\Models\Database
|
||||
*
|
||||
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
||||
* @throws \Pterodactyl\Exceptions\Repository\DuplicateDatabaseNameException
|
||||
*/
|
||||
public function createIfNotExists(array $data): Database
|
||||
{
|
||||
$count = $this->getBuilder()->where([
|
||||
['server_id', '=', array_get($data, 'server_id')],
|
||||
['database_host_id', '=', array_get($data, 'database_host_id')],
|
||||
['database', '=', array_get($data, 'database')],
|
||||
])->count();
|
||||
|
||||
if ($count > 0) {
|
||||
throw new DuplicateDatabaseNameException('A database with those details already exists for the specified server.');
|
||||
}
|
||||
|
||||
return $this->create($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new database on a given connection.
|
||||
*
|
||||
|
@ -3,18 +3,29 @@
|
||||
namespace Pterodactyl\Services\Databases;
|
||||
|
||||
use Exception;
|
||||
use InvalidArgumentException;
|
||||
use Pterodactyl\Models\Server;
|
||||
use Pterodactyl\Models\Database;
|
||||
use Pterodactyl\Helpers\Utilities;
|
||||
use Illuminate\Database\ConnectionInterface;
|
||||
use Symfony\Component\VarDumper\Cloner\Data;
|
||||
use Illuminate\Contracts\Encryption\Encrypter;
|
||||
use Pterodactyl\Extensions\DynamicDatabaseConnection;
|
||||
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
|
||||
use Pterodactyl\Repositories\Eloquent\DatabaseRepository;
|
||||
use Pterodactyl\Exceptions\Repository\DuplicateDatabaseNameException;
|
||||
use Pterodactyl\Exceptions\Service\Database\TooManyDatabasesException;
|
||||
use Pterodactyl\Exceptions\Service\Database\DatabaseClientFeatureNotEnabledException;
|
||||
|
||||
class DatabaseManagementService
|
||||
{
|
||||
/**
|
||||
* The regex used to validate that the database name passed through to the function is
|
||||
* in the expected format.
|
||||
*
|
||||
* @see \Pterodactyl\Services\Databases\DatabaseManagementService::generateUniqueDatabaseName()
|
||||
*/
|
||||
private const MATCH_NAME_REGEX = '/^(s[\d]+_)(.*)$/';
|
||||
|
||||
/**
|
||||
* @var \Illuminate\Database\ConnectionInterface
|
||||
*/
|
||||
@ -31,7 +42,7 @@ class DatabaseManagementService
|
||||
private $encrypter;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface
|
||||
* @var \Pterodactyl\Repositories\Eloquent\DatabaseRepository
|
||||
*/
|
||||
private $repository;
|
||||
|
||||
@ -50,13 +61,13 @@ class DatabaseManagementService
|
||||
*
|
||||
* @param \Illuminate\Database\ConnectionInterface $connection
|
||||
* @param \Pterodactyl\Extensions\DynamicDatabaseConnection $dynamic
|
||||
* @param \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface $repository
|
||||
* @param \Pterodactyl\Repositories\Eloquent\DatabaseRepository $repository
|
||||
* @param \Illuminate\Contracts\Encryption\Encrypter $encrypter
|
||||
*/
|
||||
public function __construct(
|
||||
ConnectionInterface $connection,
|
||||
DynamicDatabaseConnection $dynamic,
|
||||
DatabaseRepositoryInterface $repository,
|
||||
DatabaseRepository $repository,
|
||||
Encrypter $encrypter
|
||||
) {
|
||||
$this->connection = $connection;
|
||||
@ -65,6 +76,21 @@ class DatabaseManagementService
|
||||
$this->repository = $repository;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a unique database name for the given server. This name should be passed through when
|
||||
* calling this handle function for this service, otherwise the database will be created with
|
||||
* whatever name is provided.
|
||||
*
|
||||
* @param string $name
|
||||
* @param int $serverId
|
||||
* @return string
|
||||
*/
|
||||
public static function generateUniqueDatabaseName(string $name, int $serverId): string
|
||||
{
|
||||
// Max of 48 characters, including the s123_ that we append to the front.
|
||||
return sprintf('s%d_%s', $serverId, substr($name, 0, 48 - strlen("s{$serverId}_")));
|
||||
}
|
||||
|
||||
/**
|
||||
* Set wether or not this class should validate that the server has enough slots
|
||||
* left before creating the new database.
|
||||
@ -104,12 +130,15 @@ class DatabaseManagementService
|
||||
}
|
||||
}
|
||||
|
||||
// Max of 48 characters, including the s123_ that we append to the front.
|
||||
$truncatedName = substr($data['database'], 0, 48 - strlen("s{$server->id}_"));
|
||||
// Protect against developer mistakes...
|
||||
if (empty($data['database']) || ! preg_match(self::MATCH_NAME_REGEX, $data['database'])) {
|
||||
throw new InvalidArgumentException(
|
||||
'The database name passed to DatabaseManagementService::handle MUST be prefixed with "s{server_id}_".'
|
||||
);
|
||||
}
|
||||
|
||||
$data = array_merge($data, [
|
||||
'server_id' => $server->id,
|
||||
'database' => $truncatedName,
|
||||
'username' => sprintf('u%d_%s', $server->id, str_random(10)),
|
||||
'password' => $this->encrypter->encrypt(
|
||||
Utilities::randomStringWithSpecialCharacters(24)
|
||||
@ -120,7 +149,8 @@ class DatabaseManagementService
|
||||
|
||||
try {
|
||||
return $this->connection->transaction(function () use ($data, &$database) {
|
||||
$database = $this->repository->createIfNotExists($data);
|
||||
$database = $this->createModel($data);
|
||||
|
||||
$this->dynamic->set('dynamic', $data['database_host_id']);
|
||||
|
||||
$this->repository->createDatabase($database->database);
|
||||
@ -139,7 +169,7 @@ class DatabaseManagementService
|
||||
$this->repository->dropUser($database->username, $database->remote);
|
||||
$this->repository->flush();
|
||||
}
|
||||
} catch (Exception $exception) {
|
||||
} catch (Exception $deletionException) {
|
||||
// Do nothing here. We've already encountered an issue before this point so no
|
||||
// reason to prioritize this error over the initial one.
|
||||
}
|
||||
@ -166,4 +196,33 @@ class DatabaseManagementService
|
||||
|
||||
return $database->delete();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the database if there is not an identical match in the DB. While you can technically
|
||||
* have the same name across multiple hosts, for the sake of keeping this logic easy to understand
|
||||
* and avoiding user confusion we will ignore the specific host and just look across all hosts.
|
||||
*
|
||||
* @param array $data
|
||||
* @return \Pterodactyl\Models\Database
|
||||
*
|
||||
* @throws \Pterodactyl\Exceptions\Repository\DuplicateDatabaseNameException
|
||||
* @throws \Throwable
|
||||
*/
|
||||
protected function createModel(array $data): Database
|
||||
{
|
||||
$exists = Database::query()->where('server_id', $data['server_id'])
|
||||
->where('database', $data['database'])
|
||||
->exists();
|
||||
|
||||
if ($exists) {
|
||||
throw new DuplicateDatabaseNameException(
|
||||
'A database with that name already exists for this server.'
|
||||
);
|
||||
}
|
||||
|
||||
$database = (new Database)->forceFill($data);
|
||||
$database->saveOrFail();
|
||||
|
||||
return $database;
|
||||
}
|
||||
}
|
||||
|
@ -2,44 +2,27 @@
|
||||
|
||||
namespace Pterodactyl\Services\Databases;
|
||||
|
||||
use Webmozart\Assert\Assert;
|
||||
use Pterodactyl\Models\Server;
|
||||
use Pterodactyl\Models\Database;
|
||||
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
|
||||
use Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface;
|
||||
use Pterodactyl\Models\DatabaseHost;
|
||||
use Pterodactyl\Exceptions\Service\Database\NoSuitableDatabaseHostException;
|
||||
|
||||
class DeployServerDatabaseService
|
||||
{
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface
|
||||
*/
|
||||
private $databaseHostRepository;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Services\Databases\DatabaseManagementService
|
||||
*/
|
||||
private $managementService;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface
|
||||
*/
|
||||
private $repository;
|
||||
|
||||
/**
|
||||
* ServerDatabaseCreationService constructor.
|
||||
*
|
||||
* @param \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface $repository
|
||||
* @param \Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface $databaseHostRepository
|
||||
* @param \Pterodactyl\Services\Databases\DatabaseManagementService $managementService
|
||||
*/
|
||||
public function __construct(
|
||||
DatabaseRepositoryInterface $repository,
|
||||
DatabaseHostRepositoryInterface $databaseHostRepository,
|
||||
DatabaseManagementService $managementService
|
||||
) {
|
||||
$this->databaseHostRepository = $databaseHostRepository;
|
||||
public function __construct(DatabaseManagementService $managementService)
|
||||
{
|
||||
$this->managementService = $managementService;
|
||||
$this->repository = $repository;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -53,28 +36,26 @@ class DeployServerDatabaseService
|
||||
*/
|
||||
public function handle(Server $server, array $data): Database
|
||||
{
|
||||
$allowRandom = config('pterodactyl.client_features.databases.allow_random');
|
||||
$hosts = $this->databaseHostRepository->setColumns(['id'])->findWhere([
|
||||
['node_id', '=', $server->node_id],
|
||||
]);
|
||||
|
||||
if ($hosts->isEmpty() && ! $allowRandom) {
|
||||
throw new NoSuitableDatabaseHostException;
|
||||
}
|
||||
Assert::notEmpty($data['database'] ?? null);
|
||||
Assert::notEmpty($data['remote'] ?? null);
|
||||
|
||||
$hosts = DatabaseHost::query()->get()->toBase();
|
||||
if ($hosts->isEmpty()) {
|
||||
$hosts = $this->databaseHostRepository->setColumns(['id'])->all();
|
||||
if ($hosts->isEmpty()) {
|
||||
throw new NoSuitableDatabaseHostException;
|
||||
} else {
|
||||
$nodeHosts = $hosts->where('node_id', $server->node_id)->toBase();
|
||||
|
||||
if ($nodeHosts->isEmpty() && ! config('pterodactyl.client_features.databases.allow_random')) {
|
||||
throw new NoSuitableDatabaseHostException;
|
||||
}
|
||||
}
|
||||
|
||||
$host = $hosts->random();
|
||||
|
||||
return $this->managementService->create($server, [
|
||||
'database_host_id' => $host->id,
|
||||
'database' => array_get($data, 'database'),
|
||||
'remote' => array_get($data, 'remote'),
|
||||
'database_host_id' => $nodeHosts->isEmpty()
|
||||
? $hosts->random()->id
|
||||
: $nodeHosts->random()->id,
|
||||
'database' => DatabaseManagementService::generateUniqueDatabaseName($data['database'], $server->id),
|
||||
'remote' => $data['remote'],
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,225 @@
|
||||
<?php
|
||||
|
||||
namespace Pterodactyl\Tests\Integration\Services\Databases;
|
||||
|
||||
use Mockery;
|
||||
use Exception;
|
||||
use BadMethodCallException;
|
||||
use InvalidArgumentException;
|
||||
use Pterodactyl\Models\Database;
|
||||
use Pterodactyl\Models\DatabaseHost;
|
||||
use Pterodactyl\Tests\Integration\IntegrationTestCase;
|
||||
use Pterodactyl\Repositories\Eloquent\DatabaseRepository;
|
||||
use Pterodactyl\Services\Databases\DatabaseManagementService;
|
||||
use Pterodactyl\Exceptions\Repository\DuplicateDatabaseNameException;
|
||||
use Pterodactyl\Exceptions\Service\Database\TooManyDatabasesException;
|
||||
use Pterodactyl\Exceptions\Service\Database\DatabaseClientFeatureNotEnabledException;
|
||||
|
||||
class DatabaseManagementServiceTest extends IntegrationTestCase
|
||||
{
|
||||
/** @var \Mockery\MockInterface */
|
||||
private $repository;
|
||||
|
||||
/**
|
||||
* Setup tests.
|
||||
*/
|
||||
public function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
config()->set('pterodactyl.client_features.databases.enabled', true);
|
||||
|
||||
$this->repository = Mockery::mock(DatabaseRepository::class);
|
||||
$this->swap(DatabaseRepository::class, $this->repository);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that the name generated by the unique name function is what we expect.
|
||||
*/
|
||||
public function testUniqueDatabaseNameIsGeneratedCorrectly()
|
||||
{
|
||||
$this->assertSame('s1_example', DatabaseManagementService::generateUniqueDatabaseName('example', 1));
|
||||
$this->assertSame('s123_something_else', DatabaseManagementService::generateUniqueDatabaseName('something_else', 123));
|
||||
$this->assertSame('s123_' . str_repeat('a', 43), DatabaseManagementService::generateUniqueDatabaseName(str_repeat('a', 100), 123));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that disabling the client database feature flag prevents the creation of databases.
|
||||
*/
|
||||
public function testExceptionIsThrownIfClientDatabasesAreNotEnabled()
|
||||
{
|
||||
config()->set('pterodactyl.client_features.databases.enabled', false);
|
||||
|
||||
$this->expectException(DatabaseClientFeatureNotEnabledException::class);
|
||||
|
||||
$server = $this->createServerModel();
|
||||
$this->getService()->create($server, []);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that a server at its database limit cannot have an additional one created if
|
||||
* the $validateDatabaseLimit flag is not set to false.
|
||||
*/
|
||||
public function testDatabaseCannotBeCreatedIfServerHasReachedLimit()
|
||||
{
|
||||
$server = $this->createServerModel(['database_limit' => 2]);
|
||||
$host = factory(DatabaseHost::class)->create(['node_id' => $server->node_id]);
|
||||
|
||||
factory(Database::class)->times(2)->create(['server_id' => $server->id, 'database_host_id' => $host->id]);
|
||||
|
||||
$this->expectException(TooManyDatabasesException::class);
|
||||
|
||||
$this->getService()->create($server, []);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that a missing or invalid database name format causes an exception to be thrown.
|
||||
*
|
||||
* @param array $data
|
||||
* @dataProvider invalidDataDataProvider
|
||||
*/
|
||||
public function testEmptyDatabaseNameOrInvalidNameTriggersAnException($data)
|
||||
{
|
||||
$server = $this->createServerModel();
|
||||
|
||||
$this->expectException(InvalidArgumentException::class);
|
||||
$this->expectExceptionMessage('The database name passed to DatabaseManagementService::handle MUST be prefixed with "s{server_id}_".');
|
||||
|
||||
$this->getService()->create($server, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that creating a server database with an identical name triggers an exception.
|
||||
*/
|
||||
public function testCreatingDatabaseWithIdenticalNameTriggersAnException()
|
||||
{
|
||||
$server = $this->createServerModel();
|
||||
$name = DatabaseManagementService::generateUniqueDatabaseName('soemthing', $server->id);
|
||||
|
||||
$host = factory(DatabaseHost::class)->create(['node_id' => $server->node_id]);
|
||||
$host2 = factory(DatabaseHost::class)->create(['node_id' => $server->node_id]);
|
||||
factory(Database::class)->create([
|
||||
'database' => $name,
|
||||
'database_host_id' => $host->id,
|
||||
'server_id' => $server->id,
|
||||
]);
|
||||
|
||||
$this->expectException(DuplicateDatabaseNameException::class);
|
||||
$this->expectExceptionMessage('A database with that name already exists for this server.');
|
||||
|
||||
// Try to create a database with the same name as a database on a different host. We expect
|
||||
// this to fail since we don't account for the specific host when checking uniqueness.
|
||||
$this->getService()->create($server, [
|
||||
'database' => $name,
|
||||
'database_host_id' => $host2->id,
|
||||
]);
|
||||
|
||||
$this->assertDatabaseMissing('databases', ['server_id' => $server->id]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that a server database can be created successfully.
|
||||
*/
|
||||
public function testServerDatabaseCanBeCreated()
|
||||
{
|
||||
$server = $this->createServerModel();
|
||||
$name = DatabaseManagementService::generateUniqueDatabaseName('soemthing', $server->id);
|
||||
|
||||
$host = factory(DatabaseHost::class)->create(['node_id' => $server->node_id]);
|
||||
|
||||
$this->repository->expects('createDatabase')->with($name);
|
||||
|
||||
$username = null;
|
||||
$secondUsername = null;
|
||||
$password = null;
|
||||
|
||||
// The value setting inside the closures if to avoid throwing an exception during the
|
||||
// assertions that would get caught by the functions catcher and thus lead to the exception
|
||||
// being swallowed incorrectly.
|
||||
$this->repository->expects('createUser')->with(
|
||||
Mockery::on(function ($value) use (&$username) {
|
||||
$username = $value;
|
||||
|
||||
return true;
|
||||
}),
|
||||
'%',
|
||||
Mockery::on(function ($value) use (&$password) {
|
||||
$password = $value;
|
||||
|
||||
return true;
|
||||
}),
|
||||
null
|
||||
);
|
||||
|
||||
$this->repository->expects('assignUserToDatabase')->with($name, Mockery::on(function ($value) use (&$secondUsername) {
|
||||
$secondUsername = $value;
|
||||
|
||||
return true;
|
||||
}), '%');
|
||||
|
||||
$this->repository->expects('flush')->withNoArgs();
|
||||
|
||||
$response = $this->getService()->create($server, [
|
||||
'remote' => '%',
|
||||
'database' => $name,
|
||||
'database_host_id' => $host->id,
|
||||
]);
|
||||
|
||||
$this->assertInstanceOf(Database::class, $response);
|
||||
$this->assertSame($response->server_id, $server->id);
|
||||
$this->assertRegExp('/^(u[\d]+_)(\w){10}$/', $username);
|
||||
$this->assertSame($username, $secondUsername);
|
||||
$this->assertSame(24, strlen($password));
|
||||
|
||||
$this->assertDatabaseHas('databases', ['server_id' => $server->id, 'id' => $response->id]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that an exception encountered while creating the database leads to cleanup code being called
|
||||
* and any exceptions encountered while cleaning up go unreported.
|
||||
*/
|
||||
public function testExceptionEncounteredWhileCreatingDatabaseAttemptsToCleanup()
|
||||
{
|
||||
$server = $this->createServerModel();
|
||||
$name = DatabaseManagementService::generateUniqueDatabaseName('soemthing', $server->id);
|
||||
|
||||
$host = factory(DatabaseHost::class)->create(['node_id' => $server->node_id]);
|
||||
|
||||
$this->repository->expects('createDatabase')->with($name)->andThrows(new BadMethodCallException);
|
||||
$this->repository->expects('dropDatabase')->with($name);
|
||||
$this->repository->expects('dropUser')->withAnyArgs()->andThrows(new InvalidArgumentException);
|
||||
|
||||
$this->expectException(BadMethodCallException::class);
|
||||
|
||||
$this->getService()->create($server, [
|
||||
'remote' => '%',
|
||||
'database' => $name,
|
||||
'database_host_id' => $host->id,
|
||||
]);
|
||||
|
||||
$this->assertDatabaseMissing('databases', ['server_id' => $server->id]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function invalidDataDataProvider(): array
|
||||
{
|
||||
return [
|
||||
[[]],
|
||||
[['database' => '']],
|
||||
[['database' => 'something']],
|
||||
[['database' => 's_something']],
|
||||
[['database' => 's12s_something']],
|
||||
[['database' => 's12something']],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Pterodactyl\Services\Databases\DatabaseManagementService
|
||||
*/
|
||||
private function getService()
|
||||
{
|
||||
return $this->app->make(DatabaseManagementService::class);
|
||||
}
|
||||
}
|
@ -0,0 +1,168 @@
|
||||
<?php
|
||||
|
||||
namespace Pterodactyl\Tests\Integration\Services\Databases;
|
||||
|
||||
use Mockery;
|
||||
use Pterodactyl\Models\Node;
|
||||
use InvalidArgumentException;
|
||||
use Pterodactyl\Models\Database;
|
||||
use Pterodactyl\Models\DatabaseHost;
|
||||
use Symfony\Component\VarDumper\Cloner\Data;
|
||||
use Pterodactyl\Tests\Integration\IntegrationTestCase;
|
||||
use Pterodactyl\Services\Databases\DatabaseManagementService;
|
||||
use Pterodactyl\Services\Databases\DeployServerDatabaseService;
|
||||
use Pterodactyl\Exceptions\Service\Database\NoSuitableDatabaseHostException;
|
||||
|
||||
class DeployServerDatabaseServiceTest extends IntegrationTestCase
|
||||
{
|
||||
/** @var \Mockery\MockInterface */
|
||||
private $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.
|
||||
*
|
||||
* @param array $data
|
||||
* @dataProvider invalidDataProvider
|
||||
*/
|
||||
public function testErrorIsThrownIfDatabaseNameIsEmpty($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 = factory(Node::class)->create(['location_id' => $server->location->id]);
|
||||
factory(DatabaseHost::class)->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 = factory(Node::class)->create(['location_id' => $server->location->id]);
|
||||
factory(DatabaseHost::class)->create(['node_id' => $node->id]);
|
||||
$host = factory(DatabaseHost::class)->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 = factory(Node::class)->create(['location_id' => $server->location->id]);
|
||||
$host = factory(DatabaseHost::class)->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);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function invalidDataProvider(): array
|
||||
{
|
||||
return [
|
||||
[['remote' => '%']],
|
||||
[['database' => null, 'remote' => '%']],
|
||||
[['database' => '', 'remote' => '%']],
|
||||
[['database' => '']],
|
||||
[['database' => '', 'remote' => '']],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Pterodactyl\Services\Databases\DeployServerDatabaseService
|
||||
*/
|
||||
private function getService()
|
||||
{
|
||||
return $this->app->make(DeployServerDatabaseService::class);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user