Fix job task runner test

This commit is contained in:
Dane Everitt 2020-06-23 22:10:54 -07:00
parent 536180ed0c
commit 34916e7caf
No known key found for this signature in database
GPG Key ID: EEA66103B3D71F53
2 changed files with 50 additions and 56 deletions

File diff suppressed because one or more lines are too long

View File

@ -3,53 +3,51 @@
namespace Tests\Unit\Jobs\Schedule; namespace Tests\Unit\Jobs\Schedule;
use Mockery as m; use Mockery as m;
use Carbon\Carbon;
use Tests\TestCase; use Tests\TestCase;
use Cake\Chronos\Chronos; use Cake\Chronos\Chronos;
use Pterodactyl\Models\Task; use Pterodactyl\Models\Task;
use Pterodactyl\Models\User; use Pterodactyl\Models\User;
use GuzzleHttp\Psr7\Response; use GuzzleHttp\Psr7\Response;
use InvalidArgumentException;
use Pterodactyl\Models\Server; use Pterodactyl\Models\Server;
use Pterodactyl\Models\Schedule; use Pterodactyl\Models\Schedule;
use Illuminate\Support\Facades\Bus; use Illuminate\Support\Facades\Bus;
use Pterodactyl\Jobs\Schedule\RunTaskJob; use Pterodactyl\Jobs\Schedule\RunTaskJob;
use Illuminate\Contracts\Config\Repository; use Pterodactyl\Repositories\Eloquent\TaskRepository;
use Pterodactyl\Services\Backups\InitiateBackupService;
use Pterodactyl\Repositories\Eloquent\ScheduleRepository;
use Pterodactyl\Repositories\Wings\DaemonPowerRepository;
use Pterodactyl\Repositories\Wings\DaemonCommandRepository;
use Pterodactyl\Contracts\Repository\TaskRepositoryInterface; use Pterodactyl\Contracts\Repository\TaskRepositoryInterface;
use Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService;
use Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface; use Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface;
use Pterodactyl\Contracts\Repository\Daemon\PowerRepositoryInterface;
use Pterodactyl\Contracts\Repository\Daemon\CommandRepositoryInterface;
class RunTaskJobTest extends TestCase class RunTaskJobTest extends TestCase
{ {
/** /**
* @var \Pterodactyl\Contracts\Repository\Daemon\CommandRepositoryInterface|\Mockery\Mock * @var \Mockery\MockInterface
*/ */
protected $commandRepository; private $commandRepository;
/** /**
* @var \Illuminate\Contracts\Config\Repository|\Mockery\Mock * @var \Mockery\MockInterface
*/ */
protected $config; private $powerRepository;
/** /**
* @var \Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService|\Mockery\Mock * @var \Mockery\MockInterface
*/ */
protected $keyProviderService; private $initiateBackupService;
/** /**
* @var \Pterodactyl\Contracts\Repository\Daemon\PowerRepositoryInterface|\Mockery\Mock * @var \Mockery\MockInterface
*/ */
protected $powerRepository; private $taskRepository;
/** /**
* @var \Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface|\Mockery\Mock * @var \Mockery\MockInterface
*/ */
protected $scheduleRepository; private $scheduleRepository;
/**
* @var \Pterodactyl\Contracts\Repository\TaskRepositoryInterface|\Mockery\Mock
*/
protected $taskRepository;
/** /**
* Setup tests. * Setup tests.
@ -57,17 +55,16 @@ class RunTaskJobTest extends TestCase
public function setUp(): void public function setUp(): void
{ {
parent::setUp(); parent::setUp();
Bus::fake(); Bus::fake();
Chronos::setTestNow(Chronos::now()); Carbon::setTestNow(Carbon::now());
$this->commandRepository = m::mock(CommandRepositoryInterface::class); $this->commandRepository = m::mock(DaemonCommandRepository::class);
$this->config = m::mock(Repository::class); $this->powerRepository = m::mock(DaemonPowerRepository::class);
$this->keyProviderService = m::mock(DaemonKeyProviderService::class); $this->taskRepository = m::mock(TaskRepository::class);
$this->powerRepository = m::mock(PowerRepositoryInterface::class); $this->initiateBackupService = m::mock(InitiateBackupService::class);
$this->scheduleRepository = m::mock(ScheduleRepositoryInterface::class); $this->scheduleRepository = m::mock(ScheduleRepository::class);
$this->taskRepository = m::mock(TaskRepositoryInterface::class);
$this->app->instance(Repository::class, $this->config);
$this->app->instance(TaskRepositoryInterface::class, $this->taskRepository); $this->app->instance(TaskRepositoryInterface::class, $this->taskRepository);
$this->app->instance(ScheduleRepositoryInterface::class, $this->scheduleRepository); $this->app->instance(ScheduleRepositoryInterface::class, $this->scheduleRepository);
} }
@ -77,17 +74,20 @@ class RunTaskJobTest extends TestCase
*/ */
public function testPowerAction() public function testPowerAction()
{ {
$schedule = factory(Schedule::class)->make(); /** @var \Pterodactyl\Models\Schedule $schedule */
$schedule = factory(Schedule::class)->make(['is_active' => true]);
/** @var \Pterodactyl\Models\Task $task */
$task = factory(Task::class)->make(['action' => 'power', 'sequence_id' => 1]); $task = factory(Task::class)->make(['action' => 'power', 'sequence_id' => 1]);
/* @var \Pterodactyl\Models\Server $server */
$task->setRelation('server', $server = factory(Server::class)->make()); $task->setRelation('server', $server = factory(Server::class)->make());
$task->setRelation('schedule', $schedule); $task->setRelation('schedule', $schedule);
$server->setRelation('user', factory(User::class)->make()); $server->setRelation('user', factory(User::class)->make());
$this->taskRepository->shouldReceive('getTaskForJobProcess')->with($task->id)->once()->andReturn($task); $this->taskRepository->expects('getTaskForJobProcess')->with($task->id)->andReturn($task);
$this->keyProviderService->shouldReceive('handle')->with($server, $server->user)->once()->andReturn('123456'); $this->powerRepository->expects('setServer')->with($task->server)->andReturnSelf()
$this->powerRepository->shouldReceive('setServer')->with($task->server)->once()->andReturnSelf() ->getMock()->expects('send')->with($task->payload)->andReturn(new Response);
->shouldReceive('setToken')->with('123456')->once()->andReturnSelf()
->shouldReceive('sendSignal')->with($task->payload)->once()->andReturn(new Response);
$this->taskRepository->shouldReceive('update')->with($task->id, ['is_queued' => false])->once()->andReturnNull(); $this->taskRepository->shouldReceive('update')->with($task->id, ['is_queued' => false])->once()->andReturnNull();
$this->taskRepository->shouldReceive('getNextTask')->with($schedule->id, $task->sequence_id)->once()->andReturnNull(); $this->taskRepository->shouldReceive('getNextTask')->with($schedule->id, $task->sequence_id)->once()->andReturnNull();
@ -113,14 +113,12 @@ class RunTaskJobTest extends TestCase
$task->setRelation('schedule', $schedule); $task->setRelation('schedule', $schedule);
$server->setRelation('user', factory(User::class)->make()); $server->setRelation('user', factory(User::class)->make());
$this->taskRepository->shouldReceive('getTaskForJobProcess')->with($task->id)->once()->andReturn($task); $this->taskRepository->expects('getTaskForJobProcess')->with($task->id)->andReturn($task);
$this->keyProviderService->shouldReceive('handle')->with($server, $server->user)->once()->andReturn('123456'); $this->commandRepository->expects('setServer')->with($task->server)->andReturnSelf()
$this->commandRepository->shouldReceive('setServer')->with($task->server)->once()->andReturnSelf() ->getMock()->expects('send')->with($task->payload)->andReturn(new Response);
->shouldReceive('setToken')->with('123456')->once()->andReturnSelf()
->shouldReceive('send')->with($task->payload)->once()->andReturn(new Response);
$this->taskRepository->shouldReceive('update')->with($task->id, ['is_queued' => false])->once()->andReturnNull(); $this->taskRepository->expects('update')->with($task->id, ['is_queued' => false])->andReturnNull();
$this->taskRepository->shouldReceive('getNextTask')->with($schedule->id, $task->sequence_id)->once()->andReturnNull(); $this->taskRepository->expects('getNextTask')->with($schedule->id, $task->sequence_id)->andReturnNull();
$this->scheduleRepository->shouldReceive('withoutFreshModel->update')->with($schedule->id, [ $this->scheduleRepository->shouldReceive('withoutFreshModel->update')->with($schedule->id, [
'is_processing' => false, 'is_processing' => false,
@ -143,19 +141,17 @@ class RunTaskJobTest extends TestCase
$task->setRelation('schedule', $schedule); $task->setRelation('schedule', $schedule);
$server->setRelation('user', factory(User::class)->make()); $server->setRelation('user', factory(User::class)->make());
$this->taskRepository->shouldReceive('getTaskForJobProcess')->with($task->id)->once()->andReturn($task); $this->taskRepository->expects('getTaskForJobProcess')->with($task->id)->andReturn($task);
$this->keyProviderService->shouldReceive('handle')->with($server, $server->user)->once()->andReturn('123456'); $this->commandRepository->expects('setServer')->with($task->server)->andReturnSelf()
$this->commandRepository->shouldReceive('setServer')->with($task->server)->once()->andReturnSelf() ->getMock()->expects('send')->with($task->payload)->andReturn(new Response);
->shouldReceive('setToken')->with('123456')->once()->andReturnSelf()
->shouldReceive('send')->with($task->payload)->once()->andReturn(new Response);
$this->taskRepository->shouldReceive('update')->with($task->id, ['is_queued' => false])->once()->andReturnNull(); $this->taskRepository->shouldReceive('update')->with($task->id, ['is_queued' => false])->once()->andReturnNull();
$nextTask = factory(Task::class)->make(); $nextTask = factory(Task::class)->make();
$this->taskRepository->shouldReceive('getNextTask')->with($schedule->id, $task->sequence_id)->once()->andReturn($nextTask); $this->taskRepository->expects('getNextTask')->with($schedule->id, $task->sequence_id)->andReturn($nextTask);
$this->taskRepository->shouldReceive('update')->with($nextTask->id, [ $this->taskRepository->expects('update')->with($nextTask->id, [
'is_queued' => true, 'is_queued' => true,
])->once()->andReturnNull(); ])->andReturnNull();
$this->getJobInstance($task->id, $schedule->id); $this->getJobInstance($task->id, $schedule->id);
@ -170,19 +166,19 @@ class RunTaskJobTest extends TestCase
/** /**
* Test that an exception is thrown if an invalid task action is supplied. * Test that an exception is thrown if an invalid task action is supplied.
*
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Cannot run a task that points to a non-existent action.
*/ */
public function testInvalidActionPassedToJob() public function testInvalidActionPassedToJob()
{ {
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Cannot run a task that points to a non-existent action.');
$schedule = factory(Schedule::class)->make(); $schedule = factory(Schedule::class)->make();
$task = factory(Task::class)->make(['action' => 'invalid', 'sequence_id' => 1]); $task = factory(Task::class)->make(['action' => 'invalid', 'sequence_id' => 1]);
$task->setRelation('server', $server = factory(Server::class)->make()); $task->setRelation('server', $server = factory(Server::class)->make());
$task->setRelation('schedule', $schedule); $task->setRelation('schedule', $schedule);
$server->setRelation('user', factory(User::class)->make()); $server->setRelation('user', factory(User::class)->make());
$this->taskRepository->shouldReceive('getTaskForJobProcess')->with($task->id)->once()->andReturn($task); $this->taskRepository->expects('getTaskForJobProcess')->with($task->id)->andReturn($task);
$this->getJobInstance($task->id, 1234); $this->getJobInstance($task->id, 1234);
} }
@ -218,14 +214,12 @@ class RunTaskJobTest extends TestCase
* @param int $schedule * @param int $schedule
* *
* @throws \Pterodactyl\Exceptions\Model\DataValidationException * @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Repository\Daemon\InvalidPowerSignalException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/ */
private function getJobInstance($task, $schedule) private function getJobInstance($task, $schedule)
{ {
return (new RunTaskJob($task, $schedule))->handle( return (new RunTaskJob($task, $schedule))->handle(
$this->commandRepository, $this->commandRepository,
$this->keyProviderService, $this->initiateBackupService,
$this->powerRepository, $this->powerRepository,
$this->taskRepository $this->taskRepository
); );