Skip to content

Commit

Permalink
EM-933. Fixing existing tests to work with the new lock table and fie…
Browse files Browse the repository at this point in the history
…lds.
  • Loading branch information
johnmarston-nhs committed Sep 20, 2024
1 parent 8342d4d commit ff7491d
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 5 deletions.
50 changes: 48 additions & 2 deletions poetry.lock

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

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ mypy = "^1.4.0"
coverage = "^7.2.7"
pytest = "^8.1.1"
pytest-asyncio = "^0.23.6"
moto = {extras = ["s3", "ssm", "stepfunctions", "secretsmanager"], version = "^5.0.5"}
moto = {extras = ["s3", "ssm", "stepfunctions", "secretsmanager", "dynamodb"], version = "^5.0.14"}
boto3-stubs = {extras = ["s3", "ssm", "secretsmanager", "dynamodb", "stepfunctions", "sqs", "lambda", "logs",], version = "^1.34.32"}
ruff = "^0"
petname = "^2.6"
Expand Down
1 change: 1 addition & 0 deletions src/shared/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ def acquire_lock(ddb_client: DynamoDBClient, lock_name: str, execution_id: str):
except ClientError as ce:
if ce.response["Error"]["Code"] == "ConditionalCheckFailedException":
raise SingletonCheckFailure(f"Lock already exists for {lock_name}") from ce
raise ce
return resp.items


Expand Down
39 changes: 39 additions & 0 deletions tests/mocked/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@
from integration.test_helpers import temp_env_vars
from mesh_client import MeshClient
from moto import mock_aws
from mypy_boto3_dynamodb import DynamoDBClient
from mypy_boto3_s3 import S3Client
from mypy_boto3_stepfunctions import SFNClient
from nhs_aws_helpers import (
dynamodb_client as _ddb_client,
)
from nhs_aws_helpers import (
s3_client as _s3_client,
)
Expand Down Expand Up @@ -39,6 +43,40 @@ def s3_client(_mock_aws) -> S3Client:
return _s3_client()


@pytest.fixture(name="ddb_client")
def ddb_client(_mock_aws) -> DynamoDBClient:
return _ddb_client()


@pytest.fixture
def mocked_lock_table(ddb_client: DynamoDBClient, environment):
table_name = os.getenv("DDB_LOCK_TABLE_NAME") or "mocked-lock-table"
ddb_client.create_table(
AttributeDefinitions=[
{"AttributeName": "LockOwner", "AttributeType": "S"},
{"AttributeName": "LockType", "AttributeType": "S"},
{"AttributeName": "LockName", "AttributeType": "S"},
],
TableName=table_name,
KeySchema=[{"AttributeName": "LockName", "KeyType": "HASH"}],
GlobalSecondaryIndexes=[
{
"IndexName": "LockTypeOwnerTableIndex",
"KeySchema": [
{"AttributeName": "LockType", "KeyType": "HASH"},
{"AttributeName": "LockOwner", "KeyType": "RANGE"},
],
"Projection": {"ProjectionType": "KEYS_ONLY"},
}
],
BillingMode="PAY_PER_REQUEST",
)
yield ddb_client.describe_table(TableName=table_name).get("Table", None)

# FIXME - do I need this?
ddb_client.delete_table(TableName=table_name)


@pytest.fixture
def environment(
_mock_aws,
Expand All @@ -61,6 +99,7 @@ def environment(
SHARED_KEY_CONFIG_KEY=f"/{environment}/mesh/MESH_SHARED_KEY",
MAILBOXES_BASE_CONFIG_KEY=f"/{environment}/mesh/mailboxes",
VERIFY_CHECKS_COMMON_NAME=False,
DDB_LOCK_TABLE_NAME="mocked-lock-table",
):
yield environment

Expand Down
13 changes: 11 additions & 2 deletions tests/mocked/mesh_check_send_parameters_application_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@


def test_mesh_check_send_parameters_happy_path_chunked(
mesh_s3_bucket: str, environment: str, send_message_sfn_arn: str, capsys
mesh_s3_bucket: str,
environment: str,
send_message_sfn_arn: str,
capsys,
mocked_lock_table,
):
"""Test the lambda as a whole, happy path for small file"""

Expand Down Expand Up @@ -52,6 +56,8 @@ def test_mesh_check_send_parameters_happy_path_chunked(
"total_chunks": 4,
"workflow_id": "TESTWORKFLOW",
},
"lock_name": f"SendLock_{mesh_s3_bucket}_X26ABC2/outbound/testfile.json",
"execution_id": "TEST1234",
},
}
from mesh_check_send_parameters_application import (
Expand Down Expand Up @@ -305,7 +311,10 @@ def sample_trigger_event(
"time": "2021-06-29T14:10:55Z",
"region": "eu-west-2",
"resources": [],
"detail": s3_event,
"EventDetail": {
"detail": s3_event,
},
"ExecutionId": "TEST1234",
}

return event_bridge_event

0 comments on commit ff7491d

Please sign in to comment.