@@ -72,20 +72,34 @@ def tagged_name(self, name, architecture):
72
72
73
73
def get_tag (self , architecture ):
74
74
return "" if architecture == "" else str (f"-{ architecture } " )
75
+
76
+ def run_command (self , cmd ):
77
+ Popen (cmd .split (" " )).communicate ()
78
+
79
+ def sleep_1s (self ):
80
+ time .sleep (SLEEP_TIME )
81
+
82
+ def invoke_function (self , port ):
83
+ return requests .post (
84
+ f"http://localhost:{ port } /2015-03-31/functions/function/invocations" , json = {}
85
+ )
86
+
87
+ def create_container_and_invoke_function (self , cmd , port ):
88
+ self .run_command (cmd )
89
+
90
+ # sleep 1s to give enough time for the endpoint to be up to curl
91
+ self .sleep_1s ()
92
+
93
+ return self .invoke_function (port )
75
94
76
95
@parameterized .expand ([("x86_64" , "8000" ), ("arm64" , "9000" ), ("" , "9050" )])
77
96
def test_env_var_with_equal_sign (self , arch , port ):
78
97
image , rie , image_name = self .tagged_name ("envvarcheck" , arch )
79
98
80
99
cmd = f"docker run --name { image } -d -v { self .path_to_binary } :/local-lambda-runtime-server -p { port } :8080 --entrypoint /local-lambda-runtime-server/{ rie } { image_name } { DEFAULT_1P_ENTRYPOINT } main.check_env_var_handler"
81
- Popen (cmd .split (" " )).communicate ()
82
-
83
- # sleep 1s to give enough time for the endpoint to be up to curl
84
- time .sleep (SLEEP_TIME )
85
-
86
- r = requests .post (
87
- f"http://localhost:{ port } /2015-03-31/functions/function/invocations" , json = {}
88
- )
100
+
101
+ r = self .create_container_and_invoke_function (cmd , port )
102
+
89
103
self .assertEqual (b'"4=4"' , r .content )
90
104
91
105
@parameterized .expand ([("x86_64" , "8001" ), ("arm64" , "9001" ), ("" , "9051" )])
@@ -94,20 +108,13 @@ def test_two_invokes(self, arch, port):
94
108
95
109
cmd = f"docker run --name { image } -d -v { self .path_to_binary } :/local-lambda-runtime-server -p { port } :8080 --entrypoint /local-lambda-runtime-server/{ rie } { image_name } { DEFAULT_1P_ENTRYPOINT } main.success_handler"
96
110
97
- Popen (cmd .split (" " )).communicate ()
98
-
99
- # sleep 1s to give enough time for the endpoint to be up to curl
100
- time .sleep (SLEEP_TIME )
101
-
102
- r = requests .post (
103
- f"http://localhost:{ port } /2015-03-31/functions/function/invocations" , json = {}
104
- )
111
+ r = self .create_container_and_invoke_function (cmd , port )
112
+
105
113
self .assertEqual (b'"My lambda ran succesfully"' , r .content )
106
114
107
115
# Make sure we can invoke the function twice
108
- r = requests .post (
109
- f"http://localhost:{ port } /2015-03-31/functions/function/invocations" , json = {}
110
- )
116
+ r = self .invoke_function (port )
117
+
111
118
self .assertEqual (b'"My lambda ran succesfully"' , r .content )
112
119
113
120
@parameterized .expand ([("x86_64" , "8002" ), ("arm64" , "9002" ), ("" , "9052" )])
@@ -116,29 +123,18 @@ def test_lambda_function_arn_exists(self, arch, port):
116
123
117
124
cmd = f"docker run --name { image } -d -v { self .path_to_binary } :/local-lambda-runtime-server -p { port } :8080 --entrypoint /local-lambda-runtime-server/{ rie } { image_name } { DEFAULT_1P_ENTRYPOINT } main.assert_lambda_arn_in_context"
118
125
119
- Popen (cmd .split (" " )).communicate ()
120
-
121
- # sleep 1s to give enough time for the endpoint to be up to curl
122
- time .sleep (SLEEP_TIME )
123
-
124
- r = requests .post (
125
- f"http://localhost:{ port } /2015-03-31/functions/function/invocations" , json = {}
126
- )
126
+ r = self .create_container_and_invoke_function (cmd , port )
127
+
127
128
self .assertEqual (b'"My lambda ran succesfully"' , r .content )
128
129
129
130
@parameterized .expand ([("x86_64" , "8003" ), ("arm64" , "9003" ), ("" , "9053" )])
130
131
def test_lambda_function_arn_exists_with_defining_custom_name (self , arch , port ):
131
132
image , rie , image_name = self .tagged_name ("customname" , arch )
132
133
133
134
cmd = f"docker run --name { image } --env AWS_LAMBDA_FUNCTION_NAME=MyCoolName -d -v { self .path_to_binary } :/local-lambda-runtime-server -p { port } :8080 --entrypoint /local-lambda-runtime-server/{ rie } { image_name } { DEFAULT_1P_ENTRYPOINT } main.assert_lambda_arn_in_context"
134
- Popen (cmd .split (" " )).communicate ()
135
-
136
- # sleep 1s to give enough time for the endpoint to be up to curl
137
- time .sleep (SLEEP_TIME )
138
-
139
- r = requests .post (
140
- f"http://localhost:{ port } /2015-03-31/functions/function/invocations" , json = {}
141
- )
135
+
136
+ r = self .create_container_and_invoke_function (cmd , port )
137
+
142
138
self .assertEqual (b'"My lambda ran succesfully"' , r .content )
143
139
144
140
@parameterized .expand ([("x86_64" , "8004" ), ("arm64" , "9004" ), ("" , "9054" )])
@@ -147,14 +143,8 @@ def test_timeout_invoke(self, arch, port):
147
143
148
144
cmd = f"docker run --name { image } -d --env AWS_LAMBDA_FUNCTION_TIMEOUT=1 -v { self .path_to_binary } :/local-lambda-runtime-server -p { port } :8080 --entrypoint /local-lambda-runtime-server/{ rie } { image_name } { DEFAULT_1P_ENTRYPOINT } main.sleep_handler"
149
145
150
- Popen (cmd .split (" " )).communicate ()
151
-
152
- # sleep 1s to give enough time for the endpoint to be up to curl
153
- time .sleep (SLEEP_TIME )
154
-
155
- r = requests .post (
156
- f"http://localhost:{ port } /2015-03-31/functions/function/invocations" , json = {}
157
- )
146
+ r = self .create_container_and_invoke_function (cmd , port )
147
+
158
148
self .assertEqual (b"Task timed out after 1.00 seconds" , r .content )
159
149
160
150
@parameterized .expand ([("x86_64" , "8005" ), ("arm64" , "9005" ), ("" , "9055" )])
@@ -163,14 +153,8 @@ def test_exception_returned(self, arch, port):
163
153
164
154
cmd = f"docker run --name { image } -d -v { self .path_to_binary } :/local-lambda-runtime-server -p { port } :8080 --entrypoint /local-lambda-runtime-server/{ rie } { image_name } { DEFAULT_1P_ENTRYPOINT } main.exception_handler"
165
155
166
- Popen (cmd .split (" " )).communicate ()
167
-
168
- # sleep 1s to give enough time for the endpoint to be up to curl
169
- time .sleep (SLEEP_TIME )
170
-
171
- r = requests .post (
172
- f"http://localhost:{ port } /2015-03-31/functions/function/invocations" , json = {}
173
- )
156
+ r = self .create_container_and_invoke_function (cmd , port )
157
+
174
158
self .assertEqual (
175
159
b'{"errorMessage": "Raising an exception", "errorType": "Exception", "stackTrace": [" File \\ "/var/task/main.py\\ ", line 13, in exception_handler\\ n raise Exception(\\ "Raising an exception\\ ")\\ n"]}' ,
176
160
r .content ,
@@ -182,15 +166,8 @@ def test_context_get_remaining_time_in_three_seconds(self, arch, port):
182
166
183
167
cmd = f"docker run --name { image } -d --env AWS_LAMBDA_FUNCTION_TIMEOUT=3 -v { self .path_to_binary } :/local-lambda-runtime-server -p { port } :8080 --entrypoint /local-lambda-runtime-server/{ rie } { image_name } { DEFAULT_1P_ENTRYPOINT } main.check_remaining_time_handler"
184
168
185
- Popen (cmd .split (' ' )).communicate ()
186
-
187
- # sleep 1s to give enough time for the endpoint to be up to curl
188
- time .sleep (SLEEP_TIME )
189
-
190
- r = requests .post (
191
- f"http://localhost:{ port } /2015-03-31/functions/function/invocations" , json = {}
192
- )
193
-
169
+ r = self .create_container_and_invoke_function (cmd , port )
170
+
194
171
# Execution time is not decided, 1.0s ~ 3.0s is a good estimation
195
172
self .assertLess (int (r .content ), 3000 )
196
173
self .assertGreater (int (r .content ), 1000 )
@@ -201,15 +178,8 @@ def test_context_get_remaining_time_in_ten_seconds(self, arch, port):
201
178
202
179
cmd = f"docker run --name { image } -d --env AWS_LAMBDA_FUNCTION_TIMEOUT=10 -v { self .path_to_binary } :/local-lambda-runtime-server -p { port } :8080 --entrypoint /local-lambda-runtime-server/{ rie } { image_name } { DEFAULT_1P_ENTRYPOINT } main.check_remaining_time_handler"
203
180
204
- Popen (cmd .split (' ' )).communicate ()
205
-
206
- # sleep 1s to give enough time for the endpoint to be up to curl
207
- time .sleep (SLEEP_TIME )
208
-
209
- r = requests .post (
210
- f"http://localhost:{ port } /2015-03-31/functions/function/invocations" , json = {}
211
- )
212
-
181
+ r = self .create_container_and_invoke_function (cmd , port )
182
+
213
183
# Execution time is not decided, 8.0s ~ 10.0s is a good estimation
214
184
self .assertLess (int (r .content ), 10000 )
215
185
self .assertGreater (int (r .content ), 8000 )
@@ -220,14 +190,7 @@ def test_context_get_remaining_time_in_default_deadline(self, arch, port):
220
190
221
191
cmd = f"docker run --name { image } -d -v { self .path_to_binary } :/local-lambda-runtime-server -p { port } :8080 --entrypoint /local-lambda-runtime-server/{ rie } { image_name } { DEFAULT_1P_ENTRYPOINT } main.check_remaining_time_handler"
222
192
223
- Popen (cmd .split (' ' )).communicate ()
224
-
225
- # sleep 1s to give enough time for the endpoint to be up to curl
226
- time .sleep (SLEEP_TIME )
227
-
228
- r = requests .post (
229
- f"http://localhost:{ port } /2015-03-31/functions/function/invocations" , json = {}
230
- )
193
+ r = self .create_container_and_invoke_function (cmd , port )
231
194
232
195
# Executation time is not decided, 298.0s ~ 300.0s is a good estimation
233
196
self .assertLess (int (r .content ), 300000 )
@@ -239,14 +202,8 @@ def test_invoke_with_pre_runtime_api_runtime(self, arch, port):
239
202
240
203
cmd = f"docker run --name { image } -d -v { self .path_to_binary } :/local-lambda-runtime-server -p { port } :8080 --entrypoint /local-lambda-runtime-server/{ rie } { image_name } { DEFAULT_1P_ENTRYPOINT } main.success_handler"
241
204
242
- Popen (cmd .split (" " )).communicate ()
243
-
244
- # sleep 1s to give enough time for the endpoint to be up to curl
245
- time .sleep (SLEEP_TIME )
246
-
247
- r = requests .post (
248
- f"http://localhost:{ port } /2015-03-31/functions/function/invocations" , json = {}
249
- )
205
+ r = self .create_container_and_invoke_function (cmd , port )
206
+
250
207
self .assertEqual (b'"My lambda ran succesfully"' , r .content )
251
208
252
209
@parameterized .expand ([("x86_64" , "8010" ), ("arm64" , "9010" ), ("" , "9060" )])
@@ -255,14 +212,8 @@ def test_function_name_is_overriden(self, arch, port):
255
212
256
213
cmd = f"docker run --name { image } -d --env AWS_LAMBDA_FUNCTION_NAME=MyCoolName -v { self .path_to_binary } :/local-lambda-runtime-server -p { port } :8080 --entrypoint /local-lambda-runtime-server/{ rie } { image_name } { DEFAULT_1P_ENTRYPOINT } main.assert_env_var_is_overwritten"
257
214
258
- Popen (cmd .split (" " )).communicate ()
259
-
260
- # sleep 1s to give enough time for the endpoint to be up to curl
261
- time .sleep (SLEEP_TIME )
262
-
263
- r = requests .post (
264
- f"http://localhost:{ port } /2015-03-31/functions/function/invocations" , json = {}
265
- )
215
+ r = self .create_container_and_invoke_function (cmd , port )
216
+
266
217
self .assertEqual (b'"My lambda ran succesfully"' , r .content )
267
218
268
219
@parameterized .expand ([("x86_64" , "8011" ), ("arm64" , "9011" ), ("" , "9061" )])
@@ -272,14 +223,8 @@ def test_port_override(self, arch, port):
272
223
# Use port 8081 inside the container instead of 8080
273
224
cmd = f"docker run --name { image } -d -v { self .path_to_binary } :/local-lambda-runtime-server -p { port } :8081 --entrypoint /local-lambda-runtime-server/{ rie } { image_name } { DEFAULT_1P_ENTRYPOINT } main.success_handler --runtime-interface-emulator-address 0.0.0.0:8081"
274
225
275
- Popen (cmd .split (" " )).communicate ()
276
-
277
- # sleep 1s to give enough time for the endpoint to be up to curl
278
- time .sleep (SLEEP_TIME )
279
-
280
- r = requests .post (
281
- f"http://localhost:{ port } /2015-03-31/functions/function/invocations" , json = {}
282
- )
226
+ r = self .create_container_and_invoke_function (cmd , port )
227
+
283
228
self .assertEqual (b'"My lambda ran succesfully"' , r .content )
284
229
285
230
0 commit comments