From 3b4cca39dcf09f7ba3ec623e6b2524644c962b16 Mon Sep 17 00:00:00 2001 From: Gorav Arora Date: Wed, 3 Oct 2018 21:28:03 -0700 Subject: [PATCH 1/8] Ignore venv directory for virtual environments --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) 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/ From f61c0a33a79fa4670874f4469e7ceb76c644bf4b Mon Sep 17 00:00:00 2001 From: Gorav Arora Date: Wed, 3 Oct 2018 21:28:40 -0700 Subject: [PATCH 2/8] Split the parsing of input and the exporting of the variables for reuse --- lambda_local/environment_variables.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) 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) From 45ffcdc1af877625aabc35ee8a173efdab502166 Mon Sep 17 00:00:00 2001 From: Gorav Arora Date: Wed, 3 Oct 2018 21:31:12 -0700 Subject: [PATCH 3/8] Added the function so that lambda functions can be called from pytests. The return value of the functions is returned so the test can check the output. --- lambda_local/main.py | 27 ++++++++++++++--- tests/test_direct_invocations.py | 52 ++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 5 deletions(-) create mode 100644 tests/test_direct_invocations.py diff --git a/lambda_local/main.py b/lambda_local/main.py index 95d4358..c09146b 100644 --- a/lambda_local/main.py +++ b/lambda_local/main.py @@ -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/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 From 3ac6eec788d798eab95da818290aa2fe7cc30d51 Mon Sep 17 00:00:00 2001 From: YANG Xudong Date: Tue, 23 Oct 2018 11:19:56 +0900 Subject: [PATCH 4/8] Setup encoding. --- setup.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 595797c..57d0c90 100644 --- a/setup.py +++ b/setup.py @@ -4,6 +4,7 @@ Copyright 2015-2017 HDE, Inc. Licensed under MIT. ''' +import io import sys from setuptools import setup, find_packages from setuptools.command.test import test as TestCommand @@ -27,7 +28,7 @@ def run_tests(self): 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', From f4a84f90f95382929659c5502c4894df7413d457 Mon Sep 17 00:00:00 2001 From: Iskandar Setiadi Date: Tue, 23 Oct 2018 11:48:00 +0900 Subject: [PATCH 5/8] Update copyright year and use Python 3.7 --- LICENSE | 2 +- README.md | 2 +- README.rst | 2 +- lambda_local/__init__.py | 2 +- lambda_local/context.py | 2 +- lambda_local/event.py | 2 +- lambda_local/main.py | 2 +- lambda_local/timeout.py | 2 +- setup.py | 4 ++-- wercker.yml | 2 +- 10 files changed, 11 insertions(+), 11 deletions(-) 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..12ba9f9 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.7. ## Installation diff --git a/README.rst b/README.rst index deb21a0..38756b1 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.7. 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/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 c09146b..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. ''' 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 57d0c90..6de3121 100644 --- a/setup.py +++ b/setup.py @@ -1,7 +1,7 @@ ''' 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 @@ -34,7 +34,7 @@ def run_tests(self): 'Operating System :: POSIX', 'Programming Language :: Python', 'Programming Language :: Python :: 2.7', - 'Programming Language :: Python :: 3.6', + 'Programming Language :: Python :: 3.7', 'License :: OSI Approved :: MIT License' ], keywords="AWS Lambda", diff --git a/wercker.yml b/wercker.yml index 25613d5..fde1478 100644 --- a/wercker.yml +++ b/wercker.yml @@ -27,7 +27,7 @@ build-py2: python setup.py sdist bdist_wheel build-py3: - box: python:3.6-slim + box: python:3.7-slim steps: - script: name: virtualenv install From a9693f07c9ddc8862d7f9505d847045f46743b95 Mon Sep 17 00:00:00 2001 From: YANG Xudong Date: Tue, 23 Oct 2018 11:51:33 +0900 Subject: [PATCH 6/8] Bump version. --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 57d0c90..77f04f1 100644 --- a/setup.py +++ b/setup.py @@ -23,7 +23,7 @@ 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, From b92ba32370fe2d3afe57b3f6f821ac90f1af61b1 Mon Sep 17 00:00:00 2001 From: Iskandar Setiadi Date: Tue, 23 Oct 2018 11:53:41 +0900 Subject: [PATCH 7/8] Revert to 3.6 --- README.md | 2 +- README.rst | 2 +- wercker.yml | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 12ba9f9..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.7. +Please use a newly created virtualenv of Python 2.7 or Python 3.6. ## Installation diff --git a/README.rst b/README.rst index 38756b1..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.7. +Please use a newly created virtualenv of Python 2.7 or Python 3.6. Installation ------------ diff --git a/wercker.yml b/wercker.yml index fde1478..25613d5 100644 --- a/wercker.yml +++ b/wercker.yml @@ -27,7 +27,7 @@ build-py2: python setup.py sdist bdist_wheel build-py3: - box: python:3.7-slim + box: python:3.6-slim steps: - script: name: virtualenv install From abf7813742657a248822448cffb04fd7e6f4bb50 Mon Sep 17 00:00:00 2001 From: Iskandar Setiadi Date: Tue, 23 Oct 2018 12:04:26 +0900 Subject: [PATCH 8/8] Try running test --- setup.py | 2 +- wercker.yml | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 6de3121..9dee85f 100644 --- a/setup.py +++ b/setup.py @@ -34,7 +34,7 @@ def run_tests(self): 'Operating System :: POSIX', 'Programming Language :: Python', 'Programming Language :: Python :: 2.7', - 'Programming Language :: Python :: 3.7', + 'Programming Language :: Python :: 3.6', 'License :: OSI Approved :: MIT License' ], keywords="AWS Lambda", 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: