mirror of https://github.com/hauke68/Magallanes
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
179 lines
5.6 KiB
179 lines
5.6 KiB
8 years ago
|
<?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;
|
||
|
|
||
8 years ago
|
use Mage\Runtime\Exception\RuntimeException;
|
||
8 years ago
|
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;
|
||
8 years ago
|
use DateTime;
|
||
8 years ago
|
|
||
|
class RuntimeTest extends TestCase
|
||
|
{
|
||
|
public function testReleaseIdGeneration()
|
||
|
{
|
||
8 years ago
|
// 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();
|
||
8 years ago
|
|
||
|
$runtime = new Runtime();
|
||
|
$runtime->generateReleaseId();
|
||
8 years ago
|
$releaseId = $runtime->getReleaseId();
|
||
|
|
||
|
$releaseDate = DateTime::createFromFormat('YmdHis', $releaseId);
|
||
|
$this->assertTrue($releaseDate instanceof DateTime);
|
||
8 years ago
|
|
||
8 years ago
|
$dateDiff = $releaseDate->diff($now);
|
||
|
$this->assertLessThanOrEqual(2, $dateDiff->s);
|
||
8 years ago
|
}
|
||
|
|
||
|
public function testEmptyEnvironmentConfig()
|
||
|
{
|
||
|
$runtime = new Runtime();
|
||
|
$config = $runtime->getEnvironmentConfig();
|
||
|
|
||
|
$this->assertTrue(is_array($config));
|
||
|
$this->assertEquals(0, count($config));
|
||
|
}
|
||
|
|
||
8 years ago
|
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));
|
||
|
}
|
||
|
|
||
8 years ago
|
public function testInvalidEnvironments()
|
||
|
{
|
||
|
try {
|
||
|
$runtime = new Runtime();
|
||
|
$runtime->setEnvironment('invalid');
|
||
|
} catch (Exception $exception) {
|
||
8 years ago
|
$this->assertTrue($exception instanceof RuntimeException);
|
||
8 years ago
|
}
|
||
|
|
||
|
try {
|
||
|
$runtime = new Runtime();
|
||
|
$runtime->setConfiguration(['environments' => ['valid' => []]]);
|
||
|
$runtime->setEnvironment('invalid');
|
||
|
} catch (Exception $exception) {
|
||
8 years ago
|
$this->assertTrue($exception instanceof RuntimeException);
|
||
8 years ago
|
}
|
||
|
}
|
||
|
|
||
|
public function testTempFile()
|
||
|
{
|
||
|
$runtime = new Runtime();
|
||
|
$tempFile = $runtime->getTempFile();
|
||
|
|
||
|
$this->assertNotEquals('', $tempFile);
|
||
|
$this->assertTrue(file_exists($tempFile));
|
||
8 years ago
|
$this->assertTrue(is_readable($tempFile));
|
||
|
$this->assertTrue(is_writable($tempFile));
|
||
8 years ago
|
$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');
|
||
8 years ago
|
$timestamp = time();
|
||
8 years ago
|
$this->assertTrue($process->isSuccessful());
|
||
8 years ago
|
$this->assertEquals($timestamp, trim($process->getOutput()));
|
||
8 years ago
|
|
||
|
/** @var Process $process */
|
||
|
$process = $runtime->runLocalCommand('false');
|
||
|
$this->assertFalse($process->isSuccessful());
|
||
|
}
|
||
8 years ago
|
|
||
|
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());
|
||
|
}
|
||
8 years ago
|
}
|