Refactor startup modification and environment variable services
Better setup, more flexibility, more tests.
This commit is contained in:
parent
7022ec788f
commit
fa62a0982e
19 changed files with 660 additions and 563 deletions
|
@ -1,18 +1,13 @@
|
|||
<?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 Mockery as m;
|
||||
use Tests\TestCase;
|
||||
use phpmock\phpunit\PHPMock;
|
||||
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;
|
||||
|
@ -32,86 +27,57 @@ use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonS
|
|||
*/
|
||||
class ServerCreationServiceTest extends TestCase
|
||||
{
|
||||
use MocksUuids, PHPMock;
|
||||
use MocksRequestException, MocksUuids;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface|\Mockery\Mock
|
||||
*/
|
||||
protected $allocationRepository;
|
||||
private $allocationRepository;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Services\Servers\ServerConfigurationStructureService|\Mockery\Mock
|
||||
*/
|
||||
protected $configurationStructureService;
|
||||
private $configurationStructureService;
|
||||
|
||||
/**
|
||||
* @var \Illuminate\Database\ConnectionInterface|\Mockery\Mock
|
||||
*/
|
||||
protected $connection;
|
||||
private $connection;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface|\Mockery\Mock
|
||||
*/
|
||||
protected $daemonServerRepository;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $data = [
|
||||
'node_id' => 1,
|
||||
'name' => 'SomeName',
|
||||
'description' => null,
|
||||
'owner_id' => 1,
|
||||
'memory' => 128,
|
||||
'disk' => 128,
|
||||
'swap' => 0,
|
||||
'io' => 500,
|
||||
'cpu' => 0,
|
||||
'allocation_id' => 1,
|
||||
'allocation_additional' => [2, 3],
|
||||
'environment' => [
|
||||
'TEST_VAR_1' => 'var1-value',
|
||||
],
|
||||
'nest_id' => 1,
|
||||
'egg_id' => 1,
|
||||
'startup' => 'startup-param',
|
||||
'docker_image' => 'some/image',
|
||||
];
|
||||
private $daemonServerRepository;
|
||||
|
||||
/**
|
||||
* @var \GuzzleHttp\Exception\RequestException|\Mockery\Mock
|
||||
*/
|
||||
protected $exception;
|
||||
private $exception;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\NodeRepositoryInterface|\Mockery\Mock
|
||||
*/
|
||||
protected $nodeRepository;
|
||||
private $nodeRepository;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface|\Mockery\Mock
|
||||
*/
|
||||
protected $repository;
|
||||
private $repository;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\ServerVariableRepositoryInterface|\Mockery\Mock
|
||||
*/
|
||||
protected $serverVariableRepository;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Services\Servers\ServerCreationService
|
||||
*/
|
||||
protected $service;
|
||||
private $serverVariableRepository;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\UserRepositoryInterface|\Mockery\Mock
|
||||
*/
|
||||
protected $userRepository;
|
||||
private $userRepository;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Services\Servers\VariableValidatorService|\Mockery\Mock
|
||||
*/
|
||||
protected $validatorService;
|
||||
private $validatorService;
|
||||
|
||||
/**
|
||||
* Setup tests.
|
||||
|
@ -130,11 +96,87 @@ class ServerCreationServiceTest extends TestCase
|
|||
$this->serverVariableRepository = m::mock(ServerVariableRepositoryInterface::class);
|
||||
$this->userRepository = m::mock(UserRepositoryInterface::class);
|
||||
$this->validatorService = m::mock(VariableValidatorService::class);
|
||||
}
|
||||
|
||||
$this->getFunctionMock('\\Pterodactyl\\Services\\Servers', 'str_random')
|
||||
->expects($this->any())->willReturn('random_string');
|
||||
/**
|
||||
* Test core functionality of the creation process.
|
||||
*/
|
||||
public function testCreateShouldHitAllOfTheNecessaryServicesAndStoreTheServer()
|
||||
{
|
||||
$model = factory(Server::class)->make([
|
||||
'uuid' => $this->getKnownUuid(),
|
||||
]);
|
||||
|
||||
$this->service = new ServerCreationService(
|
||||
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
||||
$this->repository->shouldReceive('create')->with(m::subset([
|
||||
'uuid' => $this->getKnownUuid(),
|
||||
'node_id' => $model->node_id,
|
||||
'owner_id' => $model->owner_id,
|
||||
'nest_id' => $model->nest_id,
|
||||
'egg_id' => $model->egg_id,
|
||||
]))->once()->andReturn($model);
|
||||
|
||||
$this->allocationRepository->shouldReceive('assignAllocationsToServer')->with($model->id, [$model->allocation_id])->once()->andReturnNull();
|
||||
|
||||
$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']])
|
||||
);
|
||||
|
||||
$this->serverVariableRepository->shouldReceive('insert')->with([
|
||||
[
|
||||
'server_id' => $model->id,
|
||||
'variable_id' => 123,
|
||||
'variable_value' => 'var1-value',
|
||||
],
|
||||
])->once()->andReturnNull();
|
||||
$this->configurationStructureService->shouldReceive('handle')->with($model)->once()->andReturn(['test' => 'struct']);
|
||||
|
||||
$this->daemonServerRepository->shouldReceive('setNode')->with($model->node_id)->once()->andReturnSelf();
|
||||
$this->daemonServerRepository->shouldReceive('create')->with(['test' => 'struct'], ['start_on_completion' => false])->once()->andReturnNull();
|
||||
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
|
||||
|
||||
$response = $this->getService()->create($model->toArray());
|
||||
|
||||
$this->assertSame($model, $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test handling of node timeout or other daemon error.
|
||||
*/
|
||||
public function testExceptionShouldBeThrownIfTheRequestFails()
|
||||
{
|
||||
$this->configureExceptionMock();
|
||||
|
||||
$model = factory(Server::class)->make([
|
||||
'uuid' => $this->getKnownUuid(),
|
||||
]);
|
||||
|
||||
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
||||
$this->repository->shouldReceive('create')->once()->andReturn($model);
|
||||
$this->allocationRepository->shouldReceive('assignAllocationsToServer')->once()->andReturnNull();
|
||||
$this->validatorService->shouldReceive('setUserLevel')->once()->andReturnNull();
|
||||
$this->validatorService->shouldReceive('handle')->once()->andReturn(collect([]));
|
||||
$this->configurationStructureService->shouldReceive('handle')->once()->andReturn([]);
|
||||
$this->daemonServerRepository->shouldReceive('setNode')->with($model->node_id)->once()->andThrow($this->exception);
|
||||
$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());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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,
|
||||
|
@ -146,77 +188,4 @@ class ServerCreationServiceTest extends TestCase
|
|||
$this->validatorService
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test core functionality of the creation process.
|
||||
*/
|
||||
public function testCreateShouldHitAllOfTheNecessaryServicesAndStoreTheServer()
|
||||
{
|
||||
$this->validatorService->shouldReceive('isAdmin')->withNoArgs()->once()->andReturnSelf()
|
||||
->shouldReceive('setFields')->with($this->data['environment'])->once()->andReturnSelf()
|
||||
->shouldReceive('validate')->with($this->data['egg_id'])->once()->andReturnSelf();
|
||||
|
||||
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
||||
|
||||
$this->repository->shouldReceive('create')->with(m::subset([
|
||||
'uuid' => $this->getKnownUuid(),
|
||||
'node_id' => $this->data['node_id'],
|
||||
'owner_id' => 1,
|
||||
'nest_id' => 1,
|
||||
'egg_id' => 1,
|
||||
]))->once()->andReturn((object) [
|
||||
'node_id' => 1,
|
||||
'id' => 1,
|
||||
]);
|
||||
|
||||
$this->allocationRepository->shouldReceive('assignAllocationsToServer')->with(1, [1, 2, 3])->once()->andReturnNull();
|
||||
$this->validatorService->shouldReceive('getResults')->withNoArgs()->once()->andReturn([[
|
||||
'id' => 1,
|
||||
'key' => 'TEST_VAR_1',
|
||||
'value' => 'var1-value',
|
||||
]]);
|
||||
|
||||
$this->serverVariableRepository->shouldReceive('insert')->with([[
|
||||
'server_id' => 1,
|
||||
'variable_id' => 1,
|
||||
'variable_value' => 'var1-value',
|
||||
]])->once()->andReturnNull();
|
||||
|
||||
$this->configurationStructureService->shouldReceive('handle')->with(1)->once()->andReturn(['test' => 'struct']);
|
||||
|
||||
$this->daemonServerRepository->shouldReceive('setNode')->with(1)->once()->andReturnSelf()
|
||||
->shouldReceive('create')->with(['test' => 'struct'], ['start_on_completion' => false])->once()->andReturnNull();
|
||||
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
|
||||
|
||||
$response = $this->service->create($this->data);
|
||||
|
||||
$this->assertEquals(1, $response->id);
|
||||
$this->assertEquals(1, $response->node_id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test handling of node timeout or other daemon error.
|
||||
*/
|
||||
public function testExceptionShouldBeThrownIfTheRequestFails()
|
||||
{
|
||||
$this->validatorService->shouldReceive('isAdmin->setFields->validate->getResults')->once()->andReturn([]);
|
||||
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
||||
$this->repository->shouldReceive('create')->once()->andReturn((object) [
|
||||
'node_id' => 1,
|
||||
'id' => 1,
|
||||
]);
|
||||
|
||||
$this->allocationRepository->shouldReceive('assignAllocationsToServer')->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->exception->shouldReceive('getResponse')->withNoArgs()->once()->andReturnNull();
|
||||
$this->connection->shouldReceive('rollBack')->withNoArgs()->once()->andReturnNull();
|
||||
|
||||
try {
|
||||
$this->service->create($this->data);
|
||||
} catch (PterodactylException $exception) {
|
||||
$this->assertInstanceOf(DaemonConnectionException::class, $exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue