diff --git a/configuration/lambda_config.json b/configuration/lambda_config.json index 32060970..98fcd2ca 100755 --- a/configuration/lambda_config.json +++ b/configuration/lambda_config.json @@ -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, @@ -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" diff --git a/exodus_lambda/functions/base.py b/exodus_lambda/functions/base.py index 9c06f240..3903e4d5 100644 --- a/exodus_lambda/functions/base.py +++ b/exodus_lambda/functions/base.py @@ -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 diff --git a/exodus_lambda/functions/origin_request.py b/exodus_lambda/functions/origin_request.py index 47411b5c..b8aacc06 100755 --- a/exodus_lambda/functions/origin_request.py +++ b/exodus_lambda/functions/origin_request.py @@ -1,4 +1,5 @@ import json +import os import time import urllib from datetime import datetime, timedelta, timezone @@ -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( diff --git a/exodus_lambda/functions/origin_response.py b/exodus_lambda/functions/origin_response.py index 6ec3c4da..64c703c5 100755 --- a/exodus_lambda/functions/origin_response.py +++ b/exodus_lambda/functions/origin_response.py @@ -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): diff --git a/scripts/build-package b/scripts/build-package index 01ebfa53..2fffabcc 100755 --- a/scripts/build-package +++ b/scripts/build-package @@ -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 diff --git a/tests/conftest.py b/tests/conftest.py index 7d1bb2e5..b0dd7dfa 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,3 +1,7 @@ +import os +import subprocess +import tempfile + import boto3 import pytest @@ -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 + + +MOCK_LAMBDA_CONF_FILE = mock_conf_file() + + +def pytest_sessionfinish(session, exitstatus): + # remove temp conf after whole test run finished + MOCK_LAMBDA_CONF_FILE.close() diff --git a/tests/functions/test_alias.py b/tests/functions/test_alias.py index d7b73d4f..d377e869 100644 --- a/tests/functions/test_alias.py +++ b/tests/functions/test_alias.py @@ -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"]) diff --git a/tests/functions/test_base.py b/tests/functions/test_base.py index 250d7be3..648ffc19 100644 --- a/tests/functions/test_base.py +++ b/tests/functions/test_base.py @@ -1,4 +1,5 @@ import logging +import os import pytest @@ -6,13 +7,13 @@ 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( diff --git a/tests/functions/test_origin_request.py b/tests/functions/test_origin_request.py index 3c4e865d..de983299 100644 --- a/tests/functions/test_origin_request.py +++ b/tests/functions/test_origin_request.py @@ -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( diff --git a/tests/functions/test_origin_response.py b/tests/functions/test_origin_response.py index fe7841cf..0c3447af 100644 --- a/tests/functions/test_origin_response.py +++ b/tests/functions/test_origin_response.py @@ -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"] @@ -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 @@ -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 == {} diff --git a/tests/test_utils/utils.py b/tests/test_utils/utils.py index c275a63e..2f74ae8b 100644 --- a/tests/test_utils/utils.py +++ b/tests/test_utils/utils.py @@ -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)