Update schedule process to allow toggling/triggering via UI
This commit is contained in:
parent
02fe49892d
commit
036bea2b94
18 changed files with 280 additions and 221 deletions
|
@ -34,7 +34,7 @@ class CleanServiceBackupFilesCommandTest extends CommandTestCase
|
|||
{
|
||||
parent::setUp();
|
||||
|
||||
Carbon::setTestNow();
|
||||
Carbon::setTestNow(Carbon::now());
|
||||
$this->disk = m::mock(Filesystem::class);
|
||||
$this->filesystem = m::mock(Factory::class);
|
||||
$this->filesystem->shouldReceive('disk')->withNoArgs()->once()->andReturn($this->disk);
|
||||
|
|
|
@ -1,11 +1,4 @@
|
|||
<?php
|
||||
/*
|
||||
* Pterodactyl - Panel
|
||||
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
|
||||
*
|
||||
* This software is licensed under the terms of the MIT license.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
namespace Tests\Unit\Jobs\Schedule;
|
||||
|
||||
|
@ -87,9 +80,10 @@ class RunTaskJobTest extends TestCase
|
|||
$schedule = factory(Schedule::class)->make();
|
||||
$task = factory(Task::class)->make(['action' => 'power', 'sequence_id' => 1]);
|
||||
$task->setRelation('server', $server = factory(Server::class)->make());
|
||||
$task->setRelation('schedule', $schedule);
|
||||
$server->setRelation('user', factory(User::class)->make());
|
||||
|
||||
$this->taskRepository->shouldReceive('getTaskWithServer')->with($task->id)->once()->andReturn($task);
|
||||
$this->taskRepository->shouldReceive('getTaskForJobProcess')->with($task->id)->once()->andReturn($task);
|
||||
$this->keyProviderService->shouldReceive('handle')->with($server, $server->user)->once()->andReturn('123456');
|
||||
$this->powerRepository->shouldReceive('setServer')->with($task->server)->once()->andReturnSelf()
|
||||
->shouldReceive('setToken')->with('123456')->once()->andReturnSelf()
|
||||
|
@ -116,9 +110,10 @@ class RunTaskJobTest extends TestCase
|
|||
$schedule = factory(Schedule::class)->make();
|
||||
$task = factory(Task::class)->make(['action' => 'command', 'sequence_id' => 1]);
|
||||
$task->setRelation('server', $server = factory(Server::class)->make());
|
||||
$task->setRelation('schedule', $schedule);
|
||||
$server->setRelation('user', factory(User::class)->make());
|
||||
|
||||
$this->taskRepository->shouldReceive('getTaskWithServer')->with($task->id)->once()->andReturn($task);
|
||||
$this->taskRepository->shouldReceive('getTaskForJobProcess')->with($task->id)->once()->andReturn($task);
|
||||
$this->keyProviderService->shouldReceive('handle')->with($server, $server->user)->once()->andReturn('123456');
|
||||
$this->commandRepository->shouldReceive('setServer')->with($task->server)->once()->andReturnSelf()
|
||||
->shouldReceive('setToken')->with('123456')->once()->andReturnSelf()
|
||||
|
@ -145,9 +140,10 @@ class RunTaskJobTest extends TestCase
|
|||
$schedule = factory(Schedule::class)->make();
|
||||
$task = factory(Task::class)->make(['action' => 'command', 'sequence_id' => 1]);
|
||||
$task->setRelation('server', $server = factory(Server::class)->make());
|
||||
$task->setRelation('schedule', $schedule);
|
||||
$server->setRelation('user', factory(User::class)->make());
|
||||
|
||||
$this->taskRepository->shouldReceive('getTaskWithServer')->with($task->id)->once()->andReturn($task);
|
||||
$this->taskRepository->shouldReceive('getTaskForJobProcess')->with($task->id)->once()->andReturn($task);
|
||||
$this->keyProviderService->shouldReceive('handle')->with($server, $server->user)->once()->andReturn('123456');
|
||||
$this->commandRepository->shouldReceive('setServer')->with($task->server)->once()->andReturnSelf()
|
||||
->shouldReceive('setToken')->with('123456')->once()->andReturnSelf()
|
||||
|
@ -176,19 +172,45 @@ class RunTaskJobTest extends TestCase
|
|||
* 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-existant action.
|
||||
* @expectedExceptionMessage Cannot run a task that points to a non-existent action.
|
||||
*/
|
||||
public function testInvalidActionPassedToJob()
|
||||
{
|
||||
$schedule = factory(Schedule::class)->make();
|
||||
$task = factory(Task::class)->make(['action' => 'invalid', 'sequence_id' => 1]);
|
||||
$task->setRelation('server', $server = factory(Server::class)->make());
|
||||
$task->setRelation('schedule', $schedule);
|
||||
$server->setRelation('user', factory(User::class)->make());
|
||||
|
||||
$this->taskRepository->shouldReceive('getTaskWithServer')->with($task->id)->once()->andReturn($task);
|
||||
$this->taskRepository->shouldReceive('getTaskForJobProcess')->with($task->id)->once()->andReturn($task);
|
||||
|
||||
$this->getJobInstance($task->id, 1234);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that a schedule marked as disabled does not get processed.
|
||||
*/
|
||||
public function testScheduleMarkedAsDisabledDoesNotProcess()
|
||||
{
|
||||
$schedule = factory(Schedule::class)->make(['is_active' => false]);
|
||||
$task = factory(Task::class)->make(['action' => 'invalid', 'sequence_id' => 1]);
|
||||
$task->setRelation('server', $server = factory(Server::class)->make());
|
||||
$task->setRelation('schedule', $schedule);
|
||||
$server->setRelation('user', factory(User::class)->make());
|
||||
|
||||
$this->taskRepository->shouldReceive('getTaskForJobProcess')->with($task->id)->once()->andReturn($task);
|
||||
|
||||
$this->scheduleRepository->shouldReceive('withoutFreshModel->update')->with($schedule->id, [
|
||||
'is_processing' => false,
|
||||
'last_run_at' => Carbon::now()->toDateTimeString(),
|
||||
])->once()->andReturn(1);
|
||||
|
||||
$this->taskRepository->shouldReceive('update')->with($task->id, ['is_queued' => false])->once()->andReturn(1);
|
||||
|
||||
$this->getJobInstance($task->id, $schedule->id);
|
||||
$this->assertTrue(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the job using the mocks provided.
|
||||
*
|
||||
|
|
|
@ -1,15 +1,9 @@
|
|||
<?php
|
||||
/*
|
||||
* Pterodactyl - Panel
|
||||
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
|
||||
*
|
||||
* This software is licensed under the terms of the MIT license.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
namespace Tests\Unit\Services\Schedules;
|
||||
|
||||
use Mockery as m;
|
||||
use Carbon\Carbon;
|
||||
use Tests\TestCase;
|
||||
use Cron\CronExpression;
|
||||
use Pterodactyl\Models\Task;
|
||||
|
@ -46,8 +40,8 @@ class ProcessScheduleServiceTest extends TestCase
|
|||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
Carbon::setTestNow(Carbon::now());
|
||||
|
||||
$this->cron = m::mock('overload:' . CronExpression::class);
|
||||
$this->repository = m::mock(ScheduleRepositoryInterface::class);
|
||||
$this->runnerService = m::mock(RunTaskService::class);
|
||||
|
||||
|
@ -64,14 +58,12 @@ class ProcessScheduleServiceTest extends TestCase
|
|||
'sequence_id' => 1,
|
||||
])]));
|
||||
|
||||
$this->repository->shouldReceive('loadTasks')->with($model)->once()->andReturn($model);
|
||||
|
||||
$formatted = sprintf('%s %s %s * %s *', $model->cron_minute, $model->cron_hour, $model->cron_day_of_month, $model->cron_day_of_week);
|
||||
|
||||
$this->cron->shouldReceive('factory')->with($formatted)->once()->andReturnSelf()
|
||||
->shouldReceive('getNextRunDate')->withNoArgs()->once()->andReturn('00:00:00');
|
||||
|
||||
$this->repository->shouldReceive('update')->with($model->id, [
|
||||
'is_processing' => true,
|
||||
'next_run_at' => '00:00:00',
|
||||
'next_run_at' => CronExpression::factory($formatted)->getNextRunDate(),
|
||||
]);
|
||||
|
||||
$this->runnerService->shouldReceive('handle')->with($task)->once()->andReturnNull();
|
||||
|
@ -80,58 +72,23 @@ class ProcessScheduleServiceTest extends TestCase
|
|||
$this->assertTrue(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that passing a schedule model without a tasks relation is handled.
|
||||
*/
|
||||
public function testScheduleModelWithoutTasksIsHandled()
|
||||
{
|
||||
$nonRelationModel = factory(Schedule::class)->make();
|
||||
$model = clone $nonRelationModel;
|
||||
$model->setRelation('tasks', collect([$task = factory(Task::class)->make([
|
||||
'sequence_id' => 1,
|
||||
])]));
|
||||
|
||||
$formatted = sprintf('%s %s %s * %s *', $model->cron_minute, $model->cron_hour, $model->cron_day_of_month, $model->cron_day_of_week);
|
||||
|
||||
$this->repository->shouldReceive('getScheduleWithTasks')->with($nonRelationModel->id)->once()->andReturn($model);
|
||||
$this->cron->shouldReceive('factory')->with($formatted)->once()->andReturnSelf()
|
||||
->shouldReceive('getNextRunDate')->withNoArgs()->once()->andReturn('00:00:00');
|
||||
|
||||
$this->repository->shouldReceive('update')->with($model->id, [
|
||||
'is_processing' => true,
|
||||
'next_run_at' => '00:00:00',
|
||||
]);
|
||||
|
||||
$this->runnerService->shouldReceive('handle')->with($task)->once()->andReturnNull();
|
||||
|
||||
$this->service->handle($nonRelationModel);
|
||||
$this->assertTrue(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that a task ID can be passed in place of the task model.
|
||||
*/
|
||||
public function testPassingScheduleIdInPlaceOfModelIsHandled()
|
||||
public function testScheduleRunTimeCanBeOverridden()
|
||||
{
|
||||
$model = factory(Schedule::class)->make();
|
||||
$model->setRelation('tasks', collect([$task = factory(Task::class)->make([
|
||||
'sequence_id' => 1,
|
||||
])]));
|
||||
|
||||
$formatted = sprintf('%s %s %s * %s *', $model->cron_minute, $model->cron_hour, $model->cron_day_of_month, $model->cron_day_of_week);
|
||||
|
||||
$this->repository->shouldReceive('getScheduleWithTasks')->with($model->id)->once()->andReturn($model);
|
||||
$this->cron->shouldReceive('factory')->with($formatted)->once()->andReturnSelf()
|
||||
->shouldReceive('getNextRunDate')->withNoArgs()->once()->andReturn('00:00:00');
|
||||
$this->repository->shouldReceive('loadTasks')->with($model)->once()->andReturn($model);
|
||||
|
||||
$this->repository->shouldReceive('update')->with($model->id, [
|
||||
'is_processing' => true,
|
||||
'next_run_at' => '00:00:00',
|
||||
'next_run_at' => Carbon::now()->addSeconds(15)->toDateTimeString(),
|
||||
]);
|
||||
|
||||
$this->runnerService->shouldReceive('handle')->with($task)->once()->andReturnNull();
|
||||
|
||||
$this->service->handle($model->id);
|
||||
$this->service->setRunTimeOverride(Carbon::now()->addSeconds(15))->handle($model);
|
||||
$this->assertTrue(true);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,17 +1,10 @@
|
|||
<?php
|
||||
/**
|
||||
* Pterodactyl - Panel
|
||||
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
|
||||
*
|
||||
* This software is licensed under the terms of the MIT license.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
namespace Tests\Unit\Services\Schedules;
|
||||
|
||||
use Mockery as m;
|
||||
use Tests\TestCase;
|
||||
use Pterodactyl\Models\Node;
|
||||
use Cron\CronExpression;
|
||||
use Pterodactyl\Models\Server;
|
||||
use Pterodactyl\Models\Schedule;
|
||||
use Illuminate\Database\ConnectionInterface;
|
||||
|
@ -54,7 +47,6 @@ class ScheduleCreationServiceTest extends TestCase
|
|||
parent::setUp();
|
||||
|
||||
$this->connection = m::mock(ConnectionInterface::class);
|
||||
$this->cron = m::mock('overload:\Cron\CronExpression');
|
||||
$this->repository = m::mock(ScheduleRepositoryInterface::class);
|
||||
$this->taskCreationService = m::mock(TaskCreationService::class);
|
||||
|
||||
|
@ -69,18 +61,15 @@ class ScheduleCreationServiceTest extends TestCase
|
|||
$schedule = factory(Schedule::class)->make();
|
||||
$server = factory(Server::class)->make();
|
||||
|
||||
$this->cron->shouldReceive('factory')->with('* * * * * *')->once()->andReturnSelf()
|
||||
->shouldReceive('getNextRunDate')->withNoArgs()->once()->andReturn('nextDate');
|
||||
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
||||
$this->repository->shouldReceive('create')->with([
|
||||
'server_id' => $server->id,
|
||||
'next_run_at' => 'nextDate',
|
||||
'test_data' => 'test_value',
|
||||
'next_run_at' => CronExpression::factory('* * * * * *')->getNextRunDate(),
|
||||
'test_key' => 'value',
|
||||
])->once()->andReturn($schedule);
|
||||
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
|
||||
|
||||
$response = $this->service->handle($server, ['test_data' => 'test_value']);
|
||||
$this->assertNotEmpty($response);
|
||||
$response = $this->service->handle($server, ['test_key' => 'value', 'server_id' => '123abc']);
|
||||
$this->assertInstanceOf(Schedule::class, $response);
|
||||
$this->assertEquals($schedule, $response);
|
||||
}
|
||||
|
@ -93,14 +82,13 @@ class ScheduleCreationServiceTest extends TestCase
|
|||
$schedule = factory(Schedule::class)->make();
|
||||
$server = factory(Server::class)->make();
|
||||
|
||||
$this->cron->shouldReceive('factory')->with('* * * * * *')->once()->andReturnSelf()
|
||||
->shouldReceive('getNextRunDate')->withNoArgs()->once()->andReturn('nextDate');
|
||||
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
||||
$this->repository->shouldReceive('create')->with([
|
||||
'next_run_at' => 'nextDate',
|
||||
'server_id' => $server->id,
|
||||
'test_data' => 'test_value',
|
||||
'next_run_at' => CronExpression::factory('* * * * * *')->getNextRunDate(),
|
||||
'test_key' => 'value',
|
||||
])->once()->andReturn($schedule);
|
||||
|
||||
$this->taskCreationService->shouldReceive('handle')->with($schedule, [
|
||||
'time_interval' => 'm',
|
||||
'time_value' => 10,
|
||||
|
@ -111,62 +99,10 @@ class ScheduleCreationServiceTest extends TestCase
|
|||
|
||||
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
|
||||
|
||||
$response = $this->service->handle($server, ['test_data' => 'test_value'], [
|
||||
$response = $this->service->handle($server, ['test_key' => 'value'], [
|
||||
['time_interval' => 'm', 'time_value' => 10, 'action' => 'test', 'payload' => 'testpayload'],
|
||||
]);
|
||||
$this->assertNotEmpty($response);
|
||||
$this->assertInstanceOf(Schedule::class, $response);
|
||||
$this->assertEquals($schedule, $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that an ID can be passed in place of the server model.
|
||||
*/
|
||||
public function testIdCanBePassedInPlaceOfServerModel()
|
||||
{
|
||||
$schedule = factory(Schedule::class)->make();
|
||||
|
||||
$this->cron->shouldReceive('factory')->with('* * * * * *')->once()->andReturnSelf()
|
||||
->shouldReceive('getNextRunDate')->withNoArgs()->once()->andReturn('nextDate');
|
||||
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
||||
$this->repository->shouldReceive('create')->with([
|
||||
'next_run_at' => 'nextDate',
|
||||
'server_id' => 1234,
|
||||
'test_data' => 'test_value',
|
||||
])->once()->andReturn($schedule);
|
||||
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
|
||||
|
||||
$response = $this->service->handle(1234, ['test_data' => 'test_value']);
|
||||
$this->assertNotEmpty($response);
|
||||
$this->assertInstanceOf(Schedule::class, $response);
|
||||
$this->assertEquals($schedule, $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that an exception is raised if invalid data is passed.
|
||||
*
|
||||
* @dataProvider invalidServerArgumentProvider
|
||||
* @expectedException \InvalidArgumentException
|
||||
*/
|
||||
public function testExceptionIsThrownIfServerIsInvalid($attribute)
|
||||
{
|
||||
$this->service->handle($attribute, []);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of invalid server data to test aganist.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function invalidServerArgumentProvider()
|
||||
{
|
||||
return [
|
||||
[123.456],
|
||||
['server'],
|
||||
['abc123'],
|
||||
['123_test'],
|
||||
[new Node()],
|
||||
[Server::class],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue