Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add e2e test for crontab.php #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 22 additions & 21 deletions contrib/crontab.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
];
}

1 change: 1 addition & 0 deletions test/docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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/*
Expand Down
13 changes: 4 additions & 9 deletions test/e2e/AbstractE2ETest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ abstract class AbstractE2ETest extends TestCase
*/
protected $tester;

/**
* @var Deployer
*/
protected $deployer;

public static function setUpBeforeClass(): void
{
self::cleanUp();
Expand Down Expand Up @@ -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}}');
}
}
79 changes: 79 additions & 0 deletions test/e2e/CrontabE2ETest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php declare(strict_types=1);
namespace e2e;

use function Deployer\set;

class CrontabE2ETest extends AbstractE2ETest
{
private const RECIPE = __DIR__ . '/contrib/crontab.php';

public function testAddingCrontab(): void {

$this->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']);
}
}

18 changes: 18 additions & 0 deletions test/e2e/contrib/crontab.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php declare(strict_types=1);
namespace Deployer;

require __DIR__ . '/../../../contrib/crontab.php';

host('provisioned.test')
->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");
});