Repository navigation
Description
Most of the tests that currently cover core functionalities depend heavily on (and sometimes test) internal implementation characteristics. While they should be testing whether given implementation produces desired outcome (treating its implementation more as a black box).
Current state of things is problematic for eventual internal improvements and refactors which occasionally we want to introduce, as in most of such cases proposed improvements need to be accompanied with counterproductive numerous updates to tests which are covering otherwise not altered functionalities .
At some point we've introduced a new (black box based) way of testing the internals. It's through runServerless utlity, which allows us create natural (as in real world) serverless instance, and inspect the produced outcome for chosen command. More details here: https://github.com/serverless/serverless/tree/master/test#unit-tests
This is issue is about refactoring test/unit/lib/plugins/package/lib/packageService.test.js to runServerless based variant.
To make refactor relatively easy:
- Spec for new tests is fully defined and is already committed in. It's in scope of this describe block:
serverless/test/unit/lib/plugins/package/lib/packageService.test.js
Lines 456 to 659 in 44d3986
describe.skip('TODO: lib/plugins/package/lib/packageService.test.js', () => { describe('service wide', () => { before(async () => { await runServerless({ fixture: 'packaging', cliArgs: ['package'], configExt: { package: { exclude: ['dir1', '!dir1/subdir3/**'], include: ['dir1/subdir2/**', '!dir1/subdir2/subsubdir1'], }, functions: { fnIndividual: { handler: 'index.handler', package: { individually: true, include: 'dir1/subdir3/**', exclude: 'dir1/subdir2' }, }, }, }, }); // Read path to serverless.service.artifact, unpack it to temporary folder, and // expose list of files it contains to a local variable }); it('should exclude defaults', () => { // Confirm ".gitignore" is not packaged // // Replaces partially // https://github.com/serverless/serverless/blob/b12d565ea0ad588445fb120e049db157afc7bf37/test/unit/lib/plugins/package/lib/packageService.test.js#L77-L85 }); it('should exclude service config', () => { // Confirm "serverless.yml" is not packaged // // Replaces partially // https://github.com/serverless/serverless/blob/b12d565ea0ad588445fb120e049db157afc7bf37/test/unit/lib/plugins/package/lib/packageService.test.js#L77-L85 }); it('should exclude default plugins localPath', () => { // Confirm ".serverless-plugins/index.js" is not packaged // // Replaces // https://github.com/serverless/serverless/blob/b12d565ea0ad588445fb120e049db157afc7bf37/test/unit/lib/plugins/package/lib/packageService.test.js#L87-L96 }); it('should support `package.exclude`', () => { // Confirm "dir1/subdir1/index.js" is not packaged // Confirm "dir1/subdir3/index.js" is packaged // // Replace // https://github.com/serverless/serverless/blob/b12d565ea0ad588445fb120e049db157afc7bf37/test/unit/lib/plugins/package/lib/packageService.test.js#L128-L145 // https://github.com/serverless/serverless/blob/b12d565ea0ad588445fb120e049db157afc7bf37/test/unit/lib/plugins/package/lib/packageService.test.js#L637-L649 }); it('should support `package.include`', () => { // Confirm "dir1/subdir2/index.js" is packaged // Confirm "dir1/subdir2/subsubdir1/index.js" is not packaged // Confirm "dir1/subdir2/subsubdir2/index.js" is packaged // // Replaces // https://github.com/serverless/serverless/blob/b12d565ea0ad588445fb120e049db157afc7bf37/test/unit/lib/plugins/package/lib/packageService.test.js#L43-L50 // https://github.com/serverless/serverless/blob/b12d565ea0ad588445fb120e049db157afc7bf37/test/unit/lib/plugins/package/lib/packageService.test.js#L625-L635 // https://github.com/serverless/serverless/blob/b12d565ea0ad588445fb120e049db157afc7bf37/test/unit/lib/plugins/package/lib/packageService.test.js#L651-L662 }); it('should support `functions[].package.individually`', () => { // Confirm there's functions.fnIndividual.package.artifact // Replace // https://github.com/serverless/serverless/blob/b12d565ea0ad588445fb120e049db157afc7bf37/test/unit/lib/plugins/package/lib/packageService.test.js#L201-L225 }); it('should support `functions[].package.exclude`', () => { // Confirm that in function dedicated artifact "dir1/subdir2/subsubdir2/index.js" is not packaged // // Replaces // https://github.com/serverless/serverless/blob/b12d565ea0ad588445fb120e049db157afc7bf37/test/unit/lib/plugins/package/lib/packageService.test.js#L147-L168 }); it('should support `functions[].package.include`', () => { // Confirm that in function dedicated artifact "dir1/subdir3/index.js" is packaged }); (process.platfrom === 'win32' ? it : it.skip)( 'should mark go runtime handler files as executable on windows', () => { // Confirm that packaged go handler is executable // Replace // https://github.com/serverless/serverless/blob/b12d565ea0ad588445fb120e049db157afc7bf37/test/unit/lib/plugins/package/lib/packageService.test.js#L335-L376 } ); (process.platfrom === 'win32' ? it : it.skip)( 'should normalize go runtime handler path on windows', () => { // Confirm that configured lambda handler has normalized path // // Replace // https://github.com/serverless/serverless/blob/b12d565ea0ad588445fb120e049db157afc7bf37/test/unit/lib/plugins/package/lib/packageService.test.js#L378-L402 } ); it('should package layer', () => { // Confirm that layer is packaged and content is as expected // // Replace // https://github.com/serverless/serverless/blob/b12d565ea0ad588445fb120e049db157afc7bf37/test/unit/lib/plugins/package/lib/packageService.test.js#L581-L607 }); }); describe('individually', () => { before(async () => { await runServerless({ fixture: 'packaging', cliArgs: ['package'], configExt: { package: { individually: true, artifact: 'artifact.zip', exclude: ['dir1', '!dir1/subdir3/**'], include: ['dir1/subdir2/**', '!dir1/subdir2/subsubdir1'], }, functions: { fnIndividual: { handler: 'index.handler', package: { individually: true, include: 'dir1/subdir3/**', exclude: 'dir1/subdir2' }, }, }, plugins: { localPath: './custom-plugins', modules: ['index'], }, }, }); }); it('should exclude custom plugins localPath', () => { // Confirm ".serverless-plugins/index.js" is not packaged }); it('should ignore `package.artifact` if `package.individually`', () => { // Replace // https://github.com/serverless/serverless/blob/b12d565ea0ad588445fb120e049db157afc7bf37/test/unit/lib/plugins/package/lib/packageService.test.js#L237-L260 }); it('should support `package.individually`', () => { // Confirm on different artifacts on functions // // Replaces // https://github.com/serverless/serverless/blob/b12d565ea0ad588445fb120e049db157afc7bf37/test/unit/lib/plugins/package/lib/packageService.test.js#L181-L199 }); it('should support `package.exclude`', () => { // Confirm }); it('should support `package.include`', () => { // Confirm // Replaces // https://github.com/serverless/serverless/blob/b12d565ea0ad588445fb120e049db157afc7bf37/test/unit/lib/plugins/package/lib/packageService.test.js#L52-L60 }); }); describe('pre-prepared artifact', () => { before(async () => { await runServerless({ fixture: 'packaging', cliArgs: ['package'], configExt: { package: { artifact: 'artifact.zip', exclude: ['dir1', '!dir1/subdir3/**'], include: ['dir1/subdir2/**', '!dir1/subdir2/subsubdir1'], }, functions: { fnIndividual: { handler: 'index.handler', package: { individually: true, include: 'dir1/subdir3/**', exclude: 'dir1/subdir2' }, }, fnArtifact: { handler: 'index.handler', artifact: 'artifact-function.zip', }, }, }, }); }); it('should support `package.artifact`', () => { // Confirm that file pointed at `package.artifact` is configured as service level artifact // // Replace // https://github.com/serverless/serverless/blob/b12d565ea0ad588445fb120e049db157afc7bf37/test/unit/lib/plugins/package/lib/packageService.test.js#L227-L235 }); it('should ignore `package.artifact` if `functions[].package.individually', () => { // Confirm that fnIndividual was packaged independently // // Replace // https://github.com/serverless/serverless/blob/b12d565ea0ad588445fb120e049db157afc7bf37/test/unit/lib/plugins/package/lib/packageService.test.js#L262-L287 }); it('should support `functions[].package.artifact`', () => { // Confirm that file pointed at `functions.fnArtifact.package.artifact` is configured as function level artifact }); }); }); - Each test slot indicates which test it is about to replace
- It is indicated where (and how many) exactly
runServerlessruns are expected to happen, against which fixture and command they should be based
For every refactored test, the old test should be removed
Refactor can be submitted with one PR, but partial refactors that cover some of tests are also very welcome!
Activity
scadu commentedon Dec 13, 2020
If there are no volunteers, I'd pick up this one.
medikoo commentedon Dec 13, 2020
@scadu that'll be great. Thank you!
scadu commentedon Dec 19, 2020
@medikoo Hi,
A small update, since this issue is blocking #8581 and I feel stuck. My brain is a mush this week.
I started tinkering and the current, erm, progress can be seen here (a draft PR against my own fork). Nothing useful so far.
It seems like Go-related tests weren't even used due to the typo (
process.platfrominstead ofprocess.platform).Anyway, the first part (
service wide) is barely finished. I couldn't getexcludeworking according to the requirements - seems to be conflict between service and function scope where the same path is excluded and included. Most probably I miss something obvious (see; https://github.com/scadu/serverless/blob/b4f88b13e4b4e73cfde86ca77ee2ba1f917e88c0/test/unit/lib/plugins/package/lib/packageService.test.js#L489-L491).Also, I have some doubts regarding Go-related tests:
main.go, but I've tested it locally against binary (bin/main). Not sure if building the Go binary is something we'd like to do.https://github.com/scadu/serverless/blob/b4f88b13e4b4e73cfde86ca77ee2ba1f917e88c0/test/unit/lib/plugins/package/lib/packageService.test.js#L515-L521. I haven't figured it out how to validate it properly against existing fixtures.
I just barely touched the second stage (
individually), but I ended up with the following just by removing the.skip.I'd need to check the
PluginManagerfor some clues, since it also might be something trivial.tldr: not much progress so far. I'll try to dig deeper tomorrow or the day after.
medikoo commentedon Dec 21, 2020
@scadu thanks for asking
Indeed the proposed patterns were a bit confusing. I've improved that I believe with this commit: 4505dee Let me know if after taking this in it works without issues
This test should be run only windows (as it's only in this env where it's guaranteed to pass), and we should (1) unpack generated zip artifact (2) get go handler stats (simply via
fs.promises.stat) (3) confirm thatstats.modehave execution bit(at least I think that
listZipFilesutil won't give a mean to inspect file mode (?) Maybe it can be extended to provide additional meta if requested (?))Old test as I checked simply confirmed that normalized path to handler was passed to zip function. I think we can skip that test totally, as we normalize all paths as they come. I removed this test slot in master
It was due to custom plugins as setup in fixture not exporting functions. I've fixed that with 402cfce
Great thanks for all the questions, and let me know if there's anything else
scadu commentedon Dec 21, 2020
It looks like the exclude
dir1/subdir2/subsubdir3on the function level is ignored and the include definition from the service level is taken (include: ['dir1/subdir2/**', '!dir1/subdir2/subsubdir1']) - the package includesdir1/subdir2/subsubdir2anddir1/subdir2/subsubdir3. So at least now I see the rationale behind patterns (#8581). Still, the inheritance seems to be tricky here.I did something like this in https://github.com/scadu/serverless/blob/b4f88b13e4b4e73cfde86ca77ee2ba1f917e88c0/test/unit/lib/plugins/package/lib/packageService.test.js#L507. Not sure if that would be okay.
scadu commentedon Dec 21, 2020
It looks like the exclude
dir1/subdir2/subsubdir3on the function level is ignored and the include definition from the service level is taken (include: ['dir1/subdir2/**', '!dir1/subdir2/subsubdir1']) - the package includesdir1/subdir2/subsubdir2anddir1/subdir2/subsubdir3. So at least now I see the rationale behind patterns (#8581). Still, the inheritance seems to be tricky here.I did something like this in https://github.com/scadu/serverless/blob/b4f88b13e4b4e73cfde86ca77ee2ba1f917e88c0/test/unit/lib/plugins/package/lib/packageService.test.js#L507. Not sure if that would be okay.
👍
Seems to be working if AWS credentials are set.
medikoo commentedon Dec 21, 2020
Indeed, case with
excludeandincluderules it that first exclude rules are resolved and then include rules. So in this case'dir1/subdir2/**'inclusion is shadowed'dir1/subdir2/subsubdir3'_exclusion.I've updated the fixture and comments, and hopefully now everything would work as expected -> 77fbc73
This doesn't seem to validate what we're after, note that
expecttakes one argument. so you're just confirming thatfnIndividualZippedFilesincludes 'main.go`. While what we need to confirm here, is that not only it's packaged, but that it's packaged with expected mode.It needs stubbing of AWS requests, as e.g. here:
serverless/test/unit/lib/plugins/package/lib/packageService.test.js
Line 421 in 77fbc73
scadu commentedon Dec 21, 2020
@medikoo yep, after the latest changes (77fbc73) it started working.
I'm looking into file permissions now.
scadu commentedon Dec 22, 2020
I think it'd be covered in scadu@c806839.
Now I'm trying to do the following:
individuallypackage.artifactifpackage.individuallypackage.individuallypre-prepared artifactpackage.artifactpackage.artifactiffunctions[].package.individuallyfunctions[].package.artifactI suspect that
runServerlessshould be called separately for each of them and then compare with a generated configuration. Is that right @medikoo?medikoo commentedon Dec 22, 2020
@scadu good question. I've preconfigured all
runServerlessruns which I believe are needed. Still again I might have missed something. If you feel that some thinigs cannot be tested well in context of preconfigured runs, please explain, and we'll find the right solutionAll cases in
pre-prepared artifactcan be tested with one run (in config we can have following properties set:package.artifcact,functions.a.package.individually, andfunctions.b.package[individually & artifact])scadu commentedon Jan 6, 2021
So far I've opened PR that doesn't cover all tests (I haven't touched the codebase since the latest comment and I still have no clear idea how to do it), but I wouldn't like to block #8581.