|
| 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 | +} |
0 commit comments