Refactor how repositories for the daemon work.
This commit is contained in:
parent
5f9fe4a69b
commit
d2afc29a80
58 changed files with 388 additions and 997 deletions
|
@ -1,42 +1,28 @@
|
|||
<?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 Exception;
|
||||
use Mockery as m;
|
||||
use Tests\TestCase;
|
||||
use Illuminate\Log\Writer;
|
||||
use GuzzleHttp\Psr7\Response;
|
||||
use Pterodactyl\Models\Server;
|
||||
use GuzzleHttp\Exception\RequestException;
|
||||
use Pterodactyl\Exceptions\DisplayException;
|
||||
use Pterodactyl\Services\Servers\ContainerRebuildService;
|
||||
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
|
||||
use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface;
|
||||
use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface;
|
||||
|
||||
class ContainerRebuildServiceTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface
|
||||
* @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface|\Mockery\Mock
|
||||
*/
|
||||
protected $daemonServerRepository;
|
||||
protected $repository;
|
||||
|
||||
/**
|
||||
* @var \GuzzleHttp\Exception\RequestException
|
||||
*/
|
||||
protected $exception;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface
|
||||
*/
|
||||
protected $repository;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Models\Server
|
||||
*/
|
||||
|
@ -47,11 +33,6 @@ class ContainerRebuildServiceTest extends TestCase
|
|||
*/
|
||||
protected $service;
|
||||
|
||||
/**
|
||||
* @var \Illuminate\Log\Writer
|
||||
*/
|
||||
protected $writer;
|
||||
|
||||
/**
|
||||
* Setup tests.
|
||||
*/
|
||||
|
@ -59,81 +40,33 @@ class ContainerRebuildServiceTest extends TestCase
|
|||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->daemonServerRepository = m::mock(DaemonServerRepositoryInterface::class);
|
||||
$this->exception = m::mock(RequestException::class)->makePartial();
|
||||
$this->repository = m::mock(ServerRepositoryInterface::class);
|
||||
$this->writer = m::mock(Writer::class);
|
||||
|
||||
$this->server = factory(Server::class)->make(['node_id' => 1]);
|
||||
|
||||
$this->service = new ContainerRebuildService(
|
||||
$this->daemonServerRepository,
|
||||
$this->repository,
|
||||
$this->writer
|
||||
);
|
||||
$this->service = new ContainerRebuildService($this->repository);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that a server is marked for rebuild when it's model is passed to the function.
|
||||
* Test that a server is marked for rebuild.
|
||||
*/
|
||||
public function testServerShouldBeMarkedForARebuildWhenModelIsPassed()
|
||||
public function testServerIsMarkedForRebuild()
|
||||
{
|
||||
$this->repository->shouldNotReceive('find');
|
||||
$this->daemonServerRepository->shouldReceive('setNode')->with($this->server->node_id)->once()->andReturnSelf()
|
||||
->shouldReceive('setAccessServer')->with($this->server->uuid)->once()->andReturnSelf()
|
||||
->shouldReceive('rebuild')->withNoArgs()->once()->andReturnNull();
|
||||
$this->repository->shouldReceive('setServer')->with($this->server)->once()->andReturnSelf()
|
||||
->shouldReceive('rebuild')->withNoArgs()->once()->andReturn(new Response);
|
||||
|
||||
$this->service->rebuild($this->server);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that a server is marked for rebuild when the ID of the server is passed to the function.
|
||||
*/
|
||||
public function testServerShouldBeMarkedForARebuildWhenServerIdIsPassed()
|
||||
{
|
||||
$this->repository->shouldReceive('find')->with($this->server->id)->once()->andReturn($this->server);
|
||||
|
||||
$this->daemonServerRepository->shouldReceive('setNode')->with($this->server->node_id)->once()->andReturnSelf()
|
||||
->shouldReceive('setAccessServer')->with($this->server->uuid)->once()->andReturnSelf()
|
||||
->shouldReceive('rebuild')->withNoArgs()->once()->andReturnNull();
|
||||
|
||||
$this->service->rebuild($this->server->id);
|
||||
$this->service->handle($this->server);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that an exception thrown by guzzle is rendered as a displayable exception.
|
||||
*/
|
||||
public function testExceptionThrownByGuzzleShouldBeReRenderedAsDisplayable()
|
||||
{
|
||||
$this->daemonServerRepository->shouldReceive('setNode')->with($this->server->node_id)
|
||||
->once()->andThrow($this->exception);
|
||||
|
||||
$this->exception->shouldReceive('getResponse')->withNoArgs()->once()->andReturnSelf()
|
||||
->shouldReceive('getStatusCode')->withNoArgs()->once()->andReturn(400);
|
||||
|
||||
$this->writer->shouldReceive('warning')->with($this->exception)->once()->andReturnNull();
|
||||
|
||||
try {
|
||||
$this->service->rebuild($this->server);
|
||||
} catch (Exception $exception) {
|
||||
$this->assertInstanceOf(DisplayException::class, $exception);
|
||||
$this->assertEquals(
|
||||
trans('admin/server.exceptions.daemon_exception', ['code' => 400]),
|
||||
$exception->getMessage()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that an exception thrown by something other than guzzle is not transformed to a displayable.
|
||||
*
|
||||
* @expectedException \Exception
|
||||
* @expectedException \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
|
||||
*/
|
||||
public function testExceptionNotThrownByGuzzleShouldNotBeTransformedToDisplayable()
|
||||
public function testExceptionThrownByGuzzle()
|
||||
{
|
||||
$this->daemonServerRepository->shouldReceive('setNode')->with($this->server->node_id)
|
||||
->once()->andThrow(new Exception());
|
||||
$this->repository->shouldReceive('setServer')->with($this->server)->once()->andThrow($this->exception);
|
||||
|
||||
$this->service->rebuild($this->server);
|
||||
$this->service->handle($this->server);
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue