Skip to content

chore(tests): build and deploy Lambda Layer stack once #1466

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

heitorlessa
Copy link
Contributor

Issue number: #1464

Summary

This PR introduces LambdaLayerStack that is deployed per test session. It coordinates all parallel workers across CPU to wait for completion before they begin deploying their respective stacks and test run.

This diagram may better explain. More details on a few differences at the comment section.

graph TD
    A[make e2e test] -->Spawn{"Split and group tests <br>by feature and CPU"}

    Spawn -->|Worker0| Worker0_Start["Load tests"]
    Spawn -->|Worker1| Worker1_Start["Load tests"]
    Spawn -->|WorkerN| WorkerN_Start["Load tests"]

    Worker0_Start -->|Wait| LambdaLayerStack["Lambda Layer Stack Deployment"]
    Worker1_Start -->|Wait| LambdaLayerStack["Lambda Layer Stack Deployment"]
    WorkerN_Start -->|Wait| LambdaLayerStack["Lambda Layer Stack Deployment"]

    LambdaLayerStack -->|Worker0| Worker0_Deploy["Launch feature stack"]
    LambdaLayerStack -->|Worker1| Worker1_Deploy["Launch feature stack"]
    LambdaLayerStack -->|WorkerN| WorkerN_Deploy["Launch feature stack"]

    Worker0_Deploy -->|Worker0| Worker0_Tests["Run tests"]
    Worker1_Deploy -->|Worker1| Worker1_Tests["Run tests"]
    WorkerN_Deploy -->|WorkerN| WorkerN_Tests["Run tests"]

    Worker0_Tests --> ResultCollection
    Worker1_Tests --> ResultCollection
    WorkerN_Tests --> ResultCollection

    ResultCollection{"Wait for workers<br/>Collect test results"}
    ResultCollection --> TestEnd["Report results"]
    ResultCollection --> DeployEnd["Delete Stacks"]
Loading

Changes

Please provide a summary of what's being changed

User experience

Please share what the user experience looks like before and after this change

Checklist

If your change doesn't seem to apply, please leave them unchecked.

Is this a breaking change?

RFC issue number:

Checklist:

  • Migration process documented
  • Implement warnings (if it can live side by side)

Acknowledgment

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.

@heitorlessa heitorlessa requested a review from a team as a code owner August 22, 2022 14:22
@heitorlessa heitorlessa requested review from am29d and removed request for a team August 22, 2022 14:22
@boring-cyborg boring-cyborg bot added internal Maintenance changes tests labels Aug 22, 2022
@pull-request-size pull-request-size bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Aug 22, 2022
@heitorlessa heitorlessa requested review from mploski and rubenfonseca and removed request for am29d August 22, 2022 14:23
@codecov-commenter
Copy link

codecov-commenter commented Aug 22, 2022

Codecov Report

Merging #1466 (127b093) into develop (6930b42) will increase coverage by 0.00%.
The diff coverage is n/a.

@@           Coverage Diff            @@
##           develop    #1466   +/-   ##
========================================
  Coverage    99.89%   99.89%           
========================================
  Files          123      123           
  Lines         5497     5499    +2     
  Branches       629      629           
========================================
+ Hits          5491     5493    +2     
  Misses           2        2           
  Partials         4        4           
Impacted Files Coverage Δ
aws_lambda_powertools/utilities/batch/sqs.py 96.38% <0.00%> (+0.08%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@heitorlessa
Copy link
Contributor Author

I've increased test distribution parallelization algorithm from module (loadscope) to per test file (loadist).

  • Sequential test: 279.52s
  • Parallel test + Layer per stack: 254.53s
  • Parallel + Single Layer: 243.58s

Numbers are slightly higher now as I've included all extra dependencies to futureproof when we test Parser.

@heitorlessa
Copy link
Contributor Author

Coordinating across shared queue memory required some changes in how we define infrastructure and their dependencies to run.

infrastructure fixtures per feature (e.g., Tracer, Logger, etc.) must accept layer_arn argument. This dependency injection also ensures all workers scattered across CPUs will wait for a session fixture. We then moved away from deploy_once as this is controlled up a level, making it more explicit (slightly more verbose too :/)

BEFORE

from pathlib import Path

import pytest

from tests.e2e.logger.infrastructure import LoggerStack


@pytest.fixture(autouse=True, scope="module")
def infrastructure(request: pytest.FixtureRequest, tmp_path_factory: pytest.TempPathFactory, worker_id: str):
    yield from deploy_once(stack=LoggerStack, request=request, tmp_path_factory=tmp_path_factory, worker_id=worker_id)

AFTER

from pathlib import Path

import pytest

from tests.e2e.logger.infrastructure import LoggerStack


@pytest.fixture(autouse=True, scope="module")
def infrastructure(request: pytest.FixtureRequest, lambda_layer_arn: str):
    stack = LoggerStack(handlers_dir=Path(f"{request.path.parent}/handlers"), layer_arn=lambda_layer_arn)
    try:
        yield stack.deploy()
    finally:
        stack.delete()

@heitorlessa
Copy link
Contributor Author

Stacks changed signature to accept a new optional argument (layer_arn). To reduce some level of verbosity I also added a class attribute (FEATURE_NAME) to make it easier for newcomers to copy/paste as only create_resources and the feature name will change.

BEFORE

from pathlib import Path

from tests.e2e.utils.infrastructure import BaseInfrastructureV2


class LoggerStack(BaseInfrastructureV2):
    def __init__(self, handlers_dir: Path, feature_name: str = "logger") -> None:
        super().__init__(feature_name, handlers_dir)

    def create_resources(self):
        self.create_lambda_functions()

AFTER

from pathlib import Path

from tests.e2e.utils.infrastructure import BaseInfrastructureV2


class LoggerStack(BaseInfrastructureV2):
    FEATURE_NAME = "logger"

    def __init__(self, handlers_dir: Path, feature_name: str = FEATURE_NAME, layer_arn: str = "") -> None:
        super().__init__(feature_name, handlers_dir, layer_arn)

    def create_resources(self):
        self.create_lambda_functions()

Copy link
Contributor

@rubenfonseca rubenfonseca left a comment

Choose a reason for hiding this comment

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

Amazing work Heitor, no comments from me. I was expecting a lot of logic to handle the "map-reduce", but it seems the pytest plugin already handles this for you. So no comments here :)

@heitorlessa
Copy link
Contributor Author

I'm gonna remove the old BaseInfrastructure and rename V2 so we solidify this thing now that all stacks are migrated.

@pull-request-size pull-request-size bot removed the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Aug 22, 2022
@boring-cyborg boring-cyborg bot added the dependencies Pull requests that update a dependency file label Aug 22, 2022
@pull-request-size pull-request-size bot added the size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. label Aug 22, 2022
@heitorlessa
Copy link
Contributor Author

thanks for the approval @rubenfonseca !! Merging now, and will send one last PR to enable E2E at CI level and document it in the contributing guide.

@heitorlessa heitorlessa merged commit 122f989 into aws-powertools:develop Aug 22, 2022
rubenfonseca pushed a commit to rubenfonseca/aws-lambda-powertools-python that referenced this pull request Aug 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dependencies Pull requests that update a dependency file internal Maintenance changes size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Refactor E2E: Build Lambda Layer once and expose to children stacks
3 participants