Skip to content

Commit c042a61

Browse files
committed
fpm: Add configuration file tests
1 parent bc0d0b0 commit c042a61

7 files changed

+207
-13
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
--TEST--
2+
FPM: Validates arrays in configuration are correctly set - php_value array must be passed a key
3+
--SKIPIF--
4+
<?php include "skipif.inc"; ?>
5+
--FILE--
6+
<?php
7+
8+
require_once "tester.inc";
9+
10+
$cfg = <<<EOT
11+
[global]
12+
error_log = {{FILE:LOG}}
13+
pid = {{FILE:PID}}
14+
[unconfined]
15+
listen = {{ADDR}}
16+
php_value[] = E_ALL
17+
pm = static
18+
pm.max_children = 5
19+
EOT;
20+
21+
$tester = new FPM\Tester($cfg);
22+
$tester->start(['-tt']);
23+
$tester->expectLogError("\[%s:%d\] You must provide a key for field 'php_value'");
24+
25+
?>
26+
Done
27+
--EXPECT--
28+
29+
Done
30+
--CLEAN--
31+
<?php
32+
require_once "tester.inc";
33+
FPM\Tester::clean();
34+
?>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
--TEST--
2+
FPM: Validates arrays in configuration are correctly set - access.suppress_path doesn't accept key with forward slash
3+
--SKIPIF--
4+
<?php include "skipif.inc"; ?>
5+
--FILE--
6+
<?php
7+
8+
require_once "tester.inc";
9+
10+
$cfg = <<<EOT
11+
[global]
12+
error_log = {{FILE:LOG}}
13+
pid = {{FILE:PID}}
14+
[unconfined]
15+
listen = {{ADDR}}
16+
access.suppress_path[/] = test
17+
pm = static
18+
pm.max_children = 5
19+
EOT;
20+
21+
$tester = new FPM\Tester($cfg);
22+
$tester->start(['-tt']);
23+
$tester->expectLogError("\[%s:%d\] Keys provided to field 'access.suppress_path' are ignored");
24+
25+
?>
26+
Done
27+
--EXPECT--
28+
29+
Done
30+
--CLEAN--
31+
<?php
32+
require_once "tester.inc";
33+
FPM\Tester::clean();
34+
?>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
--TEST--
2+
FPM: Validates arrays in configuration are correctly set - access.suppress_path doesn't allow key
3+
--SKIPIF--
4+
<?php include "skipif.inc"; ?>
5+
--FILE--
6+
<?php
7+
8+
require_once "tester.inc";
9+
10+
$cfg = <<<EOT
11+
[global]
12+
error_log = {{FILE:LOG}}
13+
pid = {{FILE:PID}}
14+
[unconfined]
15+
listen = {{ADDR}}
16+
access.suppress_path[pingpath] = /ping
17+
pm = static
18+
pm.max_children = 5
19+
EOT;
20+
21+
$tester = new FPM\Tester($cfg);
22+
$tester->start(['-tt']);
23+
$tester->expectLogError("\[%s:%d\] Keys provided to field 'access.suppress_path' are ignored");
24+
25+
?>
26+
Done
27+
--EXPECT--
28+
29+
Done
30+
--CLEAN--
31+
<?php
32+
require_once "tester.inc";
33+
FPM\Tester::clean();
34+
?>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
--TEST--
2+
FPM: Validates arrays in configuration are correctly set - access.suppress_path begins with forward slash
3+
--SKIPIF--
4+
<?php include "skipif.inc"; ?>
5+
--FILE--
6+
<?php
7+
8+
require_once "tester.inc";
9+
10+
$cfg = <<<EOT
11+
[global]
12+
error_log = {{FILE:LOG}}
13+
pid = {{FILE:PID}}
14+
[unconfined]
15+
listen = {{ADDR}}
16+
access.suppress_path[] = needs / to start with a slash
17+
pm = static
18+
pm.max_children = 5
19+
EOT;
20+
21+
$tester = new FPM\Tester($cfg);
22+
$tester->start(['-tt']);
23+
$tester->expectLogError("\[%s:%d\] Values provided to field 'access.suppress_path' must begin with '\/'");
24+
25+
?>
26+
Done
27+
--EXPECT--
28+
29+
Done
30+
--CLEAN--
31+
<?php
32+
require_once "tester.inc";
33+
FPM\Tester::clean();
34+
?>

sapi/fpm/tests/config-array.phpt

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
--TEST--
2+
FPM: Set arrays in configuration
3+
--SKIPIF--
4+
<?php include "skipif.inc"; ?>
5+
--FILE--
6+
<?php
7+
8+
require_once "tester.inc";
9+
10+
$cfg = <<<EOT
11+
[global]
12+
error_log = {{FILE:LOG}}
13+
pid = {{FILE:PID}}
14+
[unconfined]
15+
listen = {{ADDR}}
16+
access.suppress_path[] = /ping
17+
access.suppress_path[] = /health_check.php
18+
pm = static
19+
pm.max_children = 5
20+
php_value[error_reporting] = E_ALL
21+
php_value[date.timezone] = Europe/London
22+
php_admin_value[disable_functions] = eval
23+
php_flag[display_errors] = On
24+
php_admin_flag[log_errors] = 1
25+
EOT;
26+
27+
$tester = new FPM\Tester($cfg);
28+
$tester->start(['-tt']);
29+
$tester->expectLogConfigOptions([
30+
'access.suppress_path[] = /ping',
31+
'access.suppress_path[] = /health_check.php',
32+
'php_value[error_reporting] = 32767',
33+
'php_value[date.timezone] = Europe/London',
34+
'php_value[display_errors] = 1',
35+
'php_admin_value[disable_functions] = eval',
36+
'php_admin_value[log_errors] = 1',
37+
]);
38+
39+
40+
?>
41+
Done
42+
--EXPECT--
43+
44+
Done
45+
--CLEAN--
46+
<?php
47+
require_once "tester.inc";
48+
FPM\Tester::clean();
49+
?>

sapi/fpm/tests/pm-max-spawn-rate-config.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ pm.max_spawn_rate = 64
2424
EOT;
2525

2626
$tester = new FPM\Tester($cfg);
27-
$tester->start(['-t', '-t']);
28-
$tester->expectLogConfigOptions(['pm.max_spawn_rate' => 64]);
27+
$tester->start(['-tt']);
28+
$tester->expectLogConfigOptions(['pm.max_spawn_rate = 64']);
2929
$tester->close();
3030

3131
?>

sapi/fpm/tests/tester.inc

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1441,18 +1441,28 @@ class Tester
14411441
* @param array $options
14421442
* @return bool
14431443
*/
1444-
public function expectLogConfigOptions(array $options)
1444+
public function expectLogConfigOptions(array $expectedOptions)
14451445
{
14461446
$configOptions = $this->getConfigOptions();
1447-
foreach ($options as $name => $value) {
1448-
if (!isset($configOptions[$name])) {
1449-
return $this->error("Expected config option: {$name} = {$value} but {$name} is not set");
1447+
foreach ($expectedOptions as $expectedOption) {
1448+
if (array_search($expectedOption, $configOptions, true)) {
1449+
// Exact match found, no error
1450+
continue;
14501451
}
1451-
if ($configOptions[$name] != $value) {
1452-
return $this->error(
1453-
"Expected config option: {$name} = {$value} but got: {$name} = {$configOptions[$name]}"
1454-
);
1452+
1453+
// Try to find similar key
1454+
$key = substr($expectedOption, 0, strpos($expectedOption, " = "));
1455+
$matches = array_filter($configOptions, fn($configOption) => substr($configOption, 0, strlen($key)) == $key);
1456+
1457+
if (empty($matches)) {
1458+
return $this->error("Expected config option: $expectedOption but {$key} is not set");
14551459
}
1460+
1461+
return $this->error(sprintf(
1462+
"Expected config option: %s but got: %s",
1463+
$expectedOption,
1464+
implode("; ", $matches)
1465+
));
14561466
}
14571467

14581468
return true;
@@ -1466,14 +1476,13 @@ class Tester
14661476
private function getConfigOptions()
14671477
{
14681478
$options = [];
1469-
14701479
foreach ($this->getLogLines(-1) as $line) {
14711480
preg_match('/.+NOTICE:\s+(.+)\s=\s(.+)/', $line, $matches);
14721481
if ($matches) {
1473-
$options[$matches[1]] = $matches[2];
1482+
// normalize format for consistent checking
1483+
$options[] = sprintf("%s = %s", $matches[1], $matches[2]);
14741484
}
14751485
}
1476-
14771486
return $options;
14781487
}
14791488

0 commit comments

Comments
 (0)