Implement server creation though the API.

Also implements auto-deployment to specific locations and ports.
This commit is contained in:
Dane Everitt 2018-01-28 17:14:14 -06:00
parent 97ee95b4da
commit 5ed164e13e
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
24 changed files with 927 additions and 223 deletions

View file

@ -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
);
}