unit-test FpmDaemons-ApiCommand

Signed-off-by: Michael Kaufmann (d00p) <d00p@froxlor.org>
This commit is contained in:
Michael Kaufmann (d00p)
2018-03-26 14:26:05 +02:00
parent efb416ae7c
commit cb3d5f3488
3 changed files with 114 additions and 10 deletions

View File

@@ -72,8 +72,9 @@ class FpmDaemons extends ApiCommand implements ResourceEntity
/**
* return a fpm-daemon entry by id
*
* @param int $id fpm-daemon-id
*
* @param int $id
* fpm-daemon-id
*
* @access admin
* @throws Exception
* @return array
@@ -121,7 +122,7 @@ class FpmDaemons extends ApiCommand implements ResourceEntity
$max_spare_servers = $this->getParam('max_spare_servers', true, 0);
$max_requests = $this->getParam('max_requests', true, 0);
$idle_timeout = $this->getParam('idle_timeout', true, 0);
$limit_extensions = $this->getParam('limit_extensions', true, '');
$limit_extensions = $this->getParam('limit_extensions', true, '.php');
// validation
$description = validate($description, 'description', '', '', array(), true);
@@ -134,6 +135,9 @@ class FpmDaemons extends ApiCommand implements ResourceEntity
))) {
throw new ErrorException("Unknown process manager", 406);
}
if (empty($limit_extensions)) {
$limit_extensions = '.php';
}
$limit_extensions = validate($limit_extensions, 'limit_extensions', '/^(\.[a-z]([a-z0-9]+)\ ?)+$/', '', array(), true);
if (strlen($description) == 0 || strlen($description) > 50) {
@@ -168,11 +172,14 @@ class FpmDaemons extends ApiCommand implements ResourceEntity
'limit_extensions' => $limit_extensions
);
Database::pexecute($ins_stmt, $ins_data);
$ins_data['id'] = Database::lastInsertId();
$id = Database::lastInsertId();
inserttask('1');
$this->logger()->logAction(ADM_ACTION, LOG_INFO, "[API] fpm-daemon with description '" . $description . "' has been created by '" . $this->getUserDetail('loginname') . "'");
return $this->response(200, "successfull", $ins_data);
$result = $this->apiCall('FpmDaemons.get', array(
'id' => $id
));
return $this->response(200, "successfull", $result);
}
throw new Exception("Not allowed to execute given command.", 403);
}
@@ -192,8 +199,8 @@ class FpmDaemons extends ApiCommand implements ResourceEntity
// required parameter
$id = $this->getParam('id');
$result = $this->apiCall('PhpSettings.get', array(
$result = $this->apiCall('FpmDaemons.get', array(
'id' => $id
));
@@ -221,6 +228,9 @@ class FpmDaemons extends ApiCommand implements ResourceEntity
))) {
throw new ErrorException("Unknown process manager", 406);
}
if (empty($limit_extensions)) {
$limit_extensions = '.php';
}
$limit_extensions = validate($limit_extensions, 'limit_extensions', '/^(\.[a-z]([a-z0-9]+)\ ?)+$/', '', array(), true);
if (strlen($description) == 0 || strlen($description) > 50) {
@@ -268,8 +278,9 @@ class FpmDaemons extends ApiCommand implements ResourceEntity
/**
* delete a fpm-daemon entry by id
*
* @param int $id fpm-daemon-id
*
* @param int $id
* fpm-daemon-id
*
* @access admin
* @throws Exception
* @return array
@@ -282,7 +293,7 @@ class FpmDaemons extends ApiCommand implements ResourceEntity
if ($id == 1) {
standard_error('cannotdeletedefaultphpconfig', '', true);
}
$result = $this->apiCall('FpmDaemons.get', array(
'id' => $id
));

View File

@@ -0,0 +1,92 @@
<?php
use PHPUnit\Framework\TestCase;
/**
*
* @covers ApiCommand
* @covers ApiParameter
* @covers FpmDaemons
*/
class FpmDaemonsTest extends TestCase
{
private static $id = 0;
public function testAdminFpmDaemonsAdd()
{
global $admin_userdata;
$data = [
'description' => 'test2 fpm',
'reload_cmd' => 'service php7.1-fpm reload',
'config_dir' => '/etc/php/7.1/fpm/pool.d'
];
$json_result = FpmDaemons::getLocal($admin_userdata, $data)->add();
$result = json_decode($json_result, true)['data'];
$this->assertEquals('/etc/php/7.1/fpm/pool.d/', $result['config_dir']);
$this->assertEquals(0, $result['max_children']);
$this->assertEquals('.php', $result['limit_extensions']);
self::$id = $result['id'];
}
/**
* @depends testAdminFpmDaemonsAdd
*/
public function testAdminFpmDaemonsUpdate()
{
global $admin_userdata;
$data = [
'id' => self::$id,
'description' => 'test2 fpm edit',
'pm' => 'dynamic',
'max_children' => '10',
'start_servers' => '4',
'limit_extensions' => '.php .php.xml',
];
$json_result = FpmDaemons::getLocal($admin_userdata, $data)->update();
$result = json_decode($json_result, true)['data'];
$this->assertEquals('/etc/php/7.1/fpm/pool.d/', $result['config_dir']);
$this->assertEquals(10, $result['max_children']);
$this->assertEquals('.php .php.xml', $result['limit_extensions']);
}
/**
* @depends testAdminFpmDaemonsUpdate
*/
public function testAdminFpmDaemonsList()
{
global $admin_userdata;
$json_result = FpmDaemons::getLocal($admin_userdata)->listing();
$result = json_decode($json_result, true)['data'];
$this->assertEquals(2, $result['count']);
$this->assertEquals('test fpm', $result['list'][0]['description']);
$this->assertEquals('test2 fpm edit', $result['list'][1]['description']);
}
/**
* @depends testAdminFpmDaemonsList
*/
public function testAdminFpmDaemonsDelete()
{
global $admin_userdata;
$data = [
'id' => self::$id
];
$json_result = FpmDaemons::getLocal($admin_userdata, $data)->delete();
$result = json_decode($json_result, true)['data'];
$this->assertEquals('/etc/php/7.1/fpm/pool.d/', $result['config_dir']);
$this->assertEquals(10, $result['max_children']);
$this->assertEquals('.php .php.xml', $result['limit_extensions']);
}
/**
* @depends testAdminFpmDaemonsDelete
*/
public function testAdminFpmDaemonsDeleteDefaultConfig()
{
global $admin_userdata;
$data = [
'id' => 1
];
$this->expectExceptionMessage("This PHP-configuration is set as default and cannot be deleted.");
FpmDaemons::getLocal($admin_userdata, $data)->delete();
}
}

View File

@@ -55,6 +55,7 @@ Database::query("TRUNCATE TABLE `" . TABLE_PANEL_ADMINS . "`;");
Database::query("TRUNCATE TABLE `" . TABLE_PANEL_IPSANDPORTS . "`;");
Database::query("TRUNCATE TABLE `" . TABLE_API_KEYS . "`;");
Database::query("TRUNCATE TABLE `" . TABLE_PANEL_DATABASES . "`;");
Database::query("ALTER TABLE `" . TABLE_PANEL_FPMDAEMONS . "` AUTO_INCREMENT=2;");
// add superadmin
Database::query("INSERT INTO `" . TABLE_PANEL_ADMINS . "` SET