Skip to content

Commit

Permalink
Test filesystem integration
Browse files Browse the repository at this point in the history
  • Loading branch information
estringana committed Nov 26, 2024
1 parent f8a0af2 commit d72e8b7
Show file tree
Hide file tree
Showing 4 changed files with 228 additions and 28 deletions.
77 changes: 77 additions & 0 deletions tests/ext/telemetry/integration_filesystem_01.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
--TEST--
Filesystem integration depends on RASP. If RASP is not enabled, integration is disabled
--SKIPIF--
<?php
if (getenv('PHP_PEAR_RUNTESTS') === '1') die("skip: pecl run-tests does not support {PWD}");
if (PHP_OS === "WINNT" && PHP_VERSION_ID < 70400) die("skip: Windows on PHP 7.2 and 7.3 have permission issues with synchronous access to telemetry");
if (getenv('USE_ZEND_ALLOC') === '0' && !getenv("SKIP_ASAN")) die('skip timing sensitive test - valgrind is too slow');
require __DIR__ . '/../includes/clear_skipif_telemetry.inc'
?>
--ENV--
DD_TRACE_GENERATE_ROOT_SPAN=0
_DD_LOAD_TEST_INTEGRATIONS=1
DD_INSTRUMENTATION_TELEMETRY_ENABLED=1
--INI--
datadog.trace.agent_url="file://{PWD}/integration-telemetry.out"
--FILE--
<?php
namespace
{
dd_trace_internal_fn("finalize_telemetry");

for ($i = 0; $i < 100; ++$i) {
usleep(100000);
if (file_exists(__DIR__ . '/integration-telemetry.out')) {
foreach (file(__DIR__ . '/integration-telemetry.out') as $l) {
if ($l) {
$json = json_decode($l, true);
$batch = $json["request_type"] == "message-batch" ? $json["payload"] : [$json];
foreach ($batch as $json) {
if ($json["request_type"] == "app-integrations-change") {
var_dump($json["payload"]);
break 3;
}
}
}
}
}
}
}

?>
--EXPECT--
array(1) {
["integrations"]=>
array(2) {
[0]=>
array(5) {
["name"]=>
string(10) "filesystem"
["enabled"]=>
bool(false)
["version"]=>
string(0) ""
["compatible"]=>
NULL
["auto_enabled"]=>
NULL
}
[1]=>
array(5) {
["name"]=>
string(4) "logs"
["enabled"]=>
bool(false)
["version"]=>
string(0) ""
["compatible"]=>
NULL
["auto_enabled"]=>
NULL
}
}
}
--CLEAN--
<?php

@unlink(__DIR__ . '/integration-telemetry.out');
65 changes: 65 additions & 0 deletions tests/ext/telemetry/integration_filesystem_02.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
--TEST--
Filesystem integration depends on RASP. If RASP is enabled but wrapped functions are not used, it is not enabled
--SKIPIF--
<?php
if (getenv('PHP_PEAR_RUNTESTS') === '1') die("skip: pecl run-tests does not support {PWD}");
if (PHP_OS === "WINNT" && PHP_VERSION_ID < 70400) die("skip: Windows on PHP 7.2 and 7.3 have permission issues with synchronous access to telemetry");
if (getenv('USE_ZEND_ALLOC') === '0' && !getenv("SKIP_ASAN")) die('skip timing sensitive test - valgrind is too slow');
require __DIR__ . '/../includes/clear_skipif_telemetry.inc'
?>
--ENV--
DD_TRACE_GENERATE_ROOT_SPAN=0
_DD_LOAD_TEST_INTEGRATIONS=1
DD_INSTRUMENTATION_TELEMETRY_ENABLED=1
DD_APPSEC_RASP_ENABLED=1
--INI--
datadog.trace.agent_url="file://{PWD}/integration-telemetry.out"
--FILE--
<?php
namespace
{
dd_trace_internal_fn("finalize_telemetry");

for ($i = 0; $i < 100; ++$i) {
usleep(100000);
if (file_exists(__DIR__ . '/integration-telemetry.out')) {
foreach (file(__DIR__ . '/integration-telemetry.out') as $l) {
if ($l) {
$json = json_decode($l, true);
$batch = $json["request_type"] == "message-batch" ? $json["payload"] : [$json];
foreach ($batch as $json) {
if ($json["request_type"] == "app-integrations-change") {
var_dump($json["payload"]);
break 3;
}
}
}
}
}
}
}

?>
--EXPECT--
array(1) {
["integrations"]=>
array(1) {
[0]=>
array(5) {
["name"]=>
string(4) "logs"
["enabled"]=>
bool(false)
["version"]=>
string(0) ""
["compatible"]=>
NULL
["auto_enabled"]=>
NULL
}
}
}
--CLEAN--
<?php

@unlink(__DIR__ . '/integration-telemetry.out');
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
--TEST--
Filesystem integration depends on RASP. If RASP enabled, integration is enabled
Filesystem integration depends on RASP. If RASP is enabled and wrapped function are used, then it is enabled
--SKIPIF--
<?php
if (getenv('PHP_PEAR_RUNTESTS') === '1') die("skip: pecl run-tests does not support {PWD}");
Expand All @@ -16,32 +16,10 @@ DD_APPSEC_RASP_ENABLED=1
datadog.trace.agent_url="file://{PWD}/integration-telemetry.out"
--FILE--
<?php

namespace DDTrace\Test
{
class TestSandboxedIntegration implements \DDTrace\Integration
{
function init(): int
{
dd_trace_method("Test", "public_static_method", function() {
echo "test_access hook" . PHP_EOL;
});
return self::LOADED;
}
}
}

namespace
{
class Test
{
public static function public_static_method()
{
echo "PUBLIC STATIC METHOD\n";
}
}

Test::public_static_method();
//Here we use one wrapped function
file_put_contents('/tmp/dummy', 'foo');

dd_trace_internal_fn("finalize_telemetry");

Expand All @@ -66,15 +44,13 @@ namespace

?>
--EXPECT--
PUBLIC STATIC METHOD
test_access hook
array(1) {
["integrations"]=>
array(2) {
[0]=>
array(5) {
["name"]=>
string(37) "ddtrace\test\testsandboxedintegration"
string(10) "filesystem"
["enabled"]=>
bool(true)
["version"]=>
Expand Down
82 changes: 82 additions & 0 deletions tests/ext/telemetry/integration_filesystem_04.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
--TEST--
Filesystem integration can also be disabled with default integrations flag
--SKIPIF--
<?php
if (getenv('PHP_PEAR_RUNTESTS') === '1') die("skip: pecl run-tests does not support {PWD}");
if (PHP_OS === "WINNT" && PHP_VERSION_ID < 70400) die("skip: Windows on PHP 7.2 and 7.3 have permission issues with synchronous access to telemetry");
if (getenv('USE_ZEND_ALLOC') === '0' && !getenv("SKIP_ASAN")) die('skip timing sensitive test - valgrind is too slow');
require __DIR__ . '/../includes/clear_skipif_telemetry.inc'
?>
--ENV--
DD_TRACE_GENERATE_ROOT_SPAN=0
_DD_LOAD_TEST_INTEGRATIONS=1
DD_INSTRUMENTATION_TELEMETRY_ENABLED=1
DD_APPSEC_RASP_ENABLED=1
DD_TRACE_FILESYSTEM_ENABLED=0
--INI--
datadog.trace.agent_url="file://{PWD}/integration-telemetry.out"
--FILE--
<?php
namespace
{
//Here we use one wrapped function
file_put_contents('/tmp/dummy', 'foo');

dd_trace_internal_fn("finalize_telemetry");

for ($i = 0; $i < 100; ++$i) {
usleep(100000);
if (file_exists(__DIR__ . '/integration-telemetry.out')) {
foreach (file(__DIR__ . '/integration-telemetry.out') as $l) {
if ($l) {
$json = json_decode($l, true);
$batch = $json["request_type"] == "message-batch" ? $json["payload"] : [$json];
foreach ($batch as $json) {
if ($json["request_type"] == "app-integrations-change") {
var_dump($json["payload"]);
break 3;
}
}
}
}
}
}
}

?>
--EXPECT--
array(1) {
["integrations"]=>
array(2) {
[0]=>
array(5) {
["name"]=>
string(10) "filesystem"
["enabled"]=>
bool(false)
["version"]=>
string(0) ""
["compatible"]=>
NULL
["auto_enabled"]=>
NULL
}
[1]=>
array(5) {
["name"]=>
string(4) "logs"
["enabled"]=>
bool(false)
["version"]=>
string(0) ""
["compatible"]=>
NULL
["auto_enabled"]=>
NULL
}
}
}
--CLEAN--
<?php

@unlink(__DIR__ . '/integration-telemetry.out');

0 comments on commit d72e8b7

Please sign in to comment.