Skip to content

Commit 45ffcdc

Browse files
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.
1 parent f61c0a3 commit 45ffcdc

File tree

2 files changed

+74
-5
lines changed

2 files changed

+74
-5
lines changed

lambda_local/main.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
from . import event
1717
from . import context
18-
from .environment_variables import set_environment_variables
18+
from .environment_variables import set_environment_variables, export_variables
1919
from .timeout import time_limit
2020
from .timeout import TimeoutException
2121

@@ -31,6 +31,17 @@
3131
EXITCODE_ERR = 1
3232

3333

34+
def call(func, event, timeout, environment_variables={}, arn_string="", version_name="", library=None):
35+
export_variables(environment_variables)
36+
e = json.loads(event)
37+
c = context.Context(timeout, arn_string, version_name)
38+
if library is not None:
39+
load_lib(library)
40+
request_id = uuid.uuid4()
41+
42+
return _runner(request_id, e, c, func)
43+
44+
3445
def run(args):
3546
# set env vars if path to json file was given
3647
set_environment_variables(args.environment_variables)
@@ -41,16 +52,23 @@ def run(args):
4152
load_lib(args.library)
4253
request_id = uuid.uuid4()
4354
func = load(request_id, args.file, args.function)
55+
56+
(result, err_type) = _runner(request_id, e, c, func)
57+
58+
if err_type is not None:
59+
sys.exit(EXITCODE_ERR)
60+
4461

62+
def _runner(request_id, event, context, func):
4563
logger = logging.getLogger()
4664
result = None
4765

48-
logger.info("Event: {}".format(e))
66+
logger.info("Event: {}".format(event))
4967

5068
logger.info("START RequestId: {}".format(request_id))
5169

5270
start_time = timeit.default_timer()
53-
result, err_type = execute(func, e, c)
71+
result, err_type = execute(func, event, context)
5472
end_time = timeit.default_timer()
5573

5674
logger.info("END RequestId: {}".format(request_id))
@@ -64,8 +82,7 @@ def run(args):
6482
logger.info("REPORT RequestId: {}\tDuration: {}".format(
6583
request_id, duration))
6684

67-
if err_type is not None:
68-
sys.exit(EXITCODE_ERR)
85+
return (result, err_type)
6986

7087

7188
def load_lib(path):

tests/test_direct_invocations.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
'''
2+
python-lambda-local: Test Direct Inovactions
3+
(command-line and direct).
4+
5+
Meant for use with py.test.
6+
7+
Copyright 2015 HDE, Inc.
8+
Licensed under MIT
9+
'''
10+
import json
11+
import argparse
12+
from multiprocessing import Process
13+
import os
14+
from lambda_local.main import run as lambda_run
15+
from lambda_local.main import call as lambda_call
16+
17+
18+
def my_lambda_function(event, context):
19+
print("Hello World from My Lambda Function!")
20+
return 42
21+
22+
def test_function_call_for_pytest():
23+
request = json.dumps({})
24+
(result, error_type) = lambda_call(func=my_lambda_function, event=request, timeout=1)
25+
26+
assert error_type is None
27+
28+
assert result == 42
29+
30+
31+
def test_check_command_line():
32+
request = json.dumps({})
33+
request_file = 'check_command_line_event.json'
34+
with open (request_file, "w") as f:
35+
f.write(request)
36+
37+
args = argparse.Namespace(event=request_file,
38+
file='tests/test_direct_invocations.py',
39+
function='my_lambda_function',
40+
timeout=1,
41+
environment_variables='',
42+
library=None,
43+
version_name='',
44+
arn_string=''
45+
)
46+
p = Process(target=lambda_run, args=(args,))
47+
p.start()
48+
p.join()
49+
50+
os.remove(request_file)
51+
assert p.exitcode == 0
52+

0 commit comments

Comments
 (0)