From 677bf1ebd4f5ac3a4de8512cf31aedb457875c2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Monta=C3=B1ez?= Date: Sun, 1 Jan 2017 03:55:09 -0300 Subject: [PATCH] [Nostromo] Fixes and Tests --- src/Mage/Command/BuiltIn/DeployCommand.php | 26 ++- src/Mage/MageApplication.php | 9 +- src/Mage/Task/BuiltIn/Deploy/RsyncTask.php | 3 +- .../Task/BuiltIn/Deploy/TarGz/CleanupTask.php | 4 +- src/Mage/Task/ErrorException.php | 2 +- .../Command/BuiltIn/DeployCommandTest.php | 167 ++++++++++++++++++ 6 files changed, 194 insertions(+), 17 deletions(-) diff --git a/src/Mage/Command/BuiltIn/DeployCommand.php b/src/Mage/Command/BuiltIn/DeployCommand.php index 368d8a0..7d738de 100644 --- a/src/Mage/Command/BuiltIn/DeployCommand.php +++ b/src/Mage/Command/BuiltIn/DeployCommand.php @@ -32,6 +32,11 @@ use Mage\Command\AbstractCommand; */ class DeployCommand extends AbstractCommand { + /** + * @var int + */ + protected $statusCode = 0; + /** * @var TaskFactory */ @@ -93,7 +98,7 @@ class DeployCommand extends AbstractCommand $output->writeln('Finished Magallanes'); - return 0; + return $this->statusCode; } /** @@ -133,20 +138,21 @@ class DeployCommand extends AbstractCommand $this->runtime->setStage(Runtime::ON_DEPLOY); $onDeployTasks = $this->runtime->getTasks(); - if ($this->runtime->getEnvironmentConfig('releases', false) && !$this->runtime->inRollback()) { + if (!$this->runtime->inRollback()) { + } + + if ($this->runtime->getEnvironmentConfig('releases', false)) { if (!in_array('deploy/targz/copy', $onDeployTasks)) { array_unshift($onDeployTasks, 'deploy/targz/copy'); } - } else { - if (!in_array('deploy/rsync', $onDeployTasks) && !$this->runtime->inRollback()) { - array_unshift($onDeployTasks, 'deploy/rsync'); - } - } - if ($this->runtime->getEnvironmentConfig('releases', false) && !$this->runtime->inRollback()) { if (!in_array('deploy/release/prepare', $onDeployTasks)) { array_unshift($onDeployTasks, 'deploy/release/prepare'); } + } else { + if (!in_array('deploy/rsync', $onDeployTasks)) { + array_unshift($onDeployTasks, 'deploy/rsync'); + } } foreach ($hosts as $host) { @@ -269,6 +275,7 @@ class DeployCommand extends AbstractCommand $this->log(sprintf('Task %s (%s) finished with OK', $task->getDescription(), $task->getName())); } else { $output->writeln('FAIL'); + $this->statusCode = 500; $this->log(sprintf('Task %s (%s) finished with FAIL', $task->getDescription(), $task->getName())); } } catch (SkipException $exception) { @@ -276,8 +283,9 @@ class DeployCommand extends AbstractCommand $output->writeln('SKIPPED'); $this->log(sprintf('Task %s (%s) finished with SKIPPED, thrown SkipException', $task->getDescription(), $task->getName())); } catch (ErrorException $exception) { - $output->writeln(sprintf('FAIL [%s]', $exception->getTrimmedMessage())); + $output->writeln(sprintf('ERROR [%s]', $exception->getTrimmedMessage())); $this->log(sprintf('Task %s (%s) finished with FAIL, with Error "%s"', $task->getDescription(), $task->getName(), $exception->getMessage())); + $this->statusCode = $exception->getCode(); } } } diff --git a/src/Mage/MageApplication.php b/src/Mage/MageApplication.php index dbbb39e..1c792b2 100644 --- a/src/Mage/MageApplication.php +++ b/src/Mage/MageApplication.php @@ -48,17 +48,18 @@ class MageApplication extends Application if (array_key_exists('magephp', $config)) { $config = $config['magephp']; + $logger = null; if (array_key_exists('log_dir', $config)) { $logfile = sprintf('%s/%s.log', $config['log_dir'], date('Ymd_His')); $config['log_file'] = $logfile; $logger = new Logger('magephp'); $logger->pushHandler(new StreamHandler($logfile)); - - $this->runtime = new Runtime(); - $this->runtime->setConfiguration($config); - $this->runtime->setLogger($logger); } + + $this->runtime = new Runtime(); + $this->runtime->setConfiguration($config); + $this->runtime->setLogger($logger); } else { throw new RuntimeException(sprintf('The file "%s" does not have a valid Magallanes configuration.', $file)); } diff --git a/src/Mage/Task/BuiltIn/Deploy/RsyncTask.php b/src/Mage/Task/BuiltIn/Deploy/RsyncTask.php index 9d6f8a3..2b871d8 100644 --- a/src/Mage/Task/BuiltIn/Deploy/RsyncTask.php +++ b/src/Mage/Task/BuiltIn/Deploy/RsyncTask.php @@ -10,6 +10,7 @@ namespace Mage\Task\BuiltIn\Deploy; +use Mage\Task\ErrorException; use Symfony\Component\Process\Process; use Mage\Task\AbstractTask; @@ -38,7 +39,7 @@ class RsyncTask extends AbstractTask $targetDir = rtrim($hostPath, '/'); if ($this->runtime->getEnvironmentConfig('releases', false)) { - $targetDir = sprintf('%s/releases/%s', $hostPath, $this->runtime->getReleaseId()); + throw new ErrorException('Can\'t be used with Releases, use "deploy/targz/copy"'); } $excludes = $this->getExcludes(); diff --git a/src/Mage/Task/BuiltIn/Deploy/TarGz/CleanupTask.php b/src/Mage/Task/BuiltIn/Deploy/TarGz/CleanupTask.php index 516d5c0..b4a46e5 100644 --- a/src/Mage/Task/BuiltIn/Deploy/TarGz/CleanupTask.php +++ b/src/Mage/Task/BuiltIn/Deploy/TarGz/CleanupTask.php @@ -10,7 +10,7 @@ namespace Mage\Task\BuiltIn\Deploy\TarGz; -use Mage\Runtime\Exception\DeploymentException; +use Mage\Task\ErrorException; use Symfony\Component\Process\Process; use Mage\Task\AbstractTask; @@ -34,7 +34,7 @@ class CleanupTask extends AbstractTask public function execute() { if (!$this->runtime->getEnvironmentConfig('releases', false)) { - throw new DeploymentException('This task is only available with releases enabled', 400); + throw new ErrorException('This task is only available with releases enabled', 400); } $tarGzLocal = $this->runtime->getVar('targz_local'); diff --git a/src/Mage/Task/ErrorException.php b/src/Mage/Task/ErrorException.php index c5521f5..6fb64a4 100644 --- a/src/Mage/Task/ErrorException.php +++ b/src/Mage/Task/ErrorException.php @@ -19,7 +19,7 @@ use Exception; */ class ErrorException extends Exception { - public function getTrimmedMessage($maxLength = 20) + public function getTrimmedMessage($maxLength = 80) { $message = $this->getMessage(); diff --git a/src/Mage/Tests/Command/BuiltIn/DeployCommandTest.php b/src/Mage/Tests/Command/BuiltIn/DeployCommandTest.php index 89cab7c..65075ef 100644 --- a/src/Mage/Tests/Command/BuiltIn/DeployCommandTest.php +++ b/src/Mage/Tests/Command/BuiltIn/DeployCommandTest.php @@ -124,6 +124,169 @@ class DeployCommandTest extends TestCase foreach ($testCase as $index => $command) { $this->assertEquals($ranCommands[$index], $command); } + + $this->assertEquals($tester->getStatusCode(), 0); + } + + public function testDeploymentWithErrorTaskCommands() + { + $application = new MageTestApplication(); + $application->add(new DeployCommand()); + + $runtime = new RuntimeMockup(); + $runtime->setConfiguration(array( + 'environments' => + array( + 'test' => + array( + 'user' => 'tester', + 'branch' => 'test', + 'host_path' => '/var/www/test', + 'releases' => 4, + 'exclude' => + array( + 0 => 'vendor', + 1 => 'app/cache', + 2 => 'app/log', + 3 => 'web/app_dev.php', + ), + 'hosts' => + array( + 0 => 'testhost', + ), + 'pre-deploy' => + array( + 0 => 'git/update', + 1 => 'composer/install', + 2 => 'composer/generate-autoload', + ), + 'on-deploy' => + array( + 0 => 'deploy/rsync', + ), + 'on-release' => null, + 'post-release' => null, + 'post-deploy' => null, + ), + ), + ) + ); + + $runtime->setReleaseId('20170101015120'); + + /** @var AbstractCommand $command */ + $command = $application->find('deploy'); + $command->setRuntime($runtime); + + $tester = new CommandTester($command); + $tester->execute(['command' => $command->getName(), 'environment' => 'test']); + + $ranCommands = $runtime->getRanCommands(); + + $testCase = array( + 0 => 'git branch | grep "*"', + 1 => 'git checkout test', + 2 => 'git pull', + 3 => 'composer install --dev', + 4 => 'composer dumpautoload --optimize', + 5 => 'tar cfz /tmp/mageXYZ --exclude=.git --exclude=vendor --exclude=app/cache --exclude=app/log --exclude=web/app_dev.php ./', + 6 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"mkdir -p /var/www/test/releases/1234567890\\"', + 7 => 'scp -P 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no /tmp/mageXYZ tester@testhost:/var/www/test/releases/1234567890/mageXYZ', + 8 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"cd /var/www/test/releases/1234567890 \\&\\& tar xfz mageXYZ\\"', + 9 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"rm /var/www/test/releases/1234567890/mageXYZ\\"', + ); + + // Check total of Executed Commands + $this->assertEquals(count($ranCommands), count($testCase)); + + // Check Generated Commands + foreach ($testCase as $index => $command) { + $this->assertEquals($ranCommands[$index], $command); + } + + $this->assertTrue(strpos($tester->getDisplay(), 'ERROR') !== false); + + $this->assertNotEquals($tester->getStatusCode(), 0); + } + + public function testDeploymentWithFailingPostDeployTaskCommands() + { + $application = new MageTestApplication(); + $application->add(new DeployCommand()); + + $runtime = new RuntimeMockup(); + $runtime->setConfiguration(array( + 'environments' => + array( + 'test' => + array( + 'user' => 'tester', + 'branch' => 'test', + 'host_path' => '/var/www/test', + 'exclude' => + array( + 0 => 'vendor', + 1 => 'app/cache', + 2 => 'app/log', + 3 => 'web/app_dev.php', + ), + 'hosts' => + array( + 0 => 'testhost', + ), + 'pre-deploy' => + array( + 0 => 'git/update', + 1 => 'composer/install', + 2 => 'composer/generate-autoload', + ), + 'on-deploy' => + array( + 0 => 'deploy/rsync', + ), + 'on-release' => null, + 'post-release' => null, + 'post-deploy' => + array( + 0 => 'deploy/targz/cleanup', + ), + ), + ), + ) + ); + + $runtime->setReleaseId('20170101015120'); + + /** @var AbstractCommand $command */ + $command = $application->find('deploy'); + $command->setRuntime($runtime); + + $tester = new CommandTester($command); + $tester->execute(['command' => $command->getName(), 'environment' => 'test']); + + $ranCommands = $runtime->getRanCommands(); + + $testCase = array( + 0 => 'git branch | grep "*"', + 1 => 'git checkout test', + 2 => 'git pull', + 3 => 'composer install --dev', + 4 => 'composer dumpautoload --optimize', + 5 => 'rsync -avz --exclude=.git --exclude=vendor --exclude=app/cache --exclude=app/log --exclude=web/app_dev.php ./ tester@testhost:/var/www/test', + 6 => 'git checkout master', + ); + + // Check total of Executed Commands + $this->assertEquals(count($ranCommands), count($testCase)); + + // Check Generated Commands + foreach ($testCase as $index => $command) { + $this->assertEquals($ranCommands[$index], $command); + } + + $this->assertTrue(strpos($tester->getDisplay(), 'ERROR') !== false); + + $this->assertNotEquals($tester->getStatusCode(), 0); } public function testDeploymentWithoutReleasesCommands() @@ -228,6 +391,8 @@ class DeployCommandTest extends TestCase foreach ($testCase as $index => $command) { $this->assertEquals($ranCommands[$index], $command); } + + $this->assertEquals($tester->getStatusCode(), 0); } public function testDeploymentWithSkippingTask() @@ -333,5 +498,7 @@ class DeployCommandTest extends TestCase } $this->assertTrue(strpos($tester->getDisplay(), 'SKIPPED') !== false); + + $this->assertEquals($tester->getStatusCode(), 0); } }