Skip to content

Commit 6351703

Browse files
committed
Add lambda test
Signed-off-by: Yves Bastide <[email protected]>
1 parent 366bd20 commit 6351703

File tree

7 files changed

+155
-8
lines changed

7 files changed

+155
-8
lines changed

examples/lambda.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,22 @@
1-
from simpleflow import Workflow, futures
1+
from simpleflow import Workflow
22
from simpleflow.lambda_function import LambdaFunction
33
from simpleflow.swf.task import LambdaFunctionTask
44

5+
"""
6+
The lambda function is:
7+
8+
from __future__ import print_function
9+
10+
import json
11+
12+
print('Loading function')
13+
14+
15+
def lambda_handler(event, context):
16+
print("Received event: " + json.dumps(event, indent=2))
17+
return 42
18+
"""
19+
520

621
class LambdaWorkflow(Workflow):
722
name = 'basic'
@@ -20,4 +35,4 @@ def run(self):
2035
foo='bar',
2136
)
2237
)
23-
print(futures.wait(future))
38+
print(future.result)

simpleflow/lambda_function.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ class LambdaFunction(Submittable):
55
def __init__(self,
66
name,
77
start_to_close_timeout=None,
8-
idempotent=None):
8+
idempotent=None,
9+
is_python_function=True,
10+
):
911
self.name = name
1012
self.start_to_close_timeout = start_to_close_timeout
1113
self.idempotent = idempotent
14+
self.is_python_function = is_python_function

simpleflow/swf/task.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -373,10 +373,13 @@ def name(self):
373373
return self.lambda_function.name
374374

375375
def schedule(self, *args, **kwargs):
376-
input = {
377-
'args': self.args,
378-
'kwargs': self.kwargs,
379-
}
376+
if self.lambda_function.is_python_function:
377+
input = {
378+
'args': self.args,
379+
'kwargs': self.kwargs,
380+
}
381+
else:
382+
input = self.kwargs or self.args
380383

381384
decision = swf.models.decision.LambdaFunctionDecision(
382385
'schedule',
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
interactions:
2+
- request:
3+
body: '{"domain": "TestDomain", "workflowType": {"name": "basic", "version": "example"}}'
4+
headers:
5+
Authorization: ['AWS4-HMAC-SHA256 Credential=1234AB/20160823/us-east-1/swf/aws4_request,SignedHeaders=host;x-amz-date;x-amz-target,Signature=foobar']
6+
Content-Encoding: [amz-1.0]
7+
Content-Length: ['81']
8+
Content-Type: [application/json; charset=UTF-8]
9+
Host: [swf.us-east-1.amazonaws.com]
10+
User-Agent: [Boto/2.47.0 Python/3.6.1 Linux/4.10.0-21-generic]
11+
X-Amz-Date: [20170610T183557Z]
12+
X-Amz-Target: [com.amazonaws.swf.service.model.SimpleWorkflowService.DescribeWorkflowType]
13+
method: POST
14+
uri: https://swf.us-east-1.amazonaws.com/
15+
response:
16+
body: {string: '{"configuration":{"defaultChildPolicy":"TERMINATE","defaultExecutionStartToCloseTimeout":"300","defaultTaskList":{"name":"None"},"defaultTaskStartToCloseTimeout":"300"},"typeInfo":{"creationDate":1.488630704586E9,"status":"REGISTERED","workflowType":{"name":"basic","version":"example"}}}'}
17+
headers:
18+
Content-Length: ['288']
19+
Content-Type: [application/json]
20+
x-amzn-RequestId: [a4ff48f5-4e0b-11e7-a675-552ffed790c9]
21+
status: {code: 200, message: OK}
22+
- request:
23+
body: '{"domain": "TestDomain", "workflowId": "test-simpleflow-workflow", "workflowType":
24+
{"name": "basic", "version": "example"}, "taskList": {"name": "test-simpleflow-workflow-7d4d6b0513764d3ba38c60c4061483ba"},
25+
"childPolicy": "TERMINATE", "input": "{\"args\":[],\"kwargs\":{}}", "lambdaRole":
26+
"arn:aws:iam::111111000000:role/swf-lambda"}'
27+
headers:
28+
Authorization: ['AWS4-HMAC-SHA256 Credential=1234AB/20160823/us-east-1/swf/aws4_request,SignedHeaders=host;x-amz-date;x-amz-target,Signature=foobar']
29+
Content-Encoding: [amz-1.0]
30+
Content-Length: ['333']
31+
Content-Type: [application/json; charset=UTF-8]
32+
Host: [swf.us-east-1.amazonaws.com]
33+
User-Agent: [Boto/2.47.0 Python/3.6.1 Linux/4.10.0-21-generic]
34+
X-Amz-Date: [20170610T183557Z]
35+
X-Amz-Target: [com.amazonaws.swf.service.model.SimpleWorkflowService.StartWorkflowExecution]
36+
method: POST
37+
uri: https://swf.us-east-1.amazonaws.com/
38+
response:
39+
body: {string: '{"runId":"22Yr7TxK3TwETHtk/p2S2dWiEyzwD8e9bjDaOKtBXy1wE="}'}
40+
headers:
41+
Content-Length: ['58']
42+
Content-Type: [application/json]
43+
x-amzn-RequestId: [a54bbb7e-4e0b-11e7-818d-3f4a87b50f20]
44+
status: {code: 200, message: OK}
45+
- request:
46+
body: '{"domain": "TestDomain", "execution": {"runId": "22Yr7TxK3TwETHtk/p2S2dWiEyzwD8e9bjDaOKtBXy1wE=",
47+
"workflowId": "test-simpleflow-workflow"}}'
48+
headers:
49+
Authorization: ['AWS4-HMAC-SHA256 Credential=1234AB/20160823/us-east-1/swf/aws4_request,SignedHeaders=host;x-amz-date;x-amz-target,Signature=foobar']
50+
Content-Encoding: [amz-1.0]
51+
Content-Length: ['140']
52+
Content-Type: [application/json; charset=UTF-8]
53+
Host: [swf.us-east-1.amazonaws.com]
54+
User-Agent: [Boto/2.47.0 Python/3.6.1 Linux/4.10.0-21-generic]
55+
X-Amz-Date: [20170610T183600Z]
56+
X-Amz-Target: [com.amazonaws.swf.service.model.SimpleWorkflowService.DescribeWorkflowExecution]
57+
method: POST
58+
uri: https://swf.us-east-1.amazonaws.com/
59+
response:
60+
body: {string: '{"executionConfiguration":{"childPolicy":"TERMINATE","executionStartToCloseTimeout":"300","lambdaRole":"arn:aws:iam::111111000000:role/swf-lambda","taskList":{"name":"test-simpleflow-workflow-7d4d6b0513764d3ba38c60c4061483ba"},"taskStartToCloseTimeout":"300"},"executionInfo":{"cancelRequested":false,"closeStatus":"COMPLETED","closeTimestamp":1.497119759876E9,"execution":{"runId":"22Yr7TxK3TwETHtk/p2S2dWiEyzwD8e9bjDaOKtBXy1wE=","workflowId":"test-simpleflow-workflow"},"executionStatus":"CLOSED","startTimestamp":1.497119758313E9,"workflowType":{"name":"basic","version":"example"}},"openCounts":{"openActivityTasks":0,"openChildWorkflowExecutions":0,"openDecisionTasks":0,"openLambdaFunctions":0,"openTimers":0}}'}
61+
headers:
62+
Content-Length: ['716']
63+
Content-Type: [application/json]
64+
x-amzn-RequestId: [a6d36cb2-4e0b-11e7-8e25-3b3c40344394]
65+
status: {code: 200, message: OK}
66+
- request:
67+
body: '{"domain": "TestDomain", "execution": {"runId": "22Yr7TxK3TwETHtk/p2S2dWiEyzwD8e9bjDaOKtBXy1wE=",
68+
"workflowId": "test-simpleflow-workflow"}}'
69+
headers:
70+
Authorization: ['AWS4-HMAC-SHA256 Credential=1234AB/20160823/us-east-1/swf/aws4_request,SignedHeaders=host;x-amz-date;x-amz-target,Signature=foobar']
71+
Content-Encoding: [amz-1.0]
72+
Content-Length: ['140']
73+
Content-Type: [application/json; charset=UTF-8]
74+
Host: [swf.us-east-1.amazonaws.com]
75+
User-Agent: [Boto/2.47.0 Python/3.6.1 Linux/4.10.0-21-generic]
76+
X-Amz-Date: [20170610T183701Z]
77+
X-Amz-Target: [com.amazonaws.swf.service.model.SimpleWorkflowService.GetWorkflowExecutionHistory]
78+
method: POST
79+
uri: https://swf.us-east-1.amazonaws.com/
80+
response:
81+
body: {string: '{"events":[{"eventId":1,"eventTimestamp":1.497119758313E9,"eventType":"WorkflowExecutionStarted","workflowExecutionStartedEventAttributes":{"childPolicy":"TERMINATE","executionStartToCloseTimeout":"300","input":"{\"args\":[],\"kwargs\":{}}","lambdaRole":"arn:aws:iam::111111000000:role/swf-lambda","parentInitiatedEventId":0,"taskList":{"name":"test-simpleflow-workflow-7d4d6b0513764d3ba38c60c4061483ba"},"taskStartToCloseTimeout":"300","workflowType":{"name":"basic","version":"example"}}},{"decisionTaskScheduledEventAttributes":{"startToCloseTimeout":"300","taskList":{"name":"test-simpleflow-workflow-7d4d6b0513764d3ba38c60c4061483ba"}},"eventId":2,"eventTimestamp":1.497119758313E9,"eventType":"DecisionTaskScheduled"},{"decisionTaskStartedEventAttributes":{"identity":"{\"hostname\":\"zeb-P5Q-E\",\"pid\":6614,\"user\":\"zeb\"}","scheduledEventId":2},"eventId":3,"eventTimestamp":1.497119758358E9,"eventType":"DecisionTaskStarted"},{"decisionTaskCompletedEventAttributes":{"scheduledEventId":2,"startedEventId":3},"eventId":4,"eventTimestamp":1.497119758915E9,"eventType":"DecisionTaskCompleted"},{"eventId":5,"eventTimestamp":1.497119758915E9,"eventType":"LambdaFunctionScheduled","lambdaFunctionScheduledEventAttributes":{"decisionTaskCompletedEventId":4,"id":"hello-world-python-5ced6d5d9fae58fd0a7eb0d3908cabed","input":"{\"args\":[8],\"kwargs\":{\"foo\":\"bar\"}}","name":"hello-world-python"}},{"eventId":6,"eventTimestamp":1.497119758941E9,"eventType":"LambdaFunctionStarted","lambdaFunctionStartedEventAttributes":{"scheduledEventId":5}},{"eventId":7,"eventTimestamp":1.497119759328E9,"eventType":"LambdaFunctionCompleted","lambdaFunctionCompletedEventAttributes":{"result":"42","scheduledEventId":5,"startedEventId":6}},{"decisionTaskScheduledEventAttributes":{"startToCloseTimeout":"300","taskList":{"name":"test-simpleflow-workflow-7d4d6b0513764d3ba38c60c4061483ba"}},"eventId":8,"eventTimestamp":1.497119759328E9,"eventType":"DecisionTaskScheduled"},{"decisionTaskStartedEventAttributes":{"identity":"{\"hostname\":\"zeb-P5Q-E\",\"pid\":6613,\"user\":\"zeb\"}","scheduledEventId":8},"eventId":9,"eventTimestamp":1.497119759353E9,"eventType":"DecisionTaskStarted"},{"decisionTaskCompletedEventAttributes":{"scheduledEventId":8,"startedEventId":9},"eventId":10,"eventTimestamp":1.497119759876E9,"eventType":"DecisionTaskCompleted"},{"eventId":11,"eventTimestamp":1.497119759876E9,"eventType":"WorkflowExecutionCompleted","workflowExecutionCompletedEventAttributes":{"decisionTaskCompletedEventId":10,"result":"null"}}]}'}
82+
headers:
83+
Content-Length: ['2535']
84+
Content-Type: [application/json]
85+
x-amzn-RequestId: [cb1ece9f-4e0b-11e7-8c8a-29c4fe87cb8e]
86+
status: {code: 200, message: OK}
87+
version: 1

tests/integration/test_lambda.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from sure import expect
2+
3+
from tests.integration import VCRIntegrationTest, vcr
4+
5+
6+
class TestLambda(VCRIntegrationTest):
7+
@vcr.use_cassette
8+
def test_lambda(self):
9+
events = self.run_standalone('tests.integration.workflow.LambdaWorkflow')
10+
expect(len(events)).should.equal(11)
11+
expect(events[0]['workflowExecutionStartedEventAttributes']).should.have.key('lambdaRole')
12+
expect(events[4]['eventType']).should.equal('LambdaFunctionScheduled')
13+
expect(events[4]['lambdaFunctionScheduledEventAttributes']['name']).should.equal('hello-world-python')
14+
expect(events[5]['eventType']).should.equal('LambdaFunctionStarted')
15+
expect(events[6]['eventType']).should.equal('LambdaFunctionCompleted')
16+
expect(events[6]['lambdaFunctionCompletedEventAttributes']['result']).should.equal('42')
17+
print(events)

tests/integration/test_task_list.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ def test_not_standalone(self):
5656
input='[]',
5757
input_file=None,
5858
local=False,
59+
lambda_role=None,
5960
)
6061
while True:
6162
time.sleep(1)

tests/integration/workflow.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
from simpleflow.canvas import Chain, Group
1111
from simpleflow.constants import HOUR, MINUTE
1212
from simpleflow.swf.utils import get_workflow_execution
13+
from simpleflow.lambda_function import LambdaFunction
14+
from simpleflow.swf.task import LambdaFunctionTask
1315
from simpleflow.task import ActivityTask
1416

1517

@@ -154,6 +156,26 @@ def run(self):
154156
return future.result
155157

156158

159+
class LambdaWorkflow(Workflow):
160+
name = 'basic'
161+
version = 'example'
162+
task_list = 'example'
163+
lambda_role = 'arn:aws:iam::111111000000:role/swf-lambda'
164+
165+
def run(self):
166+
future = self.submit(
167+
LambdaFunctionTask(
168+
LambdaFunction(
169+
'hello-world-python',
170+
idempotent=True,
171+
),
172+
8,
173+
foo='bar',
174+
)
175+
)
176+
print(future.result)
177+
178+
157179
class TimerWorkflow(Workflow):
158180
name = 'example'
159181
version = 'example'
@@ -190,7 +212,6 @@ def run(self):
190212
return future.result
191213

192214

193-
194215
class WorkflowToCancel(Workflow):
195216
name = 'example'
196217
version = 'example'

0 commit comments

Comments
 (0)