Skip to content

fix(AWS Deploy): Ensure that we only read/hash local artifact files exactly once when checking for changes. - #8351

Merged
medikoo merged 14 commits into
serverless:masterfrom
FormidableLabs:bug-zip-hash-check
Oct 9, 2020
Merged

medikoo merged 14 commits into
serverless:masterfrom
FormidableLabs:bug-zip-hash-check

Conversation

@ryan-roemer

Copy link
Copy Markdown
Contributor

As plugins/out-of-band processes can place artifacts in .serverless/NAME.zip, ensure that we only read unique file paths once when generating hashes in checkForChanges.js to decide whether or not to deploy.

Closes: #8350

@codecov-io

codecov-io commented Oct 6, 2020 •

Copy link
Copy Markdown

Codecov Report

Merging #8351 into master will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #8351   +/-   ##
=======================================
  Coverage   88.01%   88.02%           
=======================================
  Files         249      249           
  Lines        9307     9278   -29     
=======================================
- Hits         8192     8167   -25     
+ Misses       1115     1111    -4     
Impacted Files Coverage Δ
lib/plugins/aws/deploy/lib/checkForChanges.js 98.27% <100.00%> (+0.01%) ⬆️
...ws/package/compile/events/cloudWatchEvent/index.js 97.67% <0.00%> (-2.33%) ⬇️
...ns/aws/package/compile/events/eventBridge/index.js 98.30% <0.00%> (-1.70%) ⬇️
.../package/compile/events/apiGateway/lib/validate.js 96.23% <0.00%> (-0.88%) ⬇️
lib/plugins/aws/deployFunction/index.js 89.56% <0.00%> (-0.71%) ⬇️
...ins/aws/customResources/resources/s3/lib/bucket.js 0.00% <0.00%> (ø)
...ins/aws/package/compile/events/apiGateway/index.js 100.00% <0.00%> (ø)
.../aws/package/compile/events/apiGateway/lib/cors.js 100.00% <0.00%> (ø)
...ws/customResources/resources/s3/lib/permissions.js 0.00% <0.00%> (ø)
...Resources/resources/eventBridge/lib/eventBridge.js 0.00% <0.00%> (ø)
... and 8 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d2fb696...92b3ac5. Read the comment docs.

@medikoo medikoo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ryan-roemer thanks for that! Fix looks good to me. I have posted just one suggestion towards testing, as we move away from the way tests were written in older times.

@medikoo

medikoo commented Oct 8, 2020

Copy link
Copy Markdown
Contributor

@ryan-roemer from what I inspect the test behavior is right. Note that when you create a service as in fixture, and run sls package you'd receive same error (and runServerless is to reflect real world experience)

Maybe after all Framework doesn't support artifacts prepared in .serverless path (?) Of course in such case, other issue is that error reporting is not great

@ryan-roemer

Copy link
Copy Markdown
Contributor Author

@medikoo -- So the full lifecycle is that that package.artifact gets sets within the sls lifecycle and placed in .serverless/NAME.zip. I know that .serverless/ gets wiped at the start of the run. How do I approximate that in runServerless fixtures?

I'm going to try just adding serverless-jetpack which does explicitly this and see if runServerless works with actual plugins...

@medikoo

medikoo commented Oct 8, 2020

Copy link
Copy Markdown
Contributor

@ryan-roemer runServerless reflects real world framework usage.

And here it appears that placing artifacts in a .serverless simply doesn't work. Question is, should it work, or maybe we should not allow it, just fix error reporting.

I'd remove here serverless-jetpack from a picture here, as far as artifacts handling goes i think this plugin should not influence any different behavior, or do I miss something?

@ryan-roemer

Copy link
Copy Markdown
Contributor Author

serverless-jetpack and other things can and do place artifacts in .serverless/**.zip mid-lifecycle and we've done this successfully for a long time (it's also how we guarantee to be a faster, drop-in replacement for serverless packaging).

It's also what uncovered the existing bug in servlerless code about globbing .serverless.

Ultimately, I don't care what test I need to write, or if any test at all -- can you advise on how you'd test this one line change in PR?

@ryan-roemer

ryan-roemer commented Oct 8, 2020 •

Copy link
Copy Markdown
Contributor Author

Update -- I now have a simple, custom plugin like other plugins in fixtures to write to package.artifact at the appropriate lifecycle phase. I'm now stuck on the fact the runServerless AWS stub assumes scalar values as far as I can tell looking at the code and I need to have S3.headObject return different values:

        S3: {
          // **Note**: We need`headObject` here to return different HEAD values for
          // - compiled-cloudformation-template.json
          // - my-own.zip
          headObject: {
            Metadata: { filesha256: 'NEEDS_TO_BE_DIFFERENT_FOR_SUBSEQUENT_CALLS' }
          },
          listObjectsV2: {
            Contents: [
              {
                Key:
                  'serverless/test-package-artifact/dev/1589988704359-2020-05-20T15:31:44.359Z/compiled-cloudformation-template.json',
                LastModified: new Date(),
                ETag: '"5102a4cf710cae6497dba9e61b85d0a4"',
                Size: 356,
                StorageClass: 'STANDARD',
              },
              {
                Key:
                  'serverless/test-package-artifact/dev/1589988704359-2020-05-20T15:31:44.359Z/my-own.zip',
                LastModified: new Date(),
                ETag: '"5102a4cf710cae6497dba9e61b85d0a4"',
                Size: 356,
                StorageClass: 'STANDARD',
              },
            ],
          },
        },

Any ideas?

I'm trying to come up with something, but I'm in a very awkward place now as the awsRequestStubMap controls all of AwsProvider.request so I can't just manually stub when called with 'S3', 'headObject' and leave everything else intact. I don't really see a solution outside of now changing the runServerless code too 😢 . Offhand, a workable solution might be something like detecting a function / upstream stub and doing something like:

  return sinon.stub(provider, 'request').callsFake(
    (service, method) =>
      new Promise((resolve, reject) => {
        if (!config[service] || !config[service][method]) {
          reject(new Error(`Missing AWS request stub configuration for ${service}.${method}`));
          return;
        }

        // REPLACE
        // resolve(config[service][method]);
        // WITH...
        const meth = config[service][method];
        resolve(typeof meth === 'function' ? meth() : meth);
      })
  );

which would then allow me to do:

      return runServerless({
        // ...
        S3: {
          headObject: sandbox.stub()
            .onCall(0).returns({
              Metadata: { filesha256: 'FIRST_VALUE' }
            })
            .onCall(1).returns({
              Metadata: { filesha256: 'SECOND_VALUE' }
            }),
          // ...

and give me different values. Unfortunately, that would mean that I'm now depending on yet another upstream PR to close a one line bug here 😢 But, my original bigger fix for not requiring AWS authentication for serverless package and moving functionality into checkForChanges.js would probably leverage such a change. And, I'm guessing long term other developers in the serverless code base will eventually hit the need for non-scalar values for repeated calls to AWS request stubs...

(My WIP new test + fixtures is pushed up to: master...FormidableLabs:bug-zip-hash-check-new-tests)

@ryan-roemer

Copy link
Copy Markdown
Contributor Author

To make the above change @serverless/test easier, I went ahead and opened that PR here: https://github.com/serverless/test/pull/54

@ryan-roemer

Copy link
Copy Markdown
Contributor Author

OK, assuming I can get https://github.com/serverless/test/pull/54 merged and released, then I'll update @serverless/test in this PR and merge in this working branch-of-branch that now contains a fully working regression test (fails appropriately without the Array.From(new Set())) change): master...FormidableLabs:bug-zip-hash-check-new-tests-and-new-sls-test

@medikoo

medikoo commented Oct 9, 2020

Copy link
Copy Markdown
Contributor

@ryan-roemer I didn't clearly realize that we deal here with a non natural package.artifact scenario, which can only be reproduced via plugin updating this value in a middle of processing the framework command.

Anyway it's impressive you've prepared a valid test case for this scenario. I've just reviewed serverless/test#54

@ryan-roemer

Copy link
Copy Markdown
Contributor Author

@medikoo -- I've merged in my branch of branch and assuming everything passes CI I think we're all ready to go! I didn't squash my commits, but please do let me know if you'd like me to rebase and force push. Thanks for all your guidance along the way!

@medikoo medikoo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That test case is really special. Thank you!

@medikoo
medikoo merged commit 661caad into serverless:master Oct 9, 2020
@ryan-roemer
ryan-roemer deleted the bug-zip-hash-check branch October 9, 2020 18:54
@codecov-commenter

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.02%. Comparing base (d2fb696) to head (92b3ac5).
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #8351   +/-   ##
=======================================
  Coverage   88.01%   88.02%           
=======================================
  Files         249      249           
  Lines        9307     9278   -29     
=======================================
- Hits         8192     8167   -25     
+ Misses       1115     1111    -4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Deployment checks erroneously fail if local artifacts are placed in ".serverless/NAME.zip"

4 participants