Skip to content

Commit 0217946

Browse files
committed
Add test to check whether the plugin mounts the entire ssh dir
This test makes sure the plugin actually mounts the SSH directory into the container, if the dockerizePip and dockerSsh are given. It does this by creating a new known_hosts file and placing the RSA fingerprint of Github inside, then installing a package via Git, which requires this fingerprint to be present in known_hosts. If it is not present, the process will fail. This patch also adds minor debugging output, in case no requirements.txt is found.
1 parent 25ca180 commit 0217946

File tree

4 files changed

+40
-3
lines changed

4 files changed

+40
-3
lines changed

lib/pip.js

+3
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,9 @@ function installRequirementsIfNeeded(
491491

492492
// Skip requirements generation, if requirements file doesn't exist
493493
if (!requirementsFileExists(servicePath, options, fileName)) {
494+
serverless.cli.log(
495+
`Skipping generation of requirements: file ${fileName} not found`
496+
);
494497
return false;
495498
}
496499

test.js

+32-1
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ const {
88
removeSync,
99
readFile,
1010
copySync,
11+
ensureFileSync,
12+
appendFileSync,
1113
writeFileSync,
1214
statSync,
1315
pathExistsSync
1416
} = require('fs-extra');
1517
const { quote } = require('shell-quote');
16-
const { sep } = require('path');
18+
const { sep, resolve } = require('path');
1719

1820
const { getUserCachePath, sha256Path } = require('./lib/shared');
1921

@@ -579,6 +581,35 @@ test(
579581
{ skip: !hasPython(2) }
580582
);
581583

584+
test(
585+
'dockerSsh mounts entire ssh folder into docker',
586+
t => {
587+
process.chdir('tests/base');
588+
const path = npm(['pack', '../..']);
589+
npm(['i', path]);
590+
// create a known_hosts file with the rsa fingerprint of github
591+
// the plugin should mount the entire .ssh directory into the container
592+
const known_hosts_file = resolve(process.env.HOME, `./.ssh/known_hosts`);
593+
ensureFileSync(known_hosts_file);
594+
appendFileSync(
595+
known_hosts_file,
596+
'github.com ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAq2A7hRGmdnm9tUDbO9IDSwBK6TbQa+PXYPCPy6rbTrTtw7PHkccKrpp0yVhp5HdEIcKr6pLlVDBfOLX9QUsyCOV0wzfjIJNlGEYsdlLJizHhbn2mUjvSAHQqZETYP81eFzLQNnPHt4EVVUh7VfDESU84KezmD5QlWpXLmvU31/yMf+Se8xhHTvKSCZIFImWwoG6mbUoWf9nzpIoaSjB+weqqUUmpaaasXVal72J+UX2B+2RPW3RcT0eOzQgqlJL3RKrTJvdsjE3JEAvGq3lGHSZXy28G3skua2SmVi/w4yCE6gbODqnTWlg7+wC604ydGXA8VJiS5ap43JXiUFFAaQ=='
597+
);
598+
// verify this by installing a requirement via git+ssh
599+
sls([
600+
'--dockerizePip=true',
601+
'--dockerSsh=true',
602+
'--fileName=requirements-w-git-ssh.txt',
603+
'package'
604+
]);
605+
const zipfiles = listZipFiles('.serverless/sls-py-req-test.zip');
606+
// check if the requirement is actually in the archive
607+
t.true(zipfiles.includes(`boto3/__init__.py`), 'boto3 is packaged via ssh');
608+
t.end();
609+
},
610+
{ skip: !canUseDocker() }
611+
);
612+
582613
test(
583614
'py2.7 can package flask with slim & dockerizePip & slimPatterns options',
584615
async t => {

tests/base/requirements-w-git-ssh.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
flask
2+
bottle
3+
git+ssh://[email protected]/boto/boto3.git#egg=boto3

tests/base/serverless.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ custom:
1010
pythonRequirements:
1111
zip: ${opt:zip, self:custom.defaults.zip}
1212
dockerizePip: ${opt:dockerizePip, self:custom.defaults.dockerizePip}
13+
dockerSsh: ${opt:dockerSsh, self:custom.defaults.dockerSsh}
1314
slim: ${opt:slim, self:custom.defaults.slim}
1415
slimPatterns: ${file(./slimPatterns.yml):slimPatterns, self:custom.defaults.slimPatterns}
1516
slimPatternsAppendDefaults: ${opt:slimPatternsAppendDefaults, self:custom.defaults.slimPatternsAppendDefaults}
@@ -24,6 +25,7 @@ custom:
2425
slimPatternsAppendDefaults: true
2526
zip: false
2627
dockerizePip: false
28+
dockerSsh: false
2729
individually: false
2830
useStaticCache: true
2931
useDownloadCache: true
@@ -49,5 +51,3 @@ functions:
4951
package:
5052
include:
5153
- 'fn2/**'
52-
53-

0 commit comments

Comments
 (0)