From a20a209b73086a51a89591403d1055e340823d20 Mon Sep 17 00:00:00 2001 From: Scott BonAmi Date: Fri, 15 Jun 2018 08:09:26 -0700 Subject: [PATCH 1/3] Add test case for modules with zip option --- test.bats | 20 ++++++++++++++++++++ tests/individually/serverless.yml | 8 +++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/test.bats b/test.bats index 4501ba52..71af3ca6 100755 --- a/test.bats +++ b/test.bats @@ -376,6 +376,26 @@ teardown() { ! ls puck2/pyaml } +@test "py2.7 can package module requirements with zip option" { + cd tests/individually + npm i $(npm pack ../..) + sls --zip=true --runtime=python2.7 package + unzip .serverless/hello1.zip -d puck + unzip .serverless/hello2.zip -d puck2 + ls puck/.requirements.zip puck/unzip_requirements.py + ls puck2/.requirements.zip puck2/unzip_requirements.py +} + +@test "py3.6 can package module requirements with zip option" { + cd tests/individually + npm i $(npm pack ../..) + sls --zip=true package + unzip .serverless/hello1.zip -d puck + unzip .serverless/hello2.zip -d puck2 + ls puck/.requirements.zip puck/unzip_requirements.py + ls puck2/.requirements.zip puck2/unzip_requirements.py +} + @test "py3.6 can package lambda-decorators using vendor option" { cd tests/base npm i $(npm pack ../..) diff --git a/tests/individually/serverless.yml b/tests/individually/serverless.yml index 427dba75..1e8af3cc 100644 --- a/tests/individually/serverless.yml +++ b/tests/individually/serverless.yml @@ -2,11 +2,17 @@ service: sls-py-req-test-indiv provider: name: aws - runtime: python3.6 + runtime: ${opt:runtime, 'python3.6'} package: individually: true +custom: + pythonRequirements: + zip: ${opt:zip, self:custom.defaults.zip} + defaults: + zip: false + functions: hello1: handler: handler1.hello From 6f7b22913cc6dd5aa5b64e1ca2b0911220cb3e0c Mon Sep 17 00:00:00 2001 From: Scott BonAmi Date: Fri, 15 Jun 2018 08:03:37 -0700 Subject: [PATCH 2/3] Add test case for non-Python runtimes --- test.bats | 14 ++++++++++++++ tests/individually/module3/handler3.js | 3 +++ tests/individually/serverless.yml | 8 ++++++++ 3 files changed, 25 insertions(+) create mode 100644 tests/individually/module3/handler3.js diff --git a/test.bats b/test.bats index 71af3ca6..efb5cf8f 100755 --- a/test.bats +++ b/test.bats @@ -396,6 +396,20 @@ teardown() { ls puck2/.requirements.zip puck2/unzip_requirements.py } +@test "py2.7 can package only python runtimes" { + cd tests/individually + npm i $(npm pack ../..) + sls --runtime=python2.7 package + unzip .serverless/module1-sls-py-req-test-indiv-dev-hello1.zip -d puck + unzip .serverless/module2-sls-py-req-test-indiv-dev-hello2.zip -d puck2 + unzip .serverless/hello3.zip -d puck3 + ls puck3/module3/handler3.js + ! ls puck/handler3.js + ! ls puck2/handler3.js + ! ls puck3/flask + ! ls puck3/pyaml +} + @test "py3.6 can package lambda-decorators using vendor option" { cd tests/base npm i $(npm pack ../..) diff --git a/tests/individually/module3/handler3.js b/tests/individually/module3/handler3.js new file mode 100644 index 00000000..d8694077 --- /dev/null +++ b/tests/individually/module3/handler3.js @@ -0,0 +1,3 @@ +exports.hello = function(event, context, callback) { + callback(null, { status: 200 }) +}; diff --git a/tests/individually/serverless.yml b/tests/individually/serverless.yml index 1e8af3cc..23578e9a 100644 --- a/tests/individually/serverless.yml +++ b/tests/individually/serverless.yml @@ -20,6 +20,14 @@ functions: hello2: handler: handler2.hello module: module2 + hello3: + handler: module3/handler3.hello + runtime: node6.10 + package: + exclude: + - '**/*' + include: + - 'module3/**/*' plugins: - serverless-python-requirements From 8fb9334c463833ad8abaca7c99dcad28e363a723 Mon Sep 17 00:00:00 2001 From: Scott BonAmi Date: Fri, 15 Jun 2018 08:10:04 -0700 Subject: [PATCH 3/3] Only pack requirements for Python runtimes --- lib/zip.js | 15 +++++++++++++++ test.bats | 16 ++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/lib/zip.js b/lib/zip.js index eba21976..a5a9a7fb 100644 --- a/lib/zip.js +++ b/lib/zip.js @@ -18,6 +18,11 @@ function addVendorHelper() { if (this.options.zip) { if (this.serverless.service.package.individually) { return BbPromise.resolve(values(this.serverless.service.functions)) + .filter(func => + (func.runtime || this.serverless.service.provider.runtime).match( + /^python.*/ + ) + ) .map(f => { if (!get(f, 'package.include')) { set(f, ['package', 'include'], []); @@ -64,6 +69,11 @@ function removeVendorHelper() { if (this.options.zip && this.options.cleanupZipHelper) { if (this.serverless.service.package.individually) { return BbPromise.resolve(values(this.serverless.service.functions)) + .filter(func => + (func.runtime || this.serverless.service.provider.runtime).match( + /^python.*/ + ) + ) .map(f => { if (!get(f, 'module')) { set(f, ['module'], '.'); @@ -96,6 +106,11 @@ function packRequirements() { if (this.options.zip) { if (this.serverless.service.package.individually) { return BbPromise.resolve(values(this.serverless.service.functions)) + .filter(f => + (f.runtime || this.serverless.service.provider.runtime).match( + /^python.*/ + ) + ) .map(f => { if (!get(f, 'module')) { set(f, ['module'], '.'); diff --git a/test.bats b/test.bats index efb5cf8f..a5b68d11 100755 --- a/test.bats +++ b/test.bats @@ -410,6 +410,22 @@ teardown() { ! ls puck3/pyaml } +@test "py2.7 can package only python runtimes with zip option" { + cd tests/individually + npm i $(npm pack ../..) + sls --zip=true --runtime=python2.7 package + unzip .serverless/hello3.zip -d puck3 + ! ls puck3/.requirements.zip puck3/unzip_requirements.py +} + +@test "py3.6 can package only python runtimes with zip option" { + cd tests/individually + npm i $(npm pack ../..) + sls --zip=true package + unzip .serverless/hello3.zip -d puck3 + ! ls puck3/.requirements.zip puck3/unzip_requirements.py +} + @test "py3.6 can package lambda-decorators using vendor option" { cd tests/base npm i $(npm pack ../..)