Skip to content
Snippets Groups Projects
Commit 7bb5af8f authored by znerol's avatar znerol
Browse files

Issue #3510996: Add integration test

parent 3dae656d
No related branches found
No related tags found
1 merge request!5Issue #3510996: Add integration test
Pipeline #440067 passed with warnings
......@@ -20,6 +20,9 @@
"open-telemetry/api": "^1.1",
"open-telemetry/sem-conv": "^1.0"
},
"require-dev": {
"drupal/otunit": "@dev"
},
"suggest": {
"open-telemetry/sdk": "To perform zero-code OpenTelemetry instrumentation"
}
......
name: 'otlrs test'
type: module
description: 'Support module for otlrs testing.'
package: Testing
otlog_test.debug:
path: '/otlog_test/debug'
defaults:
_controller: '\Drupal\otlog_test\TestControllers::debug'
requirements:
_access: 'TRUE'
otlog_test.exception:
path: '/otlog_test/error'
defaults:
_controller: '\Drupal\otlog_test\TestControllers::exception'
requirements:
_access: 'TRUE'
<?php
declare(strict_types=1);
namespace Drupal\otlog_test;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\HttpFoundation\Response;
/**
* Controller routines for testing otlog module.
*/
class TestControllers extends ControllerBase {
/**
* Returns test response.
*/
public function debug() {
$this->getLogger('test controller')->debug('test {level} message', [
'level' => 'debug',
]);
return new Response('ok');
}
/**
* Throws test exception.
*/
public function exception() {
throw new \RuntimeException('test exception');
}
}
<?php
declare(strict_types=1);
namespace Drupal\Tests\otlog\Functional;
use Drupal\Component\OTUnit\FileExporterTrait;
use Drupal\Component\OTUnit\ProtoHelpersTrait;
use Drupal\Component\OTUnit\SdkAutoloadingTrait;
use Drupal\otlog\Logger\OtLog;
use Drupal\Tests\BrowserTestBase;
use Opentelemetry\Proto\Logs\V1\LogRecord;
use OpenTelemetry\SemConv\TraceAttributes;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;
/**
* End-to-end test for OpenTelemetry SDK autoloading and configuration.
*/
#[Group('otlog')]
#[CoversClass(OtLog::class)]
final class LogIntegrationTest extends BrowserTestBase {
use FileExporterTrait;
use ProtoHelpersTrait;
use SdkAutoloadingTrait;
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* {@inheritdoc}
*/
protected static $modules = ['otlog', 'otlog_test'];
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->setupSdkAutoloading($this->getFileExporterConfigurationVariables());
}
/**
* Tests a controller generating a debug message.
*/
public function testDebugMessage(): void {
$this->resetFileExporterResultFiles();
$this->drupalGet('/otlog_test/debug');
[$logMessage] = $this->waitForLogs();
// Check resource attribute.
$traceResource = $this->getLogResource($logMessage);
$sdkName = $this->getAttributeValue(
TraceAttributes::TELEMETRY_SDK_NAME,
$traceResource->getAttributes()
);
$this->assertNotNull($sdkName);
$this->assertTrue($sdkName->hasStringValue());
$this->assertEquals('opentelemetry', $sdkName->getStringValue());
// Check scope.
$scope = $this->getLogScope($logMessage);
$this->assertEquals(
'org.drupal.otlog',
$scope->getName()
);
$logs = $this->getLogRecords($logMessage);
assert($logs[0] instanceof LogRecord);
$body = $logs[0]->getBody();
$this->assertNotNull($body);
$this->assertTrue($body->hasStringValue());
$this->assertEquals('[test controller] [debug] test debug message', $body->getStringValue());
$this->assertEquals(5, $logs[0]->getSeverityNumber());
$this->assertEquals('debug', $logs[0]->getSeverityText());
}
/**
* Tests controller causing an uncaught exception.
*/
public function testException(): void {
$this->resetFileExporterResultFiles();
$this->drupalGet('/otlog_test/error');
[$logMessage] = $this->waitForLogs();
$logs = $this->getLogRecords($logMessage);
assert($logs[0] instanceof LogRecord);
$body = $logs[0]->getBody();
$this->assertNotNull($body);
$this->assertTrue($body->hasStringValue());
$this->assertStringStartsWith('[php] [error] RuntimeException: test exception in Drupal\otlog_test\TestControllers->exception() (line ', $body->getStringValue());
$this->assertStringEndsWith('/otlrs_test/src/TestControllers.php).', $body->getStringValue());
$this->assertEquals(17, $logs[0]->getSeverityNumber());
$this->assertEquals('error', $logs[0]->getSeverityText());
$codeStacktrace = $this->getAttributeValue(
TraceAttributes::CODE_STACKTRACE,
$logs[0]->getAttributes(),
);
$this->assertNotNull($codeStacktrace);
$this->assertTrue($codeStacktrace->hasStringValue());
$this->assertStringContainsString('TestControllers->exception()', $codeStacktrace->getStringValue());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment