Test and refactors.

This commit is contained in:
Pablo Martin 2023-01-09 13:38:49 +01:00
parent f684b2a043
commit 8534c727c4
4 changed files with 70 additions and 12 deletions

View file

@ -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
)

24
lolafect/utils.py Normal file
View file

@ -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")
)

View file

@ -1,3 +1,4 @@
prefect==1.2.2
requests==2.28.1
boto3==1.26.40
boto3==1.26.40
pytest==7.2.0

35
tests/test_lolaconfig.py Normal file
View file

@ -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