2020-10-11 18:59:46 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Tests\Integration\Services\Databases;
|
|
|
|
|
|
|
|
use Mockery;
|
2022-10-14 16:59:20 +00:00
|
|
|
use Mockery\MockInterface;
|
2020-10-11 18:59:46 +00:00
|
|
|
use Pterodactyl\Models\Node;
|
|
|
|
use InvalidArgumentException;
|
|
|
|
use Pterodactyl\Models\Database;
|
|
|
|
use Pterodactyl\Models\DatabaseHost;
|
|
|
|
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
|
|
|
|
{
|
2022-10-14 16:59:20 +00:00
|
|
|
private MockInterface $managementService;
|
2020-10-11 18:59:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @dataProvider invalidDataProvider
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function testErrorIsThrownIfDatabaseNameIsEmpty(array $data)
|
2020-10-11 18:59:46 +00:00
|
|
|
{
|
|
|
|
$server = $this->createServerModel();
|
|
|
|
|
|
|
|
$this->expectException(InvalidArgumentException::class);
|
2022-05-04 23:01:29 +00:00
|
|
|
$this->expectExceptionMessageMatches('/^Expected a non-empty value\. Got: /');
|
2020-10-11 18:59:46 +00:00
|
|
|
$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();
|
|
|
|
|
2021-01-23 20:09:16 +00:00
|
|
|
$node = Node::factory()->create(['location_id' => $server->location->id]);
|
|
|
|
DatabaseHost::factory()->create(['node_id' => $node->id]);
|
2020-10-11 18:59:46 +00:00
|
|
|
|
|
|
|
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();
|
|
|
|
|
2021-01-23 20:09:16 +00:00
|
|
|
$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]);
|
2020-10-11 18:59:46 +00:00
|
|
|
|
|
|
|
$this->managementService->expects('create')->with($server, [
|
|
|
|
'database_host_id' => $host->id,
|
|
|
|
'database' => "s{$server->id}_something",
|
|
|
|
'remote' => '%',
|
2021-01-23 20:33:34 +00:00
|
|
|
])->andReturns(new Database());
|
2020-10-11 18:59:46 +00:00
|
|
|
|
|
|
|
$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();
|
|
|
|
|
2021-01-23 20:09:16 +00:00
|
|
|
$node = Node::factory()->create(['location_id' => $server->location->id]);
|
|
|
|
$host = DatabaseHost::factory()->create(['node_id' => $node->id]);
|
2020-10-11 18:59:46 +00:00
|
|
|
|
|
|
|
$this->managementService->expects('create')->with($server, [
|
|
|
|
'database_host_id' => $host->id,
|
|
|
|
'database' => "s{$server->id}_something",
|
|
|
|
'remote' => '%',
|
2021-01-23 20:33:34 +00:00
|
|
|
])->andReturns(new Database());
|
2020-10-11 18:59:46 +00:00
|
|
|
|
|
|
|
$response = $this->getService()->handle($server, [
|
|
|
|
'database' => 'something',
|
|
|
|
'remote' => '%',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertInstanceOf(Database::class, $response);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function invalidDataProvider(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
[['remote' => '%']],
|
|
|
|
[['database' => null, 'remote' => '%']],
|
|
|
|
[['database' => '', 'remote' => '%']],
|
|
|
|
[['database' => '']],
|
|
|
|
[['database' => '', 'remote' => '']],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2022-10-14 16:59:20 +00:00
|
|
|
private function getService(): DeployServerDatabaseService
|
2020-10-11 18:59:46 +00:00
|
|
|
{
|
|
|
|
return $this->app->make(DeployServerDatabaseService::class);
|
|
|
|
}
|
|
|
|
}
|