Skip to content

Commit

Permalink
exodus-cdn-playbooks should be able to configure log levels & more [R…
Browse files Browse the repository at this point in the history
…HELDST-9223]
  • Loading branch information
dichn committed Jan 10, 2022
1 parent b40ca26 commit 2b92aca
Show file tree
Hide file tree
Showing 11 changed files with 114 additions and 69 deletions.
8 changes: 4 additions & 4 deletions configuration/lambda_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
"us-east-1"
]
},
"config_cache_ttl": 2,
"config_cache_ttl": $EXODUS_CONFIG_CACHE_TTL,
"headers": {
"max_age": "600"
"max_age": "$EXODUS_HEADERS_MAX_AGE"
},
"logging": {
"version": 1,
Expand All @@ -27,10 +27,10 @@
},
"loggers": {
"origin-response": {
"level": "WARNING"
"level": "$ORIGIN_RESPONSE_LOGGER_LEVEL"
},
"origin-request": {
"level": "WARNING"
"level": "$ORIGIN_REQUEST_LOGGER_LEVEL"
},
"default": {
"level": "WARNING"
Expand Down
24 changes: 8 additions & 16 deletions exodus_lambda/functions/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,14 @@ def conf(self):
if isinstance(self._conf_file, dict):
self._conf = self._conf_file
else:
for conf_file in [
self._conf_file,
os.path.join(
os.path.dirname(
os.path.dirname(os.path.dirname(__file__))
),
"configuration",
"lambda_config.json",
),
]:
if os.path.exists(conf_file):
with open(
conf_file, "r", encoding="UTF-8"
) as json_file:
self._conf = json.load(json_file)
break
# to be polished: what if the self._conf_file not exist
if os.path.exists(self._conf_file):
with open(
self._conf_file, "r", encoding="UTF-8"
) as json_file:
self._conf = json.load(json_file)
else:
self.logger.error("Conf file not found: '%s'", self._conf_file)
return self._conf

@property
Expand Down
6 changes: 5 additions & 1 deletion exodus_lambda/functions/origin_request.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import json
import time
import urllib
Expand All @@ -9,8 +10,11 @@
from .base import LambdaBase


CONF_FILE = os.environ.get("EXODUS_LAMBDA_CONF_FILE") or "lambda_config.json"


class OriginRequest(LambdaBase):
def __init__(self, conf_file="lambda_config.json"):
def __init__(self, conf_file=CONF_FILE):
super().__init__("origin-request", conf_file)
self._db_client = None
self._cache = cachetools.TTLCache(
Expand Down
6 changes: 5 additions & 1 deletion exodus_lambda/functions/origin_response.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import os
import json
from base64 import b64encode

from .base import LambdaBase


CONF_FILE = os.environ.get("EXODUS_LAMBDA_CONF_FILE") or "lambda_config.json"


class OriginResponse(LambdaBase):
def __init__(self, conf_file="lambda_config.json"):
def __init__(self, conf_file=CONF_FILE):
super().__init__("origin-response", conf_file)

def handler(self, event, context):
Expand Down
5 changes: 5 additions & 0 deletions scripts/build-package
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
#!/bin/bash
set -e

export ORIGIN_RESPONSE_LOGGER_LEVEL=${ORIGIN_RESPONSE_LOGGER_LEVEL:-WARNING}
export ORIGIN_REQUEST_LOGGER_LEVEL=${ORIGIN_REQUEST_LOGGER_LEVEL:-WARNING}
export EXODUS_HEADERS_MAX_AGE=${EXODUS_HEADERS_MAX_AGE:-600}
export EXODUS_CONFIG_CACHE_TTL=${EXODUS_CONFIG_CACHE_TTL:2}

pip install --require-hashes -r requirements.txt --target ./package
pip install --no-deps --target ./package .
cp ./configuration/exodus-lambda-deploy.yaml ./package
Expand Down
29 changes: 29 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import os
import boto3
import pytest
import tempfile
import subprocess


def get_distribution_url(stack_name):
Expand Down Expand Up @@ -41,3 +44,29 @@ def cdn_test_url(request):
else:
pytest.skip("Test skipped without --cdn-test-url or --lambda-stack")
return url


def mock_conf_file():
temp_file = tempfile.NamedTemporaryFile(delete=False)

os.environ["PROJECT"] = "test"
os.environ["ENV_TYPE"] = "dev"
os.environ["EXODUS_CONFIG_CACHE_TTL"] = "2"
os.environ["ORIGIN_RESPONSE_LOGGER_LEVEL"] = "DEBUG"
os.environ["ORIGIN_REQUEST_LOGGER_LEVEL"] = "DEBUG"
os.environ["EXODUS_HEADERS_MAX_AGE"] = "600"
os.environ["EXODUS_LAMBDA_CONF_FILE"] = temp_file.name

cmd = "envsubst < ./configuration/lambda_config.json > {temp_path}"
cmd = cmd.format(temp_path=temp_file.name)

subprocess.run(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
check=True,
shell=True,
)


mock_conf_file()
3 changes: 1 addition & 2 deletions tests/functions/test_alias.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@

from ..test_utils.utils import generate_test_config

CONF_PATH = "configuration/lambda_config.json"
TEST_CONF = generate_test_config(CONF_PATH)
TEST_CONF = generate_test_config()

Alias = namedtuple("Alias", ["src", "dest"])

Expand Down
9 changes: 5 additions & 4 deletions tests/functions/test_base.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import logging

import os
import pytest

from exodus_lambda.functions.base import LambdaBase

from ..test_utils.utils import generate_test_config

CONF_PATH = "configuration/lambda_config.json"
TEST_CONF = generate_test_config(CONF_PATH)

CONF_FILE = os.environ.get("EXODUS_LAMBDA_CONF_FILE")
TEST_CONF = generate_test_config()


def test_base_handler():
with pytest.raises(NotImplementedError):
LambdaBase(conf_file=CONF_PATH).handler(event=None, context=None)
LambdaBase(conf_file=CONF_FILE).handler(event=None, context=None)


@pytest.mark.parametrize(
Expand Down
3 changes: 1 addition & 2 deletions tests/functions/test_origin_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@

TEST_PATH = "/origin/rpms/repo/ver/dir/filename.ext"
MOCKED_DT = "2020-02-17T15:38:05.864+00:00"
CONF_PATH = "configuration/lambda_config.json"
TEST_CONF = generate_test_config(CONF_PATH)
TEST_CONF = generate_test_config()


@pytest.mark.parametrize(
Expand Down
9 changes: 4 additions & 5 deletions tests/functions/test_origin_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@

from ..test_utils.utils import generate_test_config

CONF_PATH = "configuration/lambda_config.json"
TEST_CONF = generate_test_config(CONF_PATH)
TEST_CONF = generate_test_config()
MAX_AGE = TEST_CONF["headers"]["max_age"]


Expand Down Expand Up @@ -60,7 +59,7 @@ def test_origin_response_valid_headers(original_uri, want_digest):
}
]

response = OriginResponse(conf_file=CONF_PATH).handler(event, context=None)
response = OriginResponse(conf_file=TEST_CONF).handler(event, context=None)
assert response["headers"] == expected_headers


Expand All @@ -75,14 +74,14 @@ def test_origin_response_empty_headers(test_input):
]
}

response = OriginResponse(conf_file=CONF_PATH).handler(event, context=None)
response = OriginResponse(conf_file=TEST_CONF).handler(event, context=None)
assert response["headers"] == {}


def test_origin_response_missing_headers():
event = {"Records": [{"cf": {"request": {}, "response": {}}}]}

response = OriginResponse(conf_file=CONF_PATH).handler(event, context=None)
response = OriginResponse(conf_file=TEST_CONF).handler(event, context=None)
assert response == {}


Expand Down
81 changes: 47 additions & 34 deletions tests/test_utils/utils.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,55 @@
import json
import os

CONF_FILE = os.environ.get("EXODUS_LAMBDA_CONF_FILE")

def generate_test_config(conf="configuration/lambda_config.json"):
with open(conf, "r") as json_file:
conf = json.load(json_file)

# table
conf["table"]["name"] = "test-table"
conf["table"]["available_regions"] = [
"us-east-1",
"us-east-2",
"us-west-1",
"us-west-2",
"ca-central-1",
"eu-central-1",
"eu-west-2",
"eu-west-3",
"eu-west-1",
"sa-east-1",
"ap-south-1",
"ap-northeast-2",
"ap-southeast-1",
"ap-southeast-2",
"ap-northeast-1",
]

# logging
conf["logging"]["formatters"]["default"][
"format"
] = "[%(levelname)s] - %(message)s\n"
conf["logging"]["loggers"]["origin-response"]["level"] = "DEBUG"
conf["logging"]["loggers"]["origin-request"]["level"] = "DEBUG"
conf["logging"]["loggers"]["default"]["level"] = "DEBUG"

conf["config_table"]["name"] = "test-config-table"
conf["config_table"]["cache_ttl"] = 2

def generate_test_config(conf="configuration/lambda_config.json"):
conf = {
"table": {
"name": "test-table",
"available_regions": [
"us-east-1",
"us-east-2",
"us-west-1",
"us-west-2",
"ca-central-1",
"eu-central-1",
"eu-west-2",
"eu-west-3",
"eu-west-1",
"sa-east-1",
"ap-south-1",
"ap-northeast-2",
"ap-southeast-1",
"ap-southeast-2",
"ap-northeast-1",
],
},
"config_table": {
"name": "test-config-table",
"available_regions": ["us-east-1"],
"cache_ttl": 2,
},
"config_cache_ttl": 2,
"headers": {"max_age": "600"},
"logging": {
"version": 1,
"incremental": "True",
"disable_existing_loggers": "False",
"formatters": {
"default": {
"format": "[%(levelname)s] - %(message)s\n",
"datefmt": "%Y-%m-%d %H:%M:%S",
}
},
"loggers": {
"origin-response": {"level": "DEBUG"},
"origin-request": {"level": "DEBUG"},
"default": {"level": "DEBUG"},
},
},
}
return conf


Expand Down

0 comments on commit 2b92aca

Please sign in to comment.