forked from aws-powertools/powertools-lambda-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconftest.py
67 lines (48 loc) · 2.06 KB
/
conftest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import datetime
import sys
import uuid
from dataclasses import dataclass
import boto3
# We only need typing_extensions for python versions <3.8
from tests.e2e.utils import data_fetcher
if sys.version_info >= (3, 8):
from typing import TypedDict
else:
from typing_extensions import TypedDict
from typing import Dict, Generator, Optional
import pytest
from e2e.utils import infrastructure
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)