Skip to content

Commit 00d793b

Browse files
authored
fix: Add a fake arn to Context (#23)
* fix: Add a fake arn to Context * Fix DEFUALT_1P_ENTRYPOINT to DEFAULT_1P_ENTRYPOINT in tests Co-authored-by: Jacob Fuss <[email protected]>
1 parent 2c9ea44 commit 00d793b

File tree

3 files changed

+41
-15
lines changed

3 files changed

+41
-15
lines changed

cmd/aws-lambda-rie/handlers.go

+6-8
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,12 @@ func InvokeHandler(w http.ResponseWriter, r *http.Request, sandbox Sandbox) {
9494

9595
invokeStart := time.Now()
9696
invokePayload := &interop.Invoke{
97-
ID: uuid.New().String(),
98-
TraceID: r.Header.Get("X-Amzn-Trace-Id"),
99-
LambdaSegmentID: r.Header.Get("X-Amzn-Segment-Id"),
100-
Payload: bodyBytes,
101-
CorrelationID: "invokeCorrelationID",
97+
ID: uuid.New().String(),
98+
InvokedFunctionArn: fmt.Sprintf("arn:aws:lambda:us-east-1:012345678912:function:%s", GetenvWithDefault("AWS_LAMBDA_FUNCTION_NAME", "test_function")),
99+
TraceID: r.Header.Get("X-Amzn-Trace-Id"),
100+
LambdaSegmentID: r.Header.Get("X-Amzn-Segment-Id"),
101+
Payload: bodyBytes,
102+
CorrelationID: "invokeCorrelationID",
102103
}
103104
fmt.Println("START RequestId: " + invokePayload.ID + " Version: " + functionVersion)
104105

@@ -176,7 +177,6 @@ func InitHandler(sandbox Sandbox, functionVersion string, timeout int64) (time.T
176177
additionalFunctionEnvironmentVariables["AWS_LAMBDA_FUNCTION_MEMORY_SIZE"] = "3008"
177178
additionalFunctionEnvironmentVariables["AWS_LAMBDA_FUNCTION_NAME"] = "test_function"
178179

179-
180180
// Forward Env Vars from the running system (container) to what the function can view. Without this, Env Vars will
181181
// not be viewable when the function runs.
182182
for _, env := range os.Environ() {
@@ -185,8 +185,6 @@ func InitHandler(sandbox Sandbox, functionVersion string, timeout int64) (time.T
185185
additionalFunctionEnvironmentVariables[envVar[0]] = envVar[1]
186186
}
187187

188-
189-
190188
initStart := time.Now()
191189
// pass to rapid
192190
sandbox.Init(&interop.Init{

test/integration/local_lambda/end-to-end-test.py

+29-7
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import requests
1010

1111
SLEEP_TIME = 2
12-
DEFUALT_1P_ENTRYPOINT = "/lambda-entrypoint.sh"
12+
DEFAULT_1P_ENTRYPOINT = "/lambda-entrypoint.sh"
1313

1414
class TestEndToEnd(TestCase):
1515

@@ -36,7 +36,7 @@ def tearDownClass(cls):
3636

3737

3838
def test_env_var_with_eqaul_sign(self):
39-
cmd = f"docker run --name envvarcheck -d -v {self.path_to_binary}:/local-lambda-runtime-server -p 9003:8080 --entrypoint /local-lambda-runtime-server/aws-lambda-rie {self.image_name} {DEFUALT_1P_ENTRYPOINT} main.check_env_var_handler"
39+
cmd = f"docker run --name envvarcheck -d -v {self.path_to_binary}:/local-lambda-runtime-server -p 9003:8080 --entrypoint /local-lambda-runtime-server/aws-lambda-rie {self.image_name} {DEFAULT_1P_ENTRYPOINT} main.check_env_var_handler"
4040

4141
Popen(cmd.split(' ')).communicate()
4242

@@ -47,7 +47,7 @@ def test_env_var_with_eqaul_sign(self):
4747
self.assertEqual(b'"4=4"', r.content)
4848

4949
def test_two_invokes(self):
50-
cmd = f"docker run --name testing -d -v {self.path_to_binary}:/local-lambda-runtime-server -p 9000:8080 --entrypoint /local-lambda-runtime-server/aws-lambda-rie {self.image_name} {DEFUALT_1P_ENTRYPOINT} main.success_handler"
50+
cmd = f"docker run --name testing -d -v {self.path_to_binary}:/local-lambda-runtime-server -p 9000:8080 --entrypoint /local-lambda-runtime-server/aws-lambda-rie {self.image_name} {DEFAULT_1P_ENTRYPOINT} main.success_handler"
5151

5252
Popen(cmd.split(' ')).communicate()
5353

@@ -61,9 +61,31 @@ def test_two_invokes(self):
6161
r = requests.post("http://localhost:9000/2015-03-31/functions/function/invocations", json={})
6262
self.assertEqual(b'"My lambda ran succesfully"', r.content)
6363

64+
def test_lambda_function_arn_exists(self):
65+
cmd = f"docker run --name testing -d -v {self.path_to_binary}:/local-lambda-runtime-server -p 9000:8080 --entrypoint /local-lambda-runtime-server/aws-lambda-rie {self.image_name} {DEFAULT_1P_ENTRYPOINT} main.assert_lambda_arn_in_context"
66+
67+
Popen(cmd.split(' ')).communicate()
68+
69+
# sleep 1s to give enough time for the endpoint to be up to curl
70+
time.sleep(SLEEP_TIME)
71+
72+
r = requests.post("http://localhost:9000/2015-03-31/functions/function/invocations", json={})
73+
self.assertEqual(b'"My lambda ran succesfully"', r.content)
74+
75+
def test_lambda_function_arn_exists_with_defining_custom_name(self):
76+
cmd = f"docker run --name testing --env AWS_LAMBDA_FUNCTION_NAME=MyCoolName -d -v {self.path_to_binary}:/local-lambda-runtime-server -p 9000:8080 --entrypoint /local-lambda-runtime-server/aws-lambda-rie {self.image_name} {DEFAULT_1P_ENTRYPOINT} main.assert_lambda_arn_in_context"
77+
78+
Popen(cmd.split(' ')).communicate()
79+
80+
# sleep 1s to give enough time for the endpoint to be up to curl
81+
time.sleep(SLEEP_TIME)
82+
83+
r = requests.post("http://localhost:9000/2015-03-31/functions/function/invocations", json={})
84+
self.assertEqual(b'"My lambda ran succesfully"', r.content)
85+
6486

6587
def test_timeout_invoke(self):
66-
cmd = f"docker run --name timeout -d --env AWS_LAMBDA_FUNCTION_TIMEOUT=1 -v {self.path_to_binary}:/local-lambda-runtime-server -p 9001:8080 --entrypoint /local-lambda-runtime-server/aws-lambda-rie {self.image_name} {DEFUALT_1P_ENTRYPOINT} main.sleep_handler"
88+
cmd = f"docker run --name timeout -d --env AWS_LAMBDA_FUNCTION_TIMEOUT=1 -v {self.path_to_binary}:/local-lambda-runtime-server -p 9001:8080 --entrypoint /local-lambda-runtime-server/aws-lambda-rie {self.image_name} {DEFAULT_1P_ENTRYPOINT} main.sleep_handler"
6789

6890
Popen(cmd.split(' ')).communicate()
6991

@@ -74,7 +96,7 @@ def test_timeout_invoke(self):
7496
self.assertEqual(b"Task timed out after 1.00 seconds", r.content)
7597

7698
def test_exception_returned(self):
77-
cmd = f"docker run --name exception -d -v {self.path_to_binary}:/local-lambda-runtime-server -p 9002:8080 --entrypoint /local-lambda-runtime-server/aws-lambda-rie {self.image_name} {DEFUALT_1P_ENTRYPOINT} main.exception_handler"
99+
cmd = f"docker run --name exception -d -v {self.path_to_binary}:/local-lambda-runtime-server -p 9002:8080 --entrypoint /local-lambda-runtime-server/aws-lambda-rie {self.image_name} {DEFAULT_1P_ENTRYPOINT} main.exception_handler"
78100

79101
Popen(cmd.split(' ')).communicate()
80102

@@ -108,7 +130,7 @@ def tearDownClass(cls):
108130
Popen(f"docker rmi {cls.image_name}".split(' ')).communicate()
109131

110132
def test_invoke_with_pre_runtime_api_runtime(self):
111-
cmd = f"docker run --name testing -d -v {self.path_to_binary}:/local-lambda-runtime-server -p 9000:8080 --entrypoint /local-lambda-runtime-server/aws-lambda-rie {self.image_name} {DEFUALT_1P_ENTRYPOINT} main.success_handler"
133+
cmd = f"docker run --name testing -d -v {self.path_to_binary}:/local-lambda-runtime-server -p 9000:8080 --entrypoint /local-lambda-runtime-server/aws-lambda-rie {self.image_name} {DEFAULT_1P_ENTRYPOINT} main.success_handler"
112134

113135
Popen(cmd.split(' ')).communicate()
114136

@@ -119,7 +141,7 @@ def test_invoke_with_pre_runtime_api_runtime(self):
119141
self.assertEqual(b'"My lambda ran succesfully"', r.content)
120142

121143
def test_function_name_is_overriden(self):
122-
cmd = f"docker run --name assert-overwritten -d --env AWS_LAMBDA_FUNCTION_NAME=MyCoolName -v {self.path_to_binary}:/local-lambda-runtime-server -p 9009:8080 --entrypoint /local-lambda-runtime-server/aws-lambda-rie {self.image_name} {DEFUALT_1P_ENTRYPOINT} main.assert_env_var_is_overwritten"
144+
cmd = f"docker run --name assert-overwritten -d --env AWS_LAMBDA_FUNCTION_NAME=MyCoolName -v {self.path_to_binary}:/local-lambda-runtime-server -p 9009:8080 --entrypoint /local-lambda-runtime-server/aws-lambda-rie {self.image_name} {DEFAULT_1P_ENTRYPOINT} main.assert_env_var_is_overwritten"
123145

124146
Popen(cmd.split(' ')).communicate()
125147

test/integration/testdata/main.py

+6
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,9 @@ def assert_env_var_is_overwritten(event, context):
2929
raise("Function name was not overwritten")
3030
else:
3131
return "My lambda ran succesfully"
32+
33+
def assert_lambda_arn_in_context(event, context):
34+
if context.invoked_function_arn == f"arn:aws:lambda:us-east-1:012345678912:function:{os.environ.get('AWS_LAMBDA_FUNCTION_NAME', 'test_function')}":
35+
return "My lambda ran succesfully"
36+
else:
37+
raise("Function Arn was not there")

0 commit comments

Comments
 (0)