[Nostromo] Add tests

This commit is contained in:
Andrés Montañez 2017-01-01 01:28:58 -03:00
parent 74c60f55ef
commit 196f84ceb1
16 changed files with 1957 additions and 17 deletions

View file

@ -20,6 +20,9 @@
"symfony/yaml": "^3.0",
"symfony/process": "^3.0"
},
"require-dev": {
"phpunit/phpunit": "5.5.*"
},
"autoload": {
"psr-4": {
"Mage\\": "src/Mage/"

1273
composer.lock generated

File diff suppressed because it is too large Load diff

37
phpunit.xml.dist Normal file
View file

@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.1/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="vendor/autoload.php"
>
<php>
<ini name="error_reporting" value="-1" />
<ini name="intl.default_locale" value="en" />
<ini name="intl.error_level" value="0" />
<ini name="memory_limit" value="-1" />
</php>
<testsuites>
<testsuite name="Magallanes Test Suite">
<directory>./src/Mage/Tests/</directory>
</testsuite>
</testsuites>
<groups>
<exclude>
<group>benchmark</group>
<group>intl-data</group>
</exclude>
</groups>
<filter>
<whitelist>
<directory>./src/Mage/</directory>
<exclude>
<directory>./src/Mage/Tests/</directory>
</exclude>
</whitelist>
</filter>
</phpunit>

View file

@ -72,7 +72,7 @@ class DeployCommand extends AbstractCommand
$this->log(sprintf('Environment: %s', $this->runtime->getEnvironment()));
if ($this->runtime->getEnvironmentConfig('releases', false)) {
$this->runtime->setReleaseId(date('YmdHis'));
$this->runtime->generateReleaseId();
$output->writeln(sprintf(' Release ID: <fg=green>%s</>', $this->runtime->getReleaseId()));
$this->log(sprintf('Release ID: %s', $this->runtime->getReleaseId()));
}

View file

@ -87,7 +87,7 @@ class ListCommand extends AbstractCommand
$cmdListReleases = sprintf('ls -1 %s/releases', $hostPath);
/** @var Process $process */
$process = $this->runtime->runRemoteCommand($cmdListReleases, true);
$process = $this->runtime->runRemoteCommand($cmdListReleases, false);
if (!$process->isSuccessful()) {
throw new RuntimeException(sprintf('Unable to retrieve releases from host %s', $host), 800);
}
@ -102,7 +102,7 @@ class ListCommand extends AbstractCommand
$cmdCurrentRelease = sprintf('readlink -f %s/current', $hostPath);
/** @var Process $process */
$process = $this->runtime->runRemoteCommand($cmdCurrentRelease, true);
$process = $this->runtime->runRemoteCommand($cmdCurrentRelease, false);
if (!$process->isSuccessful()) {
throw new RuntimeException(sprintf('Unable to retrieve current release from host %s', $host), 850);
}

View file

@ -118,7 +118,7 @@ class RollbackCommand extends DeployCommand
$cmdListReleases = sprintf('ls -1 %s/releases', $hostPath);
/** @var Process $process */
$process = $this->runtime->runRemoteCommand($cmdListReleases, true);
$process = $this->runtime->runRemoteCommand($cmdListReleases, false);
if (!$process->isSuccessful()) {
$releases = [];
} else {

View file

@ -62,6 +62,17 @@ class Runtime implements RuntimeInterface
*/
protected $rollback = false;
/**
* Generate the Release ID
*
* @return RuntimeInterface
*/
public function generateReleaseId()
{
$this->setReleaseId(date('YmdHis'));
return $this;
}
/**
* Sets the Release ID
*
@ -409,4 +420,14 @@ class Runtime implements RuntimeInterface
return $sshConfig;
}
/**
* Gets a Temporal File name
*
* @return string
*/
public function getTempFile()
{
return tempnam(sys_get_temp_dir(), 'mage');
}
}

View file

@ -28,6 +28,13 @@ interface RuntimeInterface
const ON_RELEASE = 'on-release';
const POST_RELEASE = 'post-release';
/**
* Generate the Release ID
*
* @return RuntimeInterface
*/
public function generateReleaseId();
/**
* Sets the Release ID
*
@ -217,4 +224,11 @@ interface RuntimeInterface
* @return array
*/
public function getSSHConfig();
/**
* Gets a Temporal File name
*
* @return string
*/
public function getTempFile();
}

View file

@ -37,7 +37,7 @@ class PrepareTask extends AbstractTask
throw new DeploymentException('This task is only available with releases enabled', 400);
}
$tarGzLocal = tempnam(sys_get_temp_dir(), 'mage');
$tarGzLocal = $this->runtime->getTempFile();
$this->runtime->setVar('targz_local', $tarGzLocal);
$excludes = $this->getExcludes();

View file

@ -0,0 +1,233 @@
<?php
namespace Mage\Tests\Command\BuiltIn;
use Mage\Command\BuiltIn\DeployCommand;
use Mage\Command\AbstractCommand;
use Mage\Tests\MageTestApplication;
use Mage\Tests\Runtime\RuntimeMockup;
use Symfony\Component\Console\Tester\CommandTester;
use PHPUnit\Framework\TestCase;
class DeployCommandTest extends TestCase
{
public function testDeploymentWithReleasesCommands()
{
$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 =>
array (
'symfony/cache-clear' =>
array (
'env' => 'dev',
),
),
1 =>
array (
'symfony/cache-warmup' =>
array (
'env' => 'dev',
),
),
2 =>
array (
'symfony/assets-install' =>
array (
'env' => 'dev',
),
),
3 =>
array (
'symfony/assetic-dump' =>
array (
'env' => 'dev',
),
),
),
'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\\"',
10 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"cd /var/www/test/releases/1234567890 \\&\\& bin/console cache:clear --env=dev \\"',
11 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"cd /var/www/test/releases/1234567890 \\&\\& bin/console cache:warmup --env=dev \\"',
12 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"cd /var/www/test/releases/1234567890 \\&\\& bin/console assets:install --env=dev --symlink --relative web\\"',
13 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"cd /var/www/test/releases/1234567890 \\&\\& bin/console assetic:dump --env=dev \\"',
14 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"cd /var/www/test \\&\\& ln -snf releases/1234567890 current\\"',
15 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"ls -1 /var/www/test/releases\\"',
16 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"rm -rf /var/www/test/releases/20170101015110\\"',
17 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"rm -rf /var/www/test/releases/20170101015111\\"',
18 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"rm -rf /var/www/test/releases/20170101015112\\"',
19 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"rm -rf /var/www/test/releases/20170101015113\\"',
20 => 'rm /tmp/mageXYZ',
21 => '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);
}
}
public function testDeploymentWithoutReleasesCommands()
{
$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 =>
array (
'symfony/cache-clear' =>
array (
'env' => 'dev',
),
),
1 =>
array (
'symfony/cache-warmup' =>
array (
'env' => 'dev',
),
),
2 =>
array (
'symfony/assets-install' =>
array (
'env' => 'dev',
),
),
3 =>
array (
'symfony/assetic-dump' =>
array (
'env' => 'dev',
),
),
),
'on-release' => NULL,
'post-release' => NULL,
'post-deploy' => NULL,
),
),
)
);
$runtime->setReleaseId('1234567890');
/** @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 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"cd /var/www/test/releases/1234567890 \\&\\& bin/console cache:clear --env=dev \\"',
7 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"cd /var/www/test/releases/1234567890 \\&\\& bin/console cache:warmup --env=dev \\"',
8 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"cd /var/www/test/releases/1234567890 \\&\\& bin/console assets:install --env=dev --symlink --relative web\\"',
9 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"cd /var/www/test/releases/1234567890 \\&\\& bin/console assetic:dump --env=dev \\"',
10 => '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);
}
}
}

View file

@ -0,0 +1,107 @@
<?php
namespace Mage\Tests\Command\BuiltIn\Releases;
use Mage\Command\BuiltIn\Releases\ListCommand;
use Mage\Command\AbstractCommand;
use Mage\Tests\MageTestApplication;
use Mage\Tests\Runtime\RuntimeMockup;
use Symfony\Component\Console\Tester\CommandTester;
use PHPUnit\Framework\TestCase;
class ListCommandTest extends TestCase
{
public function testListReleasesCommands()
{
$application = new MageTestApplication();
$application->add(new ListCommand());
$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 =>
array (
'symfony/cache-clear' =>
array (
'env' => 'dev',
),
),
1 =>
array (
'symfony/cache-warmup' =>
array (
'env' => 'dev',
),
),
2 =>
array (
'symfony/assets-install' =>
array (
'env' => 'dev',
),
),
3 =>
array (
'symfony/assetic-dump' =>
array (
'env' => 'dev',
),
),
),
'on-release' => NULL,
'post-release' => NULL,
'post-deploy' => NULL,
),
),
)
);
/** @var AbstractCommand $command */
$command = $application->find('releases:list');
$command->setRuntime($runtime);
$tester = new CommandTester($command);
$tester->execute(['command' => $command->getName(), 'environment' => 'test']);
$ranCommands = $runtime->getRanCommands();
$testCase = array (
0 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"ls -1 /var/www/test/releases\\"',
1 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"readlink -f /var/www/test/current\\"',
);
// Check total of Executed Commands
$this->assertEquals(count($ranCommands), count($testCase));
// Check Generated Commands
foreach ($testCase as $index => $command) {
$this->assertEquals($ranCommands[$index], $command);
}
}
}

View file

@ -0,0 +1,107 @@
<?php
namespace Mage\Tests\Command\BuiltIn\Releases;
use Mage\Command\BuiltIn\Releases\RollbackCommand;
use Mage\Command\AbstractCommand;
use Mage\Tests\MageTestApplication;
use Mage\Tests\Runtime\RuntimeMockup;
use Symfony\Component\Console\Tester\CommandTester;
use PHPUnit\Framework\TestCase;
class RollbackCommandTest extends TestCase
{
public function testRollbackReleaseCommands()
{
$application = new MageTestApplication();
$application->add(new RollbackCommand());
$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 =>
array (
'symfony/cache-clear' =>
array (
'env' => 'dev',
),
),
1 =>
array (
'symfony/cache-warmup' =>
array (
'env' => 'dev',
),
),
2 =>
array (
'symfony/assets-install' =>
array (
'env' => 'dev',
),
),
3 =>
array (
'symfony/assetic-dump' =>
array (
'env' => 'dev',
),
),
),
'on-release' => NULL,
'post-release' => NULL,
'post-deploy' => NULL,
),
),
)
);
/** @var AbstractCommand $command */
$command = $application->find('releases:rollback');
$command->setRuntime($runtime);
$tester = new CommandTester($command);
$tester->execute(['command' => $command->getName(), 'environment' => 'test', 'release' => '20170101015115']);
$ranCommands = $runtime->getRanCommands();
$testCase = array (
0 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"ls -1 /var/www/test/releases\\"',
1 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"cd /var/www/test \\&\\& ln -snf releases/20170101015115 current\\"',
);
// Check total of Executed Commands
$this->assertEquals(count($ranCommands), count($testCase));
// Check Generated Commands
foreach ($testCase as $index => $command) {
$this->assertEquals($ranCommands[$index], $command);
}
}
}

View file

@ -0,0 +1,30 @@
<?php
namespace Mage\Tests\Command\BuiltIn;
use Mage\Command\BuiltIn\VersionCommand;
use Mage\Command\AbstractCommand;
use Mage\Tests\MageTestApplication;
use Mage\Tests\Runtime\RuntimeMockup;
use Mage\Mage;
use Symfony\Component\Console\Tester\CommandTester;
use PHPUnit\Framework\TestCase;
class VersionCommandTest extends TestCase
{
public function testVersionOutput()
{
$application = new MageTestApplication();
$application->add(new VersionCommand());
/** @var AbstractCommand $command */
$command = $application->find('version');
$command->setRuntime(new RuntimeMockup());
$tester = new CommandTester($command);
$tester->execute(['command' => $command->getName()]);
$output = trim($tester->getDisplay());
$this->assertEquals(sprintf('Magallanes v%s [%s]', Mage::VERSION, Mage::CODENAME), $output);
}
}

View file

@ -0,0 +1,32 @@
<?php
/*
* This file is part of the Magallanes package.
*
* (c) Andrés Montañez <andres@andresmontanez.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mage\Tests;
use Mage\Command\AbstractCommand;
use Mage\Runtime\Runtime;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Symfony\Component\Console\Application;
use Symfony\Component\Yaml\Yaml;
use Mage\Runtime\Exception\RuntimeException;
/**
* The Console Application for launching the Mage command in a standalone instance
*
* @author Andrés Montañez <andresmontanez@gmail.com>
*/
class MageTestApplication extends Application
{
}

View file

@ -0,0 +1,52 @@
<?php
namespace Mage\Tests\Runtime;
use Symfony\Component\Process\Process;
class ProcessMockup extends Process
{
protected $commandline;
protected $timeout;
public function __construct($commandline, $cwd = null, array $env = null, $input = null, $timeout = 60, array $options = array())
{
$this->commandline = $commandline;
}
public function setTimeout($timeout)
{
$this->timeout = $timeout;
}
public function run($callback = null)
{
}
public function isSuccessful()
{
return true;
}
public function getErrorOutput()
{
return '';
}
public function getOutput()
{
if ($this->commandline == 'git branch | grep "*"') {
return '* master';
}
if ($this->commandline == 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost 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@testhost sh -c \"readlink -f /var/www/test/current\"') {
return '/var/www/test/releases/20170101015120';
}
return '';
}
}

View file

@ -0,0 +1,55 @@
<?php
namespace Mage\Tests\Runtime;
use Mage\Runtime\Runtime;
use Mage\Runtime\RuntimeInterface;
use Symfony\Component\Process\Process;
class RuntimeMockup extends Runtime
{
protected $ranCommands = [];
public function getRanCommands()
{
return $this->ranCommands;
}
/**
* Generate the Release ID
*
* @return RuntimeInterface
*/
public function generateReleaseId()
{
$this->setReleaseId('1234567890');
return $this;
}
/**
* Execute a command locally
*
* @param string $cmd Command to execute
* @param int $timeout Seconds to wait
* @return Process
*/
public function runLocalCommand($cmd, $timeout = 120)
{
$this->ranCommands[] = $cmd;
$process = new ProcessMockup($cmd);
$process->setTimeout($timeout);
$process->run();
return $process;
}
/**
* Gets a Temporal File name
*
* @return string
*/
public function getTempFile()
{
return '/tmp/mageXYZ';
}
}