Skip to content

Commit

Permalink
Merge pull request #1615 from t-richard/fix-plugin-layers
Browse files Browse the repository at this point in the history
  • Loading branch information
mnapoli committed Aug 14, 2023
2 parents a61011f + f210d46 commit e2488b4
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 15 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ jobs:
- uses: actions/setup-node@v3
with:
node-version: 12
# serverless is required by some tests
- name: Install serverless
run: npm i -g serverless
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
"guzzlehttp/guzzle": "^7.5",
"phpstan/phpstan": "^1.10.26",
"phpunit/phpunit": "^9.6.10",
"symfony/console": "^4.4|^5.0|^6.0|^7.0"
"symfony/console": "^4.4|^5.0|^6.0|^7.0",
"symfony/yaml": "^4.4|^5.0|^6.0|^7.0"
},
"scripts": {
"test": [
Expand Down
22 changes: 8 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,22 +197,16 @@ class ServerlessPlugin {

const config = this.serverless.service;
const isArmGlobally = config.provider.architecture === 'arm64';

// Check provider config
if (this.runtimes.includes(config.provider.runtime || '')) {
config.provider.layers = includeBrefLayers(
config.provider.runtime,
config.provider.layers || [], // make sure it's an array
isArmGlobally,
);
config.provider.runtime = 'provided.al2';
}
const isBrefRuntimeGlobally = this.runtimes.includes(config.provider.runtime || '');

// Check functions config
for (const f of Object.values(config.functions || {})) {
if (f.runtime && this.runtimes.includes(f.runtime)) {
if (
(f.runtime && this.runtimes.includes(f.runtime)) ||
(!f.runtime && isBrefRuntimeGlobally)
) {
f.layers = includeBrefLayers(
f.runtime,
f.runtime || config.provider.runtime,
f.layers || [], // make sure it's an array
f.architecture === 'arm64' || (isArmGlobally && !f.architecture),
);
Expand All @@ -224,9 +218,9 @@ class ServerlessPlugin {
for (const construct of Object.values(this.serverless.configurationInput.constructs || {})) {
if (construct.type !== 'queue' && construct.type !== 'webhook') continue;
const f = construct.type === 'queue' ? construct.worker : construct.authorizer;
if (f && f.runtime && this.runtimes.includes(f.runtime)) {
if (f && (f.runtime && this.runtimes.includes(f.runtime) || !f.runtime && isBrefRuntimeGlobally) ) {
f.layers = includeBrefLayers(
f.runtime,
f.runtime || config.provider.runtime,
f.layers || [], // make sure it's an array
f.architecture === 'arm64' || (isArmGlobally && !f.architecture),
);
Expand Down
14 changes: 14 additions & 0 deletions tests/Plugin/serverless-runtime-root.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
service: bref
provider:
name: aws
runtime: php-83

plugins:
- ../../index.js

functions:
function:
handler: function.php
function-arm:
handler: function.php
architecture: arm64
30 changes: 30 additions & 0 deletions tests/Plugin/serverless.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
service: bref
provider:
name: aws

plugins:
- ../../index.js

functions:
function:
handler: function.php
runtime: php-83
fpm:
handler: fpm.php
runtime: php-83-fpm
console:
handler: console.php
runtime: php-83-console

function-arm:
handler: function.php
architecture: arm64
runtime: php-83
fpm-arm:
handler: fpm.php
architecture: arm64
runtime: php-83-fpm
console-arm:
handler: console.php
architecture: arm64
runtime: php-83-console
70 changes: 70 additions & 0 deletions tests/PluginTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php declare(strict_types=1);

namespace Bref\Test;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Process\Process;
use Symfony\Component\Yaml\Yaml;

class PluginTest extends TestCase
{
public function test the plugin adds the layers(): void
{
$output = $this->slsPrint('serverless.yml');

self::assertFunction($output['functions']['function'], 'provided.al2', [
'arn:aws:lambda:us-east-1:534081306603:layer:php-83:',
]);
self::assertFunction($output['functions']['fpm'], 'provided.al2', [
'arn:aws:lambda:us-east-1:534081306603:layer:php-83-fpm:',
]);
self::assertFunction($output['functions']['console'], 'provided.al2', [
'arn:aws:lambda:us-east-1:534081306603:layer:php-83:',
'arn:aws:lambda:us-east-1:534081306603:layer:console:',
]);

self::assertFunction($output['functions']['function-arm'], 'provided.al2', [
'arn:aws:lambda:us-east-1:534081306603:layer:arm-php-83:',
]);
self::assertFunction($output['functions']['fpm-arm'], 'provided.al2', [
'arn:aws:lambda:us-east-1:534081306603:layer:arm-php-83-fpm:',
]);
self::assertFunction($output['functions']['console-arm'], 'provided.al2', [
'arn:aws:lambda:us-east-1:534081306603:layer:arm-php-83:',
'arn:aws:lambda:us-east-1:534081306603:layer:console:',
]);
}

public function test the plugin adds the layers when the runtime is set in the provider(): void
{
$output = $this->slsPrint('serverless-runtime-root.yml');

self::assertFunction($output['functions']['function'], 'provided.al2', [
'arn:aws:lambda:us-east-1:534081306603:layer:php-83:',
]);
self::assertFunction($output['functions']['function-arm'], 'provided.al2', [
'arn:aws:lambda:us-east-1:534081306603:layer:arm-php-83:',
]);
}

private function slsPrint(string $configFile): array
{
$process = (new Process(
['serverless', 'print', '-c', $configFile],
cwd: __DIR__ . '/Plugin',
env: [
'SLS_TELEMETRY_DISABLED' => '1', // else we sometimes get HTTP errors (and its faster)
],
))->mustRun();
return Yaml::parse($process->getOutput());
}

private static function assertFunction(array $config, string $runtime, array $layers): void
{
self::assertEquals($runtime, $config['runtime']);
self::assertCount(count($layers), $config['layers']);
foreach ($layers as $index => $layer) {
self::assertStringStartsWith($layer, $config['layers'][$index]);
}
}
}

0 comments on commit e2488b4

Please sign in to comment.