From 8534c727c41e1454d0bf47ab4e58ab17f2b6c4b8 Mon Sep 17 00:00:00 2001 From: Pablo Martin Date: Mon, 9 Jan 2023 13:38:49 +0100 Subject: [PATCH] Test and refactors. --- lolafect/lolaconfig.py | 20 +++++++++----------- lolafect/utils.py | 24 ++++++++++++++++++++++++ requirements-dev.txt | 3 ++- tests/test_lolaconfig.py | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 70 insertions(+), 12 deletions(-) create mode 100644 lolafect/utils.py create mode 100644 tests/test_lolaconfig.py diff --git a/lolafect/lolaconfig.py b/lolafect/lolaconfig.py index 519b128..e7eefda 100644 --- a/lolafect/lolaconfig.py +++ b/lolafect/lolaconfig.py @@ -1,4 +1,3 @@ -import json from typing import List from prefect.storage import S3 @@ -11,6 +10,7 @@ from lolafect.defaults import ( DEFAULT_KUBERNETES_LABELS, DEFAULT_FLOWS_PATH_IN_BUCKET, ) +from lolafect.utils import S3FileReader class LolaConfig: @@ -62,23 +62,21 @@ class LolaConfig: DEFAULT_KUBERNETES_IMAGE if kubernetes_image is None else kubernetes_image ) - def fetch_slack_webhooks(self, s3_client=None) -> None: + self._s3_reader = S3FileReader(s3_client=boto3.client("s3")) + + def fetch_slack_webhooks(self, s3_reader=None) -> None: """ Read the slack webhooks file from S3 and store the webhooks in memory. - :param s3_client: a client to fetch files from S3. + :param s3_reader: a client to fetch files from S3. :return: None """ - if s3_client is None: - s3_client = boto3.client("s3") + if s3_reader is None: + s3_reader = self._s3_reader - self.SLACK_WEBHOOKS = json.loads( - s3_client.get_object( - Bucket=self.S3_BUCKET_NAME, Key=self.SLACK_WEBHOOKS_FILE - )["Body"] - .read() - .decode("utf-8") + self.SLACK_WEBHOOKS = s3_reader.read_json_from_s3_file( + bucket=self.S3_BUCKET_NAME, key=self.SLACK_WEBHOOKS_FILE ) diff --git a/lolafect/utils.py b/lolafect/utils.py new file mode 100644 index 0000000..621e59d --- /dev/null +++ b/lolafect/utils.py @@ -0,0 +1,24 @@ +import json + + +class S3FileReader: + """ + An S3 client along with a few reading utils. + """ + + def __init__(self, s3_client): + self.s3_client = s3_client + + def read_json_from_s3_file(self, bucket: str, key: str) -> dict: + """ + Read a JSON file from an S3 location and return contents as a dict. + + :param bucket: the name of the bucket where the file is stored. + :param key: the path to the file within the bucket. + :return: the file contents. + """ + return json.loads( + self.s3_client.get_object(Bucket=bucket, Key=key)["Body"] + .read() + .decode("utf-8") + ) diff --git a/requirements-dev.txt b/requirements-dev.txt index 369f5f8..86204fb 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -1,3 +1,4 @@ prefect==1.2.2 requests==2.28.1 -boto3==1.26.40 \ No newline at end of file +boto3==1.26.40 +pytest==7.2.0 \ No newline at end of file diff --git a/tests/test_lolaconfig.py b/tests/test_lolaconfig.py new file mode 100644 index 0000000..742de77 --- /dev/null +++ b/tests/test_lolaconfig.py @@ -0,0 +1,35 @@ +from types import SimpleNamespace + +from lolafect.lolaconfig import LolaConfig + + +def test_lolaconfig_instantiates_with_defaults_properly(): + + lolaconfig = LolaConfig(flow_name="some-flow") + + +def test_lolaconfig_instantiates_without_defaults_proplery(): + + lolaconfig = LolaConfig( + flow_name="some-flow", + env_s3_bucket="bucket", + kubernetes_labels=["some_label"], + kubernetes_image="loliloli:latest", + slack_webhooks_file="the_file/is/here.json", + ) + + +def test_lolaconfig_fetches_webhooks_properly(): + + lolaconfig = LolaConfig(flow_name="some-flow") + + fake_s3_reader = SimpleNamespace() + + def mock_read_json_from_s3_file(bucket, key): + return {"a-channel-name": "a-channel-url.com"} + + fake_s3_reader.read_json_from_s3_file = mock_read_json_from_s3_file + + lolaconfig.fetch_slack_webhooks(s3_reader=fake_s3_reader) + + assert type(lolaconfig.SLACK_WEBHOOKS) is dict