From 9760ee47b01976740f9413a2d1ba0a1bb5ce25d9 Mon Sep 17 00:00:00 2001 From: Thibault RICHARD Date: Mon, 14 Aug 2023 13:01:35 +0200 Subject: [PATCH 1/4] Make sure Bref layers are always added --- index.js | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/index.js b/index.js index d78895b7a..8612cd35f 100644 --- a/index.js +++ b/index.js @@ -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), ); @@ -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), ); From 38d2e3aea1e25a41568c8916911b317261ce5807 Mon Sep 17 00:00:00 2001 From: Matthieu Napoli Date: Mon, 14 Aug 2023 15:15:50 +0200 Subject: [PATCH 2/4] Add tests for the JS serverless plugin --- composer.json | 3 +- tests/Plugin/serverless-runtime-root.yml | 14 +++++ tests/Plugin/serverless.yml | 30 ++++++++++ tests/PluginTest.php | 70 ++++++++++++++++++++++++ 4 files changed, 116 insertions(+), 1 deletion(-) create mode 100644 tests/Plugin/serverless-runtime-root.yml create mode 100644 tests/Plugin/serverless.yml create mode 100644 tests/PluginTest.php diff --git a/composer.json b/composer.json index e50515bc4..7bfda7ba2 100644 --- a/composer.json +++ b/composer.json @@ -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": "^6.3" }, "scripts": { "test": [ diff --git a/tests/Plugin/serverless-runtime-root.yml b/tests/Plugin/serverless-runtime-root.yml new file mode 100644 index 000000000..206332e32 --- /dev/null +++ b/tests/Plugin/serverless-runtime-root.yml @@ -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 diff --git a/tests/Plugin/serverless.yml b/tests/Plugin/serverless.yml new file mode 100644 index 000000000..867f2253a --- /dev/null +++ b/tests/Plugin/serverless.yml @@ -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 diff --git a/tests/PluginTest.php b/tests/PluginTest.php new file mode 100644 index 000000000..4629a413e --- /dev/null +++ b/tests/PluginTest.php @@ -0,0 +1,70 @@ +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]); + } + } +} From 4d7301cdae3f249949c24680f0648ebe75ccf12c Mon Sep 17 00:00:00 2001 From: Matthieu Napoli Date: Mon, 14 Aug 2023 15:19:58 +0200 Subject: [PATCH 3/4] Relax Symfony constraints --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 7bfda7ba2..148a6061d 100644 --- a/composer.json +++ b/composer.json @@ -43,7 +43,7 @@ "phpstan/phpstan": "^1.10.26", "phpunit/phpunit": "^9.6.10", "symfony/console": "^4.4|^5.0|^6.0|^7.0", - "symfony/yaml": "^6.3" + "symfony/yaml": "^4.4|^5.0|^6.0|^7.0" }, "scripts": { "test": [ From f210d46693184299297eb5c2a5515433355c2cd3 Mon Sep 17 00:00:00 2001 From: Matthieu Napoli Date: Mon, 14 Aug 2023 15:22:06 +0200 Subject: [PATCH 4/4] Fix CI --- .github/workflows/ci.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 456f9c4c5..a7da45dcf 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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: