2018-03-03 03:26:42 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Unit\Commands\Server;
|
|
|
|
|
|
|
|
use Mockery as m;
|
2019-08-03 21:04:31 +00:00
|
|
|
use Pterodactyl\Models\Node;
|
|
|
|
use GuzzleHttp\Psr7\Response;
|
2018-03-03 03:26:42 +00:00
|
|
|
use Pterodactyl\Models\Server;
|
2020-06-24 04:33:56 +00:00
|
|
|
use Illuminate\Support\Collection;
|
2018-03-03 03:26:42 +00:00
|
|
|
use Illuminate\Validation\Factory;
|
|
|
|
use Tests\Unit\Commands\CommandTestCase;
|
2020-06-24 04:33:56 +00:00
|
|
|
use Illuminate\Validation\ValidationException;
|
2020-06-24 04:24:37 +00:00
|
|
|
use Pterodactyl\Repositories\Wings\DaemonPowerRepository;
|
2018-03-03 03:26:42 +00:00
|
|
|
use Pterodactyl\Console\Commands\Server\BulkPowerActionCommand;
|
|
|
|
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
|
|
|
|
|
|
|
|
class BulkPowerActionCommandTest extends CommandTestCase
|
|
|
|
{
|
|
|
|
/**
|
2020-06-24 04:24:37 +00:00
|
|
|
* @var \Mockery\MockInterface
|
2018-03-03 03:26:42 +00:00
|
|
|
*/
|
|
|
|
private $powerRepository;
|
|
|
|
|
|
|
|
/**
|
2020-06-24 04:24:37 +00:00
|
|
|
* @var \Mockery\MockInterface
|
2018-03-03 03:26:42 +00:00
|
|
|
*/
|
|
|
|
private $repository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup test.
|
|
|
|
*/
|
2020-05-09 16:00:52 +00:00
|
|
|
public function setUp(): void
|
2018-03-03 03:26:42 +00:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
2020-06-24 04:24:37 +00:00
|
|
|
$this->powerRepository = m::mock(DaemonPowerRepository::class);
|
2018-03-03 03:26:42 +00:00
|
|
|
$this->repository = m::mock(ServerRepositoryInterface::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that an action can be sent to all servers.
|
|
|
|
*/
|
|
|
|
public function testSendAction()
|
|
|
|
{
|
2019-08-03 21:04:31 +00:00
|
|
|
/** @var \Pterodactyl\Models\Server[] $servers */
|
2018-03-03 03:26:42 +00:00
|
|
|
$servers = factory(Server::class)->times(2)->make();
|
|
|
|
|
2019-08-03 21:04:31 +00:00
|
|
|
foreach ($servers as &$server) {
|
|
|
|
$server->setRelation('node', factory(Node::class)->make());
|
|
|
|
}
|
|
|
|
|
2020-06-24 04:33:56 +00:00
|
|
|
$this->repository->expects('getServersForPowerActionCount')->with([], [])->andReturn(2);
|
|
|
|
$this->repository->expects('getServersForPowerAction')->with([], [])->andReturn($servers);
|
2018-03-03 03:26:42 +00:00
|
|
|
|
|
|
|
for ($i = 0; $i < count($servers); $i++) {
|
2020-06-24 04:33:56 +00:00
|
|
|
$this->powerRepository->expects('setNode->setServer->send')->with('kill')->andReturnNull();
|
2018-03-03 03:26:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$display = $this->runCommand($this->getCommand(), ['action' => 'kill'], ['yes']);
|
|
|
|
|
|
|
|
$this->assertNotEmpty($display);
|
2020-06-24 04:33:56 +00:00
|
|
|
$this->assertStringContainsString('2/2', $display);
|
|
|
|
$this->assertStringContainsString(trans('command/messages.server.power.confirm', ['action' => 'kill', 'count' => 2]), $display);
|
2018-03-03 03:26:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test filtering servers and nodes.
|
|
|
|
*/
|
|
|
|
public function testSendWithFilters()
|
|
|
|
{
|
|
|
|
$server = factory(Server::class)->make();
|
2019-08-03 21:04:31 +00:00
|
|
|
$server->setRelation('node', $node = factory(Node::class)->make());
|
2018-03-03 03:26:42 +00:00
|
|
|
|
2020-06-24 04:33:56 +00:00
|
|
|
$this->repository->expects('getServersForPowerActionCount')
|
2018-03-03 03:26:42 +00:00
|
|
|
->with([1, 2], [3, 4])
|
|
|
|
->andReturn(1);
|
|
|
|
|
2020-06-24 04:33:56 +00:00
|
|
|
$this->repository->expects('getServersForPowerAction')
|
2018-03-03 03:26:42 +00:00
|
|
|
->with([1, 2], [3, 4])
|
2020-06-24 04:33:56 +00:00
|
|
|
->andReturn(Collection::make([$server]));
|
2018-03-03 03:26:42 +00:00
|
|
|
|
2019-08-03 21:04:31 +00:00
|
|
|
$this->powerRepository->expects('setNode')->with($node)->andReturnSelf();
|
|
|
|
$this->powerRepository->expects('setServer')->with($server)->andReturnSelf();
|
2020-06-24 04:33:56 +00:00
|
|
|
$this->powerRepository->expects('send')->with('kill')->andReturn(new Response);
|
2018-03-03 03:26:42 +00:00
|
|
|
|
|
|
|
$display = $this->runCommand($this->getCommand(), [
|
|
|
|
'action' => 'kill',
|
|
|
|
'--servers' => '1,2',
|
|
|
|
'--nodes' => '3,4',
|
|
|
|
], ['yes']);
|
|
|
|
|
|
|
|
$this->assertNotEmpty($display);
|
2020-06-24 04:33:56 +00:00
|
|
|
$this->assertStringContainsString('1/1', $display);
|
|
|
|
$this->assertStringContainsString(trans('command/messages.server.power.confirm', ['action' => 'kill', 'count' => 1]), $display);
|
2018-03-03 03:26:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that sending empty options returns the expected results.
|
|
|
|
*/
|
|
|
|
public function testSendWithEmptyOptions()
|
|
|
|
{
|
|
|
|
$server = factory(Server::class)->make();
|
2019-08-03 21:04:31 +00:00
|
|
|
$server->setRelation('node', factory(Node::class)->make());
|
2018-03-03 03:26:42 +00:00
|
|
|
|
2020-06-24 04:33:56 +00:00
|
|
|
$this->repository->expects('getServersForPowerActionCount')
|
2018-03-03 03:26:42 +00:00
|
|
|
->with([], [])
|
|
|
|
->andReturn(1);
|
|
|
|
|
2020-06-24 04:33:56 +00:00
|
|
|
$this->repository->expects('getServersForPowerAction')->with([], [])->andReturn(Collection::make([$server]));
|
|
|
|
$this->powerRepository->expects('setNode->setServer->send')->with('kill')->andReturnNull();
|
2018-03-03 03:26:42 +00:00
|
|
|
|
|
|
|
$display = $this->runCommand($this->getCommand(), [
|
|
|
|
'action' => 'kill',
|
|
|
|
'--servers' => '',
|
|
|
|
'--nodes' => '',
|
|
|
|
], ['yes']);
|
|
|
|
|
|
|
|
$this->assertNotEmpty($display);
|
2020-06-24 04:33:56 +00:00
|
|
|
$this->assertStringContainsString('1/1', $display);
|
|
|
|
$this->assertStringContainsString(trans('command/messages.server.power.confirm', ['action' => 'kill', 'count' => 1]), $display);
|
2018-03-03 03:26:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-05-13 15:05:52 +00:00
|
|
|
* Test that validation occurs correctly.
|
2018-03-03 03:26:42 +00:00
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
*
|
|
|
|
* @dataProvider validationFailureDataProvider
|
|
|
|
*/
|
|
|
|
public function testValidationErrors(array $data)
|
|
|
|
{
|
2020-06-24 04:33:56 +00:00
|
|
|
$this->expectException(ValidationException::class);
|
2018-03-03 03:26:42 +00:00
|
|
|
$this->runCommand($this->getCommand(), $data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Provide invalid data for the command.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function validationFailureDataProvider(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
[['action' => 'hodor']],
|
|
|
|
[['action' => 'hodor', '--servers' => 'hodor']],
|
|
|
|
[['action' => 'kill', '--servers' => 'hodor']],
|
|
|
|
[['action' => 'kill', '--servers' => '1,2,3', '--nodes' => 'hodor']],
|
|
|
|
[['action' => 'kill', '--servers' => '1,2,3', '--nodes' => '1,2,test']],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return an instance of the command with mocked dependencies.
|
|
|
|
*
|
|
|
|
* @return \Pterodactyl\Console\Commands\Server\BulkPowerActionCommand
|
|
|
|
*/
|
|
|
|
private function getCommand(): BulkPowerActionCommand
|
|
|
|
{
|
|
|
|
return new BulkPowerActionCommand($this->powerRepository, $this->repository, $this->app->make(Factory::class));
|
|
|
|
}
|
|
|
|
}
|