mirror of
https://github.com/hauke68/Magallanes.git
synced 2026-08-29 18:48:56 +02:00
Change structure.
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
<?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\Runtime;
|
||||
|
||||
use Symfony\Component\Process\Process;
|
||||
|
||||
class ProcessMockup extends Process
|
||||
{
|
||||
public $forceFail = [];
|
||||
protected $commandline;
|
||||
protected $timeout;
|
||||
protected $success = true;
|
||||
|
||||
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)
|
||||
{
|
||||
if (in_array($this->commandline, $this->forceFail)) {
|
||||
$this->success = false;
|
||||
}
|
||||
|
||||
if ($this->commandline == 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@host1 sh -c \"readlink -f /var/www/test/current\"') {
|
||||
$this->success = false;
|
||||
}
|
||||
|
||||
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()
|
||||
{
|
||||
return $this->success;
|
||||
}
|
||||
|
||||
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/20170101015117';
|
||||
}
|
||||
|
||||
if ($this->commandline == 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@host1 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@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 '';
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
<?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\Runtime;
|
||||
|
||||
use Mage\Runtime\Runtime;
|
||||
use Symfony\Component\Process\Process;
|
||||
|
||||
class RuntimeMockup extends Runtime
|
||||
{
|
||||
protected $ranCommands = [];
|
||||
protected $forceFail = [];
|
||||
|
||||
public function getRanCommands()
|
||||
{
|
||||
return $this->ranCommands;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate the Release ID
|
||||
*
|
||||
* @return Runtime
|
||||
*/
|
||||
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->forceFail = $this->forceFail;
|
||||
$process->setTimeout($timeout);
|
||||
$process->run();
|
||||
|
||||
return $process;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a Temporal File name
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getTempFile()
|
||||
{
|
||||
return '/tmp/mageXYZ';
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows to set an invalid environments
|
||||
*
|
||||
* @param string $environment
|
||||
* @return Runtime
|
||||
*/
|
||||
public function setInvalidEnvironment($environment)
|
||||
{
|
||||
$this->environment = $environment;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function forceFail($cmd)
|
||||
{
|
||||
$this->forceFail[] = $cmd;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,178 @@
|
||||
<?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\Runtime;
|
||||
|
||||
use Mage\Runtime\Exception\RuntimeException;
|
||||
use Mage\Runtime\Runtime;
|
||||
use Exception;
|
||||
use Monolog\Logger;
|
||||
use Monolog\Handler\TestHandler;
|
||||
use PHPUnit_Framework_TestCase as TestCase;
|
||||
use Psr\Log\LogLevel;
|
||||
use Symfony\Component\Process\Process;
|
||||
use DateTime;
|
||||
|
||||
class RuntimeTest extends TestCase
|
||||
{
|
||||
public function testReleaseIdGeneration()
|
||||
{
|
||||
// Given that this is a time based operation, lets conform that at least the format is right
|
||||
// and the time diff is less than 2 seconds
|
||||
|
||||
$now = new DateTime();
|
||||
|
||||
$runtime = new Runtime();
|
||||
$runtime->generateReleaseId();
|
||||
$releaseId = $runtime->getReleaseId();
|
||||
|
||||
$releaseDate = DateTime::createFromFormat('YmdHis', $releaseId);
|
||||
$this->assertTrue($releaseDate instanceof DateTime);
|
||||
|
||||
$dateDiff = $releaseDate->diff($now);
|
||||
$this->assertLessThanOrEqual(2, $dateDiff->s);
|
||||
}
|
||||
|
||||
public function testEmptyEnvironmentConfig()
|
||||
{
|
||||
$runtime = new Runtime();
|
||||
$config = $runtime->getEnvironmentConfig();
|
||||
|
||||
$this->assertTrue(is_array($config));
|
||||
$this->assertEquals(0, count($config));
|
||||
}
|
||||
|
||||
public function testInvalidEnvironmentConfig()
|
||||
{
|
||||
$runtime = new RuntimeMockup();
|
||||
$runtime->setConfiguration(['environments' => ['valid' => []]]);
|
||||
$runtime->setInvalidEnvironment('invalid');
|
||||
$config = $runtime->getEnvironmentConfig();
|
||||
|
||||
$this->assertTrue(is_array($config));
|
||||
$this->assertEquals(0, count($config));
|
||||
}
|
||||
|
||||
public function testInvalidEnvironments()
|
||||
{
|
||||
try {
|
||||
$runtime = new Runtime();
|
||||
$runtime->setEnvironment('invalid');
|
||||
} catch (Exception $exception) {
|
||||
$this->assertTrue($exception instanceof RuntimeException);
|
||||
}
|
||||
|
||||
try {
|
||||
$runtime = new Runtime();
|
||||
$runtime->setConfiguration(['environments' => ['valid' => []]]);
|
||||
$runtime->setEnvironment('invalid');
|
||||
} catch (Exception $exception) {
|
||||
$this->assertTrue($exception instanceof RuntimeException);
|
||||
}
|
||||
}
|
||||
|
||||
public function testTempFile()
|
||||
{
|
||||
$runtime = new Runtime();
|
||||
$tempFile = $runtime->getTempFile();
|
||||
|
||||
$this->assertNotEquals('', $tempFile);
|
||||
$this->assertTrue(file_exists($tempFile));
|
||||
$this->assertTrue(is_readable($tempFile));
|
||||
$this->assertTrue(is_writable($tempFile));
|
||||
$this->assertEquals(0, filesize($tempFile));
|
||||
}
|
||||
|
||||
public function testSSHConfigUndefinedOptions()
|
||||
{
|
||||
$runtime = new Runtime();
|
||||
$sshConfig = $runtime->getSSHConfig();
|
||||
|
||||
$this->assertTrue(is_array($sshConfig));
|
||||
|
||||
$this->assertTrue(array_key_exists('port', $sshConfig));
|
||||
$this->assertEquals('22', $sshConfig['port']);
|
||||
|
||||
$this->assertTrue(array_key_exists('flags', $sshConfig));
|
||||
$this->assertEquals('-q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no', $sshConfig['flags']);
|
||||
}
|
||||
|
||||
public function testSSHConfigEmptyOptions()
|
||||
{
|
||||
$runtime = new Runtime();
|
||||
$runtime->setConfiguration(['environments' => ['test' => ['ssh' => []]]]);
|
||||
$runtime->setEnvironment('test');
|
||||
$sshConfig = $runtime->getSSHConfig();
|
||||
|
||||
$this->assertTrue(is_array($sshConfig));
|
||||
|
||||
$this->assertTrue(array_key_exists('port', $sshConfig));
|
||||
$this->assertEquals('22', $sshConfig['port']);
|
||||
|
||||
$this->assertTrue(array_key_exists('flags', $sshConfig));
|
||||
$this->assertEquals('-q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no', $sshConfig['flags']);
|
||||
}
|
||||
|
||||
public function testSSHConfigCustomOptions()
|
||||
{
|
||||
$runtime = new Runtime();
|
||||
$runtime->setConfiguration(['environments' => ['test' => ['ssh' => ['port' => '2222', 'flags' => '-q']]]]);
|
||||
$runtime->setEnvironment('test');
|
||||
$sshConfig = $runtime->getSSHConfig();
|
||||
|
||||
$this->assertTrue(is_array($sshConfig));
|
||||
|
||||
$this->assertTrue(array_key_exists('port', $sshConfig));
|
||||
$this->assertEquals('2222', $sshConfig['port']);
|
||||
|
||||
$this->assertTrue(array_key_exists('flags', $sshConfig));
|
||||
$this->assertEquals('-q', $sshConfig['flags']);
|
||||
}
|
||||
|
||||
public function testLogger()
|
||||
{
|
||||
$logger = new Logger('test');
|
||||
$handler = new TestHandler();
|
||||
$logger->pushHandler($handler);
|
||||
|
||||
$runtime = new Runtime();
|
||||
$runtime->setLogger($logger);
|
||||
|
||||
$runtime->log('Test Message', LogLevel::INFO);
|
||||
|
||||
$this->assertTrue($handler->hasInfoRecords());
|
||||
$this->assertTrue($handler->hasInfo('Test Message'));
|
||||
}
|
||||
|
||||
public function testLocalCommand()
|
||||
{
|
||||
$runtime = new Runtime();
|
||||
|
||||
/** @var Process $process */
|
||||
$process = $runtime->runLocalCommand('date +%s');
|
||||
$timestamp = time();
|
||||
$this->assertTrue($process->isSuccessful());
|
||||
$this->assertEquals($timestamp, trim($process->getOutput()));
|
||||
|
||||
/** @var Process $process */
|
||||
$process = $runtime->runLocalCommand('false');
|
||||
$this->assertFalse($process->isSuccessful());
|
||||
}
|
||||
|
||||
public function testCurrentUser()
|
||||
{
|
||||
$runtime = new Runtime();
|
||||
$userData = posix_getpwuid(posix_geteuid());
|
||||
|
||||
$this->assertTrue(is_array($userData));
|
||||
$this->assertArrayHasKey('name', $userData);
|
||||
$this->assertEquals($userData['name'], $runtime->getCurrentUser());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user