From 373ce5d761002ce40ba9f152a1902cf54574fa40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Monta=C3=B1ez?= Date: Sat, 20 Feb 2021 17:16:24 -0300 Subject: [PATCH] Fixes for v4.1.1 --- CHANGELOG.md | 6 + src/Mage.php | 2 +- src/Runtime/Runtime.php | 5 + src/Task/BuiltIn/Deploy/Tar/PrepareTask.php | 8 +- .../BuiltIn/DeployCommandWithReleasesTest.php | 103 +++++++++++++++++- tests/MageApplicationWindowsMockup.php | 27 +++++ .../testhost-with-from-copy-directory.yml | 29 +++++ tests/Runtime/RuntimeWindowsMockup.php | 19 ++++ 8 files changed, 193 insertions(+), 6 deletions(-) create mode 100644 tests/MageApplicationWindowsMockup.php create mode 100644 tests/Resources/testhost-with-from-copy-directory.yml create mode 100644 tests/Runtime/RuntimeWindowsMockup.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 97b3264..704d048 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,12 @@ CHANGELOG for 4.X ================= +* 4.1.1 (2021-02-20) + * Add `copyDirectory` option + * Bug fixes + * Improve testing and coverage + + * 4.1.0 (2021-02-19) * PHP 8 and Symfony 5 compatibility [PR#448] * Timeout option for SSH [PR#436] diff --git a/src/Mage.php b/src/Mage.php index de09bfd..1d4e641 100644 --- a/src/Mage.php +++ b/src/Mage.php @@ -17,6 +17,6 @@ namespace Mage; */ class Mage { - const VERSION = '4.1.0'; + const VERSION = '4.1.1'; const CODENAME = 'Discovery One'; } diff --git a/src/Runtime/Runtime.php b/src/Runtime/Runtime.php index 95dd8c1..e1f026a 100755 --- a/src/Runtime/Runtime.php +++ b/src/Runtime/Runtime.php @@ -71,6 +71,11 @@ class Runtime */ protected $rollback = false; + public function isWindows() + { + return stripos(PHP_OS, 'WIN') === 0; + } + /** * Generate the Release ID * diff --git a/src/Task/BuiltIn/Deploy/Tar/PrepareTask.php b/src/Task/BuiltIn/Deploy/Tar/PrepareTask.php index 642fddc..e4c5daa 100644 --- a/src/Task/BuiltIn/Deploy/Tar/PrepareTask.php +++ b/src/Task/BuiltIn/Deploy/Tar/PrepareTask.php @@ -42,11 +42,11 @@ class PrepareTask extends AbstractTask $excludes = $this->getExcludes(); $tarPath = $this->runtime->getEnvOption('tar_create_path', 'tar'); - $flags = $this->runtime->getEnvOption('tar_create', stripos(PHP_OS, 'WIN') === 0 ? '--force-local -c -z -p -f' : 'cfzp'); - $from = './'; + $flags = $this->runtime->getEnvOption('tar_create', $this->runtime->isWindows() ? '--force-local -c -z -p -f' : 'cfzp'); + $from = $this->runtime->getEnvOption('from', './'); - if ($fromPath = $this->runtime->getEnvOption('from', false)) { - $from = sprintf('-C %s %s', $fromPath, $from); + if ($this->runtime->getEnvOption('copyDirectory', false)) { + $from = sprintf('-C %s ./', $from); } $cmdTar = sprintf('%s %s %s %s %s', $tarPath, $flags, $tarLocal, $excludes, $from); diff --git a/tests/Command/BuiltIn/DeployCommandWithReleasesTest.php b/tests/Command/BuiltIn/DeployCommandWithReleasesTest.php index 3bb7e33..7cb7cd9 100755 --- a/tests/Command/BuiltIn/DeployCommandWithReleasesTest.php +++ b/tests/Command/BuiltIn/DeployCommandWithReleasesTest.php @@ -13,6 +13,7 @@ namespace Mage\Tests\Command\BuiltIn; use Mage\Command\BuiltIn\DeployCommand; use Mage\Command\AbstractCommand; use Mage\Tests\MageApplicationMockup; +use Mage\Tests\MageApplicationWindowsMockup; use Symfony\Component\Console\Tester\CommandTester; use PHPUnit\Framework\TestCase; @@ -133,6 +134,106 @@ class DeployCommandWithReleasesTest extends TestCase $ranCommands = $application->getRuntime()->getRanCommands(); + $testCase = array( + 0 => 'git branch | grep "*"', + 1 => 'git checkout test', + 2 => 'git pull', + 3 => 'composer install --optimize-autoloader', + 4 => 'composer dump-autoload --optimize', + 5 => 'tar cfzp /tmp/mageXYZ --exclude=".git" --exclude="./var/cache/*" --exclude="./var/log/*" --exclude="./web/app_dev.php" ./dist', + 6 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "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 "cd /var/www/test/releases/1234567890 && tar xfzop mageXYZ"', + 9 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "rm /var/www/test/releases/1234567890/mageXYZ"', + 10 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "cd /var/www/test/releases/1234567890 && bin/console cache:warmup --env=dev"', + 11 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "cd /var/www/test/releases/1234567890 && bin/console assets:install web --env=dev --symlink --relative"', + 12 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "cd /var/www/test/releases/1234567890 && bin/console cache:pool:prune --env=dev"', + 13 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "cd /var/www/test && ln -snf releases/1234567890 current"', + 14 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "ls -1 /var/www/test/releases"', + 15 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "rm -rf /var/www/test/releases/20170101015110"', + 16 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "rm -rf /var/www/test/releases/20170101015111"', + 17 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "rm -rf /var/www/test/releases/20170101015112"', + 18 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "rm -rf /var/www/test/releases/20170101015113"', + 19 => 'rm /tmp/mageXYZ', + 20 => 'git checkout master', + ); + + // Check total of Executed Commands + $this->assertEquals(count($testCase), count($ranCommands)); + + // Check Generated Commands + foreach ($testCase as $index => $command) { + $this->assertEquals($command, $ranCommands[$index]); + } + + $this->assertEquals(0, $tester->getStatusCode()); + } + + public function testDeploymentWithReleasesWithFromCommandsOnWindows() + { + $application = new MageApplicationWindowsMockup(__DIR__ . '/../../Resources/testhost-with-from.yml'); + + $application->getRuntime()->setReleaseId('20170101015120'); + + /** @var AbstractCommand $command */ + $command = $application->find('deploy'); + $this->assertTrue($command instanceof DeployCommand); + + $tester = new CommandTester($command); + $tester->execute(['command' => $command->getName(), 'environment' => 'test']); + + $ranCommands = $application->getRuntime()->getRanCommands(); + + $testCase = array( + 0 => 'git branch | grep "*"', + 1 => 'git checkout test', + 2 => 'git pull', + 3 => 'composer install --optimize-autoloader', + 4 => 'composer dump-autoload --optimize', + 5 => 'tar --force-local -c -z -p -f /tmp/mageXYZ --exclude=".git" --exclude="./var/cache/*" --exclude="./var/log/*" --exclude="./web/app_dev.php" ./dist', + 6 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "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 "cd /var/www/test/releases/1234567890 && tar xfzop mageXYZ"', + 9 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "rm /var/www/test/releases/1234567890/mageXYZ"', + 10 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "cd /var/www/test/releases/1234567890 && bin/console cache:warmup --env=dev"', + 11 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "cd /var/www/test/releases/1234567890 && bin/console assets:install web --env=dev --symlink --relative"', + 12 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "cd /var/www/test/releases/1234567890 && bin/console cache:pool:prune --env=dev"', + 13 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "cd /var/www/test && ln -snf releases/1234567890 current"', + 14 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "ls -1 /var/www/test/releases"', + 15 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "rm -rf /var/www/test/releases/20170101015110"', + 16 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "rm -rf /var/www/test/releases/20170101015111"', + 17 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "rm -rf /var/www/test/releases/20170101015112"', + 18 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "rm -rf /var/www/test/releases/20170101015113"', + 19 => 'rm /tmp/mageXYZ', + 20 => 'git checkout master', + ); + + // Check total of Executed Commands + $this->assertEquals(count($testCase), count($ranCommands)); + + // Check Generated Commands + foreach ($testCase as $index => $command) { + $this->assertEquals($command, $ranCommands[$index]); + } + + $this->assertEquals(0, $tester->getStatusCode()); + } + + public function testDeploymentWithReleasesWithFromCommandsWithDirectoryCopy() + { + $application = new MageApplicationMockup(__DIR__ . '/../../Resources/testhost-with-from-copy-directory.yml'); + + $application->getRuntime()->setReleaseId('20170101015120'); + + /** @var AbstractCommand $command */ + $command = $application->find('deploy'); + $this->assertTrue($command instanceof DeployCommand); + + $tester = new CommandTester($command); + $tester->execute(['command' => $command->getName(), 'environment' => 'test']); + + $ranCommands = $application->getRuntime()->getRanCommands(); + $testCase = array( 0 => 'git branch | grep "*"', 1 => 'git checkout test', @@ -189,7 +290,7 @@ class DeployCommandWithReleasesTest extends TestCase 2 => 'git pull', 3 => 'composer install --optimize-autoloader', 4 => 'composer dump-autoload --optimize', - 5 => 'tar cfzp /tmp/mageXYZ --exclude=".git" --exclude="./var/cache/*" --exclude="./var/log/*" --exclude="./web/app_dev.php" -C ./ ./', + 5 => 'tar cfzp /tmp/mageXYZ --exclude=".git" --exclude="./var/cache/*" --exclude="./var/log/*" --exclude="./web/app_dev.php" ./', 6 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "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 "cd /var/www/test/releases/1234567890 && tar xfzop mageXYZ"', diff --git a/tests/MageApplicationWindowsMockup.php b/tests/MageApplicationWindowsMockup.php new file mode 100644 index 0000000..a1845ee --- /dev/null +++ b/tests/MageApplicationWindowsMockup.php @@ -0,0 +1,27 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Mage\Tests; + +use Mage\Tests\Runtime\RuntimeWindowsMockup; +use Mage\MageApplication; + +class MageApplicationWindowsMockup extends MageApplication +{ + /** + * Gets the Runtime instance to use + * + * @return RuntimeWindowsMockup + */ + protected function instantiateRuntime() + { + return new RuntimeWindowsMockup(); + } +} diff --git a/tests/Resources/testhost-with-from-copy-directory.yml b/tests/Resources/testhost-with-from-copy-directory.yml new file mode 100644 index 0000000..4ef8d17 --- /dev/null +++ b/tests/Resources/testhost-with-from-copy-directory.yml @@ -0,0 +1,29 @@ +magephp: + log_dir: /tmp + environments: + test: + user: tester + branch: test + from: ./dist + copyDirectory: true + host_path: /var/www/test + releases: 4 + exclude: + - ./var/cache/* + - ./var/log/* + - ./web/app_dev.php + - + - + hosts: + - testhost + pre-deploy: + - git/update + - composer/install + - composer/dump-autoload + on-deploy: + - symfony/cache-warmup: { env: 'dev' } + - symfony/assets-install: { env: 'dev' } + - symfony/cache-pool-prune: { env: 'dev' } + on-release: + post-release: + post-deploy: \ No newline at end of file diff --git a/tests/Runtime/RuntimeWindowsMockup.php b/tests/Runtime/RuntimeWindowsMockup.php new file mode 100644 index 0000000..805031f --- /dev/null +++ b/tests/Runtime/RuntimeWindowsMockup.php @@ -0,0 +1,19 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Mage\Tests\Runtime; + +class RuntimeWindowsMockup extends RuntimeMockup +{ + public function isWindows() + { + return true; + } +}