8000 Allow conditional skip with skip_snapshot_verify (#6731) · localstack/localstack@7a87b5e · GitHub
[go: up one dir, main page]

Skip to content

Commit 7a87b5e

Browse files
Allow conditional skip with skip_snapshot_verify (#6731)
1 parent c422d9e commit 7a87b5e

File tree

7 files changed

+61
-86
lines changed

7 files changed

+61
-86
lines changed

localstack/testing/pytest/snapshot.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
from localstack.testing.snapshots.transformer_utility import SNAPSHOT_BASIC_TRANSFORMER
2121

2222

23+
def is_aws():
24+
return os.environ.get("TEST_TARGET", "") == "AWS_CLOUD"
25+
26+
2327
@pytest.hookimpl
2428
def pytest_configure(config: Config):
2529
config.addinivalue_line("markers", "skip_snapshot_verify")
@@ -59,12 +63,31 @@ def pytest_runtest_call(item: Item) -> None:
5963

6064
# TODO: extremely dirty... maybe it would be better to find a way to fail the test itself instead?
6165
sm = item.funcargs.get("snapshot")
66+
6267
if sm:
6368
verify = True
6469
paths = []
65-
for m in item.iter_markers(name="skip_snapshot_verify"):
66-
verify = False
67-
paths = m.kwargs.get("paths", [])
70+
71+
if not is_aws(): # only skip for local tests
72+
73+
for m in item.iter_markers(name="skip_snapshot_verify"):
74+
75+
skip_paths = m.kwargs.get("paths", [])
76+
77+
skip_condition = m.kwargs.get("condition")
78+
# can optionally include a condition, when this will be skipped
79+
# a condition must be a Callable returning something truthy/falsey
80+
if skip_condition:
81+
if not callable(skip_condition):
82+
raise ValueError("condition must be a callable")
83+
84+
if not skip_condition():
85+
continue # don't skip
86+
87+
# we skip verification if no condition has been specified
88+
verify = False
89+
paths.extend(skip_paths)
90+
6891
sm._assert_all(verify, paths)
6992

7093

tests/integration/awslambda/test_lambda.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@
4444
TEST_LAMBDA_JAVA,
4545
download_and_extract,
4646
)
47+
from localstack.testing.aws.lambda_utils import (
48+
concurrency_update_done,
49+
get_invoke_init_type,
50+
update_done,
51+
)
4752
from localstack.utils import testutil
4853
from localstack.utils.aws import aws_stack
4954
from localstack.utils.common import (
@@ -71,7 +76,6 @@
7176
)
7277

7378
from .functions import lambda_integration
74-
from .lambda_test_util import concurrency_update_done, get_invoke_init_type, update_done
7579

7680
LOG = logging.getLogger(__name__)
7781

tests/integration/awslambda/test_lambda_api.py

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# ALL TESTS IN HERE ARE VALIDATED AGAINST AWS CLOUD
21
import logging
32
import os.path
43

@@ -31,10 +30,6 @@
3130
],
3231
}
3332

34-
lambda_asf_only = pytest.mark.skipif(
35-
os.environ.get("PROVIDER_OVERRIDE_LAMBDA") != "asf", reason="Skip for non-asf provider"
36-
)
37-
3833

3934
# TODO: move this to fixtures / reconcile with other fixture usage
4035
@pytest.fixture
@@ -77,27 +72,18 @@ def _is_not_pending():
7772
LOG.debug(f"Unable to delete function {arn=} in cleanup")
7873

7974

80-
# 1. run test: AWS --snapshot-update
81-
# 2. (recommended) run test: AWS (to verify transformers and behavior)
82-
# 3. run test: localstack
83-
# a) use marker skip_snapshot_verify(paths=[...]) to exclude paths that do not match yet
84-
85-
86-
@pytest.mark.snapshot
87-
@pytest.mark.aws_compatible
75+
@pytest.mark.skip_snapshot_verify
76+
@pytest.mark.aws_validated
8877
class TestLambdaAsfApi:
89-
@pytest.mark.skip_snapshot_verify
9078
def test_basic_invoke(
9179
self, lambda_client, create_lambda_function_aws, lambda_su_role, snapshot
9280
):
81+
snapshot.add_transformer(snapshot.transform.lambda_api())
82+
9383
# predefined names
9484
fn_name = f"ls-fn-{short_uid()}"
9585
fn_name_2 = f"ls-fn-{short_uid()}"
9686

97-
# custom transformers -> valid for the whole test
98-
# doesn't matter if added before or after a "match"-instruction
99-
snapshot.add_transformer(snapshot.transform.lambda_api())
100-
10187
# infra setup (& validations)
10288
with open(os.path.join(os.path.dirname(__file__), "functions/echo.zip"), "rb") as f:
10389
response = create_lambda_function_aws(
@@ -123,8 +109,6 @@ def test_basic_invoke(
123109

124110
get_fn_result = lambda_client.get_function(FunctionName=fn_name)
125111

126-
# match -> registers they key and takes a dict as input
127-
# actual assert happens later (pytest-hook)
128112
snapshot.match("lambda_get_fn", get_fn_result)
129113

130114
get_fn_result_2 = lambda_client.get_function(FunctionName=fn_name_2)

tests/integration/awslambda/test_lambda_size_lims.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from io import BytesIO
33

44
import pytest
5-
from botocore.exceptions import ClientError
65

76
from localstack.services.awslambda.lambda_api import LAMBDA_DEFAULT_HANDLER
87
from localstack.services.awslambda.lambda_utils import LAMBDA_RUNTIME_PYTHON37
@@ -25,9 +24,11 @@ def generate_sized_python_str(size):
2524
return py_str
2625

2726

27+
@pytest.mark.aws_validated
2828
class TestLambdaSizeLimits:
29-
@pytest.mark.aws_validated
30-
def test_oversized_lambda(self, lambda_client, s3_client, s3_bucket, lambda_su_role):
29+
def test_oversized_lambda(self, lambda_client, s3_client, s3_bucket, lambda_su_role, snapshot):
30+
snapshot.add_transformer(snapshot.transform.lambda_api())
31+
3132
function_name = f"test_lambda_{short_uid()}"
3233
bucket_key = "test_lambda.zip"
3334
code_str = generate_sized_python_str(FUNCTION_MAX_UNZIPPED_SIZE)
@@ -39,7 +40,7 @@ def test_oversized_lambda(self, lambda_client, s3_client, s3_bucket, lambda_su_r
3940
s3_client.upload_fileobj(BytesIO(zip_file), s3_bucket, bucket_key)
4041

4142
# create lambda function
42-
with pytest.raises(ClientError) as e:
43+
with pytest.raises(lambda_client.exceptions.InvalidParameterValueException) as e:
4344
lambda_client.create_function(
4445
FunctionName=function_name,
4546
Runtime=LAMBDA_RUNTIME_PYTHON37,
@@ -48,13 +49,9 @@ def test_oversized_lambda(self, lambda_client, s3_client, s3_bucket, lambda_su_r
4849
Code={"S3Bucket": s3_bucket, "S3Key": bucket_key},
4950
Timeout=10,
5051
)
51-
assert e
52-
assert e.value.response["ResponseMetadata"]["HTTPStatusCode"] == 400
53-
e.match(
54-
r"An error occurred \(InvalidParameterValueException\) when calling the CreateFunction operation\: Unzipped size must be smaller than [0-9]* bytes"
55-
)
52+
snapshot.match("invalid_param_exc", e.value.response)
5653

57-
@pytest.mark.aws_validated
54+
# TODO: snapshot
5855
def test_large_lambda(self, lambda_client, s3_client, s3_bucket, lambda_su_role):
5956
function_name = f"test_lambda_{short_uid()}"
6057
bucket_key = "test_lambda.zip"
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"tests/integration/awslambda/test_lambda_size_lims.py::TestLambdaSizeLimits::test_oversized_lambda": {
3+
"recorded-date": "23-08-2022, 15:36:11",
4+
"recorded-content": {
5+
"invalid_param_exc": {
6+
"Error": {
7+
"Code": "InvalidParameterValueException",
8+
"Message": "Unzipped size must be smaller than 262144000 bytes"
9+
},
10+
"ResponseMetadata": {
11+
"HTTPHeaders": {},
12+
"HTTPStatusCode": 400
13+
},
14+
"Type": "User",
15+
"message": "Unzipped size must be smaller than 262144000 bytes"
16+
}
17+
}
18+
}
19+
}

tests/integration/awslambda/test_snapshots.snapshot.json

Lines changed: 0 additions & 52 deletions
This file was deleted.

0 commit comments

Comments
 (0)
0