diff --git a/contrib/crontab.php b/contrib/crontab.php index 78a26004f..598a406c1 100644 --- a/contrib/crontab.php +++ b/contrib/crontab.php @@ -104,31 +104,32 @@ run ('unlink /tmp/crontab_save'); }); +if (!function_exists('Deployer\parseJob')) { + function parseJob ($job) { + if (!is_string($job)) { + return NULL; + } -function parseJob ($job) { - if (!is_string($job)) { - return NULL; - } + if (substr ($job, 0, 1) == '#') { + return NULL; + } - if (substr ($job, 0, 1) == '#') { - return NULL; - } + $jobData = explode (' ', $job, 6); - $jobData = explode (' ', $job, 6); + if (count ($jobData) != 6) { + return NULL; + } - if (count ($jobData) != 6) { - return NULL; + return [ + 'skey' => md5 ($job), + 'ckey' => md5 ($jobData['5']), + 'minute' => $jobData['0'], + 'hour' => $jobData['1'], + 'day' => $jobData['2'], + 'month' => $jobData['3'], + 'weekday' => $jobData['4'], + 'cmd' => $jobData['5'], + ]; } - - return [ - 'skey' => md5 ($job), - 'ckey' => md5 ($jobData['5']), - 'minute' => $jobData['0'], - 'hour' => $jobData['1'], - 'day' => $jobData['2'], - 'month' => $jobData['3'], - 'weekday' => $jobData['4'], - 'cmd' => $jobData['5'], - ]; } diff --git a/test/docker/Dockerfile b/test/docker/Dockerfile index cbc67ea0e..cebf15a41 100644 --- a/test/docker/Dockerfile +++ b/test/docker/Dockerfile @@ -103,6 +103,7 @@ CMD [ "start-servers" ] FROM ubuntu:20.04 AS provisioned RUN apt-get update && apt-get install -y \ + cron \ openssh-server \ sudo \ && rm -rf /var/lib/apt/lists/* diff --git a/test/e2e/AbstractE2ETest.php b/test/e2e/AbstractE2ETest.php index 3669e684f..e0d56cdfb 100644 --- a/test/e2e/AbstractE2ETest.php +++ b/test/e2e/AbstractE2ETest.php @@ -14,11 +14,6 @@ abstract class AbstractE2ETest extends TestCase */ protected $tester; - /** - * @var Deployer - */ - protected $deployer; - public static function setUpBeforeClass(): void { self::cleanUp(); @@ -47,9 +42,9 @@ protected function init(string $recipe): void $console->setAutoExit(false); $this->tester = new ApplicationTester($console); - $this->deployer = new Deployer($console); - $this->deployer->importer->import($recipe); - $this->deployer->init(); - $this->deployer->config->set('deploy_path', __TEMP_DIR__ . '/{{hostname}}'); + $deployer = new Deployer($console); + $deployer->importer->import($recipe); + $deployer->init(); + $deployer->config->set('deploy_path', __TEMP_DIR__ . '/{{hostname}}'); } } diff --git a/test/e2e/CrontabE2ETest.php b/test/e2e/CrontabE2ETest.php new file mode 100644 index 000000000..a73cd2452 --- /dev/null +++ b/test/e2e/CrontabE2ETest.php @@ -0,0 +1,79 @@ +init(self::RECIPE); + + set('crontab:jobs', [ + '* * * * * command-x', + ]); + + $this->tester->run([ + 'crontab:sync', + '-f' => self::RECIPE, + 'selector' => 'all', + ]); + + self::assertStringContainsString('command-x: NEW', $this->tester->getDisplay()); + + $this->init(self::RECIPE); + + set('crontab:jobs', [ + '* * * * * command-x', + ]); + + // Run the crontab:sync again to assert the same job won't be added twice + $this->tester->run([ + 'crontab:sync', + '-f' => self::RECIPE, + 'selector' => 'all', + ]); + + self::assertStringContainsString('command-x: OK', $this->tester->getDisplay()); + } + + public function testUpdatingCrontab(): void { + $this->init(self::RECIPE); + + set('crontab:jobs', [ + '1 * * * * command-y', + ]); + + $this->tester->run([ + 'crontab:sync', + '-f' => self::RECIPE, + 'selector' => 'all', + ]); + + self::assertStringContainsString('command-y: NEW', $this->tester->getDisplay()); + + $this->init(self::RECIPE); + + set('crontab:jobs', [ + '2 * * * * command-y', + ]); + + $this->tester->run([ + 'crontab:sync', + '-f' => self::RECIPE, + 'selector' => 'all', + ]); + + self::assertStringContainsString('command-y: FIX', $this->tester->getDisplay()); + } + + protected function tearDown(): void + { + parent::tearDown(); + + $this->tester->run(['crontab-test:truncate', '-f' => self::RECIPE, 'selector' => 'all']); + } +} + diff --git a/test/e2e/contrib/crontab.php b/test/e2e/contrib/crontab.php new file mode 100644 index 000000000..ddfc7f2fb --- /dev/null +++ b/test/e2e/contrib/crontab.php @@ -0,0 +1,18 @@ +set('timeout', 300) + ->setTag('e2e') + ->setRemoteUser('root') + ->setSshArguments([ + '-o UserKnownHostsFile=/dev/null', + '-o StrictHostKeyChecking=no', + ]); + +task('crontab-test:truncate', function() { + run ("echo '' > /tmp/crontab_save"); + run ("{{bin/crontab}} /tmp/crontab_save"); +});