File size: 6,808 Bytes
4021124 | 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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 | import os
import warnings
from dataclasses import dataclass
from pathlib import Path
import pytest
from pydantic import BaseModel
from aws_lambda_powertools.shared import constants
from aws_lambda_powertools.shared.functions import (
abs_lambda_path,
extract_event_from_common_models,
powertools_debug_is_set,
powertools_dev_is_set,
resolve_env_var_choice,
resolve_max_age,
resolve_truthy_env_var_choice,
sanitize_xray_segment_name,
strtobool,
)
from aws_lambda_powertools.utilities.data_classes.common import DictWrapper
from aws_lambda_powertools.utilities.parameters.base import DEFAULT_MAX_AGE_SECS
@pytest.fixture
def default_lambda_path():
return "/var/task"
def test_resolve_env_var_choice_explicit_wins_over_env_var():
assert resolve_truthy_env_var_choice(env="true", choice=False) is False
assert resolve_env_var_choice(env="something", choice=False) is False
def test_resolve_env_var_choice_env_wins_over_absent_explicit():
assert resolve_truthy_env_var_choice(env="true") == 1
assert resolve_env_var_choice(env="something") == "something"
@pytest.mark.parametrize("true_value", ["y", "yes", "t", "true", "on", "1"])
def test_strtobool_true(true_value):
assert strtobool(true_value)
@pytest.mark.parametrize("false_value", ["n", "no", "f", "false", "off", "0"])
def test_strtobool_false(false_value):
assert strtobool(false_value) is False
def test_strtobool_value_error():
with pytest.raises(ValueError) as exp:
strtobool("fail")
assert str(exp.value) == "invalid truth value 'fail'"
def test_powertools_dev_warning(monkeypatch: pytest.MonkeyPatch):
# GIVEN POWERTOOLS_DEBUG is set
monkeypatch.setenv(constants.POWERTOOLS_DEV_ENV, "1")
warning_message = "POWERTOOLS_DEV environment variable is enabled. Increasing verbosity across utilities."
# WHEN set_package_logger is used at initialization
# THEN a warning should be emitted
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("default")
powertools_dev_is_set()
assert len(w) == 1
assert str(w[0].message) == warning_message
def test_powertools_debug_warning(monkeypatch: pytest.MonkeyPatch):
# GIVEN POWERTOOLS_DEBUG is set
monkeypatch.setenv(constants.POWERTOOLS_DEBUG_ENV, "1")
warning_message = "POWERTOOLS_DEBUG environment variable is enabled. Setting logging level to DEBUG."
# WHEN set_package_logger is used at initialization
# THEN a warning should be emitted
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("default")
powertools_debug_is_set()
assert len(w) == 1
assert str(w[0].message) == warning_message
def test_extract_event_dict():
data = {"hello": "world"}
assert extract_event_from_common_models(data) == data
def test_extract_event_pydantic():
class DummyModel(BaseModel):
hello: str
data = {"hello": "world"}
assert extract_event_from_common_models(DummyModel(**data)) == data
def test_extract_event_dict_wrapper():
class DummyClassSample(DictWrapper):
pass
data = {"hello": "world"}
assert extract_event_from_common_models(DummyClassSample(data)) == data
def test_extract_event_dataclass():
@dataclass
class DummyDataclass:
hello: str
data = {"hello": "world"}
assert extract_event_from_common_models(DummyDataclass(**data)) == data
@pytest.mark.parametrize("data", [False, True, "", 10, [], {}, object])
def test_extract_event_any(data):
assert extract_event_from_common_models(data) == data
def test_resolve_max_age_explicit_wins_over_env_var(monkeypatch: pytest.MonkeyPatch):
# GIVEN POWERTOOLS_PARAMETERS_MAX_AGE environment variable is set
monkeypatch.setenv(constants.PARAMETERS_MAX_AGE_ENV, "20")
# WHEN the choice is set explicitly
max_age = resolve_max_age(env=os.getenv(constants.PARAMETERS_MAX_AGE_ENV, DEFAULT_MAX_AGE_SECS), choice=10)
# THEN the result must be the choice
assert max_age == 10
def test_resolve_max_age_with_default_value():
# GIVEN POWERTOOLS_PARAMETERS_MAX_AGE is not set
# WHEN the choice is set to None
max_age = resolve_max_age(env=os.getenv(constants.PARAMETERS_MAX_AGE_ENV, DEFAULT_MAX_AGE_SECS), choice=None)
# THEN the result must be the default value (DEFAULT_MAX_AGE_SECS)
assert max_age == int(DEFAULT_MAX_AGE_SECS)
def test_resolve_max_age_env_var_wins_over_default_value(monkeypatch: pytest.MonkeyPatch):
# GIVEN POWERTOOLS_PARAMETERS_MAX_AGE environment variable is set
monkeypatch.setenv(constants.PARAMETERS_MAX_AGE_ENV, "20")
# WHEN the choice is set to None
max_age = resolve_max_age(env=os.getenv(constants.PARAMETERS_MAX_AGE_ENV, DEFAULT_MAX_AGE_SECS), choice=None)
# THEN the result must be the environment variable value
assert max_age == 20
def test_abs_lambda_path_empty():
# Given Env is not set
os.environ["LAMBDA_TASK_ROOT"] = ""
# Then path = os.getcwd
assert abs_lambda_path() == f"{Path.cwd()}"
def test_abs_lambda_path_empty_envvar(default_lambda_path):
# Given Env is set
os.environ["LAMBDA_TASK_ROOT"] = default_lambda_path
# Then path = Env/
assert abs_lambda_path() == default_lambda_path
def test_abs_lambda_path_w_filename():
# Given Env is not set and relative_path provided
relatvie_path = "cert/pub.cert"
os.environ["LAMBDA_TASK_ROOT"] = ""
# Then path = os.getcwd + relative_path
assert abs_lambda_path(relatvie_path) == str(Path(Path.cwd(), relatvie_path))
def test_abs_lambda_path_w_filename_envvar(default_lambda_path):
# Given Env is set and relative_path provided
relative_path = "cert/pub.cert"
os.environ["LAMBDA_TASK_ROOT"] = default_lambda_path
# Then path = env + relative_path
assert abs_lambda_path(relative_path="cert/pub.cert") == str(Path(os.environ["LAMBDA_TASK_ROOT"], relative_path))
def test_sanitize_xray_segment_name():
# GIVEN a name with invalid characters
invalid_name = "app?;*.lambda_function.(<locals>).get_todos!$~^<>"
# WHEN we sanitize this name by removing invalid characters
sanitized_name = sanitize_xray_segment_name(invalid_name)
# THEN the sanitized name should not contain invalid characters
expected_name = "app.lambda_function.locals.get_todos"
assert sanitized_name == expected_name
def test_sanitize_xray_segment_name_with_no_special_characters():
# GIVEN a name without any invalid characters
valid_name = "app#lambda_function"
# WHEN we sanitize this name
sanitized_name = sanitize_xray_segment_name(valid_name)
# THEN the sanitized name remains the same as the original name
expected_name = valid_name
assert sanitized_name == expected_name
|