Add basic test coverage for server creation functionality
This commit is contained in:
parent
b2970e3117
commit
192a578a03
3 changed files with 225 additions and 14 deletions
|
@ -33,8 +33,6 @@ class ServerConfigurationStructureService
|
||||||
* @param \Pterodactyl\Models\Server $server
|
* @param \Pterodactyl\Models\Server $server
|
||||||
* @param bool $legacy
|
* @param bool $legacy
|
||||||
* @return array
|
* @return array
|
||||||
*
|
|
||||||
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
|
||||||
*/
|
*/
|
||||||
public function handle(Server $server, bool $legacy = false): array
|
public function handle(Server $server, bool $legacy = false): array
|
||||||
{
|
{
|
||||||
|
|
|
@ -123,15 +123,12 @@ class ServerCreationService
|
||||||
* @throws \Throwable
|
* @throws \Throwable
|
||||||
* @throws \Pterodactyl\Exceptions\DisplayException
|
* @throws \Pterodactyl\Exceptions\DisplayException
|
||||||
* @throws \Illuminate\Validation\ValidationException
|
* @throws \Illuminate\Validation\ValidationException
|
||||||
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
|
||||||
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
||||||
* @throws \Pterodactyl\Exceptions\Service\Deployment\NoViableNodeException
|
* @throws \Pterodactyl\Exceptions\Service\Deployment\NoViableNodeException
|
||||||
* @throws \Pterodactyl\Exceptions\Service\Deployment\NoViableAllocationException
|
* @throws \Pterodactyl\Exceptions\Service\Deployment\NoViableAllocationException
|
||||||
*/
|
*/
|
||||||
public function handle(array $data, DeploymentObject $deployment = null): Server
|
public function handle(array $data, DeploymentObject $deployment = null): Server
|
||||||
{
|
{
|
||||||
$this->connection->beginTransaction();
|
|
||||||
|
|
||||||
// If a deployment object has been passed we need to get the allocation
|
// If a deployment object has been passed we need to get the allocation
|
||||||
// that the server should use, and assign the node from that allocation.
|
// that the server should use, and assign the node from that allocation.
|
||||||
if ($deployment instanceof DeploymentObject) {
|
if ($deployment instanceof DeploymentObject) {
|
||||||
|
@ -158,23 +155,26 @@ class ServerCreationService
|
||||||
->setUserLevel(User::USER_LEVEL_ADMIN)
|
->setUserLevel(User::USER_LEVEL_ADMIN)
|
||||||
->handle(Arr::get($data, 'egg_id'), Arr::get($data, 'environment', []));
|
->handle(Arr::get($data, 'egg_id'), Arr::get($data, 'environment', []));
|
||||||
|
|
||||||
// Create the server and assign any additional allocations to it.
|
|
||||||
$server = $this->createModel($data);
|
|
||||||
|
|
||||||
$this->storeAssignedAllocations($server, $data);
|
|
||||||
$this->storeEggVariables($server, $eggVariableData);
|
|
||||||
|
|
||||||
// Due to the design of the Daemon, we need to persist this server to the disk
|
// Due to the design of the Daemon, we need to persist this server to the disk
|
||||||
// before we can actually create it on the Daemon.
|
// before we can actually create it on the Daemon.
|
||||||
//
|
//
|
||||||
// If that connection fails out we will attempt to perform a cleanup by just
|
// If that connection fails out we will attempt to perform a cleanup by just
|
||||||
// deleting the server itself from the system.
|
// deleting the server itself from the system.
|
||||||
$this->connection->commit();
|
/** @var \Pterodactyl\Models\Server $server */
|
||||||
|
$server = $this->connection->transaction(function () use ($data, $eggVariableData) {
|
||||||
|
// Create the server and assign any additional allocations to it.
|
||||||
|
$server = $this->createModel($data);
|
||||||
|
|
||||||
$structure = $this->configurationStructureService->handle($server);
|
$this->storeAssignedAllocations($server, $data);
|
||||||
|
$this->storeEggVariables($server, $eggVariableData);
|
||||||
|
|
||||||
|
return $server;
|
||||||
|
});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$this->daemonServerRepository->setServer($server)->create($structure);
|
$this->daemonServerRepository->setServer($server)->create(
|
||||||
|
$this->configurationStructureService->handle($server)
|
||||||
|
);
|
||||||
} catch (DaemonConnectionException $exception) {
|
} catch (DaemonConnectionException $exception) {
|
||||||
$this->serverDeletionService->withForce(true)->handle($server);
|
$this->serverDeletionService->withForce(true)->handle($server);
|
||||||
|
|
||||||
|
|
213
tests/Integration/Services/Servers/ServerCreationServiceTest.php
Normal file
213
tests/Integration/Services/Servers/ServerCreationServiceTest.php
Normal file
|
@ -0,0 +1,213 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Pterodactyl\Tests\Integration\Services\Servers;
|
||||||
|
|
||||||
|
use Mockery;
|
||||||
|
use Pterodactyl\Models\Egg;
|
||||||
|
use Pterodactyl\Models\Node;
|
||||||
|
use Pterodactyl\Models\User;
|
||||||
|
use GuzzleHttp\Psr7\Request;
|
||||||
|
use GuzzleHttp\Psr7\Response;
|
||||||
|
use Pterodactyl\Models\Server;
|
||||||
|
use Pterodactyl\Models\Location;
|
||||||
|
use Pterodactyl\Models\Allocation;
|
||||||
|
use Illuminate\Foundation\Testing\WithFaker;
|
||||||
|
use Illuminate\Validation\ValidationException;
|
||||||
|
use GuzzleHttp\Exception\BadResponseException;
|
||||||
|
use Pterodactyl\Models\Objects\DeploymentObject;
|
||||||
|
use Pterodactyl\Tests\Integration\IntegrationTestCase;
|
||||||
|
use Pterodactyl\Services\Servers\ServerCreationService;
|
||||||
|
use Pterodactyl\Repositories\Wings\DaemonServerRepository;
|
||||||
|
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
|
||||||
|
|
||||||
|
class ServerCreationServiceTest extends IntegrationTestCase
|
||||||
|
{
|
||||||
|
use WithFaker;
|
||||||
|
|
||||||
|
/** @var \Mockery\MockInterface */
|
||||||
|
private $daemonServerRepository;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stub the calls to Wings so that we don't actually hit those API endpoints.
|
||||||
|
*/
|
||||||
|
public function setUp(): void
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
$this->daemonServerRepository = Mockery::mock(DaemonServerRepository::class);
|
||||||
|
$this->swap(DaemonServerRepository::class, $this->daemonServerRepository);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test that a server can be created when a deployment object is provided to the service.
|
||||||
|
*
|
||||||
|
* This doesn't really do anything super complicated, we'll rely on other more specific
|
||||||
|
* tests to cover that the logic being used does indeed find suitable nodes and ports. For
|
||||||
|
* this test we just care that it is recognized and passed off to those functions.
|
||||||
|
*/
|
||||||
|
public function testServerIsCreatedWithDeploymentObject()
|
||||||
|
{
|
||||||
|
/** @var \Pterodactyl\Models\User $user */
|
||||||
|
$user = factory(User::class)->create();
|
||||||
|
|
||||||
|
/** @var \Pterodactyl\Models\Node $node */
|
||||||
|
$node = factory(Node::class)->create([
|
||||||
|
'location_id' => factory(Location::class)->create()->id,
|
||||||
|
]);
|
||||||
|
|
||||||
|
/** @var \Pterodactyl\Models\Allocation[]|\Illuminate\Database\Eloquent\Collection $allocations */
|
||||||
|
$allocations = factory(Allocation::class)->times(5)->create([
|
||||||
|
'node_id' => $node->id,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$deployment = (new DeploymentObject())->setDedicated(true)->setLocations([$node->location_id])->setPorts([
|
||||||
|
$allocations[0]->port,
|
||||||
|
]);
|
||||||
|
|
||||||
|
/** @noinspection PhpParamsInspection */
|
||||||
|
$egg = $this->cloneEggAndVariables(Egg::query()->findOrFail(1));
|
||||||
|
// We want to make sure that the validator service runs as an admin, and not as a regular
|
||||||
|
// user when saving variables.
|
||||||
|
$egg->variables()->first()->update([
|
||||||
|
'user_editable' => false,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$data = [
|
||||||
|
'name' => $this->faker->name,
|
||||||
|
'description' => $this->faker->sentence,
|
||||||
|
'owner_id' => $user->id,
|
||||||
|
'memory' => 256,
|
||||||
|
'swap' => 128,
|
||||||
|
'disk' => 100,
|
||||||
|
'io' => 500,
|
||||||
|
'cpu' => 0,
|
||||||
|
'startup' => 'java server2.jar',
|
||||||
|
'image' => 'java:8',
|
||||||
|
'egg_id' => $egg->id,
|
||||||
|
'allocation_additional' => [
|
||||||
|
$allocations[4]->id,
|
||||||
|
],
|
||||||
|
'environment' => [
|
||||||
|
'BUNGEE_VERSION' => '123',
|
||||||
|
'SERVER_JARFILE' => 'server2.jar',
|
||||||
|
],
|
||||||
|
];
|
||||||
|
|
||||||
|
$this->daemonServerRepository->expects('setServer')->andReturnSelf();
|
||||||
|
$this->daemonServerRepository->expects('create')->with(Mockery::on(function ($value) {
|
||||||
|
$this->assertIsArray($value);
|
||||||
|
// Just check for some keys to make sure we're getting the expected configuration
|
||||||
|
// structure back. Other tests exist to confirm it is the correct structure.
|
||||||
|
$this->assertArrayHasKey('uuid', $value);
|
||||||
|
$this->assertArrayHasKey('environment', $value);
|
||||||
|
$this->assertArrayHasKey('invocation', $value);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}))->andReturnUndefined();
|
||||||
|
|
||||||
|
try {
|
||||||
|
$this->getService()->handle(array_merge($data, [
|
||||||
|
'environment' => [
|
||||||
|
'BUNGEE_VERSION' => '',
|
||||||
|
'SERVER_JARFILE' => 'server2.jar',
|
||||||
|
],
|
||||||
|
]), $deployment);
|
||||||
|
$this->assertTrue(false, 'This statement should not be reached.');
|
||||||
|
} catch (ValidationException $exception) {
|
||||||
|
$this->assertCount(1, $exception->errors());
|
||||||
|
$this->assertArrayHasKey('environment.BUNGEE_VERSION', $exception->errors());
|
||||||
|
$this->assertSame('The Bungeecord Version variable field is required.', $exception->errors()['environment.BUNGEE_VERSION'][0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
$response = $this->getService()->handle($data, $deployment);
|
||||||
|
|
||||||
|
$this->assertInstanceOf(Server::class, $response);
|
||||||
|
$this->assertNotNull($response->uuid);
|
||||||
|
$this->assertSame($response->uuidShort, substr($response->uuid, 0, 8));
|
||||||
|
$this->assertSame($egg->id, $response->egg_id);
|
||||||
|
$this->assertCount(2, $response->variables);
|
||||||
|
$this->assertSame('123', $response->variables[0]->server_value);
|
||||||
|
$this->assertSame('server2.jar', $response->variables[1]->server_value);
|
||||||
|
|
||||||
|
foreach ($data as $key => $value) {
|
||||||
|
if (in_array($key, ['allocation_additional', 'environment'])) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->assertSame($value, $response->{$key});
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->assertCount(2, $response->allocations);
|
||||||
|
$this->assertSame($response->allocation_id, $response->allocations[0]->id);
|
||||||
|
$this->assertSame($allocations[0]->id, $response->allocations[0]->id);
|
||||||
|
$this->assertSame($allocations[4]->id, $response->allocations[1]->id);
|
||||||
|
|
||||||
|
$this->assertFalse($response->suspended);
|
||||||
|
$this->assertTrue($response->oom_disabled);
|
||||||
|
$this->assertEmpty($response->database_limit);
|
||||||
|
$this->assertEmpty($response->allocation_limit);
|
||||||
|
$this->assertEmpty($response->backup_limit);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test that a server is deleted from the Panel if Wings returns an error during the creation
|
||||||
|
* process.
|
||||||
|
*/
|
||||||
|
public function testErrorEncounteredByWingsCausesServerToBeDeleted()
|
||||||
|
{
|
||||||
|
/** @var \Pterodactyl\Models\User $user */
|
||||||
|
$user = factory(User::class)->create();
|
||||||
|
|
||||||
|
/** @var \Pterodactyl\Models\Node $node */
|
||||||
|
$node = factory(Node::class)->create([
|
||||||
|
'location_id' => factory(Location::class)->create()->id,
|
||||||
|
]);
|
||||||
|
|
||||||
|
/** @var \Pterodactyl\Models\Allocation $allocation */
|
||||||
|
$allocation = factory(Allocation::class)->create([
|
||||||
|
'node_id' => $node->id,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$data = [
|
||||||
|
'name' => $this->faker->name,
|
||||||
|
'description' => $this->faker->sentence,
|
||||||
|
'owner_id' => $user->id,
|
||||||
|
'allocation_id' => $allocation->id,
|
||||||
|
'node_id' => $allocation->node_id,
|
||||||
|
'memory' => 256,
|
||||||
|
'swap' => 128,
|
||||||
|
'disk' => 100,
|
||||||
|
'io' => 500,
|
||||||
|
'cpu' => 0,
|
||||||
|
'startup' => 'java server2.jar',
|
||||||
|
'image' => 'java:8',
|
||||||
|
'egg_id' => 1,
|
||||||
|
'environment' => [
|
||||||
|
'BUNGEE_VERSION' => '123',
|
||||||
|
'SERVER_JARFILE' => 'server2.jar',
|
||||||
|
],
|
||||||
|
];
|
||||||
|
|
||||||
|
$this->daemonServerRepository->expects('setServer->create')->andThrows(
|
||||||
|
new DaemonConnectionException(
|
||||||
|
new BadResponseException('Bad request', new Request('POST', '/create'), new Response(500))
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->daemonServerRepository->expects('setServer->delete')->andReturnUndefined();
|
||||||
|
|
||||||
|
$this->expectException(DaemonConnectionException::class);
|
||||||
|
|
||||||
|
$this->getService()->handle($data);
|
||||||
|
|
||||||
|
$this->assertDatabaseMissing('servers', ['owner_id' => $user->id]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return \Pterodactyl\Services\Servers\ServerCreationService
|
||||||
|
*/
|
||||||
|
private function getService()
|
||||||
|
{
|
||||||
|
return $this->app->make(ServerCreationService::class);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue