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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ unit-test:
poetry run pytest tests/unit

e2e-test:
poetry run pytest -rP -n 3 --dist loadscope --durations=0 --durations-min=1 tests/e2e
poetry run pytest -rP -n auto --dist loadfile -o log_cli=true tests/e2e

coverage-html:
poetry run pytest -m "not perf" --ignore tests/e2e --cov=aws_lambda_powertools --cov-report=html
Expand Down
78 changes: 39 additions & 39 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,20 @@ flake8-bugbear = "^22.7.1"
mkdocs-git-revision-date-plugin = "^0.3.2"
mike = "^0.6.0"
mypy = "^0.971"
mypy-boto3-secretsmanager = "^1.24.11"
mypy-boto3-ssm = "^1.24.0"
mypy-boto3-appconfig = "^1.24.29"
mypy-boto3-dynamodb = "^1.24.27"
retry = "^0.9.2"
pytest-xdist = "^2.5.0"
aws-cdk-lib = "^2.23.0"
pytest-benchmark = "^3.4.1"
mypy-boto3-cloudwatch = "^1.24.35"
mypy-boto3-lambda = "^1.24.0"
mypy-boto3-xray = "^1.24.0"
mypy-boto3-s3 = { version = "^1.24.0", python = ">=3.7" }
mypy-boto3-appconfig = { version = "^1.24.29", python = ">=3.7" }
mypy-boto3-cloudformation = { version = "^1.24.0", python = ">=3.7" }
mypy-boto3-cloudwatch = { version = "^1.24.35", python = ">=3.7" }
mypy-boto3-dynamodb = { version = "^1.24.27", python = ">=3.7" }
mypy-boto3-lambda = { version = "^1.24.0", python = ">=3.7" }
mypy-boto3-logs = { version = "^1.24.0", python = ">=3.7" }
mypy-boto3-secretsmanager = { version = "^1.24.11", python = ">=3.7" }
mypy-boto3-ssm = { version = "^1.24.0", python = ">=3.7" }
mypy-boto3-s3 = { version = "^1.24.0", python = ">=3.7" }
mypy-boto3-xray = { version = "^1.24.0", python = ">=3.7" }
types-requests = "^2.28.8"
typing-extensions = { version = "^4.3.0", python = ">=3.7" }
python-snappy = "^0.6.1"
Expand Down
97 changes: 33 additions & 64 deletions tests/e2e/conftest.py
Original file line number Diff line number Diff line change
@@ -1,66 +1,35 @@
import datetime
import sys
import uuid
from dataclasses import dataclass

import boto3

from tests.e2e.utils import data_fetcher, infrastructure

# We only need typing_extensions for python versions <3.8
if sys.version_info >= (3, 8):
from typing import TypedDict
else:
from typing_extensions import TypedDict

from typing import Dict, Generator, Optional

import pytest


class LambdaConfig(TypedDict):
parameters: dict
environment_variables: Dict[str, str]


@dataclass
class InfrastructureOutput:
arns: Dict[str, str]
execution_time: datetime.datetime

def get_lambda_arns(self) -> Dict[str, str]:
return self.arns

def get_lambda_function_arn(self, cf_output_name: str) -> Optional[str]:
return self.arns.get(cf_output_name)

def get_lambda_function_name(self, cf_output_name: str) -> Optional[str]:
lambda_arn = self.get_lambda_function_arn(cf_output_name=cf_output_name)
return lambda_arn.split(":")[-1] if lambda_arn else None

def get_lambda_execution_time(self) -> datetime.datetime:
return self.execution_time

def get_lambda_execution_time_timestamp(self) -> int:
return int(self.execution_time.timestamp() * 1000)


@pytest.fixture(scope="module")
def create_infrastructure(config, request) -> Generator[Dict[str, str], None, None]:
stack_name = f"test-lambda-{uuid.uuid4()}"
test_dir = request.fspath.dirname
handlers_dir = f"{test_dir}/handlers/"

infra = infrastructure.Infrastructure(stack_name=stack_name, handlers_dir=handlers_dir, config=config)
yield infra.deploy(Stack=infrastructure.InfrastructureStack)
infra.delete()


@pytest.fixture(scope="module")
def execute_lambda(create_infrastructure) -> InfrastructureOutput:
execution_time = datetime.datetime.utcnow()
session = boto3.Session()
client = session.client("lambda")
for _, arn in create_infrastructure.items():
data_fetcher.get_lambda_response(lambda_arn=arn, client=client)
return InfrastructureOutput(arns=create_infrastructure, execution_time=execution_time)
from tests.e2e.utils.infrastructure import LambdaLayerStack, deploy_once


@pytest.fixture(scope="session")
def lambda_layer_arn(lambda_layer_deployment):
yield lambda_layer_deployment.get("LayerArn")


@pytest.fixture(scope="session")
def lambda_layer_deployment(request: pytest.FixtureRequest, tmp_path_factory: pytest.TempPathFactory, worker_id: str):
"""Setup and teardown logic for E2E test infrastructure

Parameters
----------
request : pytest.FixtureRequest
pytest request fixture to introspect absolute path to test being executed
tmp_path_factory : pytest.TempPathFactory
pytest temporary path factory to discover shared tmp when multiple CPU processes are spun up
worker_id : str
pytest-xdist worker identification to detect whether parallelization is enabled

Yields
------
Dict[str, str]
CloudFormation Outputs from deployed infrastructure
"""
yield from deploy_once(
stack=LambdaLayerStack,
request=request,
tmp_path_factory=tmp_path_factory,
worker_id=worker_id,
layer_arn="",
)
17 changes: 10 additions & 7 deletions tests/e2e/logger/conftest.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
from pathlib import Path

import pytest

from tests.e2e.logger.infrastructure import LoggerStack
from tests.e2e.utils.infrastructure import deploy_once


@pytest.fixture(autouse=True, scope="module")
def infrastructure(request: pytest.FixtureRequest, tmp_path_factory: pytest.TempPathFactory, worker_id: str):
def infrastructure(request: pytest.FixtureRequest, lambda_layer_arn: str):
"""Setup and teardown logic for E2E test infrastructure

Parameters
----------
request : pytest.FixtureRequest
pytest request fixture to introspect absolute path to test being executed
tmp_path_factory : pytest.TempPathFactory
pytest temporary path factory to discover shared tmp when multiple CPU processes are spun up
worker_id : str
pytest-xdist worker identification to detect whether parallelization is enabled
lambda_layer_arn : str
Lambda Layer ARN

Yields
------
Dict[str, str]
CloudFormation Outputs from deployed infrastructure
"""
yield from deploy_once(stack=LoggerStack, request=request, tmp_path_factory=tmp_path_factory, worker_id=worker_id)
stack = LoggerStack(handlers_dir=Path(f"{request.path.parent}/handlers"), layer_arn=lambda_layer_arn)
try:
yield stack.deploy()
finally:
stack.delete()
10 changes: 6 additions & 4 deletions tests/e2e/logger/infrastructure.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from pathlib import Path

from tests.e2e.utils.infrastructure import BaseInfrastructureV2
from tests.e2e.utils.infrastructure import BaseInfrastructure


class LoggerStack(BaseInfrastructureV2):
def __init__(self, handlers_dir: Path, feature_name: str = "logger") -> None:
super().__init__(feature_name, handlers_dir)
class LoggerStack(BaseInfrastructure):
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()
Loading