Files
panel-pelican-dev/tests/Integration/Jobs/Schedule/RunTaskJobTest.php

174 lines
5.7 KiB
PHP
Raw Normal View History

2021-05-01 12:24:42 -07:00
<?php
2024-03-12 22:39:16 -04:00
namespace App\Tests\Integration\Jobs\Schedule;
2021-05-01 12:24:42 -07:00
2024-04-18 03:50:20 -04:00
use App\Enums\ServerState;
use Carbon\Carbon;
2021-05-01 12:24:42 -07:00
use Carbon\CarbonImmutable;
2024-03-12 22:39:16 -04:00
use App\Models\Task;
use App\Models\Server;
use App\Models\Schedule;
2021-05-01 12:24:42 -07:00
use Illuminate\Support\Facades\Bus;
2024-03-12 22:39:16 -04:00
use App\Jobs\Schedule\RunTaskJob;
use App\Tests\Integration\IntegrationTestCase;
use App\Repositories\Daemon\DaemonPowerRepository;
use Illuminate\Http\Client\ConnectionException;
use PHPUnit\Framework\Attributes\DataProvider;
2021-05-01 12:24:42 -07:00
class RunTaskJobTest extends IntegrationTestCase
{
/**
* An inactive job should not be run by the system.
*/
public function test_inactive_job_is_not_run(): void
2021-05-01 12:24:42 -07:00
{
$server = $this->createServerModel();
2024-03-12 22:39:16 -04:00
/** @var \App\Models\Schedule $schedule */
2021-05-01 12:24:42 -07:00
$schedule = Schedule::factory()->create([
'server_id' => $server->id,
'is_processing' => true,
'last_run_at' => null,
'is_active' => false,
]);
2024-03-12 22:39:16 -04:00
/** @var \App\Models\Task $task */
2021-05-01 12:24:42 -07:00
$task = Task::factory()->create(['schedule_id' => $schedule->id, 'is_queued' => true]);
$job = new RunTaskJob($task);
2023-02-23 12:30:16 -07:00
Bus::dispatchSync($job);
2021-05-01 12:24:42 -07:00
$task->refresh();
$schedule->refresh();
$this->assertFalse($task->is_queued);
$this->assertFalse($schedule->is_processing);
$this->assertFalse($schedule->is_active);
2023-02-23 12:30:16 -07:00
$this->assertTrue(CarbonImmutable::now()->isSameAs(\DateTimeInterface::ATOM, $schedule->last_run_at));
2021-05-01 12:24:42 -07:00
}
public function test_job_with_invalid_action_throws_exception(): void
2021-05-01 12:24:42 -07:00
{
$server = $this->createServerModel();
2024-03-12 22:39:16 -04:00
/** @var \App\Models\Schedule $schedule */
2021-05-01 12:24:42 -07:00
$schedule = Schedule::factory()->create(['server_id' => $server->id]);
2024-03-12 22:39:16 -04:00
/** @var \App\Models\Task $task */
2021-05-01 12:24:42 -07:00
$task = Task::factory()->create(['schedule_id' => $schedule->id, 'action' => 'foobar']);
$job = new RunTaskJob($task);
2023-02-23 12:30:16 -07:00
$this->expectException(\InvalidArgumentException::class);
2021-05-01 12:24:42 -07:00
$this->expectExceptionMessage('Invalid task action provided: foobar');
2023-02-23 12:30:16 -07:00
Bus::dispatchSync($job);
2021-05-01 12:24:42 -07:00
}
#[DataProvider('isManualRunDataProvider')]
public function test_job_is_executed(bool $isManualRun): void
2021-05-01 12:24:42 -07:00
{
$server = $this->createServerModel();
2024-03-12 22:39:16 -04:00
/** @var \App\Models\Schedule $schedule */
2021-05-01 12:24:42 -07:00
$schedule = Schedule::factory()->create([
'server_id' => $server->id,
'is_active' => !$isManualRun,
'is_processing' => true,
'last_run_at' => null,
]);
2024-03-12 22:39:16 -04:00
/** @var \App\Models\Task $task */
2021-05-01 12:24:42 -07:00
$task = Task::factory()->create([
'schedule_id' => $schedule->id,
'action' => Task::ACTION_POWER,
'payload' => 'start',
'is_queued' => true,
'continue_on_failure' => false,
]);
2023-02-23 12:30:16 -07:00
$mock = \Mockery::mock(DaemonPowerRepository::class);
2021-05-01 12:24:42 -07:00
$this->instance(DaemonPowerRepository::class, $mock);
2023-02-23 12:30:16 -07:00
$mock->expects('setServer')->with(\Mockery::on(function ($value) use ($server) {
2021-05-01 12:24:42 -07:00
return $value instanceof Server && $value->id === $server->id;
}))->andReturnSelf();
2024-03-23 16:10:47 -04:00
$mock->expects('send')->with('start');
2021-05-01 12:24:42 -07:00
2023-02-23 12:30:16 -07:00
Bus::dispatchSync(new RunTaskJob($task, $isManualRun));
2021-05-01 12:24:42 -07:00
$task->refresh();
$schedule->refresh();
$this->assertFalse($task->is_queued);
$this->assertFalse($schedule->is_processing);
2023-02-23 12:30:16 -07:00
$this->assertTrue(CarbonImmutable::now()->isSameAs(\DateTimeInterface::ATOM, $schedule->last_run_at));
2021-05-01 12:24:42 -07:00
}
#[DataProvider('isManualRunDataProvider')]
public function test_exception_during_run_is_handled_correctly(bool $continueOnFailure): void
2021-05-01 12:24:42 -07:00
{
$server = $this->createServerModel();
2024-03-12 22:39:16 -04:00
/** @var \App\Models\Schedule $schedule */
2021-05-01 12:24:42 -07:00
$schedule = Schedule::factory()->create(['server_id' => $server->id]);
2024-03-12 22:39:16 -04:00
/** @var \App\Models\Task $task */
2021-05-01 12:24:42 -07:00
$task = Task::factory()->create([
'schedule_id' => $schedule->id,
'action' => Task::ACTION_POWER,
'payload' => 'start',
'continue_on_failure' => $continueOnFailure,
]);
2023-02-23 12:30:16 -07:00
$mock = \Mockery::mock(DaemonPowerRepository::class);
2021-05-01 12:24:42 -07:00
$this->instance(DaemonPowerRepository::class, $mock);
$mock->expects('setServer->send')->andThrow(new ConnectionException());
2021-05-01 12:24:42 -07:00
if (!$continueOnFailure) {
$this->expectException(ConnectionException::class);
2021-05-01 12:24:42 -07:00
}
2023-02-23 12:30:16 -07:00
Bus::dispatchSync(new RunTaskJob($task));
2021-05-01 12:24:42 -07:00
if ($continueOnFailure) {
$task->refresh();
$schedule->refresh();
$this->assertFalse($task->is_queued);
$this->assertFalse($schedule->is_processing);
2023-02-23 12:30:16 -07:00
$this->assertTrue(CarbonImmutable::now()->isSameAs(\DateTimeInterface::ATOM, $schedule->last_run_at));
2021-05-01 12:24:42 -07:00
}
}
/**
* Test that a schedule is not executed if the server is suspended.
*/
public function test_task_is_not_run_if_server_is_suspended(): void
{
$server = $this->createServerModel([
2024-04-18 03:50:20 -04:00
'status' => ServerState::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',
]);
2023-02-23 12:30:16 -07:00
Bus::dispatchSync(new RunTaskJob($task));
$task->refresh();
$schedule->refresh();
$this->assertFalse($task->is_queued);
$this->assertFalse($schedule->is_processing);
2023-02-23 12:30:16 -07:00
$this->assertTrue(Carbon::now()->isSameAs(\DateTimeInterface::ATOM, $schedule->last_run_at));
}
2023-02-23 12:30:16 -07:00
public static function isManualRunDataProvider(): array
2021-05-01 12:24:42 -07:00
{
return [[true], [false]];
}
}