Skip to content

Commit

Permalink
feat: Add architecture to requirements cache directory name (#645)
Browse files Browse the repository at this point in the history
  • Loading branch information
mLupine authored Dec 1, 2021
1 parent 89b3bab commit 328cb01
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
3 changes: 2 additions & 1 deletion lib/pip.js
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,8 @@ async function installRequirementsIfNeeded(
const workingReqsFolder = getRequirementsWorkingPath(
reqChecksum,
requirementsTxtDirectory,
options
options,
serverless
);

// Check if our static cache is present and is valid
Expand Down
7 changes: 5 additions & 2 deletions lib/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,20 @@ function checkForAndDeleteMaxCacheVersions({ serverless, options, log }) {
* @param {string} subfolder
* @param {string} servicePath
* @param {Object} options
* @param {Object} serverless
* @return {string}
*/
function getRequirementsWorkingPath(
subfolder,
requirementsTxtDirectory,
options
options,
serverless
) {
// If we want to use the static cache
if (options && options.useStaticCache) {
if (subfolder) {
subfolder = subfolder + '_slspyc';
const architecture = serverless.service.provider.architecture || 'x86_64';
subfolder = `${subfolder}_${architecture}_slspyc`;
}
// If we have max number of cache items...

Expand Down
35 changes: 25 additions & 10 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2115,12 +2115,15 @@ test(
sls(['package']);
const cachepath = getUserCachePath();
const cacheFolderHash = sha256Path('.serverless/requirements.txt');
const arch = 'x86_64';
t.true(
pathExistsSync(`${cachepath}${sep}downloadCacheslspyc${sep}http`),
'http exists in download-cache'
);
t.true(
pathExistsSync(`${cachepath}${sep}${cacheFolderHash}_slspyc${sep}flask`),
pathExistsSync(
`${cachepath}${sep}${cacheFolderHash}_${arch}_slspyc${sep}flask`
),
'flask exists in static-cache'
);
t.end();
Expand All @@ -2137,12 +2140,15 @@ test(
sls(['--dockerizePip=true', 'package']);
const cachepath = getUserCachePath();
const cacheFolderHash = sha256Path('.serverless/requirements.txt');
const arch = 'x86_64';
t.true(
pathExistsSync(`${cachepath}${sep}downloadCacheslspyc${sep}http`),
'http exists in download-cache'
);
t.true(
pathExistsSync(`${cachepath}${sep}${cacheFolderHash}_slspyc${sep}flask`),
pathExistsSync(
`${cachepath}${sep}${cacheFolderHash}_${arch}_slspyc${sep}flask`
),
'flask exists in static-cache'
);
t.end();
Expand All @@ -2159,20 +2165,23 @@ test(
sls(['package']);
const cachepath = getUserCachePath();
const cacheFolderHash = sha256Path('.serverless/requirements.txt');
const arch = 'x86_64';
t.true(
pathExistsSync(`${cachepath}${sep}${cacheFolderHash}_slspyc${sep}flask`),
pathExistsSync(
`${cachepath}${sep}${cacheFolderHash}_${arch}_slspyc${sep}flask`
),
'flask exists in static-cache'
);
t.true(
pathExistsSync(
`${cachepath}${sep}${cacheFolderHash}_slspyc${sep}.completed_requirements`
`${cachepath}${sep}${cacheFolderHash}_${arch}_slspyc${sep}.completed_requirements`
),
'.completed_requirements exists in static-cache'
);

// py3.6 checking that static cache actually pulls from cache (by poisoning it)
writeFileSync(
`${cachepath}${sep}${cacheFolderHash}_slspyc${sep}injected_file_is_bad_form`,
`${cachepath}${sep}${cacheFolderHash}_${arch}_slspyc${sep}injected_file_is_bad_form`,
'injected new file into static cache folder'
);
sls(['package']);
Expand All @@ -2197,13 +2206,16 @@ test(
const cachepath = '.requirements-cache';
sls([`--cacheLocation=${cachepath}`, 'package']);
const cacheFolderHash = sha256Path('.serverless/requirements.txt');
const arch = 'x86_64';
t.true(
pathExistsSync(`${cachepath}${sep}${cacheFolderHash}_slspyc${sep}flask`),
pathExistsSync(
`${cachepath}${sep}${cacheFolderHash}_${arch}_slspyc${sep}flask`
),
'flask exists in static-cache'
);
t.true(
pathExistsSync(
`${cachepath}${sep}${cacheFolderHash}_slspyc${sep}.completed_requirements`
`${cachepath}${sep}${cacheFolderHash}_${arch}_slspyc${sep}.completed_requirements`
),
'.completed_requirements exists in static-cache'
);
Expand All @@ -2221,20 +2233,23 @@ test(
sls(['--dockerizePip=true', '--slim=true', 'package']);
const cachepath = getUserCachePath();
const cacheFolderHash = sha256Path('.serverless/requirements.txt');
const arch = 'x86_64';
t.true(
pathExistsSync(`${cachepath}${sep}${cacheFolderHash}_slspyc${sep}flask`),
pathExistsSync(
`${cachepath}${sep}${cacheFolderHash}_${arch}_slspyc${sep}flask`
),
'flask exists in static-cache'
);
t.true(
pathExistsSync(
`${cachepath}${sep}${cacheFolderHash}_slspyc${sep}.completed_requirements`
`${cachepath}${sep}${cacheFolderHash}_${arch}_slspyc${sep}.completed_requirements`
),
'.completed_requirements exists in static-cache'
);

// py3.6 checking that static cache actually pulls from cache (by poisoning it)
writeFileSync(
`${cachepath}${sep}${cacheFolderHash}_slspyc${sep}injected_file_is_bad_form`,
`${cachepath}${sep}${cacheFolderHash}_${arch}_slspyc${sep}injected_file_is_bad_form`,
'injected new file into static cache folder'
);
sls(['--dockerizePip=true', '--slim=true', 'package']);
Expand Down

0 comments on commit 328cb01

Please sign in to comment.