Fix test failures

This commit is contained in:
Dane Everitt 2017-10-06 00:16:22 -05:00
parent 38075c6b9f
commit 675e780946
No known key found for this signature in database
GPG Key ID: EEA66103B3D71F53
4 changed files with 100 additions and 103 deletions

View File

@ -14,6 +14,9 @@ use Throwable;
class DisplayException extends PterodactylException class DisplayException extends PterodactylException
{ {
const LEVEL_WARNING = 'warning';
const LEVEL_ERROR = 'error';
/** /**
* @var string * @var string
*/ */
@ -27,7 +30,7 @@ class DisplayException extends PterodactylException
* @param string $level * @param string $level
* @internal param mixed $log * @internal param mixed $log
*/ */
public function __construct($message, Throwable $previous = null, $level = 'error') public function __construct($message, Throwable $previous = null, $level = self::LEVEL_ERROR)
{ {
$this->level = $level; $this->level = $level;

View File

@ -0,0 +1,31 @@
<?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 Pterodactyl\Exceptions\Http\Connection;
use GuzzleHttp\Exception\GuzzleException;
use Pterodactyl\Exceptions\DisplayException;
class DaemonConnectionException extends DisplayException
{
/**
* Throw a displayable exception caused by a daemon connection error.
*
* @param \GuzzleHttp\Exception\GuzzleException $previous
*/
public function __construct(GuzzleException $previous)
{
/** @var \GuzzleHttp\Psr7\Response|null $response */
$response = method_exists($previous, 'getResponse') ? $previous->getResponse() : null;
parent::__construct(trans('admin/server.exceptions.daemon_exception', [
'code' => is_null($response) ? 'E_CONN_REFUSED' : $response->getStatusCode(),
]), $previous, DisplayException::LEVEL_WARNING);
}
}

View File

@ -10,15 +10,14 @@
namespace Pterodactyl\Services\Servers; namespace Pterodactyl\Services\Servers;
use Ramsey\Uuid\Uuid; use Ramsey\Uuid\Uuid;
use Illuminate\Log\Writer;
use Illuminate\Database\DatabaseManager;
use GuzzleHttp\Exception\RequestException; use GuzzleHttp\Exception\RequestException;
use Pterodactyl\Exceptions\DisplayException; use Illuminate\Database\ConnectionInterface;
use Pterodactyl\Services\Nodes\NodeCreationService; use Pterodactyl\Services\Nodes\NodeCreationService;
use Pterodactyl\Contracts\Repository\NodeRepositoryInterface; use Pterodactyl\Contracts\Repository\NodeRepositoryInterface;
use Pterodactyl\Contracts\Repository\UserRepositoryInterface; use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface; use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface; use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface;
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
use Pterodactyl\Contracts\Repository\ServerVariableRepositoryInterface; use Pterodactyl\Contracts\Repository\ServerVariableRepositoryInterface;
use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface; use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface;
@ -34,16 +33,16 @@ class ServerCreationService
*/ */
protected $configurationStructureService; protected $configurationStructureService;
/**
* @var \Illuminate\Database\ConnectionInterface
*/
protected $connection;
/** /**
* @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface * @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface
*/ */
protected $daemonServerRepository; protected $daemonServerRepository;
/**
* @var \Illuminate\Database\DatabaseManager
*/
protected $database;
/** /**
* @var \Pterodactyl\Contracts\Repository\NodeRepositoryInterface * @var \Pterodactyl\Contracts\Repository\NodeRepositoryInterface
*/ */
@ -74,17 +73,12 @@ class ServerCreationService
*/ */
protected $validatorService; protected $validatorService;
/**
* @var \Illuminate\Log\Writer
*/
protected $writer;
/** /**
* CreationService constructor. * CreationService constructor.
* *
* @param \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface $allocationRepository * @param \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface $allocationRepository
* @param \Illuminate\Database\ConnectionInterface $connection
* @param \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface $daemonServerRepository * @param \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface $daemonServerRepository
* @param \Illuminate\Database\DatabaseManager $database
* @param \Pterodactyl\Contracts\Repository\NodeRepositoryInterface $nodeRepository * @param \Pterodactyl\Contracts\Repository\NodeRepositoryInterface $nodeRepository
* @param \Pterodactyl\Services\Servers\ServerConfigurationStructureService $configurationStructureService * @param \Pterodactyl\Services\Servers\ServerConfigurationStructureService $configurationStructureService
* @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $repository * @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $repository
@ -92,32 +86,29 @@ class ServerCreationService
* @param \Pterodactyl\Contracts\Repository\UserRepositoryInterface $userRepository * @param \Pterodactyl\Contracts\Repository\UserRepositoryInterface $userRepository
* @param \Pterodactyl\Services\Servers\UsernameGenerationService $usernameService * @param \Pterodactyl\Services\Servers\UsernameGenerationService $usernameService
* @param \Pterodactyl\Services\Servers\VariableValidatorService $validatorService * @param \Pterodactyl\Services\Servers\VariableValidatorService $validatorService
* @param \Illuminate\Log\Writer $writer
*/ */
public function __construct( public function __construct(
AllocationRepositoryInterface $allocationRepository, AllocationRepositoryInterface $allocationRepository,
ConnectionInterface $connection,
DaemonServerRepositoryInterface $daemonServerRepository, DaemonServerRepositoryInterface $daemonServerRepository,
DatabaseManager $database,
NodeRepositoryInterface $nodeRepository, NodeRepositoryInterface $nodeRepository,
ServerConfigurationStructureService $configurationStructureService, ServerConfigurationStructureService $configurationStructureService,
ServerRepositoryInterface $repository, ServerRepositoryInterface $repository,
ServerVariableRepositoryInterface $serverVariableRepository, ServerVariableRepositoryInterface $serverVariableRepository,
UserRepositoryInterface $userRepository, UserRepositoryInterface $userRepository,
UsernameGenerationService $usernameService, UsernameGenerationService $usernameService,
VariableValidatorService $validatorService, VariableValidatorService $validatorService
Writer $writer
) { ) {
$this->allocationRepository = $allocationRepository; $this->allocationRepository = $allocationRepository;
$this->daemonServerRepository = $daemonServerRepository;
$this->configurationStructureService = $configurationStructureService; $this->configurationStructureService = $configurationStructureService;
$this->database = $database; $this->connection = $connection;
$this->daemonServerRepository = $daemonServerRepository;
$this->nodeRepository = $nodeRepository; $this->nodeRepository = $nodeRepository;
$this->repository = $repository; $this->repository = $repository;
$this->serverVariableRepository = $serverVariableRepository; $this->serverVariableRepository = $serverVariableRepository;
$this->userRepository = $userRepository; $this->userRepository = $userRepository;
$this->usernameService = $usernameService; $this->usernameService = $usernameService;
$this->validatorService = $validatorService; $this->validatorService = $validatorService;
$this->writer = $writer;
} }
/** /**
@ -136,7 +127,7 @@ class ServerCreationService
$validator = $this->validatorService->isAdmin()->setFields($data['environment'])->validate($data['option_id']); $validator = $this->validatorService->isAdmin()->setFields($data['environment'])->validate($data['option_id']);
$uniqueShort = str_random(8); $uniqueShort = str_random(8);
$this->database->beginTransaction(); $this->connection->beginTransaction();
$server = $this->repository->create([ $server = $this->repository->create([
'uuid' => Uuid::uuid4()->toString(), 'uuid' => Uuid::uuid4()->toString(),
@ -187,16 +178,13 @@ class ServerCreationService
// Create the server on the daemon & commit it to the database. // Create the server on the daemon & commit it to the database.
try { try {
$this->daemonServerRepository->setNode($server->node_id)->create($structure, ['start_on_completion' => (bool) $data['start_on_completion']]); $this->daemonServerRepository->setNode($server->node_id)->create($structure, [
$this->database->commit(); 'start_on_completion' => (bool) array_get($data, 'start_on_completion', false),
]);
$this->connection->commit();
} catch (RequestException $exception) { } catch (RequestException $exception) {
$response = $exception->getResponse(); $this->connection->rollBack();
$this->writer->warning($exception); throw new DaemonConnectionException($exception);
$this->database->rollBack();
throw new DisplayException(trans('admin/server.exceptions.daemon_exception', [
'code' => is_null($response) ? 'E_CONN_REFUSED' : $response->getStatusCode(),
]));
} }
return $server; return $server;

View File

@ -9,14 +9,12 @@
namespace Tests\Unit\Services\Servers; namespace Tests\Unit\Services\Servers;
use Exception;
use Mockery as m; use Mockery as m;
use Tests\TestCase; use Tests\TestCase;
use Illuminate\Log\Writer;
use phpmock\phpunit\PHPMock; use phpmock\phpunit\PHPMock;
use Illuminate\Database\DatabaseManager;
use GuzzleHttp\Exception\RequestException; use GuzzleHttp\Exception\RequestException;
use Pterodactyl\Exceptions\DisplayException; use Illuminate\Database\ConnectionInterface;
use Pterodactyl\Exceptions\PterodactylException;
use Pterodactyl\Services\Servers\ServerCreationService; use Pterodactyl\Services\Servers\ServerCreationService;
use Pterodactyl\Services\Servers\VariableValidatorService; use Pterodactyl\Services\Servers\VariableValidatorService;
use Pterodactyl\Services\Servers\UsernameGenerationService; use Pterodactyl\Services\Servers\UsernameGenerationService;
@ -24,20 +22,35 @@ use Pterodactyl\Contracts\Repository\NodeRepositoryInterface;
use Pterodactyl\Contracts\Repository\UserRepositoryInterface; use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface; use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface; 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\ServerVariableRepositoryInterface;
use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface; use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface;
/**
* @preserveGlobalState disabled
*/
class ServerCreationServiceTest extends TestCase class ServerCreationServiceTest extends TestCase
{ {
use PHPMock; use PHPMock;
/** /**
* @var \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface * @var \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface|\Mockery\Mock
*/ */
protected $allocationRepository; protected $allocationRepository;
/** /**
* @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface * @var \Pterodactyl\Services\Servers\ServerConfigurationStructureService|\Mockery\Mock
*/
protected $configurationStructureService;
/**
* @var \Illuminate\Database\ConnectionInterface|\Mockery\Mock
*/
protected $connection;
/**
* @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface|\Mockery\Mock
*/ */
protected $daemonServerRepository; protected $daemonServerRepository;
@ -66,27 +79,22 @@ class ServerCreationServiceTest extends TestCase
]; ];
/** /**
* @var \Illuminate\Database\DatabaseManager * @var \GuzzleHttp\Exception\RequestException|\Mockery\Mock
*/
protected $database;
/**
* @var \GuzzleHttp\Exception\RequestException
*/ */
protected $exception; protected $exception;
/** /**
* @var \Pterodactyl\Contracts\Repository\NodeRepositoryInterface * @var \Pterodactyl\Contracts\Repository\NodeRepositoryInterface|\Mockery\Mock
*/ */
protected $nodeRepository; protected $nodeRepository;
/** /**
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface * @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface|\Mockery\Mock
*/ */
protected $repository; protected $repository;
/** /**
* @var \Pterodactyl\Contracts\Repository\ServerVariableRepositoryInterface * @var \Pterodactyl\Contracts\Repository\ServerVariableRepositoryInterface|\Mockery\Mock
*/ */
protected $serverVariableRepository; protected $serverVariableRepository;
@ -96,30 +104,25 @@ class ServerCreationServiceTest extends TestCase
protected $service; protected $service;
/** /**
* @var \Pterodactyl\Contracts\Repository\UserRepositoryInterface * @var \Pterodactyl\Contracts\Repository\UserRepositoryInterface|\Mockery\Mock
*/ */
protected $userRepository; protected $userRepository;
/** /**
* @var \Pterodactyl\Services\Servers\UsernameGenerationService * @var \Pterodactyl\Services\Servers\UsernameGenerationService|\Mockery\Mock
*/ */
protected $usernameService; protected $usernameService;
/** /**
* @var \Pterodactyl\Services\Servers\VariableValidatorService * @var \Pterodactyl\Services\Servers\VariableValidatorService|\Mockery\Mock
*/ */
protected $validatorService; protected $validatorService;
/** /**
* @var \Ramsey\Uuid\Uuid * @var \Ramsey\Uuid\Uuid|\Mockery\Mock
*/ */
protected $uuid; protected $uuid;
/**
* @var \Illuminate\Log\Writer
*/
protected $writer;
/** /**
* Setup tests. * Setup tests.
*/ */
@ -128,8 +131,9 @@ class ServerCreationServiceTest extends TestCase
parent::setUp(); parent::setUp();
$this->allocationRepository = m::mock(AllocationRepositoryInterface::class); $this->allocationRepository = m::mock(AllocationRepositoryInterface::class);
$this->configurationStructureService = m::mock(ServerConfigurationStructureService::class);
$this->connection = m::mock(ConnectionInterface::class);
$this->daemonServerRepository = m::mock(DaemonServerRepositoryInterface::class); $this->daemonServerRepository = m::mock(DaemonServerRepositoryInterface::class);
$this->database = m::mock(DatabaseManager::class);
$this->exception = m::mock(RequestException::class); $this->exception = m::mock(RequestException::class);
$this->nodeRepository = m::mock(NodeRepositoryInterface::class); $this->nodeRepository = m::mock(NodeRepositoryInterface::class);
$this->repository = m::mock(ServerRepositoryInterface::class); $this->repository = m::mock(ServerRepositoryInterface::class);
@ -138,25 +142,21 @@ class ServerCreationServiceTest extends TestCase
$this->usernameService = m::mock(UsernameGenerationService::class); $this->usernameService = m::mock(UsernameGenerationService::class);
$this->validatorService = m::mock(VariableValidatorService::class); $this->validatorService = m::mock(VariableValidatorService::class);
$this->uuid = m::mock('overload:Ramsey\Uuid\Uuid'); $this->uuid = m::mock('overload:Ramsey\Uuid\Uuid');
$this->writer = m::mock(Writer::class);
$this->getFunctionMock('\\Pterodactyl\\Services\\Servers', 'str_random') $this->getFunctionMock('\\Pterodactyl\\Services\\Servers', 'str_random')
->expects($this->any())->willReturn('random_string'); ->expects($this->any())->willReturn('random_string');
$this->getFunctionMock('\\Ramsey\\Uuid\\Uuid', 'uuid4')
->expects($this->any())->willReturn('s');
$this->service = new ServerCreationService( $this->service = new ServerCreationService(
$this->allocationRepository, $this->allocationRepository,
$this->connection,
$this->daemonServerRepository, $this->daemonServerRepository,
$this->database,
$this->nodeRepository, $this->nodeRepository,
$this->configurationStructureService,
$this->repository, $this->repository,
$this->serverVariableRepository, $this->serverVariableRepository,
$this->userRepository, $this->userRepository,
$this->usernameService, $this->usernameService,
$this->validatorService, $this->validatorService
$this->writer
); );
} }
@ -169,37 +169,12 @@ class ServerCreationServiceTest extends TestCase
->shouldReceive('setFields')->with($this->data['environment'])->once()->andReturnSelf() ->shouldReceive('setFields')->with($this->data['environment'])->once()->andReturnSelf()
->shouldReceive('validate')->with($this->data['option_id'])->once()->andReturnSelf(); ->shouldReceive('validate')->with($this->data['option_id'])->once()->andReturnSelf();
$this->database->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull(); $this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->uuid->shouldReceive('uuid4')->withNoArgs()->once()->andReturnSelf() $this->uuid->shouldReceive('uuid4->toString')->withNoArgs()->once()->andReturn('uuid-0000');
->shouldReceive('toString')->withNoArgs()->once()->andReturn('uuid-0000');
$this->usernameService->shouldReceive('generate')->with($this->data['name'], 'random_string') $this->usernameService->shouldReceive('generate')->with($this->data['name'], 'random_string')
->once()->andReturn('user_name'); ->once()->andReturn('user_name');
$this->repository->shouldReceive('create')->with([ $this->repository->shouldReceive('create')->withAnyArgs()->once()->andReturn((object) [
'uuid' => 'uuid-0000',
'uuidShort' => 'random_string',
'node_id' => $this->data['node_id'],
'name' => $this->data['name'],
'description' => $this->data['description'],
'skip_scripts' => false,
'suspended' => false,
'owner_id' => $this->data['owner_id'],
'memory' => $this->data['memory'],
'swap' => $this->data['swap'],
'disk' => $this->data['disk'],
'io' => $this->data['io'],
'cpu' => $this->data['cpu'],
'oom_disabled' => false,
'allocation_id' => $this->data['allocation_id'],
'service_id' => $this->data['service_id'],
'option_id' => $this->data['option_id'],
'pack_id' => null,
'startup' => $this->data['startup'],
'daemonSecret' => 'random_string',
'image' => $this->data['docker_image'],
'username' => 'user_name',
'sftp_password' => null,
])->once()->andReturn((object) [
'node_id' => 1, 'node_id' => 1,
'id' => 1, 'id' => 1,
]); ]);
@ -216,9 +191,12 @@ class ServerCreationServiceTest extends TestCase
'variable_id' => 1, 'variable_id' => 1,
'variable_value' => 'var1-value', 'variable_value' => 'var1-value',
]])->once()->andReturnNull(); ]])->once()->andReturnNull();
$this->configurationStructureService->shouldReceive('handle')->with(1)->once()->andReturn(['test' => 'struct']);
$this->daemonServerRepository->shouldReceive('setNode')->with(1)->once()->andReturnSelf() $this->daemonServerRepository->shouldReceive('setNode')->with(1)->once()->andReturnSelf()
->shouldReceive('create')->with(1)->once()->andReturnNull(); ->shouldReceive('create')->with(['test' => 'struct'], ['start_on_completion' => false])->once()->andReturnNull();
$this->database->shouldReceive('commit')->withNoArgs()->once()->andReturnNull(); $this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$response = $this->service->create($this->data); $response = $this->service->create($this->data);
@ -232,9 +210,9 @@ class ServerCreationServiceTest extends TestCase
public function testExceptionShouldBeThrownIfTheRequestFails() public function testExceptionShouldBeThrownIfTheRequestFails()
{ {
$this->validatorService->shouldReceive('isAdmin->setFields->validate->getResults')->once()->andReturn([]); $this->validatorService->shouldReceive('isAdmin->setFields->validate->getResults')->once()->andReturn([]);
$this->database->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull(); $this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->uuid->shouldReceive('uuid4->toString')->once()->andReturn('uuid-0000');
$this->usernameService->shouldReceive('generate')->once()->andReturn('user_name'); $this->usernameService->shouldReceive('generate')->once()->andReturn('user_name');
$this->uuid->shouldReceive('uuid4->toString')->withNoArgs()->once()->andReturn('uuid-0000');
$this->repository->shouldReceive('create')->once()->andReturn((object) [ $this->repository->shouldReceive('create')->once()->andReturn((object) [
'node_id' => 1, 'node_id' => 1,
'id' => 1, 'id' => 1,
@ -242,18 +220,15 @@ class ServerCreationServiceTest extends TestCase
$this->allocationRepository->shouldReceive('assignAllocationsToServer')->once()->andReturnNull(); $this->allocationRepository->shouldReceive('assignAllocationsToServer')->once()->andReturnNull();
$this->serverVariableRepository->shouldReceive('insert')->with([])->once()->andReturnNull(); $this->serverVariableRepository->shouldReceive('insert')->with([])->once()->andReturnNull();
$this->configurationStructureService->shouldReceive('handle')->once()->andReturnNull();
$this->daemonServerRepository->shouldReceive('setNode->create')->once()->andThrow($this->exception); $this->daemonServerRepository->shouldReceive('setNode->create')->once()->andThrow($this->exception);
$this->exception->shouldReceive('getResponse')->withNoArgs()->once()->andReturnNull(); $this->exception->shouldReceive('getResponse')->withNoArgs()->once()->andReturnNull();
$this->writer->shouldReceive('warning')->with($this->exception)->once()->andReturnNull(); $this->connection->shouldReceive('rollBack')->withNoArgs()->once()->andReturnNull();
$this->database->shouldReceive('rollBack')->withNoArgs()->once()->andReturnNull();
try { try {
$this->service->create($this->data); $this->service->create($this->data);
} catch (Exception $exception) { } catch (PterodactylException $exception) {
$this->assertInstanceOf(DisplayException::class, $exception); $this->assertInstanceOf(DaemonConnectionException::class, $exception);
$this->assertEquals(trans('admin/server.exceptions.daemon_exception', [
'code' => 'E_CONN_REFUSED',
]), $exception->getMessage());
} }
} }
} }