Implement server creation though the API.
Also implements auto-deployment to specific locations and ports.
This commit is contained in:
parent
97ee95b4da
commit
5ed164e13e
24 changed files with 927 additions and 223 deletions
|
@ -9,16 +9,15 @@ use Pterodactyl\Models\User;
|
|||
use Tests\Traits\MocksUuids;
|
||||
use Pterodactyl\Models\Server;
|
||||
use Tests\Traits\MocksRequestException;
|
||||
use GuzzleHttp\Exception\RequestException;
|
||||
use Illuminate\Database\ConnectionInterface;
|
||||
use Pterodactyl\Exceptions\PterodactylException;
|
||||
use Pterodactyl\Services\Servers\ServerCreationService;
|
||||
use Pterodactyl\Services\Servers\VariableValidatorService;
|
||||
use Pterodactyl\Contracts\Repository\NodeRepositoryInterface;
|
||||
use Pterodactyl\Services\Deployment\FindViableNodesService;
|
||||
use Pterodactyl\Contracts\Repository\EggRepositoryInterface;
|
||||
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
|
||||
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
|
||||
use Pterodactyl\Services\Deployment\AllocationSelectionService;
|
||||
use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface;
|
||||
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
|
||||
use Pterodactyl\Services\Servers\ServerConfigurationStructureService;
|
||||
use Pterodactyl\Contracts\Repository\ServerVariableRepositoryInterface;
|
||||
use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface;
|
||||
|
@ -35,6 +34,11 @@ class ServerCreationServiceTest extends TestCase
|
|||
*/
|
||||
private $allocationRepository;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Services\Deployment\AllocationSelectionService
|
||||
*/
|
||||
private $allocationSelectionService;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Services\Servers\ServerConfigurationStructureService|\Mockery\Mock
|
||||
*/
|
||||
|
@ -51,14 +55,14 @@ class ServerCreationServiceTest extends TestCase
|
|||
private $daemonServerRepository;
|
||||
|
||||
/**
|
||||
* @var \GuzzleHttp\Exception\RequestException|\Mockery\Mock
|
||||
* @var \Pterodactyl\Contracts\Repository\EggRepositoryInterface
|
||||
*/
|
||||
private $exception;
|
||||
private $eggRepository;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\NodeRepositoryInterface|\Mockery\Mock
|
||||
* @var \Pterodactyl\Services\Deployment\FindViableNodesService
|
||||
*/
|
||||
private $nodeRepository;
|
||||
private $findViableNodesService;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface|\Mockery\Mock
|
||||
|
@ -88,11 +92,12 @@ class ServerCreationServiceTest extends TestCase
|
|||
parent::setUp();
|
||||
|
||||
$this->allocationRepository = m::mock(AllocationRepositoryInterface::class);
|
||||
$this->allocationSelectionService = m::mock(AllocationSelectionService::class);
|
||||
$this->configurationStructureService = m::mock(ServerConfigurationStructureService::class);
|
||||
$this->connection = m::mock(ConnectionInterface::class);
|
||||
$this->daemonServerRepository = m::mock(DaemonServerRepositoryInterface::class);
|
||||
$this->exception = m::mock(RequestException::class);
|
||||
$this->nodeRepository = m::mock(NodeRepositoryInterface::class);
|
||||
$this->eggRepository = m::mock(EggRepositoryInterface::class);
|
||||
$this->findViableNodesService = m::mock(FindViableNodesService::class);
|
||||
$this->repository = m::mock(ServerRepositoryInterface::class);
|
||||
$this->serverVariableRepository = m::mock(ServerVariableRepositoryInterface::class);
|
||||
$this->userRepository = m::mock(UserRepositoryInterface::class);
|
||||
|
@ -119,7 +124,7 @@ class ServerCreationServiceTest extends TestCase
|
|||
|
||||
$this->allocationRepository->shouldReceive('assignAllocationsToServer')->with($model->id, [$model->allocation_id])->once()->andReturn(1);
|
||||
|
||||
$this->validatorService->shouldReceive('setUserLevel')->with(User::USER_LEVEL_ADMIN)->once()->andReturnNull();
|
||||
$this->validatorService->shouldReceive('setUserLevel')->with(User::USER_LEVEL_ADMIN)->once()->andReturnSelf();
|
||||
$this->validatorService->shouldReceive('handle')->with($model->egg_id, [])->once()->andReturn(
|
||||
collect([(object) ['id' => 123, 'value' => 'var1-value']])
|
||||
);
|
||||
|
@ -133,20 +138,19 @@ class ServerCreationServiceTest extends TestCase
|
|||
])->once()->andReturn(true);
|
||||
$this->configurationStructureService->shouldReceive('handle')->with($model)->once()->andReturn(['test' => 'struct']);
|
||||
|
||||
$node = factory(Node::class)->make();
|
||||
$this->nodeRepository->shouldReceive('find')->with($model->node_id)->once()->andReturn($node);
|
||||
|
||||
$this->daemonServerRepository->shouldReceive('setNode')->with($node)->once()->andReturnSelf();
|
||||
$this->daemonServerRepository->shouldReceive('setServer')->with($model)->once()->andReturnSelf();
|
||||
$this->daemonServerRepository->shouldReceive('create')->with(['test' => 'struct'], ['start_on_completion' => false])->once();
|
||||
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
|
||||
|
||||
$response = $this->getService()->create($model->toArray());
|
||||
$response = $this->getService()->handle($model->toArray());
|
||||
|
||||
$this->assertSame($model, $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test handling of node timeout or other daemon error.
|
||||
*
|
||||
* @expectedException \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
|
||||
*/
|
||||
public function testExceptionShouldBeThrownIfTheRequestFails()
|
||||
{
|
||||
|
@ -159,21 +163,14 @@ class ServerCreationServiceTest extends TestCase
|
|||
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
||||
$this->repository->shouldReceive('create')->once()->andReturn($model);
|
||||
$this->allocationRepository->shouldReceive('assignAllocationsToServer')->once()->andReturn(1);
|
||||
$this->validatorService->shouldReceive('setUserLevel')->once()->andReturnNull();
|
||||
$this->validatorService->shouldReceive('setUserLevel')->once()->andReturnSelf();
|
||||
$this->validatorService->shouldReceive('handle')->once()->andReturn(collect([]));
|
||||
$this->configurationStructureService->shouldReceive('handle')->once()->andReturn([]);
|
||||
|
||||
$node = factory(Node::class)->make();
|
||||
$this->nodeRepository->shouldReceive('find')->with($model->node_id)->once()->andReturn($node);
|
||||
$this->daemonServerRepository->shouldReceive('setNode')->with($node)->once()->andThrow($this->exception);
|
||||
$this->daemonServerRepository->shouldReceive('setServer')->with($model)->once()->andThrow($this->getExceptionMock());
|
||||
$this->connection->shouldReceive('rollBack')->withNoArgs()->once()->andReturnNull();
|
||||
|
||||
try {
|
||||
$this->getService()->create($model->toArray());
|
||||
} catch (PterodactylException $exception) {
|
||||
$this->assertInstanceOf(DaemonConnectionException::class, $exception);
|
||||
$this->assertInstanceOf(RequestException::class, $exception->getPrevious());
|
||||
}
|
||||
$this->getService()->handle($model->toArray());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -185,13 +182,14 @@ class ServerCreationServiceTest extends TestCase
|
|||
{
|
||||
return new ServerCreationService(
|
||||
$this->allocationRepository,
|
||||
$this->allocationSelectionService,
|
||||
$this->connection,
|
||||
$this->daemonServerRepository,
|
||||
$this->nodeRepository,
|
||||
$this->eggRepository,
|
||||
$this->findViableNodesService,
|
||||
$this->configurationStructureService,
|
||||
$this->repository,
|
||||
$this->serverVariableRepository,
|
||||
$this->userRepository,
|
||||
$this->validatorService
|
||||
);
|
||||
}
|
||||
|
|
|
@ -1,23 +1,14 @@
|
|||
<?php
|
||||
/**
|
||||
* Pterodactyl - Panel
|
||||
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
|
||||
*
|
||||
* This software is licensed under the terms of the MIT license.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
namespace Tests\Unit\Services\Servers;
|
||||
|
||||
use Exception;
|
||||
use Mockery as m;
|
||||
use Tests\TestCase;
|
||||
use Illuminate\Log\Writer;
|
||||
use GuzzleHttp\Psr7\Response;
|
||||
use Pterodactyl\Models\Server;
|
||||
use GuzzleHttp\Exception\RequestException;
|
||||
use Tests\Traits\MocksRequestException;
|
||||
use Illuminate\Database\ConnectionInterface;
|
||||
use Pterodactyl\Exceptions\DisplayException;
|
||||
use Pterodactyl\Services\Servers\ServerDeletionService;
|
||||
use Pterodactyl\Services\Databases\DatabaseManagementService;
|
||||
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
|
||||
|
@ -26,50 +17,37 @@ use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonS
|
|||
|
||||
class ServerDeletionServiceTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var \Illuminate\Database\ConnectionInterface
|
||||
*/
|
||||
protected $connection;
|
||||
use MocksRequestException;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface
|
||||
* @var \Illuminate\Database\ConnectionInterface|\Mockery\Mock
|
||||
*/
|
||||
protected $daemonServerRepository;
|
||||
private $connection;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Services\Databases\DatabaseManagementService
|
||||
* @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface|\Mockery\Mock
|
||||
*/
|
||||
protected $databaseManagementService;
|
||||
private $daemonServerRepository;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface
|
||||
* @var \Pterodactyl\Services\Databases\DatabaseManagementService|\Mockery\Mock
|
||||
*/
|
||||
protected $databaseRepository;
|
||||
private $databaseManagementService;
|
||||
|
||||
/**
|
||||
* @var \GuzzleHttp\Exception\RequestException
|
||||
* @var \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface|\Mockery\Mock
|
||||
*/
|
||||
protected $exception;
|
||||
private $databaseRepository;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Models\Server
|
||||
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface|\Mockery\Mock
|
||||
*/
|
||||
protected $model;
|
||||
private $repository;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface
|
||||
* @var \Illuminate\Log\Writer|\Mockery\Mock
|
||||
*/
|
||||
protected $repository;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Services\Servers\ServerDeletionService
|
||||
*/
|
||||
protected $service;
|
||||
|
||||
/**
|
||||
* @var \Illuminate\Log\Writer
|
||||
*/
|
||||
protected $writer;
|
||||
private $writer;
|
||||
|
||||
/**
|
||||
* Setup tests.
|
||||
|
@ -82,19 +60,8 @@ class ServerDeletionServiceTest extends TestCase
|
|||
$this->daemonServerRepository = m::mock(DaemonServerRepositoryInterface::class);
|
||||
$this->databaseRepository = m::mock(DatabaseRepositoryInterface::class);
|
||||
$this->databaseManagementService = m::mock(DatabaseManagementService::class);
|
||||
$this->exception = m::mock(RequestException::class);
|
||||
$this->model = factory(Server::class)->make();
|
||||
$this->repository = m::mock(ServerRepositoryInterface::class);
|
||||
$this->writer = m::mock(Writer::class);
|
||||
|
||||
$this->service = new ServerDeletionService(
|
||||
$this->connection,
|
||||
$this->daemonServerRepository,
|
||||
$this->databaseRepository,
|
||||
$this->databaseManagementService,
|
||||
$this->repository,
|
||||
$this->writer
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -102,7 +69,7 @@ class ServerDeletionServiceTest extends TestCase
|
|||
*/
|
||||
public function testForceParameterCanBeSet()
|
||||
{
|
||||
$response = $this->service->withForce(true);
|
||||
$response = $this->getService()->withForce(true);
|
||||
|
||||
$this->assertInstanceOf(ServerDeletionService::class, $response);
|
||||
}
|
||||
|
@ -112,20 +79,22 @@ class ServerDeletionServiceTest extends TestCase
|
|||
*/
|
||||
public function testServerCanBeDeletedWithoutForce()
|
||||
{
|
||||
$this->daemonServerRepository->shouldReceive('setServer')->with($this->model)->once()->andReturnSelf()
|
||||
->shouldReceive('delete')->withNoArgs()->once()->andReturn(new Response);
|
||||
$model = factory(Server::class)->make();
|
||||
|
||||
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
||||
$this->databaseRepository->shouldReceive('setColumns')->with('id')->once()->andReturnSelf()
|
||||
->shouldReceive('findWhere')->with([
|
||||
['server_id', '=', $this->model->id],
|
||||
])->once()->andReturn(collect([(object) ['id' => 50]]));
|
||||
$this->daemonServerRepository->shouldReceive('setServer')->once()->with($model)->andReturnSelf();
|
||||
$this->daemonServerRepository->shouldReceive('delete')->once()->withNoArgs()->andReturn(new Response);
|
||||
|
||||
$this->databaseManagementService->shouldReceive('delete')->with(50)->once()->andReturnNull();
|
||||
$this->repository->shouldReceive('delete')->with($this->model->id)->once()->andReturn(1);
|
||||
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
|
||||
$this->connection->shouldReceive('beginTransaction')->once()->withNoArgs()->andReturnNull();
|
||||
$this->databaseRepository->shouldReceive('setColumns')->once()->with('id')->andReturnSelf();
|
||||
$this->databaseRepository->shouldReceive('findWhere')->once()->with([
|
||||
['server_id', '=', $model->id],
|
||||
])->andReturn(collect([(object) ['id' => 50]]));
|
||||
|
||||
$this->service->handle($this->model);
|
||||
$this->databaseManagementService->shouldReceive('delete')->once()->with(50)->andReturnNull();
|
||||
$this->repository->shouldReceive('delete')->once()->with($model->id)->andReturn(1);
|
||||
$this->connection->shouldReceive('commit')->once()->withNoArgs()->andReturnNull();
|
||||
|
||||
$this->getService()->handle($model);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -133,64 +102,55 @@ class ServerDeletionServiceTest extends TestCase
|
|||
*/
|
||||
public function testServerShouldBeDeletedEvenWhenFailureOccursIfForceIsSet()
|
||||
{
|
||||
$this->daemonServerRepository->shouldReceive('setServer')->with($this->model)->once()->andReturnSelf()
|
||||
->shouldReceive('delete')->withNoArgs()->once()->andThrow($this->exception);
|
||||
$this->configureExceptionMock();
|
||||
$model = factory(Server::class)->make();
|
||||
|
||||
$this->daemonServerRepository->shouldReceive('setServer')->once()->with($model)->andReturnSelf();
|
||||
$this->daemonServerRepository->shouldReceive('delete')->once()->withNoArgs()->andThrow($this->getExceptionMock());
|
||||
|
||||
$this->exception->shouldReceive('getResponse')->withNoArgs()->once()->andReturnNull();
|
||||
$this->writer->shouldReceive('warning')->with($this->exception)->once()->andReturnNull();
|
||||
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
||||
$this->databaseRepository->shouldReceive('setColumns')->with('id')->once()->andReturnSelf()
|
||||
->shouldReceive('findWhere')->with([
|
||||
['server_id', '=', $this->model->id],
|
||||
])->once()->andReturn(collect([(object) ['id' => 50]]));
|
||||
$this->databaseRepository->shouldReceive('setColumns')->with('id')->once()->andReturnSelf();
|
||||
$this->databaseRepository->shouldReceive('findWhere')->with([
|
||||
['server_id', '=', $model->id],
|
||||
])->once()->andReturn(collect([(object) ['id' => 50]]));
|
||||
|
||||
$this->databaseManagementService->shouldReceive('delete')->with(50)->once()->andReturnNull();
|
||||
$this->repository->shouldReceive('delete')->with($this->model->id)->once()->andReturn(1);
|
||||
$this->repository->shouldReceive('delete')->with($model->id)->once()->andReturn(1);
|
||||
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
|
||||
|
||||
$this->service->withForce()->handle($this->model);
|
||||
$this->getService()->withForce()->handle($model);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that an exception is thrown if a server cannot be deleted from the node and force is not set.
|
||||
*
|
||||
* @expectedException \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
|
||||
*/
|
||||
public function testExceptionShouldBeThrownIfDaemonReturnsAnErrorAndForceIsNotSet()
|
||||
{
|
||||
$this->daemonServerRepository->shouldReceive('setServer->delete')->once()->andThrow($this->exception);
|
||||
$this->exception->shouldReceive('getResponse')->withNoArgs()->once()->andReturnNull();
|
||||
$this->writer->shouldReceive('warning')->with($this->exception)->once()->andReturnNull();
|
||||
$this->configureExceptionMock();
|
||||
$model = factory(Server::class)->make();
|
||||
|
||||
try {
|
||||
$this->service->handle($this->model);
|
||||
} catch (Exception $exception) {
|
||||
$this->assertInstanceOf(DisplayException::class, $exception);
|
||||
$this->assertEquals(trans('admin/server.exceptions.daemon_exception', [
|
||||
'code' => 'E_CONN_REFUSED',
|
||||
]), $exception->getMessage());
|
||||
}
|
||||
$this->daemonServerRepository->shouldReceive('setServer->delete')->once()->andThrow($this->getExceptionMock());
|
||||
|
||||
$this->getService()->handle($model);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that an integer can be passed in place of the Server model.
|
||||
* Return an instance of the class with mocked dependencies.
|
||||
*
|
||||
* @return \Pterodactyl\Services\Servers\ServerDeletionService
|
||||
*/
|
||||
public function testIntegerCanBePassedInPlaceOfServerModel()
|
||||
private function getService(): ServerDeletionService
|
||||
{
|
||||
$this->repository->shouldReceive('setColumns')->with(['id', 'node_id', 'uuid'])->once()->andReturnSelf()
|
||||
->shouldReceive('find')->with($this->model->id)->once()->andReturn($this->model);
|
||||
|
||||
$this->daemonServerRepository->shouldReceive('setServer')->with($this->model)->once()->andReturnSelf()
|
||||
->shouldReceive('delete')->withNoArgs()->once()->andReturn(new Response);
|
||||
|
||||
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
||||
$this->databaseRepository->shouldReceive('setColumns')->with('id')->once()->andReturnSelf()
|
||||
->shouldReceive('findWhere')->with([
|
||||
['server_id', '=', $this->model->id],
|
||||
])->once()->andReturn(collect([(object) ['id' => 50]]));
|
||||
|
||||
$this->databaseManagementService->shouldReceive('delete')->with(50)->once()->andReturnNull();
|
||||
$this->repository->shouldReceive('delete')->with($this->model->id)->once()->andReturn(1);
|
||||
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
|
||||
|
||||
$this->service->handle($this->model->id);
|
||||
return new ServerDeletionService(
|
||||
$this->connection,
|
||||
$this->daemonServerRepository,
|
||||
$this->databaseRepository,
|
||||
$this->databaseManagementService,
|
||||
$this->repository,
|
||||
$this->writer
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -128,10 +128,13 @@ class VariableValidatorServiceTest extends TestCase
|
|||
$messages = $exception->validator->getMessageBag()->all();
|
||||
|
||||
$this->assertNotEmpty($messages);
|
||||
$this->assertSame(1, count($messages));
|
||||
$this->assertSame(trans('validation.required', [
|
||||
'attribute' => trans('validation.internal.variable_value', ['env' => $variables[0]->name]),
|
||||
]), $messages[0]);
|
||||
$this->assertSame(4, count($messages));
|
||||
|
||||
for ($i = 0; $i < 4; $i++) {
|
||||
$this->assertSame(trans('validation.required', [
|
||||
'attribute' => trans('validation.internal.variable_value', ['env' => $variables[$i]->name]),
|
||||
]), $messages[$i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue