Skip to content

test(tracer): make e2e tests to follow the same convention in Logger and Metrics #788

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 12 commits into from
Apr 29, 2022

Conversation

ijemmy
Copy link
Contributor

@ijemmy ijemmy commented Apr 20, 2022

Description of your changes

  1. Refactor tracer traces to be the same as other utilities.
    • Test infra are split from one stack to 4 different stack (for each test file)
    • Take annotation and metadata test out of the existing test cases
    • Create helper function to abstract some details to improve readability
  2. Make stack names unique (Maintenance: make e2e test stack name unique #497 )
  3. Support running in multiple runtime

How to verify this change

  1. Run e2e tests in workflow
  2. Run this command locally
 AWS_PROFILE=YOUR_PROFILE AWS_REGION=eu-west-1 npx jest --group=e2e/tracer

Results (partial)


[test:e2e:nodejs12x]     console.log
[test:e2e:nodejs12x]       Manual query: aws xray get-trace-summaries --start-time 1650468060 --end-time 1650468169 --filter-expression 'resource.arn = "arn:aws:lambda:eu-west-1:561912387782:function:Tracer-E2E-nodejs12x-AllFeatures-Decorator-TracerDisabled-f19a6b"'
[test:e2e:nodejs12x]
[test:e2e:nodejs12x]       at tests/helpers/tracesUtils.ts:89:13
[test:e2e:nodejs12x]
[test:e2e:nodejs12x]
[test:e2e:nodejs12x] Test Suites: 4 passed, 4 of 9 total
[test:e2e:nodejs12x] Tests:       12 passed, 12 total
[test:e2e:nodejs12x] Snapshots:   0 total
[test:e2e:nodejs12x] Time:        197.391 s
[test:e2e:nodejs12x] Ran all test suites.
[test:e2e:nodejs12x] npm run test:e2e:nodejs12x exited with code 0

Related issues, RFCs

#497
#529

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
  • I have made corresponding changes to the examples
  • 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

Breaking change checklist

  • I have documented the migration process
  • I have added, implemented necessary warnings (if it can live side by side)

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

Comment on lines +220 to +251
it('should have correct annotations and metadata', async () => {
const tracesWhenAllFlagsEnabled = await getTraces(xray, startTime, await getFunctionArn(functionNameWithAllFlagsEnabled), invocations, 5);

for (let i = 0; i < invocations; i++) {
const trace = tracesWhenAllFlagsEnabled[i];
const invocationSubsegment = getInvocationSubsegment(trace);
const handlerSubsegment = getFirstSubsegment(invocationSubsegment);
const { annotations, metadata } = handlerSubsegment;

const isColdStart = (i === 0);
assertAnnotation({
annotations,
isColdStart,
expectedServiceName: serviceNameWithAllFlagsEnabled,
expectedCustomAnnotationKey,
expectedCustomAnnotationValue,
});

if (!metadata) {
fail('metadata is missing');
}
expect(metadata[serviceNameWithAllFlagsEnabled][expectedCustomMetadataKey])
.toEqual(expectedCustomMetadataValue);

const shouldThrowAnError = (i === (invocations - 1));
if (!shouldThrowAnError) {
// Assert that the metadata object contains the response
expect(metadata[serviceNameWithAllFlagsEnabled]['index.handler response'])
.toEqual(expectedCustomResponseValue);
}
}
}, TEST_CASE_TIMEOUT);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I break this part out of the existing test case to make it not too long.

* 3. httpbin.org (Remote call)
* 4. '### myMethod' (method decorator)
*/
const handlerSubsegment = getFirstSubsegment(invocationSubsegment);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Instead of nested if. I use a helper function to ensure that the item isn't null. The check to please TypeScript compilers are moved to the helper method.

Copy link
Contributor

Choose a reason for hiding this comment

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

Love this, thanks for simplyfing that indentation hell that I got us in :D

Comment on lines +295 to +302
const shouldThrowAnError = (i === (invocations - 1));
if (shouldThrowAnError) {
// Assert that the subsegment has the expected fault
expect(invocationSubsegment.error).toBe(true);
expect(handlerSubsegment.error).toBe(true);
// Assert that no error was captured on the subsegment
expect(handlerSubsegment.hasOwnProperty('cause')).toBe(false);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the only unique part of this test case.

Comment on lines +1 to +6
/**
* Test tracer in middy setup
*
* @group e2e/tracer/middy
*/

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is almost the same as decorator case. The only difference is that we do not captureMethod() so we have 1 less custom trace.

@ijemmy ijemmy marked this pull request as ready for review April 20, 2022 15:29
@ijemmy
Copy link
Contributor Author

ijemmy commented Apr 21, 2022

@ijemmy ijemmy changed the title test(tracer): refactor e2e tests to follow the same convention in Logger and Metrics test(tracer): make e2e tests to follow the same convention in Logger and Metrics Apr 21, 2022
@dreamorosi dreamorosi added internal PRs that introduce changes in governance, tech debt and chores (linting setup, baseline, etc.) tracer This item relates to the Tracer Utility labels Apr 21, 2022
@dreamorosi dreamorosi added this to the production-ready-release milestone Apr 21, 2022
@dreamorosi dreamorosi linked an issue Apr 21, 2022 that may be closed by this pull request
import {
generateUniqueName,
isValidRuntimeKey,
} from '@aws-lambda-powertools/commons/tests/utils/e2eUtils';
Copy link
Contributor

Choose a reason for hiding this comment

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

extra-nit: can we swap this import and the previous one so we have all the relative ones after the one from node_modules?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a mistake. I intend to import relatively, not from the package. (or we need to rebuild common for every change)

I'll change this (and other files) to relative import.

dreamorosi
dreamorosi previously approved these changes Apr 21, 2022
Copy link
Contributor

@dreamorosi dreamorosi left a comment

Choose a reason for hiding this comment

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

Left one extra-minor comment about module import order but approving already since it's purely stylistic.

Really like the new test setup, a breadth of fresh air :)

Copy link
Contributor

@dreamorosi dreamorosi left a comment

Choose a reason for hiding this comment

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

LGTM

@dreamorosi dreamorosi merged commit a47c94c into main Apr 29, 2022
@dreamorosi dreamorosi deleted the ijemmy/refactor-e2e-tests-tracer branch April 29, 2022 12:13
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.) tracer This item relates to the Tracer Utility
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Maintenance: make e2e test stack name unique
3 participants