Skip to content

Commit

Permalink
Testing packages just after they're built (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
intuibase authored Jun 24, 2024
1 parent 565f188 commit 805137d
Show file tree
Hide file tree
Showing 7 changed files with 197 additions and 38 deletions.
24 changes: 20 additions & 4 deletions packaging/nfpm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,18 @@ section: "default"
priority: "extra"

replaces:
- elastic-otel-php
provides:
- elastic-otel-php
- apm-agent-php

overrides:
rpm:
provides:
- elastic-otel-php
deb:
provides:
- elastic-otel-php

depends:
- bash
maintainer: "APM Team <[email protected]>"
description: |
Elastic OpenTelemetry PHP distribution
Expand All @@ -29,7 +38,14 @@ contents:
expand: true
- src: /source/prod/php
dst: /opt/elastic/elastic-otel-php/php
expand: true
type: tree
packager: rpm
- src: /source/prod/php
dst: /opt/elastic/elastic-otel-php/php
packager: deb
- src: /source/prod/php
dst: /opt/elastic/elastic-otel-php/php
packager: apk
- src: /source/packaging/configs/elastic-otel-custom-template.ini
dst: /opt/elastic/elastic-otel-php/etc/elastic-otel-custom.ini
type: config
Expand Down
42 changes: 42 additions & 0 deletions packaging/test/smokeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

const CRED = "\033[31m";
const CGREEN = "\033[32m";
const CDEF = "\033[39m";

echo CGREEN."Starting package smoke test\n".CDEF;

echo "Checking if extension is loaded: ";
if (array_search("elastic_otel", get_loaded_extensions()) === false) {
echo CRED."FAILED. Elastic OpenTelemetry extension not found\n".CDEF;
exit(1);
}
echo CGREEN."OK\n".CDEF;

echo "Looking for internal function 'elastic_otel_is_enabled': ";
if (array_search("elastic_otel_is_enabled", get_extension_funcs("elastic_otel")) === false) {
echo CRED."FAILED. Elastic OpenTelemetry extension function 'elastic_otel_is_enabled' not found\n".CDEF;
exit(1);
}
echo CGREEN."OK\n".CDEF;


echo "Checking if extension is enabled: ";
if (elastic_otel_is_enabled() !== true) {
echo CRED."FAILED. Elastic OpenTelemetry extension is not enabled\n".CDEF;
exit(1);
}
echo CGREEN."OK\n".CDEF;

echo "Looking for PhpPartFacade class: ";
if (array_search("Elastic\OTel\AutoInstrument\PhpPartFacade", get_declared_classes()) === false) {
echo CRED."FAILED. Elastic\OTel\AutoInstrument\PhpPartFacade class not found. Bootstrap failed\n".CDEF;
exit(1);
}
echo CGREEN."OK\n".CDEF;

echo "Trying to log something to stderr: ";
Elastic\OTel\AutoInstrument\BootstrapStageLogger::logCritical("This is just a message to test logger", __LINE__, __FUNCTION__);
echo CGREEN."OK\n".CDEF;

echo CGREEN."Smoke test passed\n".CDEF;
30 changes: 30 additions & 0 deletions packaging/test/smokeTestUninstalled.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

const CRED = "\033[31m";
const CGREEN = "\033[32m";
const CDEF = "\033[39m";

echo CGREEN."Starting package uninstalled smoke test\n".CDEF;

echo "Checking if extension is loaded: ";
if (array_search("elastic_otel", get_loaded_extensions()) !== false) {
echo CRED."FAILED. Elastic OpenTelemetry extension found\n".CDEF;
exit(1);
}
echo CGREEN."OK\n".CDEF;

echo "Looking for internal function 'elastic_otel_is_enabled': ";
if (array_search("elastic_otel_is_enabled", get_defined_functions()["internal"]) !== false) {
echo CRED."FAILED. Elastic OpenTelemetry extension function 'elastic_otel_is_enabled' found\n".CDEF;
exit(1);
}
echo CGREEN."OK\n".CDEF;

echo "Looking for PhpPartFacade class: ";
if (array_search("Elastic\OTel\AutoInstrument\PhpPartFacade", get_declared_classes()) !== false) {
echo CRED."FAILED. Elastic\OTel\AutoInstrument\PhpPartFacade class not found. Bootstrap failed\n".CDEF;
exit(1);
}
echo CGREEN."OK\n".CDEF;

echo CGREEN."Smoke test passed\n".CDEF;
20 changes: 10 additions & 10 deletions prod/php/ElasticOTel/AutoInstrument/BootstrapStageLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ public static function logTrace(
/** @noinspection PhpUndefinedConstantInspection */
self::logWithLevel(
/**
* ELASTIC_APM_* constants are provided by the elastic_apm extension
* ELASTIC_OTEL_* constants are provided by the elastic_otel_php extension
*
* @phpstan-ignore-next-line
*/
ELASTIC_APM_LOG_LEVEL_TRACE,
ELASTIC_OTEL_LOG_LEVEL_TRACE,
$message,
$srcCodeLine,
$srcCodeFunc
Expand All @@ -67,11 +67,11 @@ public static function logDebug(
/** @noinspection PhpUndefinedConstantInspection */
self::logWithLevel(
/**
* ELASTIC_APM_* constants are provided by the elastic_apm extension
* ELASTIC_OTEL_* constants are provided by the elastic_otel_php extension
*
* @phpstan-ignore-next-line
*/
ELASTIC_APM_LOG_LEVEL_DEBUG,
ELASTIC_OTEL_LOG_LEVEL_DEBUG,
$message,
$srcCodeLine,
$srcCodeFunc
Expand All @@ -86,11 +86,11 @@ public static function logWarning(
/** @noinspection PhpUndefinedConstantInspection */
self::logWithLevel(
/**
* ELASTIC_APM_* constants are provided by the elastic_apm extension
* ELASTIC_OTEL_* constants are provided by the elastic_otel_php extension
*
* @phpstan-ignore-next-line
*/
ELASTIC_APM_LOG_LEVEL_WARNING,
ELASTIC_OTEL_LOG_LEVEL_WARNING,
$message,
$srcCodeLine,
$srcCodeFunc
Expand All @@ -105,11 +105,11 @@ public static function logCritical(
/** @noinspection PhpUndefinedConstantInspection */
self::logWithLevel(
/**
* ELASTIC_APM_* constants are provided by the elastic_apm extension
* ELASTIC_OTEL_* constants are provided by the elastic_otel_php extension
*
* @phpstan-ignore-next-line
*/
ELASTIC_APM_LOG_LEVEL_CRITICAL,
ELASTIC_OTEL_LOG_LEVEL_CRITICAL,
$message,
$srcCodeLine,
$srcCodeFunc
Expand Down Expand Up @@ -142,12 +142,12 @@ private static function logWithLevel(
}

/**
* elastic_apm_* functions are provided by the elastic_apm extension
* elastic_otel_php_* functions are provided by the elastic_otel_php extension
*
* @noinspection PhpFullyQualifiedNameUsageInspection, PhpUndefinedFunctionInspection
* @phpstan-ignore-next-line
*/
\elastic_apm_log(
\elastic_otel_log(
0 /* $isForced */,
$statementLevel,
'Bootstrap' /* category */,
Expand Down
64 changes: 40 additions & 24 deletions prod/php/ElasticOTel/AutoInstrument/PhpPartFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
*
* @internal
*
* Called by elastic_apm extension
* Called by elastic_otel extension
*
* @noinspection PhpUnused
*/
Expand All @@ -57,25 +57,25 @@ final class PhpPartFacade

private function __construct(float $requestInitStartTime)
{
if (!ElasticApmExtensionUtil::isLoaded()) {
throw new RuntimeException(ElasticApmExtensionUtil::EXTENSION_NAME . ' extension is not loaded');
}

$tracer = self::buildTracer();
if ($tracer === null) {
BootstrapStageLogger::logDebug(
'Cutting bootstrap sequence short - tracing is disabled',
__LINE__,
__FUNCTION__
);
return;
}

$this->transactionForExtensionRequest = new TransactionForExtensionRequest($requestInitStartTime);
// if (!ElasticApmExtensionUtil::isLoaded()) {
// throw new RuntimeException(ElasticApmExtensionUtil::EXTENSION_NAME . ' extension is not loaded');
// }

// $tracer = self::buildTracer();
// if ($tracer === null) {
// BootstrapStageLogger::logDebug(
// 'Cutting bootstrap sequence short - tracing is disabled',
// __LINE__,
// __FUNCTION__
// );
// return;
// }

// $this->transactionForExtensionRequest = new TransactionForExtensionRequest($requestInitStartTime);
}

/**
* Called by elastic_apm extension
* Called by elastic_otel extension
*
* @noinspection PhpUnused
*
Expand Down Expand Up @@ -233,14 +233,14 @@ private static function ensureHaveLastErrorData(
private static function ensureHaveLastThrown(TransactionForExtensionRequest $transactionForExtensionRequest): void
{
/**
* elastic_apm_* functions are provided by the elastic_apm extension
* elastic_otel_* functions are provided by the elastic_otel extension
*
* @var mixed $lastThrown
*
* @noinspection PhpFullyQualifiedNameUsageInspection, PhpUndefinedFunctionInspection
* @phpstan-ignore-next-line
*/
$lastThrown = \elastic_apm_get_last_thrown();
$lastThrown = \elastic_otel_get_last_thrown();
if ($lastThrown === null) {
return;
}
Expand Down Expand Up @@ -376,25 +376,25 @@ private static function buildPhpErrorData(array $dataFromExt): PhpErrorData
private static function ensureHaveLastPhpError(TransactionForExtensionRequest $transactionForExtensionRequest): void
{
/**
* elastic_apm_* functions are provided by the elastic_apm extension
* elastic_otel_* functions are provided by the elastic_otel extension
*
* @noinspection PhpFullyQualifiedNameUsageInspection, PhpUndefinedFunctionInspection
* @phpstan-ignore-next-line
*/
$lastPhpErrorData = \elastic_apm_get_last_php_error();
$lastPhpErrorData = \elastic_otel_get_last_php_error();
if ($lastPhpErrorData === null) {
return;
}

if (is_array($lastPhpErrorData)) {
BootstrapStageLogger::logDebug(
'Type of value returned by elastic_apm_get_last_php_error(): ' . DbgUtil::getType($lastPhpErrorData),
'Type of value returned by elastic_otel_get_last_php_error(): ' . DbgUtil::getType($lastPhpErrorData),
__LINE__,
__FUNCTION__
);
} else {
BootstrapStageLogger::logCritical(
'Value returned by elastic_apm_get_last_php_error() is not an array'
'Value returned by elastic_otel_get_last_php_error() is not an array'
. ', ' . 'returned value type: ' . DbgUtil::getType($lastPhpErrorData)
. ', ' . 'returned value: ' . $lastPhpErrorData,
__LINE__,
Expand All @@ -408,7 +408,7 @@ private static function ensureHaveLastPhpError(TransactionForExtensionRequest $t
}

/**
* Called by elastic_apm extension
* Called by elastic_otel extension
*
* @noinspection PhpUnused
*/
Expand All @@ -423,4 +423,20 @@ function (TransactionForExtensionRequest $transactionForExtensionRequest): void

self::$singletonInstance = null;
}

/**
* Called by elastic_otel extension
*
* @noinspection PhpUnused
*
* @param int $maxEnabledLogLevel
* @param float $requestInitStartTime
*
* @return bool
*/
public static function handle_error()
{
var_dump(func_get_args());
}

}
2 changes: 2 additions & 0 deletions prod/php/bootstrap_php_part.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
use Elastic\OTel\SrcRootDir;

require __DIR__ . '/ElasticOTel/SrcRootDir.php';
require __DIR__ . '/ElasticOTel/Util/HiddenConstructorTrait.php';

SrcRootDir::$fullPath = __DIR__;

require __DIR__ . '/ElasticOTel/AutoInstrument/bootstrap_php_part.php';
53 changes: 53 additions & 0 deletions tools/build/build_packages.sh
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,56 @@ if [[ -z "$PACKAGE_VERSION" ]] || [[ -z "$BUILD_ARCHITECUTRE" ]] || [[ -z "$PACK
exit 1
fi

test_package() {
local PHP_VERSION=8.3
local PKG_TYPE=$1
local PKG_FILENAME=$2

echo ${PKG_FILENAME}

echo "Starting ${PKG_FILENAME} smoke test"

local TEST_LICENSE_FILES="echo -n 'Checking for \"copyright\" files existence: ' && test -f /opt/elastic/elastic-otel-php/LICENSE && test -f /opt/elastic/elastic-otel-php/NOTICE && echo -e '\033[0;32mOK\033[0;39m'"

case "${PKG_TYPE}" in
"apk")
local INSTALL_SMOKE="apk add --allow-untrusted --verbose --no-cache /source/build/packages/${PKG_FILENAME} && php /source/packaging/test/smokeTest.php"
local UNINSTALL_SMOKE="apk del --verbose --no-cache elastic-otel-php && php /source/packaging/test/smokeTestUninstalled.php"
docker run --rm \
-v ${PWD}:/source \
-e ELASTIC_OTEL_LOG_LEVEL_STDERR=error \
php:${PHP_VERSION}-alpine sh -c "ls /source/build/packages && ${INSTALL_SMOKE} && ${TEST_LICENSE_FILES} && ${UNINSTALL_SMOKE} && ls -alR /opt/elastic"
;;
"deb")
local INSTALL_SMOKE="dpkg -i /source/build/packages/${PKG_FILENAME} && php /source/packaging/test/smokeTest.php"
local UNINSTALL_SMOKE="dpkg --purge elastic-otel-php && php /source/packaging/test/smokeTestUninstalled.php"
docker run --rm \
-v ${PWD}:/source \
-e ELASTIC_OTEL_LOG_LEVEL_STDERR=error \
php:${PHP_VERSION} sh -c "ls /source/build/packages && ${INSTALL_SMOKE} && ${TEST_LICENSE_FILES} && ${UNINSTALL_SMOKE} && ls -alR /opt/elastic"
;;
"rpm")
local INSTALL_PHP="cat /etc/redhat-release && dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-\$(grep -oP '(?<=release )\d+' /etc/redhat-release).noarch.rpm -y && dnf install https://rpms.remirepo.net/enterprise/remi-release-\$(grep -oP '(?<=release )\d+\.\d+' /etc/redhat-release).rpm -y && dnf install --setopt=install_weak_deps=False -y php${PHP_VERSION//./} php${PHP_VERSION//./}-syspaths"
local INSTALL_SMOKE="rpm -ivh /source/build/packages/${PKG_FILENAME} && php /source/packaging/test/smokeTest.php"
local UNINSTALL_SMOKE="rpm -ve elastic-otel-php && php /source/packaging/test/smokeTestUninstalled.php"

docker run --rm \
-v ${PWD}:/source \
-e ELASTIC_OTEL_LOG_LEVEL_STDERR=error \
redhat/ubi9 sh -c "ls /source/build/packages && ${INSTALL_PHP} && ${INSTALL_SMOKE} && ${TEST_LICENSE_FILES} && ${UNINSTALL_SMOKE} && ls -alR /opt/elastic"
;;
*)
echo -e "\033[0;33mPackage ${PKG_FILENAME} can't be tested because smoke test is not implemented\033[0;39m"
;;
esac

if [ $? -ne 0 ]; then
echo -e "\033[0;31mPackage ${PKG_FILENAME} smoke test FAILED\033[0;39m"
exit 1
fi

}

if [ "${PACKAGE_SHA}" == "unknown" ]; then
GITCMD=$(command -v git)
if [ $? -eq 0 ]; then
Expand Down Expand Up @@ -106,6 +156,9 @@ do
pushd "${PWD}/build/packages"
md5sum "${PKG_FILENAME}" >"${PKG_FILENAME}".sha512
popd

test_package ${pkg} "${PKG_FILENAME}"

done

rm ${PWD}/build/packages/nfpm.yaml
Expand Down

0 comments on commit 805137d

Please sign in to comment.