Skip to content

Commit 5f48712

Browse files
committed
Add test coverage for RunTaskJob
1 parent 8ab3ad3 commit 5f48712

File tree

2 files changed

+158
-1
lines changed

2 files changed

+158
-1
lines changed

app/Jobs/Schedule/RunTaskJob.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function handle(
7474
$backupService->setIgnoredFiles(explode(PHP_EOL, $this->task->payload))->handle($server, null, true);
7575
break;
7676
default:
77-
throw new InvalidArgumentException('Cannot run a task that points to a non-existent action.');
77+
throw new InvalidArgumentException('Invalid task action provided: ' . $this->task->action);
7878
}
7979
} catch (Exception $exception) {
8080
// If this isn't a DaemonConnectionException on a task that allows for failures
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
<?php
2+
3+
namespace Pterodactyl\Tests\Integration\Jobs\Schedule;
4+
5+
use Mockery;
6+
use Exception;
7+
use Carbon\CarbonImmutable;
8+
use Pterodactyl\Models\Task;
9+
use GuzzleHttp\Psr7\Request;
10+
use InvalidArgumentException;
11+
use GuzzleHttp\Psr7\Response;
12+
use Pterodactyl\Models\Server;
13+
use Pterodactyl\Models\Schedule;
14+
use Illuminate\Support\Facades\Bus;
15+
use Pterodactyl\Jobs\Schedule\RunTaskJob;
16+
use GuzzleHttp\Exception\BadResponseException;
17+
use Pterodactyl\Tests\Integration\IntegrationTestCase;
18+
use Pterodactyl\Repositories\Wings\DaemonPowerRepository;
19+
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
20+
21+
class RunTaskJobTest extends IntegrationTestCase
22+
{
23+
/**
24+
* An inactive job should not be run by the system.
25+
*/
26+
public function testInactiveJobIsNotRun()
27+
{
28+
$server = $this->createServerModel();
29+
30+
/** @var \Pterodactyl\Models\Schedule $schedule */
31+
$schedule = Schedule::factory()->create([
32+
'server_id' => $server->id,
33+
'is_processing' => true,
34+
'last_run_at' => null,
35+
'is_active' => false,
36+
]);
37+
/** @var \Pterodactyl\Models\Task $task */
38+
$task = Task::factory()->create(['schedule_id' => $schedule->id, 'is_queued' => true]);
39+
40+
$job = new RunTaskJob($task);
41+
42+
Bus::dispatchNow($job);
43+
44+
$task->refresh();
45+
$schedule->refresh();
46+
47+
$this->assertFalse($task->is_queued);
48+
$this->assertFalse($schedule->is_processing);
49+
$this->assertFalse($schedule->is_active);
50+
$this->assertTrue(CarbonImmutable::now()->isSameAs(CarbonImmutable::ISO8601, $schedule->last_run_at));
51+
}
52+
53+
public function testJobWithInvalidActionThrowsException()
54+
{
55+
$server = $this->createServerModel();
56+
57+
/** @var \Pterodactyl\Models\Schedule $schedule */
58+
$schedule = Schedule::factory()->create(['server_id' => $server->id]);
59+
/** @var \Pterodactyl\Models\Task $task */
60+
$task = Task::factory()->create(['schedule_id' => $schedule->id, 'action' => 'foobar']);
61+
62+
$job = new RunTaskJob($task);
63+
64+
$this->expectException(InvalidArgumentException::class);
65+
$this->expectExceptionMessage('Invalid task action provided: foobar');
66+
Bus::dispatchNow($job);
67+
}
68+
69+
/**
70+
* @dataProvider isManualRunDataProvider
71+
*/
72+
public function testJobIsExecuted(bool $isManualRun)
73+
{
74+
$server = $this->createServerModel();
75+
76+
/** @var \Pterodactyl\Models\Schedule $schedule */
77+
$schedule = Schedule::factory()->create([
78+
'server_id' => $server->id,
79+
'is_active' => !$isManualRun,
80+
'is_processing' => true,
81+
'last_run_at' => null,
82+
]);
83+
/** @var \Pterodactyl\Models\Task $task */
84+
$task = Task::factory()->create([
85+
'schedule_id' => $schedule->id,
86+
'action' => Task::ACTION_POWER,
87+
'payload' => 'start',
88+
'is_queued' => true,
89+
'continue_on_failure' => false,
90+
]);
91+
92+
$mock = Mockery::mock(DaemonPowerRepository::class);
93+
$this->instance(DaemonPowerRepository::class, $mock);
94+
95+
$mock->expects('setServer')->with(Mockery::on(function ($value) use ($server) {
96+
return $value instanceof Server && $value->id === $server->id;
97+
}))->andReturnSelf();
98+
$mock->expects('send')->with('start')->andReturn(new Response());
99+
100+
Bus::dispatchNow(new RunTaskJob($task, $isManualRun));
101+
102+
$task->refresh();
103+
$schedule->refresh();
104+
105+
$this->assertFalse($task->is_queued);
106+
$this->assertFalse($schedule->is_processing);
107+
$this->assertTrue(CarbonImmutable::now()->isSameAs(CarbonImmutable::ISO8601, $schedule->last_run_at));
108+
}
109+
110+
/**
111+
* @dataProvider isManualRunDataProvider
112+
*/
113+
public function testExceptionDuringRunIsHandledCorrectly(bool $continueOnFailure)
114+
{
115+
$server = $this->createServerModel();
116+
117+
/** @var \Pterodactyl\Models\Schedule $schedule */
118+
$schedule = Schedule::factory()->create(['server_id' => $server->id]);
119+
/** @var \Pterodactyl\Models\Task $task */
120+
$task = Task::factory()->create([
121+
'schedule_id' => $schedule->id,
122+
'action' => Task::ACTION_POWER,
123+
'payload' => 'start',
124+
'continue_on_failure' => $continueOnFailure,
125+
]);
126+
127+
$mock = Mockery::mock(DaemonPowerRepository::class);
128+
$this->instance(DaemonPowerRepository::class, $mock);
129+
130+
$mock->expects('setServer->send')->andThrow(
131+
new DaemonConnectionException(new BadResponseException('Bad request', new Request('GET', '/test'), new Response()))
132+
);
133+
134+
if (!$continueOnFailure) {
135+
$this->expectException(DaemonConnectionException::class);
136+
}
137+
138+
Bus::dispatchNow(new RunTaskJob($task));
139+
140+
if ($continueOnFailure) {
141+
$task->refresh();
142+
$schedule->refresh();
143+
144+
$this->assertFalse($task->is_queued);
145+
$this->assertFalse($schedule->is_processing);
146+
$this->assertTrue(CarbonImmutable::now()->isSameAs(CarbonImmutable::ISO8601, $schedule->last_run_at));
147+
}
148+
}
149+
150+
/**
151+
* @return array
152+
*/
153+
public function isManualRunDataProvider()
154+
{
155+
return [[true], [false]];
156+
}
157+
}

0 commit comments

Comments
 (0)