Skip to content

Commit

Permalink
Merge pull request #41 from brefphp/fix-ini-override
Browse files Browse the repository at this point in the history
  • Loading branch information
mnapoli authored Dec 19, 2022
2 parents b4e3533 + 622f3d6 commit a11f839
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 3 deletions.
4 changes: 3 additions & 1 deletion layers/fpm/bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ set -e

# We don't compile PHP anymore, so the only way to configure where PHP looks for
# .ini files is via the PHP_INI_SCAN_DIR environment variable.
export PHP_INI_SCAN_DIR="/opt/bref/etc/php/conf.d:/var/task/php/conf.d"
# Note: we use that weird syntax to set the variable only if it's not already set (can be overridden)
: "${PHP_INI_SCAN_DIR:='/opt/bref/etc/php/conf.d:/var/task/php/conf.d'}"
export PHP_INI_SCAN_DIR

# We redirect stderr to stdout so that everything
# written on the output ends up in Cloudwatch automatically
Expand Down
4 changes: 3 additions & 1 deletion layers/function/bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ set -e

# We don't compile PHP anymore, so the only way to configure where PHP looks for
# .ini files is via the PHP_INI_SCAN_DIR environment variable.
export PHP_INI_SCAN_DIR="/opt/bref/etc/php/conf.d:/var/task/php/conf.d"
# Note: we use that weird syntax to set the variable only if it's not already set (can be overridden)
: "${PHP_INI_SCAN_DIR:='/opt/bref/etc/php/conf.d:/var/task/php/conf.d'}"
export PHP_INI_SCAN_DIR

while true
do
Expand Down
4 changes: 4 additions & 0 deletions tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ test-%: vendor
docker-compose up --detach php-$*-console-handler
docker-compose exec -T php-$*-console-handler php test_6_console_invocation.php \
|| (docker-compose logs php-$*-console-handler && exit 1) # print logs in case of failure
# Test that we can override PHP_INI_SCAN_DIR
docker-compose up --detach php-$*-handler-test7
docker-compose exec -T php-$*-handler-test7 php test_7_custom_ini_scan_dir.php \
|| (docker-compose logs php-$*-handler && exit 1) # print logs in case of failure
# Clean up containers
docker-compose down
echo "\033[1;32m✓ Tests succeeded\033[0m"
Expand Down
27 changes: 27 additions & 0 deletions tests/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ services:
ports: [ '9001:8080' ]
command: test_4_function_handler.php

php-80-handler-test7:
image: bref/${CPU_PREFIX}php-80
volumes: [ '.:/var/task:ro' ]
ports: [ '9004:8080' ]
command: test_4_function_handler.php
environment:
# Override for test 7
PHP_INI_SCAN_DIR: "/opt/bref/etc/php/conf.d/:/var/task/"

php-80-fpm-handler:
image: bref/${CPU_PREFIX}php-80-fpm
volumes: [ '.:/var/task:ro' ]
Expand All @@ -36,6 +45,15 @@ services:
ports: [ '9001:8080' ]
command: test_4_function_handler.php

php-81-handler-test7:
image: bref/${CPU_PREFIX}php-81
volumes: [ '.:/var/task:ro' ]
ports: [ '9004:8080' ]
command: test_4_function_handler.php
environment:
# Override for test 7
PHP_INI_SCAN_DIR: "/opt/bref/etc/php/conf.d/:/var/task/"

php-81-fpm-handler:
image: bref/${CPU_PREFIX}php-81-fpm
volumes: [ '.:/var/task:ro' ]
Expand All @@ -59,6 +77,15 @@ services:
ports: [ '9001:8080' ]
command: test_4_function_handler.php

php-82-handler-test7:
image: bref/${CPU_PREFIX}php-82
volumes: [ '.:/var/task:ro' ]
ports: [ '9004:8080' ]
command: test_4_function_handler.php
environment:
# Override for test 7
PHP_INI_SCAN_DIR: "/opt/bref/etc/php/conf.d/:/var/task/"

php-82-fpm-handler:
image: bref/${CPU_PREFIX}php-82-fpm
volumes: [ '.:/var/task:ro' ]
Expand Down
7 changes: 6 additions & 1 deletion tests/test_4_function_handler.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
<?php declare(strict_types=1);

return function ($event, \Bref\Context\Context $context) {
// Support for test 7
if ($event === 'list_extensions') {
return get_loaded_extensions();
}

return [
'event' => $event,
'server' => $_SERVER,
'memory_limit' => ini_get('memory_limit'),
];
};
};
47 changes: 47 additions & 0 deletions tests/test_7_custom_ini_scan_dir.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php declare(strict_types=1);

require_once __DIR__ . '/utils.php';

function post(string $url, string $params)
{
$ch = curl_init();

$jsonData = json_encode($params);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json', 'Content-Length: ' . strlen($jsonData)]);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);

$response = curl_exec($ch);

curl_close($ch);

if ($response === false) {
throw new Exception('Curl error: ' . curl_error($ch));
}

return $response;
}

$body = 'list_extensions';

try {
$response = post('http://127.0.0.1:8080/2015-03-31/functions/function/invocations', $body);
$response = json_decode($response, true, 512, JSON_THROW_ON_ERROR);
} catch (Throwable $e) {
error($e->getMessage() . PHP_EOL . $e->getTraceAsString());
}

if (! is_array($response)) {
error('The response is not an array');
}

// We changed PHP_INI_SCAN_DIR to `/var/task` to load `test_3_manual_extensions.ini`
// We check one of the extensions was indeed loaded
if (! in_array('intl', $response, true)) {
error('Could not override PHP_INI_SCAN_DIR, test_3_manual_extensions.ini was not loaded');
}

success('[Invoke] Function');

0 comments on commit a11f839

Please sign in to comment.