Skip to content

Tests: Refactor lib/plugins/package/lib/packageService.test.js  #8594

Closed

Description

@medikoo

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:
    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 runServerless runs 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

  1. scadu commented on Dec 13, 2020

    @scadu
    Contributor

    If there are no volunteers, I'd pick up this one.

  2. medikoo commented on Dec 13, 2020

    @medikoo
    ContributorAuthor

    @scadu that'll be great. Thank you!

  3. scadu commented on Dec 19, 2020

    @scadu
    Contributor

    @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.platfrom instead of process.platform).
    Anyway, the first part (service wide) is barely finished. I couldn't get exclude working 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:

    1. Make sure that handler file is marked executable on Windows: https://github.com/scadu/serverless/blob/b4f88b13e4b4e73cfde86ca77ee2ba1f917e88c0/test/unit/lib/plugins/package/lib/packageService.test.js#L489-L491. For now I just pointed to the 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.
    2. Make sure normalized handler path is used on Windows:
      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 PluginManager for some clues, since it also might be something trivial.

      1) lib/plugins/package/lib/packageService.test.js
           TODO: individually
             "before all" hook for "should exclude custom plugins localPath":
         TypeError: Plugin is not a constructor
    

    tldr: not much progress so far. I'll try to dig deeper tomorrow or the day after.

  4. medikoo commented on Dec 21, 2020

    @medikoo
    ContributorAuthor

    @scadu thanks for asking

    I couldn't get exclude working according to the requirements - seems to be conflict between service and function scope where the same path is excluded and included.

    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

    Make sure that handler file is marked executable on Windows

    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 that stats.mode have execution bit

    (at least I think that listZipFiles util won't give a mean to inspect file mode (?) Maybe it can be extended to provide additional meta if requested (?))

    Make sure normalized handler path is used on Windows

    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

    I just barely touched the second stage (individually), but I ended up with the following just by removing the .skip

    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

  5. scadu commented on Dec 21, 2020

    @scadu
    Contributor

    @scadu thanks for asking

    I couldn't get exclude working according to the requirements - seems to be conflict between service and function scope where the same path is excluded and included.

    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

    It looks like the exclude dir1/subdir2/subsubdir3 on the function level is ignored and the include definition from the service level is taken (include: ['dir1/subdir2/**', '!dir1/subdir2/subsubdir1']) - the package includes dir1/subdir2/subsubdir2 and dir1/subdir2/subsubdir3. So at least now I see the rationale behind patterns (#8581). Still, the inheritance seems to be tricky here.

    Make sure that handler file is marked executable on Windows

    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 that stats.mode have execution bit

    (at least I think that listZipFiles util won't give a mean to inspect file mode (?) Maybe it can be extended to provide additional meta if requested (?))

    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.

    Make sure normalized handler path is used on Windows

    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

    I just barely touched the second stage (individually), but I ended up with the following just by removing the .skip

    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

  6. scadu commented on Dec 21, 2020

    @scadu
    Contributor

    @scadu thanks for asking

    I couldn't get exclude working according to the requirements - seems to be conflict between service and function scope where the same path is excluded and included.

    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

    It looks like the exclude dir1/subdir2/subsubdir3 on the function level is ignored and the include definition from the service level is taken (include: ['dir1/subdir2/**', '!dir1/subdir2/subsubdir1']) - the package includes dir1/subdir2/subsubdir2 and dir1/subdir2/subsubdir3. So at least now I see the rationale behind patterns (#8581). Still, the inheritance seems to be tricky here.

    Make sure that handler file is marked executable on Windows

    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 that stats.mode have execution bit
    (at least I think that listZipFiles util won't give a mean to inspect file mode (?) Maybe it can be extended to provide additional meta if requested (?))

    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.

    Make sure normalized handler path is used on Windows

    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

    👍

    I just barely touched the second stage (individually), but I ended up with the following just by removing the .skip

    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

    Seems to be working if AWS credentials are set.

  7. medikoo commented on Dec 21, 2020

    @medikoo
    ContributorAuthor

    It looks like the exclude dir1/subdir2/subsubdir3 on the function level is ignored and the include definition from the service level is taken

    Indeed, case with exclude and include rules 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

    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.

    This doesn't seem to validate what we're after, note that expect takes one argument. so you're just confirming that fnIndividualZippedFiles includes 'main.go`. While what we need to confirm here, is that not only it's packaged, but that it's packaged with expected mode.

    Seems to be working if AWS credentials are set.

    It needs stubbing of AWS requests, as e.g. here:

    awsRequestStubMap: mockedDescribeStacksResponse,

  8. scadu commented on Dec 21, 2020

    @scadu
    Contributor

    @medikoo yep, after the latest changes (77fbc73) it started working.

    I'm looking into file permissions now.

  9. scadu commented on Dec 22, 2020

    @scadu
    Contributor

    I think it'd be covered in scadu@c806839.
    Now I'm trying to do the following:

    • individually
      • should ignore package.artifact if package.individually
      • should support package.individually
    • pre-prepared artifact
      • should support package.artifact
      • should ignore package.artifact if functions[].package.individually
      • should support functions[].package.artifact

    I suspect that runServerless should be called separately for each of them and then compare with a generated configuration. Is that right @medikoo?

  10. medikoo commented on Dec 22, 2020

    @medikoo
    ContributorAuthor

    I suspect that runServerless should be called separately for each of them and then compare with a generated configuration. Is that right @medikoo?

    @scadu good question. I've preconfigured all runServerless runs 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 solution

    All cases in pre-prepared artifact can be tested with one run (in config we can have following properties set: package.artifcact, functions.a.package.individually, and functions.b.package[individually & artifact])

  11. scadu commented on Jan 6, 2021

    @scadu
    Contributor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions