forked from pterodactyl/panel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRunTaskJobTest.php
More file actions
178 lines (144 loc) · 5.93 KB
/
RunTaskJobTest.php
File metadata and controls
178 lines (144 loc) · 5.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
namespace Pterodactyl\Tests\Integration\Jobs\Schedule;
use Carbon\Carbon;
use Carbon\CarbonImmutable;
use GuzzleHttp\Psr7\Request;
use Pterodactyl\Models\Task;
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 Schedule $schedule */
$schedule = Schedule::factory()->create([
'server_id' => $server->id,
'is_processing' => true,
'last_run_at' => null,
'is_active' => false,
]);
/** @var Task $task */
$task = Task::factory()->create(['schedule_id' => $schedule->id, 'is_queued' => true]);
$job = new RunTaskJob($task);
Bus::dispatchSync($job);
$task->refresh();
$schedule->refresh();
$this->assertFalse($task->is_queued);
$this->assertFalse($schedule->is_processing);
$this->assertFalse($schedule->is_active);
$this->assertTrue(CarbonImmutable::now()->isSameAs(\DateTimeInterface::ATOM, $schedule->last_run_at));
}
public function testJobWithInvalidActionThrowsException()
{
$server = $this->createServerModel();
/** @var Schedule $schedule */
$schedule = Schedule::factory()->create(['server_id' => $server->id]);
/** @var Task $task */
$task = Task::factory()->create(['schedule_id' => $schedule->id, 'action' => 'foobar']);
$job = new RunTaskJob($task);
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid task action provided: foobar');
Bus::dispatchSync($job);
}
#[\PHPUnit\Framework\Attributes\DataProvider('isManualRunDataProvider')]
public function testJobIsExecuted(bool $isManualRun)
{
$server = $this->createServerModel();
/** @var Schedule $schedule */
$schedule = Schedule::factory()->create([
'server_id' => $server->id,
'is_active' => !$isManualRun,
'is_processing' => true,
'last_run_at' => null,
]);
/** @var Task $task */
$task = Task::factory()->create([
'schedule_id' => $schedule->id,
'action' => Task::ACTION_POWER,
'payload' => 'start',
'is_queued' => true,
'continue_on_failure' => false,
]);
$mock = \Mockery::mock(DaemonPowerRepository::class);
$this->instance(DaemonPowerRepository::class, $mock);
$mock->expects('setServer')->with(\Mockery::on(function ($value) use ($server) {
return $value instanceof Server && $value->id === $server->id;
}))->andReturnSelf();
$mock->expects('send')->with('start')->andReturn(new Response());
Bus::dispatchSync(new RunTaskJob($task, $isManualRun));
$task->refresh();
$schedule->refresh();
$this->assertFalse($task->is_queued);
$this->assertFalse($schedule->is_processing);
$this->assertTrue(CarbonImmutable::now()->isSameAs(\DateTimeInterface::ATOM, $schedule->last_run_at));
}
#[\PHPUnit\Framework\Attributes\DataProvider('isManualRunDataProvider')]
public function testExceptionDuringRunIsHandledCorrectly(bool $continueOnFailure)
{
$server = $this->createServerModel();
/** @var Schedule $schedule */
$schedule = Schedule::factory()->create(['server_id' => $server->id]);
/** @var Task $task */
$task = Task::factory()->create([
'schedule_id' => $schedule->id,
'action' => Task::ACTION_POWER,
'payload' => 'start',
'continue_on_failure' => $continueOnFailure,
]);
$mock = \Mockery::mock(DaemonPowerRepository::class);
$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::dispatchSync(new RunTaskJob($task));
if ($continueOnFailure) {
$task->refresh();
$schedule->refresh();
$this->assertFalse($task->is_queued);
$this->assertFalse($schedule->is_processing);
$this->assertTrue(CarbonImmutable::now()->isSameAs(\DateTimeInterface::ATOM, $schedule->last_run_at));
}
}
/**
* 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::dispatchSync(new RunTaskJob($task));
$task->refresh();
$schedule->refresh();
$this->assertFalse($task->is_queued);
$this->assertFalse($schedule->is_processing);
$this->assertTrue(Carbon::now()->isSameAs(\DateTimeInterface::ATOM, $schedule->last_run_at));
}
public static function isManualRunDataProvider(): array
{
return [[true], [false]];
}
}