2017-07-23 01:15:01 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Unit\Services\Servers;
|
|
|
|
|
|
|
|
use Mockery as m;
|
2017-08-05 22:26:30 +00:00
|
|
|
use Tests\TestCase;
|
2018-01-06 00:27:47 +00:00
|
|
|
use Pterodactyl\Models\Node;
|
2017-10-27 04:49:54 +00:00
|
|
|
use Pterodactyl\Models\User;
|
2017-10-08 20:44:28 +00:00
|
|
|
use Tests\Traits\MocksUuids;
|
2017-10-27 04:49:54 +00:00
|
|
|
use Pterodactyl\Models\Server;
|
|
|
|
use Tests\Traits\MocksRequestException;
|
2017-08-16 04:21:01 +00:00
|
|
|
use GuzzleHttp\Exception\RequestException;
|
2017-10-06 05:16:22 +00:00
|
|
|
use Illuminate\Database\ConnectionInterface;
|
|
|
|
use Pterodactyl\Exceptions\PterodactylException;
|
2017-08-27 20:10:51 +00:00
|
|
|
use Pterodactyl\Services\Servers\ServerCreationService;
|
2017-07-23 01:15:01 +00:00
|
|
|
use Pterodactyl\Services\Servers\VariableValidatorService;
|
2017-08-05 22:26:30 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\NodeRepositoryInterface;
|
|
|
|
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
|
|
|
|
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
|
2017-07-23 01:15:01 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface;
|
2017-10-06 05:16:22 +00:00
|
|
|
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
|
|
|
|
use Pterodactyl\Services\Servers\ServerConfigurationStructureService;
|
2017-08-05 22:26:30 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\ServerVariableRepositoryInterface;
|
2017-07-23 01:15:01 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface;
|
|
|
|
|
2017-10-06 05:16:22 +00:00
|
|
|
/**
|
|
|
|
* @preserveGlobalState disabled
|
|
|
|
*/
|
2017-08-27 20:10:51 +00:00
|
|
|
class ServerCreationServiceTest extends TestCase
|
2017-07-23 01:15:01 +00:00
|
|
|
{
|
2017-10-27 04:49:54 +00:00
|
|
|
use MocksRequestException, MocksUuids;
|
2017-07-23 01:15:01 +00:00
|
|
|
|
|
|
|
/**
|
2017-10-06 05:16:22 +00:00
|
|
|
* @var \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface|\Mockery\Mock
|
2017-07-23 01:15:01 +00:00
|
|
|
*/
|
2017-10-27 04:49:54 +00:00
|
|
|
private $allocationRepository;
|
2017-07-23 01:15:01 +00:00
|
|
|
|
|
|
|
/**
|
2017-10-06 05:16:22 +00:00
|
|
|
* @var \Pterodactyl\Services\Servers\ServerConfigurationStructureService|\Mockery\Mock
|
|
|
|
*/
|
2017-10-27 04:49:54 +00:00
|
|
|
private $configurationStructureService;
|
2017-10-06 05:16:22 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Illuminate\Database\ConnectionInterface|\Mockery\Mock
|
|
|
|
*/
|
2017-10-27 04:49:54 +00:00
|
|
|
private $connection;
|
2017-10-06 05:16:22 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface|\Mockery\Mock
|
2017-07-23 01:15:01 +00:00
|
|
|
*/
|
2017-10-27 04:49:54 +00:00
|
|
|
private $daemonServerRepository;
|
2017-08-13 19:55:09 +00:00
|
|
|
|
2017-07-23 01:15:01 +00:00
|
|
|
/**
|
2017-10-06 05:16:22 +00:00
|
|
|
* @var \GuzzleHttp\Exception\RequestException|\Mockery\Mock
|
2017-08-13 19:55:09 +00:00
|
|
|
*/
|
2017-10-27 04:49:54 +00:00
|
|
|
private $exception;
|
2017-08-13 19:55:09 +00:00
|
|
|
|
2017-07-23 01:15:01 +00:00
|
|
|
/**
|
2017-10-06 05:16:22 +00:00
|
|
|
* @var \Pterodactyl\Contracts\Repository\NodeRepositoryInterface|\Mockery\Mock
|
2017-07-23 01:15:01 +00:00
|
|
|
*/
|
2017-10-27 04:49:54 +00:00
|
|
|
private $nodeRepository;
|
2017-07-23 01:15:01 +00:00
|
|
|
|
|
|
|
/**
|
2017-10-06 05:16:22 +00:00
|
|
|
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface|\Mockery\Mock
|
2017-07-23 01:15:01 +00:00
|
|
|
*/
|
2017-10-27 04:49:54 +00:00
|
|
|
private $repository;
|
2017-07-23 01:15:01 +00:00
|
|
|
|
|
|
|
/**
|
2017-10-06 05:16:22 +00:00
|
|
|
* @var \Pterodactyl\Contracts\Repository\ServerVariableRepositoryInterface|\Mockery\Mock
|
2017-07-23 01:15:01 +00:00
|
|
|
*/
|
2017-10-27 04:49:54 +00:00
|
|
|
private $serverVariableRepository;
|
2017-07-23 01:15:01 +00:00
|
|
|
|
|
|
|
/**
|
2017-10-06 05:16:22 +00:00
|
|
|
* @var \Pterodactyl\Contracts\Repository\UserRepositoryInterface|\Mockery\Mock
|
2017-07-23 01:15:01 +00:00
|
|
|
*/
|
2017-10-27 04:49:54 +00:00
|
|
|
private $userRepository;
|
2017-07-23 01:15:01 +00:00
|
|
|
|
|
|
|
/**
|
2017-10-06 05:16:22 +00:00
|
|
|
* @var \Pterodactyl\Services\Servers\VariableValidatorService|\Mockery\Mock
|
2017-07-23 01:15:01 +00:00
|
|
|
*/
|
2017-10-27 04:49:54 +00:00
|
|
|
private $validatorService;
|
2017-07-23 01:15:01 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup tests.
|
|
|
|
*/
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->allocationRepository = m::mock(AllocationRepositoryInterface::class);
|
2017-10-06 05:16:22 +00:00
|
|
|
$this->configurationStructureService = m::mock(ServerConfigurationStructureService::class);
|
|
|
|
$this->connection = m::mock(ConnectionInterface::class);
|
2017-07-23 01:15:01 +00:00
|
|
|
$this->daemonServerRepository = m::mock(DaemonServerRepositoryInterface::class);
|
2017-08-13 19:55:09 +00:00
|
|
|
$this->exception = m::mock(RequestException::class);
|
2017-07-23 01:15:01 +00:00
|
|
|
$this->nodeRepository = m::mock(NodeRepositoryInterface::class);
|
|
|
|
$this->repository = m::mock(ServerRepositoryInterface::class);
|
|
|
|
$this->serverVariableRepository = m::mock(ServerVariableRepositoryInterface::class);
|
|
|
|
$this->userRepository = m::mock(UserRepositoryInterface::class);
|
|
|
|
$this->validatorService = m::mock(VariableValidatorService::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test core functionality of the creation process.
|
|
|
|
*/
|
|
|
|
public function testCreateShouldHitAllOfTheNecessaryServicesAndStoreTheServer()
|
|
|
|
{
|
2017-10-27 04:49:54 +00:00
|
|
|
$model = factory(Server::class)->make([
|
|
|
|
'uuid' => $this->getKnownUuid(),
|
|
|
|
]);
|
2017-07-23 01:15:01 +00:00
|
|
|
|
2017-10-06 05:16:22 +00:00
|
|
|
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
2017-10-08 20:44:28 +00:00
|
|
|
$this->repository->shouldReceive('create')->with(m::subset([
|
|
|
|
'uuid' => $this->getKnownUuid(),
|
2017-10-27 04:49:54 +00:00
|
|
|
'node_id' => $model->node_id,
|
|
|
|
'owner_id' => $model->owner_id,
|
|
|
|
'nest_id' => $model->nest_id,
|
|
|
|
'egg_id' => $model->egg_id,
|
|
|
|
]))->once()->andReturn($model);
|
2017-07-23 01:15:01 +00:00
|
|
|
|
2018-01-05 04:49:50 +00:00
|
|
|
$this->allocationRepository->shouldReceive('assignAllocationsToServer')->with($model->id, [$model->allocation_id])->once()->andReturn(1);
|
2017-10-06 05:16:22 +00:00
|
|
|
|
2017-10-27 04:49:54 +00:00
|
|
|
$this->validatorService->shouldReceive('setUserLevel')->with(User::USER_LEVEL_ADMIN)->once()->andReturnNull();
|
|
|
|
$this->validatorService->shouldReceive('handle')->with($model->egg_id, [])->once()->andReturn(
|
|
|
|
collect([(object) ['id' => 123, 'value' => 'var1-value']])
|
|
|
|
);
|
2017-10-06 05:16:22 +00:00
|
|
|
|
2017-10-27 04:49:54 +00:00
|
|
|
$this->serverVariableRepository->shouldReceive('insert')->with([
|
|
|
|
[
|
|
|
|
'server_id' => $model->id,
|
|
|
|
'variable_id' => 123,
|
|
|
|
'variable_value' => 'var1-value',
|
|
|
|
],
|
2018-01-05 04:49:50 +00:00
|
|
|
])->once()->andReturn(true);
|
2017-10-27 04:49:54 +00:00
|
|
|
$this->configurationStructureService->shouldReceive('handle')->with($model)->once()->andReturn(['test' => 'struct']);
|
|
|
|
|
2018-01-06 00:27:47 +00:00
|
|
|
$node = factory(Node::class)->make();
|
|
|
|
$this->nodeRepository->shouldReceive('find')->with($model->node_id)->once()->andReturn($node);
|
|
|
|
|
|
|
|
$this->daemonServerRepository->shouldReceive('setNode')->with($node)->once()->andReturnSelf();
|
2017-12-17 19:07:38 +00:00
|
|
|
$this->daemonServerRepository->shouldReceive('create')->with(['test' => 'struct'], ['start_on_completion' => false])->once();
|
2017-10-06 05:16:22 +00:00
|
|
|
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
|
2017-07-23 01:15:01 +00:00
|
|
|
|
2017-10-27 04:49:54 +00:00
|
|
|
$response = $this->getService()->create($model->toArray());
|
2017-07-23 01:15:01 +00:00
|
|
|
|
2017-10-27 04:49:54 +00:00
|
|
|
$this->assertSame($model, $response);
|
2017-07-23 01:15:01 +00:00
|
|
|
}
|
2017-08-13 19:55:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test handling of node timeout or other daemon error.
|
|
|
|
*/
|
|
|
|
public function testExceptionShouldBeThrownIfTheRequestFails()
|
|
|
|
{
|
2017-10-27 04:49:54 +00:00
|
|
|
$this->configureExceptionMock();
|
|
|
|
|
|
|
|
$model = factory(Server::class)->make([
|
|
|
|
'uuid' => $this->getKnownUuid(),
|
2017-08-13 19:55:09 +00:00
|
|
|
]);
|
|
|
|
|
2017-10-27 04:49:54 +00:00
|
|
|
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
|
|
|
$this->repository->shouldReceive('create')->once()->andReturn($model);
|
2018-01-05 04:49:50 +00:00
|
|
|
$this->allocationRepository->shouldReceive('assignAllocationsToServer')->once()->andReturn(1);
|
2017-10-27 04:49:54 +00:00
|
|
|
$this->validatorService->shouldReceive('setUserLevel')->once()->andReturnNull();
|
|
|
|
$this->validatorService->shouldReceive('handle')->once()->andReturn(collect([]));
|
|
|
|
$this->configurationStructureService->shouldReceive('handle')->once()->andReturn([]);
|
2018-01-06 00:27:47 +00:00
|
|
|
|
|
|
|
$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);
|
2017-10-06 05:16:22 +00:00
|
|
|
$this->connection->shouldReceive('rollBack')->withNoArgs()->once()->andReturnNull();
|
2017-08-13 19:55:09 +00:00
|
|
|
|
|
|
|
try {
|
2017-10-27 04:49:54 +00:00
|
|
|
$this->getService()->create($model->toArray());
|
2017-10-06 05:16:22 +00:00
|
|
|
} catch (PterodactylException $exception) {
|
|
|
|
$this->assertInstanceOf(DaemonConnectionException::class, $exception);
|
2017-10-27 04:49:54 +00:00
|
|
|
$this->assertInstanceOf(RequestException::class, $exception->getPrevious());
|
2017-08-13 19:55:09 +00:00
|
|
|
}
|
|
|
|
}
|
2017-10-27 04:49:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return an instance of the service with mocked dependencies.
|
|
|
|
*
|
|
|
|
* @return \Pterodactyl\Services\Servers\ServerCreationService
|
|
|
|
*/
|
|
|
|
private function getService(): ServerCreationService
|
|
|
|
{
|
|
|
|
return new ServerCreationService(
|
|
|
|
$this->allocationRepository,
|
|
|
|
$this->connection,
|
|
|
|
$this->daemonServerRepository,
|
|
|
|
$this->nodeRepository,
|
|
|
|
$this->configurationStructureService,
|
|
|
|
$this->repository,
|
|
|
|
$this->serverVariableRepository,
|
|
|
|
$this->userRepository,
|
|
|
|
$this->validatorService
|
|
|
|
);
|
|
|
|
}
|
2017-07-23 01:15:01 +00:00
|
|
|
}
|