Update some server service tests

This commit is contained in:
Dane Everitt 2020-06-24 22:18:48 -07:00
parent 83a59cdf4f
commit b55767426f
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
6 changed files with 117 additions and 165 deletions

View file

@ -5,24 +5,27 @@ namespace Tests\Unit\Services\Servers;
use Mockery as m;
use Tests\TestCase;
use Pterodactyl\Models\Egg;
use GuzzleHttp\Psr7\Request;
use Pterodactyl\Models\User;
use Tests\Traits\MocksUuids;
use Pterodactyl\Models\Server;
use Pterodactyl\Models\Allocation;
use Tests\Traits\MocksRequestException;
use GuzzleHttp\Exception\ConnectException;
use Illuminate\Database\ConnectionInterface;
use Pterodactyl\Models\Objects\DeploymentObject;
use Pterodactyl\Repositories\Eloquent\EggRepository;
use Pterodactyl\Repositories\Eloquent\ServerRepository;
use Pterodactyl\Services\Servers\ServerCreationService;
use Pterodactyl\Services\Servers\ServerDeletionService;
use Pterodactyl\Repositories\Wings\DaemonServerRepository;
use Pterodactyl\Services\Servers\VariableValidatorService;
use Pterodactyl\Repositories\Eloquent\AllocationRepository;
use Pterodactyl\Services\Deployment\FindViableNodesService;
use Pterodactyl\Contracts\Repository\EggRepositoryInterface;
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
use Pterodactyl\Repositories\Eloquent\ServerVariableRepository;
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;
/**
* @preserveGlobalState disabled
@ -32,60 +35,60 @@ class ServerCreationServiceTest extends TestCase
use MocksRequestException, MocksUuids;
/**
* @var \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface|\Mockery\Mock
* @var \Mockery\MockInterface
*/
private $allocationRepository;
/**
* @var \Pterodactyl\Services\Deployment\AllocationSelectionService|\Mockery\Mock
* @var \Mockery\MockInterface
*/
private $allocationSelectionService;
/**
* @var \Pterodactyl\Services\Servers\ServerConfigurationStructureService|\Mockery\Mock
* @var \Mockery\MockInterface
*/
private $configurationStructureService;
/**
* @var \Illuminate\Database\ConnectionInterface|\Mockery\Mock
* @var \Mockery\MockInterface
*/
private $connection;
/**
* @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface|\Mockery\Mock
* @var \Mockery\MockInterface
*/
private $daemonServerRepository;
/**
* @var \Pterodactyl\Contracts\Repository\EggRepositoryInterface|\Mockery\Mock
* @var \Mockery\MockInterface
*/
private $eggRepository;
/**
* @var \Pterodactyl\Services\Deployment\FindViableNodesService|\Mockery\Mock
* @var \Mockery\MockInterface
*/
private $findViableNodesService;
/**
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface|\Mockery\Mock
* @var \Mockery\MockInterface
*/
private $repository;
/**
* @var \Pterodactyl\Contracts\Repository\ServerVariableRepositoryInterface|\Mockery\Mock
* @var \Mockery\MockInterface
*/
private $serverVariableRepository;
/**
* @var \Pterodactyl\Contracts\Repository\UserRepositoryInterface|\Mockery\Mock
*/
private $userRepository;
/**
* @var \Pterodactyl\Services\Servers\VariableValidatorService|\Mockery\Mock
* @var \Mockery\MockInterface
*/
private $validatorService;
/**
* @var \Mockery\MockInterface
*/
private $serverDeletionService;
/**
* Setup tests.
*/
@ -93,17 +96,17 @@ class ServerCreationServiceTest extends TestCase
{
parent::setUp();
$this->allocationRepository = m::mock(AllocationRepositoryInterface::class);
$this->allocationRepository = m::mock(AllocationRepository::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->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);
$this->validatorService = m::mock(VariableValidatorService::class);
$this->eggRepository = m::mock(EggRepository::class);
$this->repository = m::mock(ServerRepository::class);
$this->serverVariableRepository = m::mock(ServerVariableRepository::class);
$this->daemonServerRepository = m::mock(DaemonServerRepository::class);
$this->serverDeletionService = m::mock(ServerDeletionService::class);
}
/**
@ -148,7 +151,7 @@ class ServerCreationServiceTest extends TestCase
$this->configurationStructureService->shouldReceive('handle')->with($model)->once()->andReturn(['test' => 'struct']);
$this->daemonServerRepository->shouldReceive('setServer')->with($model)->once()->andReturnSelf();
$this->daemonServerRepository->shouldReceive('create')->with(['test' => 'struct'], ['start_on_completion' => false])->once();
$this->daemonServerRepository->shouldReceive('create')->with(['test' => 'struct'])->once();
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$response = $this->getService()->handle($model->toArray());
@ -250,12 +253,10 @@ class ServerCreationServiceTest extends TestCase
/**
* Test handling of node timeout or other daemon error.
*
* @expectedException \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
*/
public function testExceptionShouldBeThrownIfTheRequestFails()
{
$this->configureExceptionMock();
$this->expectException(DaemonConnectionException::class);
$model = factory(Server::class)->make([
'uuid' => $this->getKnownUuid(),
@ -269,8 +270,16 @@ class ServerCreationServiceTest extends TestCase
$this->validatorService->shouldReceive('handle')->once()->andReturn(collect([]));
$this->configurationStructureService->shouldReceive('handle')->once()->andReturn([]);
$this->daemonServerRepository->shouldReceive('setServer')->with($model)->once()->andThrow($this->getExceptionMock());
$this->connection->shouldReceive('rollBack')->withNoArgs()->once()->andReturnNull();
$this->connection->expects('commit')->withNoArgs();
$this->daemonServerRepository->shouldReceive('setServer')->with($model)->once()->andThrow(
new DaemonConnectionException(
new ConnectException('', new Request('GET', 'test'))
)
);
$this->serverDeletionService->expects('withForce')->with(true)->andReturnSelf();
$this->serverDeletionService->expects('handle')->with($model);
$this->getService()->handle($model->toArray());
}
@ -290,6 +299,7 @@ class ServerCreationServiceTest extends TestCase
$this->eggRepository,
$this->findViableNodesService,
$this->configurationStructureService,
$this->serverDeletionService,
$this->repository,
$this->serverVariableRepository,
$this->validatorService