Skip to content

Commit e03787b

Browse files
committed
Adapted CI to handle enabled set to false
1 parent 1b71824 commit e03787b

File tree

12 files changed

+194
-14
lines changed

12 files changed

+194
-14
lines changed

.ci/shared.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ export ELASTIC_APM_PHP_TESTS_APP_CODE_HOST_KINDS_SHORT_NAMES=("${ELASTIC_APM_PHP
2323
export ELASTIC_APM_PHP_TESTS_LEAF_GROUPS_SHORT_NAMES=(no_ext_svc with_ext_svc)
2424
export ELASTIC_APM_PHP_TESTS_GROUPS_SHORT_NAMES=("${ELASTIC_APM_PHP_TESTS_LEAF_GROUPS_SHORT_NAMES[@]}" smoke)
2525

26+
export ELASTIC_APM_PHP_TESTS_AGENT_ENABLED_CONFIG_DEFAULT=false
27+
2628
function ensureSyslogIsRunningImpl () {
2729
if ps -ef | grep -v 'grep' | grep -q 'syslogd' ; then
2830
echo 'Syslog is already started.'

.ci/static-check-unit-test.sh

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,13 @@ runPhpCoposerInstall
4242
composer run-script static_check
4343

4444
# Run unit tests
45+
composerCommandToRunUnitTests=(composer run-script --)
4546
phpUnitConfigFile=$(php ./tests/ElasticApmTests/Util/runSelectPhpUnitConfigFile.php --tests-type=unit)
46-
composer run-script -- run_unit_tests_custom_config -c "${phpUnitConfigFile}"
47+
composerCommandToRunUnitTests=("${composerCommand[@]}" run_unit_tests_custom_config -c "${phpUnitConfigFile})
48+
if [ "${ELASTIC_APM_PHP_TESTS_AGENT_ENABLED_CONFIG_DEFAULT:?}" == "false" ]; then
49+
composerCommandToRunUnitTests=("${AgentEnabledConfigUnitTest[@]}" --filter "AgentEnabledConfigUnitTest")
50+
fi
51+
"${composerCommandToRunUnitTests[@]}"
4752
ls -l ./build/unit-tests-phpunit-junit.xml
4853
4954
# Generate junit output for phpstan

.ci/validate_agent_installation.sh

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,16 @@ function runComponentTests () {
2222
phpUnitConfigFile=$(php ./tests/ElasticApmTests/Util/runSelectPhpUnitConfigFile.php --tests-type=component)
2323
composerCommand=("${composerCommand[@]}" -c "${phpUnitConfigFile}")
2424

25-
if [ -n "${ELASTIC_APM_PHP_TESTS_GROUP}" ] ; then
26-
composerCommand=("${composerCommand[@]}" --group "${ELASTIC_APM_PHP_TESTS_GROUP}")
27-
fi
28-
29-
if [ -n "${ELASTIC_APM_PHP_TESTS_FILTER}" ] ; then
30-
composerCommand=("${composerCommand[@]}" --filter "${ELASTIC_APM_PHP_TESTS_FILTER}")
25+
if [ "${ELASTIC_APM_PHP_TESTS_AGENT_ENABLED_CONFIG_DEFAULT:?}" == "true" ]; then
26+
if [ -n "${ELASTIC_APM_PHP_TESTS_GROUP}" ] ; then
27+
composerCommand=("${composerCommand[@]}" --group "${ELASTIC_APM_PHP_TESTS_GROUP}")
28+
fi
29+
30+
if [ -n "${ELASTIC_APM_PHP_TESTS_FILTER}" ] ; then
31+
composerCommand=("${composerCommand[@]}" --filter "${ELASTIC_APM_PHP_TESTS_FILTER}")
32+
fi
33+
else
34+
composerCommand=("${composerCommand[@]}" --filter "AgentEnabledConfigComponentTest")
3135
fi
3236

3337
local initialTimeoutInMinutes=30

agent/native/ext/lifecycle.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -507,11 +507,11 @@ void logImportantAgentInfo( const ConfigSnapshot* config, String calledFromFunc
507507
"; Custom changes: "
508508
"- agent is disabled by default (enabled: false)"
509509
"; config->enabled: %s."
510-
"; SAPI: %s"
510+
"; SAPI module name: %s"
511511
"; Called from: %s"
512512
, PHP_ELASTIC_APM_VERSION
513513
, boolToString( config->enabled )
514-
, isPhpRunningAsCliScript()
514+
, getPhpSapiModuleName()
515515
, calledFromFunc
516516
);
517517
}

agent/native/ext/util_for_PHP.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,11 @@ ResultCode callPhpFunctionRetZval( StringView phpFunctionName, uint32_t argsCoun
275275
return callPhpFunction( phpFunctionName, argsCount, args, consumeZvalRetVal, retVal );
276276
}
277277

278+
String getPhpSapiModuleName()
279+
{
280+
return sapi_module.name;
281+
}
282+
278283
bool isPhpRunningAsCliScript()
279284
{
280285
return strcmp( sapi_module.name, "cli" ) == 0;

agent/native/ext/util_for_PHP.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ ResultCode callPhpFunctionRetZval( StringView phpFunctionName, uint32_t argsCoun
8585

8686
void getArgsFromZendExecuteData( zend_execute_data *execute_data, size_t dstArraySize, zval dstArray[], uint32_t* argsCount );
8787

88+
String getPhpSapiModuleName();
8889
bool isPhpRunningAsCliScript();
8990
bool detectOpcachePreload();
9091
bool isScriptRestricedByOpcacheAPI();

agent/php/ElasticApm/Impl/Config/AllOptionsMetadata.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public static function get(): array
9393
OptionNames::DEV_INTERNAL => new NullableWildcardListOptionMetadata(),
9494
OptionNames::DISABLE_INSTRUMENTATIONS => new NullableWildcardListOptionMetadata(),
9595
OptionNames::DISABLE_SEND => new BoolOptionMetadata(/* default */ false),
96-
OptionNames::ENABLED => new BoolOptionMetadata(/* default */ true),
96+
OptionNames::ENABLED => new BoolOptionMetadata(/* default */ false),
9797
OptionNames::ENVIRONMENT => new NullableStringOptionMetadata(),
9898
OptionNames::GLOBAL_LABELS => new NullableLabelsOptionMetadata(),
9999
OptionNames::HOSTNAME => new NullableStringOptionMetadata(),
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
3+
/*
4+
* Licensed to Elasticsearch B.V. under one or more contributor
5+
* license agreements. See the NOTICE file distributed with
6+
* this work for additional information regarding copyright
7+
* ownership. Elasticsearch B.V. licenses this file to you under
8+
* the Apache License, Version 2.0 (the "License"); you may
9+
* not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*/
21+
22+
declare(strict_types=1);
23+
24+
namespace ElasticApmTests\ComponentTests;
25+
26+
use Elastic\Apm\Impl\Config\OptionNames;
27+
use ElasticApmTests\ComponentTests\Util\AppCodeHostParams;
28+
use ElasticApmTests\ComponentTests\Util\AppCodeRequestParams;
29+
use ElasticApmTests\ComponentTests\Util\AppCodeTarget;
30+
use ElasticApmTests\ComponentTests\Util\ComponentTestCaseBase;
31+
use ElasticApmTests\ComponentTests\Util\ExpectedEventCounts;
32+
use ElasticApmTests\Util\AssertMessageStack;
33+
use ElasticApmTests\Util\DataProviderForTestBuilder;
34+
use ElasticApmTests\Util\MixedMap;
35+
36+
/**
37+
* @group smoke
38+
* @group does_not_require_external_services
39+
*/
40+
final class AgentEnabledConfigComponentTest extends ComponentTestCaseBase
41+
{
42+
private const ENABLED_CONFIG_SEEN_BY_TEST = 'enabled_config_seen_by_test';
43+
44+
public static function appCodeForTestWhenAgentIsDisabledItShouldNotSendAnyData(MixedMap $appCodeArgs): void
45+
{
46+
$enabledConfigSeenByTest = $appCodeArgs->getBool(self::ENABLED_CONFIG_SEEN_BY_TEST);
47+
48+
/**
49+
* elastic_apm_* functions are provided by the elastic_apm extension
50+
*
51+
* @noinspection PhpFullyQualifiedNameUsageInspection, PhpUndefinedFunctionInspection
52+
* @phpstan-ignore-next-line
53+
*/
54+
$enabledConfigSeenByNativePart = \elastic_apm_is_enabled();
55+
self::assertSame($enabledConfigSeenByTest, $enabledConfigSeenByNativePart);
56+
}
57+
58+
/**
59+
* @return iterable<string, array{MixedMap}>
60+
*/
61+
public static function dataProviderForTestWhenAgentIsDisabledItShouldNotSendAnyData(): iterable
62+
{
63+
$result = (new DataProviderForTestBuilder())
64+
->addKeyedDimensionOnlyFirstValueCombinable(OptionNames::ENABLED, [null, true . false])
65+
->build();
66+
67+
return self::adaptToSmoke(DataProviderForTestBuilder::convertEachDataSetToMixedMap($result));
68+
}
69+
70+
/**
71+
* @dataProvider dataProviderForTestWhenAgentIsDisabledItShouldNotSendAnyData
72+
*/
73+
public function testWhenAgentIsDisabledItShouldNotSendAnyData(MixedMap $testArgs): void
74+
{
75+
AssertMessageStack::newScope(/* out */ $dbgCtx, AssertMessageStack::funcArgs());
76+
77+
$testCaseHandle = $this->getTestCaseHandle();
78+
$appCodeHost = $testCaseHandle->ensureMainAppCodeHost(
79+
function (AppCodeHostParams $appCodeParams) use ($testArgs): void {
80+
self::setConfigIfNotNull($testArgs, OptionNames::ENABLED, $appCodeParams);
81+
}
82+
);
83+
84+
$enabledConfigSeenByTest = $appCodeHost->appCodeHostParams->getEffectiveAgentConfig()->enabled();
85+
$dbgCtx->add(compact('enabledConfigSeenByTest'));
86+
$logger = self::getLogger(__NAMESPACE__, __CLASS__, __FILE__);
87+
($loggerProxy = $logger->ifInfoLevelEnabled(__LINE__, __FUNCTION__)) && $loggerProxy->log('', ['enabledConfigSeenByTest' => $enabledConfigSeenByTest]);
88+
89+
$appCodeHost->sendRequest(
90+
AppCodeTarget::asRouted([__CLASS__, 'appCodeForTestWhenAgentIsDisabledItShouldNotSendAnyData']),
91+
function (AppCodeRequestParams $appCodeRequestParams) use ($enabledConfigSeenByTest): void {
92+
$appCodeRequestParams->setAppCodeArgs([self::ENABLED_CONFIG_SEEN_BY_TEST => $enabledConfigSeenByTest]);
93+
}
94+
);
95+
96+
if ($enabledConfigSeenByTest) {
97+
$dataFromAgent = $this->waitForOneEmptyTransaction($testCaseHandle);
98+
self::assertCount(1, $dataFromAgent->idToTransaction);
99+
self::assertCount(1, $dataFromAgent->metadatas);
100+
} else {
101+
sleep(/* seconds */ 5);
102+
$dataFromAgent = $testCaseHandle->waitForDataFromAgent((new ExpectedEventCounts())->metadatas(0)->transactions(0), /* shouldValidate */ false);
103+
self::assertCount(0, $dataFromAgent->idToTransaction);
104+
}
105+
self::assertCount(0, $dataFromAgent->idToSpan);
106+
self::assertCount(0, $dataFromAgent->idToError);
107+
}
108+
}

tests/ElasticApmTests/ComponentTests/Util/ComponentTestCaseBase.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,11 +254,12 @@ protected static function implTestIsAutoInstrumentationEnabled(string $instrClas
254254
}
255255

256256
/**
257-
* @template T
257+
* @template TKey
258+
* @template TValue
258259
*
259-
* @param iterable<T> $variants
260+
* @param iterable<TKey, TValue> $variants
260261
*
261-
* @return iterable<T>
262+
* @return iterable<TKey, TValue>
262263
*/
263264
public static function adaptToSmoke(iterable $variants): iterable
264265
{

tests/ElasticApmTests/ComponentTests/Util/ConfigSnapshotForTests.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ final class ConfigSnapshotForTests implements LoggableInterface
6060
/** @var bool */
6161
public $deleteTempPhpIni;
6262

63+
/** @var ?bool */
64+
public $agentEnabledConfigDefault = null;
65+
6366
/** @var ?WildcardListMatcher */
6467
public $envVarsToPassThrough;
6568

0 commit comments

Comments
 (0)