Skip to content

Commit d591e42

Browse files
author
bweigel
committed
move caching tests to tape
1 parent 4ed641b commit d591e42

File tree

3 files changed

+220
-106
lines changed

3 files changed

+220
-106
lines changed

test.bats

Lines changed: 0 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -146,111 +146,6 @@ teardown() {
146146
test $(find puck -type d -name "*.egg-info*" | wc -l) -eq 0
147147
}
148148

149-
@test "py3.6 uses download cache with dockerizePip option" {
150-
cd tests/base
151-
npm i $(npm pack ../..)
152-
docker &> /dev/null || skip "docker not present"
153-
! uname -sm|grep Linux || groups|grep docker || id -u|egrep '^0$' || skip "can't dockerize on linux if not root & not in docker group"
154-
perl -p -i'.bak' -e 's/(pythonRequirements:$)/\1\n useDownloadCache: true/' serverless.yml
155-
sls --dockerizePip=true package
156-
USR_CACHE_DIR=`node -e 'console.log(require("../../lib/shared").getUserCachePath())'`
157-
ls $USR_CACHE_DIR/downloadCacheslspyc/http
158-
}
159-
160-
@test "py3.6 uses download cache with dockerizePip + cacheLocation option" {
161-
cd tests/base
162-
npm i $(npm pack ../..)
163-
docker &> /dev/null || skip "docker not present"
164-
! uname -sm|grep Linux || groups|grep docker || id -u|egrep '^0$' || skip "can't dockerize on linux if not root & not in docker group"
165-
perl -p -i'.bak' -e 's/(pythonRequirements:$)/\1\n useDownloadCache: true\n cacheLocation: .requirements-cache/' serverless.yml
166-
sls --dockerizePip=true package
167-
ls .requirements-cache/downloadCacheslspyc/http
168-
}
169-
170-
@test "py3.6 uses static and download cache" {
171-
cd tests/base
172-
npm i $(npm pack ../..)
173-
perl -p -i'.bak' -e 's/(pythonRequirements:$)/\1\n useDownloadCache: true\n useStaticCache: true/' serverless.yml
174-
sls package
175-
USR_CACHE_DIR=`node -e 'console.log(require("./node_modules/serverless-python-requirements/lib/shared").getUserCachePath())'`
176-
CACHE_FOLDER_HASH=$(md5sum <(grep -v boto3 requirements.txt|sort) | cut -d' ' -f1)_slspyc
177-
ls $USR_CACHE_DIR/$CACHE_FOLDER_HASH/flask
178-
ls $USR_CACHE_DIR/downloadCacheslspyc/http
179-
}
180-
181-
@test "py3.6 uses static and download cache with dockerizePip option" {
182-
cd tests/base
183-
npm i $(npm pack ../..)
184-
docker &> /dev/null || skip "docker not present"
185-
! uname -sm|grep Linux || groups|grep docker || id -u|egrep '^0$' || skip "can't dockerize on linux if not root & not in docker group"
186-
perl -p -i'.bak' -e 's/(pythonRequirements:$)/\1\n useDownloadCache: true\n useStaticCache: true/' serverless.yml
187-
sls --dockerizePip=true package
188-
USR_CACHE_DIR=`node -e 'console.log(require("../../lib/shared").getUserCachePath())'`
189-
CACHE_FOLDER_HASH=$(md5sum <(grep -v boto3 requirements.txt|sort) | cut -d' ' -f1)_slspyc
190-
ls $USR_CACHE_DIR/$CACHE_FOLDER_HASH/flask
191-
ls $USR_CACHE_DIR/downloadCacheslspyc/http
192-
}
193-
194-
@test "py3.6 uses static cache" {
195-
cd tests/base
196-
npm i $(npm pack ../..)
197-
perl -p -i'.bak' -e 's/(pythonRequirements:$)/\1\n useStaticCache: true/' serverless.yml
198-
sls package
199-
USR_CACHE_DIR=`node -e 'console.log(require("../../lib/shared").getUserCachePath())'`
200-
CACHE_FOLDER_HASH=$(md5sum <(grep -v boto3 requirements.txt|sort) | cut -d' ' -f1)_slspyc
201-
ls $USR_CACHE_DIR/$CACHE_FOLDER_HASH/flask
202-
ls $USR_CACHE_DIR/$CACHE_FOLDER_HASH/.completed_requirements
203-
}
204-
205-
@test "py3.6 uses static cache with cacheLocation option" {
206-
cd tests/base
207-
npm i $(npm pack ../..)
208-
perl -p -i'.bak' -e 's/(pythonRequirements:$)/\1\n useStaticCache: true\n cacheLocation: .requirements-cache/' serverless.yml
209-
sls package
210-
USR_CACHE_DIR=`node -e 'console.log(require("../../lib/shared").getUserCachePath())'`
211-
CACHE_FOLDER_HASH=$(md5sum <(grep -v boto3 requirements.txt|sort) | cut -d' ' -f1)_slspyc
212-
ls .requirements-cache/$CACHE_FOLDER_HASH/flask
213-
ls .requirements-cache/$CACHE_FOLDER_HASH/.completed_requirements
214-
}
215-
216-
@test "py3.6 checking that static cache actually pulls from cache (by poisoning it)" {
217-
cd tests/base
218-
npm i $(npm pack ../..)
219-
perl -p -i'.bak' -e 's/(pythonRequirements:$)/\1\n useStaticCache: true/' serverless.yml
220-
sls package
221-
cp .serverless/sls-py-req-test.zip ./puck
222-
USR_CACHE_DIR=`node -e 'console.log(require("../../lib/shared").getUserCachePath())'`
223-
CACHE_FOLDER_HASH=$(md5sum <(grep -v boto3 requirements.txt|sort) | cut -d' ' -f1)_slspyc
224-
echo "injected new file into static cache folder" > $USR_CACHE_DIR/$CACHE_FOLDER_HASH/injected_file_is_bad_form
225-
sls package
226-
[ `wc -c ./.serverless/sls-py-req-test.zip | awk '{ print $1 }'` -gt `wc -c ./puck | awk '{ print $1 }'` ]
227-
}
228-
229-
@test "py3.6 uses static cache with dockerizePip & slim option" {
230-
cd tests/base
231-
npm i $(npm pack ../..)
232-
docker &> /dev/null || skip "docker not present"
233-
! uname -sm|grep Linux || groups|grep docker || id -u|egrep '^0$' || skip "can't dockerize on linux if not root & not in docker group"
234-
perl -p -i'.bak' -e 's/(pythonRequirements:$)/\1\n useStaticCache: true/' serverless.yml
235-
sls --dockerizePip=true --slim=true package
236-
CACHE_FOLDER_HASH=$(md5sum <(grep -v boto3 requirements.txt|sort) | cut -d' ' -f1)_slspyc
237-
ls $USR_CACHE_DIR/$CACHE_FOLDER_HASH/flask
238-
unzip .serverless/sls-py-req-test.zip -d puck
239-
test $(find puck -name "*.pyc" | wc -l) -eq 0
240-
}
241-
242-
@test "py3.6 uses download cache with dockerizePip & slim option" {
243-
cd tests/base
244-
npm i $(npm pack ../..)
245-
docker &> /dev/null || skip "docker not present"
246-
! uname -sm|grep Linux || groups|grep docker || id -u|egrep '^0$' || skip "can't dockerize on linux if not root & not in docker group"
247-
perl -p -i'.bak' -e 's/(pythonRequirements:$)/\1\n useDownloadCache: true/' serverless.yml
248-
sls --dockerizePip=true --slim=true package
249-
ls $USR_CACHE_DIR/downloadCacheslspyc/http
250-
unzip .serverless/sls-py-req-test.zip -d puck
251-
test $(find puck -name "*.pyc" | wc -l) -eq 0
252-
}
253-
254149
@test "py2.7 can package flask with default options" {
255150
cd tests/base
256151
npm i $(npm pack ../..)

test.js

Lines changed: 218 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const {
1212
} = require('fs-extra');
1313
const { sep } = require('path');
1414

15-
const { getUserCachePath } = require('./lib/shared');
15+
const { getUserCachePath, md5Path } = require('./lib/shared');
1616

1717
const initialWorkingDir = process.cwd();
1818

@@ -688,3 +688,220 @@ test('py3.6 uses download cache with cacheLocation option', t => {
688688
);
689689
t.end();
690690
});
691+
692+
test(
693+
'py3.6 uses download cache with dockerizePip option',
694+
t => {
695+
process.chdir('tests/base');
696+
const path = npm(['pack', '../..']);
697+
npm(['i', path]);
698+
sls(['--useDownloadCache=true', '--dockerizePip=true', 'package']);
699+
const cachepath = getUserCachePath();
700+
t.true(
701+
pathExistsSync(`${cachepath}${sep}downloadCacheslspyc${sep}http`),
702+
'cache directoy exists'
703+
);
704+
t.end();
705+
},
706+
{ skip: !canUseDocker() }
707+
);
708+
709+
test('py3.6 uses download cache with dockerizePip + cacheLocation option', t => {
710+
process.chdir('tests/base');
711+
const path = npm(['pack', '../..']);
712+
npm(['i', path]);
713+
sls([
714+
'--useDownloadCache=true',
715+
'--dockerizePip=true',
716+
'--cacheLocation=.requirements-cache',
717+
'package'
718+
]);
719+
t.true(
720+
pathExistsSync(`.requirements-cache${sep}downloadCacheslspyc${sep}http`),
721+
'cache directoy exists'
722+
);
723+
t.end();
724+
});
725+
726+
test('py3.6 uses static and download cache', t => {
727+
process.chdir('tests/base');
728+
const path = npm(['pack', '../..']);
729+
npm(['i', path]);
730+
sls(['--useDownloadCache=true', '--useStaticCache=true', 'package']);
731+
const cachepath = getUserCachePath();
732+
const cacheFolderHash = md5Path('.serverless/requirements.txt');
733+
t.true(
734+
pathExistsSync(`${cachepath}${sep}downloadCacheslspyc${sep}http`),
735+
'http exists in download-cache'
736+
);
737+
t.true(
738+
pathExistsSync(`${cachepath}${sep}${cacheFolderHash}_slspyc${sep}flask`),
739+
'flask exists in static-cache'
740+
);
741+
t.end();
742+
});
743+
744+
test(
745+
'py3.6 uses static and download cache with dockerizePip option',
746+
t => {
747+
process.chdir('tests/base');
748+
const path = npm(['pack', '../..']);
749+
npm(['i', path]);
750+
sls([
751+
'--useDownloadCache=true',
752+
'--useStaticCache=true',
753+
'--dockerizePip=true',
754+
'package'
755+
]);
756+
const cachepath = getUserCachePath();
757+
const cacheFolderHash = md5Path('.serverless/requirements.txt');
758+
t.true(
759+
pathExistsSync(`${cachepath}${sep}downloadCacheslspyc${sep}http`),
760+
'http exists in download-cache'
761+
);
762+
t.true(
763+
pathExistsSync(`${cachepath}${sep}${cacheFolderHash}_slspyc${sep}flask`),
764+
'flask exists in static-cache'
765+
);
766+
t.end();
767+
},
768+
{ skip: !canUseDocker() }
769+
);
770+
771+
test('py3.6 uses static cache', t => {
772+
process.chdir('tests/base');
773+
const path = npm(['pack', '../..']);
774+
npm(['i', path]);
775+
sls(['--useStaticCache=true', 'package']);
776+
const cachepath = getUserCachePath();
777+
const cacheFolderHash = md5Path('.serverless/requirements.txt');
778+
t.true(
779+
pathExistsSync(`${cachepath}${sep}${cacheFolderHash}_slspyc${sep}flask`),
780+
'flask exists in static-cache'
781+
);
782+
t.true(
783+
pathExistsSync(
784+
`${cachepath}${sep}${cacheFolderHash}_slspyc${sep}.completed_requirements`
785+
),
786+
'.completed_requirements exists in static-cache'
787+
);
788+
789+
// py3.6 checking that static cache actually pulls from cache (by poisoning it)
790+
writeFileSync(
791+
`${cachepath}${sep}${cacheFolderHash}_slspyc${sep}injected_file_is_bad_form`,
792+
'injected new file into static cache folder'
793+
);
794+
sls(['--useStaticCache=true', 'package']);
795+
796+
const zipfiles = listZipFiles('.serverless/sls-py-req-test.zip');
797+
t.true(
798+
zipfiles.includes('injected_file_is_bad_form'),
799+
"static cache is really used when running 'sls package' again"
800+
);
801+
802+
t.end();
803+
});
804+
805+
test('py3.6 uses static cache with cacheLocation option', t => {
806+
process.chdir('tests/base');
807+
const path = npm(['pack', '../..']);
808+
npm(['i', path]);
809+
const cachepath = '.requirements-cache';
810+
sls(['--useStaticCache=true', `--cacheLocation=${cachepath}`, 'package']);
811+
const cacheFolderHash = md5Path('.serverless/requirements.txt');
812+
t.true(
813+
pathExistsSync(`${cachepath}${sep}${cacheFolderHash}_slspyc${sep}flask`),
814+
'flask exists in static-cache'
815+
);
816+
t.true(
817+
pathExistsSync(
818+
`${cachepath}${sep}${cacheFolderHash}_slspyc${sep}.completed_requirements`
819+
),
820+
'.completed_requirements exists in static-cache'
821+
);
822+
t.end();
823+
});
824+
825+
test(
826+
'py3.6 uses static cache with dockerizePip & slim option',
827+
t => {
828+
process.chdir('tests/base');
829+
const path = npm(['pack', '../..']);
830+
npm(['i', path]);
831+
sls([
832+
'--useStaticCache=true',
833+
'--dockerizePip=true',
834+
'--slim=true',
835+
'package'
836+
]);
837+
const cachepath = getUserCachePath();
838+
const cacheFolderHash = md5Path('.serverless/requirements.txt');
839+
t.true(
840+
pathExistsSync(`${cachepath}${sep}${cacheFolderHash}_slspyc${sep}flask`),
841+
'flask exists in static-cache'
842+
);
843+
t.true(
844+
pathExistsSync(
845+
`${cachepath}${sep}${cacheFolderHash}_slspyc${sep}.completed_requirements`
846+
),
847+
'.completed_requirements exists in static-cache'
848+
);
849+
850+
// py3.6 checking that static cache actually pulls from cache (by poisoning it)
851+
writeFileSync(
852+
`${cachepath}${sep}${cacheFolderHash}_slspyc${sep}injected_file_is_bad_form`,
853+
'injected new file into static cache folder'
854+
);
855+
sls([
856+
'--useStaticCache=true',
857+
'--dockerizePip=true',
858+
'--slim=true',
859+
'package'
860+
]);
861+
862+
const zipfiles = listZipFiles('.serverless/sls-py-req-test.zip');
863+
t.true(
864+
zipfiles.includes('injected_file_is_bad_form'),
865+
"static cache is really used when running 'sls package' again"
866+
);
867+
t.deepEqual(
868+
zipfiles.filter(filename => filename.endsWith('.pyc')),
869+
[],
870+
'no pyc files are packaged'
871+
);
872+
873+
t.end();
874+
},
875+
{ skip: !canUseDocker() }
876+
);
877+
878+
test(
879+
'py3.6 uses download cache with dockerizePip & slim option',
880+
t => {
881+
process.chdir('tests/base');
882+
const path = npm(['pack', '../..']);
883+
npm(['i', path]);
884+
sls([
885+
'--useDownloadCache=true',
886+
'--dockerizePip=true',
887+
'--slim=true',
888+
'package'
889+
]);
890+
const cachepath = getUserCachePath();
891+
t.true(
892+
pathExistsSync(`${cachepath}${sep}downloadCacheslspyc${sep}http`),
893+
'http exists in download-cache'
894+
);
895+
896+
const zipfiles = listZipFiles('.serverless/sls-py-req-test.zip');
897+
t.true(zipfiles.includes(`flask${sep}__init__.py`), 'flask is packaged');
898+
t.deepEqual(
899+
zipfiles.filter(filename => filename.endsWith('.pyc')),
900+
[],
901+
'no pyc files are packaged'
902+
);
903+
904+
t.end();
905+
},
906+
{ skip: !canUseDocker() }
907+
);

tests/base/serverless.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ custom:
1515
slimPatternsAppendDefaults: ${opt:slimPatternsAppendDefaults, self:custom.defaults.slimPatternsAppendDefaults}
1616
vendor: ${opt:vendor, ''}
1717
fileName: ${opt:fileName, 'requirements.txt'}
18+
useStaticCache: ${opt:useStaticCache, self:custom.defaults.useStaticCache}
1819
useDownloadCache: ${opt:useDownloadCache, self:custom.defaults.useDownloadCache}
1920
cacheLocation: ${opt:cacheLocation, self:custom.defaults.cacheLocation}
2021
defaults:
@@ -24,6 +25,7 @@ custom:
2425
zip: false
2526
dockerizePip: false
2627
individually: false
28+
useStaticCache: false
2729
useDownloadCache: false
2830
cacheLocation: ''
2931

0 commit comments

Comments
 (0)