@@ -72,20 +72,34 @@ def tagged_name(self, name, architecture):
7272
7373 def get_tag (self , architecture ):
7474 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 )
7594
7695 @parameterized .expand ([("x86_64" , "8000" ), ("arm64" , "9000" ), ("" , "9050" )])
7796 def test_env_var_with_equal_sign (self , arch , port ):
7897 image , rie , image_name = self .tagged_name ("envvarcheck" , arch )
7998
8099 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+
89103 self .assertEqual (b'"4=4"' , r .content )
90104
91105 @parameterized .expand ([("x86_64" , "8001" ), ("arm64" , "9001" ), ("" , "9051" )])
@@ -94,20 +108,13 @@ def test_two_invokes(self, arch, port):
94108
95109 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"
96110
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+
105113 self .assertEqual (b'"My lambda ran succesfully"' , r .content )
106114
107115 # 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+
111118 self .assertEqual (b'"My lambda ran succesfully"' , r .content )
112119
113120 @parameterized .expand ([("x86_64" , "8002" ), ("arm64" , "9002" ), ("" , "9052" )])
@@ -116,29 +123,18 @@ def test_lambda_function_arn_exists(self, arch, port):
116123
117124 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"
118125
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+
127128 self .assertEqual (b'"My lambda ran succesfully"' , r .content )
128129
129130 @parameterized .expand ([("x86_64" , "8003" ), ("arm64" , "9003" ), ("" , "9053" )])
130131 def test_lambda_function_arn_exists_with_defining_custom_name (self , arch , port ):
131132 image , rie , image_name = self .tagged_name ("customname" , arch )
132133
133134 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+
142138 self .assertEqual (b'"My lambda ran succesfully"' , r .content )
143139
144140 @parameterized .expand ([("x86_64" , "8004" ), ("arm64" , "9004" ), ("" , "9054" )])
@@ -147,14 +143,8 @@ def test_timeout_invoke(self, arch, port):
147143
148144 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"
149145
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+
158148 self .assertEqual (b"Task timed out after 1.00 seconds" , r .content )
159149
160150 @parameterized .expand ([("x86_64" , "8005" ), ("arm64" , "9005" ), ("" , "9055" )])
@@ -163,14 +153,8 @@ def test_exception_returned(self, arch, port):
163153
164154 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"
165155
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+
174158 self .assertEqual (
175159 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"]}' ,
176160 r .content ,
@@ -182,15 +166,8 @@ def test_context_get_remaining_time_in_three_seconds(self, arch, port):
182166
183167 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"
184168
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+
194171 # Execution time is not decided, 1.0s ~ 3.0s is a good estimation
195172 self .assertLess (int (r .content ), 3000 )
196173 self .assertGreater (int (r .content ), 1000 )
@@ -201,15 +178,8 @@ def test_context_get_remaining_time_in_ten_seconds(self, arch, port):
201178
202179 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"
203180
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+
213183 # Execution time is not decided, 8.0s ~ 10.0s is a good estimation
214184 self .assertLess (int (r .content ), 10000 )
215185 self .assertGreater (int (r .content ), 8000 )
@@ -220,14 +190,7 @@ def test_context_get_remaining_time_in_default_deadline(self, arch, port):
220190
221191 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"
222192
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 )
231194
232195 # Executation time is not decided, 298.0s ~ 300.0s is a good estimation
233196 self .assertLess (int (r .content ), 300000 )
@@ -239,14 +202,8 @@ def test_invoke_with_pre_runtime_api_runtime(self, arch, port):
239202
240203 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"
241204
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+
250207 self .assertEqual (b'"My lambda ran succesfully"' , r .content )
251208
252209 @parameterized .expand ([("x86_64" , "8010" ), ("arm64" , "9010" ), ("" , "9060" )])
@@ -255,14 +212,8 @@ def test_function_name_is_overriden(self, arch, port):
255212
256213 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"
257214
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+
266217 self .assertEqual (b'"My lambda ran succesfully"' , r .content )
267218
268219 @parameterized .expand ([("x86_64" , "8011" ), ("arm64" , "9011" ), ("" , "9061" )])
@@ -272,14 +223,8 @@ def test_port_override(self, arch, port):
272223 # Use port 8081 inside the container instead of 8080
273224 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"
274225
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+
283228 self .assertEqual (b'"My lambda ran succesfully"' , r .content )
284229
285230
0 commit comments