Skip to content

chore(cicd): cdk examples and e2e tests for metrics #326

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 21, 2021
Merged

Conversation

flochaz
Copy link
Contributor

@flochaz flochaz commented Dec 16, 2021

Description of your changes

  1. Add global examples based on CDK v2 using our final packages
➜  cdk git:(examples/cdk) npm run cdk deploy

> [email protected] cdk
> cdk "deploy"

Bundling asset CdkAppStack/MyFunction/Code/Stage...
...
✅  CdkAppStack

Stack ARN:
arn:aws:cloudformation:eu-west-1:264025721901:stack/CdkAppStack/aae82b70-5821-11ec-b7f5-023677044a7b

Screenshot 2021-12-16 at 09 38 06

Screenshot 2021-12-16 at 09 41 15

Screenshot 2021-12-16 at 09 41 48

  1. Add an example of e2e test
➜  metrics git:(examples/cdk) npm run test:e2e                        

> @aws-lambda-powertools/[email protected] test:e2e
> jest --group=e2eBundling asset ExampleIntegTest/MyFunction/Code/Stage...  ...d640df993fc53f1db8f40962c2c0e5b9900873153fe428478bc48ce9/index.js  10.7kb
⚡ Done in 7ms
[0%] start: Publishing 26112141b6a636c88b4a429c757121777675f5fc2b6437ed1416d6ea26d51f58:current
[100%] success: Published 26112141b6a636c88b4a429c757121777675f5fc2b6437ed1416d6ea26d51f58:current
ExampleIntegTest: creating CloudFormation changeset...
ExampleIntegTest | 0/4 | 09:27:42 | REVIEW_IN_PROGRESS   | AWS::CloudFormation::Stack | ExampleIntegTest User Initiated
ExampleIntegTest | 0/4 | 09:27:48 | CREATE_IN_PROGRESS   | AWS::CloudFormation::Stack | ExampleIntegTest User Initiated
ExampleIntegTest | 0/4 | 09:27:52 | CREATE_IN_PROGRESS   | AWS::IAM::Role        | MyFunction/ServiceRole (MyFunctionServiceRole3C357FF2) 
ExampleIntegTest | 0/4 | 09:27:53 | CREATE_IN_PROGRESS   | AWS::IAM::Role        | MyFunction/ServiceRole (MyFunctionServiceRole3C357FF2) Resource creation Initiated
ExampleIntegTest | 1/4 | 09:28:08 | CREATE_COMPLETE      | AWS::IAM::Role        | MyFunction/ServiceRole (MyFunctionServiceRole3C357FF2) 
ExampleIntegTest | 1/4 | 09:28:10 | CREATE_IN_PROGRESS   | AWS::IAM::Policy      | MyFunction/ServiceRole/DefaultPolicy (MyFunctionServiceRoleDefaultPolicyB705ABD4) 
ExampleIntegTest | 1/4 | 09:28:11 | CREATE_IN_PROGRESS   | AWS::IAM::Policy      | MyFunction/ServiceRole/DefaultPolicy (MyFunctionServiceRoleDefaultPolicyB705ABD4) Resource creation Initiated
ExampleIntegTest | 2/4 | 09:28:25 | CREATE_COMPLETE      | AWS::IAM::Policy      | MyFunction/ServiceRole/DefaultPolicy (MyFunctionServiceRoleDefaultPolicyB705ABD4) 
ExampleIntegTest | 2/4 | 09:28:27 | CREATE_IN_PROGRESS   | AWS::Lambda::Function | MyFunction (MyFunction3BAA72D1) 
ExampleIntegTest | 2/4 | 09:28:29 | CREATE_IN_PROGRESS   | AWS::Lambda::Function | MyFunction (MyFunction3BAA72D1) Resource creation Initiated
ExampleIntegTest | 3/4 | 09:28:37 | CREATE_COMPLETE      | AWS::Lambda::Function | MyFunction (MyFunction3BAA72D1) 
ExampleIntegTest | 4/4 | 09:28:38 | CREATE_COMPLETE      | AWS::CloudFormation::Stack | ExampleIntegTest 
ExampleIntegTest |   0 | 09:28:42 | DELETE_IN_PROGRESS   | AWS::CloudFormation::Stack | ExampleIntegTest User Initiated
ExampleIntegTest |   0 | 09:28:44 | DELETE_IN_PROGRESS   | AWS::Lambda::Function | MyFunction (MyFunction3BAA72D1) 
ExampleIntegTest |   1 | 09:28:52 | DELETE_COMPLETE      | AWS::Lambda::Function | MyFunction (MyFunction3BAA72D1) 
PASS  tests/e2e/decorator.test.ts (83.021 s)

Test Suites: 1 passed, 1 of 2 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        83.5 s, estimated 90 s
Ran all test suites.

How to verify this change

Global example

cd examples/cdk
npm install
npm run build
npm run cdk deploy

This will deploy 3 lambdas and invoke them twice (to have one cold start and one warm)

Metrics e2e test

Run procedure

cd packages/metrics
npm install
npm run test:e2e

Doc (might be worth to move that in CONTRIBUTING.md ?)

Tests are under test folder and splitted into two categories: Unit tests and e2e tests.

This split happen thanks to jest-runner-groups.

Unit tests, under test/unit folder, don't need AWS Account to run and can only verify your construct's logic and it's capability to be synthesised to Cloud Formation.

Integration tests, under test/e2e folder, will test the deployability (cfn stack deploy) of your construct, potentially its functionality and its deprovisioning capability (cfn stack destroy) and therefore will need an AWS account.

Unit testing

Write

As mentioned before, tests are splitted thanks to jest-runner-groups and therefore needs to be tagged properly by adding the following comments in your unit test file:

/**
 * Tests metrics
 *
 * @group unit/<YOUR CATEGORY>/<YOUR SUB CATEGORY>
 */

Run

npm run test

You can run selective tests by restricting the group to the one you want. For instance npx jest --group=unit/metrics/all.

e2e testing

Write

As mentioned before, tests are splitted thanks to jest-runner-groups and therefore needs to be tagged properly by adding the following comments in your unit test file:

/**
 * Tests data lake catalog
 *
 * @group e2e/<YOUR CATEGORY>/<YOUR SUB CATEGORY>
 */

and leverage aws-cdk package to programatically deploy and destroy stacks. See metrics/tests/e2e/decorator.test.ts as an example.

Run

To run unit tests you can either use projen task

  • npm run test:e2e which will only run jest integ tests
  • or jest directly npx jest --group=e2e

You can run selective tests by restricting the group to the one you want. For instance npx jest --group=e2e/other/example.

Two important env variable can be used:

  • AWS_PROFILE to use the right credentials
  • DISABLE_TEARDOWN if you don't want your stack to be destroyed at the end of the test (useful in dev mode when iterating over your code).

Example: DISABLE_TEARDOWN=true AWS_PROFILE=ara npx jest --group=integ/other/example

Related issues, RFCs

#136

PR status

Is this ready for review?: YES
Is it a breaking change?: NO

Checklist

  • My changes meet the tenets criteria
  • I have performed a self-review of my own code
  • I have commented my code where necessary, particularly in areas that should be flagged with a TODO, or hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • The code coverage hasn't decreased
  • I have added tests that prove my change is effective and works
  • New and existing unit tests pass locally and in Github Actions
  • Any dependent changes have been merged and published in downstream module
  • The PR title follows the conventional commit semantics

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@dreamorosi
Copy link
Contributor

Pushed a change in the example-stack.MyFunctionWithDecorator.ts & example-stack.MyFunction.ts files so that now manual & decorator should be able to do the same thing:
image

image

Opened also a separate issue to work on exposing better APIs for manual instrumentation: #334

saragerion
saragerion previously approved these changes Dec 17, 2021
Copy link
Contributor

@saragerion saragerion left a comment

Choose a reason for hiding this comment

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

Happy to merge this

Copy link
Contributor

@ijemmy ijemmy left a comment

Choose a reason for hiding this comment

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

Overall, it looks good to me. My comments are nitpicking. We can discuss and change them in a separate PR.

Comment on lines +8 to +20
/* If you don't specify 'env', this stack will be environment-agnostic.
* Account/Region-dependent features and context lookups will not work,
* but a single synthesized template can be deployed anywhere. */

/* Uncomment the next line to specialize this stack for the AWS Account
* and Region that are implied by the current CLI configuration. */
// env: { account: process.env.CDK_DEFAULT_ACCOUNT, region: process.env.CDK_DEFAULT_REGION },

/* Uncomment the next line if you know exactly what Account and Region you
* want to deploy the stack to. */
// env: { account: '123456789012', region: 'us-east-1' },

/* For more information, see https://docs.aws.amazon.com/cdk/latest/guide/environments.html */
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we remove this?


// Invoke all functions twice
for (let i = 0; i < 2; i++) {
new custom_resources.AwsCustomResource(this, `Invoke-std-func-${i}`, {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we wrap these 3 resource creations in to a function? They are almost the same except the passed function.

defaultDimensions: { environment: 'example', type: 'withDecorator' },
})
public handler(_event: unknown, _context: Context, _callback: Callback<unknown>): void | Promise<unknown> {
// ### Experiment logger
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is there a word "Experiment"?

@@ -0,0 +1,17 @@
// import * as cdk from 'aws-cdk-lib';
Copy link
Contributor

Choose a reason for hiding this comment

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

We can also remove these unused comments.

// Despite lambda has been called twice, coldstart metric sum should only be 1
const singleDataPoint = coldStartMetricStat.Datapoints ? coldStartMetricStat.Datapoints[0] : {};
expect(singleDataPoint.Sum).toBe(1);
}, 9000000);
Copy link
Contributor

Choose a reason for hiding this comment

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

That's 2.5 hr. Do you intend to be 15 mins? (one less 0)

stack: stackArtifact,
});
}
}, 9000000);
Copy link
Contributor

Choose a reason for hiding this comment

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

That's 2.5 hr. Do you intend to be 15 mins? (one less 0)

@saragerion saragerion merged commit 031be05 into main Dec 21, 2021
@saragerion saragerion deleted the examples/cdk branch December 21, 2021 19:35
@heitorlessa heitorlessa added the internal PRs that introduce changes in governance, tech debt and chores (linting setup, baseline, etc.) label Jan 5, 2022
@michaelbrewer
Copy link
Contributor

@saragerion @ijemmy @flochaz - looks like this lock file was generated by node@16, which will break node@14 users when the want to do npm test

@flochaz
Copy link
Contributor Author

flochaz commented Jan 24, 2022

@saragerion @ijemmy @flochaz - looks like this lock file was generated by node@16, which will break node@14 users when the want to do npm test

Thx @michaelbrewer. I am opening an issue and will fix asap.

@dreamorosi
Copy link
Contributor

@saragerion @ijemmy @flochaz - looks like this lock file was generated by node@16, which will break node@14 users when the want to do npm test

Thx @michaelbrewer. I am opening an issue and will fix asap.

For those coming here in the future, the issue was that we recently started recommending [email protected] for development on the project and those still using a lower version right after the switch got their builds broken for a short while.

Using [email protected] and [email protected] should work as expected, if anyone encounters issues with this setup they should please open an issue and provide as many details as possible about the setup.

@michaelbrewer
Copy link
Contributor

Sorry about this. I guess things where in flux (so now with the clear contributor guide and the gitpod/CodeSpaces support). It should be clear what the local setup should be.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
internal PRs that introduce changes in governance, tech debt and chores (linting setup, baseline, etc.)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants