Skip to content

Commit

Permalink
mesh-1684: ensure moto mocks on test setup too
Browse files Browse the repository at this point in the history
  • Loading branch information
matt-mercer committed Sep 24, 2023
1 parent 793fad3 commit ebc2c5c
Show file tree
Hide file tree
Showing 16 changed files with 695 additions and 612 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/merge-develop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ jobs:
with:
path: |
.venv
key: ${{ runner.os }}-poetry-v2-py3.9-${{ hashFiles('./poetry.lock') }}
**/.lock-hash
**/requirements.txt
key: ${{ runner.os }}-poetry-v2-${{ hashFiles('./poetry.lock') }}

- name: git reset
run: git reset --hard
Expand Down
8 changes: 6 additions & 2 deletions .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ jobs:
with:
path: |
.venv
key: ${{ runner.os }}-poetry-v2-py3.9-${{ hashFiles('./poetry.lock') }}
**/.lock-hash
**/requirements.txt
key: ${{ runner.os }}-poetry-v2-${{ hashFiles('./poetry.lock') }}

- name: git reset
run: git reset --hard
Expand Down Expand Up @@ -179,7 +181,9 @@ jobs:
with:
path: |
.venv
key: ${{ runner.os }}-poetry-v2-py3.9-${{ hashFiles('./poetry.lock') }}
**/.lock-hash
**/requirements.txt
key: ${{ runner.os }}-poetry-v2-${{ hashFiles('./poetry.lock') }}

- name: git reset
run: git reset --hard
Expand Down
3 changes: 1 addition & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ services:
image: "${LOCALSTACK_IMAGE:-localstack/localstack:latest}"
restart: always
environment:
# - SERVICES=ssm,secretsmanager,s3,dynamodb,logs,lambda,cloudwatch,iam,sqs,sns,sts,resourcegroupstaggingapi,monitoring,events,ec2,kms,stepfunctions
- PROVIDER_OVERRIDE_STEPFUNCTIONS=v2
- DEFAULT_REGION=eu-west-2
- DEBUG=1
Expand All @@ -29,7 +28,7 @@ services:
build:
context: https://github.com/NHSDigital/mesh-sandbox.git#develop
ports:
- "8700:80"
- "8700:443"
deploy:
restart_policy:
condition: on-failure
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
from http import HTTPStatus
from math import ceil

import boto3
from aws_lambda_powertools.utilities.data_classes import EventBridgeEvent
from nhs_aws_helpers import s3_client, ssm_client
from spine_aws_common import LambdaApplication
from spine_aws_common.utilities import human_readable_bytes

Expand Down Expand Up @@ -120,8 +120,8 @@ def _get_mapping(self, bucket, key):
folder += "/"

path = f"/{self.environment}/mesh/mapping/{bucket}/{folder}"
ssm_client = boto3.client("ssm", region_name="eu-west-2")
mailbox_mapping_params = ssm_client.get_parameters_by_path(
ssm = ssm_client()
mailbox_mapping_params = ssm.get_parameters_by_path(
Path=path,
Recursive=False,
WithDecryption=True,
Expand All @@ -148,8 +148,8 @@ def _get_mapping(self, bucket, key):
@staticmethod
def _get_file_size(bucket, key):
"""Get file size"""
s3_client = boto3.client("s3")
response = s3_client.head_object(Bucket=bucket, Key=key)
s3 = s3_client()
response = s3.head_object(Bucket=bucket, Key=key)
file_size = response.get("ContentLength")
return file_size

Expand Down
16 changes: 7 additions & 9 deletions mesh_client_aws_serverless/mesh_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
import os
from collections import namedtuple

import boto3

REGION_NAME = os.environ.get("AWS_REGION", "eu-west-2")
from nhs_aws_helpers import secrets_client, ssm_client, stepfunctions


class SingletonCheckFailure(Exception):
Expand Down Expand Up @@ -33,7 +31,7 @@ class MeshCommon:
@staticmethod
def singleton_check(mailbox, my_step_function_name):
"""Find out whether there is another step function running for my mailbox"""
sfn_client = boto3.client("stepfunctions", region_name="eu-west-2")
sfn_client = stepfunctions()
response = sfn_client.list_state_machines()
# Get my step function arn
my_step_function_arn = None
Expand Down Expand Up @@ -99,8 +97,8 @@ def get_params(path, recursive=False, decryption=True):
"""
Get parameters from SSM and secrets manager
"""
ssm_client = boto3.client("ssm", region_name=REGION_NAME)
params_result = ssm_client.get_parameters_by_path(
ssm = ssm_client()
params_result = ssm.get_parameters_by_path(
Path=path,
Recursive=recursive,
WithDecryption=decryption,
Expand All @@ -113,13 +111,13 @@ def get_params(path, recursive=False, decryption=True):
var_name = os.path.basename(name)
new_params_dict[var_name] = entry.get("Value", None)
if os.environ.get("use_secrets_manager") == "true":
secrets_client = boto3.client("secretsmanager", region_name=REGION_NAME)
all_secrets_dict = secrets_client.list_secrets()
secrets = secrets_client()
all_secrets_dict = secrets.list_secrets()
all_secrets_list = all_secrets_dict["SecretList"]
for secret in all_secrets_list:
name = secret["Name"]
if name.startswith(path):
secret_value = secrets_client.get_secret_value(SecretId=name)[
secret_value = secrets.get_secret_value(SecretId=name)[
"SecretString"
]
var_name = os.path.basename(name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,15 @@
import os
from http import HTTPStatus

import boto3
from botocore.exceptions import ClientError
from nhs_aws_helpers import s3_client
from spine_aws_common import LambdaApplication

from mesh_client_aws_serverless.mesh_common import MeshCommon
from mesh_client_aws_serverless.mesh_mailbox import MeshMailbox


class MeshFetchMessageChunkApplication(
LambdaApplication
): # pylint: disable=too-many-instance-attributes
class MeshFetchMessageChunkApplication(LambdaApplication):
"""
MESH API Lambda for sending a message
"""
Expand All @@ -42,7 +40,6 @@ def __init__(self, additional_log_config=None, load_ssm_params=False):
self.number_of_chunks = 0
self.current_chunk = 0
self.message_id = None
self.region = os.environ.get("AWS_REGION", "eu-west-2")
self.s3_client = None
self.s3_bucket = ""
self.http_headers_bytes_read = 0
Expand All @@ -60,7 +57,7 @@ def initialise(self):
self.current_chunk = self.input.get("chunk_num", 1)
self.message_id = self.input["message_id"]
self.response = self.event.raw_event
self.s3_client = boto3.client("s3", region_name=self.region)
self.s3_client = s3_client()
self.log_object.internal_id = self.internal_id
self._setup_mailbox()

Expand Down
4 changes: 1 addition & 3 deletions mesh_client_aws_serverless/mesh_mailbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def __init__(self, msg=None):
self.msg = msg


class MeshMailbox: # pylint: disable=too-many-instance-attributes
class MeshMailbox:
"""Mailbox class that handles all the complexity of talking to MESH API"""

AUTH_SCHEMA_NAME = "NHSMESH"
Expand Down Expand Up @@ -126,7 +126,6 @@ def _write_certs_to_files(self) -> None:
"""Write the certificates to a local file"""
self.log_object.write_log("MESHMBOX0002", None, None)

# pylint: disable=consider-using-with
self.temp_dir_object = tempfile.TemporaryDirectory()
temp_dir = self.temp_dir_object.name

Expand All @@ -147,7 +146,6 @@ def _write_certs_to_files(self) -> None:
ca_cert = self.params[MeshMailbox.MESH_CA_CERT]
self.ca_cert_file.write(ca_cert.encode("utf-8"))
self.ca_cert_file.seek(0)
# pylint: enable=consider-using-with

def _build_mesh_authorization_header(
self, nonce: Optional[str] = None, noncecount: int = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ def __init__(self, msg=None):
self.msg = msg


class MeshSendMessageChunkApplication(
LambdaApplication
): # pylint: disable=too-many-instance-attributes
class MeshSendMessageChunkApplication(LambdaApplication):
"""
MESH API Lambda for sending a message / message chunk
"""
Expand Down
Loading

0 comments on commit ebc2c5c

Please sign in to comment.