This commit is contained in:
Fabio Del Bene 2017-01-02 18:27:52 +00:00 committed by GitHub
commit 3576271113
3 changed files with 179 additions and 0 deletions

View file

@ -0,0 +1,64 @@
<?php
namespace Mage\Task\BuiltIn\Symfony2;
use Mage\Task\AbstractTask;
use Mage\Task\SkipException;
use Mage\Task\Releases\IsReleaseAware;
/**
* Task to setup Symfony file permission
*
* @see http://symfony.com/doc/current/setup/file_permissions.html Documentation of Symfony file permission setup.
*
* @author Fabio Del Bene <fabio.delbene@gmail.com>
*/
class ApplyFaclsTask extends AbstractTask implements IsReleaseAware
{
/**
* Returns the Title of the Task
* @return string
*/
public function getName()
{
return 'Set Symfony file ACLs on remote system [built-in]';
}
/**
* Runs the task
*
* @return boolean
* @throws SkipException
*/
public function run()
{
$releasesDirectory = $this->getConfig()->release('directory', 'releases');
$currentCopy = $releasesDirectory . '/' . $this->getConfig()->getReleaseId();
$httpUser = $this->getParameter('httpuser', '');
if (empty($httpUser)) {
throw new SkipException('Parameter httpuser not set.');
}
$localuser = $this->getParameter('localuser', '');
if (empty($localuser)) {
throw new SkipException('Parameter localuser not set.');
}
$folders = $this->getParameter('folders', array());
foreach ($folders as $folder) {
$folderPath = $currentCopy."/".$folder;
$this->runCommandRemote($this->createFaclCommand('-R', $httpUser,$localuser, $folderPath), $output);
$this->runCommandRemote($this->createFaclCommand('-dR', $httpUser,$localuser, $folderPath), $output);
}
return true;
}
public function createFaclCommand($setFaclOptions, $httpUser,$localuser, $folder)
{
return sprintf('setfacl %s -m u:%s:rwX -m u:%s:rwX %s',
$setFaclOptions, $httpUser, $localuser, $folder
);
}
}

View file

@ -21,6 +21,12 @@ tasks:
- privileges
- sampleTask
- sampleTaskRollbackAware
- symfony2/apply-facls:
httpuser: www-data
localuser: deployer
folders:
- var
- web
verbose_logging: true
env:
variables:

View file

@ -0,0 +1,109 @@
<?php
namespace MageTest\Task\BuiltIn\Symfony2;
use Mage\Config;
use Mage\Task\BuiltIn\Symfony2\ApplyFaclsTask;
use Mage\Task\Factory;
use PHPUnit_Framework_TestCase;
class ApplyFaclsTaskTest extends PHPUnit_Framework_TestCase
{
private $config;
protected function setUp()
{
$this->config = $this->getMock('Mage\\Config');
}
/**
* @covers Mage\Task\Factory::get
*/
public function testGet()
{
$task = Factory::get('symfony2/apply-facls', $this->config);
$this->assertInstanceOf('\\Mage\\Task\\BuiltIn\\Symfony2\\ApplyFaclsTask', $task);
}
/**
* @covers Mage\Task\Factory::get
*/
public function testGetTaskDataIsArray()
{
$taskData = array(
'name' => 'symfony2/apply-facls',
'parameters' => array(
'httpuser' => 'www-data',
'localuser' => 'deployer',
'folders' => array('var', 'web')
),
);
$task = Factory::get($taskData, $this->config);
$this->assertInstanceOf('\\Mage\\Task\\BuiltIn\\Symfony2\\ApplyFaclsTask', $task);
}
/**
* @expectedException \Mage\Task\SkipException
* @expectedExceptionMessage Parameter httpuser not set.
* @covers Mage\Task\BuiltIn\Symfony2\ApplyFaclsTask::run
*/
public function test_Run_WithNoHttpUserParameter_Expect_throw_SkipException()
{
// Arrange
$config = new Config();
$config->addParameter('httpuser', '');
$task = new ApplyFaclsTask($config);
// Act
$task->run();
}
/**
* @group unit
* @expectedException \Mage\Task\SkipException
* @expectedExceptionMessage Parameter localuser not set.
* @covers Mage\Task\BuiltIn\Symfony2\ApplyFaclsTask::run
*/
public function test_run_withNoLocalUserParameter_trow_SkipException()
{
// Arrange
$config = new Config();
$config->addParameter('httpuser', 'www-data');
$config->addParameter('localuser', '');
$task = new ApplyFaclsTask($config);
// Act
$task->run();
}
public function provider_createFaclsCommand()
{
return array(
'no facls options' => array(
'', 'www-data', 'deployer', 'var', 'setfacl -m u:www-data:rwX -m u:deployer:rwX var'
),
'with facls options' => array(
'-dR', 'www-data', 'deployer', 'var', 'setfacl -dR -m u:www-data:rwX -m u:deployer:rwX var'
)
);
}
/**
* @group unit
* @dataProvider provider_createFaclsCommand
* @covers Mage\Task\BuiltIn\Symfony2\ApplyFaclsTask::createFaclCommand
*
* @param string $faclOptions
* @param string $httpUser
* @param string $localUser
* @param string $folder
* @param string $expectedCommand
*/
public function test_createFaclsCommand($faclOptions, $httpUser, $localUser, $folder, $expectedCommand)
{
// Arrange
$task = new ApplyFaclsTask($this->config);
// Act
$command = $task->createFaclCommand($faclOptions, $httpUser, $localUser, $folder);
// Assert
$this->assertEquals($expectedCommand, $command);
}
}