2021-05-01 19:24:42 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Tests\Integration\Jobs\Schedule;
|
|
|
|
|
2022-05-05 00:41:53 +00:00
|
|
|
use Carbon\Carbon;
|
2021-05-01 19:24:42 +00:00
|
|
|
use Carbon\CarbonImmutable;
|
|
|
|
use GuzzleHttp\Psr7\Request;
|
2021-08-16 00:20:36 +00:00
|
|
|
use Pterodactyl\Models\Task;
|
2021-05-01 19:24:42 +00:00
|
|
|
use GuzzleHttp\Psr7\Response;
|
|
|
|
use Pterodactyl\Models\Server;
|
|
|
|
use Pterodactyl\Models\Schedule;
|
|
|
|
use Illuminate\Support\Facades\Bus;
|
|
|
|
use Pterodactyl\Jobs\Schedule\RunTaskJob;
|
|
|
|
use GuzzleHttp\Exception\BadResponseException;
|
|
|
|
use Pterodactyl\Tests\Integration\IntegrationTestCase;
|
|
|
|
use Pterodactyl\Repositories\Wings\DaemonPowerRepository;
|
|
|
|
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
|
|
|
|
|
|
|
|
class RunTaskJobTest extends IntegrationTestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* An inactive job should not be run by the system.
|
|
|
|
*/
|
|
|
|
public function testInactiveJobIsNotRun()
|
|
|
|
{
|
|
|
|
$server = $this->createServerModel();
|
|
|
|
|
|
|
|
/** @var \Pterodactyl\Models\Schedule $schedule */
|
|
|
|
$schedule = Schedule::factory()->create([
|
|
|
|
'server_id' => $server->id,
|
|
|
|
'is_processing' => true,
|
|
|
|
'last_run_at' => null,
|
|
|
|
'is_active' => false,
|
|
|
|
]);
|
|
|
|
/** @var \Pterodactyl\Models\Task $task */
|
|
|
|
$task = Task::factory()->create(['schedule_id' => $schedule->id, 'is_queued' => true]);
|
|
|
|
|
|
|
|
$job = new RunTaskJob($task);
|
|
|
|
|
|
|
|
Bus::dispatchNow($job);
|
|
|
|
|
|
|
|
$task->refresh();
|
|
|
|
$schedule->refresh();
|
|
|
|
|
|
|
|
$this->assertFalse($task->is_queued);
|
|
|
|
$this->assertFalse($schedule->is_processing);
|
|
|
|
$this->assertFalse($schedule->is_active);
|
2022-11-29 17:53:59 +00:00
|
|
|
$this->assertTrue(CarbonImmutable::now()->isSameAs(\DateTimeInterface::ATOM, $schedule->last_run_at));
|
2021-05-01 19:24:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testJobWithInvalidActionThrowsException()
|
|
|
|
{
|
|
|
|
$server = $this->createServerModel();
|
|
|
|
|
|
|
|
/** @var \Pterodactyl\Models\Schedule $schedule */
|
|
|
|
$schedule = Schedule::factory()->create(['server_id' => $server->id]);
|
|
|
|
/** @var \Pterodactyl\Models\Task $task */
|
|
|
|
$task = Task::factory()->create(['schedule_id' => $schedule->id, 'action' => 'foobar']);
|
|
|
|
|
|
|
|
$job = new RunTaskJob($task);
|
|
|
|
|
2022-11-29 17:53:59 +00:00
|
|
|
$this->expectException(\InvalidArgumentException::class);
|
2021-05-01 19:24:42 +00:00
|
|
|
$this->expectExceptionMessage('Invalid task action provided: foobar');
|
|
|
|
Bus::dispatchNow($job);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider isManualRunDataProvider
|
|
|
|
*/
|
|
|
|
public function testJobIsExecuted(bool $isManualRun)
|
|
|
|
{
|
|
|
|
$server = $this->createServerModel();
|
|
|
|
|
|
|
|
/** @var \Pterodactyl\Models\Schedule $schedule */
|
|
|
|
$schedule = Schedule::factory()->create([
|
|
|
|
'server_id' => $server->id,
|
|
|
|
'is_active' => !$isManualRun,
|
|
|
|
'is_processing' => true,
|
|
|
|
'last_run_at' => null,
|
|
|
|
]);
|
|
|
|
/** @var \Pterodactyl\Models\Task $task */
|
|
|
|
$task = Task::factory()->create([
|
|
|
|
'schedule_id' => $schedule->id,
|
|
|
|
'action' => Task::ACTION_POWER,
|
|
|
|
'payload' => 'start',
|
|
|
|
'is_queued' => true,
|
|
|
|
'continue_on_failure' => false,
|
|
|
|
]);
|
|
|
|
|
2022-11-29 17:53:59 +00:00
|
|
|
$mock = \Mockery::mock(DaemonPowerRepository::class);
|
2021-05-01 19:24:42 +00:00
|
|
|
$this->instance(DaemonPowerRepository::class, $mock);
|
|
|
|
|
2022-11-29 17:53:59 +00:00
|
|
|
$mock->expects('setServer')->with(\Mockery::on(function ($value) use ($server) {
|
2021-05-01 19:24:42 +00:00
|
|
|
return $value instanceof Server && $value->id === $server->id;
|
|
|
|
}))->andReturnSelf();
|
|
|
|
$mock->expects('send')->with('start')->andReturn(new Response());
|
|
|
|
|
|
|
|
Bus::dispatchNow(new RunTaskJob($task, $isManualRun));
|
|
|
|
|
|
|
|
$task->refresh();
|
|
|
|
$schedule->refresh();
|
|
|
|
|
|
|
|
$this->assertFalse($task->is_queued);
|
|
|
|
$this->assertFalse($schedule->is_processing);
|
2022-11-29 17:53:59 +00:00
|
|
|
$this->assertTrue(CarbonImmutable::now()->isSameAs(\DateTimeInterface::ATOM, $schedule->last_run_at));
|
2021-05-01 19:24:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider isManualRunDataProvider
|
|
|
|
*/
|
|
|
|
public function testExceptionDuringRunIsHandledCorrectly(bool $continueOnFailure)
|
|
|
|
{
|
|
|
|
$server = $this->createServerModel();
|
|
|
|
|
|
|
|
/** @var \Pterodactyl\Models\Schedule $schedule */
|
|
|
|
$schedule = Schedule::factory()->create(['server_id' => $server->id]);
|
|
|
|
/** @var \Pterodactyl\Models\Task $task */
|
|
|
|
$task = Task::factory()->create([
|
|
|
|
'schedule_id' => $schedule->id,
|
|
|
|
'action' => Task::ACTION_POWER,
|
|
|
|
'payload' => 'start',
|
|
|
|
'continue_on_failure' => $continueOnFailure,
|
|
|
|
]);
|
|
|
|
|
2022-11-29 17:53:59 +00:00
|
|
|
$mock = \Mockery::mock(DaemonPowerRepository::class);
|
2021-05-01 19:24:42 +00:00
|
|
|
$this->instance(DaemonPowerRepository::class, $mock);
|
|
|
|
|
|
|
|
$mock->expects('setServer->send')->andThrow(
|
|
|
|
new DaemonConnectionException(new BadResponseException('Bad request', new Request('GET', '/test'), new Response()))
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!$continueOnFailure) {
|
|
|
|
$this->expectException(DaemonConnectionException::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
Bus::dispatchNow(new RunTaskJob($task));
|
|
|
|
|
|
|
|
if ($continueOnFailure) {
|
|
|
|
$task->refresh();
|
|
|
|
$schedule->refresh();
|
|
|
|
|
|
|
|
$this->assertFalse($task->is_queued);
|
|
|
|
$this->assertFalse($schedule->is_processing);
|
2022-11-29 17:53:59 +00:00
|
|
|
$this->assertTrue(CarbonImmutable::now()->isSameAs(\DateTimeInterface::ATOM, $schedule->last_run_at));
|
2021-05-01 19:24:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-05 00:41:53 +00:00
|
|
|
/**
|
|
|
|
* Test that a schedule is not executed if the server is suspended.
|
|
|
|
*
|
|
|
|
* @see https://github.com/pterodactyl/panel/issues/4008
|
|
|
|
*/
|
|
|
|
public function testTaskIsNotRunIfServerIsSuspended()
|
|
|
|
{
|
|
|
|
$server = $this->createServerModel([
|
|
|
|
'status' => Server::STATUS_SUSPENDED,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$schedule = Schedule::factory()->for($server)->create([
|
|
|
|
'last_run_at' => Carbon::now()->subHour(),
|
|
|
|
]);
|
|
|
|
|
|
|
|
$task = Task::factory()->for($schedule)->create([
|
|
|
|
'action' => Task::ACTION_POWER,
|
|
|
|
'payload' => 'start',
|
|
|
|
]);
|
|
|
|
|
|
|
|
Bus::dispatchNow(new RunTaskJob($task));
|
|
|
|
|
|
|
|
$task->refresh();
|
|
|
|
$schedule->refresh();
|
|
|
|
|
|
|
|
$this->assertFalse($task->is_queued);
|
|
|
|
$this->assertFalse($schedule->is_processing);
|
2022-11-29 17:53:59 +00:00
|
|
|
$this->assertTrue(Carbon::now()->isSameAs(\DateTimeInterface::ATOM, $schedule->last_run_at));
|
2022-05-05 00:41:53 +00:00
|
|
|
}
|
|
|
|
|
2022-10-14 16:59:20 +00:00
|
|
|
public function isManualRunDataProvider(): array
|
2021-05-01 19:24:42 +00:00
|
|
|
{
|
|
|
|
return [[true], [false]];
|
|
|
|
}
|
|
|
|
}
|