Skip to content

chore(ci): move e2e utils into testing #1661

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

Conversation

dreamorosi
Copy link
Contributor

@dreamorosi dreamorosi commented Aug 29, 2023

Description of your changes

This PR introduces cross-cutting changes to the integration tests for all utilities in the project. The main goals of this PR are two:

  • Complete the move of code used to create, deploy, and run integration test CDK stack to the dedicated internal testing package
  • Reduce duplication / verbosity of the code used to declare the test resources

In #1633 we have created an internal testing package that holds resources dedicated to deploy and run integration tests using CDK. Prior to this PR the package contained only the basic TestStack construct and a handful helper functions. Most of the actual logic used to add resources to a CDK stack was still scattered around the different packages of the workspace.

This PR almost completely removes the usage of custom functions/abstractions to create CDK resources in favor of following patterns suggested by CDK like composition and inheritance.

For instance, before this PR multiple utilities were defining their version of an helper function that looked something like this:

type CreateLambdaFunctionProps = {
  stack: Stack,
  id: string,
  runtime: string,
  handler: string,
  entry: string,
  ...
};

const createLambdaFunction = (props: CreateLambdaFunctionProps): NodejsFunction => {
  const fn = new NodejsFunction(props.stack, props.id, {
    runtime: props.runtime as Runtime,
    ....
};

This led to several different but similar implementations that created slightly different variations of the same type of resource.

With this PR the testing package offers a base CDK resource that extends the CDK L3 construct directly and that applies the same set of defaults across all tests:

class TestNodejsFunction extends NodejsFunction {
  public constructor(
    stack: TestStack,
    props: TestNodejsFunctionProps,
    extraProps: ExtraTestProps
  ) {
    super(stack.stack, `fn-${randomUUID().substring(0, 5)}`, {
      timeout: Duration.seconds(30),
      memorySize: 256,
      tracing: Tracing.ACTIVE,
      ...props,
      functionName: concatenateResourceName({
        testName: stack.testName,
        resourceName: extraProps.nameSuffix,
      }),
      runtime: TEST_RUNTIMES[getRuntimeKey()],
      logRetention: RetentionDays.ONE_DAY,
    });

    new CfnOutput(this, extraProps.nameSuffix, {
      value: this.functionName,
    });
  }
}

Different utilities (Tracer, Metrics, etc.) can then use this base L3+ construct and extend it further to apply specific configurations that are unique to that test/utility:

class TracerTestNodejsFunction extends TestNodejsFunction {
  public constructor(
    scope: TestStack,
    props: TestNodejsFunctionProps,
    extraProps: ExtraTestProps
  ) {
    super(
      scope,
      {
        ...props,
        environment: {
          ...commonEnvironmentVars,
          EXPECTED_SERVICE_NAME: extraProps.nameSuffix,
          EXPECTED_CUSTOM_METADATA_VALUE: JSON.stringify(
            commonEnvironmentVars.EXPECTED_CUSTOM_METADATA_VALUE
          ),
          EXPECTED_CUSTOM_RESPONSE_VALUE: JSON.stringify(
            commonEnvironmentVars.EXPECTED_CUSTOM_RESPONSE_VALUE
          ),
          ...props.environment,
        },
      },
      extraProps
    );
  }
}

In this way, in addition to having less code to maintain, we have the certainty that all the resources used during integration tests start from the same set of defaults.

The same pattern has been applied to DynamoDB tables and other resources that are used across different tests / utilities.

In addition to the benefits described above this, together with a handful new helper functions that allow to create unique resource names, also leads to more terse test setups.

The integration tests for this projects are run for different runtimes and utilities concurrently. This means that all resources from stacks to single AWS resources must have unique but identifiable names.

Another change brought by this PR is enforcing this constraint and making sure that all resources follow the same scheme: <Utility Name>-<runtime>-<uuid{0, 5}>-<human readable suffix>, i.e. Logger-E2E-node18-1dad3-AllFeatures-Decorator.

This is done with the introduction and rollout of a couple of helper functions that take in a prefix (the test name) and a suffix (the resource name) and generate the string by reading the test context.

Related issues, RFCs

Issue number: closes #1660

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
  • I have added tests that prove my change is effective and works
  • The PR title follows the conventional commit semantics

Breaking change checklist

Is it a breaking change?: NO

  • 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.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

@dreamorosi dreamorosi self-assigned this Aug 29, 2023
@dreamorosi dreamorosi requested a review from a team August 29, 2023 13:00
@dreamorosi dreamorosi linked an issue Aug 29, 2023 that may be closed by this pull request
2 tasks
@boring-cyborg boring-cyborg bot added layers Items related to the Lambda Layers pipeline dependencies Changes that touch dependencies, e.g. Dependabot, etc. tests PRs that add or change tests labels Aug 29, 2023
@pull-request-size pull-request-size bot added the size/XXL PRs with 1K+ LOC, largely documentation related label Aug 29, 2023
@dreamorosi dreamorosi force-pushed the 1660-maintenance-move-e2e-utilities-into-testing-package branch from abf2d43 to e511b71 Compare September 1, 2023 16:42
@sonarqubecloud
Copy link

sonarqubecloud bot commented Sep 1, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 2 Code Smells

No Coverage information No Coverage information
14.0% 14.0% Duplication

@dreamorosi
Copy link
Contributor Author

@dreamorosi dreamorosi merged commit e442c3d into main Sep 1, 2023
@dreamorosi dreamorosi deleted the 1660-maintenance-move-e2e-utilities-into-testing-package branch September 1, 2023 18:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dependencies Changes that touch dependencies, e.g. Dependabot, etc. layers Items related to the Lambda Layers pipeline size/XXL PRs with 1K+ LOC, largely documentation related tests PRs that add or change tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Maintenance: move e2e utilities into testing package Maintenance: review e2e test utilities
1 participant