diff --git a/src/Mage/Command/BuiltIn/Releases/RollbackCommand.php b/src/Mage/Command/BuiltIn/Releases/RollbackCommand.php index 06e81b6..d92eac8 100644 --- a/src/Mage/Command/BuiltIn/Releases/RollbackCommand.php +++ b/src/Mage/Command/BuiltIn/Releases/RollbackCommand.php @@ -90,7 +90,7 @@ class RollbackCommand extends DeployCommand return $exception->getCode(); } } else { - throw new DeploymentException(sprintf('Release %s is not available on all hosts', $releaseToRollback), 72); + throw new DeploymentException(sprintf('Release "%s" is not available on all hosts', $releaseToRollback), 72); } $output->writeln('Finished Magallanes'); @@ -106,11 +106,10 @@ class RollbackCommand extends DeployCommand */ protected function checkReleaseAvailability($releaseToRollback) { - $releaseIdCandidate = false; $hosts = $this->runtime->getEnvironmentConfig('hosts'); $hostPath = rtrim($this->runtime->getEnvironmentConfig('host_path'), '/'); - $releaseAvailableInAllHosts = true; + $releaseAvailableInHosts = 0; foreach ($hosts as $host) { $this->runtime->setWorkingHost($host); @@ -127,20 +126,14 @@ class RollbackCommand extends DeployCommand } if (in_array($releaseToRollback, $releases)) { - if ($releaseIdCandidate === false) { - $releaseIdCandidate = $releaseToRollback; - } else { - if ($releaseIdCandidate != $releaseToRollback) { - $releaseAvailableInAllHosts = false; - } - } + $releaseAvailableInHosts++; } $this->runtime->setWorkingHost(null); } - if ($releaseAvailableInAllHosts) { - return $releaseIdCandidate; + if ($releaseAvailableInHosts === count($hosts)) { + return $releaseToRollback; } return false; diff --git a/src/Mage/Task/BuiltIn/Deploy/TarGz/CopyTask.php b/src/Mage/Task/BuiltIn/Deploy/TarGz/CopyTask.php index 03201d1..36a861f 100644 --- a/src/Mage/Task/BuiltIn/Deploy/TarGz/CopyTask.php +++ b/src/Mage/Task/BuiltIn/Deploy/TarGz/CopyTask.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 CopyTask extends AbstractTask public function execute() { if (!$this->runtime->getEnvironmentConfig('releases', false)) { - throw new DeploymentException('This task is only available with releases enabled', 40); + throw new ErrorException('This task is only available with releases enabled', 40); } $user = $this->runtime->getEnvironmentConfig('user', $this->runtime->getCurrentUser()); diff --git a/src/Mage/Task/BuiltIn/Deploy/TarGz/PrepareTask.php b/src/Mage/Task/BuiltIn/Deploy/TarGz/PrepareTask.php index 2027d81..440a26b 100644 --- a/src/Mage/Task/BuiltIn/Deploy/TarGz/PrepareTask.php +++ b/src/Mage/Task/BuiltIn/Deploy/TarGz/PrepareTask.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 PrepareTask extends AbstractTask public function execute() { if (!$this->runtime->getEnvironmentConfig('releases', false)) { - throw new DeploymentException('This task is only available with releases enabled', 40); + throw new ErrorException('This task is only available with releases enabled', 40); } $tarGzLocal = $this->runtime->getTempFile(); diff --git a/src/Mage/Tests/Command/BuiltIn/DeployCommandMiscTasksTest.php b/src/Mage/Tests/Command/BuiltIn/DeployCommandMiscTasksTest.php index 9f88af6..af57eb4 100644 --- a/src/Mage/Tests/Command/BuiltIn/DeployCommandMiscTasksTest.php +++ b/src/Mage/Tests/Command/BuiltIn/DeployCommandMiscTasksTest.php @@ -93,9 +93,11 @@ class DeployCommandMiscTasksTest extends TestCase $command = $application->find('deploy'); $this->assertTrue($command instanceof DeployCommand); + $tester = new CommandTester($command); + try { - $tester = new CommandTester($command); $tester->execute(['command' => $command->getName(), 'environment' => 'test']); + $this->assertTrue(false, 'Command did not failed'); } catch (Exception $exception) { $this->assertTrue($exception instanceof RuntimeException); $this->assertEquals('Invalid task name "invalid/task"', $exception->getMessage()); diff --git a/src/Mage/Tests/Command/BuiltIn/DeployCommandWithReleasesTest.php b/src/Mage/Tests/Command/BuiltIn/DeployCommandWithReleasesTest.php index eb6e1fe..67caf21 100644 --- a/src/Mage/Tests/Command/BuiltIn/DeployCommandWithReleasesTest.php +++ b/src/Mage/Tests/Command/BuiltIn/DeployCommandWithReleasesTest.php @@ -68,4 +68,68 @@ class DeployCommandWithReleasesTest extends TestCase $this->assertEquals(0, $tester->getStatusCode()); } + + public function testDeploymentWithoutReleasesTarPrepare() + { + $application = new MageApplicationMockup(); + $application->configure(__DIR__ . '/../../Resources/testhost-force-tar1.yml'); + + /** @var AbstractCommand $command */ + $command = $application->find('deploy'); + $this->assertTrue($command instanceof DeployCommand); + + $tester = new CommandTester($command); + $tester->execute(['command' => $command->getName(), 'environment' => 'test']); + + $this->assertContains('This task is only available with releases enabled', $tester->getDisplay()); + $this->assertNotEquals(0, $tester->getStatusCode()); + } + + public function testDeploymentWithoutReleasesTarCopy() + { + $application = new MageApplicationMockup(); + $application->configure(__DIR__ . '/../../Resources/testhost-force-tar2.yml'); + + /** @var AbstractCommand $command */ + $command = $application->find('deploy'); + $this->assertTrue($command instanceof DeployCommand); + + $tester = new CommandTester($command); + $tester->execute(['command' => $command->getName(), 'environment' => 'test']); + + $this->assertContains('This task is only available with releases enabled', $tester->getDisplay()); + $this->assertNotEquals(0, $tester->getStatusCode()); + } + + public function testDeploymentWithoutReleasesTarCleanup() + { + $application = new MageApplicationMockup(); + $application->configure(__DIR__ . '/../../Resources/testhost-force-tar3.yml'); + + /** @var AbstractCommand $command */ + $command = $application->find('deploy'); + $this->assertTrue($command instanceof DeployCommand); + + $tester = new CommandTester($command); + $tester->execute(['command' => $command->getName(), 'environment' => 'test']); + + $this->assertContains('This task is only available with releases enabled', $tester->getDisplay()); + $this->assertNotEquals(0, $tester->getStatusCode()); + } + + public function testDeploymentFailCopyCommands() + { + $application = new MageApplicationMockup(); + $application->configure(__DIR__ . '/../../Resources/testhost-fail-copy-tar.yml'); + + /** @var AbstractCommand $command */ + $command = $application->find('deploy'); + $this->assertTrue($command instanceof DeployCommand); + + $tester = new CommandTester($command); + $tester->execute(['command' => $command->getName(), 'environment' => 'test']); + + $this->assertContains('Copying files with TarGZ ... FAIL', $tester->getDisplay()); + $this->assertNotEquals(0, $tester->getStatusCode()); + } } diff --git a/src/Mage/Tests/Command/BuiltIn/Releases/ListCommandTest.php b/src/Mage/Tests/Command/BuiltIn/Releases/ListCommandTest.php index 2d21cc6..ae92c71 100644 --- a/src/Mage/Tests/Command/BuiltIn/Releases/ListCommandTest.php +++ b/src/Mage/Tests/Command/BuiltIn/Releases/ListCommandTest.php @@ -78,6 +78,7 @@ class ListCommandTest extends TestCase try { $tester->execute(['command' => $command->getName(), 'environment' => 'test']); + $this->assertTrue(false, 'Command did not failed'); } catch (Exception $exception) { $this->assertTrue($exception instanceof DeploymentException); $this->assertEquals('Releases are not enabled', $exception->getMessage()); @@ -97,6 +98,7 @@ class ListCommandTest extends TestCase try { $tester->execute(['command' => $command->getName(), 'environment' => 'test']); + $this->assertTrue(false, 'Command did not failed'); } catch (Exception $exception) { $this->assertTrue($exception instanceof RuntimeException); $this->assertEquals('Unable to retrieve current release from host "host1"', $exception->getMessage()); @@ -131,6 +133,7 @@ class ListCommandTest extends TestCase try { $tester->execute(['command' => $command->getName(), 'environment' => 'test']); + $this->assertTrue(false, 'Command did not failed'); } catch (Exception $exception) { $this->assertTrue($exception instanceof RuntimeException); $this->assertEquals('Unable to retrieve releases from host "host3"', $exception->getMessage()); diff --git a/src/Mage/Tests/Command/BuiltIn/Releases/RollbackCommandTest.php b/src/Mage/Tests/Command/BuiltIn/Releases/RollbackCommandTest.php index 4d9b15b..4bb61df 100644 --- a/src/Mage/Tests/Command/BuiltIn/Releases/RollbackCommandTest.php +++ b/src/Mage/Tests/Command/BuiltIn/Releases/RollbackCommandTest.php @@ -73,12 +73,34 @@ class RollbackCommandTest extends TestCase $command = $application->find('releases:rollback'); $this->assertTrue($command instanceof RollbackCommand); + $tester = new CommandTester($command); + try { - $tester = new CommandTester($command); $tester->execute(['command' => $command->getName(), 'environment' => 'test', 'release' => '20170101015115']); + $this->assertTrue(false, 'Command did not failed'); } catch (Exception $exception) { $this->assertTrue($exception instanceof DeploymentException); $this->assertEquals('Releases are not enabled', $exception->getMessage()); } } + + public function testRollbackReleaseNotAvailable() + { + $application = new MageApplicationMockup(); + $application->configure(__DIR__ . '/../../../Resources/testhost-not-have-release.yml'); + + /** @var AbstractCommand $command */ + $command = $application->find('releases:rollback'); + $this->assertTrue($command instanceof RollbackCommand); + + $tester = new CommandTester($command); + + try { + $tester->execute(['command' => $command->getName(), 'environment' => 'test', 'release' => '20170101015115']); + $this->assertTrue(false, 'Command did not failed'); + } catch (Exception $exception) { + $this->assertTrue($exception instanceof DeploymentException); + $this->assertEquals('Release "20170101015115" is not available on all hosts', $exception->getMessage()); + } + } } diff --git a/src/Mage/Tests/Resources/testhost-fail-copy-tar.yml b/src/Mage/Tests/Resources/testhost-fail-copy-tar.yml new file mode 100644 index 0000000..80809c7 --- /dev/null +++ b/src/Mage/Tests/Resources/testhost-fail-copy-tar.yml @@ -0,0 +1,14 @@ +magephp: + log_dir: /tmp + environments: + test: + user: tester + branch: test + releases: 4 + host_path: /var/www/test + exclude: + - ./var/cache/* + - ./var/log/* + - ./web/app_dev.php + hosts: + - host4 diff --git a/src/Mage/Tests/Resources/testhost-force-tar1.yml b/src/Mage/Tests/Resources/testhost-force-tar1.yml new file mode 100644 index 0000000..deca757 --- /dev/null +++ b/src/Mage/Tests/Resources/testhost-force-tar1.yml @@ -0,0 +1,15 @@ +magephp: + log_dir: /tmp + environments: + test: + user: tester + branch: test + host_path: /var/www/test + exclude: + - ./var/cache/* + - ./var/log/* + - ./web/app_dev.php + hosts: + - host2 + pre-deploy: + - deploy/targz/prepare \ No newline at end of file diff --git a/src/Mage/Tests/Resources/testhost-force-tar2.yml b/src/Mage/Tests/Resources/testhost-force-tar2.yml new file mode 100644 index 0000000..595a921 --- /dev/null +++ b/src/Mage/Tests/Resources/testhost-force-tar2.yml @@ -0,0 +1,15 @@ +magephp: + log_dir: /tmp + environments: + test: + user: tester + branch: test + host_path: /var/www/test + exclude: + - ./var/cache/* + - ./var/log/* + - ./web/app_dev.php + hosts: + - host2 + on-deploy: + - deploy/targz/copy \ No newline at end of file diff --git a/src/Mage/Tests/Resources/testhost-force-tar3.yml b/src/Mage/Tests/Resources/testhost-force-tar3.yml new file mode 100644 index 0000000..26f0c90 --- /dev/null +++ b/src/Mage/Tests/Resources/testhost-force-tar3.yml @@ -0,0 +1,15 @@ +magephp: + log_dir: /tmp + environments: + test: + user: tester + branch: test + host_path: /var/www/test + exclude: + - ./var/cache/* + - ./var/log/* + - ./web/app_dev.php + hosts: + - host2 + post-deploy: + - deploy/targz/cleanup \ No newline at end of file diff --git a/src/Mage/Tests/Resources/testhost-not-have-release.yml b/src/Mage/Tests/Resources/testhost-not-have-release.yml new file mode 100644 index 0000000..4a23a85 --- /dev/null +++ b/src/Mage/Tests/Resources/testhost-not-have-release.yml @@ -0,0 +1,16 @@ +magephp: + log_dir: /tmp + environments: + test: + user: tester + branch: test + host_path: /var/www/test + releases: 4 + exclude: + - ./var/cache/* + - ./var/log/* + - ./web/app_dev.php + hosts: + - hostdemo1 + - hostdemo2 + - hostdemo3 diff --git a/src/Mage/Tests/Runtime/ProcessMockup.php b/src/Mage/Tests/Runtime/ProcessMockup.php index 0ff7df6..ede851d 100644 --- a/src/Mage/Tests/Runtime/ProcessMockup.php +++ b/src/Mage/Tests/Runtime/ProcessMockup.php @@ -37,6 +37,14 @@ class ProcessMockup extends Process if ($this->commandline == 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@host3 sh -c \"ls -1 /var/www/test/releases\"') { $this->success = false; } + + if ($this->commandline == 'scp -P 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no /tmp/mageXYZ tester@host4:/var/www/test/releases/1234567890/mageXYZ') { + $this->success = false; + } + + if ($this->commandline == 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@hostdemo2 sh -c \"ls -1 /var/www/test/releases\"') { + $this->success = false; + } } public function isSuccessful() @@ -67,6 +75,14 @@ class ProcessMockup extends Process return implode(PHP_EOL, ['20170101015110', '20170101015111', '20170101015112', '20170101015113', '20170101015114', '20170101015115', '20170101015116', '20170101015117']); } + if ($this->commandline == 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@hostdemo1 sh -c \"ls -1 /var/www/test/releases\"') { + return implode(PHP_EOL, ['20170101015110', '20170101015111', '20170101015112', '20170101015113', '20170101015114', '20170101015115', '20170101015116', '20170101015117']); + } + + if ($this->commandline == 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@hostdemo3 sh -c \"ls -1 /var/www/test/releases\"') { + return implode(PHP_EOL, ['20170101015110', '20170101015111', '20170101015112', '20170101015113', '20170101015114', '20170101015116', '20170101015117']); + } + if ($this->commandline == 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@host2 sh -c \"ls -1 /var/www/test/releases\"') { return ''; }