Skip to content

Add FPM tester logs printing for all errors #12902

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions sapi/fpm/tests/logreader.inc
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,11 @@ class LogReader
*/
public function printLogs(): void
{
if (empty($this->sources)) {
return;
}
$hasMultipleDescriptors = count($this->sources) > 1;
echo "LOGS:\n";
echo "\nLOGS:\n";
foreach ($this->sources as $name => $source) {
if ($hasMultipleDescriptors) {
echo ">>> source: $name\n";
Expand All @@ -128,6 +131,7 @@ class LogReader
}
$this->printSeparator();
}
echo "\n";
}

/**
Expand All @@ -142,9 +146,8 @@ class LogReader
if (is_null($errorMessage)) {
return false;
}
echo "ERROR: " . $errorMessage . "\n\n";
echo "ERROR: " . $errorMessage . "\n";
$this->printLogs();
echo "\n";

return false;
}
Expand Down
130 changes: 73 additions & 57 deletions sapi/fpm/tests/response.inc
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,68 @@

namespace FPM;

class Response
abstract class BaseResponse
{
/**
* Tester instance
* @var Tester
*/
private Tester $tester;

/**
* @var bool
*/
protected bool $debugOutputted = false;

/**
* @param Tester $tester
*/
public function __construct(Tester $tester)
{
$this->tester = $tester;
}

/**
* Debug response output.
*
* @return void
*/
abstract function debugOutput(): void;

/**
* Emit error message
*
* @param string $message
* @param bool $throw
*
* @return bool
* @throws \Exception
*/
protected function error(string $message, bool $throw = false): bool
{
$errorMessage = "ERROR: $message\n";
if ($throw) {
throw new \Exception($errorMessage);
}
if ( ! $this->debugOutputted) {
$this->debugOutput();
}
echo $errorMessage;

$this->tester->printLogs();

return false;
}
}

class Response extends BaseResponse
{
const HEADER_SEPARATOR = "\r\n\r\n";

/**
* @var array
*/
private $data;
private array $data;

/**
* @var string
Expand Down Expand Up @@ -39,19 +93,17 @@ class Response
/**
* @var bool
*/
private $expectInvalid;

/**
* @var bool
*/
private bool $debugOutputted = false;
private bool $expectInvalid;

/**
* @param Tester $tester
* @param string|array|null $data
* @param bool $expectInvalid
*/
public function __construct($data = null, $expectInvalid = false)
public function __construct(Tester $tester, $data = null, bool $expectInvalid = false)
{
parent::__construct($tester);

if ( ! is_array($data)) {
$data = [
'response' => $data,
Expand Down Expand Up @@ -105,7 +157,7 @@ class Response
*
* @return Response
*/
public function expectJsonBodyPatternForStatusProcessField(string $fieldName, string $pattern)
public function expectJsonBodyPatternForStatusProcessField(string $fieldName, string $pattern): Response
{
$rawData = $this->getBody('application/json');
$data = json_decode($rawData, true);
Expand Down Expand Up @@ -270,7 +322,7 @@ class Response
/**
* Debug response output
*/
public function debugOutput()
public function debugOutput(): void
{
echo ">>> Response\n";
echo "----------------- OUT -----------------\n";
Expand Down Expand Up @@ -416,37 +468,24 @@ class Response
);
}
}

/**
* Emit error message
*
* @param string $message
*
* @return bool
*/
private function error(string $message): bool
{
if ( ! $this->debugOutputted) {
$this->debugOutput();
}
echo "ERROR: $message\n";

return false;
}
}

class ValuesResponse
class ValuesResponse extends BaseResponse
{
/**
* @var array
*/
private array $values;

/**
* @param Tester $tester
* @param string|array|null $values
* @throws \Exception
*/
public function __construct($values = null)
public function __construct(Tester $tester, $values = null)
{
parent::__construct($tester);

if ( ! is_array($values)) {
if ( ! is_null($values) ) {
$this->error('Invalid values supplied', true);
Expand All @@ -463,14 +502,15 @@ class ValuesResponse
* @param string $name
* @param mixed $value
* @return ValuesResponse
* @throws \Exception
*/
public function expectValue(string $name, $value = null)
{
if ( ! isset($this->values[$name])) {
return $this->error("Value $name not found in values");
$this->error("Value $name not found in values");
}
if ( ! is_null($value) && $value !== $this->values[$name]) {
return $this->error("Value $name is {$this->values[$name]} but expected $value");
$this->error("Value $name is {$this->values[$name]} but expected $value");
}
return $this;
}
Expand All @@ -487,36 +527,12 @@ class ValuesResponse

/**
* Debug output data.
*
* @return ValuesResponse
*/
public function debugOutput()
public function debugOutput(): void
{
echo ">>> ValuesResponse\n";
echo "----------------- Values -----------------\n";
var_dump($this->values);
echo "---------------------------------------\n\n";

return $this;
}

/**
* Emit error message
*
* @param string $message
* @param bool $throw
*
* @return ValuesResponse
*/
private function error(string $message, $throw = false): bool
{
$errorMessage = "ERROR: $message\n";
if ($throw) {
throw new \Exception($errorMessage);
}
$this->debugOutput();
echo $errorMessage;

return $this;
}
}
40 changes: 30 additions & 10 deletions sapi/fpm/tests/status.inc
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,34 @@ class Status
'slow requests' => '\d+',
];

/**
* @var Tester
*/
private Tester $tester;

/**
* @param Tester $tester
*/
public function __construct(Tester $tester)
{
$this->tester = $tester;
}

/**
* @param string $body
* @param string $pattern
* @return void
*/
private function matchError(string $body, string $pattern): void
{
echo "ERROR: Expected body does not match pattern\n";
echo "BODY:\n";
var_dump($body);
echo "PATTERN:\n";
var_dump($pattern);
$this->tester->printLogs();
}

/**
* Check status page.
*
Expand Down Expand Up @@ -106,11 +134,7 @@ class Status
$pattern .= $footer . ')';

if (!preg_match($pattern, $body)) {
echo "ERROR: Expected body does not match pattern\n";
echo "BODY:\n";
var_dump($body);
echo "PATTERN:\n";
var_dump($pattern);
$this->matchError($body, $pattern);
}
}

Expand Down Expand Up @@ -245,11 +269,7 @@ class Status
"# EOF)\n";

if (!preg_match($pattern, $body)) {
echo "ERROR: Expected body does not match pattern\n";
echo "BODY:\n";
var_dump($body);
echo "PATTERN:\n";
var_dump($pattern);
$this->matchError($body, $pattern);
}
}
}
Loading