2017-07-23 01:15:01 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Pterodactyl - Panel
|
|
|
|
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
|
|
|
|
*
|
2017-09-26 02:43:01 +00:00
|
|
|
* This software is licensed under the terms of the MIT license.
|
|
|
|
* https://opensource.org/licenses/MIT
|
2017-07-23 01:15:01 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Tests\Unit\Services\Servers;
|
|
|
|
|
|
|
|
use Mockery as m;
|
2017-08-05 22:26:30 +00:00
|
|
|
use Tests\TestCase;
|
2017-07-23 01:15:01 +00:00
|
|
|
use phpmock\phpunit\PHPMock;
|
2017-10-08 20:44:28 +00:00
|
|
|
use Tests\Traits\MocksUuids;
|
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\Services\Servers\UsernameGenerationService;
|
|
|
|
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-08 20:44:28 +00:00
|
|
|
use MocksUuids, PHPMock;
|
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
|
|
|
*/
|
|
|
|
protected $allocationRepository;
|
|
|
|
|
|
|
|
/**
|
2017-10-06 05:16:22 +00:00
|
|
|
* @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
|
2017-07-23 01:15:01 +00:00
|
|
|
*/
|
|
|
|
protected $daemonServerRepository;
|
|
|
|
|
2017-08-13 19:55:09 +00:00
|
|
|
/**
|
|
|
|
* @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',
|
|
|
|
],
|
2017-10-08 20:44:28 +00:00
|
|
|
'nest_id' => 1,
|
|
|
|
'egg_id' => 1,
|
2017-08-13 19:55:09 +00:00
|
|
|
'startup' => 'startup-param',
|
|
|
|
'docker_image' => 'some/image',
|
|
|
|
];
|
|
|
|
|
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
|
|
|
*/
|
|
|
|
protected $exception;
|
|
|
|
|
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
|
|
|
*/
|
|
|
|
protected $nodeRepository;
|
|
|
|
|
|
|
|
/**
|
2017-10-06 05:16:22 +00:00
|
|
|
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface|\Mockery\Mock
|
2017-07-23 01:15:01 +00:00
|
|
|
*/
|
|
|
|
protected $repository;
|
|
|
|
|
|
|
|
/**
|
2017-10-06 05:16:22 +00:00
|
|
|
* @var \Pterodactyl\Contracts\Repository\ServerVariableRepositoryInterface|\Mockery\Mock
|
2017-07-23 01:15:01 +00:00
|
|
|
*/
|
|
|
|
protected $serverVariableRepository;
|
|
|
|
|
|
|
|
/**
|
2017-08-27 20:10:51 +00:00
|
|
|
* @var \Pterodactyl\Services\Servers\ServerCreationService
|
2017-07-23 01:15:01 +00:00
|
|
|
*/
|
|
|
|
protected $service;
|
|
|
|
|
|
|
|
/**
|
2017-10-06 05:16:22 +00:00
|
|
|
* @var \Pterodactyl\Contracts\Repository\UserRepositoryInterface|\Mockery\Mock
|
2017-07-23 01:15:01 +00:00
|
|
|
*/
|
|
|
|
protected $userRepository;
|
|
|
|
|
|
|
|
/**
|
2017-10-06 05:16:22 +00:00
|
|
|
* @var \Pterodactyl\Services\Servers\UsernameGenerationService|\Mockery\Mock
|
2017-07-23 01:15:01 +00:00
|
|
|
*/
|
|
|
|
protected $usernameService;
|
|
|
|
|
|
|
|
/**
|
2017-10-06 05:16:22 +00:00
|
|
|
* @var \Pterodactyl\Services\Servers\VariableValidatorService|\Mockery\Mock
|
2017-07-23 01:15:01 +00:00
|
|
|
*/
|
|
|
|
protected $validatorService;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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->usernameService = m::mock(UsernameGenerationService::class);
|
|
|
|
$this->validatorService = m::mock(VariableValidatorService::class);
|
|
|
|
|
2017-09-14 04:07:02 +00:00
|
|
|
$this->getFunctionMock('\\Pterodactyl\\Services\\Servers', 'str_random')
|
|
|
|
->expects($this->any())->willReturn('random_string');
|
2017-07-23 01:15:01 +00:00
|
|
|
|
2017-08-27 20:10:51 +00:00
|
|
|
$this->service = new ServerCreationService(
|
2017-07-23 01:15:01 +00:00
|
|
|
$this->allocationRepository,
|
2017-10-06 05:16:22 +00:00
|
|
|
$this->connection,
|
2017-07-23 01:15:01 +00:00
|
|
|
$this->daemonServerRepository,
|
|
|
|
$this->nodeRepository,
|
2017-10-06 05:16:22 +00:00
|
|
|
$this->configurationStructureService,
|
2017-07-23 01:15:01 +00:00
|
|
|
$this->repository,
|
|
|
|
$this->serverVariableRepository,
|
|
|
|
$this->userRepository,
|
|
|
|
$this->usernameService,
|
2017-10-06 05:16:22 +00:00
|
|
|
$this->validatorService
|
2017-07-23 01:15:01 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test core functionality of the creation process.
|
|
|
|
*/
|
|
|
|
public function testCreateShouldHitAllOfTheNecessaryServicesAndStoreTheServer()
|
|
|
|
{
|
2017-07-25 02:34:10 +00:00
|
|
|
$this->validatorService->shouldReceive('isAdmin')->withNoArgs()->once()->andReturnSelf()
|
2017-08-13 19:55:09 +00:00
|
|
|
->shouldReceive('setFields')->with($this->data['environment'])->once()->andReturnSelf()
|
2017-10-08 20:44:28 +00:00
|
|
|
->shouldReceive('validate')->with($this->data['egg_id'])->once()->andReturnSelf();
|
2017-07-23 01:15:01 +00:00
|
|
|
|
2017-10-06 05:16:22 +00:00
|
|
|
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
2017-09-14 04:07:02 +00:00
|
|
|
$this->usernameService->shouldReceive('generate')->with($this->data['name'], 'random_string')
|
2017-07-23 01:15:01 +00:00
|
|
|
->once()->andReturn('user_name');
|
|
|
|
|
2017-10-08 20:44:28 +00:00
|
|
|
$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) [
|
2017-07-23 01:15:01 +00:00
|
|
|
'node_id' => 1,
|
|
|
|
'id' => 1,
|
|
|
|
]);
|
|
|
|
|
2017-08-13 19:55:09 +00:00
|
|
|
$this->allocationRepository->shouldReceive('assignAllocationsToServer')->with(1, [1, 2, 3])->once()->andReturnNull();
|
2017-07-23 01:15:01 +00:00
|
|
|
$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();
|
2017-10-06 05:16:22 +00:00
|
|
|
|
|
|
|
$this->configurationStructureService->shouldReceive('handle')->with(1)->once()->andReturn(['test' => 'struct']);
|
|
|
|
|
2017-07-23 01:15:01 +00:00
|
|
|
$this->daemonServerRepository->shouldReceive('setNode')->with(1)->once()->andReturnSelf()
|
2017-10-06 05:16:22 +00:00
|
|
|
->shouldReceive('create')->with(['test' => 'struct'], ['start_on_completion' => false])->once()->andReturnNull();
|
|
|
|
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
|
2017-07-23 01:15:01 +00:00
|
|
|
|
2017-08-13 19:55:09 +00:00
|
|
|
$response = $this->service->create($this->data);
|
2017-07-23 01:15:01 +00:00
|
|
|
|
|
|
|
$this->assertEquals(1, $response->id);
|
|
|
|
$this->assertEquals(1, $response->node_id);
|
|
|
|
}
|
2017-08-13 19:55:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test handling of node timeout or other daemon error.
|
|
|
|
*/
|
|
|
|
public function testExceptionShouldBeThrownIfTheRequestFails()
|
|
|
|
{
|
|
|
|
$this->validatorService->shouldReceive('isAdmin->setFields->validate->getResults')->once()->andReturn([]);
|
2017-10-06 05:16:22 +00:00
|
|
|
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
2017-08-13 19:55:09 +00:00
|
|
|
$this->usernameService->shouldReceive('generate')->once()->andReturn('user_name');
|
|
|
|
$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();
|
2017-10-06 05:16:22 +00:00
|
|
|
$this->configurationStructureService->shouldReceive('handle')->once()->andReturnNull();
|
2017-08-13 19:55:09 +00:00
|
|
|
$this->daemonServerRepository->shouldReceive('setNode->create')->once()->andThrow($this->exception);
|
|
|
|
$this->exception->shouldReceive('getResponse')->withNoArgs()->once()->andReturnNull();
|
2017-10-06 05:16:22 +00:00
|
|
|
$this->connection->shouldReceive('rollBack')->withNoArgs()->once()->andReturnNull();
|
2017-08-13 19:55:09 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
$this->service->create($this->data);
|
2017-10-06 05:16:22 +00:00
|
|
|
} catch (PterodactylException $exception) {
|
|
|
|
$this->assertInstanceOf(DaemonConnectionException::class, $exception);
|
2017-08-13 19:55:09 +00:00
|
|
|
}
|
|
|
|
}
|
2017-07-23 01:15:01 +00:00
|
|
|
}
|