diff --git a/.gitignore b/.gitignore index 4b36cff..4f5a7f6 100644 --- a/.gitignore +++ b/.gitignore @@ -58,3 +58,6 @@ target/ # Python environment .python-version + +# Vitual Environments +venv/ diff --git a/LICENSE b/LICENSE index a3a3ed5..c4d8dfd 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2015-2017 HDE, Inc. +Copyright (c) 2015-2018 HDE, Inc. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index c314974..39b34b4 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ Run lambda function on local machine ## Prepare development environment -Please use a newly created virtualenv of Python 2.7 or Python 3.6 . +Please use a newly created virtualenv of Python 2.7 or Python 3.6. ## Installation diff --git a/README.rst b/README.rst index deb21a0..c85b854 100644 --- a/README.rst +++ b/README.rst @@ -9,7 +9,7 @@ Run lambda function on local machine Prepare development environment ------------------------------- -Please use a newly created virtualenv of Python 2.7 or Python 3.6 . +Please use a newly created virtualenv of Python 2.7 or Python 3.6. Installation ------------ diff --git a/lambda_local/__init__.py b/lambda_local/__init__.py index f56a061..eabee17 100644 --- a/lambda_local/__init__.py +++ b/lambda_local/__init__.py @@ -1,7 +1,7 @@ ''' python-lambda-local: Main module -Copyright 2015-2017 HDE, Inc. +Copyright 2015-2018 HDE, Inc. Licensed under MIT. ''' diff --git a/lambda_local/context.py b/lambda_local/context.py index 4be412e..d1288b4 100644 --- a/lambda_local/context.py +++ b/lambda_local/context.py @@ -1,5 +1,5 @@ ''' -Copyright 2015-2017 HDE, Inc. +Copyright 2015-2018 HDE, Inc. Licensed under MIT. ''' diff --git a/lambda_local/environment_variables.py b/lambda_local/environment_variables.py index b518309..ea102bd 100644 --- a/lambda_local/environment_variables.py +++ b/lambda_local/environment_variables.py @@ -2,6 +2,11 @@ import os +def export_variables(environment_variables): + for env_name, env_value in environment_variables.items(): + os.environ[str(env_name)] = str(env_value) + + def set_environment_variables(json_file_path): """ Read and set environment variables from a flat json file. @@ -25,5 +30,4 @@ def set_environment_variables(json_file_path): with open(json_file_path) as json_file: env_vars = json.loads(json_file.read()) - for env_name, env_value in env_vars.items(): - os.environ[str(env_name)] = str(env_value) + export_variables(env_vars) diff --git a/lambda_local/event.py b/lambda_local/event.py index 3030ff1..3d0f23f 100644 --- a/lambda_local/event.py +++ b/lambda_local/event.py @@ -1,5 +1,5 @@ ''' -Copyright 2015-2017 HDE, Inc. +Copyright 2015-2018 HDE, Inc. Licensed under MIT. ''' diff --git a/lambda_local/main.py b/lambda_local/main.py index 95d4358..9d97e23 100644 --- a/lambda_local/main.py +++ b/lambda_local/main.py @@ -1,5 +1,5 @@ ''' -Copyright 2015-2017 HDE, Inc. +Copyright 2015-2018 HDE, Inc. Licensed under MIT. ''' @@ -15,7 +15,7 @@ from . import event from . import context -from .environment_variables import set_environment_variables +from .environment_variables import set_environment_variables, export_variables from .timeout import time_limit from .timeout import TimeoutException @@ -31,6 +31,17 @@ EXITCODE_ERR = 1 +def call(func, event, timeout, environment_variables={}, arn_string="", version_name="", library=None): + export_variables(environment_variables) + e = json.loads(event) + c = context.Context(timeout, arn_string, version_name) + if library is not None: + load_lib(library) + request_id = uuid.uuid4() + + return _runner(request_id, e, c, func) + + def run(args): # set env vars if path to json file was given set_environment_variables(args.environment_variables) @@ -41,16 +52,23 @@ def run(args): load_lib(args.library) request_id = uuid.uuid4() func = load(request_id, args.file, args.function) + + (result, err_type) = _runner(request_id, e, c, func) + + if err_type is not None: + sys.exit(EXITCODE_ERR) + +def _runner(request_id, event, context, func): logger = logging.getLogger() result = None - logger.info("Event: {}".format(e)) + logger.info("Event: {}".format(event)) logger.info("START RequestId: {}".format(request_id)) start_time = timeit.default_timer() - result, err_type = execute(func, e, c) + result, err_type = execute(func, event, context) end_time = timeit.default_timer() logger.info("END RequestId: {}".format(request_id)) @@ -64,8 +82,7 @@ def run(args): logger.info("REPORT RequestId: {}\tDuration: {}".format( request_id, duration)) - if err_type is not None: - sys.exit(EXITCODE_ERR) + return (result, err_type) def load_lib(path): diff --git a/lambda_local/timeout.py b/lambda_local/timeout.py index 17646c3..9d0354a 100644 --- a/lambda_local/timeout.py +++ b/lambda_local/timeout.py @@ -1,5 +1,5 @@ ''' -Copyright 2015-2017 HDE, Inc. +Copyright 2015-2018 HDE, Inc. Licensed under MIT. ''' diff --git a/setup.py b/setup.py index 595797c..e7a029e 100644 --- a/setup.py +++ b/setup.py @@ -1,9 +1,10 @@ ''' python-lambda-local: Run lambda function in python on local machine. -Copyright 2015-2017 HDE, Inc. +Copyright 2015-2018 HDE, Inc. Licensed under MIT. ''' +import io import sys from setuptools import setup, find_packages from setuptools.command.test import test as TestCommand @@ -22,12 +23,12 @@ def run_tests(self): sys.exit(pytest.main(self.test_args)) -version = "0.1.6" +version = "0.1.7" setup(name="python-lambda-local", version=version, description="Run lambda function in python on local machine.", - long_description=open("README.rst").read(), + long_description=io.open("README.rst", encoding="utf-8").read(), classifiers=[ 'Development Status :: 3 - Alpha', 'Operating System :: POSIX', diff --git a/tests/test_direct_invocations.py b/tests/test_direct_invocations.py new file mode 100644 index 0000000..d952981 --- /dev/null +++ b/tests/test_direct_invocations.py @@ -0,0 +1,52 @@ +''' +python-lambda-local: Test Direct Inovactions +(command-line and direct). + +Meant for use with py.test. + +Copyright 2015 HDE, Inc. +Licensed under MIT +''' +import json +import argparse +from multiprocessing import Process +import os +from lambda_local.main import run as lambda_run +from lambda_local.main import call as lambda_call + + +def my_lambda_function(event, context): + print("Hello World from My Lambda Function!") + return 42 + +def test_function_call_for_pytest(): + request = json.dumps({}) + (result, error_type) = lambda_call(func=my_lambda_function, event=request, timeout=1) + + assert error_type is None + + assert result == 42 + + +def test_check_command_line(): + request = json.dumps({}) + request_file = 'check_command_line_event.json' + with open (request_file, "w") as f: + f.write(request) + + args = argparse.Namespace(event=request_file, + file='tests/test_direct_invocations.py', + function='my_lambda_function', + timeout=1, + environment_variables='', + library=None, + version_name='', + arn_string='' + ) + p = Process(target=lambda_run, args=(args,)) + p.start() + p.join() + + os.remove(request_file) + assert p.exitcode == 0 + \ No newline at end of file diff --git a/wercker.yml b/wercker.yml index 25613d5..7d3c6bf 100644 --- a/wercker.yml +++ b/wercker.yml @@ -26,6 +26,11 @@ build-py2: code: | python setup.py sdist bdist_wheel + - script: + name: test + code: | + python setup.py test + build-py3: box: python:3.6-slim steps: @@ -49,6 +54,11 @@ build-py3: code: | python setup.py sdist bdist_wheel + - script: + name: test + code: | + python setup.py test + deploy: pypi: - script: