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 11, 2022
1 parent b40ca26 commit 94e76e5
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 35 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
18 changes: 2 additions & 16 deletions exodus_lambda/functions/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,8 @@ 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
with open(self._conf_file, "r", encoding="UTF-8") as json_file:
self._conf = json.load(json_file)
return self._conf

@property
Expand Down
5 changes: 4 additions & 1 deletion exodus_lambda/functions/origin_request.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import os
import time
import urllib
from datetime import datetime, timedelta, timezone
Expand All @@ -8,9 +9,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
5 changes: 4 additions & 1 deletion exodus_lambda/functions/origin_response.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import json
import os
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
41 changes: 41 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import os
import subprocess
import tempfile

import boto3
import pytest

Expand Down Expand Up @@ -41,3 +45,40 @@ 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(prefix="lambda_unittests_")

os.environ["EXODUS_LAMBDA_CONF_FILE"] = temp_file.name

test_env = os.environ.copy()

test_env["PROJECT"] = "test"
test_env["ENV_TYPE"] = "dev"
test_env["EXODUS_CONFIG_CACHE_TTL"] = "2"
test_env["ORIGIN_RESPONSE_LOGGER_LEVEL"] = "DEBUG"
test_env["ORIGIN_REQUEST_LOGGER_LEVEL"] = "DEBUG"
test_env["EXODUS_HEADERS_MAX_AGE"] = "600"

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,
env=test_env,
)

return temp_file


TEMP_FILE = mock_conf_file()


def pytest_sessionfinish(session, exitstatus):
# remove temp conf after whole test run finished
TEMP_FILE.close()
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
7 changes: 4 additions & 3 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
4 changes: 3 additions & 1 deletion tests/test_utils/utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import json
import os

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

def generate_test_config(conf="configuration/lambda_config.json"):

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

Expand Down

0 comments on commit 94e76e5

Please sign in to comment.