mirror of
				https://github.com/hauke68/Magallanes.git
				synced 2025-11-04 00:50:18 +01:00 
			
		
		
		
	[Nostromo] Exec task
This commit is contained in:
		
							parent
							
								
									2e7caa8229
								
							
						
					
					
						commit
						fc851dd661
					
				@ -2,6 +2,7 @@ CHANGELOG for 3.X
 | 
			
		||||
=================
 | 
			
		||||
 | 
			
		||||
* 3.X (2017-XX-XX)
 | 
			
		||||
 * Add new Exec task to execute arbitrary shell commands
 | 
			
		||||
 * Add new Composer task, to update phar (composer/self-update)
 | 
			
		||||
 * [#344] Allow to flag Filesystem tasks
 | 
			
		||||
 * [PR#346] Add new File System task, to change file's modes (fs/chmod)
 | 
			
		||||
 | 
			
		||||
@ -12,6 +12,7 @@ namespace Mage\Task\BuiltIn;
 | 
			
		||||
 | 
			
		||||
use Mage\Task\Exception\ErrorException;
 | 
			
		||||
use Mage\Task\AbstractTask;
 | 
			
		||||
use Symfony\Component\Process\Process;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Exec task. Allows you to execute arbitrary commands.
 | 
			
		||||
@ -35,11 +36,11 @@ class ExecTask extends AbstractTask
 | 
			
		||||
    {
 | 
			
		||||
        $options = $this->getOptions();
 | 
			
		||||
 | 
			
		||||
        if ('' !== $options['descr']) {
 | 
			
		||||
            return (string) $options['descr'];
 | 
			
		||||
        if ($options['desc']) {
 | 
			
		||||
            return '[Exec] ' . $options['desc'];
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return '[Exec] Executing custom command';
 | 
			
		||||
        return '[Exec] Custom command';
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
@ -51,16 +52,11 @@ class ExecTask extends AbstractTask
 | 
			
		||||
    {
 | 
			
		||||
        $options = $this->getOptions();
 | 
			
		||||
 | 
			
		||||
        if ('' === $options['cmd']) {
 | 
			
		||||
            throw new ErrorException('What about if you gave me a command to execute?');
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        // If not jailed, it must run as remote command
 | 
			
		||||
        if (false === $options['jail']) {
 | 
			
		||||
            $process = $this->runtime->runRemoteCommand($options['cmd'], false, $options['timeout']);
 | 
			
		||||
            return $process->isSuccessful();
 | 
			
		||||
        if (!$options['cmd']) {
 | 
			
		||||
            throw new ErrorException('Parameter "cmd" is not defined');
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /** @var Process $process */
 | 
			
		||||
        $process = $this->runtime->runCommand($options['cmd'], $options['timeout']);
 | 
			
		||||
        return $process->isSuccessful();
 | 
			
		||||
    }
 | 
			
		||||
@ -70,12 +66,8 @@ class ExecTask extends AbstractTask
 | 
			
		||||
     */
 | 
			
		||||
    protected function getOptions()
 | 
			
		||||
    {
 | 
			
		||||
        $options = array_merge([
 | 
			
		||||
                'cmd' => '',
 | 
			
		||||
                'descr' => '',
 | 
			
		||||
                'jail' => true,
 | 
			
		||||
                'timeout' => 120
 | 
			
		||||
            ],
 | 
			
		||||
        $options = array_merge(
 | 
			
		||||
            ['cmd' => '', 'desc' => '', 'timeout' => 120],
 | 
			
		||||
            $this->options
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -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
									
								
								tests/Task/BuiltIn/ExecTaskTest.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										97
									
								
								tests/Task/BuiltIn/ExecTaskTest.php
									
									
									
									
									
										Normal 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());
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user