2017-01-01 05:28:58 +01:00
|
|
|
<?php
|
2017-01-01 22:20:43 +01:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2017-01-01 05:28:58 +01:00
|
|
|
namespace Mage\Tests\Command\BuiltIn\Releases;
|
|
|
|
|
|
|
|
use Mage\Command\BuiltIn\Releases\ListCommand;
|
2017-01-05 02:21:38 +01:00
|
|
|
use Mage\Runtime\Exception\DeploymentException;
|
2017-01-06 01:22:38 +01:00
|
|
|
use Mage\Runtime\Exception\RuntimeException;
|
2017-01-01 05:28:58 +01:00
|
|
|
use Mage\Command\AbstractCommand;
|
2017-01-05 01:46:40 +01:00
|
|
|
use Mage\Tests\MageApplicationMockup;
|
2017-01-01 05:28:58 +01:00
|
|
|
use Symfony\Component\Console\Tester\CommandTester;
|
2017-01-05 02:21:38 +01:00
|
|
|
use Exception;
|
2017-01-01 06:14:17 +01:00
|
|
|
use PHPUnit_Framework_TestCase as TestCase;
|
2017-01-01 05:28:58 +01:00
|
|
|
|
|
|
|
class ListCommandTest extends TestCase
|
|
|
|
{
|
|
|
|
public function testListReleasesCommands()
|
|
|
|
{
|
2017-01-05 01:46:40 +01:00
|
|
|
$application = new MageApplicationMockup();
|
|
|
|
$application->configure(__DIR__ . '/../../../Resources/testhost.yml');
|
2017-01-01 05:28:58 +01:00
|
|
|
|
|
|
|
/** @var AbstractCommand $command */
|
|
|
|
$command = $application->find('releases:list');
|
2017-01-05 01:46:40 +01:00
|
|
|
$this->assertTrue($command instanceof ListCommand);
|
2017-01-01 05:28:58 +01:00
|
|
|
|
|
|
|
$tester = new CommandTester($command);
|
|
|
|
$tester->execute(['command' => $command->getName(), 'environment' => 'test']);
|
|
|
|
|
2017-01-05 01:46:40 +01:00
|
|
|
$ranCommands = $application->getRuntime()->getRanCommands();
|
2017-01-01 05:28:58 +01:00
|
|
|
|
2017-01-01 05:34:14 +01:00
|
|
|
$testCase = array(
|
2017-01-01 05:28:58 +01:00
|
|
|
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
|
2017-01-01 22:20:43 +01:00
|
|
|
$this->assertEquals(count($testCase), count($ranCommands));
|
2017-01-01 05:28:58 +01:00
|
|
|
|
|
|
|
// Check Generated Commands
|
|
|
|
foreach ($testCase as $index => $command) {
|
2017-01-01 22:20:43 +01:00
|
|
|
$this->assertEquals($command, $ranCommands[$index]);
|
2017-01-01 05:28:58 +01:00
|
|
|
}
|
|
|
|
}
|
2017-01-05 02:21:38 +01:00
|
|
|
|
|
|
|
public function testListReleasesWithInvalidEnvironment()
|
|
|
|
{
|
|
|
|
$application = new MageApplicationMockup();
|
|
|
|
$application->configure(__DIR__ . '/../../../Resources/testhost.yml');
|
|
|
|
|
|
|
|
/** @var AbstractCommand $command */
|
|
|
|
$command = $application->find('releases:list');
|
|
|
|
$this->assertTrue($command instanceof ListCommand);
|
|
|
|
|
|
|
|
$tester = new CommandTester($command);
|
|
|
|
$tester->execute(['command' => $command->getName(), 'environment' => 'developers']);
|
|
|
|
|
|
|
|
$this->assertNotEquals(0, $tester->getStatusCode());
|
|
|
|
$this->assertContains('The environment "developers" does not exists.', $tester->getDisplay());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testListReleasesWithoutReleases()
|
|
|
|
{
|
|
|
|
$application = new MageApplicationMockup();
|
|
|
|
$application->configure(__DIR__ . '/../../../Resources/testhost-without-releases.yml');
|
|
|
|
|
|
|
|
/** @var AbstractCommand $command */
|
|
|
|
$command = $application->find('releases:list');
|
|
|
|
$this->assertTrue($command instanceof ListCommand);
|
|
|
|
|
|
|
|
$tester = new CommandTester($command);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$tester->execute(['command' => $command->getName(), 'environment' => 'test']);
|
|
|
|
} catch (Exception $exception) {
|
|
|
|
$this->assertTrue($exception instanceof DeploymentException);
|
|
|
|
$this->assertEquals('Releases are not enabled', $exception->getMessage());
|
|
|
|
}
|
|
|
|
}
|
2017-01-06 01:22:38 +01:00
|
|
|
|
|
|
|
public function testFailToGetCurrentRelease()
|
|
|
|
{
|
|
|
|
$application = new MageApplicationMockup();
|
|
|
|
$application->configure(__DIR__ . '/../../../Resources/testhost-fail-get-current.yml');
|
|
|
|
|
|
|
|
/** @var AbstractCommand $command */
|
|
|
|
$command = $application->find('releases:list');
|
|
|
|
$this->assertTrue($command instanceof ListCommand);
|
|
|
|
|
|
|
|
$tester = new CommandTester($command);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$tester->execute(['command' => $command->getName(), 'environment' => 'test']);
|
|
|
|
} catch (Exception $exception) {
|
|
|
|
$this->assertTrue($exception instanceof RuntimeException);
|
|
|
|
$this->assertEquals('Unable to retrieve current release from host "host1"', $exception->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testNoReleasesAvailable()
|
|
|
|
{
|
|
|
|
$application = new MageApplicationMockup();
|
|
|
|
$application->configure(__DIR__ . '/../../../Resources/testhost-no-releases.yml');
|
|
|
|
|
|
|
|
/** @var AbstractCommand $command */
|
|
|
|
$command = $application->find('releases:list');
|
|
|
|
$this->assertTrue($command instanceof ListCommand);
|
|
|
|
|
|
|
|
$tester = new CommandTester($command);
|
|
|
|
|
|
|
|
$tester->execute(['command' => $command->getName(), 'environment' => 'test']);
|
|
|
|
$this->assertContains('No releases available on host host2', $tester->getDisplay());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFailGetReleases()
|
|
|
|
{
|
|
|
|
$application = new MageApplicationMockup();
|
|
|
|
$application->configure(__DIR__ . '/../../../Resources/testhost-fail-get-releases.yml');
|
|
|
|
|
|
|
|
/** @var AbstractCommand $command */
|
|
|
|
$command = $application->find('releases:list');
|
|
|
|
$this->assertTrue($command instanceof ListCommand);
|
|
|
|
|
|
|
|
$tester = new CommandTester($command);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$tester->execute(['command' => $command->getName(), 'environment' => 'test']);
|
|
|
|
} catch (Exception $exception) {
|
|
|
|
$this->assertTrue($exception instanceof RuntimeException);
|
|
|
|
$this->assertEquals('Unable to retrieve releases from host "host3"', $exception->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testNoHosts()
|
|
|
|
{
|
|
|
|
$application = new MageApplicationMockup();
|
|
|
|
$application->configure(__DIR__ . '/../../../Resources/testhost-no-hosts.yml');
|
|
|
|
|
|
|
|
/** @var AbstractCommand $command */
|
|
|
|
$command = $application->find('releases:list');
|
|
|
|
$this->assertTrue($command instanceof ListCommand);
|
|
|
|
|
|
|
|
$tester = new CommandTester($command);
|
|
|
|
|
|
|
|
$tester->execute(['command' => $command->getName(), 'environment' => 'test']);
|
|
|
|
$this->assertContains('No hosts defined', $tester->getDisplay());
|
|
|
|
}
|
2017-01-01 05:34:14 +01:00
|
|
|
}
|