SyncUtils: allow special varnames, add tests

fixes #839
This commit is contained in:
Thomas Gelf 2017-03-09 10:57:37 +01:00
parent 4963b5fe54
commit 1b57171583
2 changed files with 111 additions and 3 deletions

View File

@ -16,7 +16,7 @@ class SyncUtils
*/
public static function extractVariableNames($string)
{
if (preg_match_all('/\${([A-Za-z0-9\._-]+)}/', $string, $m, PREG_PATTERN_ORDER)) {
if (preg_match_all('/\${([^}]+)}/', $string, $m, PREG_PATTERN_ORDER)) {
return $m[1];
} else {
return array();
@ -103,7 +103,7 @@ class SyncUtils
*/
public static function fillVariables($string, $row)
{
if (preg_match('/^\${([A-Za-z0-9\._-]+)}$/', $string, $m)) {
if (preg_match('/^\${([^}]+)}$/', $string, $m)) {
return static::getSpecificValue($row, $m[1]);
}
@ -111,7 +111,7 @@ class SyncUtils
return SyncUtils::getSpecificValue($row, $match[1]);
};
return preg_replace_callback('/\${([A-Za-z0-9\._-]+)}/', $func, $string);
return preg_replace_callback('/\${([^}]+)}/', $func, $string);
}
public static function getRootVariables($vars)

View File

@ -0,0 +1,108 @@
<?php
namespace Tests\Icinga\Module\Director\IcingaConfig;
use Icinga\Module\Director\Import\SyncUtils;
use Icinga\Module\Director\Test\BaseTestCase;
class SyncUtilsTest extends BaseTestCase
{
public function testVariableNamesAreExtracted()
{
$this->assertEquals(
array(
'var.name',
'$Special Var'
),
SyncUtils::extractVariableNames('This ${var.name} is ${$Special Var} are vars')
);
$this->assertEquals(
array(),
SyncUtils::extractVariableNames('No ${var.name vars ${$Special Var here')
);
}
public function testSpecificValuesCanBeRetrievedByName()
{
$row = (object)array(
'host' => 'localhost',
'ipaddress' => '127.0.0.1'
);
$this->assertEquals(
'127.0.0.1',
SyncUtils::getSpecificValue($row, 'ipaddress')
);
}
public function testMissingPropertiesMustBeNull()
{
$row = (object)array(
'host' => 'localhost',
'ipaddress' => '127.0.0.1'
);
$this->assertNull(
SyncUtils::getSpecificValue($row, 'address')
);
}
public function testNestedValuesCanBeRetrievedByPath()
{
$row = $this->getSampleRow();
$this->assertEquals(
'192.0.2.10',
SyncUtils::getSpecificValue($row, 'addresses.entries.eth0:1')
);
$this->assertEquals(
2,
SyncUtils::getSpecificValue($row, 'addresses.count')
);
}
public function testRootVariablesCanBeExtracted()
{
$vars = array('test', 'nested.test', 'nested.dee.per');
$this->assertEquals(
array(
'test' => 'test',
'nested' => 'nested'
),
SyncUtils::getRootVariables($vars)
);
}
public function testVariables()
{
$string = '${addresses.entries.lo} and ${addresses.entries.eth0:1} are'
. ' ${This one?.$höüld be}${addressesmissing}';
$this->assertEquals(
'127.0.0.1 and 192.0.2.10 are fine',
SyncUtils::fillVariables(
$string,
$this->getSampleRow()
)
);
}
protected function getSampleRow()
{
return (object) array(
'host' => 'localhost',
'addresses' => (object) array(
'count' => 2,
'entries' => (object) array(
'lo' => '127.0.0.1',
'eth0:1' => '192.0.2.10',
)
),
'This one?' => (object) array(
'$höüld be' => 'fine'
)
);
}
}