[Nostromo] Exec task

This commit is contained in:
Andrés Montañez
2017-02-25 19:31:25 -03:00
parent 2e7caa8229
commit fc851dd661
4 changed files with 107 additions and 107 deletions
-90
View File
@@ -1,90 +0,0 @@
<?php
namespace Mage\Tests\Command\BuiltIn;
use Mage\Runtime\Runtime;
use Mage\Task\BuiltIn\ExecTask;
use PHPUnit_Framework_TestCase as TestCase;
use Symfony\Component\Process\Process;
class ExecTaskTest extends TestCase
{
public function testBasics()
{
$task = new ExecTask();
$this->assertSame('exec', $task->getName());
$this->assertSame('[Exec] Executing custom command', $task->getDescription());
}
public function testCustomDescription()
{
$task = new ExecTask();
$task->setOptions(['descr' => '[My project] This is my wonderful task']);
$this->assertSame('[My project] This is my wonderful task', $task->getDescription());
}
/**
* @expectedException \Mage\Task\Exception\ErrorException
*/
public function testNoCommandProvided()
{
$task = new ExecTask();
$task->execute();
}
public function testNonJailedCommand()
{
$runtime = $this->getMockBuilder(Runtime::class)
->setMethods(['runRemoteCommand'])
->getMock();
$runtime
->expects($this->once())
->method('runRemoteCommand')
->with('rm -rf /')
->willReturn($this->mockProcess(true));
$task = $this->getTask($runtime);
$task->setOptions(['cmd' => 'rm -rf /', 'jail' => false]);
$this->assertTrue($task->execute());
}
public function testRegularCommand()
{
$runtime = $this->getMockBuilder(Runtime::class)
->setMethods(['runCommand'])
->getMock();
$runtime
->expects($this->once())
->method('runCommand')
->with('rm -rf /', 10)
->willReturn($this->mockProcess(true));
$task = $this->getTask($runtime);
$task->setOptions(['cmd' => 'rm -rf /', 'timeout' => 10]);
$task->execute();
}
private function getTask($runtime)
{
$task = new ExecTask();
$task->setRuntime($runtime);
return $task;
}
private function mockProcess($successful)
{
$process = $this->getMockBuilder(Process::class)
->disableOriginalConstructor()
->getMock();
$process
->expects($this->any())
->method('isSuccessful')
->willReturn($successful);
return $process;
}
}
+97
View File
@@ -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\Task\BuiltIn;
use Mage\Task\Exception\ErrorException;
use Mage\Task\BuiltIn\ExecTask;
use Exception;
use Mage\Tests\Runtime\RuntimeMockup;
use PHPUnit_Framework_TestCase as TestCase;
class ExecTest extends TestCase
{
public function testSimpleCommand()
{
$runtime = new RuntimeMockup();
$runtime->setConfiguration(['environments' => ['test' => []]]);
$runtime->setEnvironment('test');
$task = new ExecTask();
$task->setOptions(['cmd' => 'ls -l', 'desc' => 'Loading docker']);
$task->setRuntime($runtime);
$this->assertContains('[Exec] Loading docker', $task->getDescription());
$task->execute();
$ranCommands = $runtime->getRanCommands();
$testCase = array(
0 => 'ls -l',
);
// Check total of Executed Commands
$this->assertEquals(count($testCase), count($ranCommands));
// Check Generated Commands
foreach ($testCase as $index => $command) {
$this->assertEquals($command, $ranCommands[$index]);
}
}
public function testCommandWithoutDescription()
{
$runtime = new RuntimeMockup();
$runtime->setConfiguration(['environments' => ['test' => []]]);
$runtime->setEnvironment('test');
$task = new ExecTask();
$task->setOptions(['cmd' => 'ls -la']);
$task->setRuntime($runtime);
$this->assertContains('[Exec] Custom command', $task->getDescription());
$task->execute();
$ranCommands = $runtime->getRanCommands();
$testCase = array(
0 => 'ls -la',
);
// Check total of Executed Commands
$this->assertEquals(count($testCase), count($ranCommands));
// Check Generated Commands
foreach ($testCase as $index => $command) {
$this->assertEquals($command, $ranCommands[$index]);
}
}
public function testWithoutCommand()
{
$runtime = new RuntimeMockup();
$runtime->setConfiguration(['environments' => ['test' => []]]);
$runtime->setEnvironment('test');
$task = new ExecTask();
$task->setOptions(['desc' => 'Loading docker']);
$task->setRuntime($runtime);
$this->assertContains('[Exec] Loading docker', $task->getDescription());
try {
$task->execute();
$this->assertTrue(false, 'Task did not failed');
} catch (Exception $exception) {
$this->assertTrue($exception instanceof ErrorException);
$this->assertEquals('Parameter "cmd" is not defined', $exception->getMessage());
}
}
}